Newer
Older
#ifndef COW_PTR_TEST_H_
#define COW_PTR_TEST_H_
#include <cxxtest/TestSuite.h>
#include "MantidKernel/cow_ptr.h"
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
using namespace Mantid::Kernel;
namespace {
struct MyType {
public:
MyType() : value(0) {}
MyType(int val) : value(val) {}
int value;
};
}
class CowPtrTest : public CxxTest::TestSuite {
public:
void testDefaultConstruct() {
cow_ptr<MyType> cow{};
TSM_ASSERT_EQUALS("Should give us the default of the default constructed T",
0, cow->value);
}
void testConstructorNullptr() {
cow_ptr<MyType> cow{nullptr};
TS_ASSERT_EQUALS(cow.operator->(), nullptr);
}
void testConstructorByPtr() {
auto *resource = new MyType{2};
cow_ptr<MyType> cow{resource};
TSM_ASSERT_EQUALS("COW does not hold the expected value", 2, cow->value);
}
void testConstructorByTemporarySptr() {
int value = 3;
auto resource = boost::make_shared<MyType>(value);
TS_ASSERT_EQUALS(cow->value, value);
TSM_ASSERT("Resource should have been moved", resource.get() == nullptr);
}
void testConstructorByNamedSptr() {
int value = 3;
auto resource = boost::make_shared<MyType>(value);
cow_ptr<MyType> cow{resource};
TS_ASSERT_EQUALS(cow->value, value);
TSM_ASSERT("Resource should NOT have been moved",
resource.get() != nullptr);
TSM_ASSERT_EQUALS("Two shared_ptr objects in scope", resource.use_count(),
2)
}
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
void test_copy_assign_nullptr() {
cow_ptr<MyType> cow1{nullptr};
TS_ASSERT(!cow1);
auto cow2 = cow1;
TS_ASSERT(!cow2);
auto cow3(cow1);
TS_ASSERT(!cow3);
cow_ptr<MyType> cow4;
TS_ASSERT(cow4);
cow4 = cow1;
TS_ASSERT(!cow4);
boost::shared_ptr<MyType> shared;
TS_ASSERT(!shared);
cow4 = shared;
TS_ASSERT(!cow4);
MyType *resource = nullptr;
cow_ptr<MyType> cow5{resource};
TS_ASSERT(!cow5);
}
void test_get() {
auto resource = boost::make_shared<MyType>(42);
cow_ptr<MyType> cow(resource);
TS_ASSERT_DIFFERS(resource.get(), nullptr);
TS_ASSERT_EQUALS(cow.get(), resource.get());
}
void test_operator_bool() {
cow_ptr<MyType> cow1{nullptr};
TS_ASSERT(!cow1);
auto resource = boost::make_shared<MyType>(42);
cow_ptr<MyType> cow2{resource};
TS_ASSERT(cow2);
}
void test_use_count_and_unique() {
cow_ptr<MyType> cow{nullptr};
TS_ASSERT(!cow.unique());
TS_ASSERT_EQUALS(cow.use_count(), 0);
cow = boost::make_shared<MyType>(42);
TS_ASSERT(cow.unique());
TS_ASSERT_EQUALS(cow.use_count(), 1);
auto copy = cow;
TS_ASSERT(!cow.unique());
TS_ASSERT_EQUALS(cow.use_count(), 2);
// acessing copy makes a copy of data, so ref count in cow drops
copy.access();
TS_ASSERT(cow.unique());
TS_ASSERT_EQUALS(cow.use_count(), 1);
}
cow_ptr<MyType> original{boost::make_shared<MyType>(value)};
auto copy = original; // Now internal shared_ptr count should be at 2
MyType ©Resource = copy.access(); // The resource should now be copied.
TSM_ASSERT_EQUALS("Value should NOT have changed", original->value,
copyResource.value = 4;
TSM_ASSERT_DIFFERS("Value should now have changed", original->value,
copyResource.value);
};
#endif /*COW_PTR_TEST_H_*/