bavl_test.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. * @file bavl_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 <stdlib.h>
  30. #include <misc/offset.h>
  31. #include <misc/debug.h>
  32. #include <misc/balloc.h>
  33. #include <misc/compare.h>
  34. #include <structure/BAVL.h>
  35. #include <security/BRandom.h>
  36. struct mynode {
  37. int used;
  38. int num;
  39. BAVLNode avl_node;
  40. };
  41. static int int_comparator (void *user, int *val1, int *val2)
  42. {
  43. return B_COMPARE(*val1, *val2);
  44. }
  45. static void print_indent (int indent)
  46. {
  47. for (int i = 0; i < indent; i++) {
  48. printf(" ");
  49. }
  50. }
  51. static void print_avl_recurser (BAVLNode *node, int indent)
  52. {
  53. print_indent(indent);
  54. if (!node) {
  55. printf("null\n");
  56. } else {
  57. struct mynode *mnode = UPPER_OBJECT(node, struct mynode, avl_node);
  58. printf("(%d) %d %p\n", node->balance, mnode->num, node);
  59. print_avl_recurser(node->link[0], indent + 1);
  60. print_avl_recurser(node->link[1], indent + 1);
  61. }
  62. }
  63. static void print_avl (BAVL *tree)
  64. {
  65. print_avl_recurser(tree->root, 0);
  66. }
  67. int main (int argc, char **argv)
  68. {
  69. int num_nodes;
  70. int num_random_delete;
  71. if (argc != 3 || (num_nodes = atoi(argv[1])) <= 0 || (num_random_delete = atoi(argv[2])) < 0) {
  72. fprintf(stderr, "Usage: %s <num> <numrandomdelete>\n", (argc > 0 ? argv[0] : NULL));
  73. return 1;
  74. }
  75. struct mynode *nodes = BAllocArray(num_nodes, sizeof(*nodes));
  76. ASSERT_FORCE(nodes)
  77. int *values_ins = BAllocArray(num_nodes, sizeof(int));
  78. ASSERT_FORCE(values_ins)
  79. int *values = BAllocArray(num_random_delete, sizeof(int));
  80. ASSERT_FORCE(values)
  81. BAVL avl;
  82. BAVL_Init(&avl, OFFSET_DIFF(struct mynode, num, avl_node), (BAVL_comparator)int_comparator, NULL);
  83. /*
  84. printf("Inserting in reverse order...\n");
  85. for (int i = num_nodes - 1; i >= 0; i--) {
  86. nodes[i].used = 1;
  87. nodes[i].num = i;
  88. int res = BAVL_Insert(&avl, &nodes[i].avl_node);
  89. ASSERT(res == 1)
  90. }
  91. */
  92. printf("Inserting random values...\n");
  93. BRandom_randomize((uint8_t *)values_ins, num_nodes * sizeof(int));
  94. for (int i = 0; i < num_nodes; i++) {
  95. nodes[i].num = values_ins[i];
  96. if (BAVL_Insert(&avl, &nodes[i].avl_node, NULL)) {
  97. nodes[i].used = 1;
  98. } else {
  99. nodes[i].used = 0;
  100. printf("Insert collision!\n");
  101. }
  102. }
  103. printf("Removing random entries...\n");
  104. int removed = 0;
  105. BRandom_randomize((uint8_t *)values, num_random_delete * sizeof(int));
  106. for (int i = 0; i < num_random_delete; i++) {
  107. int index = (((unsigned int *)values)[i] % num_nodes);
  108. struct mynode *node = nodes + index;
  109. if (node->used) {
  110. BAVL_Remove(&avl, &node->avl_node);
  111. node->used = 0;
  112. removed++;
  113. }
  114. }
  115. printf("Removed %d entries\n", removed);
  116. BFree(nodes);
  117. BFree(values_ins);
  118. BFree(values);
  119. return 0;
  120. }