ememory/test/testEnableSharedFromThis.cpp

54 lines
1.4 KiB
C++
Raw Normal View History

/** @file
* @author Edouard DUPIN
* @copyright 2016, Edouard DUPIN, all right reserved
* @license MPL v2.0 (see license file)
*/
2017-09-07 23:38:26 +02:00
#include <etest/etest.hpp>
2016-09-30 22:28:36 +02:00
#include <ememory/memory.hpp>
#include "main.hpp"
namespace testESFT {
class TestClassBase : public ememory::EnableSharedFromThis<TestClassBase> {
public:
TestClassBase() {
EMEMORY_INFO("create TestClassBase");
}
~TestClassBase() {
EMEMORY_INFO("delete TestClassBase");
}
};
class TestClass : public testESFT::TestClassBase {
public:
TestClass() {
EMEMORY_INFO("create TestClass");
}
~TestClass() {
EMEMORY_INFO("delete TestClass");
}
};
}
TEST(TestEnableSharedFromThis, testClassBase) {
ememory::SharedPtr<testESFT::TestClassBase> data = ememory::makeShared<testESFT::TestClassBase>();
EXPECT_EQ(data.useCount(), 1);
ememory::WeakPtr<testESFT::TestClassBase> dataWeak = data;
EXPECT_EQ(data.useCount(), 1);
EXPECT_EQ(dataWeak.useCount(), 1);
data.reset();
EXPECT_EQ(data.useCount(), 0);
EXPECT_EQ(dataWeak.useCount(), 0);
}
TEST(TestEnableSharedFromThis, testClass) {
ememory::SharedPtr<testESFT::TestClass> data = ememory::makeShared<testESFT::TestClass>();
EXPECT_EQ(data.useCount(), 1);
ememory::WeakPtr<testESFT::TestClass> dataWeak = data;
EXPECT_EQ(data.useCount(), 1);
EXPECT_EQ(dataWeak.useCount(), 1);
data.reset();
EXPECT_EQ(data.useCount(), 0);
EXPECT_EQ(dataWeak.useCount(), 0);
}