ListTests.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include "tests/ListTests.h"
  2. #include "tests/Test.h"
  3. #include "utils/List.h"
  4. #include "utils/StringBuffer.h"
  5. typedef List<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), "add returns false without overflow");
  40. }
  41. for(int i = 0; i < 1000000; i++) {
  42. test.checkEqual(true, list.add(i), "add returns true with overflow");
  43. }
  44. for(int i = 0; i < list.getLength(); i++) {
  45. test.checkEqual(i, list[i], "still contains values after overflow");
  46. }
  47. test.checkEqual(true, true, "survives overflow");
  48. }
  49. static void testCopy(Test& test) {
  50. IntList list;
  51. list.add(1);
  52. list.add(2);
  53. list.add(3);
  54. IntList copy(list);
  55. test.checkEqual(list.getLength(), copy.getLength(), "copy has same length");
  56. for(int i = 0; i < copy.getLength() && i < list.getLength(); i++) {
  57. test.checkEqual(list[i], copy[i], "copy has same values");
  58. }
  59. }
  60. static void testCopyAssignment(Test& test) {
  61. IntList list;
  62. list.add(1);
  63. list.add(2);
  64. list.add(3);
  65. IntList copy;
  66. copy = list;
  67. test.checkEqual(list.getLength(), copy.getLength(), "copy assignment has same length");
  68. for(int i = 0; i < copy.getLength() && i < list.getLength(); i++) {
  69. test.checkEqual(list[i], copy[i], "copy assignment has same values");
  70. }
  71. }
  72. static void testMove(Test& test) {
  73. IntList list;
  74. list.add(1);
  75. list.add(2);
  76. list.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);
  87. list.add(2);
  88. list.add(3);
  89. IntList move;
  90. move = std::move(list);
  91. test.checkEqual(0, list.getLength(), "assignment moved has length 0");
  92. test.checkEqual(3, move.getLength(), "assignment moved passes length");
  93. test.checkEqual(1, move[0], "assignment moved passes values");
  94. test.checkEqual(2, move[1], "assignment moved passes values");
  95. test.checkEqual(3, move[2], "assignment moved passes values");
  96. }
  97. static void testToString1(Test& test) {
  98. IntList list;
  99. list.add(1);
  100. list.add(243);
  101. list.add(-423);
  102. test.checkEqual(String("[1, 243, -423]"), String(list), "to string 1");
  103. }
  104. static void testToString2(Test& test) {
  105. IntList list;
  106. list.add(1);
  107. test.checkEqual(String("[1]"), String(list), "to string 2");
  108. }
  109. static void testToString3(Test& test) {
  110. IntList list;
  111. test.checkEqual(String("[]"), String(list), "to string 3");
  112. }
  113. static void testRemove(Test& test) {
  114. IntList list;
  115. list.add(4);
  116. list.add(3);
  117. list.add(2);
  118. list.remove(0);
  119. test.checkEqual(2, list[0], "remove 1");
  120. test.checkEqual(3, list[1], "remove 2");
  121. test.checkEqual(2, list.getLength(), "remove 3");
  122. list.remove(1);
  123. test.checkEqual(2, list[0], "remove 4");
  124. test.checkEqual(1, list.getLength(), "remove 5");
  125. list.remove(0);
  126. test.checkEqual(0, list.getLength(), "remove 6");
  127. }
  128. static void testRemoveIsSafe(Test& test) {
  129. IntList list;
  130. for(int i = -500000; i < 500000; i++) {
  131. test.checkEqual(true, list.remove(i), "removing from empty list is safe");
  132. }
  133. }
  134. void ListTests::test() {
  135. Test test("List");
  136. testAdd(test);
  137. testMultipleAdd(test);
  138. testAddReplace(test);
  139. testClear(test);
  140. testOverflow(test);
  141. testCopy(test);
  142. testCopyAssignment(test);
  143. testMove(test);
  144. testMoveAssignment(test);
  145. testToString1(test);
  146. testToString2(test);
  147. testToString3(test);
  148. testRemove(test);
  149. testRemoveIsSafe(test);
  150. test.finalize();
  151. }