ListTests.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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).add(3).add(2);
  16. test.checkEqual(4, list[0], "list contains added value");
  17. test.checkEqual(3, list[1], "list contains added value");
  18. test.checkEqual(2, list[2], "list contains added value");
  19. test.checkEqual(3, list.getLength(), "list 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], "list value is overwritten");
  26. }
  27. static void testClear(Test& test) {
  28. IntList list;
  29. list.add(5).add(4).clear();
  30. test.checkEqual(0, list.getLength(), "list length is 0 after clear");
  31. }
  32. static void testOverflow(Test& test) {
  33. IntList list;
  34. for(int i = 0; i < 1000000; i++) {
  35. list.add(i);
  36. }
  37. for(int i = 0; i < list.getLength(); i++) {
  38. test.checkEqual(i, list[i], "list still contains values after overflow");
  39. }
  40. test.checkEqual(true, true, "list survives overflow");
  41. }
  42. static void testCopy(Test& test) {
  43. IntList list;
  44. list.add(1).add(2).add(3);
  45. IntList copy(list);
  46. test.checkEqual(list.getLength(), copy.getLength(), "list copy has same length");
  47. for(int i = 0; i < copy.getLength() && i < list.getLength(); i++) {
  48. test.checkEqual(list[i], copy[i], "list copy has same values");
  49. }
  50. }
  51. static void testCopyAssignment(Test& test) {
  52. IntList list;
  53. list.add(1).add(2).add(3);
  54. IntList copy;
  55. copy = list;
  56. test.checkEqual(list.getLength(), copy.getLength(), "list copy assignment has same length");
  57. for(int i = 0; i < copy.getLength() && i < list.getLength(); i++) {
  58. test.checkEqual(list[i], copy[i], "list copy assignment has same values");
  59. }
  60. }
  61. static void testMove(Test& test) {
  62. IntList list;
  63. list.add(1).add(2).add(3);
  64. IntList move(std::move(list));
  65. test.checkEqual(0, list.getLength(), "moved list has length 0");
  66. test.checkEqual(3, move.getLength(), "moved list passes length");
  67. test.checkEqual(1, move[0], "moved list passes values");
  68. test.checkEqual(2, move[1], "moved list passes values");
  69. test.checkEqual(3, move[2], "moved list passes values");
  70. }
  71. static void testMoveAssignment(Test& test) {
  72. IntList list;
  73. list.add(1).add(2).add(3);
  74. IntList move;
  75. move = std::move(list);
  76. test.checkEqual(0, list.getLength(), "assignment moved list has length 0");
  77. test.checkEqual(3, move.getLength(), "assignment moved list passes length");
  78. test.checkEqual(1, move[0], "assignment moved list passes values");
  79. test.checkEqual(2, move[1], "assignment moved list passes values");
  80. test.checkEqual(3, move[2], "assignment moved list passes values");
  81. }
  82. static void testToString1(Test& test) {
  83. IntList list;
  84. list.add(1).add(243).add(-423);
  85. test.checkEqual(String("[1, 243, -423]"), String(list), "list to string 1");
  86. }
  87. static void testToString2(Test& test) {
  88. IntList list;
  89. list.add(1);
  90. test.checkEqual(String("[1]"), String(list), "list to string 2");
  91. }
  92. static void testToString3(Test& test) {
  93. IntList list;
  94. test.checkEqual(String("[]"), String(list), "list to string 3");
  95. }
  96. void ListTests::test() {
  97. Test test("List");
  98. testAdd(test);
  99. testMultipleAdd(test);
  100. testAddReplace(test);
  101. testClear(test);
  102. testOverflow(test);
  103. testCopy(test);
  104. testCopyAssignment(test);
  105. testMove(test);
  106. testMoveAssignment(test);
  107. testToString1(test);
  108. testToString2(test);
  109. testToString3(test);
  110. test.finalize();
  111. }