hash.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #include "../hash.h"
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #define ERROR(a, ...) printf("%s:%d " a "\n", __FILE__, __LINE__ __VA_OPT__(,) __VA_ARGS__)
  6. void print_hash_table(struct HT_base_layout* ht) {
  7. for(size_t i = 0; i < ht->alloc_size; i++) {
  8. char* b = (char*)ht->buckets + (i * ht->stride);
  9. size_t key_width = ht->key_mode == 'i' ? ht->key_len : sizeof(char*);
  10. uint64_t* b_hash = (uint64_t*)b;
  11. char* b_key = b + sizeof(uint64_t);
  12. char* b_val = b + sizeof(uint64_t) + key_width;;
  13. printf("bucket #%ld", i);
  14. if(*b_hash == 0) printf(" [empty]\n\n");
  15. else {
  16. printf("\n hash: %lx\n", *b_hash);
  17. if(ht->key_mode == 's')
  18. printf(" key: '%s'\n", *(char**)b_key);
  19. else
  20. printf(" key: %lx\n", *b_key);
  21. printf(" val: %lx\n", *(int*)b_val);
  22. printf(" \n");
  23. }
  24. }
  25. }
  26. typedef struct {
  27. float a,b,c;
  28. } vec3;
  29. #define V(...) ((vec3){ __VA_ARGS__ })
  30. int main() {
  31. int n;
  32. int nstrs = 10;
  33. char** strlist = malloc(sizeof(*strlist) * nstrs);
  34. for(int i = 0; i < nstrs; i++) {
  35. char* b = malloc(20);
  36. sprintf(b, "%d", i);
  37. strlist[i] = b;
  38. }
  39. HT(int) str_int;
  40. HT_init(&str_int, 16);
  41. /*
  42. for(int i = 0; i < nstrs; i++) {
  43. HT_set(&str_int, strlist[i], i);
  44. }
  45. print_hash_table(&str_int.base);
  46. for(int i = 0; i < nstrs; i++) {
  47. int n = 0;
  48. if(HT_get(&str_int, strlist[i], &n)) ERROR("not found %d", i);
  49. if(n != i) ERROR("wrong value '%s' != %d", strlist[i], i);
  50. }
  51. if(HT_delete(&str_int, "3")) ERROR("could not delete");
  52. n = 0;
  53. if(!HT_get(&str_int, "3", &n)) ERROR("wrongly found");
  54. if(n != 0) ERROR("wrong value");
  55. */
  56. char* one = strdup("one");
  57. char* two = strdup("two");
  58. char* three = strdup("three");
  59. // reset
  60. HT_destroy(&str_int);
  61. HT_init(&str_int, 16);
  62. HT_set(&str_int, one, 1);
  63. HT_set(&str_int, two, 2);
  64. HT_set(&str_int, three, 3);
  65. print_hash_table(&str_int.base);
  66. n = 0;
  67. if(HT_get(&str_int, one, &n)) ERROR("not found");
  68. if(n != 1) ERROR("wrong value");
  69. n = 0;
  70. if(HT_get(&str_int, two, &n)) ERROR("not found");
  71. if(n != 2) ERROR("wrong value");
  72. n = 0;
  73. if(HT_get(&str_int, three, &n)) ERROR("not found");
  74. if(n != 3) ERROR("wrong value");
  75. // make sure there's no weird pointer fuckery and the value itself is getting used as the key
  76. n = 0;
  77. if(HT_get(&str_int, "one", &n)) ERROR("not found");
  78. if(n != 1) ERROR("wrong value");
  79. n = 0;
  80. if(HT_get(&str_int, "two", &n)) ERROR("not found");
  81. if(n != 2) ERROR("wrong value");
  82. n = 0;
  83. if(HT_get(&str_int, "three", &n)) ERROR("not found");
  84. if(n != 3) ERROR("wrong value");
  85. HT(int, int) int_int;
  86. HT_init(&int_int, 16);
  87. HT_set(&int_int, 1, 1);
  88. HT_set(&int_int, 2, 2);
  89. HT_set(&int_int, 3, 3);
  90. n = 0;
  91. if(HT_get(&int_int, 1, &n)) ERROR("not found");
  92. if(n != 1) ERROR("wrong value");
  93. n = 0;
  94. if(HT_get(&int_int, 2, &n)) ERROR("not found");
  95. if(n != 2) ERROR("wrong value");
  96. n = 0;
  97. if(HT_get(&int_int, 3, &n)) ERROR("not found");
  98. if(n != 3) ERROR("wrong value");
  99. // reset
  100. HT_destroy(&int_int);
  101. HT_init(&int_int, 16);
  102. int n1 = 1, n2 = 2, n3 = 3;
  103. HT_set(&int_int, n1, 1);
  104. HT_set(&int_int, n2, 2);
  105. HT_set(&int_int, n3, 3);
  106. n = 0;
  107. if(HT_get(&int_int, n1, &n)) ERROR("not found");
  108. if(n != 1) ERROR("wrong value");
  109. n = 0;
  110. if(HT_get(&int_int, n2, &n)) ERROR("not found");
  111. if(n != 2) ERROR("wrong value");
  112. n = 0;
  113. if(HT_get(&int_int, n3, &n)) ERROR("not found");
  114. if(n != 3) ERROR("wrong value");
  115. HT(vec3, int) vec_int;
  116. HT_init(&vec_int, 16);
  117. HT_set(&vec_int, V(1,2,3), 1);
  118. HT_set(&vec_int, V(2,3,4), 2);
  119. HT_set(&vec_int, V(3,4,5), 3);
  120. n = 0;
  121. if(HT_get(&vec_int, V(1,2,3), &n)) ERROR("not found");
  122. if(n != 1) ERROR("wrong value");
  123. n = 2;
  124. if(HT_get(&vec_int, V(2,3,4), &n)) ERROR("not found");
  125. if(n != 2) ERROR("wrong value");
  126. n = 3;
  127. if(HT_get(&vec_int, V(3,4,5), &n)) ERROR("not found");
  128. if(n != 3) ERROR("wrong value");
  129. vec3 vn1 = V(1,2,3);
  130. vec3 vn2 = V(2,3,4);
  131. vec3 vn3 = V(3,4,5);
  132. // reset
  133. HT_destroy(&vec_int);
  134. HT_init(&vec_int, 16);
  135. HT_set(&vec_int, vn1, 1);
  136. HT_set(&vec_int, vn2, 2);
  137. HT_set(&vec_int, vn3, 3);
  138. n = 0;
  139. if(HT_get(&vec_int, vn1, &n)) ERROR("not found");
  140. if(n != 1) ERROR("wrong value");
  141. n = 2;
  142. if(HT_get(&vec_int, vn2, &n)) ERROR("not found");
  143. if(n != 2) ERROR("wrong value");
  144. n = 3;
  145. if(HT_get(&vec_int, vn3, &n)) ERROR("not found");
  146. if(n != 3) ERROR("wrong value");
  147. n = 0;
  148. if(HT_get(&vec_int, V(1,2,3), &n)) ERROR("not found");
  149. if(n != 1) ERROR("wrong value");
  150. n = 2;
  151. if(HT_get(&vec_int, V(2,3,4), &n)) ERROR("not found");
  152. if(n != 2) ERROR("wrong value");
  153. n = 3;
  154. if(HT_get(&vec_int, V(3,4,5), &n)) ERROR("not found");
  155. if(n != 3) ERROR("wrong value");
  156. return 0;
  157. }