BLI_memiter_test.cc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /* Apache License, Version 2.0 */
  2. #include "testing/testing.h"
  3. extern "C" {
  4. #include "BLI_array_utils.h"
  5. #include "BLI_memiter.h"
  6. #include "MEM_guardedalloc.h"
  7. #include "BLI_string.h"
  8. #include "BLI_ressource_strings.h"
  9. }
  10. TEST(memiter, Nop)
  11. {
  12. BLI_memiter *mi = BLI_memiter_create(64);
  13. BLI_memiter_destroy(mi);
  14. }
  15. void memiter_empty_test(int num_elems, const int chunk_size)
  16. {
  17. BLI_memiter *mi = BLI_memiter_create(chunk_size);
  18. void *data;
  19. for (int index = 0; index < num_elems; index++) {
  20. data = BLI_memiter_alloc(mi, 0);
  21. }
  22. int index = 0, total_size = 0;
  23. BLI_memiter_handle it;
  24. BLI_memiter_iter_init(mi, &it);
  25. uint elem_size;
  26. while ((data = BLI_memiter_iter_step_size(&it, &elem_size))) {
  27. index += 1;
  28. total_size += elem_size;
  29. }
  30. EXPECT_EQ(0, total_size);
  31. EXPECT_EQ(num_elems, index);
  32. BLI_memiter_destroy(mi);
  33. }
  34. #define MEMITER_NUMBER_TEST_FN(fn, number_type) \
  35. void fn(int num_elems, const int chunk_size) \
  36. { \
  37. BLI_memiter *mi = BLI_memiter_create(chunk_size); \
  38. number_type *data; \
  39. for (int index = 0; index < num_elems; index++) { \
  40. data = (number_type *)BLI_memiter_alloc(mi, sizeof(number_type)); \
  41. *data = index; \
  42. } \
  43. BLI_memiter_handle it; \
  44. BLI_memiter_iter_init(mi, &it); \
  45. uint elem_size; \
  46. int index = 0; \
  47. while ((data = (number_type *)BLI_memiter_iter_step_size(&it, &elem_size))) { \
  48. EXPECT_EQ(sizeof(number_type), elem_size); \
  49. EXPECT_EQ(index, *data); \
  50. index += 1; \
  51. } \
  52. BLI_memiter_destroy(mi); \
  53. }
  54. /* generate number functions */
  55. MEMITER_NUMBER_TEST_FN(memiter_char_test, char)
  56. MEMITER_NUMBER_TEST_FN(memiter_short_test, short)
  57. MEMITER_NUMBER_TEST_FN(memiter_int_test, int)
  58. MEMITER_NUMBER_TEST_FN(memiter_long_test, int64_t)
  59. void memiter_string_test(const char *strings[], const int chunk_size)
  60. {
  61. BLI_memiter *mi = BLI_memiter_create(chunk_size);
  62. char *data;
  63. int index = 0;
  64. int total_size_expect = 0;
  65. while (strings[index]) {
  66. const int size = strlen(strings[index]) + 1;
  67. BLI_memiter_alloc_from(mi, size, strings[index]);
  68. total_size_expect += size;
  69. index += 1;
  70. }
  71. const int strings_len = index;
  72. int total_size = 0;
  73. BLI_memiter_handle it;
  74. BLI_memiter_iter_init(mi, &it);
  75. uint elem_size;
  76. index = 0;
  77. while ((data = (char *)BLI_memiter_iter_step_size(&it, &elem_size))) {
  78. EXPECT_EQ(strlen(strings[index]) + 1, elem_size);
  79. EXPECT_STREQ(strings[index], data);
  80. total_size += elem_size;
  81. index += 1;
  82. }
  83. EXPECT_EQ(total_size_expect, total_size);
  84. EXPECT_EQ(strings_len, index);
  85. BLI_memiter_destroy(mi);
  86. }
  87. void memiter_words10k_test(const char split_char, const int chunk_size)
  88. {
  89. const int words_len = sizeof(words10k) - 1;
  90. char *words = BLI_strdupn(words10k, words_len);
  91. BLI_str_replace_char(words, split_char, '\0');
  92. BLI_memiter *mi = BLI_memiter_create(chunk_size);
  93. char *data;
  94. int index;
  95. char *c_end, *c;
  96. c_end = words + words_len;
  97. c = words;
  98. index = 0;
  99. while (c < c_end) {
  100. int elem_size = strlen(c) + 1;
  101. data = (char *)BLI_memiter_alloc(mi, elem_size);
  102. memcpy(data, c, elem_size);
  103. c += elem_size;
  104. index += 1;
  105. }
  106. const int len_expect = index;
  107. c = words;
  108. uint size;
  109. BLI_memiter_handle it;
  110. BLI_memiter_iter_init(mi, &it);
  111. index = 0;
  112. while ((data = (char *)BLI_memiter_iter_step_size(&it, &size))) {
  113. int size_expect = strlen(c) + 1;
  114. EXPECT_EQ(size_expect, size);
  115. EXPECT_STREQ(c, data);
  116. c += size;
  117. index += 1;
  118. }
  119. EXPECT_EQ(len_expect, index);
  120. BLI_memiter_destroy(mi);
  121. MEM_freeN(words);
  122. }
  123. #define TEST_EMPTY_AT_CHUNK_SIZE(chunk_size) \
  124. TEST(memiter, Empty0_##chunk_size) \
  125. { \
  126. memiter_empty_test(0, chunk_size); \
  127. } \
  128. TEST(memiter, Empty1_##chunk_size) \
  129. { \
  130. memiter_empty_test(1, chunk_size); \
  131. } \
  132. TEST(memiter, Empty2_##chunk_size) \
  133. { \
  134. memiter_empty_test(2, chunk_size); \
  135. } \
  136. TEST(memiter, Empty3_##chunk_size) \
  137. { \
  138. memiter_empty_test(3, chunk_size); \
  139. } \
  140. TEST(memiter, Empty13_##chunk_size) \
  141. { \
  142. memiter_empty_test(13, chunk_size); \
  143. } \
  144. TEST(memiter, Empty256_##chunk_size) \
  145. { \
  146. memiter_empty_test(256, chunk_size); \
  147. }
  148. TEST_EMPTY_AT_CHUNK_SIZE(1)
  149. TEST_EMPTY_AT_CHUNK_SIZE(2)
  150. TEST_EMPTY_AT_CHUNK_SIZE(3)
  151. TEST_EMPTY_AT_CHUNK_SIZE(13)
  152. TEST_EMPTY_AT_CHUNK_SIZE(256)
  153. #define TEST_NUMBER_AT_CHUNK_SIZE(chunk_size) \
  154. TEST(memiter, Char1_##chunk_size) \
  155. { \
  156. memiter_char_test(1, chunk_size); \
  157. } \
  158. TEST(memiter, Short1_##chunk_size) \
  159. { \
  160. memiter_short_test(1, chunk_size); \
  161. } \
  162. TEST(memiter, Int1_##chunk_size) \
  163. { \
  164. memiter_int_test(1, chunk_size); \
  165. } \
  166. TEST(memiter, Long1_##chunk_size) \
  167. { \
  168. memiter_long_test(1, chunk_size); \
  169. } \
  170. \
  171. TEST(memiter, Char2_##chunk_size) \
  172. { \
  173. memiter_char_test(2, chunk_size); \
  174. } \
  175. TEST(memiter, Short2_##chunk_size) \
  176. { \
  177. memiter_short_test(2, chunk_size); \
  178. } \
  179. TEST(memiter, Int2_##chunk_size) \
  180. { \
  181. memiter_int_test(2, chunk_size); \
  182. } \
  183. TEST(memiter, Long2_##chunk_size) \
  184. { \
  185. memiter_long_test(2, chunk_size); \
  186. } \
  187. \
  188. TEST(memiter, Char3_##chunk_size) \
  189. { \
  190. memiter_char_test(3, chunk_size); \
  191. } \
  192. TEST(memiter, Short3_##chunk_size) \
  193. { \
  194. memiter_short_test(3, chunk_size); \
  195. } \
  196. TEST(memiter, Int3_##chunk_size) \
  197. { \
  198. memiter_int_test(3, chunk_size); \
  199. } \
  200. TEST(memiter, Long3_##chunk_size) \
  201. { \
  202. memiter_long_test(3, chunk_size); \
  203. } \
  204. \
  205. TEST(memiter, Char256_##chunk_size) \
  206. { \
  207. memiter_char_test(256, chunk_size); \
  208. } \
  209. TEST(memiter, Short256_##chunk_size) \
  210. { \
  211. memiter_short_test(256, chunk_size); \
  212. } \
  213. TEST(memiter, Int256_##chunk_size) \
  214. { \
  215. memiter_int_test(256, chunk_size); \
  216. } \
  217. TEST(memiter, Long256_##chunk_size) \
  218. { \
  219. memiter_long_test(256, chunk_size); \
  220. }
  221. TEST_NUMBER_AT_CHUNK_SIZE(1)
  222. TEST_NUMBER_AT_CHUNK_SIZE(2)
  223. TEST_NUMBER_AT_CHUNK_SIZE(3)
  224. TEST_NUMBER_AT_CHUNK_SIZE(13)
  225. TEST_NUMBER_AT_CHUNK_SIZE(256)
  226. #define STRINGS_TEST(chunk_size, ...) \
  227. { \
  228. const char *data[] = {__VA_ARGS__, NULL}; \
  229. memiter_string_test(data, chunk_size); \
  230. }
  231. #define TEST_STRINGS_AT_CHUNK_SIZE(chunk_size) \
  232. TEST(memiter, Strings_##chunk_size) \
  233. { \
  234. STRINGS_TEST(chunk_size, ""); \
  235. STRINGS_TEST(chunk_size, "test", "me"); \
  236. STRINGS_TEST(chunk_size, "more", "test", "data", "to", "follow"); \
  237. }
  238. TEST_STRINGS_AT_CHUNK_SIZE(1)
  239. TEST_STRINGS_AT_CHUNK_SIZE(2)
  240. TEST_STRINGS_AT_CHUNK_SIZE(3)
  241. TEST_STRINGS_AT_CHUNK_SIZE(13)
  242. TEST_STRINGS_AT_CHUNK_SIZE(256)
  243. #define TEST_WORDS10K_AT_CHUNK_SIZE(chunk_size) \
  244. TEST(memiter, Words10kSentence_##chunk_size) \
  245. { \
  246. memiter_words10k_test('.', chunk_size); \
  247. } \
  248. TEST(memiter, Words10kWords_##chunk_size) \
  249. { \
  250. memiter_words10k_test(' ', chunk_size); \
  251. }
  252. TEST_WORDS10K_AT_CHUNK_SIZE(1)
  253. TEST_WORDS10K_AT_CHUNK_SIZE(2)
  254. TEST_WORDS10K_AT_CHUNK_SIZE(3)
  255. TEST_WORDS10K_AT_CHUNK_SIZE(13)
  256. TEST_WORDS10K_AT_CHUNK_SIZE(256)