ArrayListTests.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include "tests/ArrayListTests.h"
  2. #include "data/ArrayList.h"
  3. #include "test/Test.h"
  4. using IntList = Core::ArrayList<int, 20>;
  5. using String = Core::ArrayString<128>;
  6. template<typename T>
  7. static String build(Core::Test& test, const T& t) {
  8. String s;
  9. test.checkFalse(s.append(t), "append works");
  10. return s;
  11. }
  12. static void testAdd(Core::Test& test) {
  13. IntList list;
  14. test.checkFalse(list.add(5), "add works 1");
  15. test.checkEqual(5, list[0], "contains added value");
  16. test.checkEqual(1, list.getLength(), "sizes is increased by add");
  17. }
  18. static void testMultipleAdd(Core::Test& test) {
  19. IntList list;
  20. test.checkFalse(list.add(4), "add works 2");
  21. test.checkFalse(list.add(3), "add works 3");
  22. test.checkFalse(list.add(2), "add works 4");
  23. test.checkEqual(4, list[0], "contains added value 1");
  24. test.checkEqual(3, list[1], "contains added value 2");
  25. test.checkEqual(2, list[2], "contains added value 3");
  26. test.checkEqual(3, list.getLength(), "sizes is increased by add");
  27. }
  28. static void testAddReplace(Core::Test& test) {
  29. IntList list;
  30. test.checkFalse(list.add(5), "add works 5");
  31. list[0] = 3;
  32. test.checkEqual(3, list[0], "value is overwritten");
  33. }
  34. static void testClear(Core::Test& test) {
  35. IntList list;
  36. test.checkFalse(list.add(5), "add works 6");
  37. test.checkFalse(list.add(4), "add works 7");
  38. list.clear();
  39. test.checkEqual(0, list.getLength(), "length is 0 after clear");
  40. }
  41. static void testOverflow(Core::Test& test) {
  42. IntList list;
  43. for(int i = 0; i < 20; i++) {
  44. test.checkEqual(false, list.add(i),
  45. "add returns false without overflow");
  46. }
  47. for(int i = 0; i < 1000000; i++) {
  48. test.checkEqual(true, list.add(i), "add returns true with overflow");
  49. }
  50. for(int i = 0; i < list.getLength(); i++) {
  51. test.checkEqual(i, list[i], "still contains values after overflow");
  52. }
  53. test.checkEqual(true, true, "survives overflow");
  54. }
  55. static void testCopy(Core::Test& test) {
  56. IntList list;
  57. test.checkFalse(list.add(1), "add works 8");
  58. test.checkFalse(list.add(2), "add works 9");
  59. test.checkFalse(list.add(3), "add works 10");
  60. IntList copy(list);
  61. test.checkEqual(list.getLength(), copy.getLength(), "copy has same length");
  62. for(int i = 0; i < copy.getLength() && i < list.getLength(); i++) {
  63. test.checkEqual(list[i], copy[i], "copy has same values");
  64. }
  65. }
  66. static void testCopyAssignment(Core::Test& test) {
  67. IntList list;
  68. test.checkFalse(list.add(1), "add works 11");
  69. test.checkFalse(list.add(2), "add works 12");
  70. test.checkFalse(list.add(3), "add works 13");
  71. IntList copy;
  72. copy = list;
  73. test.checkEqual(list.getLength(), copy.getLength(),
  74. "copy assignment has same length");
  75. for(int i = 0; i < copy.getLength() && i < list.getLength(); i++) {
  76. test.checkEqual(list[i], copy[i], "copy assignment has same values");
  77. }
  78. }
  79. static void testMove(Core::Test& test) {
  80. IntList list;
  81. test.checkFalse(list.add(1), "add works 14");
  82. test.checkFalse(list.add(2), "add works 15");
  83. test.checkFalse(list.add(3), "add works 16");
  84. IntList move(Core::move(list));
  85. test.checkEqual(0, list.getLength(), "moved has length 0");
  86. test.checkEqual(3, move.getLength(), "moved passes length");
  87. test.checkEqual(1, move[0], "moved passes values");
  88. test.checkEqual(2, move[1], "moved passes values");
  89. test.checkEqual(3, move[2], "moved passes values");
  90. }
  91. static void testMoveAssignment(Core::Test& test) {
  92. IntList list;
  93. test.checkFalse(list.add(1), "add works 17");
  94. test.checkFalse(list.add(2), "add works 18");
  95. test.checkFalse(list.add(3), "add works 19");
  96. IntList move;
  97. move = Core::move(list);
  98. test.checkEqual(0, list.getLength(), "assignment moved has length 0");
  99. test.checkEqual(3, move.getLength(), "assignment moved passes length");
  100. test.checkEqual(1, move[0], "assignment moved passes values");
  101. test.checkEqual(2, move[1], "assignment moved passes values");
  102. test.checkEqual(3, move[2], "assignment moved passes values");
  103. }
  104. static void testToString1(Core::Test& test) {
  105. IntList list;
  106. test.checkFalse(list.add(1), "add works 20");
  107. test.checkFalse(list.add(243), "add works 21");
  108. test.checkFalse(list.add(-423), "add works 22");
  109. test.checkEqual(build(test, "[1, 243, -423]"), build(test, list),
  110. "to string 1");
  111. }
  112. static void testToString2(Core::Test& test) {
  113. IntList list;
  114. test.checkFalse(list.add(1), "add works 23");
  115. test.checkEqual(build(test, "[1]"), build(test, list), "to string 2");
  116. }
  117. static void testToString3(Core::Test& test) {
  118. IntList list;
  119. test.checkEqual(build(test, "[]"), build(test, list), "to string 3");
  120. }
  121. static void testRemove(Core::Test& test) {
  122. IntList list;
  123. test.checkFalse(list.add(4), "add works 24");
  124. test.checkFalse(list.add(3), "add works 25");
  125. test.checkFalse(list.add(2), "add works 26");
  126. test.checkFalse(list.removeBySwap(0), "remove by swap works 1");
  127. test.checkEqual(2, list[0], "remove by swap 1");
  128. test.checkEqual(3, list[1], "remove by swap 2");
  129. test.checkEqual(2, list.getLength(), "remove by swap 3");
  130. test.checkFalse(list.removeBySwap(1), "remove by swap works 2");
  131. test.checkEqual(2, list[0], "remove by swap 4");
  132. test.checkEqual(1, list.getLength(), "remove by swap 5");
  133. test.checkFalse(list.removeBySwap(0), "remove by swap works 3");
  134. test.checkEqual(0, list.getLength(), "remove by swap 6");
  135. }
  136. void Core::ArrayListTests::test() {
  137. Core::Test test("ArrayList");
  138. testAdd(test);
  139. testMultipleAdd(test);
  140. testAddReplace(test);
  141. testClear(test);
  142. testOverflow(test);
  143. testCopy(test);
  144. testCopyAssignment(test);
  145. testMove(test);
  146. testMoveAssignment(test);
  147. testToString1(test);
  148. testToString2(test);
  149. testToString3(test);
  150. testRemove(test);
  151. test.finalize();
  152. }