cavl_test.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /**
  2. * @file cavl_test.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <time.h>
  32. #include <stdint.h>
  33. #include <limits.h>
  34. #include <misc/balloc.h>
  35. #include <misc/compare.h>
  36. #include <misc/debug.h>
  37. #include <misc/print_macros.h>
  38. #include <structure/CAvl.h>
  39. #define USE_COUNTS 0
  40. #define USE_ASSOC 1
  41. typedef size_t entry_index;
  42. #define MAX_INDICES SIZE_MAX
  43. typedef uint32_t entry_key;
  44. typedef uint8_t assoc_value;
  45. typedef uint64_t assoc_sum;
  46. struct entry {
  47. entry_index tree_child[2];
  48. entry_index tree_parent;
  49. int8_t tree_balance;
  50. #if USE_COUNTS
  51. size_t tree_count;
  52. #endif
  53. #if USE_ASSOC
  54. assoc_value assoc_value;
  55. assoc_sum assoc_sum;
  56. #endif
  57. entry_key key;
  58. };
  59. typedef struct entry *entry_ptr;
  60. #include "cavl_test_tree.h"
  61. #include <structure/CAvl_decl.h>
  62. #include "cavl_test_tree.h"
  63. #include <structure/CAvl_impl.h>
  64. static void random_bytes (char *buf, size_t len)
  65. {
  66. while (len > 0) {
  67. *((unsigned char *)buf) = rand();
  68. buf++;
  69. len--;
  70. }
  71. }
  72. static int uint64_less (void *user, uint64_t a, uint64_t b)
  73. {
  74. return (a < b);
  75. }
  76. #if USE_ASSOC
  77. static MyTreeRef assoc_continue_last_lesser_equal (MyTree *tree, struct entry *arg, MyTreeRef ref, assoc_sum target_sum)
  78. {
  79. assoc_sum cur_sum = MyTree_ExclusiveAssocPrefixSum(tree, arg, ref);
  80. ASSERT(target_sum >= cur_sum)
  81. while (cur_sum + ref.ptr->assoc_value <= target_sum) {
  82. MyTreeRef next_ref = MyTree_GetNext(tree, arg, ref);
  83. if (next_ref.link == -1) {
  84. break;
  85. }
  86. cur_sum += ref.ptr->assoc_value;
  87. ref = next_ref;
  88. }
  89. return ref;
  90. }
  91. #endif
  92. static void test_assoc (MyTree *tree, struct entry *arg)
  93. {
  94. #if USE_ASSOC
  95. assoc_sum sum = 0;
  96. for (MyTreeRef ref = MyTree_GetFirst(tree, arg); ref.link != -1; ref = MyTree_GetNext(tree, arg, ref)) {
  97. assoc_sum tree_sum = MyTree_ExclusiveAssocPrefixSum(tree, arg, ref);
  98. ASSERT_FORCE(tree_sum == sum);
  99. ASSERT_FORCE(MyTree_FindLastExclusiveAssocPrefixSumLesserEqual(tree, arg, sum, uint64_less, NULL).link == assoc_continue_last_lesser_equal(tree, arg, ref, sum).link);
  100. ASSERT_FORCE(MyTree_FindLastExclusiveAssocPrefixSumLesserEqual(tree, arg, sum + 1, uint64_less, NULL).link == assoc_continue_last_lesser_equal(tree, arg, ref, sum + 1).link);
  101. sum += ref.ptr->assoc_value;
  102. }
  103. ASSERT_FORCE(sum == MyTree_AssocSum(tree, arg));
  104. #endif
  105. }
  106. int main (int argc, char *argv[])
  107. {
  108. //srand(time(NULL));
  109. printf("sizeof(struct entry)=%" PRIsz "\n", sizeof(struct entry));
  110. if (argc != 6) {
  111. fprintf(stderr, "Usage: %s <num_keys> <num_lookups> <num_remove> <do_remove=1/0> <do_verify=1/0>\n", (argc > 0 ? argv[0] : ""));
  112. return 1;
  113. }
  114. size_t num_keys = atoi(argv[1]);
  115. size_t num_lookups = atoi(argv[2]);
  116. size_t num_remove = atoi(argv[3]);
  117. size_t do_remove = atoi(argv[4]);
  118. size_t do_verify = atoi(argv[5]);
  119. printf("Allocating keys...\n");
  120. entry_key *keys = (entry_key *)BAllocArray(num_keys, sizeof(keys[0]));
  121. ASSERT_FORCE(keys);
  122. printf("Generating random keys...\n");
  123. random_bytes((char *)keys, num_keys * sizeof(keys[0]));
  124. printf("Allocating lookup indices...\n");
  125. uint64_t *lookup_indices = (uint64_t *)BAllocArray(num_lookups, sizeof(lookup_indices[0]));
  126. ASSERT_FORCE(lookup_indices);
  127. printf("Generating random lookup indices...\n");
  128. random_bytes((char *)lookup_indices, num_lookups * sizeof(lookup_indices[0]));
  129. printf("Allocating remove indices...\n");
  130. uint64_t *remove_indices = (uint64_t *)BAllocArray(num_remove, sizeof(remove_indices[0]));
  131. ASSERT_FORCE(remove_indices);
  132. printf("Generating random remove indices...\n");
  133. random_bytes((char *)remove_indices, num_remove * sizeof(remove_indices[0]));
  134. #if USE_ASSOC
  135. printf("Allocating assoc values...\n");
  136. assoc_value *assoc_values = (assoc_value *)BAllocArray(num_keys, sizeof(assoc_values[0]));
  137. ASSERT_FORCE(assoc_values);
  138. printf("Generating random assoc values...\n");
  139. random_bytes((char *)assoc_values, num_keys * sizeof(assoc_values[0]));
  140. #endif
  141. printf("Allocating entries...\n");
  142. ASSERT_FORCE(num_keys <= MAX_INDICES);
  143. struct entry *entries = (struct entry *)BAllocArray(num_keys, sizeof(*entries));
  144. ASSERT_FORCE(entries);
  145. entry_index num_used_entries = 0;
  146. MyTree tree;
  147. MyTree_Init(&tree);
  148. struct entry *arg = entries;
  149. ASSERT_FORCE(MyTree_IsEmpty(&tree));
  150. #if USE_COUNTS
  151. ASSERT_FORCE(MyTree_Count(&tree, arg) == 0);
  152. #endif
  153. test_assoc(&tree, arg);
  154. size_t num;
  155. #if USE_COUNTS
  156. size_t prevNum;
  157. #endif
  158. printf("Inserting random numbers...\n");
  159. num = 0;
  160. for (size_t i = 0; i < num_keys; i++) {
  161. entries[num_used_entries].key = keys[i];
  162. #if USE_ASSOC
  163. entries[num_used_entries].assoc_value = assoc_values[i];
  164. #endif
  165. MyTreeRef ref = {&entries[num_used_entries], num_used_entries};
  166. if (!MyTree_Insert(&tree, arg, ref, NULL)) {
  167. //printf("Insert collision!\n");
  168. continue;
  169. }
  170. num_used_entries++;
  171. num++;
  172. }
  173. printf("Inserted %" PRIsz ".\n", num);
  174. #if USE_COUNTS
  175. ASSERT_FORCE(MyTree_Count(&tree, arg) == num);
  176. #endif
  177. if (do_verify) {
  178. printf("Verifying...\n");
  179. MyTree_Verify(&tree, arg);
  180. test_assoc(&tree, arg);
  181. }
  182. printf("Looking up random inserted keys...\n");
  183. for (size_t i = 0; i < num_lookups; i++) {
  184. entry_index idx = lookup_indices[i] % num_keys;
  185. MyTreeRef entry = MyTree_LookupExact(&tree, arg, keys[idx]);
  186. ASSERT_FORCE(!MyTreeIsNullRef(entry));
  187. }
  188. #if USE_COUNTS
  189. prevNum = MyTree_Count(&tree, arg);
  190. #endif
  191. num = 0;
  192. printf("Looking up and removing random inserted keys...\n");
  193. for (size_t i = 0; i < num_remove; i++) {
  194. entry_index idx = remove_indices[i] % num_keys;
  195. MyTreeRef entry = MyTree_LookupExact(&tree, arg, keys[idx]);
  196. if (MyTreeIsNullRef(entry)) {
  197. //printf("Remove collision!\n");
  198. continue;
  199. }
  200. ASSERT_FORCE(entry.ptr->key == keys[idx]);
  201. MyTree_Remove(&tree, arg, entry);
  202. num++;
  203. }
  204. printf("Removed %" PRIsz ".\n", num);
  205. #if USE_COUNTS
  206. ASSERT_FORCE(MyTree_Count(&tree, arg) == prevNum - num);
  207. #endif
  208. if (do_verify) {
  209. printf("Verifying...\n");
  210. MyTree_Verify(&tree, arg);
  211. test_assoc(&tree, arg);
  212. }
  213. if (do_remove) {
  214. #if USE_COUNTS
  215. prevNum = MyTree_Count(&tree, arg);
  216. #endif
  217. num = 0;
  218. printf("Removing remaining...\n");
  219. MyTreeRef cur = MyTree_GetFirst(&tree, arg);
  220. while (!MyTreeIsNullRef(cur)) {
  221. MyTreeRef prev = cur;
  222. cur = MyTree_GetNext(&tree, arg, cur);
  223. MyTree_Remove(&tree, arg, prev);
  224. num++;
  225. }
  226. printf("Removed %" PRIsz ".\n", num);
  227. ASSERT_FORCE(MyTree_IsEmpty(&tree));
  228. #if USE_COUNTS
  229. ASSERT_FORCE(MyTree_Count(&tree, arg) == 0);
  230. ASSERT_FORCE(num == prevNum);
  231. #endif
  232. if (do_verify) {
  233. printf("Verifying...\n");
  234. MyTree_Verify(&tree, arg);
  235. }
  236. }
  237. printf("Freeing...\n");
  238. BFree(keys);
  239. BFree(lookup_indices);
  240. BFree(remove_indices);
  241. #if USE_ASSOC
  242. BFree(assoc_values);
  243. #endif
  244. BFree(entries);
  245. return 0;
  246. }