]> git.ozlabs.org Git - ccan/blob - ccan/btree/test/run-search-implement.c
Joey's btree module.
[ccan] / ccan / btree / test / run-search-implement.c
1 /* Include the main header first, to test it works */
2 #include <ccan/btree/btree.h>
3 /* Include the C files directly. */
4 #include <ccan/btree/btree.c>
5 #include <ccan/tap/tap.h>
6
7 #include <string.h>
8
9 #ifndef ARRAY_SIZE
10 #define ARRAY_SIZE(array) (sizeof(array) / sizeof(*(array)))
11 #endif
12
13 struct foo {
14         const char *string;
15         int number;
16 };
17
18 struct foo foo_structs[] = {
19         {"apple", 1},
20         {"banana", 2},
21         {"banana", 4},
22         {"cherry", 4},
23         {"doughnut", 5},
24 };
25
26 struct foo *foo_base[ARRAY_SIZE(foo_structs)];
27 const unsigned int foo_count = ARRAY_SIZE(foo_structs);
28
29 static void init_foo_pointers(void)
30 {
31         unsigned int i;
32         
33         for (i = 0; i < foo_count; i++)
34                 foo_base[i] = &foo_structs[i];
35 }
36
37 /* Make sure forward declarations work */
38 btree_search_proto order_by_string, order_by_number;
39
40 static void test_order_by_string(void)
41 {
42         struct {
43                 const char *key;
44                 int lr;
45                 unsigned int expect_offset;
46                 int expect_found;
47         } test[] = {
48                 {"anchovies", 0, 0, 0},
49                 {"anchovies", 1, 0, 0},
50                 {"apple", 0, 0, 1},
51                 {"apple", 1, 1, 1},
52                 {"banana", 0, 1, 1},
53                 {"banana", 1, 3, 1},
54                 {"blueberry", 0, 3, 0},
55                 {"blueberry", 1, 3, 0},
56                 {"doughnut", 0, 4, 1},
57                 {"doughnut", 1, 5, 1},
58         };
59         
60         size_t i;
61         for (i=0; i<ARRAY_SIZE(test); i++) {
62                 struct foo foo = {test[i].key, 0};
63                 unsigned int offset;
64                 int found = 0;
65                 
66                 offset = order_by_string(&foo, (void*)foo_base, foo_count,
67                                                 test[i].lr, &found);
68                 
69                 ok1(offset == test[i].expect_offset && found == test[i].expect_found);
70         }
71 }
72
73 static void test_empty(void)
74 {
75         unsigned int offset;
76         int found;
77         struct foo key = {"apple", -1};
78         
79         offset = order_by_string(&key, NULL, 0, 0, &found);
80         ok1(offset == 0);
81         
82         offset = order_by_string(&key, NULL, 0, 1, &found);
83         ok1(offset == 0);
84         
85         offset = order_by_number(&key, NULL, 0, 0, &found);
86         ok1(offset == 0);
87         
88         offset = order_by_number(&key, NULL, 0, 1, &found);
89         ok1(offset == 0);
90 }
91
92 static void test_order_by_number(void)
93 {
94         struct {
95                 int key;
96                 int lr;
97                 unsigned int expect_offset;
98                 int expect_found;
99         } test[] = {
100                 {-2, 0, 0, 0},
101                 {-2, 1, 0, 0},
102                 {-1, 0, 0, 0},
103                 {-1, 1, 0, 0},
104                 {0, 0, 0, 0},
105                 {0, 1, 0, 0},
106                 {1, 0, 0, 1},
107                 {1, 1, 1, 1},
108                 {2, 0, 1, 1},
109                 {2, 1, 2, 1},
110                 {4, 0, 2, 1},
111                 {4, 1, 4, 1},
112                 {3, 0, 2, 0},
113                 {3, 1, 2, 0},
114                 {5, 0, 4, 1},
115                 {5, 1, 5, 1},
116                 {6, 0, 5, 0},
117                 {6, 1, 5, 0},
118                 {7, 0, 5, 0},
119                 {7, 1, 5, 0},
120         };
121         
122         size_t i;
123         for (i=0; i<ARRAY_SIZE(test); i++) {
124                 struct foo foo = {"", test[i].key};
125                 unsigned int offset;
126                 int found = 0;
127                 
128                 offset = order_by_number(&foo, (void*)foo_base, foo_count,
129                                                 test[i].lr, &found);
130                 
131                 ok1(offset == test[i].expect_offset && found == test[i].expect_found);
132         }
133 }
134
135 int main(void)
136 {
137         plan_tests(34);
138         init_foo_pointers();
139         
140         test_order_by_string();
141         test_order_by_number();
142         test_empty();
143         
144         return exit_status();
145 }
146
147 btree_search_implement (
148         order_by_string,
149         const struct foo *,
150         int c = strcmp(a->string, b->string),
151         c == 0,
152         c < 0
153 )
154
155 btree_search_implement (
156         order_by_number,
157         const struct foo *,
158         ,
159         a->number == b->number,
160         a->number < b->number
161 )