simple search.h tests
[libc-test] / src / functional / search_hsearch.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include <search.h>
4 #include <errno.h>
5 #include "test.h"
6
7 #define set(k,v) do{ \
8         e = hsearch((ENTRY){.key = k, .data = (void*)v}, ENTER); \
9         if (!e || strcmp(e->key, k) != 0) \
10                 t_error("hsearch ENTER %s %d failed\n", k, v); \
11 }while(0)
12
13 #define get(k) hsearch((ENTRY){.key = k, .data = 0}, FIND)
14
15 int main()
16 {
17         ENTRY *e;
18
19         if (hcreate(-1) || errno != ENOMEM)
20                 t_error("hcreate((size_t)-1) should fail with ENOMEM got %s\n", strerror(errno));
21         if (!hcreate(13))
22                 t_error("hcreate(13) failed\n");
23         set("", 0);
24         set("a", 1);
25         set("b", 2);
26         set("abc", 3);
27         set("cd", 4);
28         set("e", 5);
29         set("ef", 6);
30         set("g", 7);
31         set("h", 8);
32         set("iiiiiiiiii", 9);
33         if (!get("a"))
34                 t_error("hsearch FIND a failed\n");
35         if (get("c"))
36                 t_error("hsearch FIND c should fail\n");
37         set("g", 10);
38         if (e && (int)(e->data) != 7)
39                 t_error("hsearch ENTER g 10 returned data %d, wanted 7\n", (int)(e->data));
40         set("g", 10);
41         if (e && (int)(e->data) != 7)
42                 t_error("hsearch ENTER g 10 returned data %d, wanted 7\n", (int)(e->data));
43         set("j", 10);
44         if (e && (int)(e->data) != 10)
45                 t_error("hsearch ENTER j 10 returned data %d, wanted 10\n", (int)(e->data));
46         hdestroy();
47         return t_status;
48 }