1 #include "test/jemalloc_test.h"
3 TEST_BEGIN(test_new_delete
)
10 assert_false(ckh_new(tsd
, &ckh
, 2, ckh_string_hash
, ckh_string_keycomp
),
11 "Unexpected ckh_new() error");
12 ckh_delete(tsd
, &ckh
);
14 assert_false(ckh_new(tsd
, &ckh
, 3, ckh_pointer_hash
,
15 ckh_pointer_keycomp
), "Unexpected ckh_new() error");
16 ckh_delete(tsd
, &ckh
);
20 TEST_BEGIN(test_count_insert_search_remove
)
24 const char *strs
[] = {
30 const char *missing
= "A string not in the hash table.";
35 assert_false(ckh_new(tsd
, &ckh
, 2, ckh_string_hash
, ckh_string_keycomp
),
36 "Unexpected ckh_new() error");
37 assert_zu_eq(ckh_count(&ckh
), 0,
38 "ckh_count() should return %zu, but it returned %zu", ZU(0),
42 for (i
= 0; i
< sizeof(strs
)/sizeof(const char *); i
++) {
43 ckh_insert(tsd
, &ckh
, strs
[i
], strs
[i
]);
44 assert_zu_eq(ckh_count(&ckh
), i
+1,
45 "ckh_count() should return %zu, but it returned %zu", i
+1,
50 for (i
= 0; i
< sizeof(strs
)/sizeof(const char *); i
++) {
58 kp
= (i
& 1) ? &k
.p
: NULL
;
59 vp
= (i
& 2) ? &v
.p
: NULL
;
62 assert_false(ckh_search(&ckh
, strs
[i
], kp
, vp
),
63 "Unexpected ckh_search() error");
65 ks
= (i
& 1) ? strs
[i
] : (const char *)NULL
;
66 vs
= (i
& 2) ? strs
[i
] : (const char *)NULL
;
67 assert_ptr_eq((void *)ks
, (void *)k
.s
, "Key mismatch, i=%zu",
69 assert_ptr_eq((void *)vs
, (void *)v
.s
, "Value mismatch, i=%zu",
72 assert_true(ckh_search(&ckh
, missing
, NULL
, NULL
),
73 "Unexpected ckh_search() success");
76 for (i
= 0; i
< sizeof(strs
)/sizeof(const char *); i
++) {
84 kp
= (i
& 1) ? &k
.p
: NULL
;
85 vp
= (i
& 2) ? &v
.p
: NULL
;
88 assert_false(ckh_remove(tsd
, &ckh
, strs
[i
], kp
, vp
),
89 "Unexpected ckh_remove() error");
91 ks
= (i
& 1) ? strs
[i
] : (const char *)NULL
;
92 vs
= (i
& 2) ? strs
[i
] : (const char *)NULL
;
93 assert_ptr_eq((void *)ks
, (void *)k
.s
, "Key mismatch, i=%zu",
95 assert_ptr_eq((void *)vs
, (void *)v
.s
, "Value mismatch, i=%zu",
97 assert_zu_eq(ckh_count(&ckh
),
98 sizeof(strs
)/sizeof(const char *) - i
- 1,
99 "ckh_count() should return %zu, but it returned %zu",
100 sizeof(strs
)/sizeof(const char *) - i
- 1,
104 ckh_delete(tsd
, &ckh
);
108 TEST_BEGIN(test_insert_iter_remove
)
110 #define NITEMS ZU(1000)
119 assert_false(ckh_new(tsd
, &ckh
, 2, ckh_pointer_hash
,
120 ckh_pointer_keycomp
), "Unexpected ckh_new() error");
122 for (i
= 0; i
< NITEMS
; i
++) {
123 p
[i
] = mallocx(i
+1, 0);
124 assert_ptr_not_null(p
[i
], "Unexpected mallocx() failure");
127 for (i
= 0; i
< NITEMS
; i
++) {
130 for (j
= i
; j
< NITEMS
; j
++) {
131 assert_false(ckh_insert(tsd
, &ckh
, p
[j
], p
[j
]),
132 "Unexpected ckh_insert() failure");
133 assert_false(ckh_search(&ckh
, p
[j
], &q
, &r
),
134 "Unexpected ckh_search() failure");
135 assert_ptr_eq(p
[j
], q
, "Key pointer mismatch");
136 assert_ptr_eq(p
[j
], r
, "Value pointer mismatch");
139 assert_zu_eq(ckh_count(&ckh
), NITEMS
,
140 "ckh_count() should return %zu, but it returned %zu",
141 NITEMS
, ckh_count(&ckh
));
143 for (j
= i
+ 1; j
< NITEMS
; j
++) {
144 assert_false(ckh_search(&ckh
, p
[j
], NULL
, NULL
),
145 "Unexpected ckh_search() failure");
146 assert_false(ckh_remove(tsd
, &ckh
, p
[j
], &q
, &r
),
147 "Unexpected ckh_remove() failure");
148 assert_ptr_eq(p
[j
], q
, "Key pointer mismatch");
149 assert_ptr_eq(p
[j
], r
, "Value pointer mismatch");
150 assert_true(ckh_search(&ckh
, p
[j
], NULL
, NULL
),
151 "Unexpected ckh_search() success");
152 assert_true(ckh_remove(tsd
, &ckh
, p
[j
], &q
, &r
),
153 "Unexpected ckh_remove() success");
160 memset(seen
, 0, sizeof(seen
));
162 for (tabind
= 0; !ckh_iter(&ckh
, &tabind
, &q
, &r
);) {
165 assert_ptr_eq(q
, r
, "Key and val not equal");
167 for (k
= 0; k
< NITEMS
; k
++) {
169 assert_false(seen
[k
],
170 "Item %zu already seen", k
);
177 for (j
= 0; j
< i
+ 1; j
++)
178 assert_true(seen
[j
], "Item %zu not seen", j
);
179 for (; j
< NITEMS
; j
++)
180 assert_false(seen
[j
], "Item %zu seen", j
);
184 for (i
= 0; i
< NITEMS
; i
++) {
185 assert_false(ckh_search(&ckh
, p
[i
], NULL
, NULL
),
186 "Unexpected ckh_search() failure");
187 assert_false(ckh_remove(tsd
, &ckh
, p
[i
], &q
, &r
),
188 "Unexpected ckh_remove() failure");
189 assert_ptr_eq(p
[i
], q
, "Key pointer mismatch");
190 assert_ptr_eq(p
[i
], r
, "Value pointer mismatch");
191 assert_true(ckh_search(&ckh
, p
[i
], NULL
, NULL
),
192 "Unexpected ckh_search() success");
193 assert_true(ckh_remove(tsd
, &ckh
, p
[i
], &q
, &r
),
194 "Unexpected ckh_remove() success");
198 assert_zu_eq(ckh_count(&ckh
), 0,
199 "ckh_count() should return %zu, but it returned %zu",
200 ZU(0), ckh_count(&ckh
));
201 ckh_delete(tsd
, &ckh
);
212 test_count_insert_search_remove
,
213 test_insert_iter_remove
));