ArrayListTests.cpp 3.3 KB

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