Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/Boxhead_FS19/test/util/SharedPtrTest.cc @ 12384

Last change on this file since 12384 was 11071, checked in by landauf, 9 years ago

merged branch cpp11_v3 back to trunk

  • Property svn:eol-style set to native
File size: 7.0 KB
Line 
1#include <memory>
2#include <utility>
3#include <gtest/gtest.h>
4#include <gmock/gmock.h>
5
6namespace 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        std::shared_ptr<TestClass> test;
43        EXPECT_EQ(nullptr, test.get());
44    }
45
46    TEST(SharedPtr, ConstructorPointer)
47    {
48        TestClass* pointer = new TestClass();
49
50        std::shared_ptr<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        std::shared_ptr<TestClass> test(pointer);
60        EXPECT_EQ(pointer, test.get());
61    }
62
63    TEST(SharedPtr, ConstructorOther)
64    {
65        TestClass* pointer = new TestClass();
66
67        std::shared_ptr<TestClass> other(pointer);
68        EXPECT_EQ(pointer, other.get());
69
70        std::shared_ptr<TestClass> test = other;
71        EXPECT_EQ(pointer, test.get());
72    }
73
74    TEST(SharedPtr, ConstructorOtherChild)
75    {
76        TestChildClass* pointer = new TestChildClass();
77
78        std::shared_ptr<TestChildClass> other(pointer);
79        EXPECT_EQ(pointer, other.get());
80
81        std::shared_ptr<TestClass> test = other;
82        EXPECT_EQ(pointer, test.get());
83    }
84
85    TEST(SharedPtr, Assign)
86    {
87        TestClass* pointer = new TestClass();
88
89        std::shared_ptr<TestClass> other(pointer);
90        EXPECT_EQ(pointer, other.get());
91
92        std::shared_ptr<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        std::shared_ptr<TestChildClass> other(pointer);
104        EXPECT_EQ(pointer, other.get());
105
106        std::shared_ptr<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        std::shared_ptr<TestChildClass> other(pointer);
118        EXPECT_EQ(pointer, other.get());
119
120        std::shared_ptr<TestClass> test = std::static_pointer_cast<TestClass>(other);
121        EXPECT_EQ(pointer, test.get());
122    }
123
124    TEST(SharedPtr, Access)
125    {
126        TestClass* pointer = new TestClass();
127
128        std::shared_ptr<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        std::shared_ptr<TestClass> test;
140        EXPECT_EQ(nullptr, test.get());
141        EXPECT_FALSE(test);
142
143        TestClass* pointer = new TestClass();
144
145        test.reset(pointer);
146        EXPECT_EQ(pointer, test.get());
147        EXPECT_TRUE(static_cast<bool>(test));
148    }
149
150    TEST(SharedPtr, Swap)
151    {
152        TestClass* pointer1 = new TestClass();
153        TestClass* pointer2 = new TestClass();
154
155        std::shared_ptr<TestClass> test1(pointer1);
156        std::shared_ptr<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        std::shared_ptr<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        std::shared_ptr<TestClass> test(pointer);
180        std::vector<std::shared_ptr<TestClass>> tests;
181        for (size_t i = 0; i < 100; ++i)
182            tests.push_back(std::shared_ptr<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        std::shared_ptr<TestChildClass> sharedPtr1;
194        EXPECT_EQ(nullptr, sharedPtr1.get());
195
196        // pointer
197        std::shared_ptr<TestChildClass> sharedPtr2(pointer);
198        EXPECT_EQ(pointer, sharedPtr2.get());
199
200        // copy
201        std::shared_ptr<TestChildClass> sharedPtr3(sharedPtr2);
202        EXPECT_EQ(pointer, sharedPtr3.get());
203
204        // move
205        std::shared_ptr<TestChildClass> sharedPtr4(std::move(sharedPtr3));
206        EXPECT_EQ(pointer, sharedPtr4.get());
207
208        // other
209        std::shared_ptr<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        std::shared_ptr<TestChildClass> sharedPtr1;
223        EXPECT_EQ(nullptr, sharedPtr1.get());
224
225        // pointer
226        std::shared_ptr<TestChildClass> sharedPtr2(pointer);
227        EXPECT_EQ(pointer, sharedPtr2.get());
228
229        // copy
230        std::shared_ptr<TestChildClass> sharedPtr3 = sharedPtr2;
231        EXPECT_EQ(pointer, sharedPtr3.get());
232
233        // move
234        std::shared_ptr<TestChildClass> sharedPtr4 = std::move(sharedPtr3);
235        EXPECT_EQ(pointer, sharedPtr4.get());
236
237        // other
238        std::shared_ptr<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        std::shared_ptr<TestChildClass> sharedPtr1(pointer);
250        EXPECT_EQ(pointer, sharedPtr1.get());
251
252        // copy
253        std::shared_ptr<TestChildClass> sharedPtr2;
254        sharedPtr2 = sharedPtr1;
255        EXPECT_EQ(pointer, sharedPtr2.get());
256
257        // move
258        std::shared_ptr<TestChildClass> sharedPtr3;
259        sharedPtr3 = std::move(sharedPtr2);
260        EXPECT_EQ(pointer, sharedPtr3.get());
261
262        // other
263        std::shared_ptr<TestClass> sharedPtr4;
264        sharedPtr4 = sharedPtr3;
265        EXPECT_EQ(pointer, sharedPtr4.get());
266
267        EXPECT_CALL(*pointer, objectDestroyed()).Times(1);
268    }
269}
Note: See TracBrowser for help on using the repository browser.