Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/archive/PLehmann_FS16/test/core/object/WeakPtrTest.cc @ 12302

Last change on this file since 12302 was 11097, checked in by landauf, 9 years ago

fixed build on tardis.

  • Property svn:eol-style set to native
File size: 6.2 KB
Line 
1#include <utility>
2#include <gtest/gtest.h>
3#include <gmock/gmock.h>
4#include "core/object/WeakPtr.h"
5
6namespace orxonox
7{
8    namespace
9    {
10        class DestroyableTest : public Destroyable
11        {
12        };
13
14        class Callback
15        {
16            public:
17                virtual ~Callback() {}
18                MOCK_METHOD0(callback, void());
19        };
20    }
21
22    TEST(WeakPtrTest, CanReferenceObject)
23    {
24        DestroyableTest* test = new DestroyableTest();
25        WeakPtr<DestroyableTest> weakPtr = test;
26        EXPECT_EQ(test, weakPtr.get());
27        EXPECT_EQ(test, &(*weakPtr));
28        EXPECT_EQ(static_cast<Destroyable*>(test), weakPtr.getBase());
29        test->destroy();
30    }
31
32    TEST(WeakPtrTest, DestroyRemovesReference)
33    {
34        DestroyableTest* test = new DestroyableTest();
35        WeakPtr<DestroyableTest> weakPtr = test;
36        EXPECT_EQ(test, weakPtr.get());
37        test->destroy();
38        EXPECT_EQ(nullptr, weakPtr.get());
39    }
40
41    TEST(WeakPtrTest, CanHaveMultiplePointers)
42    {
43        DestroyableTest* test = new DestroyableTest();
44        WeakPtr<DestroyableTest> weakPtr1 = test;
45        WeakPtr<DestroyableTest> weakPtr2 = test;
46        WeakPtr<DestroyableTest> weakPtr3 = test;
47        EXPECT_EQ(test, weakPtr1.get());
48        EXPECT_EQ(test, weakPtr2.get());
49        EXPECT_EQ(test, weakPtr3.get());
50        test->destroy();
51        EXPECT_EQ(nullptr, weakPtr1.get());
52        EXPECT_EQ(nullptr, weakPtr2.get());
53        EXPECT_EQ(nullptr, weakPtr3.get());
54    }
55
56    TEST(WeakPtrTest, DestroyCallsCallback)
57    {
58        DestroyableTest* test = new DestroyableTest();
59        WeakPtr<DestroyableTest> weakPtr = test;
60        Callback callback;
61        weakPtr.setCallback(createFunctor(&Callback::callback, &callback));
62        EXPECT_CALL(callback, callback());
63        test->destroy();
64    }
65
66    void isNull(const WeakPtr<DestroyableTest> weakPtr)
67    {
68        EXPECT_TRUE(weakPtr == nullptr);
69        EXPECT_TRUE(weakPtr == nullptr);
70        EXPECT_TRUE(!weakPtr);
71        EXPECT_FALSE(weakPtr != nullptr);
72        EXPECT_FALSE(weakPtr != nullptr);
73        EXPECT_FALSE(weakPtr);
74    }
75
76    TEST(WeakPtrTest, IsNull)
77    {
78        {
79            WeakPtr<DestroyableTest> weakPtr;
80            isNull(weakPtr);
81        }
82        {
83            WeakPtr<DestroyableTest> weakPtr = nullptr;
84            isNull(weakPtr);
85        }
86        {
87            WeakPtr<DestroyableTest> weakPtr;
88            weakPtr = nullptr;
89            isNull(weakPtr);
90        }
91        {
92            WeakPtr<DestroyableTest> weakPtr = nullptr;
93            isNull(weakPtr);
94        }
95        {
96            WeakPtr<DestroyableTest> weakPtr;
97            weakPtr = nullptr;
98            isNull(weakPtr);
99        }
100    }
101
102    TEST(WeakPtrTest, IsNotNull)
103    {
104        DestroyableTest* test = new DestroyableTest();
105        WeakPtr<DestroyableTest> weakPtr = test;
106        EXPECT_FALSE(weakPtr == nullptr);
107        EXPECT_FALSE(weakPtr == nullptr);
108        EXPECT_FALSE(!weakPtr);
109        EXPECT_TRUE(weakPtr != nullptr);
110        EXPECT_TRUE(weakPtr != nullptr);
111        EXPECT_TRUE(weakPtr);
112        test->destroy();
113    }
114
115    TEST(WeakPtrTest, TestConstructors)
116    {
117        DestroyableTest* test = new DestroyableTest();
118
119        // default
120        WeakPtr<DestroyableTest> weakPtr1;
121        EXPECT_EQ(nullptr, weakPtr1.get());
122
123        // pointer
124        WeakPtr<DestroyableTest> weakPtr2a = test;
125        WeakPtr<DestroyableTest> weakPtr2b(test);
126        EXPECT_EQ(test, weakPtr2a.get());
127        EXPECT_EQ(test, weakPtr2b.get());
128
129        // copy
130        WeakPtr<DestroyableTest> weakPtr3a = weakPtr2a;
131        WeakPtr<DestroyableTest> weakPtr3b(weakPtr2b);
132        EXPECT_EQ(test, weakPtr3a.get());
133        EXPECT_EQ(test, weakPtr3b.get());
134
135        // move
136        WeakPtr<DestroyableTest> weakPtr4a = std::move(weakPtr3a);
137        WeakPtr<DestroyableTest> weakPtr4b(std::move(weakPtr3b));
138        EXPECT_EQ(test, weakPtr4a.get());
139        EXPECT_EQ(test, weakPtr4b.get());
140
141        // other
142        WeakPtr<Destroyable> weakPtr5a = weakPtr4a;
143        WeakPtr<Destroyable> weakPtr5b(weakPtr4b);
144        EXPECT_EQ(test, weakPtr5a.get());
145        EXPECT_EQ(test, weakPtr5b.get());
146
147        test->destroy();
148    }
149
150    TEST(WeakPtrTest, TestAssignments)
151    {
152        DestroyableTest* test = new DestroyableTest();
153
154        // pointer
155        WeakPtr<DestroyableTest> weakPtr1;
156        weakPtr1 = test;
157        EXPECT_EQ(test, weakPtr1.get());
158
159        // copy
160        WeakPtr<DestroyableTest> weakPtr2;
161        weakPtr2 = weakPtr1;
162        EXPECT_EQ(test, weakPtr2.get());
163
164        // move
165        WeakPtr<DestroyableTest> weakPtr3;
166        weakPtr3 = std::move(weakPtr2);
167        EXPECT_EQ(test, weakPtr3.get());
168
169        // other
170        WeakPtr<Destroyable> weakPtr4;
171        weakPtr4 = weakPtr3;
172        EXPECT_EQ(test, weakPtr4.get());
173
174        test->destroy();
175    }
176
177    TEST(WeakPtrTest, TestWeakPtrInVector)
178    {
179        DestroyableTest* test = new DestroyableTest();
180
181        std::vector<WeakPtr<DestroyableTest>> vector;
182        vector.push_back(test);
183        vector.push_back(test);
184        vector.push_back(test);
185
186        ASSERT_EQ(3u, vector.size());
187        EXPECT_EQ(test, vector[0].get());
188        EXPECT_EQ(test, vector[1].get());
189        EXPECT_EQ(test, vector[2].get());
190
191        test->destroy();
192
193        EXPECT_EQ(nullptr, vector[0].get());
194        EXPECT_EQ(nullptr, vector[1].get());
195        EXPECT_EQ(nullptr, vector[2].get());
196    }
197
198    TEST(WeakPtrTest, TestReset)
199    {
200        DestroyableTest* test = new DestroyableTest();
201        WeakPtr<DestroyableTest> weakPtr(test);
202        EXPECT_EQ(test, weakPtr.get());
203        weakPtr.reset();
204        EXPECT_EQ(nullptr, weakPtr.get());
205
206        test->destroy();
207    }
208
209    TEST(WeakPtrTest, TestSwap)
210    {
211        DestroyableTest* test1 = new DestroyableTest();
212        DestroyableTest* test2 = new DestroyableTest();
213
214        WeakPtr<DestroyableTest> weakPtr1(test1);
215        WeakPtr<DestroyableTest> weakPtr2(test2);
216        EXPECT_EQ(test1, weakPtr1.get());
217        EXPECT_EQ(test2, weakPtr2.get());
218
219        weakPtr1.swap(weakPtr2);
220
221        EXPECT_EQ(test2, weakPtr1.get());
222        EXPECT_EQ(test1, weakPtr2.get());
223
224        test1->destroy();
225        test2->destroy();
226    }
227}
Note: See TracBrowser for help on using the repository browser.