ArrayListTests.cpp 4.5 KB

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