ArrayListTests.cpp 3.8 KB

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