BLI_heap_test.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* Apache License, Version 2.0 */
  2. #include "testing/testing.h"
  3. #include <string.h>
  4. extern "C" {
  5. #include "BLI_compiler_attrs.h"
  6. #include "BLI_heap.h"
  7. #include "BLI_utildefines.h"
  8. #include "BLI_rand.h"
  9. #include "MEM_guardedalloc.h"
  10. };
  11. #define SIZE 1024
  12. static void range_fl(float *array_tar, const int size)
  13. {
  14. float *array_pt = array_tar + (size - 1);
  15. int i = size;
  16. while (i--) {
  17. *(array_pt--) = (float)i;
  18. }
  19. }
  20. TEST(heap, Empty)
  21. {
  22. Heap *heap;
  23. heap = BLI_heap_new();
  24. EXPECT_TRUE(BLI_heap_is_empty(heap));
  25. EXPECT_EQ(BLI_heap_len(heap), 0);
  26. BLI_heap_free(heap, NULL);
  27. }
  28. TEST(heap, One)
  29. {
  30. Heap *heap;
  31. const char *in = "test";
  32. heap = BLI_heap_new();
  33. BLI_heap_insert(heap, 0.0f, (void *)in);
  34. EXPECT_FALSE(BLI_heap_is_empty(heap));
  35. EXPECT_EQ(BLI_heap_len(heap), 1);
  36. EXPECT_EQ(in, BLI_heap_pop_min(heap));
  37. EXPECT_TRUE(BLI_heap_is_empty(heap));
  38. EXPECT_EQ(BLI_heap_len(heap), 0);
  39. BLI_heap_free(heap, NULL);
  40. }
  41. TEST(heap, Range)
  42. {
  43. const int items_total = SIZE;
  44. Heap *heap = BLI_heap_new();
  45. for (int in = 0; in < items_total; in++) {
  46. BLI_heap_insert(heap, (float)in, POINTER_FROM_INT(in));
  47. }
  48. for (int out_test = 0; out_test < items_total; out_test++) {
  49. EXPECT_EQ(out_test, POINTER_AS_INT(BLI_heap_pop_min(heap)));
  50. }
  51. EXPECT_TRUE(BLI_heap_is_empty(heap));
  52. BLI_heap_free(heap, NULL);
  53. }
  54. TEST(heap, RangeReverse)
  55. {
  56. const int items_total = SIZE;
  57. Heap *heap = BLI_heap_new();
  58. for (int in = 0; in < items_total; in++) {
  59. BLI_heap_insert(heap, (float)-in, POINTER_FROM_INT(-in));
  60. }
  61. for (int out_test = items_total - 1; out_test >= 0; out_test--) {
  62. EXPECT_EQ(-out_test, POINTER_AS_INT(BLI_heap_pop_min(heap)));
  63. }
  64. EXPECT_TRUE(BLI_heap_is_empty(heap));
  65. BLI_heap_free(heap, NULL);
  66. }
  67. TEST(heap, RangeRemove)
  68. {
  69. const int items_total = SIZE;
  70. Heap *heap = BLI_heap_new();
  71. HeapNode **nodes = (HeapNode **)MEM_mallocN(sizeof(HeapNode *) * items_total, __func__);
  72. for (int in = 0; in < items_total; in++) {
  73. nodes[in] = BLI_heap_insert(heap, (float)in, POINTER_FROM_INT(in));
  74. }
  75. for (int i = 0; i < items_total; i += 2) {
  76. BLI_heap_remove(heap, nodes[i]);
  77. nodes[i] = NULL;
  78. }
  79. for (int out_test = 1; out_test < items_total; out_test += 2) {
  80. EXPECT_EQ(out_test, POINTER_AS_INT(BLI_heap_pop_min(heap)));
  81. }
  82. EXPECT_TRUE(BLI_heap_is_empty(heap));
  83. BLI_heap_free(heap, NULL);
  84. MEM_freeN(nodes);
  85. }
  86. TEST(heap, Duplicates)
  87. {
  88. const int items_total = SIZE;
  89. Heap *heap = BLI_heap_new();
  90. for (int in = 0; in < items_total; in++) {
  91. BLI_heap_insert(heap, 1.0f, 0);
  92. }
  93. for (int out_test = 0; out_test < items_total; out_test++) {
  94. EXPECT_EQ(0, POINTER_AS_INT(BLI_heap_pop_min(heap)));
  95. }
  96. EXPECT_TRUE(BLI_heap_is_empty(heap));
  97. BLI_heap_free(heap, NULL);
  98. }
  99. static void random_heap_helper(const int items_total, const int random_seed)
  100. {
  101. Heap *heap = BLI_heap_new();
  102. float *values = (float *)MEM_mallocN(sizeof(float) * items_total, __func__);
  103. range_fl(values, items_total);
  104. BLI_array_randomize(values, sizeof(float), items_total, random_seed);
  105. for (int i = 0; i < items_total; i++) {
  106. BLI_heap_insert(heap, values[i], POINTER_FROM_INT((int)values[i]));
  107. }
  108. for (int out_test = 0; out_test < items_total; out_test++) {
  109. EXPECT_EQ(out_test, POINTER_AS_INT(BLI_heap_pop_min(heap)));
  110. }
  111. EXPECT_TRUE(BLI_heap_is_empty(heap));
  112. BLI_heap_free(heap, NULL);
  113. MEM_freeN(values);
  114. }
  115. TEST(heap, Rand1)
  116. {
  117. random_heap_helper(1, 1234);
  118. }
  119. TEST(heap, Rand2)
  120. {
  121. random_heap_helper(2, 1234);
  122. }
  123. TEST(heap, Rand100)
  124. {
  125. random_heap_helper(100, 4321);
  126. }
  127. TEST(heap, ReInsertSimple)
  128. {
  129. const int items_total = SIZE;
  130. Heap *heap = BLI_heap_new();
  131. HeapNode **nodes = (HeapNode **)MEM_mallocN(sizeof(HeapNode *) * items_total, __func__);
  132. for (int in = 0; in < items_total; in++) {
  133. nodes[in] = BLI_heap_insert(heap, (float)in, POINTER_FROM_INT(in));
  134. }
  135. for (int i = 0; i < items_total; i++) {
  136. BLI_heap_node_value_update(heap, nodes[i], (float)(items_total + i));
  137. }
  138. for (int out_test = 0; out_test < items_total; out_test++) {
  139. EXPECT_EQ(out_test, POINTER_AS_INT(BLI_heap_pop_min(heap)));
  140. }
  141. EXPECT_TRUE(BLI_heap_is_empty(heap));
  142. BLI_heap_free(heap, NULL);
  143. MEM_freeN(nodes);
  144. }
  145. static void random_heap_reinsert_helper(const int items_total, const int random_seed)
  146. {
  147. Heap *heap = BLI_heap_new();
  148. HeapNode **nodes = (HeapNode **)MEM_mallocN(sizeof(HeapNode *) * items_total, __func__);
  149. for (int in = 0; in < items_total; in++) {
  150. nodes[in] = BLI_heap_insert(heap, (float)in, POINTER_FROM_INT(in));
  151. }
  152. BLI_array_randomize(nodes, sizeof(HeapNode *), items_total, random_seed);
  153. for (int i = 0; i < items_total; i++) {
  154. BLI_heap_node_value_update(heap, nodes[i], (float)i);
  155. }
  156. EXPECT_TRUE(BLI_heap_is_valid(heap));
  157. for (int out_test = 0; out_test < items_total; out_test++) {
  158. HeapNode *node_top = BLI_heap_top(heap);
  159. float out = BLI_heap_node_value(node_top);
  160. EXPECT_EQ(out, BLI_heap_top_value(heap));
  161. EXPECT_EQ((float)out_test, out);
  162. BLI_heap_pop_min(heap);
  163. }
  164. EXPECT_TRUE(BLI_heap_is_empty(heap));
  165. BLI_heap_free(heap, NULL);
  166. MEM_freeN(nodes);
  167. }
  168. TEST(heap, ReInsertRandom1)
  169. {
  170. random_heap_reinsert_helper(1, 1234);
  171. }
  172. TEST(heap, ReInsertRandom2)
  173. {
  174. random_heap_reinsert_helper(2, 1234);
  175. }
  176. TEST(heap, ReInsertRandom100)
  177. {
  178. random_heap_reinsert_helper(100, 4321);
  179. }
  180. TEST(heap, ReInsertRandom1024)
  181. {
  182. random_heap_reinsert_helper(1024, 9876);
  183. }
  184. TEST(heap, ReInsertRandom2048)
  185. {
  186. random_heap_reinsert_helper(2048, 5321);
  187. }