1 | #include <utility> |
---|
2 | #include <gtest/gtest.h> |
---|
3 | #include <gmock/gmock.h> |
---|
4 | #include "util/SharedPtr.h" |
---|
5 | |
---|
6 | namespace orxonox |
---|
7 | { |
---|
8 | namespace |
---|
9 | { |
---|
10 | class TestClass |
---|
11 | { |
---|
12 | public: |
---|
13 | TestClass() : value_(0) {} |
---|
14 | virtual ~TestClass() {} |
---|
15 | |
---|
16 | void setValue(int value) { this->value_ = value; } |
---|
17 | int getValue() const { return this->value_; } |
---|
18 | |
---|
19 | private: |
---|
20 | int value_; |
---|
21 | }; |
---|
22 | |
---|
23 | class TestChildClass : public TestClass |
---|
24 | { |
---|
25 | public: |
---|
26 | TestChildClass() {} |
---|
27 | virtual ~TestChildClass() {} |
---|
28 | }; |
---|
29 | |
---|
30 | class TestChildClassMock : public TestChildClass |
---|
31 | { |
---|
32 | public: |
---|
33 | TestChildClassMock() {} |
---|
34 | ~TestChildClassMock() { objectDestroyed(); } |
---|
35 | |
---|
36 | MOCK_METHOD0(objectDestroyed, void()); |
---|
37 | }; |
---|
38 | } |
---|
39 | |
---|
40 | TEST(SharedPtr, ConstructorDefault) |
---|
41 | { |
---|
42 | SharedPtr<TestClass> test; |
---|
43 | EXPECT_EQ(nullptr, test.get()); |
---|
44 | } |
---|
45 | |
---|
46 | TEST(SharedPtr, ConstructorPointer) |
---|
47 | { |
---|
48 | TestClass* pointer = new TestClass(); |
---|
49 | |
---|
50 | SharedPtr<TestClass> test = pointer; |
---|
51 | EXPECT_EQ(pointer, test.get()); |
---|
52 | EXPECT_EQ(pointer, &(*test)); |
---|
53 | } |
---|
54 | |
---|
55 | TEST(SharedPtr, ConstructorChildPointer) |
---|
56 | { |
---|
57 | TestChildClass* pointer = new TestChildClass(); |
---|
58 | |
---|
59 | SharedPtr<TestClass> test = pointer; |
---|
60 | EXPECT_EQ(pointer, test.get()); |
---|
61 | } |
---|
62 | |
---|
63 | TEST(SharedPtr, ConstructorOther) |
---|
64 | { |
---|
65 | TestClass* pointer = new TestClass(); |
---|
66 | |
---|
67 | SharedPtr<TestClass> other = pointer; |
---|
68 | EXPECT_EQ(pointer, other.get()); |
---|
69 | |
---|
70 | SharedPtr<TestClass> test = other; |
---|
71 | EXPECT_EQ(pointer, test.get()); |
---|
72 | } |
---|
73 | |
---|
74 | TEST(SharedPtr, ConstructorOtherChild) |
---|
75 | { |
---|
76 | TestChildClass* pointer = new TestChildClass(); |
---|
77 | |
---|
78 | SharedPtr<TestChildClass> other = pointer; |
---|
79 | EXPECT_EQ(pointer, other.get()); |
---|
80 | |
---|
81 | SharedPtr<TestClass> test = other; |
---|
82 | EXPECT_EQ(pointer, test.get()); |
---|
83 | } |
---|
84 | |
---|
85 | TEST(SharedPtr, Assign) |
---|
86 | { |
---|
87 | TestClass* pointer = new TestClass(); |
---|
88 | |
---|
89 | SharedPtr<TestClass> other = pointer; |
---|
90 | EXPECT_EQ(pointer, other.get()); |
---|
91 | |
---|
92 | SharedPtr<TestClass> test; |
---|
93 | EXPECT_EQ(nullptr, test.get()); |
---|
94 | |
---|
95 | test = other; |
---|
96 | EXPECT_EQ(pointer, test.get()); |
---|
97 | } |
---|
98 | |
---|
99 | TEST(SharedPtr, AssignChild) |
---|
100 | { |
---|
101 | TestChildClass* pointer = new TestChildClass(); |
---|
102 | |
---|
103 | SharedPtr<TestChildClass> other = pointer; |
---|
104 | EXPECT_EQ(pointer, other.get()); |
---|
105 | |
---|
106 | SharedPtr<TestClass> test; |
---|
107 | EXPECT_EQ(nullptr, test.get()); |
---|
108 | |
---|
109 | test = other; |
---|
110 | EXPECT_EQ(pointer, test.get()); |
---|
111 | } |
---|
112 | |
---|
113 | TEST(SharedPtr, Cast) |
---|
114 | { |
---|
115 | TestChildClass* pointer = new TestChildClass(); |
---|
116 | |
---|
117 | SharedPtr<TestChildClass> other = pointer; |
---|
118 | EXPECT_EQ(pointer, other.get()); |
---|
119 | |
---|
120 | SharedPtr<TestClass> test = other.cast<TestClass>(); |
---|
121 | EXPECT_EQ(pointer, test.get()); |
---|
122 | } |
---|
123 | |
---|
124 | TEST(SharedPtr, Access) |
---|
125 | { |
---|
126 | TestClass* pointer = new TestClass(); |
---|
127 | |
---|
128 | SharedPtr<TestClass> test = pointer; |
---|
129 | EXPECT_EQ(pointer, test.get()); |
---|
130 | |
---|
131 | EXPECT_EQ(0, test->getValue()); |
---|
132 | test->setValue(5); |
---|
133 | EXPECT_EQ(5, test->getValue()); |
---|
134 | EXPECT_EQ(5, (*test).getValue()); |
---|
135 | } |
---|
136 | |
---|
137 | TEST(SharedPtr, Boolean) |
---|
138 | { |
---|
139 | SharedPtr<TestClass> test; |
---|
140 | EXPECT_EQ(nullptr, test.get()); |
---|
141 | EXPECT_FALSE(test); |
---|
142 | |
---|
143 | TestClass* pointer = new TestClass(); |
---|
144 | |
---|
145 | test = pointer; |
---|
146 | EXPECT_EQ(pointer, test.get()); |
---|
147 | EXPECT_TRUE(test); |
---|
148 | } |
---|
149 | |
---|
150 | TEST(SharedPtr, Swap) |
---|
151 | { |
---|
152 | TestClass* pointer1 = new TestClass(); |
---|
153 | TestClass* pointer2 = new TestClass(); |
---|
154 | |
---|
155 | SharedPtr<TestClass> test1 = pointer1; |
---|
156 | SharedPtr<TestClass> test2 = pointer2; |
---|
157 | |
---|
158 | EXPECT_EQ(pointer1, test1.get()); |
---|
159 | EXPECT_EQ(pointer2, test2.get()); |
---|
160 | |
---|
161 | test1.swap(test2); |
---|
162 | |
---|
163 | EXPECT_EQ(pointer2, test1.get()); |
---|
164 | EXPECT_EQ(pointer1, test2.get()); |
---|
165 | } |
---|
166 | |
---|
167 | TEST(SharedPtr, ObjectDestroyedOnePointer) |
---|
168 | { |
---|
169 | TestChildClassMock* pointer = new TestChildClassMock(); |
---|
170 | SharedPtr<TestClass> test = pointer; |
---|
171 | |
---|
172 | EXPECT_CALL(*pointer, objectDestroyed()).Times(1); |
---|
173 | } |
---|
174 | |
---|
175 | TEST(SharedPtr, ObjectDestroyedManyPointers) |
---|
176 | { |
---|
177 | TestChildClassMock* pointer = new TestChildClassMock(); |
---|
178 | |
---|
179 | SharedPtr<TestClass> test = pointer; |
---|
180 | std::vector<SharedPtr<TestClass> > tests; |
---|
181 | for (size_t i = 0; i < 100; ++i) |
---|
182 | tests.push_back(SharedPtr<TestClass>(test)); |
---|
183 | |
---|
184 | EXPECT_CALL(*pointer, objectDestroyed()).Times(1); |
---|
185 | } |
---|
186 | |
---|
187 | TEST(SharedPtr, TestConstructors) |
---|
188 | { |
---|
189 | { |
---|
190 | TestChildClassMock* pointer = new TestChildClassMock(); |
---|
191 | |
---|
192 | // default |
---|
193 | SharedPtr<TestChildClass> sharedPtr1; |
---|
194 | EXPECT_EQ(nullptr, sharedPtr1.get()); |
---|
195 | |
---|
196 | // pointer |
---|
197 | SharedPtr<TestChildClass> sharedPtr2(pointer); |
---|
198 | EXPECT_EQ(pointer, sharedPtr2.get()); |
---|
199 | |
---|
200 | // copy |
---|
201 | SharedPtr<TestChildClass> sharedPtr3(sharedPtr2); |
---|
202 | EXPECT_EQ(pointer, sharedPtr3.get()); |
---|
203 | |
---|
204 | // move |
---|
205 | SharedPtr<TestChildClass> sharedPtr4(std::move(sharedPtr3)); |
---|
206 | EXPECT_EQ(pointer, sharedPtr4.get()); |
---|
207 | |
---|
208 | // other |
---|
209 | SharedPtr<TestClass> sharedPtr5(sharedPtr4); |
---|
210 | EXPECT_EQ(pointer, sharedPtr5.get()); |
---|
211 | |
---|
212 | EXPECT_CALL(*pointer, objectDestroyed()).Times(1); |
---|
213 | } |
---|
214 | } |
---|
215 | |
---|
216 | TEST(SharedPtr, TestConstructors2) |
---|
217 | { |
---|
218 | { |
---|
219 | TestChildClassMock* pointer = new TestChildClassMock(); |
---|
220 | |
---|
221 | // default |
---|
222 | SharedPtr<TestChildClass> sharedPtr1; |
---|
223 | EXPECT_EQ(nullptr, sharedPtr1.get()); |
---|
224 | |
---|
225 | // pointer |
---|
226 | SharedPtr<TestChildClass> sharedPtr2 = pointer; |
---|
227 | EXPECT_EQ(pointer, sharedPtr2.get()); |
---|
228 | |
---|
229 | // copy |
---|
230 | SharedPtr<TestChildClass> sharedPtr3 = sharedPtr2; |
---|
231 | EXPECT_EQ(pointer, sharedPtr3.get()); |
---|
232 | |
---|
233 | // move |
---|
234 | SharedPtr<TestChildClass> sharedPtr4 = std::move(sharedPtr3); |
---|
235 | EXPECT_EQ(pointer, sharedPtr4.get()); |
---|
236 | |
---|
237 | // other |
---|
238 | SharedPtr<TestClass> sharedPtr5 = sharedPtr4; |
---|
239 | EXPECT_EQ(pointer, sharedPtr5.get()); |
---|
240 | |
---|
241 | EXPECT_CALL(*pointer, objectDestroyed()).Times(1); |
---|
242 | } |
---|
243 | } |
---|
244 | |
---|
245 | TEST(SharedPtr, TestAssignments) |
---|
246 | { |
---|
247 | TestChildClassMock* pointer = new TestChildClassMock(); |
---|
248 | |
---|
249 | SharedPtr<TestChildClass> sharedPtr1(pointer); |
---|
250 | EXPECT_EQ(pointer, sharedPtr1.get()); |
---|
251 | |
---|
252 | // copy |
---|
253 | SharedPtr<TestChildClass> sharedPtr2; |
---|
254 | sharedPtr2 = sharedPtr1; |
---|
255 | EXPECT_EQ(pointer, sharedPtr2.get()); |
---|
256 | |
---|
257 | // move |
---|
258 | SharedPtr<TestChildClass> sharedPtr3; |
---|
259 | sharedPtr3 = std::move(sharedPtr2); |
---|
260 | EXPECT_EQ(pointer, sharedPtr3.get()); |
---|
261 | |
---|
262 | // other |
---|
263 | SharedPtr<TestClass> sharedPtr4; |
---|
264 | sharedPtr4 = sharedPtr3; |
---|
265 | EXPECT_EQ(pointer, sharedPtr4.get()); |
---|
266 | |
---|
267 | EXPECT_CALL(*pointer, objectDestroyed()).Times(1); |
---|
268 | } |
---|
269 | } |
---|