ckh.c 5.2 KB

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