ListTests.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #include "../Tests.h"
  2. #include "core/List.h"
  3. #include "core/Utility.h"
  4. static void testAdd() {
  5. CoreList list = CORE_LIST(sizeof(size_t));
  6. coreListAdd(&list, 5, size_t);
  7. CORE_TEST_SIZE(5, coreListGet(&list, 0, size_t));
  8. CORE_TEST_SIZE(5, coreListGetC(&list, 0, size_t));
  9. CORE_TEST_SIZE(1, list.length);
  10. coreDestroyList(&list);
  11. }
  12. static void testMultipleAdd() {
  13. CoreList list = CORE_LIST(sizeof(size_t));
  14. coreListAdd(&list, 4, size_t);
  15. coreListAdd(&list, 3, size_t);
  16. coreListAdd(&list, 2, size_t);
  17. CORE_TEST_SIZE(4, coreListGet(&list, 0, size_t));
  18. CORE_TEST_SIZE(3, coreListGet(&list, 1, size_t));
  19. CORE_TEST_SIZE(2, coreListGet(&list, 2, size_t));
  20. CORE_TEST_SIZE(4, coreListGetC(&list, 0, size_t));
  21. CORE_TEST_SIZE(3, coreListGetC(&list, 1, size_t));
  22. CORE_TEST_SIZE(2, coreListGetC(&list, 2, size_t));
  23. CORE_TEST_SIZE(3, list.length);
  24. coreDestroyList(&list);
  25. }
  26. static void testAddReplace() {
  27. CoreList list = CORE_LIST(sizeof(size_t));
  28. coreListAdd(&list, 5, size_t);
  29. coreListGet(&list, 0, size_t) = 3;
  30. CORE_TEST_SIZE(3, coreListGet(&list, 0, size_t));
  31. coreDestroyList(&list);
  32. }
  33. static void testClear() {
  34. CoreList list = CORE_LIST(sizeof(size_t));
  35. coreListAdd(&list, 5, size_t);
  36. coreListAdd(&list, 4, size_t);
  37. coreClearList(&list);
  38. CORE_TEST_SIZE(0, list.length);
  39. coreDestroyList(&list);
  40. }
  41. static void testShrink() {
  42. CoreList list = CORE_LIST(sizeof(size_t));
  43. coreListAdd(&list, 5, size_t);
  44. coreListAdd(&list, 4, size_t);
  45. coreListAdd(&list, 3, size_t);
  46. CORE_TEST_TRUE(list.capacity >= 3);
  47. coreShrinkList(&list);
  48. CORE_TEST_SIZE(3, list.length);
  49. CORE_TEST_SIZE(3, list.capacity);
  50. CORE_TEST_SIZE(5, coreListGet(&list, 0, size_t));
  51. CORE_TEST_SIZE(4, coreListGet(&list, 1, size_t));
  52. CORE_TEST_SIZE(3, coreListGet(&list, 2, size_t));
  53. coreDestroyList(&list);
  54. }
  55. static void testBigAdd(bool light) {
  56. size_t limit = light ? 10000 : 100000;
  57. CoreList list = CORE_LIST(sizeof(size_t));
  58. for(size_t i = 0; i < limit; i++) {
  59. coreListAdd(&list, i, size_t);
  60. }
  61. for(size_t i = 0; i < list.length; i++) {
  62. CORE_TEST_SIZE(i, coreListGet(&list, i, size_t));
  63. }
  64. CORE_TEST_SIZE(limit, list.length);
  65. coreDestroyList(&list);
  66. }
  67. static void testCopy() {
  68. CoreList list = CORE_LIST(sizeof(size_t));
  69. coreListAdd(&list, 1, size_t);
  70. coreListAdd(&list, 2, size_t);
  71. coreListAdd(&list, 3, size_t);
  72. CoreList copy = CORE_LIST(0);
  73. coreCopyList(&copy, &list);
  74. coreCopyList(&copy, &copy);
  75. CORE_TEST_SIZE(list.length, copy.length);
  76. size_t limit = coreMinSize(copy.length, list.length);
  77. for(size_t i = 0; i < limit; i++) {
  78. CORE_TEST_SIZE(coreListGet(&list, i, size_t),
  79. coreListGet(&copy, i, size_t));
  80. }
  81. coreDestroyList(&copy);
  82. coreDestroyList(&list);
  83. }
  84. static void testMove() {
  85. CoreList list = CORE_LIST(sizeof(size_t));
  86. coreListAdd(&list, 1, size_t);
  87. coreListAdd(&list, 2, size_t);
  88. coreListAdd(&list, 3, size_t);
  89. CoreList move = CORE_LIST(0);
  90. coreMoveList(&move, &list);
  91. coreMoveList(&move, &move);
  92. CORE_TEST_SIZE(0, list.length);
  93. CORE_TEST_SIZE(3, move.length);
  94. CORE_TEST_SIZE(1, coreListGet(&move, 0, size_t));
  95. CORE_TEST_SIZE(2, coreListGet(&move, 1, size_t));
  96. CORE_TEST_SIZE(3, coreListGet(&move, 2, size_t));
  97. coreDestroyList(&move);
  98. }
  99. static void testToString1() {
  100. CoreList list = CORE_LIST(sizeof(size_t));
  101. coreListAdd(&list, 1, size_t);
  102. coreListAdd(&list, 243, size_t);
  103. coreListAdd(&list, 423, size_t);
  104. char buffer[128];
  105. size_t n =
  106. coreToStringList(&list, buffer, sizeof(buffer), coreToStringSize);
  107. CORE_TEST_SIZE(13, n);
  108. CORE_TEST_STRING("[1, 243, 423]", buffer);
  109. coreDestroyList(&list);
  110. }
  111. static void testToString2() {
  112. CoreList list = CORE_LIST(sizeof(size_t));
  113. coreListAdd(&list, 1, size_t);
  114. char buffer[128];
  115. size_t n =
  116. coreToStringList(&list, buffer, sizeof(buffer), coreToStringSize);
  117. CORE_TEST_SIZE(3, n);
  118. CORE_TEST_STRING("[1]", buffer);
  119. coreDestroyList(&list);
  120. }
  121. static void testToString3() {
  122. CoreList list = CORE_LIST(sizeof(size_t));
  123. char buffer[128];
  124. size_t n =
  125. coreToStringList(&list, buffer, sizeof(buffer), coreToStringSize);
  126. CORE_TEST_SIZE(2, n);
  127. CORE_TEST_STRING("[]", buffer);
  128. coreDestroyList(&list);
  129. }
  130. static void testRemoveBySwap() {
  131. CoreList list = CORE_LIST(sizeof(size_t));
  132. coreListAdd(&list, 4, size_t);
  133. coreListAdd(&list, 3, size_t);
  134. coreListAdd(&list, 2, size_t);
  135. coreListRemoveBySwap(&list, 0);
  136. CORE_TEST_SIZE(2, coreListGet(&list, 0, size_t));
  137. CORE_TEST_SIZE(3, coreListGet(&list, 1, size_t));
  138. CORE_TEST_SIZE(2, list.length);
  139. coreListRemoveBySwap(&list, 1);
  140. CORE_TEST_SIZE(2, coreListGet(&list, 0, size_t));
  141. CORE_TEST_SIZE(1, list.length);
  142. coreListRemoveBySwap(&list, 0);
  143. CORE_TEST_SIZE(0, list.length);
  144. coreDestroyList(&list);
  145. }
  146. static void testRemove() {
  147. CoreList list = CORE_LIST(sizeof(size_t));
  148. coreListAdd(&list, 4, size_t);
  149. coreListAdd(&list, 3, size_t);
  150. coreListAdd(&list, 2, size_t);
  151. coreListRemove(&list, 0);
  152. CORE_TEST_SIZE(3, coreListGet(&list, 0, size_t));
  153. CORE_TEST_SIZE(2, coreListGet(&list, 1, size_t));
  154. CORE_TEST_SIZE(2, list.length);
  155. coreListRemove(&list, 1);
  156. CORE_TEST_SIZE(3, coreListGet(&list, 0, size_t));
  157. CORE_TEST_SIZE(1, list.length);
  158. coreListRemove(&list, 0);
  159. CORE_TEST_SIZE(0, list.length);
  160. coreDestroyList(&list);
  161. }
  162. static void testRemoveLast() {
  163. CoreList list = CORE_LIST(sizeof(size_t));
  164. coreListAdd(&list, 4, size_t);
  165. coreListAdd(&list, 3, size_t);
  166. coreListAdd(&list, 2, size_t);
  167. coreListRemoveLast(&list);
  168. CORE_TEST_SIZE(4, coreListGet(&list, 0, size_t));
  169. CORE_TEST_SIZE(3, coreListGet(&list, 1, size_t));
  170. CORE_TEST_SIZE(2, list.length);
  171. coreListRemoveLast(&list);
  172. CORE_TEST_SIZE(4, coreListGet(&list, 0, size_t));
  173. CORE_TEST_SIZE(1, list.length);
  174. coreListRemoveLast(&list);
  175. CORE_TEST_SIZE(0, list.length);
  176. coreDestroyList(&list);
  177. }
  178. static void testResize() {
  179. CoreList list = CORE_LIST(sizeof(size_t));
  180. coreResizeListV(&list, 5, 10, size_t);
  181. CORE_TEST_SIZE(5, list.length);
  182. for(size_t i = 0; i < 5; i++) {
  183. CORE_TEST_SIZE(10, coreListGet(&list, i, size_t));
  184. }
  185. coreDestroyList(&list);
  186. }
  187. static void testDefaultResize() {
  188. CoreList list = CORE_LIST(sizeof(size_t));
  189. coreResizeList(&list, 5);
  190. CORE_TEST_SIZE(5, list.length);
  191. for(size_t i = 0; i < 5; i++) {
  192. CORE_TEST_SIZE(0, coreListGet(&list, i, size_t));
  193. }
  194. coreDestroyList(&list);
  195. }
  196. static void testInvalidReserve() {
  197. CoreList list = CORE_LIST(sizeof(size_t));
  198. coreListReserve(&list, 0);
  199. coreDestroyList(&list);
  200. }
  201. static void testShrinkExact() {
  202. CoreList list = CORE_LIST(sizeof(size_t));
  203. coreResizeList(&list, 50);
  204. coreShrinkList(&list);
  205. coreDestroyList(&list);
  206. }
  207. static void testShrinkResize() {
  208. CoreList list = CORE_LIST(sizeof(size_t));
  209. coreResizeList(&list, 50);
  210. CORE_TEST_SIZE(50, list.length);
  211. coreResizeListV(&list, 20, 5, size_t);
  212. CORE_TEST_SIZE(20, list.length);
  213. coreResizeList(&list, 10);
  214. CORE_TEST_SIZE(10, list.length);
  215. coreDestroyList(&list);
  216. }
  217. static void testCopyEmpty() {
  218. CoreList list = CORE_LIST(sizeof(size_t));
  219. CoreList copy = CORE_LIST(0);
  220. coreCopyList(&copy, &list);
  221. coreDestroyList(&copy);
  222. coreDestroyList(&list);
  223. }
  224. void coreTestList(bool light) {
  225. testAdd();
  226. testMultipleAdd();
  227. testAddReplace();
  228. testClear();
  229. testShrink();
  230. testBigAdd(light);
  231. testCopy();
  232. testMove();
  233. testToString1();
  234. testToString2();
  235. testToString3();
  236. testRemoveBySwap();
  237. testRemove();
  238. testRemoveLast();
  239. testResize();
  240. testDefaultResize();
  241. testInvalidReserve();
  242. testShrinkExact();
  243. testShrinkResize();
  244. testCopyEmpty();
  245. }