diff options
| author | WesleyR <[email protected]> | 2024-06-09 22:26:59 -0700 |
|---|---|---|
| committer | WesleyR <[email protected]> | 2024-06-09 22:26:59 -0700 |
| commit | c96bc9a10c216dbaee41b247c0fafe4ea497337d (patch) | |
| tree | 8ec905871c2174b915040eac68322fa183c40127 | |
| parent | In-Class Exercise 2 (diff) | |
| download | homework-1-wesleyr23-c96bc9a10c216dbaee41b247c0fafe4ea497337d.tar.xz homework-1-wesleyr23-c96bc9a10c216dbaee41b247c0fafe4ea497337d.zip | |
Updating before template node branch
| -rw-r--r-- | CST 126/CST_126.sln | 10 | ||||
| -rw-r--r-- | CST 126/Homework 3/Homework 3.vcxproj | 41 | ||||
| -rw-r--r-- | CST 126/Homework 3/Homework 3.vcxproj.filters | 13 | ||||
| -rw-r--r-- | CST 126/Homework 3/SinglyLinkedList.hpp | 256 | ||||
| -rw-r--r-- | CST 126/Homework 3/node.hpp | 42 | ||||
| -rw-r--r-- | CST 126/Homework 3/program.cpp | 10 | ||||
| -rw-r--r-- | CST 126/LinkedListUnitTests/LinkedListUnitTests.cpp | 519 | ||||
| -rw-r--r-- | CST 126/LinkedListUnitTests/LinkedListUnitTests.vcxproj | 174 | ||||
| -rw-r--r-- | CST 126/LinkedListUnitTests/LinkedListUnitTests.vcxproj.filters | 30 | ||||
| -rw-r--r-- | CST 126/LinkedListUnitTests/pch.cpp | 5 | ||||
| -rw-r--r-- | CST 126/LinkedListUnitTests/pch.h | 12 |
11 files changed, 1092 insertions, 20 deletions
diff --git a/CST 126/CST_126.sln b/CST 126/CST_126.sln index 060ef9f..cc4f241 100644 --- a/CST 126/CST_126.sln +++ b/CST 126/CST_126.sln @@ -9,6 +9,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Homework2", "Homework2\Home EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Homework 3", "Homework 3\Homework 3.vcxproj", "{9605D423-3B58-499C-9944-5C5D3CEA87AA}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "LinkedListUnitTests", "LinkedListUnitTests\LinkedListUnitTests.vcxproj", "{764A846B-5FB7-4C7A-8D47-9CC204AEEE15}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|x64 = Debug|x64 @@ -41,6 +43,14 @@ Global {9605D423-3B58-499C-9944-5C5D3CEA87AA}.Release|x64.Build.0 = Release|x64 {9605D423-3B58-499C-9944-5C5D3CEA87AA}.Release|x86.ActiveCfg = Release|Win32 {9605D423-3B58-499C-9944-5C5D3CEA87AA}.Release|x86.Build.0 = Release|Win32 + {764A846B-5FB7-4C7A-8D47-9CC204AEEE15}.Debug|x64.ActiveCfg = Debug|x64 + {764A846B-5FB7-4C7A-8D47-9CC204AEEE15}.Debug|x64.Build.0 = Debug|x64 + {764A846B-5FB7-4C7A-8D47-9CC204AEEE15}.Debug|x86.ActiveCfg = Debug|Win32 + {764A846B-5FB7-4C7A-8D47-9CC204AEEE15}.Debug|x86.Build.0 = Debug|Win32 + {764A846B-5FB7-4C7A-8D47-9CC204AEEE15}.Release|x64.ActiveCfg = Release|x64 + {764A846B-5FB7-4C7A-8D47-9CC204AEEE15}.Release|x64.Build.0 = Release|x64 + {764A846B-5FB7-4C7A-8D47-9CC204AEEE15}.Release|x86.ActiveCfg = Release|Win32 + {764A846B-5FB7-4C7A-8D47-9CC204AEEE15}.Release|x86.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/CST 126/Homework 3/Homework 3.vcxproj b/CST 126/Homework 3/Homework 3.vcxproj index 21ecb73..e0dff7d 100644 --- a/CST 126/Homework 3/Homework 3.vcxproj +++ b/CST 126/Homework 3/Homework 3.vcxproj @@ -17,7 +17,6 @@ <Configuration>Release</Configuration> <Platform>x64</Platform> </ProjectConfiguration> - </ItemGroup> <PropertyGroup Label="Globals"> <VCProjectVersion>17.0</VCProjectVersion> @@ -53,27 +52,24 @@ <WholeProgramOptimization>true</WholeProgramOptimization> <CharacterSet>Unicode</CharacterSet> </PropertyGroup> - <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> <ImportGroup Label="ExtensionSettings"> </ImportGroup> - <ImportGroup Label="Shared" > + <ImportGroup Label="Shared"> + </ImportGroup> + <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> + </ImportGroup> + <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> + </ImportGroup> + <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> + </ImportGroup> + <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> </ImportGroup> - <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> - <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> - </ImportGroup> - <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> - <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> - </ImportGroup> - <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> - <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> - </ImportGroup> - <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> - <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> - </ImportGroup> - <PropertyGroup Label="UserMacros" /> - <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> <ClCompile> <WarningLevel>Level3</WarningLevel> @@ -130,9 +126,14 @@ <GenerateDebugInformation>true</GenerateDebugInformation> </Link> </ItemDefinitionGroup> - - <ItemGroup></ItemGroup> + <ItemGroup> + <ClInclude Include="node.hpp" /> + <ClInclude Include="SinglyLinkedList.hpp" /> + </ItemGroup> + <ItemGroup> + <ClCompile Include="program.cpp" /> + </ItemGroup> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <ImportGroup Label="ExtensionTargets"> </ImportGroup> -</Project> +</Project>
\ No newline at end of file diff --git a/CST 126/Homework 3/Homework 3.vcxproj.filters b/CST 126/Homework 3/Homework 3.vcxproj.filters index a8a6563..b27b6ea 100644 --- a/CST 126/Homework 3/Homework 3.vcxproj.filters +++ b/CST 126/Homework 3/Homework 3.vcxproj.filters @@ -14,4 +14,17 @@ <Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions> </Filter> </ItemGroup> + <ItemGroup> + <ClInclude Include="SinglyLinkedList.hpp"> + <Filter>Header Files</Filter> + </ClInclude> + <ClInclude Include="node.hpp"> + <Filter>Header Files</Filter> + </ClInclude> + </ItemGroup> + <ItemGroup> + <ClCompile Include="program.cpp"> + <Filter>Source Files</Filter> + </ClCompile> + </ItemGroup> </Project>
\ No newline at end of file diff --git a/CST 126/Homework 3/SinglyLinkedList.hpp b/CST 126/Homework 3/SinglyLinkedList.hpp new file mode 100644 index 0000000..e28c142 --- /dev/null +++ b/CST 126/Homework 3/SinglyLinkedList.hpp @@ -0,0 +1,256 @@ +#ifndef SINGLY_LINKED_LIST_HPP +#define SINGLY_LINKED_LIST_HPP + +struct ListNode +{ + int _data{ 0 }; + ListNode* _next{ nullptr }; +}; + +struct SinglyLinkedList +{ + size_t _size{ 0 }; + ListNode* _head{ nullptr }; +}; + +bool Append(SinglyLinkedList* list, ListNode* node); +bool Prepend(SinglyLinkedList* list, ListNode* node); +bool RemoveFirst(SinglyLinkedList* list); +bool RemoveLast(SinglyLinkedList* list); +bool InsertAfter(SinglyLinkedList* list, const int data, ListNode* node); +bool InsertBefore(SinglyLinkedList* list, const int data, ListNode* node); +bool Clear(SinglyLinkedList* list); +ListNode* Extract(SinglyLinkedList* list, const int data); +bool Empty(SinglyLinkedList* list); +bool Remove(SinglyLinkedList* list, ListNode* node); + +//code +bool Append(SinglyLinkedList* list, ListNode* node ) +{ + //we have a list + //we need to add a node to the end of the list + + //empty + if (Empty(list)) + { + list->_head = node; + list->_size++; + return true; + } + //Append head + if(list->_size == 1) + { + list->_head->_next = node; + list->_size++; + return true; + } + //Down the list + ListNode* travel = nullptr; + for(travel = list->_head; travel->_next != nullptr;) + { + travel = travel->_next; + } + travel->_next = node; + list->_size++; + return true; +} + +bool Prepend(SinglyLinkedList* list, ListNode* node) +{ + if (Empty(list)) + { + list->_head = node; + list->_size++; + return true; + } + + node->_next = list->_head; + list->_head = node; + + list->_size++; + return true; +} + +bool RemoveFirst(SinglyLinkedList* list) +{ + //check if zero size + if (Empty(list)) return true; + + if(list->_size == 1) + { + + list->_head = nullptr; + } + else + { + ListNode* prev = list->_head; + list->_head = list->_head->_next; + prev->_next = nullptr; + } + + list->_size--; + + return true; +} + +bool RemoveLast(SinglyLinkedList* list) +{ + if (Empty(list)) return true; + + if (list->_size == 1) + { + list->_head = nullptr; + return true; + } + + ListNode* travel = list->_head; + while(travel->_next->_next != nullptr) + { + travel = travel->_next; + } + travel->_next = nullptr; + list->_size--; + return true; + +} + +bool InsertAfter(SinglyLinkedList* list, const int data, ListNode* node) +{ + //first to find data + //thoughts? What if we are inserting at after end. + ListNode* travel; + for (travel = list->_head; travel->_data != data && travel->_next != nullptr; ) + { + travel = travel->_next; + } + + if (travel->_next == nullptr) + { + return Append(list, node); + } + + node->_next = travel->_next; + travel->_next = node; + list->_size++; + + //then insert after data + return true; +} + +bool InsertBefore(SinglyLinkedList* list, const int data, ListNode* node) +{ + if (list->_head->_data == data) + return Prepend(list, node); + + ListNode* travel; + for (travel = list->_head; travel->_next->_data != data;) + { + travel = travel->_next; + } + + node->_next = travel->_next; + travel->_next = node; + list->_size++; + + return true; + +} + +bool Clear(SinglyLinkedList* list) +{ + //consider the head + //consider the size + //We have to detach every node from each other + //start at the head + //use a node* to track our position + + if (list->_size == 0) return true; + + ListNode* travel = list->_head; + ListNode* prev = travel; + + do + { + prev = travel; + travel = travel->_next; + prev->_data = 0; + prev->_next = nullptr; + + } while (travel != nullptr && travel->_next != nullptr); + + list->_size = 0; + list->_head = nullptr; + + return true; +} + +ListNode* Extract(SinglyLinkedList* list, const int data) +{ + //check if empty, shortcut return + ListNode* temp = new ListNode{0, nullptr}; + ListNode* travel = nullptr; + + if (Empty(list)) + { + return temp; + } + //Read through list + for (travel = list->_head; travel->_data != data && travel->_next != nullptr; ) + { + travel = travel->_next; + } + //find the data + //pointer to the node + temp->_data = travel->_data; + //remove node from list + if(Remove(list, travel)) + { + return temp; + } + //or don't find data + + return nullptr; +} + +bool Remove(SinglyLinkedList* list, ListNode* node) +{ + //find the node + //Read through list + ListNode* travel = list->_head; + ListNode* prev = travel; + + while(travel != node) + { + prev = travel; + + travel = travel->_next; + } + //if first, remove first + if(travel == list->_head) + { + return RemoveFirst(list); + } + //if last, remove last + if(travel->_next == nullptr) + { + return RemoveLast(list); + } + //else in middle + //remove travel's node + //will need previous node + //to set previous's next to travel's next + prev->_next = travel->_next; //fixing pointer between nodes + //removes dangling pointer from prev to travel + + //delete travel + travel->_next = nullptr; + travel->_data = 0; + list->_size--; +} + +bool Empty(SinglyLinkedList* list) +{ + return (list->_size == 0 && list->_head == nullptr); +} + +#endif // !SINGLY_LINKED_LIST_HPP
\ No newline at end of file diff --git a/CST 126/Homework 3/node.hpp b/CST 126/Homework 3/node.hpp new file mode 100644 index 0000000..ac89141 --- /dev/null +++ b/CST 126/Homework 3/node.hpp @@ -0,0 +1,42 @@ +#ifndef NODE_HPP +#define NODE_HPP + + + + +namespace CST126 +{ + template<typname T> + class Node + { + public: + Node() = default; + Node(const T& data); + + ~Node() = default; + + T& Data(); + T Value() const; + void Data(const T& data); + + + + private: + T _data; + }; + + + + + template<typename T> + T Node<T>::Data() + { + return T(); + } + + template<typename T> + void Node<T>::Data(T data) + { + _data = data; + } +}
\ No newline at end of file diff --git a/CST 126/Homework 3/program.cpp b/CST 126/Homework 3/program.cpp new file mode 100644 index 0000000..4d6c025 --- /dev/null +++ b/CST 126/Homework 3/program.cpp @@ -0,0 +1,10 @@ + + + + +void main() +{ + + + +}
\ No newline at end of file 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); + } + + }; +} diff --git a/CST 126/LinkedListUnitTests/LinkedListUnitTests.vcxproj b/CST 126/LinkedListUnitTests/LinkedListUnitTests.vcxproj new file mode 100644 index 0000000..25b18fa --- /dev/null +++ b/CST 126/LinkedListUnitTests/LinkedListUnitTests.vcxproj @@ -0,0 +1,174 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <ItemGroup Label="ProjectConfigurations"> + <ProjectConfiguration Include="Debug|Win32"> + <Configuration>Debug</Configuration> + <Platform>Win32</Platform> + </ProjectConfiguration> + <ProjectConfiguration Include="Release|Win32"> + <Configuration>Release</Configuration> + <Platform>Win32</Platform> + </ProjectConfiguration> + <ProjectConfiguration Include="Debug|x64"> + <Configuration>Debug</Configuration> + <Platform>x64</Platform> + </ProjectConfiguration> + <ProjectConfiguration Include="Release|x64"> + <Configuration>Release</Configuration> + <Platform>x64</Platform> + </ProjectConfiguration> + </ItemGroup> + <PropertyGroup Label="Globals"> + <VCProjectVersion>17.0</VCProjectVersion> + <ProjectGuid>{764A846B-5FB7-4C7A-8D47-9CC204AEEE15}</ProjectGuid> + <Keyword>Win32Proj</Keyword> + <RootNamespace>LinkedListUnitTests</RootNamespace> + <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion> + <ProjectSubType>NativeUnitTestProject</ProjectSubType> + </PropertyGroup> + <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> + <ConfigurationType>DynamicLibrary</ConfigurationType> + <UseDebugLibraries>true</UseDebugLibraries> + <PlatformToolset>v143</PlatformToolset> + <CharacterSet>Unicode</CharacterSet> + <UseOfMfc>false</UseOfMfc> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> + <ConfigurationType>DynamicLibrary</ConfigurationType> + <UseDebugLibraries>false</UseDebugLibraries> + <PlatformToolset>v143</PlatformToolset> + <WholeProgramOptimization>true</WholeProgramOptimization> + <CharacterSet>Unicode</CharacterSet> + <UseOfMfc>false</UseOfMfc> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration"> + <ConfigurationType>DynamicLibrary</ConfigurationType> + <UseDebugLibraries>true</UseDebugLibraries> + <PlatformToolset>v143</PlatformToolset> + <CharacterSet>Unicode</CharacterSet> + <UseOfMfc>false</UseOfMfc> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration"> + <ConfigurationType>DynamicLibrary</ConfigurationType> + <UseDebugLibraries>false</UseDebugLibraries> + <PlatformToolset>v143</PlatformToolset> + <WholeProgramOptimization>true</WholeProgramOptimization> + <CharacterSet>Unicode</CharacterSet> + <UseOfMfc>false</UseOfMfc> + </PropertyGroup> + <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> + <ImportGroup Label="ExtensionSettings"> + </ImportGroup> + <ImportGroup Label="Shared"> + </ImportGroup> + <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> + </ImportGroup> + <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> + </ImportGroup> + <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> + </ImportGroup> + <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> + </ImportGroup> + <PropertyGroup Label="UserMacros" /> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> + <LinkIncremental>true</LinkIncremental> + <IncludePath>..\Homework 3;$(IncludePath)</IncludePath> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> + <LinkIncremental>true</LinkIncremental> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> + <LinkIncremental>false</LinkIncremental> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> + <LinkIncremental>false</LinkIncremental> + </PropertyGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> + <ClCompile> + <PrecompiledHeader>Use</PrecompiledHeader> + <WarningLevel>Level3</WarningLevel> + <SDLCheck>true</SDLCheck> + <AdditionalIncludeDirectories>$(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> + <PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions> + <UseFullPaths>true</UseFullPaths> + <PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile> + </ClCompile> + <Link> + <SubSystem>Windows</SubSystem> + <AdditionalLibraryDirectories>$(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> + </Link> + </ItemDefinitionGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> + <ClCompile> + <PrecompiledHeader>Use</PrecompiledHeader> + <WarningLevel>Level3</WarningLevel> + <SDLCheck>true</SDLCheck> + <AdditionalIncludeDirectories>$(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> + <PreprocessorDefinitions>WIN32;_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions> + <UseFullPaths>true</UseFullPaths> + <PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile> + </ClCompile> + <Link> + <SubSystem>Windows</SubSystem> + <AdditionalLibraryDirectories>$(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> + </Link> + </ItemDefinitionGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> + <ClCompile> + <PrecompiledHeader>Use</PrecompiledHeader> + <WarningLevel>Level3</WarningLevel> + <FunctionLevelLinking>true</FunctionLevelLinking> + <IntrinsicFunctions>true</IntrinsicFunctions> + <SDLCheck>true</SDLCheck> + <AdditionalIncludeDirectories>$(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> + <PreprocessorDefinitions>WIN32;NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions> + <UseFullPaths>true</UseFullPaths> + <PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile> + </ClCompile> + <Link> + <SubSystem>Windows</SubSystem> + <EnableCOMDATFolding>true</EnableCOMDATFolding> + <OptimizeReferences>true</OptimizeReferences> + <AdditionalLibraryDirectories>$(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> + </Link> + </ItemDefinitionGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> + <ClCompile> + <PrecompiledHeader>Use</PrecompiledHeader> + <WarningLevel>Level3</WarningLevel> + <FunctionLevelLinking>true</FunctionLevelLinking> + <IntrinsicFunctions>true</IntrinsicFunctions> + <SDLCheck>true</SDLCheck> + <AdditionalIncludeDirectories>$(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> + <PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions> + <UseFullPaths>true</UseFullPaths> + <PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile> + </ClCompile> + <Link> + <SubSystem>Windows</SubSystem> + <EnableCOMDATFolding>true</EnableCOMDATFolding> + <OptimizeReferences>true</OptimizeReferences> + <AdditionalLibraryDirectories>$(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> + </Link> + </ItemDefinitionGroup> + <ItemGroup> + <ClCompile Include="LinkedListUnitTests.cpp" /> + <ClCompile Include="pch.cpp"> + <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader> + <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader> + <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader> + <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader> + </ClCompile> + </ItemGroup> + <ItemGroup> + <ClInclude Include="pch.h" /> + </ItemGroup> + <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> + <ImportGroup Label="ExtensionTargets"> + </ImportGroup> +</Project>
\ No newline at end of file diff --git a/CST 126/LinkedListUnitTests/LinkedListUnitTests.vcxproj.filters b/CST 126/LinkedListUnitTests/LinkedListUnitTests.vcxproj.filters new file mode 100644 index 0000000..ed7bc9d --- /dev/null +++ b/CST 126/LinkedListUnitTests/LinkedListUnitTests.vcxproj.filters @@ -0,0 +1,30 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <ItemGroup> + <Filter Include="Source Files"> + <UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier> + <Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions> + </Filter> + <Filter Include="Header Files"> + <UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier> + <Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions> + </Filter> + <Filter Include="Resource Files"> + <UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier> + <Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions> + </Filter> + </ItemGroup> + <ItemGroup> + <ClCompile Include="LinkedListUnitTests.cpp"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="pch.cpp"> + <Filter>Source Files</Filter> + </ClCompile> + </ItemGroup> + <ItemGroup> + <ClInclude Include="pch.h"> + <Filter>Header Files</Filter> + </ClInclude> + </ItemGroup> +</Project>
\ No newline at end of file diff --git a/CST 126/LinkedListUnitTests/pch.cpp b/CST 126/LinkedListUnitTests/pch.cpp new file mode 100644 index 0000000..64b7eef --- /dev/null +++ b/CST 126/LinkedListUnitTests/pch.cpp @@ -0,0 +1,5 @@ +// pch.cpp: source file corresponding to the pre-compiled header + +#include "pch.h" + +// When you are using pre-compiled headers, this source file is necessary for compilation to succeed. diff --git a/CST 126/LinkedListUnitTests/pch.h b/CST 126/LinkedListUnitTests/pch.h new file mode 100644 index 0000000..9d715b0 --- /dev/null +++ b/CST 126/LinkedListUnitTests/pch.h @@ -0,0 +1,12 @@ +// pch.h: This is a precompiled header file. +// Files listed below are compiled only once, improving build performance for future builds. +// This also affects IntelliSense performance, including code completion and many code browsing features. +// However, files listed here are ALL re-compiled if any one of them is updated between builds. +// Do not add files here that you will be updating frequently as this negates the performance advantage. + +#ifndef PCH_H +#define PCH_H + +// add headers that you want to pre-compile here + +#endif //PCH_H |