ArrayListTests.cpp 5.7 KB

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