simple search.h tests
[libc-test] / src / functional / search_lsearch.c
1 #include <string.h>
2 #include <search.h>
3 #include "test.h"
4
5 #define W 80
6 static char tab[100][W];
7 static size_t nel;
8
9 #define set(k) do{ \
10         char *r = lsearch(k, tab, &nel, W, (int(*)(const void*,const void*))strcmp); \
11         if (strcmp(r, k) != 0) \
12                 t_error("lsearch %s failed\n", #k); \
13 }while(0)
14
15 #define get(k) lfind(k, tab, &nel, W, (int(*)(const void*,const void*))strcmp)
16
17 int main()
18 {
19         size_t n;
20
21         set("");
22         set("a");
23         set("b");
24         set("abc");
25         set("cd");
26         set("e");
27         set("ef");
28         set("g");
29         set("h");
30         set("iiiiiiiiii");
31         if (!get("a"))
32                 t_error("lfind a failed\n");
33         if (get("c"))
34                 t_error("lfind c should fail\n");
35         n = nel;
36         set("g");
37         if (nel != n)
38                 t_error("lsearch g should not modify the table size (%d, was %d)\n", nel, n);
39         n = nel;
40         set("j");
41         if (nel != n+1)
42                 t_error("lsearch j should increase the table size (%d, was %d)\n", nel, n);
43         return t_status;
44 }