run.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #include <ccan/htable/htable.h>
  2. #include <ccan/htable/htable.c>
  3. #include <ccan/tap/tap.h>
  4. #include <stdbool.h>
  5. #include <string.h>
  6. #define NUM_BITS 7
  7. #define NUM_VALS (1 << NUM_BITS)
  8. /* We use the number divided by two as the hash (for lots of
  9. collisions), plus set all the higher bits so we can detect if they
  10. don't get masked out. */
  11. static size_t hash(const void *elem, void *unused UNNEEDED)
  12. {
  13. size_t h = *(uint64_t *)elem / 2;
  14. h |= -1UL << NUM_BITS;
  15. return h;
  16. }
  17. static bool objcmp(const void *htelem, void *cmpdata)
  18. {
  19. return *(uint64_t *)htelem == *(uint64_t *)cmpdata;
  20. }
  21. static void add_vals(struct htable *ht,
  22. const uint64_t val[],
  23. unsigned int off, unsigned int num)
  24. {
  25. uint64_t i;
  26. for (i = off; i < off+num; i++) {
  27. if (htable_get(ht, hash(&i, NULL), objcmp, &i)) {
  28. fail("%llu already in hash", (long long)i);
  29. return;
  30. }
  31. htable_add(ht, hash(&val[i], NULL), &val[i]);
  32. if (htable_get(ht, hash(&i, NULL), objcmp, &i) != &val[i]) {
  33. fail("%llu not added to hash", (long long)i);
  34. return;
  35. }
  36. }
  37. pass("Added %llu numbers to hash", (long long)i);
  38. }
  39. #if 0
  40. static void refill_vals(struct htable *ht,
  41. const uint64_t val[], unsigned int num)
  42. {
  43. uint64_t i;
  44. for (i = 0; i < num; i++) {
  45. if (htable_get(ht, hash(&i, NULL), objcmp, &i))
  46. continue;
  47. htable_add(ht, hash(&val[i], NULL), &val[i]);
  48. }
  49. }
  50. #endif
  51. static void find_vals(struct htable *ht,
  52. const uint64_t val[], unsigned int num)
  53. {
  54. uint64_t i;
  55. for (i = 0; i < num; i++) {
  56. if (htable_get(ht, hash(&i, NULL), objcmp, &i) != &val[i]) {
  57. fail("%llu not found in hash", (long long)i);
  58. return;
  59. }
  60. }
  61. pass("Found %llu numbers in hash", (long long)i);
  62. }
  63. static void del_vals(struct htable *ht,
  64. const uint64_t val[], unsigned int num)
  65. {
  66. uint64_t i;
  67. for (i = 0; i < num; i++) {
  68. if (!htable_del(ht, hash(&val[i], NULL), &val[i])) {
  69. fail("%llu not deleted from hash", (long long)i);
  70. return;
  71. }
  72. }
  73. pass("Deleted %llu numbers in hash", (long long)i);
  74. }
  75. static bool check_mask(struct htable *ht, uint64_t val[], unsigned num)
  76. {
  77. uint64_t i;
  78. for (i = 0; i < num; i++) {
  79. if (((uintptr_t)&val[i] & ht->common_mask) != ht->common_bits)
  80. return false;
  81. }
  82. return true;
  83. }
  84. int main(void)
  85. {
  86. unsigned int i, weight;
  87. uintptr_t perfect_bit;
  88. struct htable ht;
  89. uint64_t val[NUM_VALS];
  90. uint64_t dne;
  91. void *p;
  92. struct htable_iter iter;
  93. plan_tests(36);
  94. for (i = 0; i < NUM_VALS; i++)
  95. val[i] = i;
  96. dne = i;
  97. htable_init(&ht, hash, NULL);
  98. ok1(ht.max == 0);
  99. ok1(ht.bits == 0);
  100. /* We cannot find an entry which doesn't exist. */
  101. ok1(!htable_get(&ht, hash(&dne, NULL), objcmp, &dne));
  102. /* This should increase it once. */
  103. add_vals(&ht, val, 0, 1);
  104. ok1(ht.bits == 1);
  105. ok1(ht.max == 1);
  106. weight = 0;
  107. for (i = 0; i < sizeof(ht.common_mask) * CHAR_BIT; i++) {
  108. if (ht.common_mask & ((uintptr_t)1 << i)) {
  109. weight++;
  110. }
  111. }
  112. /* Only one bit should be clear. */
  113. ok1(weight == i-1);
  114. /* Mask should be set. */
  115. ok1(check_mask(&ht, val, 1));
  116. /* This should increase it again. */
  117. add_vals(&ht, val, 1, 1);
  118. ok1(ht.bits == 2);
  119. ok1(ht.max == 3);
  120. /* Mask should be set. */
  121. ok1(ht.common_mask != 0);
  122. ok1(ht.common_mask != -1);
  123. ok1(check_mask(&ht, val, 2));
  124. /* Now do the rest. */
  125. add_vals(&ht, val, 2, NUM_VALS - 2);
  126. /* Find all. */
  127. find_vals(&ht, val, NUM_VALS);
  128. ok1(!htable_get(&ht, hash(&dne, NULL), objcmp, &dne));
  129. /* Walk once, should get them all. */
  130. i = 0;
  131. for (p = htable_first(&ht,&iter); p; p = htable_next(&ht, &iter))
  132. i++;
  133. ok1(i == NUM_VALS);
  134. i = 0;
  135. for (p = htable_prev(&ht, &iter); p; p = htable_prev(&ht, &iter))
  136. i++;
  137. ok1(i == NUM_VALS);
  138. /* Delete all. */
  139. del_vals(&ht, val, NUM_VALS);
  140. ok1(!htable_get(&ht, hash(&val[0], NULL), objcmp, &val[0]));
  141. /* Worst case, a "pointer" which doesn't have any matching bits. */
  142. htable_add(&ht, 0, (void *)~(uintptr_t)&val[NUM_VALS-1]);
  143. htable_add(&ht, hash(&val[NUM_VALS-1], NULL), &val[NUM_VALS-1]);
  144. ok1(ht.common_mask == 0);
  145. ok1(ht.common_bits == 0);
  146. /* Get rid of bogus pointer before we trip over it! */
  147. htable_del(&ht, 0, (void *)~(uintptr_t)&val[NUM_VALS-1]);
  148. /* Add the rest. */
  149. add_vals(&ht, val, 0, NUM_VALS-1);
  150. /* Check we can find them all. */
  151. find_vals(&ht, val, NUM_VALS);
  152. ok1(!htable_get(&ht, hash(&dne, NULL), objcmp, &dne));
  153. /* Corner cases: wipe out the perfect bit using bogus pointer. */
  154. htable_clear(&ht);
  155. htable_add(&ht, 0, (void *)((uintptr_t)&val[NUM_VALS-1]));
  156. ok1(ht.perfect_bit);
  157. perfect_bit = ht.perfect_bit;
  158. htable_add(&ht, 0, (void *)((uintptr_t)&val[NUM_VALS-1]
  159. | perfect_bit));
  160. ok1(ht.perfect_bit == 0);
  161. htable_del(&ht, 0, (void *)((uintptr_t)&val[NUM_VALS-1] | perfect_bit));
  162. /* Enlarging should restore it... */
  163. add_vals(&ht, val, 0, NUM_VALS-1);
  164. ok1(ht.perfect_bit != 0);
  165. htable_clear(&ht);
  166. ok1(htable_init_sized(&ht, hash, NULL, 1024));
  167. ok1(ht.max >= 1024);
  168. htable_clear(&ht);
  169. ok1(htable_init_sized(&ht, hash, NULL, 1023));
  170. ok1(ht.max >= 1023);
  171. htable_clear(&ht);
  172. ok1(htable_init_sized(&ht, hash, NULL, 1025));
  173. ok1(ht.max >= 1025);
  174. htable_clear(&ht);
  175. return exit_status();
  176. }