ListTests.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include "tests/ListTests.h"
  2. #include "tests/Test.h"
  3. #include "utils/List.h"
  4. typedef List<int, 20> IntList;
  5. static void testAdd(Test& test) {
  6. IntList list;
  7. list.add(5);
  8. test.checkEqual(5, list[0], "list contains added value");
  9. test.checkEqual(1, list.getLength(), "list sizes is increased by add");
  10. }
  11. static void testMultipleAdd(Test& test) {
  12. IntList list;
  13. list.add(4);
  14. list.add(3);
  15. list.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);
  30. list.add(4);
  31. list.clear();
  32. test.checkEqual(0, list.getLength(), "list length is 0 after clear");
  33. }
  34. static void testOverflow(Test& test) {
  35. IntList list;
  36. for(int i = 0; i < 1000000; i++) {
  37. list.add(i);
  38. }
  39. for(int i = 0; i < list.getLength(); i++) {
  40. test.checkEqual(i, list[i], "list still contains values after overflow");
  41. }
  42. test.checkEqual(true, true, "list survives overflow");
  43. }
  44. static void testCopy(Test& test) {
  45. IntList list;
  46. list.add(1);
  47. list.add(2);
  48. list.add(3);
  49. IntList copy(list);
  50. test.checkEqual(list.getLength(), copy.getLength(), "list copy has same length");
  51. for(int i = 0; i < copy.getLength() && i < list.getLength(); i++) {
  52. test.checkEqual(list[i], copy[i], "list copy has same values");
  53. }
  54. }
  55. static void testCopyAssignment(Test& test) {
  56. IntList list;
  57. list.add(1);
  58. list.add(2);
  59. list.add(3);
  60. IntList copy;
  61. copy = list;
  62. test.checkEqual(list.getLength(), copy.getLength(), "list copy assignment has same length");
  63. for(int i = 0; i < copy.getLength() && i < list.getLength(); i++) {
  64. test.checkEqual(list[i], copy[i], "list copy assignment has same values");
  65. }
  66. }
  67. static void testMove(Test& test) {
  68. IntList list;
  69. list.add(1);
  70. list.add(2);
  71. list.add(3);
  72. IntList move(std::move(list));
  73. test.checkEqual(0, list.getLength(), "moved list has length 0");
  74. test.checkEqual(3, move.getLength(), "moved list passes length");
  75. test.checkEqual(1, move[0], "moved list passes values");
  76. test.checkEqual(2, move[1], "moved list passes values");
  77. test.checkEqual(3, move[2], "moved list passes values");
  78. }
  79. static void testMoveAssignment(Test& test) {
  80. IntList list;
  81. list.add(1);
  82. list.add(2);
  83. list.add(3);
  84. IntList move(std::move(list));
  85. test.checkEqual(0, list.getLength(), "assignment moved list has length 0");
  86. test.checkEqual(3, move.getLength(), "assignment moved list passes length");
  87. test.checkEqual(1, move[0], "assignment moved list passes values");
  88. test.checkEqual(2, move[1], "assignment moved list passes values");
  89. test.checkEqual(3, move[2], "assignment moved list passes values");
  90. }
  91. void ListTests::test() {
  92. Test test("List");
  93. testAdd(test);
  94. testMultipleAdd(test);
  95. testAddReplace(test);
  96. testClear(test);
  97. testOverflow(test);
  98. testCopy(test);
  99. testCopyAssignment(test);
  100. testMove(test);
  101. testMoveAssignment(test);
  102. test.finalize();
  103. }