]> git.ozlabs.org Git - ccan/blob - ccan/asearch/test/run.c
asearch: Add context pointer to asearch comparison callback
[ccan] / ccan / asearch / test / run.c
1 #include <ccan/asearch/asearch.h>
2 #include <ccan/array_size/array_size.h>
3 #include <ccan/tap/tap.h>
4 #include <limits.h>
5
6 #include <ccan/asearch/asearch.c>
7
8 static int test_cmp(const int *key, const int *elt, void *ctx)
9 {
10         if (*key < *elt)
11                 return -1;
12         else if (*key > *elt)
13                 return 1;
14         return 0;
15 }
16
17 int main(void)
18 {
19         const int arr[] = { INT_MIN, 0, 1, 2, 3, 4, 5, 6, INT_MAX };
20         unsigned int start, num, i, total = 0;
21         int key;
22
23         plan_tests(285);
24
25         for (start = 0; start < ARRAY_SIZE(arr); start++) {
26                 for (num = 0; num < ARRAY_SIZE(arr) - start; num++) {
27                         key = 7;
28                         ok1(asearch(&key, &arr[start], num, test_cmp,
29                                     NULL) == NULL);
30                         total++;
31                         for (i = start; i < start+num; i++) {
32                                 const int *ret;
33                                 key = arr[i];
34                                 ret = asearch(&key, &arr[start], num,
35                                               test_cmp, NULL);
36                                 ok1(ret);
37                                 ok1(ret && *ret == key);
38                                 total++;
39                         }
40                 }
41         }
42         diag("Tested %u searches\n", total);
43         return exit_status();
44 }