ListTests.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #include "tests/ListTests.h"
  2. #include "data/List.h"
  3. #include "tests/Test.h"
  4. #include "utils/StringBuffer.h"
  5. typedef List<int> 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).add(3).add(2);
  16. test.checkEqual(4, list[0], "contains added value 1");
  17. test.checkEqual(3, list[1], "contains added value 2");
  18. test.checkEqual(2, list[2], "contains added value 3");
  19. test.checkEqual(3, list.getLength(), "sizes is increased by add");
  20. }
  21. static void testAddReplace(Test& test) {
  22. IntList list;
  23. list.add(5);
  24. list[0] = 3;
  25. test.checkEqual(3, list[0], "value is overwritten");
  26. }
  27. static void testClear(Test& test) {
  28. IntList list;
  29. list.add(5).add(4);
  30. list.clear();
  31. test.checkEqual(0, list.getLength(), "length is 0 after clear");
  32. }
  33. static void testShrink(Test& test) {
  34. IntList list;
  35. list.add(5).add(4).add(3);
  36. test.checkTrue(list.getCapacity() >= 3, "capacity is >= 3 after adding");
  37. list.shrink();
  38. test.checkTrue(list.getLength() == 3, "length is 3 after shrink");
  39. test.checkTrue(list.getCapacity() == 3, "capacity is 3 after shrink");
  40. test.checkEqual(5, list[0], "data ok after shrink 1");
  41. test.checkEqual(4, list[1], "data ok after shrink 2");
  42. test.checkEqual(3, list[2], "data ok after shrink 3");
  43. }
  44. static void testBigAdd(Test& test) {
  45. IntList list;
  46. for(int i = 0; i < 1000000; i++) {
  47. list.add(i);
  48. }
  49. for(int i = 0; i < list.getLength(); i++) {
  50. test.checkEqual(i, list[i], "big add");
  51. }
  52. test.checkEqual(1000000, list.getLength(), "big add length");
  53. }
  54. static void testCopy(Test& test) {
  55. IntList list;
  56. list.add(1).add(2).add(3);
  57. IntList copy(list);
  58. test.checkEqual(list.getLength(), copy.getLength(), "copy has same length");
  59. for(int i = 0; i < copy.getLength() && i < list.getLength(); i++) {
  60. test.checkEqual(list[i], copy[i], "copy has same values");
  61. }
  62. }
  63. static void testCopyAssignment(Test& test) {
  64. IntList list;
  65. list.add(1).add(2).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).add(2).add(3);
  77. IntList move(std::move(list));
  78. test.checkEqual(0, list.getLength(), "moved has length 0");
  79. test.checkEqual(3, move.getLength(), "moved passes length");
  80. test.checkEqual(1, move[0], "moved passes values");
  81. test.checkEqual(2, move[1], "moved passes values");
  82. test.checkEqual(3, move[2], "moved passes values");
  83. }
  84. static void testMoveAssignment(Test& test) {
  85. IntList list;
  86. list.add(1).add(2).add(3);
  87. IntList move;
  88. move = std::move(list);
  89. test.checkEqual(0, list.getLength(), "assignment moved has length 0");
  90. test.checkEqual(3, move.getLength(), "assignment moved passes length");
  91. test.checkEqual(1, move[0], "assignment moved passes values");
  92. test.checkEqual(2, move[1], "assignment moved passes values");
  93. test.checkEqual(3, move[2], "assignment moved passes values");
  94. }
  95. static void testToString1(Test& test) {
  96. IntList list;
  97. list.add(1).add(243).add(-423);
  98. test.checkEqual(String("[1, 243, -423]"), String(list), "to string 1");
  99. }
  100. static void testToString2(Test& test) {
  101. IntList list;
  102. list.add(1);
  103. test.checkEqual(String("[1]"), String(list), "to string 2");
  104. }
  105. static void testToString3(Test& test) {
  106. IntList list;
  107. test.checkEqual(String("[]"), String(list), "to string 3");
  108. }
  109. static void testRemoveBySwap(Test& test) {
  110. IntList list;
  111. list.add(4).add(3).add(2);
  112. list.removeBySwap(0);
  113. test.checkEqual(2, list[0], "remove by swap 1");
  114. test.checkEqual(3, list[1], "remove by swap 2");
  115. test.checkEqual(2, list.getLength(), "remove by swap 3");
  116. list.removeBySwap(1);
  117. test.checkEqual(2, list[0], "remove by swap 4");
  118. test.checkEqual(1, list.getLength(), "remove by swap 5");
  119. list.removeBySwap(0);
  120. test.checkEqual(0, list.getLength(), "remove by swap 6");
  121. }
  122. static void testRemove(Test& test) {
  123. IntList list;
  124. list.add(4).add(3).add(2);
  125. list.remove(0);
  126. test.checkEqual(3, list[0], "remove 1");
  127. test.checkEqual(2, list[1], "remove 2");
  128. test.checkEqual(2, list.getLength(), "remove 3");
  129. list.remove(1);
  130. test.checkEqual(3, list[0], "remove 4");
  131. test.checkEqual(1, list.getLength(), "remove 5");
  132. list.remove(0);
  133. test.checkEqual(0, list.getLength(), "remove 6");
  134. }
  135. void ListTests::test() {
  136. Test test("List");
  137. testAdd(test);
  138. testMultipleAdd(test);
  139. testAddReplace(test);
  140. testClear(test);
  141. testShrink(test);
  142. testBigAdd(test);
  143. testCopy(test);
  144. testCopyAssignment(test);
  145. testMove(test);
  146. testMoveAssignment(test);
  147. testToString1(test);
  148. testToString2(test);
  149. testToString3(test);
  150. testRemoveBySwap(test);
  151. testRemove(test);
  152. test.finalize();
  153. }