2
0

ckh.c 5.3 KB

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