ArrayListTests.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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() {
  6. IntList list;
  7. CORE_TEST_ERROR(list.add(5));
  8. CORE_TEST_EQUAL(5, list[0]);
  9. CORE_TEST_EQUAL(1, list.getLength());
  10. }
  11. static void testMultipleAdd() {
  12. IntList list;
  13. CORE_TEST_ERROR(list.add(4));
  14. CORE_TEST_ERROR(list.add(3));
  15. CORE_TEST_ERROR(list.add(2));
  16. CORE_TEST_EQUAL(4, list[0]);
  17. CORE_TEST_EQUAL(3, list[1]);
  18. CORE_TEST_EQUAL(2, list[2]);
  19. CORE_TEST_EQUAL(3, list.getLength());
  20. }
  21. static void testAddReplace() {
  22. IntList list;
  23. CORE_TEST_ERROR(list.add(5));
  24. list[0] = 3;
  25. CORE_TEST_EQUAL(3, list[0]);
  26. }
  27. static void testClear() {
  28. IntList list;
  29. CORE_TEST_ERROR(list.add(5));
  30. CORE_TEST_ERROR(list.add(4));
  31. list.clear();
  32. CORE_TEST_EQUAL(0, list.getLength());
  33. }
  34. static void testOverflow() {
  35. IntList list;
  36. for(int i = 0; i < 20; i++) {
  37. CORE_TEST_ERROR(list.add(i));
  38. }
  39. for(int i = 0; i < 100000; i++) {
  40. CORE_TEST_EQUAL(Core::Error::CAPACITY_REACHED, list.add(i));
  41. }
  42. for(int i = 0; i < list.getLength(); i++) {
  43. CORE_TEST_EQUAL(i, list[i]);
  44. }
  45. }
  46. static void testCopy() {
  47. IntList list;
  48. CORE_TEST_ERROR(list.add(1));
  49. CORE_TEST_ERROR(list.add(2));
  50. CORE_TEST_ERROR(list.add(3));
  51. IntList copy(list);
  52. CORE_TEST_EQUAL(list.getLength(), copy.getLength());
  53. for(int i = 0; i < copy.getLength() && i < list.getLength(); i++) {
  54. CORE_TEST_EQUAL(list[i], copy[i]);
  55. }
  56. }
  57. static void testCopyAssignment() {
  58. IntList list;
  59. CORE_TEST_ERROR(list.add(1));
  60. CORE_TEST_ERROR(list.add(2));
  61. CORE_TEST_ERROR(list.add(3));
  62. IntList copy;
  63. copy = list;
  64. CORE_TEST_EQUAL(list.getLength(), copy.getLength());
  65. for(int i = 0; i < copy.getLength() && i < list.getLength(); i++) {
  66. CORE_TEST_EQUAL(list[i], copy[i]);
  67. }
  68. }
  69. static void testMove() {
  70. IntList list;
  71. CORE_TEST_ERROR(list.add(1));
  72. CORE_TEST_ERROR(list.add(2));
  73. CORE_TEST_ERROR(list.add(3));
  74. IntList move(Core::move(list));
  75. CORE_TEST_EQUAL(0, list.getLength());
  76. CORE_TEST_EQUAL(3, move.getLength());
  77. CORE_TEST_EQUAL(1, move[0]);
  78. CORE_TEST_EQUAL(2, move[1]);
  79. CORE_TEST_EQUAL(3, move[2]);
  80. }
  81. static void testMoveAssignment() {
  82. IntList list;
  83. CORE_TEST_ERROR(list.add(1));
  84. CORE_TEST_ERROR(list.add(2));
  85. CORE_TEST_ERROR(list.add(3));
  86. IntList move;
  87. move = Core::move(list);
  88. CORE_TEST_EQUAL(0, list.getLength());
  89. CORE_TEST_EQUAL(3, move.getLength());
  90. CORE_TEST_EQUAL(1, move[0]);
  91. CORE_TEST_EQUAL(2, move[1]);
  92. CORE_TEST_EQUAL(3, move[2]);
  93. }
  94. static void testToString1() {
  95. IntList list;
  96. CORE_TEST_ERROR(list.add(1));
  97. CORE_TEST_ERROR(list.add(243));
  98. CORE_TEST_ERROR(list.add(-423));
  99. CORE_TEST_STRING("[1, 243, -423]", list);
  100. }
  101. static void testToString2() {
  102. IntList list;
  103. CORE_TEST_ERROR(list.add(1));
  104. CORE_TEST_STRING("[1]", list);
  105. }
  106. static void testToString3() {
  107. IntList list;
  108. CORE_TEST_STRING("[]", list);
  109. }
  110. static void testRemove() {
  111. IntList list;
  112. CORE_TEST_ERROR(list.add(4));
  113. CORE_TEST_ERROR(list.add(3));
  114. CORE_TEST_ERROR(list.add(2));
  115. CORE_TEST_ERROR(list.removeBySwap(0));
  116. CORE_TEST_EQUAL(2, list[0]);
  117. CORE_TEST_EQUAL(3, list[1]);
  118. CORE_TEST_EQUAL(2, list.getLength());
  119. CORE_TEST_ERROR(list.removeBySwap(1));
  120. CORE_TEST_EQUAL(2, list[0]);
  121. CORE_TEST_EQUAL(1, list.getLength());
  122. CORE_TEST_ERROR(list.removeBySwap(0));
  123. CORE_TEST_EQUAL(0, list.getLength());
  124. }
  125. void Core::ArrayListTests::test() {
  126. testAdd();
  127. testMultipleAdd();
  128. testAddReplace();
  129. testClear();
  130. testOverflow();
  131. testCopy();
  132. testCopyAssignment();
  133. testMove();
  134. testMoveAssignment();
  135. testToString1();
  136. testToString2();
  137. testToString3();
  138. testRemove();
  139. }