bheap_test.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * @file bheap_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 <misc/offset.h>
  23. #include <structure/BHeap.h>
  24. #include <security/BRandom.h>
  25. struct mynode {
  26. int used;
  27. int num;
  28. BHeapNode heap_node;
  29. };
  30. static int int_comparator (void *user, int *val1, int *val2)
  31. {
  32. if (*val1 < *val2) {
  33. return -1;
  34. }
  35. if (*val1 > *val2) {
  36. return 1;
  37. }
  38. return 0;
  39. }
  40. static void print_indent (int indent)
  41. {
  42. for (int i = 0; i < indent; i++) {
  43. printf(" ");
  44. }
  45. }
  46. static void print_heap_recurser (BHeapNode *node, int indent)
  47. {
  48. print_indent(indent);
  49. if (!node) {
  50. printf("null\n");
  51. } else {
  52. struct mynode *mnode = UPPER_OBJECT(node, struct mynode, heap_node);
  53. printf("%d %p\n", mnode->num, node);
  54. print_heap_recurser(node->link[0], indent + 1);
  55. print_heap_recurser(node->link[1], indent + 1);
  56. }
  57. }
  58. static void print_heap (BHeap *heap)
  59. {
  60. print_heap_recurser(heap->root, 0);
  61. }
  62. int main (int argc, char **argv)
  63. {
  64. int num_nodes;
  65. int num_random_delete;
  66. if (argc != 3 || (num_nodes = atoi(argv[1])) <= 0 || (num_random_delete = atoi(argv[2])) < 0) {
  67. fprintf(stderr, "Usage: %s <num> <numrandomdelete>\n", (argc > 0 ? argv[0] : NULL));
  68. return 1;
  69. }
  70. struct mynode *nodes = malloc(num_nodes * sizeof(*nodes));
  71. if (!nodes) {
  72. fprintf(stderr, "malloc failed\n");
  73. return 1;
  74. }
  75. int *values = malloc(num_random_delete * sizeof(int));
  76. ASSERT_FORCE(values)
  77. BHeap heap;
  78. BHeap_Init(&heap, OFFSET_DIFF(struct mynode, num, heap_node), (BHeap_comparator)int_comparator, NULL);
  79. printf("Inserting in reverse order...\n");
  80. for (int i = num_nodes - 1; i >= 0; i--) {
  81. nodes[i].used = 1;
  82. nodes[i].num = i;
  83. BHeap_Insert(&heap, &nodes[i].heap_node);
  84. }
  85. //print_heap(&heap);
  86. printf("Removing random entries...\n");
  87. BRandom_randomize((uint8_t *)values, num_random_delete * sizeof(int));
  88. for (int i = 0; i < num_random_delete; i++) {
  89. int index = (((unsigned int *)values)[i] % num_nodes);
  90. struct mynode *node = nodes + index;
  91. if (node->used) {
  92. //printf("Removing index %d value %d\n", index, node->num);
  93. BHeap_Remove(&heap, &node->heap_node);
  94. node->used = 0;
  95. }
  96. }
  97. //print_heap(&heap);
  98. printf("Removing remaining entries...\n");
  99. BHeapNode *heap_node;
  100. while (heap_node = BHeap_GetFirst(&heap)) {
  101. struct mynode *node = UPPER_OBJECT(heap_node, struct mynode, heap_node);
  102. //printf("Removing value %d\n", node->num);
  103. BHeap_Remove(&heap, &node->heap_node);
  104. node->used = 0;
  105. }
  106. //print_heap(&heap);
  107. free(nodes);
  108. free(values);
  109. return 0;
  110. }