Sharing of my labs carried out during the TDDE18 course at Linköping University
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_list.cc 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #define CATCH_CONFIG_MAIN
  2. #include "../lab2/catch.hpp"
  3. #include "sorted_list.h"
  4. /*
  5. Lab 3 - TDDE18 - Sorted list lab - Test file
  6. Using catch.hpp framework, documentation :
  7. https://github.com/philsquared/Catch/blob/master/docs/tutorial.md
  8. */
  9. // Note: The other TODO remarks are in the sorted_list.h file
  10. //TODO: Complementary work needed, 8-8: Test cases are insufficient, what if a list is
  11. //empty? --> done (addition of test in most TEST_CASES where these tests were not
  12. //carried out).
  13. //TODO: Complementary work needed, 8-8: Test cases insufficient, you should test
  14. //all functionality for copy, such as remove values in the copied list and if this
  15. //affects the original. --> done (adding tests to check that a modification of the
  16. //original does not affect the copy and vice versa).
  17. using namespace std;
  18. //=======================================================================
  19. // Test cases
  20. //=======================================================================
  21. TEST_CASE("Testing basic functions on empty list") {
  22. Sorted_List l{};
  23. REQUIRE(l.size() == 0);
  24. REQUIRE(l.is_empty());
  25. REQUIRE(l.to_string() == "[ ]");
  26. }
  27. TEST_CASE("Insertion in a list") {
  28. Sorted_List l{};
  29. l.insert(1);
  30. l.insert(8);
  31. l.insert(5);
  32. l.insert(8);
  33. l.insert(-1);
  34. REQUIRE_FALSE(l.is_empty());
  35. REQUIRE(l.size() == 5);
  36. REQUIRE(l.to_string() == "[ -1 1 5 8 8 ]");
  37. }
  38. TEST_CASE("Removal tests") {
  39. Sorted_List l{};
  40. Sorted_List void_l{};
  41. l.insert(1);
  42. l.insert(8);
  43. l.insert(5);
  44. l.insert(8);
  45. l.insert(-1);
  46. REQUIRE_NOTHROW(l.remove(-1));
  47. REQUIRE(l.size() == 4);
  48. REQUIRE_NOTHROW(l.remove(8));
  49. REQUIRE(l.size() == 3);
  50. REQUIRE_NOTHROW(l.remove(8));
  51. REQUIRE(l.size() == 2);
  52. REQUIRE(l.to_string() == "[ 1 5 ]");
  53. REQUIRE_THROWS(l.remove(4));
  54. REQUIRE_THROWS(void_l.remove(0));
  55. }
  56. TEST_CASE("Testing the 'nice' constructor") {
  57. Sorted_List l{1,-2, 7};
  58. Sorted_List l2{};
  59. REQUIRE(l.to_string() == "[ -2 1 7 ]");
  60. REQUIRE(l2.to_string() == "[ ]");
  61. }
  62. TEST_CASE("Testing copy constructor") {
  63. Sorted_List l{1, 8, 5};
  64. Sorted_List l_copy{l};
  65. Sorted_List l_void{};
  66. Sorted_List void_copy{l_void};
  67. REQUIRE_FALSE(l_copy.is_empty());
  68. REQUIRE(l.to_string() == "[ 1 5 8 ]");
  69. REQUIRE(l_copy.to_string() == "[ 1 5 8 ]");
  70. REQUIRE(void_copy.is_empty());
  71. REQUIRE(void_copy.to_string() == l_void.to_string());
  72. // We check that the copy is not affected
  73. l.remove(8);
  74. REQUIRE_FALSE(l_copy.is_empty());
  75. REQUIRE(l_copy.to_string() == "[ 1 5 8 ]");
  76. l_void.insert(5);
  77. REQUIRE(void_copy.is_empty());
  78. l_void.remove(5);
  79. // We check that the original is also not affected
  80. l_copy.remove(5);
  81. REQUIRE(l_copy.to_string() == "[ 1 8 ]");
  82. REQUIRE(l.to_string() == "[ 1 5 ]");
  83. void_copy.insert(7);
  84. REQUIRE(l_void.is_empty());
  85. }
  86. TEST_CASE("Testing copy assignment operator") {
  87. Sorted_List l{1, 8, 5};
  88. Sorted_List l2{};
  89. Sorted_List l_copy = l;
  90. Sorted_List l2_copy = l2;
  91. REQUIRE(l.to_string() == "[ 1 5 8 ]");
  92. REQUIRE(l_copy.to_string() == "[ 1 5 8 ]");
  93. // We check that the copy is not affected by original and vice versa
  94. l.remove(8);
  95. REQUIRE(l.to_string() == "[ 1 5 ]");
  96. REQUIRE(l_copy.to_string() == "[ 1 5 8 ]");
  97. l.insert(8);
  98. l_copy.remove(5);
  99. REQUIRE(l.to_string() == "[ 1 5 8 ]");
  100. REQUIRE(l_copy.to_string() == "[ 1 8 ]");
  101. l = l2;
  102. REQUIRE(l.is_empty());
  103. REQUIRE(l_copy.to_string() == "[ 1 8 ]");
  104. // Same on empty lists
  105. REQUIRE(l2.to_string() == "[ ]");
  106. REQUIRE(l2_copy.to_string() == "[ ]");
  107. l2.insert(7);
  108. REQUIRE(l2.to_string() == "[ 7 ]");
  109. REQUIRE(l2_copy.to_string() == "[ ]");
  110. l2_copy.insert(76);
  111. REQUIRE(l2.to_string() == "[ 7 ]");
  112. REQUIRE(l2_copy.to_string() == "[ 76 ]");
  113. }
  114. TEST_CASE("Testing move constructor") {
  115. Sorted_List l{1, 8, 5};
  116. Sorted_List l2{};
  117. Sorted_List m{std::move(l)};
  118. Sorted_List m2{std::move(l2)};
  119. REQUIRE(m.to_string() == "[ 1 5 8 ]");
  120. REQUIRE(l.is_empty());
  121. REQUIRE(m2.to_string() == "[ ]");
  122. REQUIRE(l2.is_empty());
  123. // Checking for interferences
  124. m.remove(5);
  125. l.insert(9);
  126. m2.insert(3);
  127. REQUIRE(m.to_string() == "[ 1 8 ]");
  128. REQUIRE(l.to_string() == "[ 9 ]");
  129. REQUIRE(m2.to_string() == "[ 3 ]");
  130. REQUIRE(l2.is_empty());
  131. }
  132. TEST_CASE("Testing move assignment") {
  133. Sorted_List l{1, 8, 5};
  134. Sorted_List l2{};
  135. Sorted_List m{};
  136. Sorted_List m2{};
  137. m = std::move(l);
  138. REQUIRE(m.to_string() == "[ 1 5 8 ]");
  139. REQUIRE(l.is_empty());
  140. m2 = std::move(l2);
  141. REQUIRE(m2.is_empty());
  142. REQUIRE(l2.is_empty());
  143. // Checking for interferences
  144. m.remove(5);
  145. l.insert(9);
  146. m2.insert(3);
  147. REQUIRE(m.to_string() == "[ 1 8 ]");
  148. REQUIRE(l.to_string() == "[ 9 ]");
  149. REQUIRE(m2.to_string() == "[ 3 ]");
  150. REQUIRE(l2.is_empty());
  151. }
  152. TEST_CASE("Testing the at() function") {
  153. Sorted_List l{1, 8, 5, 0, 5};
  154. Sorted_List l2{};
  155. Sorted_List l3{2};
  156. REQUIRE(l.at(0) == 0);
  157. REQUIRE(l.at(2) == 5);
  158. REQUIRE(l.at(4) == 8);
  159. REQUIRE(l3.at(0) == 2);
  160. REQUIRE_THROWS(l.at(5));
  161. REQUIRE_THROWS(l2.at(0));
  162. }