savl_test.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * @file savl_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 <stdlib.h>
  30. #include <limits.h>
  31. #include <misc/debug.h>
  32. #include <misc/balloc.h>
  33. #include <misc/compare.h>
  34. #include <structure/SAvl.h>
  35. #include <security/BRandom.h>
  36. struct mynode;
  37. #include "savl_test_tree.h"
  38. #include <structure/SAvl_decl.h>
  39. struct mynode {
  40. int used;
  41. int num;
  42. MyTreeNode tree_node;
  43. };
  44. #include "savl_test_tree.h"
  45. #include <structure/SAvl_impl.h>
  46. static void verify (MyTree *tree)
  47. {
  48. printf("Verifying...\n");
  49. MyTree_Verify(tree, 0);
  50. }
  51. int main (int argc, char **argv)
  52. {
  53. int num_nodes;
  54. int num_random_delete;
  55. if (argc != 3 || (num_nodes = atoi(argv[1])) <= 0 || (num_random_delete = atoi(argv[2])) < 0) {
  56. fprintf(stderr, "Usage: %s <num> <numrandomdelete>\n", (argc > 0 ? argv[0] : NULL));
  57. return 1;
  58. }
  59. struct mynode *nodes = (struct mynode *)BAllocArray(num_nodes, sizeof(*nodes));
  60. ASSERT_FORCE(nodes)
  61. int *values_ins = (int *)BAllocArray(num_nodes, sizeof(int));
  62. ASSERT_FORCE(values_ins)
  63. int *values = (int *)BAllocArray(num_random_delete, sizeof(int));
  64. ASSERT_FORCE(values)
  65. MyTree tree;
  66. MyTree_Init(&tree);
  67. verify(&tree);
  68. printf("Inserting random values...\n");
  69. int inserted = 0;
  70. BRandom_randomize((uint8_t *)values_ins, num_nodes * sizeof(int));
  71. for (int i = 0; i < num_nodes; i++) {
  72. nodes[i].num = values_ins[i];
  73. if (MyTree_Insert(&tree, 0, &nodes[i], NULL)) {
  74. nodes[i].used = 1;
  75. inserted++;
  76. } else {
  77. nodes[i].used = 0;
  78. printf("Insert collision!\n");
  79. }
  80. }
  81. printf("Inserted %d entries\n", inserted);
  82. ASSERT_FORCE(MyTree_Count(&tree, 0) == inserted)
  83. verify(&tree);
  84. printf("Removing random entries...\n");
  85. int removed1 = 0;
  86. BRandom_randomize((uint8_t *)values, num_random_delete * sizeof(int));
  87. for (int i = 0; i < num_random_delete; i++) {
  88. int index = (((unsigned int *)values)[i] % num_nodes);
  89. struct mynode *node = nodes + index;
  90. if (node->used) {
  91. MyTree_Remove(&tree, 0, node);
  92. node->used = 0;
  93. removed1++;
  94. }
  95. }
  96. printf("Removed %d entries\n", removed1);
  97. ASSERT_FORCE(MyTree_Count(&tree, 0) == inserted - removed1)
  98. verify(&tree);
  99. printf("Removing remaining...\n");
  100. int removed2 = 0;
  101. while (!MyTree_IsEmpty(&tree)) {
  102. struct mynode *node = MyTree_GetFirst(&tree, 0);
  103. ASSERT_FORCE(node->used)
  104. MyTree_Remove(&tree, 0, node);
  105. node->used = 0;
  106. removed2++;
  107. }
  108. printf("Removed %d entries\n", removed2);
  109. ASSERT_FORCE(MyTree_IsEmpty(&tree))
  110. ASSERT_FORCE(removed1 + removed2 == inserted)
  111. ASSERT_FORCE(MyTree_Count(&tree, 0) == 0)
  112. verify(&tree);
  113. BFree(nodes);
  114. BFree(values_ins);
  115. BFree(values);
  116. return 0;
  117. }