123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- #define CATCH_CONFIG_MAIN
- #include "../lab2/catch.hpp"
- #include "sorted_list.h"
-
- /*
- Lab 3 - TDDE18 - Sorted list lab - Test file
- Using catch.hpp framework, documentation :
- https://github.com/philsquared/Catch/blob/master/docs/tutorial.md
- */
-
- // Note: The other TODO remarks are in the sorted_list.h file
-
- //TODO: Complementary work needed, 8-8: Test cases are insufficient, what if a list is
- //empty? --> done (addition of test in most TEST_CASES where these tests were not
- //carried out).
- //TODO: Complementary work needed, 8-8: Test cases insufficient, you should test
- //all functionality for copy, such as remove values in the copied list and if this
- //affects the original. --> done (adding tests to check that a modification of the
- //original does not affect the copy and vice versa).
-
-
- using namespace std;
-
- //=======================================================================
- // Test cases
- //=======================================================================
-
- TEST_CASE("Testing basic functions on empty list") {
- Sorted_List l{};
-
- REQUIRE(l.size() == 0);
- REQUIRE(l.is_empty());
- REQUIRE(l.to_string() == "[ ]");
- }
-
- TEST_CASE("Insertion in a list") {
- Sorted_List l{};
-
- l.insert(1);
- l.insert(8);
- l.insert(5);
- l.insert(8);
- l.insert(-1);
-
- REQUIRE_FALSE(l.is_empty());
- REQUIRE(l.size() == 5);
- REQUIRE(l.to_string() == "[ -1 1 5 8 8 ]");
- }
-
- TEST_CASE("Removal tests") {
- Sorted_List l{};
- Sorted_List void_l{};
-
- l.insert(1);
- l.insert(8);
- l.insert(5);
- l.insert(8);
- l.insert(-1);
-
- REQUIRE_NOTHROW(l.remove(-1));
- REQUIRE(l.size() == 4);
- REQUIRE_NOTHROW(l.remove(8));
- REQUIRE(l.size() == 3);
- REQUIRE_NOTHROW(l.remove(8));
- REQUIRE(l.size() == 2);
- REQUIRE(l.to_string() == "[ 1 5 ]");
-
- REQUIRE_THROWS(l.remove(4));
- REQUIRE_THROWS(void_l.remove(0));
- }
-
- TEST_CASE("Testing the 'nice' constructor") {
- Sorted_List l{1,-2, 7};
- Sorted_List l2{};
- REQUIRE(l.to_string() == "[ -2 1 7 ]");
- REQUIRE(l2.to_string() == "[ ]");
- }
-
- TEST_CASE("Testing copy constructor") {
- Sorted_List l{1, 8, 5};
- Sorted_List l_copy{l};
- Sorted_List l_void{};
- Sorted_List void_copy{l_void};
-
- REQUIRE_FALSE(l_copy.is_empty());
- REQUIRE(l.to_string() == "[ 1 5 8 ]");
- REQUIRE(l_copy.to_string() == "[ 1 5 8 ]");
- REQUIRE(void_copy.is_empty());
- REQUIRE(void_copy.to_string() == l_void.to_string());
-
- // We check that the copy is not affected
- l.remove(8);
- REQUIRE_FALSE(l_copy.is_empty());
- REQUIRE(l_copy.to_string() == "[ 1 5 8 ]");
- l_void.insert(5);
- REQUIRE(void_copy.is_empty());
- l_void.remove(5);
-
- // We check that the original is also not affected
- l_copy.remove(5);
- REQUIRE(l_copy.to_string() == "[ 1 8 ]");
- REQUIRE(l.to_string() == "[ 1 5 ]");
- void_copy.insert(7);
- REQUIRE(l_void.is_empty());
- }
-
- TEST_CASE("Testing copy assignment operator") {
- Sorted_List l{1, 8, 5};
- Sorted_List l2{};
- Sorted_List l_copy = l;
- Sorted_List l2_copy = l2;
-
- REQUIRE(l.to_string() == "[ 1 5 8 ]");
- REQUIRE(l_copy.to_string() == "[ 1 5 8 ]");
-
- // We check that the copy is not affected by original and vice versa
- l.remove(8);
- REQUIRE(l.to_string() == "[ 1 5 ]");
- REQUIRE(l_copy.to_string() == "[ 1 5 8 ]");
- l.insert(8);
- l_copy.remove(5);
- REQUIRE(l.to_string() == "[ 1 5 8 ]");
- REQUIRE(l_copy.to_string() == "[ 1 8 ]");
-
- l = l2;
- REQUIRE(l.is_empty());
- REQUIRE(l_copy.to_string() == "[ 1 8 ]");
-
- // Same on empty lists
- REQUIRE(l2.to_string() == "[ ]");
- REQUIRE(l2_copy.to_string() == "[ ]");
-
- l2.insert(7);
- REQUIRE(l2.to_string() == "[ 7 ]");
- REQUIRE(l2_copy.to_string() == "[ ]");
- l2_copy.insert(76);
- REQUIRE(l2.to_string() == "[ 7 ]");
- REQUIRE(l2_copy.to_string() == "[ 76 ]");
- }
-
- TEST_CASE("Testing move constructor") {
- Sorted_List l{1, 8, 5};
- Sorted_List l2{};
- Sorted_List m{std::move(l)};
- Sorted_List m2{std::move(l2)};
-
- REQUIRE(m.to_string() == "[ 1 5 8 ]");
- REQUIRE(l.is_empty());
- REQUIRE(m2.to_string() == "[ ]");
- REQUIRE(l2.is_empty());
-
- // Checking for interferences
- m.remove(5);
- l.insert(9);
- m2.insert(3);
- REQUIRE(m.to_string() == "[ 1 8 ]");
- REQUIRE(l.to_string() == "[ 9 ]");
- REQUIRE(m2.to_string() == "[ 3 ]");
- REQUIRE(l2.is_empty());
- }
-
- TEST_CASE("Testing move assignment") {
- Sorted_List l{1, 8, 5};
- Sorted_List l2{};
- Sorted_List m{};
- Sorted_List m2{};
-
- m = std::move(l);
- REQUIRE(m.to_string() == "[ 1 5 8 ]");
- REQUIRE(l.is_empty());
- m2 = std::move(l2);
- REQUIRE(m2.is_empty());
- REQUIRE(l2.is_empty());
-
- // Checking for interferences
- m.remove(5);
- l.insert(9);
- m2.insert(3);
- REQUIRE(m.to_string() == "[ 1 8 ]");
- REQUIRE(l.to_string() == "[ 9 ]");
- REQUIRE(m2.to_string() == "[ 3 ]");
- REQUIRE(l2.is_empty());
- }
-
- TEST_CASE("Testing the at() function") {
- Sorted_List l{1, 8, 5, 0, 5};
- Sorted_List l2{};
- Sorted_List l3{2};
-
- REQUIRE(l.at(0) == 0);
- REQUIRE(l.at(2) == 5);
- REQUIRE(l.at(4) == 8);
- REQUIRE(l3.at(0) == 2);
-
- REQUIRE_THROWS(l.at(5));
- REQUIRE_THROWS(l2.at(0));
- }
|