BLI_array_utils_test.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /* Apache License, Version 2.0 */
  2. #include "testing/testing.h"
  3. extern "C" {
  4. #include "BLI_utildefines.h"
  5. #include "BLI_array_utils.h"
  6. #include "BLI_utildefines_stack.h"
  7. }
  8. /* -------------------------------------------------------------------- */
  9. /* tests */
  10. /* BLI_array_reverse */
  11. TEST(array_utils, ReverseStringEmpty)
  12. {
  13. char data[] = "";
  14. BLI_array_reverse(data, ARRAY_SIZE(data) - 1);
  15. EXPECT_STREQ("", data);
  16. }
  17. TEST(array_utils, ReverseStringSingle)
  18. {
  19. char data[] = "0";
  20. BLI_array_reverse(data, ARRAY_SIZE(data) - 1);
  21. EXPECT_STREQ("0", data);
  22. }
  23. TEST(array_utils, ReverseString4)
  24. {
  25. char data[] = "0123";
  26. BLI_array_reverse(data, ARRAY_SIZE(data) - 1);
  27. EXPECT_STREQ("3210", data);
  28. }
  29. TEST(array_utils, ReverseInt4)
  30. {
  31. const int data_cmp[] = {3, 2, 1, 0};
  32. int data[] = {0, 1, 2, 3};
  33. BLI_array_reverse(data, ARRAY_SIZE(data));
  34. EXPECT_EQ_ARRAY(data_cmp, data, ARRAY_SIZE(data));
  35. }
  36. /* BLI_array_findindex */
  37. TEST(array_utils, FindIndexStringEmpty)
  38. {
  39. char data[] = "", find = '0';
  40. EXPECT_EQ(BLI_array_findindex(data, ARRAY_SIZE(data) - 1, &find), -1);
  41. EXPECT_EQ(BLI_array_rfindindex(data, ARRAY_SIZE(data) - 1, &find), -1);
  42. }
  43. TEST(array_utils, FindIndexStringSingle)
  44. {
  45. char data[] = "0", find = '0';
  46. EXPECT_EQ(BLI_array_findindex(data, ARRAY_SIZE(data) - 1, &find), 0);
  47. EXPECT_EQ(BLI_array_rfindindex(data, ARRAY_SIZE(data) - 1, &find), 0);
  48. }
  49. TEST(array_utils, FindIndexStringSingleMissing)
  50. {
  51. char data[] = "1", find = '0';
  52. EXPECT_EQ(BLI_array_findindex(data, ARRAY_SIZE(data) - 1, &find), -1);
  53. EXPECT_EQ(BLI_array_rfindindex(data, ARRAY_SIZE(data) - 1, &find), -1);
  54. }
  55. TEST(array_utils, FindIndexString4)
  56. {
  57. char data[] = "0123", find = '3';
  58. EXPECT_EQ(BLI_array_findindex(data, ARRAY_SIZE(data) - 1, &find), 3);
  59. EXPECT_EQ(BLI_array_rfindindex(data, ARRAY_SIZE(data) - 1, &find), 3);
  60. }
  61. TEST(array_utils, FindIndexInt4)
  62. {
  63. int data[] = {0, 1, 2, 3}, find = 3;
  64. EXPECT_EQ(BLI_array_findindex(data, ARRAY_SIZE(data), &find), 3);
  65. EXPECT_EQ(BLI_array_rfindindex(data, ARRAY_SIZE(data), &find), 3);
  66. }
  67. TEST(array_utils, FindIndexInt4_DupeEnd)
  68. {
  69. int data[] = {0, 1, 2, 0}, find = 0;
  70. EXPECT_EQ(BLI_array_findindex(data, ARRAY_SIZE(data), &find), 0);
  71. EXPECT_EQ(BLI_array_rfindindex(data, ARRAY_SIZE(data), &find), 3);
  72. }
  73. TEST(array_utils, FindIndexInt4_DupeMid)
  74. {
  75. int data[] = {1, 0, 0, 3}, find = 0;
  76. EXPECT_EQ(BLI_array_findindex(data, ARRAY_SIZE(data), &find), 1);
  77. EXPECT_EQ(BLI_array_rfindindex(data, ARRAY_SIZE(data), &find), 2);
  78. }
  79. TEST(array_utils, FindIndexPointer)
  80. {
  81. const char *data[4] = {NULL};
  82. STACK_DECLARE(data);
  83. STACK_INIT(data, ARRAY_SIZE(data));
  84. const char *a = "a", *b = "b", *c = "c", *d = "d";
  85. #define STACK_PUSH_AND_CHECK_FORWARD(v, i) \
  86. { \
  87. STACK_PUSH(data, v); \
  88. EXPECT_EQ(BLI_array_findindex(data, STACK_SIZE(data), &(v)), i); \
  89. } \
  90. ((void)0)
  91. #define STACK_PUSH_AND_CHECK_BACKWARD(v, i) \
  92. { \
  93. STACK_PUSH(data, v); \
  94. EXPECT_EQ(BLI_array_rfindindex(data, STACK_SIZE(data), &(v)), i); \
  95. } \
  96. ((void)0)
  97. #define STACK_PUSH_AND_CHECK_BOTH(v, i) \
  98. { \
  99. STACK_PUSH(data, v); \
  100. EXPECT_EQ(BLI_array_findindex(data, STACK_SIZE(data), &(v)), i); \
  101. EXPECT_EQ(BLI_array_rfindindex(data, STACK_SIZE(data), &(v)), i); \
  102. } \
  103. ((void)0)
  104. STACK_PUSH_AND_CHECK_BOTH(a, 0);
  105. STACK_PUSH_AND_CHECK_BOTH(b, 1);
  106. STACK_PUSH_AND_CHECK_BOTH(c, 2);
  107. STACK_PUSH_AND_CHECK_BOTH(d, 3);
  108. STACK_POP(data);
  109. STACK_PUSH_AND_CHECK_BACKWARD(a, 3);
  110. STACK_POP(data);
  111. STACK_PUSH_AND_CHECK_FORWARD(a, 0);
  112. STACK_POP(data);
  113. STACK_POP(data);
  114. STACK_PUSH_AND_CHECK_BACKWARD(b, 2);
  115. STACK_PUSH_AND_CHECK_BACKWARD(a, 3);
  116. #undef STACK_PUSH_AND_CHECK_FORWARD
  117. #undef STACK_PUSH_AND_CHECK_BACKWARD
  118. #undef STACK_PUSH_AND_CHECK_BOTH
  119. }
  120. /* BLI_array_binary_and */
  121. #define BINARY_AND_TEST(data_cmp, data_a, data_b, data_combine, length) \
  122. { \
  123. BLI_array_binary_and(data_combine, data_a, data_b, length); \
  124. EXPECT_EQ_ARRAY(data_cmp, data_combine, length); \
  125. } \
  126. ((void)0)
  127. TEST(array_utils, BinaryAndInt4Zero)
  128. {
  129. const int data_cmp[] = {0, 0, 0, 0};
  130. int data_a[] = {0, 1, 0, 1}, data_b[] = {1, 0, 1, 0};
  131. int data_combine[ARRAY_SIZE(data_cmp)];
  132. BINARY_AND_TEST(data_cmp, data_a, data_b, data_combine, ARRAY_SIZE(data_cmp));
  133. }
  134. TEST(array_utils, BinaryAndInt4Mix)
  135. {
  136. const int data_cmp[] = {1, 0, 1, 0};
  137. int data_a[] = {1, 1, 1, 1}, data_b[] = {1, 0, 1, 0};
  138. int data_combine[ARRAY_SIZE(data_cmp)];
  139. BINARY_AND_TEST(data_cmp, data_a, data_b, data_combine, ARRAY_SIZE(data_cmp));
  140. }
  141. #undef BINARY_AND_TEST
  142. /* BLI_array_binary_or */
  143. #define BINARY_OR_TEST(data_cmp, data_a, data_b, data_combine, length) \
  144. { \
  145. BLI_array_binary_or(data_combine, data_a, data_b, length); \
  146. EXPECT_EQ_ARRAY(data_combine, data_cmp, length); \
  147. } \
  148. ((void)0)
  149. TEST(array_utils, BinaryOrInt4Alternate)
  150. {
  151. int data_a[] = {0, 1, 0, 1}, data_b[] = {1, 0, 1, 0}, data_cmp[] = {1, 1, 1, 1};
  152. int data_combine[ARRAY_SIZE(data_cmp)];
  153. BINARY_OR_TEST(data_cmp, data_a, data_b, data_combine, ARRAY_SIZE(data_cmp));
  154. }
  155. TEST(array_utils, BinaryOrInt4Mix)
  156. {
  157. int data_a[] = {1, 1, 0, 0}, data_b[] = {0, 0, 1, 0}, data_cmp[] = {1, 1, 1, 0};
  158. int data_combine[ARRAY_SIZE(data_cmp)];
  159. BINARY_OR_TEST(data_cmp, data_a, data_b, data_combine, ARRAY_SIZE(data_cmp));
  160. }
  161. #undef BINARY_OR_TEST