[DEV] test unique ptr

This commit is contained in:
Edouard DUPIN 2017-09-01 22:49:13 +02:00
parent ee9a5de786
commit 24f752f79b
2 changed files with 85 additions and 13 deletions

View File

@ -1,21 +1,22 @@
#ifndef UniquePtr_HPP_INCLUDED
#define UniquePtr_HPP_INCLUDED
#pragma once
#include <etk/types.hpp>
namespace ememory {
template <class EMEM_UPTR_TYPE>
class UniquePtr {
private:
EMEM_UPTR_TYPE* m_pointer;
template <class EMEM_UPTR_TYPE> UniquePtr(UniquePtr<EMEM_UPTR_TYPE> &);
template <class EMEM_UPTR_TYPE> UniquePtr &operator=(UniquePtr<EMEM_UPTR_TYPE> &);
template <class EMEM_UPTR_TYPE_2>
UniquePtr(UniquePtr<EMEM_UPTR_TYPE_2> &) = delete;
template <class EMEM_UPTR_TYPE_2>
UniquePtr &operator=(UniquePtr<EMEM_UPTR_TYPE_2> &) = 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 <class EMEM_UPTR_TYPE_2>
UniquePtr& operator=(UniquePtr<EMEM_UPTR_TYPE_2, EMEM_UPTR_DELETER> _obj){
UniquePtr& operator=(UniquePtr<EMEM_UPTR_TYPE_2> _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<class EMEM_UPTR_TYPE>
template<class EMEM_UPTR_TYPE, class... EMEM_UPTR_ARG>
UniquePtr<EMEM_UPTR_TYPE> makeUniquePtr(EMEM_UPTR_ARG ... _obj) {
return ememory::UniquePtr<EMEM_UPTR_TYPE>(new EMEM_UPTR_TYPE(_obj...));
}

View File

@ -0,0 +1,71 @@
/**
* @author Edouard DUPIN
*
* @copyright 2011, Edouard DUPIN, all right reserved
*
* @license MPL v2.0 (see license file)
*/
#include <gtest/gtest.h>
#include <ememory/UniquePtr.hpp>
#include <test-debug/debug.hpp>
#define NAME "UNIQUE-PTR"
TEST(TestEmemoryUniquePtr, Creation_1) {
ememory::UniquePtr<uint32_t> testData;
EXPECT_EQ(testData.get(), nullptr);
}
TEST(TestEmemoryUniquePtr, Creation_2) {
ememory::UniquePtr<uint32_t> testData(new uint32_t(55));
EXPECT_NE(testData.get(), nullptr);
EXPECT_EQ(*testData.get(), 55);
}
TEST(TestEmemoryUniquePtr, Creation_3) {
ememory::UniquePtr<uint32_t> testData = ememory::makeUniquePtr<uint32_t>(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<testCreateAndDestroy> testData = ememory::makeUniquePtr<testCreateAndDestroy>(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<testCreateAndDestroy> testData = ememory::makeUniquePtr<testCreateAndDestroy>(1);
EXPECT_NE(testData.get(), nullptr);
EXPECT_EQ(vals[1], 1);
EXPECT_EQ(vals[2], 0);
testData = ememory::makeUniquePtr<testCreateAndDestroy>(2);
EXPECT_EQ(vals[1], -1);
EXPECT_EQ(vals[2], 1);
testData.reset();
EXPECT_EQ(vals[1], -1);
EXPECT_EQ(vals[2], -1);
}