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