BLI_stack_test.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* Apache License, Version 2.0 */
  2. #include "testing/testing.h"
  3. #include <string.h>
  4. extern "C" {
  5. #include "BLI_stack.h"
  6. #include "BLI_utildefines.h"
  7. #include "BLI_array.h"
  8. };
  9. #define SIZE 1024
  10. /* number of items per chunk. use a small value to expose bugs */
  11. #define STACK_CHUNK_SIZE 8
  12. /* Ensure block size is set to #STACK_NEW_EX_ARGS */
  13. #define BLI_stack_new(esize, descr) BLI_stack_new_ex(esize, descr, esize *STACK_CHUNK_SIZE)
  14. TEST(stack, Empty)
  15. {
  16. BLI_Stack *stack;
  17. stack = BLI_stack_new(sizeof(int), __func__);
  18. EXPECT_TRUE(BLI_stack_is_empty(stack));
  19. EXPECT_EQ(BLI_stack_count(stack), 0);
  20. BLI_stack_free(stack);
  21. }
  22. TEST(stack, One)
  23. {
  24. BLI_Stack *stack;
  25. unsigned int in = -1, out = 1;
  26. stack = BLI_stack_new(sizeof(in), __func__);
  27. BLI_stack_push(stack, (void *)&in);
  28. EXPECT_FALSE(BLI_stack_is_empty(stack));
  29. EXPECT_EQ(BLI_stack_count(stack), 1);
  30. BLI_stack_pop(stack, (void *)&out);
  31. EXPECT_EQ(out, in);
  32. EXPECT_TRUE(BLI_stack_is_empty(stack));
  33. EXPECT_EQ(BLI_stack_count(stack), 0);
  34. BLI_stack_free(stack);
  35. }
  36. TEST(stack, Range)
  37. {
  38. const int tot = SIZE;
  39. BLI_Stack *stack;
  40. int in, out;
  41. stack = BLI_stack_new(sizeof(in), __func__);
  42. for (in = 0; in < tot; in++) {
  43. BLI_stack_push(stack, (void *)&in);
  44. }
  45. for (in = tot - 1; in >= 0; in--) {
  46. EXPECT_FALSE(BLI_stack_is_empty(stack));
  47. BLI_stack_pop(stack, (void *)&out);
  48. EXPECT_EQ(out, in);
  49. }
  50. EXPECT_TRUE(BLI_stack_is_empty(stack));
  51. BLI_stack_free(stack);
  52. }
  53. TEST(stack, String)
  54. {
  55. const int tot = SIZE;
  56. int i;
  57. BLI_Stack *stack;
  58. char in[] = "hello world!";
  59. char out[sizeof(in)];
  60. stack = BLI_stack_new(sizeof(in), __func__);
  61. for (i = 0; i < tot; i++) {
  62. *((int *)in) = i;
  63. BLI_stack_push(stack, (void *)in);
  64. }
  65. for (i = tot - 1; i >= 0; i--) {
  66. EXPECT_FALSE(BLI_stack_is_empty(stack));
  67. *((int *)in) = i;
  68. BLI_stack_pop(stack, (void *)&out);
  69. EXPECT_STREQ(in, out);
  70. }
  71. EXPECT_TRUE(BLI_stack_is_empty(stack));
  72. BLI_stack_free(stack);
  73. }
  74. TEST(stack, Peek)
  75. {
  76. const int tot = SIZE;
  77. int i;
  78. BLI_Stack *stack;
  79. const short in[] = {1, 10, 100, 1000};
  80. stack = BLI_stack_new(sizeof(*in), __func__);
  81. for (i = 0; i < tot; i++) {
  82. BLI_stack_push(stack, &in[i % ARRAY_SIZE(in)]);
  83. }
  84. for (i = tot - 1; i >= 0; i--, BLI_stack_discard(stack)) {
  85. short *ret = (short *)BLI_stack_peek(stack);
  86. EXPECT_EQ(*ret, in[i % ARRAY_SIZE(in)]);
  87. }
  88. EXPECT_TRUE(BLI_stack_is_empty(stack));
  89. BLI_stack_free(stack);
  90. }
  91. /* Check that clearing the stack leaves in it a correct state. */
  92. TEST(stack, Clear)
  93. {
  94. const int tot_rerun = 4;
  95. int rerun;
  96. /* based on range test */
  97. int tot = SIZE;
  98. BLI_Stack *stack;
  99. int in, out;
  100. /* use a small chunk size to ensure we test */
  101. stack = BLI_stack_new(sizeof(in), __func__);
  102. for (rerun = 0; rerun < tot_rerun; rerun++) {
  103. for (in = 0; in < tot; in++) {
  104. BLI_stack_push(stack, (void *)&in);
  105. }
  106. BLI_stack_clear(stack);
  107. EXPECT_TRUE(BLI_stack_is_empty(stack));
  108. /* and again, this time check its valid */
  109. for (in = 0; in < tot; in++) {
  110. BLI_stack_push(stack, (void *)&in);
  111. }
  112. for (in = tot - 1; in >= 0; in--) {
  113. EXPECT_FALSE(BLI_stack_is_empty(stack));
  114. BLI_stack_pop(stack, (void *)&out);
  115. EXPECT_EQ(out, in);
  116. }
  117. EXPECT_TRUE(BLI_stack_is_empty(stack));
  118. /* without this, we wont test case when mixed free/used */
  119. tot /= 2;
  120. }
  121. BLI_stack_free(stack);
  122. }
  123. TEST(stack, Reuse)
  124. {
  125. const int sizes[] = {3, 11, 81, 400, 999, 12, 1, 9721, 7, 99, 5, 0};
  126. int sizes_test[ARRAY_SIZE(sizes)];
  127. const int *s;
  128. int out, i;
  129. int sum, sum_test;
  130. BLI_Stack *stack;
  131. stack = BLI_stack_new(sizeof(i), __func__);
  132. /* add a bunch of numbers, ensure we get same sum out */
  133. sum = 0;
  134. for (s = sizes; *s; s++) {
  135. for (i = *s; i != 0; i--) {
  136. BLI_stack_push(stack, (void *)&i);
  137. sum += i;
  138. }
  139. }
  140. sum_test = 0;
  141. while (!BLI_stack_is_empty(stack)) {
  142. BLI_stack_pop(stack, (void *)&out);
  143. sum_test += out;
  144. }
  145. EXPECT_EQ(sum, sum_test);
  146. /* add and remove all except last */
  147. for (s = sizes; *s; s++) {
  148. for (i = *s; i >= 0; i--) {
  149. BLI_stack_push(stack, (void *)&i);
  150. }
  151. for (i = *s; i > 0; i--) {
  152. BLI_stack_pop(stack, (void *)&out);
  153. }
  154. }
  155. i = ARRAY_SIZE(sizes) - 1;
  156. while (!BLI_stack_is_empty(stack)) {
  157. i--;
  158. BLI_stack_pop(stack, (void *)&sizes_test[i]);
  159. EXPECT_EQ(sizes_test[i], sizes[i]);
  160. EXPECT_GT(i, -1);
  161. }
  162. EXPECT_EQ(0, i);
  163. EXPECT_EQ(memcmp(sizes, sizes_test, sizeof(sizes) - sizeof(int)), 0);
  164. /* finally test BLI_stack_pop_n */
  165. for (i = ARRAY_SIZE(sizes); i--;) {
  166. BLI_stack_push(stack, (void *)&sizes[i]);
  167. }
  168. EXPECT_EQ(BLI_stack_count(stack), ARRAY_SIZE(sizes));
  169. BLI_stack_pop_n(stack, (void *)sizes_test, ARRAY_SIZE(sizes));
  170. EXPECT_EQ(memcmp(sizes, sizes_test, sizeof(sizes) - sizeof(int)), 0);
  171. BLI_stack_free(stack);
  172. }