Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/test/util/SharedPtrTest.cc @ 11436

Last change on this file since 11436 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
RevLine 
[11071]1#include <memory>
2#include <utility>
[9114]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
[11071]30        class TestChildClassMock : public TestChildClass
[9114]31        {
32            public:
[11071]33                TestChildClassMock() {}
34                ~TestChildClassMock() { objectDestroyed(); }
[9114]35
36                MOCK_METHOD0(objectDestroyed, void());
37        };
38    }
39
40    TEST(SharedPtr, ConstructorDefault)
41    {
[11071]42        std::shared_ptr<TestClass> test;
43        EXPECT_EQ(nullptr, test.get());
[9114]44    }
45
46    TEST(SharedPtr, ConstructorPointer)
47    {
48        TestClass* pointer = new TestClass();
49
[11071]50        std::shared_ptr<TestClass> test(pointer);
[9114]51        EXPECT_EQ(pointer, test.get());
[11071]52        EXPECT_EQ(pointer, &(*test));
[9114]53    }
54
55    TEST(SharedPtr, ConstructorChildPointer)
56    {
57        TestChildClass* pointer = new TestChildClass();
58
[11071]59        std::shared_ptr<TestClass> test(pointer);
[9114]60        EXPECT_EQ(pointer, test.get());
61    }
62
63    TEST(SharedPtr, ConstructorOther)
64    {
65        TestClass* pointer = new TestClass();
66
[11071]67        std::shared_ptr<TestClass> other(pointer);
[9114]68        EXPECT_EQ(pointer, other.get());
69
[11071]70        std::shared_ptr<TestClass> test = other;
[9114]71        EXPECT_EQ(pointer, test.get());
72    }
73
74    TEST(SharedPtr, ConstructorOtherChild)
75    {
76        TestChildClass* pointer = new TestChildClass();
77
[11071]78        std::shared_ptr<TestChildClass> other(pointer);
[9114]79        EXPECT_EQ(pointer, other.get());
80
[11071]81        std::shared_ptr<TestClass> test = other;
[9114]82        EXPECT_EQ(pointer, test.get());
83    }
84
85    TEST(SharedPtr, Assign)
86    {
87        TestClass* pointer = new TestClass();
88
[11071]89        std::shared_ptr<TestClass> other(pointer);
[9114]90        EXPECT_EQ(pointer, other.get());
91
[11071]92        std::shared_ptr<TestClass> test;
93        EXPECT_EQ(nullptr, test.get());
[9114]94
95        test = other;
96        EXPECT_EQ(pointer, test.get());
97    }
98
99    TEST(SharedPtr, AssignChild)
100    {
101        TestChildClass* pointer = new TestChildClass();
102
[11071]103        std::shared_ptr<TestChildClass> other(pointer);
[9114]104        EXPECT_EQ(pointer, other.get());
105
[11071]106        std::shared_ptr<TestClass> test;
107        EXPECT_EQ(nullptr, test.get());
[9114]108
109        test = other;
110        EXPECT_EQ(pointer, test.get());
111    }
112
113    TEST(SharedPtr, Cast)
114    {
115        TestChildClass* pointer = new TestChildClass();
116
[11071]117        std::shared_ptr<TestChildClass> other(pointer);
[9114]118        EXPECT_EQ(pointer, other.get());
119
[11071]120        std::shared_ptr<TestClass> test = std::static_pointer_cast<TestClass>(other);
[9114]121        EXPECT_EQ(pointer, test.get());
122    }
123
124    TEST(SharedPtr, Access)
125    {
126        TestClass* pointer = new TestClass();
127
[11071]128        std::shared_ptr<TestClass> test(pointer);
[9114]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    {
[11071]139        std::shared_ptr<TestClass> test;
140        EXPECT_EQ(nullptr, test.get());
[9114]141        EXPECT_FALSE(test);
142
143        TestClass* pointer = new TestClass();
144
[11071]145        test.reset(pointer);
[9114]146        EXPECT_EQ(pointer, test.get());
[11071]147        EXPECT_TRUE(static_cast<bool>(test));
[9114]148    }
149
150    TEST(SharedPtr, Swap)
151    {
152        TestClass* pointer1 = new TestClass();
153        TestClass* pointer2 = new TestClass();
154
[11071]155        std::shared_ptr<TestClass> test1(pointer1);
156        std::shared_ptr<TestClass> test2(pointer2);
[9114]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    {
[11071]169        TestChildClassMock* pointer = new TestChildClassMock();
170        std::shared_ptr<TestClass> test(pointer);
[9114]171
172        EXPECT_CALL(*pointer, objectDestroyed()).Times(1);
173    }
174
175    TEST(SharedPtr, ObjectDestroyedManyPointers)
176    {
[11071]177        TestChildClassMock* pointer = new TestChildClassMock();
[9114]178
[11071]179        std::shared_ptr<TestClass> test(pointer);
180        std::vector<std::shared_ptr<TestClass>> tests;
[9114]181        for (size_t i = 0; i < 100; ++i)
[11071]182            tests.push_back(std::shared_ptr<TestClass>(test));
[9114]183
184        EXPECT_CALL(*pointer, objectDestroyed()).Times(1);
185    }
[11071]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    }
[9114]269}
Note: See TracBrowser for help on using the repository browser.