cavl_test.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 <inttypes.h>
  35. #include <misc/balloc.h>
  36. #include <misc/compare.h>
  37. #include <misc/debug.h>
  38. #include <misc/print_macros.h>
  39. #include <structure/CAvl.h>
  40. #define USE_COUNTS 0
  41. #define USE_ASSOC 1
  42. typedef size_t entry_index;
  43. #define MAX_INDICES SIZE_MAX
  44. typedef uint32_t entry_key;
  45. typedef uint8_t assoc_value;
  46. typedef uint64_t assoc_sum;
  47. struct entry {
  48. entry_index tree_child[2];
  49. entry_index tree_parent;
  50. int8_t tree_balance;
  51. #if USE_COUNTS
  52. size_t tree_count;
  53. #endif
  54. #if USE_ASSOC
  55. assoc_value assoc_value;
  56. assoc_sum assoc_sum;
  57. #endif
  58. entry_key key;
  59. };
  60. typedef struct entry *entry_ptr;
  61. #include "cavl_test_tree.h"
  62. #include <structure/CAvl_decl.h>
  63. #include "cavl_test_tree.h"
  64. #include <structure/CAvl_impl.h>
  65. static void random_bytes (char *buf, size_t len)
  66. {
  67. while (len > 0) {
  68. *((unsigned char *)buf) = rand();
  69. buf++;
  70. len--;
  71. }
  72. }
  73. static int uint64_less (void *user, uint64_t a, uint64_t b)
  74. {
  75. return (a < b);
  76. }
  77. #if USE_ASSOC
  78. static MyTreeRef assoc_continue_last_lesser_equal (MyTree *tree, struct entry *arg, MyTreeRef ref, assoc_sum target_sum)
  79. {
  80. assoc_sum cur_sum = MyTree_ExclusiveAssocPrefixSum(tree, arg, ref);
  81. ASSERT(target_sum >= cur_sum)
  82. while (cur_sum + ref.ptr->assoc_value <= target_sum) {
  83. MyTreeRef next_ref = MyTree_GetNext(tree, arg, ref);
  84. if (next_ref.link == -1) {
  85. break;
  86. }
  87. cur_sum += ref.ptr->assoc_value;
  88. ref = next_ref;
  89. }
  90. return ref;
  91. }
  92. #endif
  93. static void test_assoc (MyTree *tree, struct entry *arg)
  94. {
  95. #if USE_ASSOC
  96. assoc_sum sum = 0;
  97. for (MyTreeRef ref = MyTree_GetFirst(tree, arg); ref.link != -1; ref = MyTree_GetNext(tree, arg, ref)) {
  98. assoc_sum tree_sum = MyTree_ExclusiveAssocPrefixSum(tree, arg, ref);
  99. ASSERT_FORCE(tree_sum == sum);
  100. ASSERT_FORCE(MyTree_FindLastExclusiveAssocPrefixSumLesserEqual(tree, arg, sum, uint64_less, NULL).link == assoc_continue_last_lesser_equal(tree, arg, ref, sum).link);
  101. ASSERT_FORCE(MyTree_FindLastExclusiveAssocPrefixSumLesserEqual(tree, arg, sum + 1, uint64_less, NULL).link == assoc_continue_last_lesser_equal(tree, arg, ref, sum + 1).link);
  102. sum += ref.ptr->assoc_value;
  103. }
  104. ASSERT_FORCE(sum == MyTree_AssocSum(tree, arg));
  105. #endif
  106. }
  107. int main (int argc, char *argv[])
  108. {
  109. //srand(time(NULL));
  110. printf("sizeof(struct entry)=%" PRIsz "\n", sizeof(struct entry));
  111. if (argc != 6) {
  112. fprintf(stderr, "Usage: %s <num_keys> <num_lookups> <num_remove> <do_remove=1/0> <do_verify=1/0>\n", (argc > 0 ? argv[0] : ""));
  113. return 1;
  114. }
  115. size_t num_keys = atoi(argv[1]);
  116. size_t num_lookups = atoi(argv[2]);
  117. size_t num_remove = atoi(argv[3]);
  118. size_t do_remove = atoi(argv[4]);
  119. size_t do_verify = atoi(argv[5]);
  120. printf("Allocating keys...\n");
  121. entry_key *keys = (entry_key *)BAllocArray(num_keys, sizeof(keys[0]));
  122. ASSERT_FORCE(keys);
  123. printf("Generating random keys...\n");
  124. random_bytes((char *)keys, num_keys * sizeof(keys[0]));
  125. printf("Allocating lookup indices...\n");
  126. uint64_t *lookup_indices = (uint64_t *)BAllocArray(num_lookups, sizeof(lookup_indices[0]));
  127. ASSERT_FORCE(lookup_indices);
  128. printf("Generating random lookup indices...\n");
  129. random_bytes((char *)lookup_indices, num_lookups * sizeof(lookup_indices[0]));
  130. printf("Allocating remove indices...\n");
  131. uint64_t *remove_indices = (uint64_t *)BAllocArray(num_remove, sizeof(remove_indices[0]));
  132. ASSERT_FORCE(remove_indices);
  133. printf("Generating random remove indices...\n");
  134. random_bytes((char *)remove_indices, num_remove * sizeof(remove_indices[0]));
  135. #if USE_ASSOC
  136. printf("Allocating assoc values...\n");
  137. assoc_value *assoc_values = (assoc_value *)BAllocArray(num_keys, sizeof(assoc_values[0]));
  138. ASSERT_FORCE(assoc_values);
  139. printf("Generating random assoc values...\n");
  140. random_bytes((char *)assoc_values, num_keys * sizeof(assoc_values[0]));
  141. #endif
  142. printf("Allocating entries...\n");
  143. ASSERT_FORCE(num_keys <= MAX_INDICES);
  144. struct entry *entries = (struct entry *)BAllocArray(num_keys, sizeof(*entries));
  145. ASSERT_FORCE(entries);
  146. entry_index num_used_entries = 0;
  147. MyTree tree;
  148. MyTree_Init(&tree);
  149. struct entry *arg = entries;
  150. ASSERT_FORCE(MyTree_IsEmpty(&tree));
  151. #if USE_COUNTS
  152. ASSERT_FORCE(MyTree_Count(&tree, arg) == 0);
  153. #endif
  154. test_assoc(&tree, arg);
  155. size_t num;
  156. #if USE_COUNTS
  157. size_t prevNum;
  158. #endif
  159. printf("Inserting random numbers...\n");
  160. num = 0;
  161. for (size_t i = 0; i < num_keys; i++) {
  162. entries[num_used_entries].key = keys[i];
  163. #if USE_ASSOC
  164. entries[num_used_entries].assoc_value = assoc_values[i];
  165. #endif
  166. MyTreeRef ref = {&entries[num_used_entries], num_used_entries};
  167. if (!MyTree_Insert(&tree, arg, ref, NULL)) {
  168. //printf("Insert collision!\n");
  169. continue;
  170. }
  171. num_used_entries++;
  172. num++;
  173. }
  174. printf("Inserted %" PRIsz ".\n", num);
  175. #if USE_COUNTS
  176. ASSERT_FORCE(MyTree_Count(&tree, arg) == num);
  177. #endif
  178. if (do_verify) {
  179. printf("Verifying...\n");
  180. MyTree_Verify(&tree, arg);
  181. test_assoc(&tree, arg);
  182. }
  183. printf("Looking up random inserted keys...\n");
  184. for (size_t i = 0; i < num_lookups; i++) {
  185. entry_index idx = lookup_indices[i] % num_keys;
  186. MyTreeRef entry = MyTree_LookupExact(&tree, arg, keys[idx]);
  187. ASSERT_FORCE(!MyTreeIsNullRef(entry));
  188. }
  189. #if USE_COUNTS
  190. prevNum = MyTree_Count(&tree, arg);
  191. #endif
  192. num = 0;
  193. printf("Looking up and removing random inserted keys...\n");
  194. for (size_t i = 0; i < num_remove; i++) {
  195. entry_index idx = remove_indices[i] % num_keys;
  196. MyTreeRef entry = MyTree_LookupExact(&tree, arg, keys[idx]);
  197. if (MyTreeIsNullRef(entry)) {
  198. //printf("Remove collision!\n");
  199. continue;
  200. }
  201. ASSERT_FORCE(entry.ptr->key == keys[idx]);
  202. MyTree_Remove(&tree, arg, entry);
  203. num++;
  204. }
  205. printf("Removed %" PRIsz ".\n", num);
  206. #if USE_COUNTS
  207. ASSERT_FORCE(MyTree_Count(&tree, arg) == prevNum - num);
  208. #endif
  209. if (do_verify) {
  210. printf("Verifying...\n");
  211. MyTree_Verify(&tree, arg);
  212. test_assoc(&tree, arg);
  213. }
  214. if (do_remove) {
  215. #if USE_COUNTS
  216. prevNum = MyTree_Count(&tree, arg);
  217. #endif
  218. num = 0;
  219. printf("Removing remaining...\n");
  220. MyTreeRef cur = MyTree_GetFirst(&tree, arg);
  221. while (!MyTreeIsNullRef(cur)) {
  222. MyTreeRef prev = cur;
  223. cur = MyTree_GetNext(&tree, arg, cur);
  224. MyTree_Remove(&tree, arg, prev);
  225. num++;
  226. }
  227. printf("Removed %" PRIsz ".\n", num);
  228. ASSERT_FORCE(MyTree_IsEmpty(&tree));
  229. #if USE_COUNTS
  230. ASSERT_FORCE(MyTree_Count(&tree, arg) == 0);
  231. ASSERT_FORCE(num == prevNum);
  232. #endif
  233. if (do_verify) {
  234. printf("Verifying...\n");
  235. MyTree_Verify(&tree, arg);
  236. }
  237. }
  238. printf("Freeing...\n");
  239. BFree(keys);
  240. BFree(lookup_indices);
  241. BFree(remove_indices);
  242. #if USE_ASSOC
  243. BFree(assoc_values);
  244. #endif
  245. BFree(entries);
  246. return 0;
  247. }