From 24f752f79bda1656feec6f62fb925c08f3916e6b Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Fri, 1 Sep 2017 22:49:13 +0200 Subject: [PATCH] [DEV] test unique ptr --- ememory/UniquePtr.hpp | 27 ++++++++-------- test/testUnique.cpp | 71 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 13 deletions(-) diff --git a/ememory/UniquePtr.hpp b/ememory/UniquePtr.hpp index 18a181e..d041ba0 100644 --- a/ememory/UniquePtr.hpp +++ b/ememory/UniquePtr.hpp @@ -1,21 +1,22 @@ -#ifndef UniquePtr_HPP_INCLUDED -#define UniquePtr_HPP_INCLUDED +#pragma once + +#include namespace ememory { template class UniquePtr { private: EMEM_UPTR_TYPE* m_pointer; - template UniquePtr(UniquePtr &); - template UniquePtr &operator=(UniquePtr &); + + template + UniquePtr(UniquePtr &) = delete; + + template + UniquePtr &operator=(UniquePtr &) = delete; public: UniquePtr() : m_pointer(nullptr) { - } - UniquePtr(nullptr) : - m_pointer(nullptr) { - } explicit UniquePtr(EMEM_UPTR_TYPE* _obj) : m_pointer(_obj) @@ -30,9 +31,9 @@ namespace ememory { return *this; } template - UniquePtr& operator=(UniquePtr _obj){ + UniquePtr& operator=(UniquePtr _obj){ reset(_obj.release()); - m_pointer = etk::move(_obj.pointer) + m_pointer = etk::move(_obj.pointer); return *this; } EMEM_UPTR_TYPE operator*() const{ @@ -42,10 +43,10 @@ namespace ememory { return m_pointer; } EMEM_UPTR_TYPE *get() const{ - return *m_pointer; + return m_pointer; } EMEM_UPTR_TYPE *release(){ - T *tmp = m_pointer; + EMEM_UPTR_TYPE *tmp = m_pointer; m_pointer = 0; return tmp; } @@ -58,7 +59,7 @@ namespace ememory { } }; - template + template UniquePtr makeUniquePtr(EMEM_UPTR_ARG ... _obj) { return ememory::UniquePtr(new EMEM_UPTR_TYPE(_obj...)); } diff --git a/test/testUnique.cpp b/test/testUnique.cpp index e69de29..b96ee08 100644 --- a/test/testUnique.cpp +++ b/test/testUnique.cpp @@ -0,0 +1,71 @@ +/** + * @author Edouard DUPIN + * + * @copyright 2011, Edouard DUPIN, all right reserved + * + * @license MPL v2.0 (see license file) + */ + +#include + +#include +#include +#define NAME "UNIQUE-PTR" + +TEST(TestEmemoryUniquePtr, Creation_1) { + ememory::UniquePtr testData; + EXPECT_EQ(testData.get(), nullptr); +} + +TEST(TestEmemoryUniquePtr, Creation_2) { + ememory::UniquePtr testData(new uint32_t(55)); + EXPECT_NE(testData.get(), nullptr); + EXPECT_EQ(*testData.get(), 55); +} +TEST(TestEmemoryUniquePtr, Creation_3) { + ememory::UniquePtr testData = ememory::makeUniquePtr(456789); + EXPECT_NE(testData.get(), nullptr); + EXPECT_EQ(*testData.get(), 456789); +} + +uint32_t vals[] = {0,0,0,0,0,0,0,0,0,0,0,0,0}; + +class testCreateAndDestroy { + public: + uint32_t m_val; + public: + testCreateAndDestroy(uint32_t _val): + m_val(_val) { + vals[m_val] = 1; + } + ~testCreateAndDestroy() { + vals[m_val] = -1; + } +}; + +TEST(TestEmemoryUniquePtr, reset) { + vals = {0,0,0,0,0,0,0,0,0,0,0,0,0}; + EXPECT_EQ(vals[1], 0); + ememory::UniquePtr testData = ememory::makeUniquePtr(1); + EXPECT_NE(testData.get(), nullptr); + EXPECT_EQ(vals[1], 1); + testData.reset(); + EXPECT_EQ(testData.get(), nullptr); + EXPECT_EQ(vals[1], -1); +} +TEST(TestEmemoryUniquePtr, overwrite) { + vals = {0,0,0,0,0,0,0,0,0,0,0,0,0}; + ememory::UniquePtr testData = ememory::makeUniquePtr(1); + EXPECT_NE(testData.get(), nullptr); + EXPECT_EQ(vals[1], 1); + EXPECT_EQ(vals[2], 0); + testData = ememory::makeUniquePtr(2); + EXPECT_EQ(vals[1], -1); + EXPECT_EQ(vals[2], 1); + testData.reset(); + EXPECT_EQ(vals[1], -1); + EXPECT_EQ(vals[2], -1); +} + + +