From f30bec26eea50e08fd83537723827c664d539a25 Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Sun, 21 Jun 2015 17:14:35 +0200 Subject: [PATCH] Added some more test cases for dynamic_pointer_move --- biicode.conf | 1 + test/pointer_destructor_test.cpp | 63 ++++++++++++++++++++++++++++++++ test/pointer_test.cpp | 20 ++++++---- 3 files changed, 77 insertions(+), 7 deletions(-) create mode 100644 test/pointer_destructor_test.cpp diff --git a/biicode.conf b/biicode.conf index 9a769a4e..5920e67e 100644 --- a/biicode.conf +++ b/biicode.conf @@ -2,6 +2,7 @@ [requirements] cryptopp/cryptopp: 9 + google/gmock: 2 google/gtest: 11 messmer/cmake: 3 diff --git a/test/pointer_destructor_test.cpp b/test/pointer_destructor_test.cpp new file mode 100644 index 00000000..57e549a5 --- /dev/null +++ b/test/pointer_destructor_test.cpp @@ -0,0 +1,63 @@ +#include "google/gtest/gtest.h" +#include "google/gmock/gmock.h" +#include "../pointer.h" + +using namespace cpputils; + +using std::unique_ptr; +using std::make_unique; +using std::function; + +// Putting them in an own namespace is needed, so they don't clash with globally defined Parent/Child classes +namespace withdestructorcallback { + class DestructorCallback { + public: + MOCK_CONST_METHOD0(call, void()); + }; + + class Parent { + public: + virtual ~Parent() { } + }; + + class Child : public Parent { + public: + Child(const DestructorCallback &childDestructorCallback) : _destructorCallback(childDestructorCallback) { } + + ~Child() { + _destructorCallback.call(); + } + + private: + const DestructorCallback &_destructorCallback; + }; +} +using namespace withdestructorcallback; + +class DynamicPointerMoveDestructorTest: public ::testing::Test { +public: + DestructorCallback childDestructorCallback; + unique_ptr createChild() { + return make_unique(childDestructorCallback); + } + void EXPECT_CHILD_DESTRUCTOR_CALLED() { + EXPECT_CALL(childDestructorCallback, call()).Times(1); + } +}; + +TEST_F(DynamicPointerMoveDestructorTest, ChildInParentPtr) { + unique_ptr parent = createChild(); + EXPECT_CHILD_DESTRUCTOR_CALLED(); +} + +TEST_F(DynamicPointerMoveDestructorTest, ChildToParentCast) { + unique_ptr child = createChild(); + unique_ptr parent = dynamic_pointer_move(child); + EXPECT_CHILD_DESTRUCTOR_CALLED(); +} + +TEST_F(DynamicPointerMoveDestructorTest, ParentToChildCast) { + unique_ptr parent = createChild(); + unique_ptr child = dynamic_pointer_move(parent); + EXPECT_CHILD_DESTRUCTOR_CALLED(); +} diff --git a/test/pointer_test.cpp b/test/pointer_test.cpp index d31361ba..0232934a 100644 --- a/test/pointer_test.cpp +++ b/test/pointer_test.cpp @@ -5,14 +5,20 @@ using namespace cpputils; using std::unique_ptr; -class Parent { -public: - virtual ~Parent() {} -}; -class Child: public Parent {}; -class Child2: public Parent {}; +// Putting them in an own namespace is needed, so they don't clash with globally defined Parent/Child classes +namespace testobjs { + class Parent { + public: + virtual ~Parent() { } + }; -//TODO Add test cases that the correct (virtual) destructor is called + class Child : public Parent { + }; + + class Child2 : public Parent { + }; +} +using namespace testobjs; TEST(DynamicPointerMoveTest, NullPtrParentToChildCast) { unique_ptr source(nullptr);