ListTests.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #include "../Tests.h"
  2. #include "core/List.h"
  3. #include "core/ToString.h"
  4. #include "core/Vector.h"
  5. LIST(size_t, Size)
  6. LIST(Vector3, V3)
  7. LIST_SOURCE(Vector3, V3)
  8. LIST(int, Int)
  9. static void testAdd() {
  10. ListSize list;
  11. initListSize(&list);
  12. addListDataSize(&list, 5);
  13. TEST_SIZE(5, *getListIndexSize(&list, 0));
  14. TEST_SIZE(1, list.length);
  15. destroyListSize(&list);
  16. }
  17. static void testMultipleAdd() {
  18. ListSize list;
  19. initListSize(&list);
  20. addListDataSize(&list, 4);
  21. addListDataSize(&list, 3);
  22. addListDataSize(&list, 2);
  23. TEST_SIZE(4, *getListIndexSize(&list, 0));
  24. TEST_SIZE(3, *getListIndexSize(&list, 1));
  25. TEST_SIZE(2, *getListIndexSize(&list, 2));
  26. TEST_SIZE(2, *getListLastSize(&list));
  27. TEST_SIZE(3, list.length);
  28. destroyListSize(&list);
  29. }
  30. static void testAddLast() {
  31. ListSize list;
  32. initListSize(&list);
  33. addListDataSize(&list, 4);
  34. addListDataSize(&list, 3);
  35. addLastListDataSize(&list);
  36. TEST_SIZE(4, *getListIndexSize(&list, 0));
  37. TEST_SIZE(3, *getListIndexSize(&list, 1));
  38. TEST_SIZE(3, *getListIndexSize(&list, 2));
  39. TEST_SIZE(3, list.length);
  40. destroyListSize(&list);
  41. }
  42. static void testAddReplace() {
  43. ListSize list;
  44. initListSize(&list);
  45. addListDataSize(&list, 5);
  46. *getListIndexSize(&list, 0) = 3;
  47. TEST_SIZE(3, *getListIndexSize(&list, 0));
  48. destroyListSize(&list);
  49. }
  50. static void testClear() {
  51. ListSize list;
  52. initListSize(&list);
  53. addListDataSize(&list, 5);
  54. addListDataSize(&list, 4);
  55. clearListSize(&list);
  56. TEST_SIZE(0, list.length);
  57. destroyListSize(&list);
  58. }
  59. static void testBigAdd(bool light) {
  60. size_t limit = light ? 10000 : 100000;
  61. ListSize list;
  62. initListSize(&list);
  63. for(size_t i = 0; i < limit; i++) {
  64. addListDataSize(&list, i);
  65. }
  66. for(size_t i = 0; i < list.length; i++) {
  67. TEST_SIZE(i, *getListIndexSize(&list, i));
  68. }
  69. TEST_SIZE(limit, list.length);
  70. destroyListSize(&list);
  71. }
  72. static void testToString1() {
  73. ListSize list;
  74. initListSize(&list);
  75. addListDataSize(&list, 1);
  76. addListDataSize(&list, 243);
  77. addListDataSize(&list, 423);
  78. char buffer[128];
  79. size_t n = toStringListSize(&list, buffer, sizeof(buffer), toStringSize);
  80. TEST_SIZE(13, n);
  81. TEST_STRING("[1, 243, 423]", buffer);
  82. destroyListSize(&list);
  83. }
  84. static void testToString2() {
  85. ListSize list;
  86. initListSize(&list);
  87. addListDataSize(&list, 1);
  88. char buffer[128];
  89. size_t n = toStringListSize(&list, buffer, sizeof(buffer), toStringSize);
  90. TEST_SIZE(3, n);
  91. TEST_STRING("[1]", buffer);
  92. destroyListSize(&list);
  93. }
  94. static void testToString3() {
  95. ListSize list;
  96. initListSize(&list);
  97. char buffer[128];
  98. size_t n = toStringListSize(&list, buffer, sizeof(buffer), toStringSize);
  99. TEST_SIZE(2, n);
  100. TEST_STRING("[]", buffer);
  101. destroyListSize(&list);
  102. }
  103. static void testRemoveBySwap() {
  104. ListSize list;
  105. initListSize(&list);
  106. addListDataSize(&list, 4);
  107. addListDataSize(&list, 3);
  108. addListDataSize(&list, 2);
  109. removeListIndexBySwapSize(&list, 0);
  110. TEST_SIZE(2, *getListIndexSize(&list, 0));
  111. TEST_SIZE(3, *getListIndexSize(&list, 1));
  112. TEST_SIZE(2, list.length);
  113. removeListIndexBySwapSize(&list, 1);
  114. TEST_SIZE(2, *getListIndexSize(&list, 0));
  115. TEST_SIZE(1, list.length);
  116. removeListIndexBySwapSize(&list, 0);
  117. TEST_SIZE(0, list.length);
  118. destroyListSize(&list);
  119. }
  120. static void testRemove() {
  121. ListSize list;
  122. initListSize(&list);
  123. addListDataSize(&list, 4);
  124. addListDataSize(&list, 3);
  125. addListDataSize(&list, 2);
  126. removeListIndexSize(&list, 0);
  127. TEST_SIZE(3, *getListIndexSize(&list, 0));
  128. TEST_SIZE(2, *getListIndexSize(&list, 1));
  129. TEST_SIZE(2, list.length);
  130. removeListIndexSize(&list, 1);
  131. TEST_SIZE(3, *getListIndexSize(&list, 0));
  132. TEST_SIZE(1, list.length);
  133. removeListIndexSize(&list, 0);
  134. TEST_SIZE(0, list.length);
  135. destroyListSize(&list);
  136. }
  137. static void testRemoveLast() {
  138. ListSize list;
  139. initListSize(&list);
  140. addListDataSize(&list, 4);
  141. addListDataSize(&list, 3);
  142. addListDataSize(&list, 2);
  143. removeListLastSize(&list);
  144. TEST_SIZE(4, *getListIndexSize(&list, 0));
  145. TEST_SIZE(3, *getListIndexSize(&list, 1));
  146. TEST_SIZE(2, list.length);
  147. removeListLastSize(&list);
  148. TEST_SIZE(4, *getListIndexSize(&list, 0));
  149. TEST_SIZE(1, list.length);
  150. removeListLastSize(&list);
  151. TEST_SIZE(0, list.length);
  152. destroyListSize(&list);
  153. }
  154. static void testInvalidReserve() {
  155. ListSize list;
  156. initListSize(&list);
  157. reserveListEntriesSize(&list, 0);
  158. destroyListSize(&list);
  159. }
  160. static void testStruct() {
  161. ListV3 list;
  162. Vector3 v = {{1, 2, 3}};
  163. initListV3(&list);
  164. addListDataV3(&list, v);
  165. addListDataV3(&list, (Vector3){{2, 3, 4}});
  166. char buffer[128];
  167. size_t n = toStringListV3(&list, buffer, sizeof(buffer), toStringV3);
  168. TEST_SIZE(46, n);
  169. TEST_STRING("[[1.000, 2.000, 3.000], [2.000, 3.000, 4.000]]", buffer);
  170. addLastListDataV3(&list);
  171. addLastListDataV3(&list);
  172. TEST_SIZE(4, (size_t)(getListEndV3(&list) - getListStartV3(&list)));
  173. removeListIndexV3(&list, 1);
  174. removeListIndexBySwapV3(&list, 1);
  175. removeListLastV3(&list);
  176. Vector3* entry = getListIndexV3(&list, 0);
  177. TEST_V3(&v, entry);
  178. clearListV3(&list);
  179. TEST_SIZE(0, list.length);
  180. destroyListV3(&list);
  181. }
  182. static void testIntList() {
  183. ListInt list;
  184. initListInt(&list);
  185. addListDataInt(&list, 5);
  186. addListDataInt(&list, 7);
  187. char buffer[128];
  188. size_t n = toStringListInt(&list, buffer, sizeof(buffer), toStringInt);
  189. TEST_SIZE(6, n);
  190. TEST_STRING("[5, 7]", buffer);
  191. addLastListDataInt(&list);
  192. addLastListDataInt(&list);
  193. TEST_SIZE(4, (size_t)(getListEndInt(&list) - getListStartInt(&list)));
  194. removeListIndexInt(&list, 1);
  195. removeListIndexBySwapInt(&list, 1);
  196. removeListLastInt(&list);
  197. int* entry = getListIndexInt(&list, 0);
  198. TEST_INT(5, *entry);
  199. clearListInt(&list);
  200. TEST_SIZE(0, list.length);
  201. destroyListInt(&list);
  202. }
  203. void testList(bool light) {
  204. testAdd();
  205. testMultipleAdd();
  206. testAddLast();
  207. testAddReplace();
  208. testClear();
  209. testBigAdd(light);
  210. testToString1();
  211. testToString2();
  212. testToString3();
  213. testRemoveBySwap();
  214. testRemove();
  215. testRemoveLast();
  216. testInvalidReserve();
  217. testStruct();
  218. testIntList();
  219. }