aboutsummaryrefslogtreecommitdiff
path: root/CST 126/LinkedListUnitTests/LinkedListUnitTests.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'CST 126/LinkedListUnitTests/LinkedListUnitTests.cpp')
-rw-r--r--CST 126/LinkedListUnitTests/LinkedListUnitTests.cpp519
1 files changed, 519 insertions, 0 deletions
diff --git a/CST 126/LinkedListUnitTests/LinkedListUnitTests.cpp b/CST 126/LinkedListUnitTests/LinkedListUnitTests.cpp
new file mode 100644
index 0000000..15defc0
--- /dev/null
+++ b/CST 126/LinkedListUnitTests/LinkedListUnitTests.cpp
@@ -0,0 +1,519 @@
+#include "pch.h"
+#include "CppUnitTest.h"
+
+#include <list>
+
+using namespace Microsoft::VisualStudio::CppUnitTestFramework;
+
+#include "SinglyLinkedList.hpp";
+
+namespace LinkedListUnitTests
+{
+ TEST_CLASS(LinkedListUnitTests)
+ {
+ public:
+ std::list<int> my_list{1, 5, 6, 7 ,9, 10};
+
+ //Empty
+ TEST_METHOD(EmptyListHasZeroSize)
+ {
+ //Arrange
+ SinglyLinkedList linkedList{};
+
+ //Assert
+ Assert::AreEqual(0ull, linkedList._size);
+ }
+
+ //Append
+ TEST_METHOD(AppendingLinkedList)
+ {
+ //Arrange
+ struct SinglyLinkedList linkedList = {};
+
+ ListNode node = {5, nullptr};
+
+ //Act
+ bool success = Append(&linkedList, &node);
+
+ //Assert
+ Assert::AreEqual(5, linkedList._head->_data);
+ Assert::AreEqual(1ull, linkedList._size);
+
+
+ }
+
+ TEST_METHOD(MultipleAppend_LinkedList_Success)
+ {
+ //Arrange
+ struct SinglyLinkedList linkedList = {};
+
+ ListNode node1 = { 1, nullptr };
+ ListNode node2 = { 2, nullptr };
+ ListNode node3 = { 3, nullptr };
+ ListNode node4 = { 4, nullptr };
+ ListNode node5 = { 5, nullptr };
+
+ //Act
+ bool success = Append(&linkedList, &node1);
+ success = Append(&linkedList, &node2);
+ success = Append(&linkedList, &node3);
+ success = Append(&linkedList, &node4);
+ success = Append(&linkedList, &node5);
+
+ Assert::AreEqual(node1._data, linkedList._head->_data);
+
+ ListNode* travel = linkedList._head;
+ //Assert
+ for (auto i = 1; i <= 5; i++)
+ {
+ Assert::AreEqual(i, travel->_data);
+ travel = travel->_next;
+
+ }
+
+ }
+
+ //Prepend
+ TEST_METHOD(OnePrependLinkedList)
+ {
+ //Arrange
+ struct SinglyLinkedList linkedList = {};
+
+ ListNode node = { 5, nullptr };
+
+ //Act
+ bool success = Prepend(&linkedList, &node);
+
+ //Assert
+ Assert::AreEqual(5, linkedList._head->_data);
+ Assert::AreEqual(1ull, linkedList._size);
+
+
+ }
+
+ TEST_METHOD(MultiplePrepend_LinkedList_Success)
+ {
+ //Arrange
+ struct SinglyLinkedList linkedList = {};
+
+ ListNode node1 = { 1, nullptr };
+ ListNode node2 = { 2, nullptr };
+ ListNode node3 = { 3, nullptr };
+ ListNode node4 = { 4, nullptr };
+ ListNode node5 = { 5, nullptr };
+
+ //Act
+ bool success = Prepend(&linkedList, &node1);
+ success = Prepend(&linkedList, &node2);
+ success = Prepend(&linkedList, &node3);
+ success = Prepend(&linkedList, &node4);
+ success = Prepend(&linkedList, &node5);
+
+ Assert::AreEqual(node5._data, linkedList._head->_data);
+
+ ListNode* travel = linkedList._head;
+ //Assert
+ for (auto i = 5; i >= 1; i--)
+ {
+ Assert::AreEqual(i, travel->_data);
+ travel = travel->_next;
+
+ }
+
+ }
+
+ //RemoveFirst
+ TEST_METHOD(RemoveFirstNode_LinkedList_Success)
+ {
+ struct SinglyLinkedList linkedList = {};
+
+ ListNode node1 = { 1, nullptr };
+ ListNode node2 = { 2, nullptr };
+ ListNode node3 = { 3, nullptr };
+ ListNode node4 = { 4, nullptr };
+ ListNode node5 = { 5, nullptr };
+
+ //Act
+ bool success = Append(&linkedList, &node1);
+ success = Append(&linkedList, &node2);
+ success = Append(&linkedList, &node3);
+ success = Append(&linkedList, &node4);
+ success = Append(&linkedList, &node5);
+
+ RemoveFirst(&linkedList);
+
+ ListNode* travel = linkedList._head;
+ for (auto i = 2; i <= 5; i++)
+ {
+ Assert::AreEqual(i, travel->_data);
+ travel = travel->_next;
+
+ }
+
+ }
+
+ TEST_METHOD(RemoveFirstNodeWithOneNode_LinkedList_Success)
+ {
+ struct SinglyLinkedList linkedList = {};
+
+ ListNode node1 = { 1, nullptr };
+
+ //Act
+ bool success = Append(&linkedList, &node1);
+
+ RemoveFirst(&linkedList);
+
+ Assert::IsNull(linkedList._head);
+ }
+
+ //RemoveLast
+ TEST_METHOD(RemoveLastNode_LinkedList_Success)
+ {
+ struct SinglyLinkedList linkedList = {};
+
+ ListNode node1 = { 1, nullptr };
+ ListNode node2 = { 2, nullptr };
+ ListNode node3 = { 3, nullptr };
+ ListNode node4 = { 4, nullptr };
+ ListNode node5 = { 5, nullptr };
+
+ //Act
+ bool success = Append(&linkedList, &node1);
+ success = Append(&linkedList, &node2);
+ success = Append(&linkedList, &node3);
+ success = Append(&linkedList, &node4);
+ success = Append(&linkedList, &node5);
+
+ RemoveLast(&linkedList);
+
+ ListNode* travel = linkedList._head;
+ for (auto i = 1; i <= 4; i++)
+ {
+ Assert::AreEqual(i, travel->_data);
+ travel = travel->_next;
+
+ }
+
+ }
+
+ TEST_METHOD(RemoveLastNodeWithOneNode_LinkedList_Success)
+ {
+ struct SinglyLinkedList linkedList = {};
+
+ ListNode node1 = { 1, nullptr };
+
+ //Act
+ bool success = Append(&linkedList, &node1);
+
+ RemoveLast(&linkedList);
+
+ Assert::IsNull(linkedList._head);
+ }
+
+ //InsertAfter
+ TEST_METHOD(Single_InserterAfter_LinkedList_Success)
+ {
+ //Arrange
+ struct SinglyLinkedList newList = {};
+ //Append Nodes
+ ListNode node1 = { 1, nullptr };
+ ListNode node2 = { 2, nullptr };
+ ListNode node4 = { 4, nullptr };
+ ListNode node5 = { 5, nullptr };
+
+ bool success = Append(&newList, &node1);
+ success = Append(&newList, &node2);
+ success = Append(&newList, &node4);
+ success = Append(&newList, &node5);
+
+ //Act
+ //Insert After a node
+ ListNode node3 = { 3, nullptr };
+ InsertAfter(&newList, 2, &node3);
+
+ //Assert
+ //Assert that list is correct, one node at a time
+
+ ListNode* travel = newList._head;
+ for (auto i = 1; i <= 5; i++)
+ {
+ Assert::AreEqual(i, travel->_data);
+ travel = travel->_next;
+
+ }
+ }
+
+ TEST_METHOD(Single_InserterAfterEnd_LinkedList_Success)
+ {
+ //Arrange
+ struct SinglyLinkedList newList = {};
+ //Append Nodes
+ ListNode node1 = { 1, nullptr };
+ ListNode node2 = { 2, nullptr };
+ ListNode node3 = { 3, nullptr };
+ ListNode node4 = { 4, nullptr };
+
+ bool success = Append(&newList, &node1);
+ success = Append(&newList, &node2);
+ success = Append(&newList, &node3);
+ success = Append(&newList, &node4);
+
+ //Act
+ //Insert After a node
+ ListNode node5 = { 5, nullptr };
+ InsertAfter(&newList, 4, &node5);
+
+ //Assert
+ //Assert that list is correct, one node at a time
+
+ ListNode* travel = newList._head;
+ for (auto i = 1; i <= 5; i++)
+ {
+ Assert::AreEqual(i, travel->_data);
+ travel = travel->_next;
+
+ }
+ }
+
+ //InsertBefore
+ TEST_METHOD(Single_InserterBefore_LinkedList_Success)
+ {
+ //Arrange
+ struct SinglyLinkedList newList = {};
+ //Append Nodes
+ ListNode node1 = { 1, nullptr };
+ ListNode node2 = { 2, nullptr };
+ ListNode node4 = { 4, nullptr };
+ ListNode node5 = { 5, nullptr };
+
+ bool success = Append(&newList, &node1);
+ success = Append(&newList, &node2);
+ success = Append(&newList, &node4);
+ success = Append(&newList, &node5);
+
+ //Act
+ //Insert After a node
+ ListNode node3 = { 3, nullptr };
+ InsertBefore(&newList, 4, &node3);
+
+ //Assert
+ //Assert that list is correct, one node at a time
+
+ ListNode* travel = newList._head;
+ for (auto i = 1; i <= 5; i++)
+ {
+ Assert::AreEqual(i, travel->_data);
+ travel = travel->_next;
+
+ }
+ }
+
+ TEST_METHOD(Single_InserterBeforeHead_LinkedList_Success)
+ {
+ //Arrange
+ struct SinglyLinkedList newList = {};
+ //Append Nodes
+ ListNode node2 = { 2, nullptr };
+ ListNode node3 = { 3, nullptr };
+ ListNode node4 = { 4, nullptr };
+ ListNode node5 = { 5, nullptr };
+
+ bool success = Append(&newList, &node2);
+ success = Append(&newList, &node3);
+ success = Append(&newList, &node4);
+ success = Append(&newList, &node5);
+
+ //Act
+ //Insert After a node
+ ListNode node1 = { 1, nullptr };
+ InsertBefore(&newList, 2, &node1);
+
+ //Assert
+ //Assert that list is correct, one node at a time
+
+ ListNode* travel = newList._head;
+ for (auto i = 1; i <= 5; i++)
+ {
+ Assert::AreEqual(i, travel->_data);
+ travel = travel->_next;
+
+ }
+ }
+
+ //Clear
+ TEST_METHOD(OneAppend_Clear_LinkedList_Success)
+ {
+ //Arrange
+ struct SinglyLinkedList linkedList = {};
+ ListNode node1 = { 1, nullptr };
+ bool success = Append(&linkedList, &node1);
+
+ //Act
+ //Clearing the list
+ Clear(&linkedList);
+
+ //Assert
+ Assert::AreEqual(0ull, linkedList._size);
+ Assert::IsNull(linkedList._head);
+ }
+
+ TEST_METHOD(MultipleAppend_Clear_LinkedList_Success)
+ {
+ //Arrange
+ struct SinglyLinkedList linkedList = {};
+
+ ListNode node1 = { 1, nullptr };
+ ListNode node2 = { 2, nullptr };
+ ListNode node3 = { 3, nullptr };
+ ListNode node4 = { 4, nullptr };
+ ListNode node5 = { 5, nullptr };
+
+ bool success = Append(&linkedList, &node1);
+ success = Append(&linkedList, &node2);
+ success = Append(&linkedList, &node3);
+ success = Append(&linkedList, &node4);
+ success = Append(&linkedList, &node5);
+
+ //Act
+ //Clearing the list
+ Clear(&linkedList);
+
+ //Assert
+ Assert::AreEqual(0ull, linkedList._size);
+ Assert::IsNull(linkedList._head);
+
+ Assert::IsNull(node1._next);
+ Assert::IsNull(node2._next);
+ Assert::IsNull(node3._next);
+ Assert::IsNull(node4._next);
+ Assert::IsNull(node5._next);
+ }
+
+ //Extract
+ TEST_METHOD(ExtractFirst_LinkedList_Success)
+ {
+ //Arrange
+ struct SinglyLinkedList newList = {};
+ //Append Nodes
+ ListNode node1 = { 1, nullptr };
+ ListNode node2 = { 2, nullptr };
+ ListNode node3 = { 3, nullptr };
+ ListNode node4 = { 4, nullptr };
+ ListNode node5 = { 5, nullptr };
+
+ ListNode nodeArray[] = { node1, node2, node3, node4, node5 };
+
+ for (auto& node : nodeArray)
+ {
+ Assert::IsTrue(Append(&newList, &node));
+ }
+
+ ListNode* temp = Extract(&newList, node1._data);
+
+ /*size_t nodeAddress = reinterpret_cast<size_t>(&node1);
+ size_t tempAddress = reinterpret_cast<size_t>(temp);
+
+ Assert::AreEqual(nodeAddress, tempAddress);*/
+
+ Assert::AreEqual(4ull, newList._size);
+
+ delete temp;
+ }
+
+ TEST_METHOD(ExtractLast_LinkedList_Success)
+ {
+ //Arrange
+ struct SinglyLinkedList newList = {};
+ //Append Nodes
+ ListNode node1 = { 1, nullptr };
+ ListNode node2 = { 2, nullptr };
+ ListNode node3 = { 3, nullptr };
+ ListNode node4 = { 4, nullptr };
+ ListNode node5 = { 5, nullptr };
+
+ ListNode nodeArray[] = { node1, node2, node3, node4, node5 };
+
+ for (auto& node : nodeArray)
+ {
+ Assert::IsTrue(Append(&newList, &node));
+ }
+
+ ListNode* temp = Extract(&newList, node5._data);
+
+ Assert::AreEqual(4ull, newList._size);
+
+ delete temp;
+ }
+
+ TEST_METHOD(ExtractMiddle_LinkedList_Success)
+ {
+ //Arrange
+ struct SinglyLinkedList newList = {};
+ //Append Nodes
+ ListNode node1 = { 1, nullptr };
+ ListNode node2 = { 2, nullptr };
+ ListNode node3 = { 3, nullptr };
+ ListNode node4 = { 4, nullptr };
+ ListNode node5 = { 5, nullptr };
+
+ ListNode nodeArray[] = { node1, node2, node3, node4, node5 };
+
+ for (auto& node : nodeArray)
+ {
+ Assert::IsTrue(Append(&newList, &node));
+ }
+
+ ListNode* temp = Extract(&newList, node3._data);
+
+ Assert::AreEqual(4ull, newList._size);
+
+ delete temp;
+ }
+
+ //Remove
+ TEST_METHOD(RemoveNode_LinkedList_Success)
+ {
+ struct SinglyLinkedList linkedList = {};
+
+ ListNode node1 = { 1, nullptr };
+ ListNode node2 = { 2, nullptr };
+ ListNode node3 = { 3, nullptr };
+ ListNode node4 = { 4, nullptr };
+ ListNode node5 = { 5, nullptr };
+
+ //Act
+ bool success = Append(&linkedList, &node1);
+ success = Append(&linkedList, &node2);
+ success = Append(&linkedList, &node3);
+ success = Append(&linkedList, &node4);
+ success = Append(&linkedList, &node5);
+
+ Remove(&linkedList, &node3);
+
+ ListNode* travel = linkedList._head;
+ for (auto i = 1; i <= 5; i++)
+ {
+ if (i == 3)
+ i++;
+ Assert::AreEqual(i, travel->_data);
+ travel = travel->_next;
+
+ }
+
+ }
+
+ TEST_METHOD(RemoveNodeWithOneNode_Linked_List_Success) {
+ struct SinglyLinkedList linkedList = {};
+
+ ListNode node1 = { 1, nullptr };
+
+ //Act
+ bool success = Append(&linkedList, &node1);
+
+ Remove(&linkedList, &node1);
+
+ Assert::IsNull(linkedList._head);
+ }
+
+ };
+}