diff options
| author | WesleyR <[email protected]> | 2024-06-09 23:09:50 -0700 |
|---|---|---|
| committer | WesleyR <[email protected]> | 2024-06-09 23:09:50 -0700 |
| commit | cb041a37b810a5dfa04c1c7f4315f9c3bbc8df0c (patch) | |
| tree | 61b0d3d555e1596b5156e2d18e015497b438c2e4 | |
| parent | Updating before template node branch (diff) | |
| download | homework-1-wesleyr23-cb041a37b810a5dfa04c1c7f4315f9c3bbc8df0c.tar.xz homework-1-wesleyr23-cb041a37b810a5dfa04c1c7f4315f9c3bbc8df0c.zip | |
Submission for HW 3, In-Class Exercise 3, and In-Class Exercise 4HEADdevelopTemplate_Node
| -rw-r--r-- | CST 126/Homework 3/Homework 3.vcxproj | 1 | ||||
| -rw-r--r-- | CST 126/Homework 3/Homework 3.vcxproj.filters | 3 | ||||
| -rw-r--r-- | CST 126/Homework 3/LinkedListNodes.cpp | 23 | ||||
| -rw-r--r-- | CST 126/Homework 3/node.hpp | 26 | ||||
| -rw-r--r-- | CST 126/LinkedListUnitTests/LinkedListUnitTests.cpp | 25 | ||||
| -rw-r--r-- | CST 126/LinkedListUnitTests/LinkedListUnitTests.vcxproj | 2 | ||||
| -rw-r--r-- | CST 126/LinkedListUnitTests/LinkedListUnitTests.vcxproj.filters | 6 | ||||
| -rw-r--r-- | CST 126/LinkedListUnitTests/NodeUnitTests.cpp | 37 | ||||
| -rw-r--r-- | CST 126/LinkedListUnitTests/crt_check_memory.hpp | 40 |
9 files changed, 152 insertions, 11 deletions
diff --git a/CST 126/Homework 3/Homework 3.vcxproj b/CST 126/Homework 3/Homework 3.vcxproj index e0dff7d..067f362 100644 --- a/CST 126/Homework 3/Homework 3.vcxproj +++ b/CST 126/Homework 3/Homework 3.vcxproj @@ -131,6 +131,7 @@ <ClInclude Include="SinglyLinkedList.hpp" /> </ItemGroup> <ItemGroup> + <ClCompile Include="LinkedListNodes.cpp" /> <ClCompile Include="program.cpp" /> </ItemGroup> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> diff --git a/CST 126/Homework 3/Homework 3.vcxproj.filters b/CST 126/Homework 3/Homework 3.vcxproj.filters index b27b6ea..48d9321 100644 --- a/CST 126/Homework 3/Homework 3.vcxproj.filters +++ b/CST 126/Homework 3/Homework 3.vcxproj.filters @@ -26,5 +26,8 @@ <ClCompile Include="program.cpp"> <Filter>Source Files</Filter> </ClCompile> + <ClCompile Include="LinkedListNodes.cpp"> + <Filter>Header Files</Filter> + </ClCompile> </ItemGroup> </Project>
\ No newline at end of file diff --git a/CST 126/Homework 3/LinkedListNodes.cpp b/CST 126/Homework 3/LinkedListNodes.cpp new file mode 100644 index 0000000..e45b2de --- /dev/null +++ b/CST 126/Homework 3/LinkedListNodes.cpp @@ -0,0 +1,23 @@ +#ifndef LINKED_LIST_NODES_HPP +#define LINKED_LIST_NODES_HPP + +#include "node.hpp" + +namespace CST126 +{ + template<typename T> + class singly_linked_node : Node<T> + { + protected: + singly_linked_node* _next{ nullptr }; + + }; + + template<typename T> + class doubly_linked_node final : singly_linked_node<T> + { + protected: + doubly_linked_node* _prev{ nullptr }; + }; +} +#endif
\ No newline at end of file diff --git a/CST 126/Homework 3/node.hpp b/CST 126/Homework 3/node.hpp index ac89141..44b4142 100644 --- a/CST 126/Homework 3/node.hpp +++ b/CST 126/Homework 3/node.hpp @@ -1,12 +1,9 @@ #ifndef NODE_HPP #define NODE_HPP - - - namespace CST126 { - template<typname T> + template<typename T> class Node { public: @@ -16,27 +13,36 @@ namespace CST126 ~Node() = default; T& Data(); - T Value() const; + T Data() const; void Data(const T& data); private: - T _data; + T _data{}; }; + template<typename T> + Node<T>::Node(const T& data) : _data(data) {}; + template<typename T> + T& Node<T>::Data() + { + return _data; + } template<typename T> - T Node<T>::Data() + T Node<T>::Data() const { - return T(); + return _data; } template<typename T> - void Node<T>::Data(T data) + void Node<T>::Data(const T& data) { _data = data; } -}
\ No newline at end of file +} + +#endif
\ No newline at end of file diff --git a/CST 126/LinkedListUnitTests/LinkedListUnitTests.cpp b/CST 126/LinkedListUnitTests/LinkedListUnitTests.cpp index 15defc0..a58290e 100644 --- a/CST 126/LinkedListUnitTests/LinkedListUnitTests.cpp +++ b/CST 126/LinkedListUnitTests/LinkedListUnitTests.cpp @@ -1,11 +1,14 @@ + #include "pch.h" #include "CppUnitTest.h" - #include <list> using namespace Microsoft::VisualStudio::CppUnitTestFramework; #include "SinglyLinkedList.hpp"; +#include "crt_check_memory.hpp"; + +#pragma once namespace LinkedListUnitTests { @@ -17,6 +20,7 @@ namespace LinkedListUnitTests //Empty TEST_METHOD(EmptyListHasZeroSize) { + const CrtCheckMemory check; //Arrange SinglyLinkedList linkedList{}; @@ -27,6 +31,7 @@ namespace LinkedListUnitTests //Append TEST_METHOD(AppendingLinkedList) { + const CrtCheckMemory check; //Arrange struct SinglyLinkedList linkedList = {}; @@ -44,6 +49,7 @@ namespace LinkedListUnitTests TEST_METHOD(MultipleAppend_LinkedList_Success) { + const CrtCheckMemory check; //Arrange struct SinglyLinkedList linkedList = {}; @@ -76,6 +82,7 @@ namespace LinkedListUnitTests //Prepend TEST_METHOD(OnePrependLinkedList) { + const CrtCheckMemory check; //Arrange struct SinglyLinkedList linkedList = {}; @@ -93,6 +100,7 @@ namespace LinkedListUnitTests TEST_METHOD(MultiplePrepend_LinkedList_Success) { + const CrtCheckMemory check; //Arrange struct SinglyLinkedList linkedList = {}; @@ -125,6 +133,7 @@ namespace LinkedListUnitTests //RemoveFirst TEST_METHOD(RemoveFirstNode_LinkedList_Success) { + const CrtCheckMemory check; struct SinglyLinkedList linkedList = {}; ListNode node1 = { 1, nullptr }; @@ -154,6 +163,7 @@ namespace LinkedListUnitTests TEST_METHOD(RemoveFirstNodeWithOneNode_LinkedList_Success) { + const CrtCheckMemory check; struct SinglyLinkedList linkedList = {}; ListNode node1 = { 1, nullptr }; @@ -169,6 +179,7 @@ namespace LinkedListUnitTests //RemoveLast TEST_METHOD(RemoveLastNode_LinkedList_Success) { + const CrtCheckMemory check; struct SinglyLinkedList linkedList = {}; ListNode node1 = { 1, nullptr }; @@ -198,6 +209,7 @@ namespace LinkedListUnitTests TEST_METHOD(RemoveLastNodeWithOneNode_LinkedList_Success) { + const CrtCheckMemory check; struct SinglyLinkedList linkedList = {}; ListNode node1 = { 1, nullptr }; @@ -213,6 +225,7 @@ namespace LinkedListUnitTests //InsertAfter TEST_METHOD(Single_InserterAfter_LinkedList_Success) { + const CrtCheckMemory check; //Arrange struct SinglyLinkedList newList = {}; //Append Nodes @@ -245,6 +258,7 @@ namespace LinkedListUnitTests TEST_METHOD(Single_InserterAfterEnd_LinkedList_Success) { + const CrtCheckMemory check; //Arrange struct SinglyLinkedList newList = {}; //Append Nodes @@ -278,6 +292,7 @@ namespace LinkedListUnitTests //InsertBefore TEST_METHOD(Single_InserterBefore_LinkedList_Success) { + const CrtCheckMemory check; //Arrange struct SinglyLinkedList newList = {}; //Append Nodes @@ -310,6 +325,7 @@ namespace LinkedListUnitTests TEST_METHOD(Single_InserterBeforeHead_LinkedList_Success) { + const CrtCheckMemory check; //Arrange struct SinglyLinkedList newList = {}; //Append Nodes @@ -343,6 +359,7 @@ namespace LinkedListUnitTests //Clear TEST_METHOD(OneAppend_Clear_LinkedList_Success) { + const CrtCheckMemory check; //Arrange struct SinglyLinkedList linkedList = {}; ListNode node1 = { 1, nullptr }; @@ -359,6 +376,7 @@ namespace LinkedListUnitTests TEST_METHOD(MultipleAppend_Clear_LinkedList_Success) { + const CrtCheckMemory check; //Arrange struct SinglyLinkedList linkedList = {}; @@ -392,6 +410,7 @@ namespace LinkedListUnitTests //Extract TEST_METHOD(ExtractFirst_LinkedList_Success) { + const CrtCheckMemory check; //Arrange struct SinglyLinkedList newList = {}; //Append Nodes @@ -422,6 +441,7 @@ namespace LinkedListUnitTests TEST_METHOD(ExtractLast_LinkedList_Success) { + const CrtCheckMemory check; //Arrange struct SinglyLinkedList newList = {}; //Append Nodes @@ -447,6 +467,7 @@ namespace LinkedListUnitTests TEST_METHOD(ExtractMiddle_LinkedList_Success) { + const CrtCheckMemory check; //Arrange struct SinglyLinkedList newList = {}; //Append Nodes @@ -473,6 +494,7 @@ namespace LinkedListUnitTests //Remove TEST_METHOD(RemoveNode_LinkedList_Success) { + const CrtCheckMemory check; struct SinglyLinkedList linkedList = {}; ListNode node1 = { 1, nullptr }; @@ -503,6 +525,7 @@ namespace LinkedListUnitTests } TEST_METHOD(RemoveNodeWithOneNode_Linked_List_Success) { + const CrtCheckMemory check; struct SinglyLinkedList linkedList = {}; ListNode node1 = { 1, nullptr }; diff --git a/CST 126/LinkedListUnitTests/LinkedListUnitTests.vcxproj b/CST 126/LinkedListUnitTests/LinkedListUnitTests.vcxproj index 25b18fa..f4504d5 100644 --- a/CST 126/LinkedListUnitTests/LinkedListUnitTests.vcxproj +++ b/CST 126/LinkedListUnitTests/LinkedListUnitTests.vcxproj @@ -158,6 +158,7 @@ </ItemDefinitionGroup> <ItemGroup> <ClCompile Include="LinkedListUnitTests.cpp" /> + <ClCompile Include="NodeUnitTests.cpp" /> <ClCompile Include="pch.cpp"> <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader> <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader> @@ -166,6 +167,7 @@ </ClCompile> </ItemGroup> <ItemGroup> + <ClInclude Include="crt_check_memory.hpp" /> <ClInclude Include="pch.h" /> </ItemGroup> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> diff --git a/CST 126/LinkedListUnitTests/LinkedListUnitTests.vcxproj.filters b/CST 126/LinkedListUnitTests/LinkedListUnitTests.vcxproj.filters index ed7bc9d..2f1732f 100644 --- a/CST 126/LinkedListUnitTests/LinkedListUnitTests.vcxproj.filters +++ b/CST 126/LinkedListUnitTests/LinkedListUnitTests.vcxproj.filters @@ -21,10 +21,16 @@ <ClCompile Include="pch.cpp"> <Filter>Source Files</Filter> </ClCompile> + <ClCompile Include="NodeUnitTests.cpp"> + <Filter>Source Files</Filter> + </ClCompile> </ItemGroup> <ItemGroup> <ClInclude Include="pch.h"> <Filter>Header Files</Filter> </ClInclude> + <ClInclude Include="crt_check_memory.hpp"> + <Filter>Header Files</Filter> + </ClInclude> </ItemGroup> </Project>
\ No newline at end of file diff --git a/CST 126/LinkedListUnitTests/NodeUnitTests.cpp b/CST 126/LinkedListUnitTests/NodeUnitTests.cpp new file mode 100644 index 0000000..39228ed --- /dev/null +++ b/CST 126/LinkedListUnitTests/NodeUnitTests.cpp @@ -0,0 +1,37 @@ +#include "pch.h" +#include "CppUnitTest.h" + +#include <list> + +using namespace Microsoft::VisualStudio::CppUnitTestFramework; + +#include "crt_check_memory.hpp" +#include "node.hpp"; + +using namespace CST126; + +namespace LinkedListUnitTests +{ + TEST_CLASS(NodeUnitTests) + { + public: + + //Empty + TEST_METHOD(NodeEmptyConstructor_Success) + { + const CrtCheckMemory check; + //Arrange + Node<int>* newNode = new Node<int>(5); + + + //Act + + + //Assert + Assert::IsNotNull(newNode); + Assert::AreEqual(5, newNode->Data()); + + delete newNode; + } + }; +}
\ No newline at end of file diff --git a/CST 126/LinkedListUnitTests/crt_check_memory.hpp b/CST 126/LinkedListUnitTests/crt_check_memory.hpp new file mode 100644 index 0000000..9cb4ab2 --- /dev/null +++ b/CST 126/LinkedListUnitTests/crt_check_memory.hpp @@ -0,0 +1,40 @@ +#ifndef CRTCHECKMEMORY_H +#define CRTCHECKMEMORY_H + +#include "pch.h" +#include "CppUnitTest.h" +#include "crtdbg.h" + +#define _CRTDBG_MAP_ALLOC + +using namespace Microsoft::VisualStudio::CppUnitTestFramework; + +namespace LinkedListUnitTests +{ + struct CrtCheckMemory + { + _CrtMemState state1{}; + _CrtMemState state2{}; + _CrtMemState state3{}; + CrtCheckMemory() + { + _CrtMemCheckpoint(&state1); + } + + CrtCheckMemory(const CrtCheckMemory& copy) = delete; + CrtCheckMemory& operator=(const CrtCheckMemory& copy) = delete; + CrtCheckMemory(CrtCheckMemory&& copy) = delete; + CrtCheckMemory& operator=(CrtCheckMemory&& copy) = delete; + + ~CrtCheckMemory() + { + _CrtMemCheckpoint(&state2); + + if (_CrtMemDifference(&state3, &state1, &state2) != 0) + { + Assert::Fail(L"Detected memory leaks!"); + } + } + }; +} +#endif
\ No newline at end of file |