1 /*	$OpenBSD: rde_sets_test.c,v 1.7 2019/12/17 11:57:16 claudio Exp $ */
2 
3 /*
4  * Copyright (c) 2018 Claudio Jeker <claudio@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 #include <sys/types.h>
19 #include <sys/queue.h>
20 
21 #include <err.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 
25 #include "rde.h"
26 
27 struct rde_memstats rdemem;
28 
29 u_int32_t va[] = { 19, 14, 32, 76, 125 };
30 u_int32_t vaa[] = { 125, 14, 76, 32, 19 };
31 u_int32_t vb[] = { 256, 1024, 512, 4096, 2048, 512 };
32 u_int32_t vc[] = { 42 };
33 
34 struct as_set_head as_sets;
35 
36 static struct as_set *
37 build_set(const char *name, u_int32_t *mem, size_t nmemb, size_t initial)
38 {
39 	struct as_set *a;
40 
41 	a = as_sets_new(&as_sets, name, initial, sizeof(*mem));
42 	if (a == NULL)
43 		err(1, "as_set_new %s", name);
44 	if (set_add(a->set, mem, nmemb) != 0)
45 		err(1, "as_set_add %s", name);
46 	set_prep(a->set);
47 
48 	return a;
49 }
50 
51 int
52 main(int argc, char **argv)
53 {
54 	struct as_set *a, *aa, *b, *c, *empty;
55 	size_t i;
56 
57 	SIMPLEQ_INIT(&as_sets);
58 
59 	a = build_set("a", va, sizeof(va) / sizeof(va[0]),
60 	    sizeof(va) / sizeof(va[0]));
61 	aa = build_set("aa", vaa, sizeof(vaa) / sizeof(vaa[0]), 0);
62 	b = build_set("b", vb, sizeof(vb) / sizeof(vb[0]), 1);
63 	c = build_set("c", vc, sizeof(vc) / sizeof(vc[0]), 1);
64 	empty = build_set("empty", NULL, 0, 0);
65 
66 	if (!set_equal(a->set, a->set))
67 		errx(1, "set_equal(a, a) non equal");
68 	if (!set_equal(a->set, aa->set))
69 		errx(1, "set_equal(a, aa) non equal");
70 	if (set_equal(a->set, b->set))
71 		errx(1, "set_equal(a, b) equal");
72 
73 	for (i = 0; i < sizeof(va) / sizeof(va[0]); i++)
74 		if (!as_set_match(a, va[i]))
75 			errx(1, "as_set_match(a, %u) failed to match", va[i]);
76 	for (i = 0; i < sizeof(vb) / sizeof(vb[0]); i++)
77 		if (as_set_match(a, vb[i]))
78 			errx(1, "as_set_match(a, %u) matched but should not",
79 			    vb[i]);
80 	if (!as_set_match(c, 42))
81 		errx(1, "as_set_match(c, %u) failed to match", 42);
82 	if (as_set_match(c, 7))
83 		errx(1, "as_set_match(c, %u) matched but should not", 7);
84 
85 	if (!set_equal(empty->set, empty->set))
86 		errx(1, "set_equal(empty, empty) non equal");
87 	if (as_set_match(empty, 42))
88 		errx(1, "as_set_match(empty, %u) matched but should not", 42);
89 
90 	as_sets_free(&as_sets);
91 
92 	printf("OK\n");
93 	return 0;
94 }
95