ListTests.cpp 5.2 KB

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