]> git.ozlabs.org Git - ccan/blob - ccan/sparse_bsearch/test/run.c
tdb2: cleanups for tdbtorture, add more debugging and -S flag.
[ccan] / ccan / sparse_bsearch / test / run.c
1 /* Include the main header first, to test it works */
2 #include <ccan/sparse_bsearch/sparse_bsearch.h>
3 /* Include the C files directly. */
4 #include <ccan/sparse_bsearch/sparse_bsearch.c>
5 #include <ccan/tap/tap.h>
6
7 static int cmp(const unsigned int *a, const unsigned int *b)
8 {
9         return (*a) - (*b);
10 }
11
12 static bool valid(const unsigned int *a)
13 {
14         return *a != 0;
15 }
16
17 int main(void)
18 {
19         unsigned int i, j, master[4] = { 1, 2, 3, 4 }, arr[4];
20
21         plan_tests((1 << 4) * 4+ (1 << 3) * 3);
22
23         /* We test all possibilities of an even and an odd array len. */
24         for (i = 0; i < (1 << 4); i++) {
25                 /* Setup partial arr[] */
26                 for (j = 0; j < 4; j++) {
27                         if (i & (1 << j))
28                                 arr[j] = master[j];
29                         else
30                                 arr[j] = 0;
31                 }
32
33                 for (j = 1; j <= 4; j++) {
34                         unsigned int *ptr;
35                         ptr = sparse_bsearch(&j, arr, 4, cmp, valid);
36                         if (i & (1 << (j-1)))
37                                 ok1(ptr && *ptr == j);
38                         else
39                                 ok1(!ptr);
40                 }
41         }
42
43         for (i = 0; i < (1 << 3); i++) {
44                 /* Setup partial arr[] */
45                 for (j = 0; j < 3; j++) {
46                         if (i & (1 << j))
47                                 arr[j] = master[j];
48                         else
49                                 arr[j] = 0;
50                 }
51
52                 for (j = 1; j <= 3; j++) {
53                         unsigned int *ptr;
54                         ptr = sparse_bsearch(&j, arr, 3, cmp, valid);
55                         if (i & (1 << (j-1)))
56                                 ok1(ptr && *ptr == j);
57                         else
58                                 ok1(!ptr);
59                 }
60         }
61
62         /* This exits depending on whether all tests passed */
63         return exit_status();
64 }