bavl_test.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. * @file bavl_test.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <stdlib.h>
  23. #include <misc/offset.h>
  24. #include <misc/debug.h>
  25. #include <misc/balloc.h>
  26. #include <structure/BAVL.h>
  27. #include <security/BRandom.h>
  28. struct mynode {
  29. int used;
  30. int num;
  31. BAVLNode avl_node;
  32. };
  33. static int int_comparator (void *user, int *val1, int *val2)
  34. {
  35. if (*val1 < *val2) {
  36. return -1;
  37. }
  38. if (*val1 > *val2) {
  39. return 1;
  40. }
  41. return 0;
  42. }
  43. static void print_indent (int indent)
  44. {
  45. for (int i = 0; i < indent; i++) {
  46. printf(" ");
  47. }
  48. }
  49. static void print_avl_recurser (BAVLNode *node, int indent)
  50. {
  51. print_indent(indent);
  52. if (!node) {
  53. printf("null\n");
  54. } else {
  55. struct mynode *mnode = UPPER_OBJECT(node, struct mynode, avl_node);
  56. printf("(%d) %d %p\n", node->balance, mnode->num, node);
  57. print_avl_recurser(node->link[0], indent + 1);
  58. print_avl_recurser(node->link[1], indent + 1);
  59. }
  60. }
  61. static void print_avl (BAVL *tree)
  62. {
  63. print_avl_recurser(tree->root, 0);
  64. }
  65. int main (int argc, char **argv)
  66. {
  67. int num_nodes;
  68. int num_random_delete;
  69. if (argc != 3 || (num_nodes = atoi(argv[1])) <= 0 || (num_random_delete = atoi(argv[2])) < 0) {
  70. fprintf(stderr, "Usage: %s <num> <numrandomdelete>\n", (argc > 0 ? argv[0] : NULL));
  71. return 1;
  72. }
  73. struct mynode *nodes = BAllocArray(num_nodes, sizeof(*nodes));
  74. ASSERT_FORCE(nodes)
  75. int *values_ins = BAllocArray(num_nodes, sizeof(int));
  76. ASSERT_FORCE(values_ins)
  77. int *values = BAllocArray(num_random_delete, sizeof(int));
  78. ASSERT_FORCE(values)
  79. BAVL avl;
  80. BAVL_Init(&avl, OFFSET_DIFF(struct mynode, num, avl_node), (BAVL_comparator)int_comparator, NULL);
  81. /*
  82. printf("Inserting in reverse order...\n");
  83. for (int i = num_nodes - 1; i >= 0; i--) {
  84. nodes[i].used = 1;
  85. nodes[i].num = i;
  86. int res = BAVL_Insert(&avl, &nodes[i].avl_node);
  87. ASSERT(res == 1)
  88. }
  89. */
  90. printf("Inserting random values...\n");
  91. BRandom_randomize((uint8_t *)values_ins, num_nodes * sizeof(int));
  92. for (int i = 0; i < num_nodes; i++) {
  93. nodes[i].num = values_ins[i];
  94. if (BAVL_Insert(&avl, &nodes[i].avl_node, NULL)) {
  95. nodes[i].used = 1;
  96. } else {
  97. nodes[i].used = 0;
  98. printf("Insert collision!\n");
  99. }
  100. }
  101. printf("Removing random entries...\n");
  102. int removed = 0;
  103. BRandom_randomize((uint8_t *)values, num_random_delete * sizeof(int));
  104. for (int i = 0; i < num_random_delete; i++) {
  105. int index = (((unsigned int *)values)[i] % num_nodes);
  106. struct mynode *node = nodes + index;
  107. if (node->used) {
  108. BAVL_Remove(&avl, &node->avl_node);
  109. node->used = 0;
  110. removed++;
  111. }
  112. }
  113. printf("Removed %d entries\n", removed);
  114. BFree(nodes);
  115. BFree(values_ins);
  116. BFree(values);
  117. return 0;
  118. }