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