diff options
| author | reecepwarner <[email protected]> | 2024-05-29 12:44:24 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-05-29 12:44:24 -0700 |
| commit | a94e681196881e3f8fb18ecfd760ed3f59fa8ca1 (patch) | |
| tree | 46158946d1c7aa12c44acc6043bcc595523a0c88 /CST 126/UnitTester/UnitTester.cpp | |
| parent | homework2 completed (diff) | |
| download | homework-1-reecepwarner-a94e681196881e3f8fb18ecfd760ed3f59fa8ca1.tar.xz homework-1-reecepwarner-a94e681196881e3f8fb18ecfd760ed3f59fa8ca1.zip | |
Template node branch to Develop Branch (#3)
* init
* changes
* changes
* created insert and deletion functions
* unit testing homework3
* more unit tests
* unit tests added
* unit test and memory check added
* Template node
* essentially completed
---------
Co-authored-by: rPatrickWarner <[email protected]>
Diffstat (limited to 'CST 126/UnitTester/UnitTester.cpp')
| -rw-r--r-- | CST 126/UnitTester/UnitTester.cpp | 364 |
1 files changed, 364 insertions, 0 deletions
diff --git a/CST 126/UnitTester/UnitTester.cpp b/CST 126/UnitTester/UnitTester.cpp new file mode 100644 index 0000000..83b51d9 --- /dev/null +++ b/CST 126/UnitTester/UnitTester.cpp @@ -0,0 +1,364 @@ +#include "pch.h" +#include "CppUnitTest.h" +#include "SinglyLinkedList.hpp" +#include "crt_check_memory.hpp" + +using namespace Microsoft::VisualStudio::CppUnitTestFramework; +using namespace CST_126; + +namespace LinkedListUnitTests +{ + TEST_CLASS(LinkedListUnitTests) + { + public: + + TEST_METHOD(EmptyListHasZeroSize) + { + SinglyLinkedList<int> LinkedList{}; + Assert::AreEqual(0ull, LinkedList._size); + } + + TEST_METHOD(AppendingLinkedListWith1Item) + { + SinglyLinkedList<int> LinkedList{}; + ListNode<int>* node = new ListNode<int>{ 5, nullptr }; + + + bool Success = Append(&LinkedList, node); + Assert::AreEqual(5, LinkedList._head->_data); + delete node; + } + TEST_METHOD(AppendingLinkedListWith5Items) + { + struct SinglyLinkedList<int> LinkedList = {}; + ListNode<int>Node1 ={ 1,nullptr }; + ListNode<int>Node2 ={ 2,nullptr }; + ListNode<int>Node3 ={ 3,nullptr }; + ListNode<int>Node4 ={ 4,nullptr }; + ListNode<int>Node5 = { 5, nullptr }; + + + 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<int>* TraverseNode = LinkedList._head; + + for (auto i = 1; i <= 5; i++) + { + Assert::AreEqual(i, TraverseNode->_data); + TraverseNode = TraverseNode->_next; + } + + } + + TEST_METHOD(One_Append_Clear_LinkedList_Success) + { + struct SinglyLinkedList<int> LinkedList = {}; + ListNode<int>Node1 = { 1,nullptr }; + + bool success = Append(&LinkedList, &Node1); + + Clear(&LinkedList); + + Assert::AreEqual(0ull, LinkedList._size); + Assert::IsNull(LinkedList._head); + + } + + + TEST_METHOD(Multiple_Append_Clear_LinkedList_Success) + { + struct SinglyLinkedList<int> LinkedList = {}; + ListNode<int>Node1 = { 1,nullptr }; + ListNode<int>Node2 = { 2,nullptr }; + ListNode<int>Node3 = { 3,nullptr }; + ListNode<int>Node4 = { 4,nullptr }; + ListNode<int>Node5 = { 5,nullptr }; + + bool success = Append(&LinkedList, &Node1); + success = Append(&LinkedList, &Node2); + success = Append(&LinkedList, &Node3); + success = Append(&LinkedList, &Node4); + success = Append(&LinkedList, &Node5); + + + Clear(&LinkedList); + + 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); + } + + TEST_METHOD(Insert_After_Single_LinkedList_Success) + { + //Arrange + struct SinglyLinkedList<int> NewList{}; + ListNode<int> Node1 = { 1,nullptr }; + ListNode<int>Node2 = { 2,nullptr }; + ListNode<int>Node3 = { 3,nullptr }; + ListNode<int>Node4 = { 4,nullptr }; + ListNode<int>Node5 = { 5,nullptr }; + //Act + bool Success = Append(&NewList, &Node1); + Success = Append(&NewList, &Node2); + Success = Append(&NewList, &Node3); + Success = Append(&NewList, &Node5); + + InsertAfter(&NewList, 3, &Node4); + + ListNode<int>* TraverseNode = NewList._head; + + for (auto i = 1; i < 5; i++) + { + Assert::AreEqual(i, TraverseNode->_data); + TraverseNode = TraverseNode->_next; + } + } + + TEST_METHOD(Insert_Before_UNO_List) + { + struct SinglyLinkedList<int> NewList{}; + ListNode<int> Node1 = { 1, nullptr }; + ListNode<int> Node2 = { 2, nullptr }; + bool Success = InsertBefore(&NewList, 1, &Node2); + + Assert::AreEqual(2, NewList._head->_data); + } + + TEST_METHOD(Insert_Before_5_List) + { + struct SinglyLinkedList<int> NewList {}; + ListNode<int> Node1 = { 1,nullptr }; + ListNode<int>Node2 = { 2,nullptr }; + ListNode<int>Node3 = { 3,nullptr }; + ListNode<int>Node4 = { 4,nullptr }; + ListNode<int>Node5 = { 5,nullptr }; + + bool Success = Append(&NewList, &Node1); + Success = Append(&NewList, &Node2); + Success = Append(&NewList, &Node3); + Success = Append(&NewList, &Node5); + InsertBefore(&NewList, 5, &Node4); + + ListNode<int>* Traverse = NewList._head; + for (auto i = 1; i < 5; i++) + { + Traverse = Traverse->_next; + } + + Assert::AreEqual(4, Traverse->_data); + } + + TEST_METHOD(DeleteFirstLinkWith0) + { + SinglyLinkedList<int> LinkedList{}; + ListNode<int>* node = new ListNode<int>{}; + bool Success = RemoveFirst(&LinkedList); + Assert::AreEqual(1, static_cast<int>(Success)); + delete node; + } + + TEST_METHOD(Prepend_LinkedList_With_1) + { + struct SinglyLinkedList<int> NewList {}; + ListNode<int> Node1 = { 1,nullptr }; + + Prepend(&NewList, &Node1); + + Assert::AreEqual(1, NewList._head->_data); + Assert::AreEqual(1ull, NewList._size); + } + + TEST_METHOD(Prepend_LinkedList_With_5) + { + struct SinglyLinkedList<int> NewList {}; + ListNode<int> Node1 = { 1,nullptr }; + ListNode<int>Node2 = { 2,nullptr }; + ListNode<int>Node3 = { 3,nullptr }; + ListNode<int>Node4 = { 4,nullptr }; + ListNode<int>Node5 = { 5,nullptr }; + + bool Success = Append(&NewList, &Node2); + Success = Append(&NewList, &Node3); + Success = Append(&NewList, &Node4); + Success = Append(&NewList, &Node5); + + Prepend(&NewList, &Node1); + + Assert::AreEqual(1, NewList._head->_data); + Assert::AreEqual(5ull, NewList._size); + } + + TEST_METHOD(ExtractFirst_LinkedList_Success) + { + struct SinglyLinkedList<int> NewList {}; + ListNode<int> Node1 = { 1,nullptr }; + ListNode<int>Node2 = { 2,nullptr }; + ListNode<int>Node3 = { 3,nullptr }; + ListNode<int>Node4 = { 4,nullptr }; + ListNode<int>Node5 = { 5,nullptr }; + + + bool Success = Append(&NewList, &Node1); + Success = Append(&NewList, &Node2); + Success = Append(&NewList, &Node3); + Success = Append(&NewList, &Node4); + Success = Append(&NewList, &Node5); + + + ListNode<int>* Temp = Extract(&NewList, Node1._data); + ListNode<int>* NullCheck = &Node1; + + Assert::AreEqual(1, Temp->_data); + Assert::IsNull(NullCheck->_next); + + delete Temp; + + } + + TEST_METHOD(Extract_Last_LinkedList_Success) + { + struct SinglyLinkedList<int> NewList {}; + ListNode<int> Node1 = { 1,nullptr }; + ListNode<int>Node2 = { 2,nullptr }; + ListNode<int>Node3 = { 3,nullptr }; + ListNode<int>Node4 = { 4,nullptr }; + ListNode<int>Node5 = { 5,nullptr }; + + bool Success = Append(&NewList, &Node1); + Success = Append(&NewList, &Node2); + Success = Append(&NewList, &Node3); + Success = Append(&NewList, &Node4); + Success = Append(&NewList, &Node5); + + ListNode<int>* Temp = Extract(&NewList, Node5._data); + ListNode<int>* NullCheck = &Node4; + + Assert::AreEqual(5, Temp->_data); + Assert::IsNull(NullCheck->_next); + + delete Temp; + } + + TEST_METHOD(Extract_Middle_LinkedList_Success) + { + struct SinglyLinkedList<int> NewList {}; + ListNode<int> Node1 = { 1,nullptr }; + ListNode<int>Node2 = { 2,nullptr }; + ListNode<int>Node3 = { 3,nullptr }; + ListNode<int>Node4 = { 4,nullptr }; + ListNode<int>Node5 = { 5,nullptr }; + + + + bool Success = Append(&NewList, &Node1); + Success = Append(&NewList, &Node2); + Success = Append(&NewList, &Node3); + Success = Append(&NewList, &Node4); + Success = Append(&NewList, &Node5); + + + ListNode<int>* Temp = Extract(&NewList, Node3._data); + ListNode<int>* NullCheck = &Node3; + + Assert::AreEqual(3, Temp->_data); + Assert::IsNull(Node3._next); + delete Temp; + + } + + + TEST_METHOD(Clear_LinkedList_Full) + { + struct SinglyLinkedList<int> NewList {}; + ListNode<int> Node1 = { 1,nullptr }; + ListNode<int>Node2 = { 2,nullptr }; + ListNode<int>Node3 = { 3,nullptr }; + ListNode<int>Node4 = { 4,nullptr }; + ListNode<int>Node5 = { 5,nullptr }; + + + + bool Success = Append(&NewList, &Node1); + Success = Append(&NewList, &Node2); + Success = Append(&NewList, &Node3); + Success = Append(&NewList, &Node4); + Success = Append(&NewList, &Node5); + Clear(&NewList); + + Assert::AreEqual(0ull, NewList._size); + + } + + TEST_METHOD(Clear_LinkedList_Empty) + { + struct SinglyLinkedList<int> NewList {}; + + Clear(&NewList); + + Assert::AreEqual(0ull, NewList._size); + + } + + TEST_METHOD(Clear_LinkedList_1) + { + struct SinglyLinkedList<int> NewList {}; + ListNode<int> Node1 = { 1,nullptr }; + + bool Success = Append(&NewList, &Node1); + + Clear(&NewList); + + Assert::AreEqual(0ull, NewList._size); + + } + }; + + + + + + TEST_CLASS(NodeUnitTests) + { + public: + const CrtCheckMemory Check; + + + TEST_METHOD(NodeConstructor_Success) + { + Node<int>* NewNode = new Node<int>(); + + Assert::IsNotNull(NewNode); + + + delete NewNode; + } + + + + TEST_METHOD(NodeLoadedConstructor_Success) + { + const CrtCheckMemory Check; + + Node<int>* NewNode = new Node<int>(5); + + Assert::IsNotNull(NewNode); + + Assert::AreEqual(5, NewNode->Data()); + + delete NewNode; + } + + + }; + +}; |