ListTests.cpp 4.2 KB

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