run.c 4.2 KB

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