bheap_test.c 3.4 KB

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