From 2977e76af7007f470227ccbd6a7e3cd9dd1f4bb3 Mon Sep 17 00:00:00 2001 From: rPatrickWarner Date: Wed, 22 May 2024 19:42:15 -0700 Subject: unit test and memory check added --- CST 126/Homework3/Homework3.vcxproj | 1 + CST 126/Homework3/Homework3.vcxproj.filters | 3 ++ CST 126/Homework3/Node.hpp | 55 +++++++++++++++++++++++++++ CST 126/Homework3/SinglyLinkedList.hpp | 1 + CST 126/UnitTester/UnitTester.cpp | 43 ++++++++++++++++++++- CST 126/UnitTester/UnitTester.vcxproj | 1 + CST 126/UnitTester/UnitTester.vcxproj.filters | 3 ++ CST 126/UnitTester/crt_check_memory.hpp | 40 +++++++++++++++++++ 8 files changed, 146 insertions(+), 1 deletion(-) create mode 100644 CST 126/Homework3/Node.hpp create mode 100644 CST 126/UnitTester/crt_check_memory.hpp diff --git a/CST 126/Homework3/Homework3.vcxproj b/CST 126/Homework3/Homework3.vcxproj index 4ddeafd..0625066 100644 --- a/CST 126/Homework3/Homework3.vcxproj +++ b/CST 126/Homework3/Homework3.vcxproj @@ -133,6 +133,7 @@ + diff --git a/CST 126/Homework3/Homework3.vcxproj.filters b/CST 126/Homework3/Homework3.vcxproj.filters index 5fbb0b9..6f86576 100644 --- a/CST 126/Homework3/Homework3.vcxproj.filters +++ b/CST 126/Homework3/Homework3.vcxproj.filters @@ -23,5 +23,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/CST 126/Homework3/Node.hpp b/CST 126/Homework3/Node.hpp new file mode 100644 index 0000000..9a94d3c --- /dev/null +++ b/CST 126/Homework3/Node.hpp @@ -0,0 +1,55 @@ +#ifndef NODE_HPP +#define NODE_HPP + +namespace CST_126 +{ + template + class Node + { + public: + Node() = default; + Node(const T& Data); + + ~Node() = default; + + + T& Data(); + T Data() const; + void Data(const T& Data); + + + + + private: + + T _data{ 0 }; + + }; + + template + Node::Node(const T& Data) : _data(Data) {}; + + template + inline T& Node::Data() + { + return _data; + } + + template + inline T Node::Data() const + { + return _data; + } + + template + inline void Node::Data(const T& Data) + { + _data = data; + } + +} + + + + +#endif \ No newline at end of file diff --git a/CST 126/Homework3/SinglyLinkedList.hpp b/CST 126/Homework3/SinglyLinkedList.hpp index 06e5f08..0f79890 100644 --- a/CST 126/Homework3/SinglyLinkedList.hpp +++ b/CST 126/Homework3/SinglyLinkedList.hpp @@ -1,6 +1,7 @@ #ifndef SINGLY_LINKED_LIST_HPP #define SINGLY_LINKED_LIST_HPP #include +#include "Node.hpp" template struct ListNode { diff --git a/CST 126/UnitTester/UnitTester.cpp b/CST 126/UnitTester/UnitTester.cpp index 1ad15fd..aa04fe1 100644 --- a/CST 126/UnitTester/UnitTester.cpp +++ b/CST 126/UnitTester/UnitTester.cpp @@ -1,7 +1,10 @@ #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 { @@ -216,5 +219,43 @@ namespace LinkedListUnitTests //} + + + + + }; -} + TEST_CLASS(NodeUnitTests) + { + public: + const CrtCheckMemory Check; + + + TEST_METHOD(NodeConstructor_Success) + { + Node* NewNode = new Node(); + + Assert::IsNotNull(NewNode); + + + delete NewNode; + } + + + + TEST_METHOD(NodeLoadedConstructor_Success) + { + const CrtCheckMemory Check; + + Node* NewNode = new Node(5); + + Assert::IsNotNull(NewNode); + Assert::AreEqual(5, NewNode->Data()); + } + + + } + + + +}; diff --git a/CST 126/UnitTester/UnitTester.vcxproj b/CST 126/UnitTester/UnitTester.vcxproj index 0cbba9e..1c29209 100644 --- a/CST 126/UnitTester/UnitTester.vcxproj +++ b/CST 126/UnitTester/UnitTester.vcxproj @@ -166,6 +166,7 @@ + diff --git a/CST 126/UnitTester/UnitTester.vcxproj.filters b/CST 126/UnitTester/UnitTester.vcxproj.filters index 7a76918..a210b65 100644 --- a/CST 126/UnitTester/UnitTester.vcxproj.filters +++ b/CST 126/UnitTester/UnitTester.vcxproj.filters @@ -26,5 +26,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/CST 126/UnitTester/crt_check_memory.hpp b/CST 126/UnitTester/crt_check_memory.hpp new file mode 100644 index 0000000..9cb4ab2 --- /dev/null +++ b/CST 126/UnitTester/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 -- cgit v1.2.3