ckh.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #include "test/jemalloc_test.h"
  2. TEST_BEGIN(test_new_delete) {
  3. tsd_t *tsd;
  4. ckh_t ckh;
  5. tsd = tsd_fetch();
  6. assert_false(ckh_new(tsd, &ckh, 2, ckh_string_hash,
  7. ckh_string_keycomp), "Unexpected ckh_new() error");
  8. ckh_delete(tsd, &ckh);
  9. assert_false(ckh_new(tsd, &ckh, 3, ckh_pointer_hash,
  10. ckh_pointer_keycomp), "Unexpected ckh_new() error");
  11. ckh_delete(tsd, &ckh);
  12. }
  13. TEST_END
  14. TEST_BEGIN(test_count_insert_search_remove) {
  15. tsd_t *tsd;
  16. ckh_t ckh;
  17. const char *strs[] = {
  18. "a string",
  19. "A string",
  20. "a string.",
  21. "A string."
  22. };
  23. const char *missing = "A string not in the hash table.";
  24. size_t i;
  25. tsd = tsd_fetch();
  26. assert_false(ckh_new(tsd, &ckh, 2, ckh_string_hash,
  27. ckh_string_keycomp), "Unexpected ckh_new() error");
  28. assert_zu_eq(ckh_count(&ckh), 0,
  29. "ckh_count() should return %zu, but it returned %zu", ZU(0),
  30. ckh_count(&ckh));
  31. /* Insert. */
  32. for (i = 0; i < sizeof(strs)/sizeof(const char *); i++) {
  33. ckh_insert(tsd, &ckh, strs[i], strs[i]);
  34. assert_zu_eq(ckh_count(&ckh), i+1,
  35. "ckh_count() should return %zu, but it returned %zu", i+1,
  36. ckh_count(&ckh));
  37. }
  38. /* Search. */
  39. for (i = 0; i < sizeof(strs)/sizeof(const char *); i++) {
  40. union {
  41. void *p;
  42. const char *s;
  43. } k, v;
  44. void **kp, **vp;
  45. const char *ks, *vs;
  46. kp = (i & 1) ? &k.p : NULL;
  47. vp = (i & 2) ? &v.p : NULL;
  48. k.p = NULL;
  49. v.p = NULL;
  50. assert_false(ckh_search(&ckh, strs[i], kp, vp),
  51. "Unexpected ckh_search() error");
  52. ks = (i & 1) ? strs[i] : (const char *)NULL;
  53. vs = (i & 2) ? strs[i] : (const char *)NULL;
  54. assert_ptr_eq((void *)ks, (void *)k.s, "Key mismatch, i=%zu",
  55. i);
  56. assert_ptr_eq((void *)vs, (void *)v.s, "Value mismatch, i=%zu",
  57. i);
  58. }
  59. assert_true(ckh_search(&ckh, missing, NULL, NULL),
  60. "Unexpected ckh_search() success");
  61. /* Remove. */
  62. for (i = 0; i < sizeof(strs)/sizeof(const char *); i++) {
  63. union {
  64. void *p;
  65. const char *s;
  66. } k, v;
  67. void **kp, **vp;
  68. const char *ks, *vs;
  69. kp = (i & 1) ? &k.p : NULL;
  70. vp = (i & 2) ? &v.p : NULL;
  71. k.p = NULL;
  72. v.p = NULL;
  73. assert_false(ckh_remove(tsd, &ckh, strs[i], kp, vp),
  74. "Unexpected ckh_remove() error");
  75. ks = (i & 1) ? strs[i] : (const char *)NULL;
  76. vs = (i & 2) ? strs[i] : (const char *)NULL;
  77. assert_ptr_eq((void *)ks, (void *)k.s, "Key mismatch, i=%zu",
  78. i);
  79. assert_ptr_eq((void *)vs, (void *)v.s, "Value mismatch, i=%zu",
  80. i);
  81. assert_zu_eq(ckh_count(&ckh),
  82. sizeof(strs)/sizeof(const char *) - i - 1,
  83. "ckh_count() should return %zu, but it returned %zu",
  84. sizeof(strs)/sizeof(const char *) - i - 1,
  85. ckh_count(&ckh));
  86. }
  87. ckh_delete(tsd, &ckh);
  88. }
  89. TEST_END
  90. TEST_BEGIN(test_insert_iter_remove) {
  91. #define NITEMS ZU(1000)
  92. tsd_t *tsd;
  93. ckh_t ckh;
  94. void **p[NITEMS];
  95. void *q, *r;
  96. size_t i;
  97. tsd = tsd_fetch();
  98. assert_false(ckh_new(tsd, &ckh, 2, ckh_pointer_hash,
  99. ckh_pointer_keycomp), "Unexpected ckh_new() error");
  100. for (i = 0; i < NITEMS; i++) {
  101. p[i] = mallocx(i+1, 0);
  102. assert_ptr_not_null(p[i], "Unexpected mallocx() failure");
  103. }
  104. for (i = 0; i < NITEMS; i++) {
  105. size_t j;
  106. for (j = i; j < NITEMS; j++) {
  107. assert_false(ckh_insert(tsd, &ckh, p[j], p[j]),
  108. "Unexpected ckh_insert() failure");
  109. assert_false(ckh_search(&ckh, p[j], &q, &r),
  110. "Unexpected ckh_search() failure");
  111. assert_ptr_eq(p[j], q, "Key pointer mismatch");
  112. assert_ptr_eq(p[j], r, "Value pointer mismatch");
  113. }
  114. assert_zu_eq(ckh_count(&ckh), NITEMS,
  115. "ckh_count() should return %zu, but it returned %zu",
  116. NITEMS, ckh_count(&ckh));
  117. for (j = i + 1; j < NITEMS; j++) {
  118. assert_false(ckh_search(&ckh, p[j], NULL, NULL),
  119. "Unexpected ckh_search() failure");
  120. assert_false(ckh_remove(tsd, &ckh, p[j], &q, &r),
  121. "Unexpected ckh_remove() failure");
  122. assert_ptr_eq(p[j], q, "Key pointer mismatch");
  123. assert_ptr_eq(p[j], r, "Value pointer mismatch");
  124. assert_true(ckh_search(&ckh, p[j], NULL, NULL),
  125. "Unexpected ckh_search() success");
  126. assert_true(ckh_remove(tsd, &ckh, p[j], &q, &r),
  127. "Unexpected ckh_remove() success");
  128. }
  129. {
  130. bool seen[NITEMS];
  131. size_t tabind;
  132. memset(seen, 0, sizeof(seen));
  133. for (tabind = 0; !ckh_iter(&ckh, &tabind, &q, &r);) {
  134. size_t k;
  135. assert_ptr_eq(q, r, "Key and val not equal");
  136. for (k = 0; k < NITEMS; k++) {
  137. if (p[k] == q) {
  138. assert_false(seen[k],
  139. "Item %zu already seen", k);
  140. seen[k] = true;
  141. break;
  142. }
  143. }
  144. }
  145. for (j = 0; j < i + 1; j++) {
  146. assert_true(seen[j], "Item %zu not seen", j);
  147. }
  148. for (; j < NITEMS; j++) {
  149. assert_false(seen[j], "Item %zu seen", j);
  150. }
  151. }
  152. }
  153. for (i = 0; i < NITEMS; i++) {
  154. assert_false(ckh_search(&ckh, p[i], NULL, NULL),
  155. "Unexpected ckh_search() failure");
  156. assert_false(ckh_remove(tsd, &ckh, p[i], &q, &r),
  157. "Unexpected ckh_remove() failure");
  158. assert_ptr_eq(p[i], q, "Key pointer mismatch");
  159. assert_ptr_eq(p[i], r, "Value pointer mismatch");
  160. assert_true(ckh_search(&ckh, p[i], NULL, NULL),
  161. "Unexpected ckh_search() success");
  162. assert_true(ckh_remove(tsd, &ckh, p[i], &q, &r),
  163. "Unexpected ckh_remove() success");
  164. dallocx(p[i], 0);
  165. }
  166. assert_zu_eq(ckh_count(&ckh), 0,
  167. "ckh_count() should return %zu, but it returned %zu",
  168. ZU(0), ckh_count(&ckh));
  169. ckh_delete(tsd, &ckh);
  170. #undef NITEMS
  171. }
  172. TEST_END
  173. int
  174. main(void) {
  175. return test(
  176. test_new_delete,
  177. test_count_insert_search_remove,
  178. test_insert_iter_remove);
  179. }