2015-02-16 21:31:07 +01:00
|
|
|
#include "google/gtest/gtest.h"
|
2015-06-21 17:31:19 +02:00
|
|
|
#include "../../pointer/optional_ownership_ptr.h"
|
|
|
|
#include "../../macros.h"
|
2015-02-16 21:31:07 +01:00
|
|
|
|
|
|
|
using std::unique_ptr;
|
|
|
|
using std::function;
|
|
|
|
using ::testing::Test;
|
|
|
|
|
2015-02-16 21:36:00 +01:00
|
|
|
using namespace cpputils;
|
2015-02-16 21:31:07 +01:00
|
|
|
|
|
|
|
class TestObject {
|
|
|
|
public:
|
|
|
|
TestObject(function<void()> destructorListener) : _destructorListener(destructorListener) {}
|
|
|
|
virtual ~TestObject() {
|
|
|
|
_destructorListener();
|
|
|
|
}
|
2015-10-17 20:15:20 +02:00
|
|
|
|
2015-02-16 21:31:07 +01:00
|
|
|
private:
|
|
|
|
function<void()> _destructorListener;
|
|
|
|
};
|
|
|
|
|
|
|
|
class TestObjectHolder {
|
|
|
|
public:
|
|
|
|
TestObjectHolder()
|
|
|
|
: _isDestructed(false),
|
|
|
|
_testObject(new TestObject([this]() {_isDestructed = true;})) {
|
|
|
|
}
|
|
|
|
|
|
|
|
~TestObjectHolder() {
|
|
|
|
if (!_isDestructed) {
|
|
|
|
delete _testObject;
|
|
|
|
_isDestructed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TestObject *get() {
|
|
|
|
return _testObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isDestructed() {
|
|
|
|
return _isDestructed;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
bool _isDestructed;
|
|
|
|
TestObject *_testObject;
|
2015-10-17 20:15:20 +02:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(TestObjectHolder);
|
2015-02-16 21:31:07 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class OptionalOwnershipPointerTest: public Test {
|
|
|
|
public:
|
2015-10-17 20:15:20 +02:00
|
|
|
OptionalOwnershipPointerTest(): obj(), obj2() {}
|
|
|
|
|
2015-02-16 21:31:07 +01:00
|
|
|
TestObjectHolder obj;
|
|
|
|
TestObjectHolder obj2;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(OptionalOwnershipPointerTest, TestIsInitializedCorrectly) {
|
|
|
|
EXPECT_FALSE(obj.isDestructed());
|
|
|
|
}
|
|
|
|
|
2015-07-21 15:18:14 +02:00
|
|
|
TEST_F(OptionalOwnershipPointerTest, DestructsWhenItHasOwnership_UniquePtr) {
|
2015-02-16 21:31:07 +01:00
|
|
|
{
|
2015-02-16 21:36:00 +01:00
|
|
|
optional_ownership_ptr<TestObject> ptr = WithOwnership(unique_ptr<TestObject>(obj.get()));
|
2015-07-21 15:18:14 +02:00
|
|
|
EXPECT_FALSE(obj.isDestructed());
|
2015-02-16 21:31:07 +01:00
|
|
|
UNUSED(ptr);
|
|
|
|
}
|
|
|
|
EXPECT_TRUE(obj.isDestructed());
|
|
|
|
}
|
|
|
|
|
2015-07-21 15:18:14 +02:00
|
|
|
TEST_F(OptionalOwnershipPointerTest, DestructsWhenItHasOwnership_UniqueRef) {
|
|
|
|
{
|
|
|
|
optional_ownership_ptr<TestObject> ptr = WithOwnership(cpputils::nullcheck(unique_ptr<TestObject>(obj.get())).value());
|
|
|
|
EXPECT_FALSE(obj.isDestructed());
|
|
|
|
UNUSED(ptr);
|
|
|
|
}
|
|
|
|
EXPECT_TRUE(obj.isDestructed());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-16 21:31:07 +01:00
|
|
|
TEST_F(OptionalOwnershipPointerTest, DestructsWhenItHasOwnershipAfterAssignment) {
|
|
|
|
{
|
2015-02-16 21:36:00 +01:00
|
|
|
optional_ownership_ptr<TestObject> ptr = WithoutOwnership(obj.get());
|
|
|
|
ptr = WithOwnership(unique_ptr<TestObject>(obj2.get()));
|
2015-02-16 21:31:07 +01:00
|
|
|
}
|
|
|
|
EXPECT_FALSE(obj.isDestructed());
|
|
|
|
EXPECT_TRUE(obj2.isDestructed());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(OptionalOwnershipPointerTest, DoesntDestructWhenItDoesntHaveOwnership) {
|
|
|
|
{
|
2015-02-16 21:36:00 +01:00
|
|
|
optional_ownership_ptr<TestObject> ptr = WithoutOwnership(obj.get());
|
2015-02-16 21:31:07 +01:00
|
|
|
UNUSED(ptr);
|
|
|
|
}
|
|
|
|
EXPECT_FALSE(obj.isDestructed());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(OptionalOwnershipPointerTest, DoesntDestructWhenItDoesntHaveOwnershipAfterAssignment) {
|
|
|
|
{
|
2015-02-16 21:36:00 +01:00
|
|
|
optional_ownership_ptr<TestObject> ptr = WithOwnership(unique_ptr<TestObject>(obj.get()));
|
|
|
|
ptr = WithoutOwnership(obj2.get());
|
2015-02-16 21:31:07 +01:00
|
|
|
EXPECT_TRUE(obj.isDestructed());
|
|
|
|
}
|
|
|
|
EXPECT_FALSE(obj2.isDestructed());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(OptionalOwnershipPointerTest, DestructsOnReassignmentWithNull) {
|
2015-02-16 21:36:00 +01:00
|
|
|
optional_ownership_ptr<TestObject> ptr = WithOwnership(unique_ptr<TestObject>(obj.get()));
|
|
|
|
ptr = null<TestObject>();
|
2015-02-16 21:31:07 +01:00
|
|
|
EXPECT_TRUE(obj.isDestructed());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(OptionalOwnershipPointerTest, DoesntCrashWhenDestructingNullptr1) {
|
2015-02-16 21:36:00 +01:00
|
|
|
optional_ownership_ptr<TestObject> ptr = null<TestObject>();
|
2015-02-16 21:31:07 +01:00
|
|
|
UNUSED(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(OptionalOwnershipPointerTest, DoesntCrashWhenDestructingNullptrWithoutOwnership) {
|
2015-02-16 21:36:00 +01:00
|
|
|
optional_ownership_ptr<TestObject> ptr = WithoutOwnership((TestObject*)nullptr);
|
2015-02-16 21:31:07 +01:00
|
|
|
UNUSED(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(OptionalOwnershipPointerTest, DoesntCrashWhenDestructingNullptrWithOwnership) {
|
2015-02-16 21:36:00 +01:00
|
|
|
optional_ownership_ptr<TestObject> ptr = WithOwnership(unique_ptr<TestObject>(nullptr));
|
2015-02-16 21:31:07 +01:00
|
|
|
UNUSED(ptr);
|
|
|
|
}
|