Got everything, just need to change comments and do the menu
This commit is contained in:
249
test/test_linkedlist.cpp
Normal file
249
test/test_linkedlist.cpp
Normal file
@@ -0,0 +1,249 @@
|
||||
#include "linkedlist.h"
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
|
||||
TEST_CASE("Append 3 items to linkedlist", "[linkedlist]") {
|
||||
LinkedList<int> list = {};
|
||||
|
||||
REQUIRE(list.len() == 0);
|
||||
|
||||
list.append(8);
|
||||
REQUIRE(list.len() == 1);
|
||||
REQUIRE(list.get(0) == 8);
|
||||
|
||||
list.append(9);
|
||||
REQUIRE(list.len() == 2);
|
||||
REQUIRE(list.get(1) == 9);
|
||||
|
||||
list.append(10);
|
||||
REQUIRE(list.len() == 3);
|
||||
REQUIRE(list.get(2) == 10);
|
||||
|
||||
}
|
||||
|
||||
TEST_CASE("Prepend 3 items to linkedlist", "[linkedlist]") {
|
||||
LinkedList<int> list = {};
|
||||
|
||||
REQUIRE(list.len() == 0);
|
||||
|
||||
list.prepend(8);
|
||||
REQUIRE(list.len() == 1);
|
||||
REQUIRE(list.get(0) == 8);
|
||||
|
||||
list.prepend(9);
|
||||
REQUIRE(list.len() == 2);
|
||||
REQUIRE(list.get(0) == 9);
|
||||
|
||||
list.prepend(10);
|
||||
REQUIRE(list.len() == 3);
|
||||
REQUIRE(list.get(0) == 10);
|
||||
|
||||
}
|
||||
|
||||
TEST_CASE("Get out ouf bounds item", "[linkedlist]") {
|
||||
LinkedList<int> list = {};
|
||||
|
||||
REQUIRE(list.len() == 0);
|
||||
|
||||
list.append(8);
|
||||
REQUIRE(list.len() == 1);
|
||||
REQUIRE(list.get(0) == 8);
|
||||
|
||||
list.append(9);
|
||||
REQUIRE(list.len() == 2);
|
||||
REQUIRE(list.get(1) == 9);
|
||||
|
||||
list.append(10);
|
||||
REQUIRE(list.len() == 3);
|
||||
REQUIRE(list.get(2) == 10);
|
||||
|
||||
auto result = list.get(50);
|
||||
REQUIRE(!result.has_value());
|
||||
REQUIRE(result.error() == LinkedListErr::LINKEDLIST_OUT_OF_BOUNDS);
|
||||
}
|
||||
|
||||
TEST_CASE("Set head to val", "[linkedlist]") {
|
||||
LinkedList<int> list = {};
|
||||
|
||||
REQUIRE(list.len() == 0);
|
||||
|
||||
list.append(8);
|
||||
REQUIRE(list.len() == 1);
|
||||
REQUIRE(list.get(0) == 8);
|
||||
|
||||
list.append(9);
|
||||
REQUIRE(list.len() == 2);
|
||||
REQUIRE(list.get(1) == 9);
|
||||
|
||||
list.append(10);
|
||||
REQUIRE(list.len() == 3);
|
||||
REQUIRE(list.get(2) == 10);
|
||||
|
||||
auto result = list.set(0, 90);
|
||||
REQUIRE(list.len() == 3);
|
||||
REQUIRE(result == LinkedListErr::LINKEDLIST_OK);
|
||||
REQUIRE(list.get(0) == 90);
|
||||
}
|
||||
|
||||
TEST_CASE("Set tail to val", "[linkedlist]") {
|
||||
LinkedList<int> list = {};
|
||||
|
||||
REQUIRE(list.len() == 0);
|
||||
|
||||
list.append(8);
|
||||
REQUIRE(list.len() == 1);
|
||||
REQUIRE(list.get(0) == 8);
|
||||
|
||||
list.append(9);
|
||||
REQUIRE(list.len() == 2);
|
||||
REQUIRE(list.get(1) == 9);
|
||||
|
||||
list.append(10);
|
||||
REQUIRE(list.len() == 3);
|
||||
REQUIRE(list.get(2) == 10);
|
||||
|
||||
auto result = list.set(2, 90);
|
||||
REQUIRE(list.len() == 3);
|
||||
REQUIRE(result == LinkedListErr::LINKEDLIST_OK);
|
||||
REQUIRE(list.get(2) == 90);
|
||||
}
|
||||
|
||||
TEST_CASE("Set to out of bounds", "[linkedlist]") {
|
||||
LinkedList<int> list = {};
|
||||
|
||||
REQUIRE(list.len() == 0);
|
||||
|
||||
list.append(8);
|
||||
REQUIRE(list.len() == 1);
|
||||
REQUIRE(list.get(0) == 8);
|
||||
|
||||
list.append(9);
|
||||
REQUIRE(list.len() == 2);
|
||||
REQUIRE(list.get(1) == 9);
|
||||
|
||||
list.append(10);
|
||||
REQUIRE(list.len() == 3);
|
||||
REQUIRE(list.get(2) == 10);
|
||||
|
||||
auto result = list.set(50, 90);
|
||||
REQUIRE(list.len() == 3);
|
||||
REQUIRE(result == LinkedListErr::LINKEDLIST_OUT_OF_BOUNDS);
|
||||
}
|
||||
|
||||
TEST_CASE("Pop element", "[linkedlist]") {
|
||||
LinkedList<int> list = {};
|
||||
|
||||
REQUIRE(list.len() == 0);
|
||||
|
||||
list.append(8);
|
||||
REQUIRE(list.len() == 1);
|
||||
REQUIRE(list.get(0) == 8);
|
||||
|
||||
list.append(9);
|
||||
REQUIRE(list.len() == 2);
|
||||
REQUIRE(list.get(1) == 9);
|
||||
|
||||
list.append(10);
|
||||
REQUIRE(list.len() == 3);
|
||||
REQUIRE(list.get(2) == 10);
|
||||
|
||||
REQUIRE(list.pop() == 10);
|
||||
REQUIRE(list.len() == 2);
|
||||
}
|
||||
|
||||
TEST_CASE("Pop empty", "[linkedlist]") {
|
||||
LinkedList<int> list = {};
|
||||
|
||||
REQUIRE(list.len() == 0);
|
||||
|
||||
auto result = list.pop();
|
||||
REQUIRE(result.error() == LinkedListErr::LINKEDLIST_EMPTY);
|
||||
REQUIRE(list.len() == 0);
|
||||
}
|
||||
|
||||
TEST_CASE("Remove head", "[linkedlist]") {
|
||||
LinkedList<int> list = {};
|
||||
|
||||
REQUIRE(list.len() == 0);
|
||||
|
||||
list.append(8);
|
||||
REQUIRE(list.len() == 1);
|
||||
REQUIRE(list.get(0) == 8);
|
||||
|
||||
list.append(9);
|
||||
REQUIRE(list.len() == 2);
|
||||
REQUIRE(list.get(1) == 9);
|
||||
|
||||
list.append(10);
|
||||
REQUIRE(list.len() == 3);
|
||||
REQUIRE(list.get(2) == 10);
|
||||
|
||||
auto result = list.remove(0);
|
||||
REQUIRE(result == LinkedListErr::LINKEDLIST_OK);
|
||||
REQUIRE(list.len() == 2);
|
||||
REQUIRE(list.get(0) == 9);
|
||||
}
|
||||
|
||||
TEST_CASE("Remove tail", "[linkedlist]") {
|
||||
LinkedList<int> list = {};
|
||||
|
||||
REQUIRE(list.len() == 0);
|
||||
|
||||
list.append(8);
|
||||
REQUIRE(list.len() == 1);
|
||||
REQUIRE(list.get(0) == 8);
|
||||
|
||||
list.append(9);
|
||||
REQUIRE(list.len() == 2);
|
||||
REQUIRE(list.get(1) == 9);
|
||||
|
||||
list.append(10);
|
||||
REQUIRE(list.len() == 3);
|
||||
REQUIRE(list.get(2) == 10);
|
||||
|
||||
auto result = list.remove(2);
|
||||
REQUIRE(result == LinkedListErr::LINKEDLIST_OK);
|
||||
REQUIRE(list.len() == 2);
|
||||
REQUIRE(list.get(1) == 9);
|
||||
}
|
||||
|
||||
TEST_CASE("Find 1 value present", "[linkedlist]") {
|
||||
LinkedList<int> list = {};
|
||||
|
||||
REQUIRE(list.len() == 0);
|
||||
|
||||
list.append(8);
|
||||
REQUIRE(list.len() == 1);
|
||||
REQUIRE(list.get(0) == 8);
|
||||
|
||||
list.append(9);
|
||||
REQUIRE(list.len() == 2);
|
||||
REQUIRE(list.get(1) == 9);
|
||||
|
||||
list.append(10);
|
||||
REQUIRE(list.len() == 3);
|
||||
REQUIRE(list.get(2) == 10);
|
||||
|
||||
auto result = list.find(9);
|
||||
REQUIRE(result == 1);
|
||||
}
|
||||
|
||||
TEST_CASE("Find 1 value not present", "[linkedlist]") {
|
||||
LinkedList<int> list = {};
|
||||
|
||||
REQUIRE(list.len() == 0);
|
||||
|
||||
list.append(8);
|
||||
REQUIRE(list.len() == 1);
|
||||
REQUIRE(list.get(0) == 8);
|
||||
|
||||
list.append(9);
|
||||
REQUIRE(list.len() == 2);
|
||||
REQUIRE(list.get(1) == 9);
|
||||
|
||||
list.append(10);
|
||||
REQUIRE(list.len() == 3);
|
||||
REQUIRE(list.get(2) == 10);
|
||||
|
||||
auto result = list.find(80);
|
||||
REQUIRE(result.error() == LinkedListErr::LINKEDLIST_NOT_FOUND);
|
||||
}
|
||||
Reference in New Issue
Block a user