forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CowPtrTest.h
166 lines (133 loc) · 4.37 KB
/
CowPtrTest.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#pragma once
#include "MantidKernel/cow_ptr.h"
#include <cxxtest/TestSuite.h>
#include <memory>
using namespace Mantid::Kernel;
namespace {
struct MyType {
public:
MyType() : value(0) {}
MyType(int val) : value(val) {}
int value;
};
} // namespace
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 = std::make_unique<MyType>(2);
cow_ptr<MyType> cow{resource.release()};
TSM_ASSERT_EQUALS("COW does not hold the expected value", 2, cow->value);
}
void testConstructorByTemporarySptr() {
int value = 3;
auto resource = std::make_shared<MyType>(value);
cow_ptr<MyType> cow{std::move(resource)};
TS_ASSERT_EQUALS(cow->value, value);
TSM_ASSERT("Resource should have been moved", resource.get() == nullptr);
}
void testConstructorByNamedSptr() {
int value = 3;
auto resource = std::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)
}
void test_move_constructor() {
auto resource = std::make_shared<int>(42);
cow_ptr<int> source{resource};
cow_ptr<int> clone(std::move(source));
TS_ASSERT(!source);
TS_ASSERT_EQUALS(clone.get(), resource.get());
}
void test_move_assignment() {
auto resource = std::make_shared<int>(42);
cow_ptr<int> source{resource};
cow_ptr<int> clone;
clone = std::move(source);
TS_ASSERT(!source);
TS_ASSERT_EQUALS(clone.get(), resource.get());
}
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);
std::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 = std::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 = std::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 = std::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);
}
void test_access() {
int value = 3;
cow_ptr<MyType> original{std::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);
copyResource.value = 4;
TSM_ASSERT_DIFFERS("Value should now have changed", original->value, copyResource.value);
}
void test_equals_not_equals() {
cow_ptr<MyType> cow{nullptr};
TS_ASSERT(cow == cow);
const auto cow2 = std::make_shared<MyType>(42);
TS_ASSERT(cow2 == cow2);
TS_ASSERT(cow != cow2);
cow = std::make_shared<MyType>(42);
TS_ASSERT(cow == cow);
TS_ASSERT(cow != cow2);
cow = cow2;
TS_ASSERT(cow == cow2);
}
};