run-type.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #include <ccan/htable/htable_type.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. struct obj {
  9. /* Makes sure we don't try to treat and obj as a key or vice versa */
  10. unsigned char unused;
  11. unsigned int key;
  12. };
  13. static const unsigned int *objkey(const struct obj *obj)
  14. {
  15. return &obj->key;
  16. }
  17. /* We use the number divided by two as the hash (for lots of
  18. collisions), plus set all the higher bits so we can detect if they
  19. don't get masked out. */
  20. static size_t objhash(const unsigned int *key)
  21. {
  22. size_t h = *key / 2;
  23. h |= -1UL << NUM_BITS;
  24. return h;
  25. }
  26. static bool cmp(const struct obj *obj, const unsigned int *key)
  27. {
  28. return obj->key == *key;
  29. }
  30. HTABLE_DEFINE_TYPE(struct obj, objkey, objhash, cmp, htable_obj);
  31. static void add_vals(struct htable_obj *ht,
  32. struct obj val[], unsigned int num)
  33. {
  34. unsigned int i;
  35. for (i = 0; i < num; i++) {
  36. if (htable_obj_get(ht, &i)) {
  37. fail("%u already in hash", i);
  38. return;
  39. }
  40. htable_obj_add(ht, &val[i]);
  41. if (htable_obj_get(ht, &i) != &val[i]) {
  42. fail("%u not added to hash", i);
  43. return;
  44. }
  45. }
  46. pass("Added %u numbers to hash", i);
  47. }
  48. static void find_vals(const struct htable_obj *ht,
  49. const struct obj val[], unsigned int num)
  50. {
  51. unsigned int i;
  52. for (i = 0; i < num; i++) {
  53. if (htable_obj_get(ht, &i) != &val[i]) {
  54. fail("%u not found in hash", i);
  55. return;
  56. }
  57. }
  58. pass("Found %u numbers in hash", i);
  59. }
  60. static void del_vals(struct htable_obj *ht,
  61. const struct obj val[], unsigned int num)
  62. {
  63. unsigned int i;
  64. for (i = 0; i < num; i++) {
  65. if (!htable_obj_delkey(ht, &val[i].key)) {
  66. fail("%u not deleted from hash", i);
  67. return;
  68. }
  69. }
  70. pass("Deleted %u numbers in hash", i);
  71. }
  72. static void del_vals_bykey(struct htable_obj *ht,
  73. const struct obj val[], unsigned int num)
  74. {
  75. unsigned int i;
  76. for (i = 0; i < num; i++) {
  77. if (!htable_obj_delkey(ht, &i)) {
  78. fail("%u not deleted by key from hash", i);
  79. return;
  80. }
  81. }
  82. pass("Deleted %u numbers by key from hash", i);
  83. }
  84. static bool check_mask(struct htable *ht, const struct obj val[], unsigned num)
  85. {
  86. uint64_t i;
  87. for (i = 0; i < num; i++) {
  88. if (((uintptr_t)&val[i] & ht->common_mask) != ht->common_bits)
  89. return false;
  90. }
  91. return true;
  92. }
  93. int main(int argc, char *argv[])
  94. {
  95. unsigned int i;
  96. struct htable_obj ht, ht2;
  97. struct obj val[NUM_VALS], *result;
  98. unsigned int dne;
  99. void *p;
  100. struct htable_obj_iter iter;
  101. plan_tests(29);
  102. for (i = 0; i < NUM_VALS; i++)
  103. val[i].key = i;
  104. dne = i;
  105. htable_obj_init(&ht);
  106. ok1(ht.raw.max == 0);
  107. ok1(ht.raw.bits == 0);
  108. /* We cannot find an entry which doesn't exist. */
  109. ok1(!htable_obj_get(&ht, &dne));
  110. /* Fill it, it should increase in size. */
  111. add_vals(&ht, val, NUM_VALS);
  112. ok1(ht.raw.bits == NUM_BITS + 1);
  113. ok1(ht.raw.max < (1 << ht.raw.bits));
  114. /* Mask should be set. */
  115. ok1(ht.raw.common_mask != 0);
  116. ok1(ht.raw.common_mask != -1);
  117. ok1(check_mask(&ht.raw, val, NUM_VALS));
  118. /* Find all. */
  119. find_vals(&ht, val, NUM_VALS);
  120. ok1(!htable_obj_get(&ht, &dne));
  121. /* Walk once, should get them all. */
  122. i = 0;
  123. for (p = htable_obj_first(&ht,&iter); p; p = htable_obj_next(&ht, &iter))
  124. i++;
  125. ok1(i == NUM_VALS);
  126. i = 0;
  127. for (p = htable_obj_prev(&ht,&iter); p; p = htable_obj_prev(&ht, &iter))
  128. i++;
  129. ok1(i == NUM_VALS);
  130. /* Delete all. */
  131. del_vals(&ht, val, NUM_VALS);
  132. ok1(!htable_obj_get(&ht, &val[0].key));
  133. /* Worst case, a "pointer" which doesn't have any matching bits. */
  134. htable_add(&ht.raw, 0, (void *)~(uintptr_t)&val[NUM_VALS-1]);
  135. htable_obj_add(&ht, &val[NUM_VALS-1]);
  136. ok1(ht.raw.common_mask == 0);
  137. ok1(ht.raw.common_bits == 0);
  138. /* Delete the bogus one before we trip over it. */
  139. htable_del(&ht.raw, 0, (void *)~(uintptr_t)&val[NUM_VALS-1]);
  140. /* Add the rest. */
  141. add_vals(&ht, val, NUM_VALS-1);
  142. /* Check we can find them all. */
  143. find_vals(&ht, val, NUM_VALS);
  144. ok1(!htable_obj_get(&ht, &dne));
  145. /* Check copy. */
  146. ok1(htable_obj_copy(&ht2, &ht));
  147. /* Delete them all by key. */
  148. del_vals_bykey(&ht, val, NUM_VALS);
  149. del_vals_bykey(&ht2, val, NUM_VALS);
  150. /* Write two of the same value. */
  151. val[1] = val[0];
  152. htable_obj_add(&ht, &val[0]);
  153. htable_obj_add(&ht, &val[1]);
  154. i = 0;
  155. result = htable_obj_getfirst(&ht, &i, &iter);
  156. ok1(result == &val[0] || result == &val[1]);
  157. if (result == &val[0]) {
  158. ok1(htable_obj_getnext(&ht, &i, &iter) == &val[1]);
  159. ok1(htable_obj_getnext(&ht, &i, &iter) == NULL);
  160. /* Deleting first should make us iterate over the other. */
  161. ok1(htable_obj_del(&ht, &val[0]));
  162. ok1(htable_obj_getfirst(&ht, &i, &iter) == &val[1]);
  163. ok1(htable_obj_getnext(&ht, &i, &iter) == NULL);
  164. } else {
  165. ok1(htable_obj_getnext(&ht, &i, &iter) == &val[0]);
  166. ok1(htable_obj_getnext(&ht, &i, &iter) == NULL);
  167. /* Deleting first should make us iterate over the other. */
  168. ok1(htable_obj_del(&ht, &val[1]));
  169. ok1(htable_obj_getfirst(&ht, &i, &iter) == &val[0]);
  170. ok1(htable_obj_getnext(&ht, &i, &iter) == NULL);
  171. }
  172. htable_obj_clear(&ht);
  173. htable_obj_clear(&ht2);
  174. return exit_status();
  175. }