ArrayListTests.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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(bool light) {
  34. IntList list;
  35. for(int i = 0; i < 20; i++) {
  36. CORE_TEST_ERROR(list.add(i));
  37. }
  38. int limit = light ? 1000 : 100000;
  39. for(int i = 0; i < limit; 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::testArrayList(bool light) {
  126. testAdd();
  127. testMultipleAdd();
  128. testAddReplace();
  129. testClear();
  130. testOverflow(light);
  131. testCopy();
  132. testCopyAssignment();
  133. testMove();
  134. testMoveAssignment();
  135. testToString1();
  136. testToString2();
  137. testToString3();
  138. testRemove();
  139. }