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>
10 #define ARRAY_SIZE(array) (sizeof(array) / sizeof(*(array)))
18 struct foo foo_structs[] = {
26 struct foo *foo_base[ARRAY_SIZE(foo_structs)];
27 const unsigned int foo_count = ARRAY_SIZE(foo_structs);
29 static void init_foo_pointers(void)
33 for (i = 0; i < foo_count; i++)
34 foo_base[i] = &foo_structs[i];
37 /* Make sure forward declarations work */
38 btree_search_proto order_by_string, order_by_number;
40 static void test_order_by_string(void)
45 unsigned int expect_offset;
48 {"anchovies", 0, 0, 0},
49 {"anchovies", 1, 0, 0},
54 {"blueberry", 0, 3, 0},
55 {"blueberry", 1, 3, 0},
56 {"doughnut", 0, 4, 1},
57 {"doughnut", 1, 5, 1},
61 for (i=0; i<ARRAY_SIZE(test); i++) {
62 struct foo foo = {test[i].key, 0};
66 offset = order_by_string(&foo, (void*)foo_base, foo_count,
69 ok1(offset == test[i].expect_offset && found == test[i].expect_found);
73 static void test_empty(void)
77 struct foo key = {"apple", -1};
79 offset = order_by_string(&key, NULL, 0, 0, &found);
82 offset = order_by_string(&key, NULL, 0, 1, &found);
85 offset = order_by_number(&key, NULL, 0, 0, &found);
88 offset = order_by_number(&key, NULL, 0, 1, &found);
92 static void test_order_by_number(void)
97 unsigned int expect_offset;
123 for (i=0; i<ARRAY_SIZE(test); i++) {
124 struct foo foo = {"", test[i].key};
128 offset = order_by_number(&foo, (void*)foo_base, foo_count,
131 ok1(offset == test[i].expect_offset && found == test[i].expect_found);
140 test_order_by_string();
141 test_order_by_number();
144 return exit_status();
147 btree_search_implement (
150 int c = strcmp(a->string, b->string),
155 btree_search_implement (
159 a->number == b->number,
160 a->number < b->number