BLI_heap_simple_test.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* Apache License, Version 2.0 */
  2. #include "testing/testing.h"
  3. #include <string.h>
  4. extern "C" {
  5. #include "BLI_sys_types.h"
  6. #include "BLI_compiler_attrs.h"
  7. #include "BLI_heap_simple.h"
  8. #include "BLI_utildefines.h"
  9. #include "BLI_rand.h"
  10. #include "MEM_guardedalloc.h"
  11. };
  12. #define SIZE 1024
  13. static void range_fl(float *array_tar, const int size)
  14. {
  15. float *array_pt = array_tar + (size - 1);
  16. int i = size;
  17. while (i--) {
  18. *(array_pt--) = (float)i;
  19. }
  20. }
  21. TEST(heap, SimpleEmpty)
  22. {
  23. HeapSimple *heap;
  24. heap = BLI_heapsimple_new();
  25. EXPECT_TRUE(BLI_heapsimple_is_empty(heap));
  26. EXPECT_EQ(BLI_heapsimple_len(heap), 0);
  27. BLI_heapsimple_free(heap, NULL);
  28. }
  29. TEST(heap, SimpleOne)
  30. {
  31. HeapSimple *heap;
  32. const char *in = "test";
  33. heap = BLI_heapsimple_new();
  34. BLI_heapsimple_insert(heap, 0.0f, (void *)in);
  35. EXPECT_FALSE(BLI_heapsimple_is_empty(heap));
  36. EXPECT_EQ(BLI_heapsimple_len(heap), 1);
  37. EXPECT_EQ(in, BLI_heapsimple_pop_min(heap));
  38. EXPECT_TRUE(BLI_heapsimple_is_empty(heap));
  39. EXPECT_EQ(BLI_heapsimple_len(heap), 0);
  40. BLI_heapsimple_free(heap, NULL);
  41. }
  42. TEST(heap, SimpleRange)
  43. {
  44. const int items_total = SIZE;
  45. HeapSimple *heap = BLI_heapsimple_new();
  46. for (int in = 0; in < items_total; in++) {
  47. BLI_heapsimple_insert(heap, (float)in, POINTER_FROM_INT(in));
  48. }
  49. for (int out_test = 0; out_test < items_total; out_test++) {
  50. EXPECT_EQ(out_test, POINTER_AS_INT(BLI_heapsimple_pop_min(heap)));
  51. }
  52. EXPECT_TRUE(BLI_heapsimple_is_empty(heap));
  53. BLI_heapsimple_free(heap, NULL);
  54. }
  55. TEST(heap, SimpleRangeReverse)
  56. {
  57. const int items_total = SIZE;
  58. HeapSimple *heap = BLI_heapsimple_new();
  59. for (int in = 0; in < items_total; in++) {
  60. BLI_heapsimple_insert(heap, (float)-in, POINTER_FROM_INT(-in));
  61. }
  62. for (int out_test = items_total - 1; out_test >= 0; out_test--) {
  63. EXPECT_EQ(-out_test, POINTER_AS_INT(BLI_heapsimple_pop_min(heap)));
  64. }
  65. EXPECT_TRUE(BLI_heapsimple_is_empty(heap));
  66. BLI_heapsimple_free(heap, NULL);
  67. }
  68. TEST(heap, SimpleDuplicates)
  69. {
  70. const int items_total = SIZE;
  71. HeapSimple *heap = BLI_heapsimple_new();
  72. for (int in = 0; in < items_total; in++) {
  73. BLI_heapsimple_insert(heap, 1.0f, 0);
  74. }
  75. for (int out_test = 0; out_test < items_total; out_test++) {
  76. EXPECT_EQ(0, POINTER_AS_INT(BLI_heapsimple_pop_min(heap)));
  77. }
  78. EXPECT_TRUE(BLI_heapsimple_is_empty(heap));
  79. BLI_heapsimple_free(heap, NULL);
  80. }
  81. static void random_heapsimple_helper(const int items_total, const int random_seed)
  82. {
  83. HeapSimple *heap = BLI_heapsimple_new();
  84. float *values = (float *)MEM_mallocN(sizeof(float) * items_total, __func__);
  85. range_fl(values, items_total);
  86. BLI_array_randomize(values, sizeof(float), items_total, random_seed);
  87. for (int i = 0; i < items_total; i++) {
  88. BLI_heapsimple_insert(heap, values[i], POINTER_FROM_INT((int)values[i]));
  89. }
  90. for (int out_test = 0; out_test < items_total; out_test++) {
  91. EXPECT_EQ(out_test, POINTER_AS_INT(BLI_heapsimple_pop_min(heap)));
  92. }
  93. EXPECT_TRUE(BLI_heapsimple_is_empty(heap));
  94. BLI_heapsimple_free(heap, NULL);
  95. MEM_freeN(values);
  96. }
  97. TEST(heap, SimpleRand1)
  98. {
  99. random_heapsimple_helper(1, 1234);
  100. }
  101. TEST(heap, SimpleRand2)
  102. {
  103. random_heapsimple_helper(2, 1234);
  104. }
  105. TEST(heap, SimpleRand100)
  106. {
  107. random_heapsimple_helper(100, 4321);
  108. }