ListTests.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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], "list contains added value");
  11. test.checkEqual(1, list.getLength(), "list 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], "list contains added value");
  19. test.checkEqual(3, list[1], "list contains added value");
  20. test.checkEqual(2, list[2], "list contains added value");
  21. test.checkEqual(3, list.getLength(), "list 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], "list 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(), "list length is 0 after clear");
  35. }
  36. static void testOverflow(Test& test) {
  37. IntList list;
  38. for(int i = 0; i < 1000000; i++) {
  39. list.add(i);
  40. }
  41. for(int i = 0; i < list.getLength(); i++) {
  42. test.checkEqual(i, list[i], "list still contains values after overflow");
  43. }
  44. test.checkEqual(true, true, "list survives overflow");
  45. }
  46. static void testCopy(Test& test) {
  47. IntList list;
  48. list.add(1);
  49. list.add(2);
  50. list.add(3);
  51. IntList copy(list);
  52. test.checkEqual(list.getLength(), copy.getLength(), "list copy has same length");
  53. for(int i = 0; i < copy.getLength() && i < list.getLength(); i++) {
  54. test.checkEqual(list[i], copy[i], "list copy has same values");
  55. }
  56. }
  57. static void testCopyAssignment(Test& test) {
  58. IntList list;
  59. list.add(1);
  60. list.add(2);
  61. list.add(3);
  62. IntList copy;
  63. copy = list;
  64. test.checkEqual(list.getLength(), copy.getLength(), "list copy assignment has same length");
  65. for(int i = 0; i < copy.getLength() && i < list.getLength(); i++) {
  66. test.checkEqual(list[i], copy[i], "list copy assignment has same values");
  67. }
  68. }
  69. static void testMove(Test& test) {
  70. IntList list;
  71. list.add(1);
  72. list.add(2);
  73. list.add(3);
  74. IntList move(std::move(list));
  75. test.checkEqual(0, list.getLength(), "moved list has length 0");
  76. test.checkEqual(3, move.getLength(), "moved list passes length");
  77. test.checkEqual(1, move[0], "moved list passes values");
  78. test.checkEqual(2, move[1], "moved list passes values");
  79. test.checkEqual(3, move[2], "moved list passes values");
  80. }
  81. static void testMoveAssignment(Test& test) {
  82. IntList list;
  83. list.add(1);
  84. list.add(2);
  85. list.add(3);
  86. IntList move(std::move(list));
  87. test.checkEqual(0, list.getLength(), "assignment moved list has length 0");
  88. test.checkEqual(3, move.getLength(), "assignment moved list passes length");
  89. test.checkEqual(1, move[0], "assignment moved list passes values");
  90. test.checkEqual(2, move[1], "assignment moved list passes values");
  91. test.checkEqual(3, move[2], "assignment moved list passes values");
  92. }
  93. static void testToString1(Test& test) {
  94. IntList list;
  95. list.add(1);
  96. list.add(243);
  97. list.add(-423);
  98. String s;
  99. s.append(list);
  100. test.checkEqual(String("[1, 243, -423]"), s, "list to string 1");
  101. }
  102. static void testToString2(Test& test) {
  103. IntList list;
  104. list.add(1);
  105. String s;
  106. s.append(list);
  107. test.checkEqual(String("[1]"), s, "list to string 2");
  108. }
  109. static void testToString3(Test& test) {
  110. IntList list;
  111. String s;
  112. s.append(list);
  113. test.checkEqual(String("[]"), s, "list to string 3");
  114. }
  115. void ListTests::test() {
  116. Test test("List");
  117. testAdd(test);
  118. testMultipleAdd(test);
  119. testAddReplace(test);
  120. testClear(test);
  121. testOverflow(test);
  122. testCopy(test);
  123. testCopyAssignment(test);
  124. testMove(test);
  125. testMoveAssignment(test);
  126. testToString1(test);
  127. testToString2(test);
  128. testToString3(test);
  129. test.finalize();
  130. }