bavl_test.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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/brandom.h>
  25. #include <misc/debug.h>
  26. #include <structure/BAVL.h>
  27. struct mynode {
  28. int used;
  29. int num;
  30. BAVLNode avl_node;
  31. };
  32. static int int_comparator (void *user, int *val1, int *val2)
  33. {
  34. if (*val1 < *val2) {
  35. return -1;
  36. }
  37. if (*val1 > *val2) {
  38. return 1;
  39. }
  40. return 0;
  41. }
  42. static void print_indent (int indent)
  43. {
  44. for (int i = 0; i < indent; i++) {
  45. printf(" ");
  46. }
  47. }
  48. static void print_avl_recurser (BAVLNode *node, int indent)
  49. {
  50. print_indent(indent);
  51. if (!node) {
  52. printf("null\n");
  53. } else {
  54. struct mynode *mnode = UPPER_OBJECT(node, struct mynode, avl_node);
  55. printf("(%d) %d %p\n", node->balance, mnode->num, node);
  56. print_avl_recurser(node->link[0], indent + 1);
  57. print_avl_recurser(node->link[1], indent + 1);
  58. }
  59. }
  60. static void print_avl (BAVL *tree)
  61. {
  62. print_avl_recurser(tree->root, 0);
  63. }
  64. int main (int argc, char **argv)
  65. {
  66. int num_nodes;
  67. int num_random_delete;
  68. if (argc != 3 || (num_nodes = atoi(argv[1])) <= 0 || (num_random_delete = atoi(argv[2])) < 0) {
  69. fprintf(stderr, "Usage: %s <num> <numrandomdelete>\n", (argc > 0 ? argv[0] : NULL));
  70. return 1;
  71. }
  72. struct mynode *nodes = malloc(num_nodes * sizeof(*nodes));
  73. if (!nodes) {
  74. fprintf(stderr, "malloc failed\n");
  75. return 1;
  76. }
  77. int *values_ins = malloc(num_nodes * sizeof(int));
  78. ASSERT_FORCE(values_ins)
  79. int *values = malloc(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. free(nodes);
  117. free(values);
  118. return 0;
  119. }