1 #include <assert.h>
2 #include <stdio.h>
3 #include <dict.h>
4 #include <string.h>
5
6 #define ELEMS 16
7
8 /* Make starwars fans happy */
9 const char keys[][16] = {
10 "It",
11 "is",
12 "a",
13 "period",
14 "of",
15 "civil",
16 "war.",
17 "Rebel",
18 "spaceships,",
19 "striking",
20 "from a",
21 "hidden",
22 "base,",
23 "have",
24 "won",
25 "their..."
26 };
27
28 int values[16];
29
30 const int methods[3] = {
31 AURA_DICT_HASH,
32 AURA_DICT_LIST,
33 AURA_DICT_SORTED_LIST
34 };
35
36 const char *
m2s(int method)37 m2s(int method)
38 {
39 switch(method) {
40 case AURA_DICT_HASH:
41 return "hash";
42 break; /* NOTREACHED */
43 case AURA_DICT_LIST:
44 return "list";
45 break; /* NOTREACHED */
46 case AURA_DICT_SORTED_LIST:
47 return "sorted_list";
48 break; /* NOTREACHED */
49 default:
50 return NULL;
51 break; /* NOTREACHED */
52 }
53 }
54
55 static struct aura_dict *
test_store(int method)56 test_store(int method)
57 {
58 struct aura_dict *d;
59
60 d = aura_dict_new(ELEMS, method);
61 for (int i = 0; i < ELEMS; i++) {
62 values[i] = i;
63 aura_dict_store(d, keys[i], strlen(keys[i]),
64 &values[i], sizeof(int));
65 printf("%s_store: %s=>%d\n",
66 m2s(method), keys[i], values[i]);
67 }
68 return d;
69 }
70
71 static int
test_fetch(struct aura_dict * d,int method)72 test_fetch(struct aura_dict *d, int method)
73 {
74 if (d == NULL)
75 return -1;
76
77 aura_dict_rewind(d);
78 for (int i = 0; i < ELEMS; i++) {
79 int *val = malloc(sizeof(int));
80 size_t len = sizeof(int);
81 aura_dict_fetch(d, keys[i], strlen(keys[i]),
82 (void *)&val, &len);
83 if (*val != values[i]) {
84 fprintf(stderr,
85 "mismatch *val=%d values[%d]=%d\n",
86 *val, i, values[i]);
87 return -1;
88 }
89 printf("%s_fetch: %s=>%d\n", m2s(method), keys[i], *val);
90 free(val);
91 }
92 aura_dict_free(d);
93
94 return 0;
95 }
96
97 int
main(void)98 main(void)
99 {
100 struct aura_dict *d;
101
102 for (int i = 1; i <= 3; i++) {
103 d = test_store(i);
104
105 if ((test_fetch(d, i)) == -1) {
106 return 1;
107 }
108 }
109
110 return 0;
111 }
112