bheap_test.c 4.2 KB

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