1 | #include <gtest/gtest.h> |
---|
2 | #include "core/object/SmartPtr.h" |
---|
3 | |
---|
4 | namespace orxonox |
---|
5 | { |
---|
6 | namespace |
---|
7 | { |
---|
8 | class DestroyableTest : public Destroyable |
---|
9 | { |
---|
10 | public: |
---|
11 | DestroyableTest(bool& destroyed) : destroyed_(destroyed) { destroyed_ = false; } |
---|
12 | virtual ~DestroyableTest() { destroyed_ = true; } |
---|
13 | |
---|
14 | private: |
---|
15 | bool& destroyed_; |
---|
16 | }; |
---|
17 | } |
---|
18 | |
---|
19 | TEST(SmartPtrTest, CanReferenceObject) |
---|
20 | { |
---|
21 | bool bla; |
---|
22 | DestroyableTest* test = new DestroyableTest(bla); |
---|
23 | SmartPtr<DestroyableTest> smartPtr = test; |
---|
24 | EXPECT_EQ(test, smartPtr.get()); |
---|
25 | test->destroy(); |
---|
26 | } |
---|
27 | |
---|
28 | TEST(SmartPtrTest, IncreasesReferenceCount) |
---|
29 | { |
---|
30 | bool bla; |
---|
31 | DestroyableTest* test = new DestroyableTest(bla); |
---|
32 | EXPECT_EQ(0u, test->getReferenceCount()); |
---|
33 | { |
---|
34 | SmartPtr<DestroyableTest> smartPtr = test; |
---|
35 | EXPECT_EQ(1u, test->getReferenceCount()); |
---|
36 | } |
---|
37 | EXPECT_EQ(0u, test->getReferenceCount()); |
---|
38 | test->destroy(); |
---|
39 | } |
---|
40 | |
---|
41 | TEST(SmartPtrTest, DestroyDeletesInstance) |
---|
42 | { |
---|
43 | bool destroyed = false; |
---|
44 | DestroyableTest* test = new DestroyableTest(destroyed); |
---|
45 | EXPECT_FALSE(destroyed); |
---|
46 | test->destroy(); |
---|
47 | EXPECT_TRUE(destroyed); |
---|
48 | } |
---|
49 | |
---|
50 | TEST(SmartPtrTest, PreventsDestruction) |
---|
51 | { |
---|
52 | bool destroyed = false; |
---|
53 | DestroyableTest* test = new DestroyableTest(destroyed); |
---|
54 | EXPECT_FALSE(destroyed); |
---|
55 | SmartPtr<DestroyableTest> smartPtr = test; |
---|
56 | test->destroy(); |
---|
57 | EXPECT_FALSE(destroyed); |
---|
58 | } |
---|
59 | |
---|
60 | TEST(SmartPtrTest, DestroysIfSmartPtrRemoved) |
---|
61 | { |
---|
62 | bool destroyed = false; |
---|
63 | DestroyableTest* test = new DestroyableTest(destroyed); |
---|
64 | EXPECT_FALSE(destroyed); |
---|
65 | { |
---|
66 | SmartPtr<DestroyableTest> smartPtr = test; |
---|
67 | test->destroy(); |
---|
68 | EXPECT_FALSE(destroyed); |
---|
69 | } |
---|
70 | EXPECT_TRUE(destroyed); |
---|
71 | } |
---|
72 | |
---|
73 | TEST(SmartPtrTest, DestroysIfAllSmartPtrsRemoved) |
---|
74 | { |
---|
75 | bool destroyed = false; |
---|
76 | DestroyableTest* test = new DestroyableTest(destroyed); |
---|
77 | EXPECT_FALSE(destroyed); |
---|
78 | { |
---|
79 | SmartPtr<DestroyableTest> smartPtr1 = test; |
---|
80 | { |
---|
81 | SmartPtr<DestroyableTest> smartPtr2 = test; |
---|
82 | { |
---|
83 | SmartPtr<DestroyableTest> smartPtr3 = test; |
---|
84 | test->destroy(); |
---|
85 | EXPECT_FALSE(destroyed); |
---|
86 | } |
---|
87 | EXPECT_FALSE(destroyed); |
---|
88 | } |
---|
89 | EXPECT_FALSE(destroyed); |
---|
90 | } |
---|
91 | EXPECT_TRUE(destroyed); |
---|
92 | } |
---|
93 | |
---|
94 | void isNull(const SmartPtr<DestroyableTest> smartPtr) |
---|
95 | { |
---|
96 | EXPECT_TRUE(smartPtr == NULL); |
---|
97 | EXPECT_TRUE(smartPtr == 0); |
---|
98 | EXPECT_TRUE(!smartPtr); |
---|
99 | EXPECT_FALSE(smartPtr != NULL); |
---|
100 | EXPECT_FALSE(smartPtr != 0); |
---|
101 | EXPECT_FALSE(smartPtr); |
---|
102 | } |
---|
103 | |
---|
104 | TEST(SmartPtrTest, IsNull) |
---|
105 | { |
---|
106 | { |
---|
107 | SmartPtr<DestroyableTest> smartPtr; |
---|
108 | isNull(smartPtr); |
---|
109 | } |
---|
110 | { |
---|
111 | SmartPtr<DestroyableTest> smartPtr = NULL; |
---|
112 | isNull(smartPtr); |
---|
113 | } |
---|
114 | { |
---|
115 | SmartPtr<DestroyableTest> smartPtr; |
---|
116 | smartPtr = NULL; |
---|
117 | isNull(smartPtr); |
---|
118 | } |
---|
119 | { |
---|
120 | SmartPtr<DestroyableTest> smartPtr = 0; |
---|
121 | isNull(smartPtr); |
---|
122 | } |
---|
123 | { |
---|
124 | SmartPtr<DestroyableTest> smartPtr; |
---|
125 | smartPtr = 0; |
---|
126 | isNull(smartPtr); |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | TEST(SmartPtrTest, IsNotNull) |
---|
131 | { |
---|
132 | bool destroyed = false; |
---|
133 | DestroyableTest* test = new DestroyableTest(destroyed); |
---|
134 | SmartPtr<DestroyableTest> smartPtr = test; |
---|
135 | EXPECT_FALSE(smartPtr == NULL); |
---|
136 | EXPECT_FALSE(smartPtr == 0); |
---|
137 | EXPECT_FALSE(!smartPtr); |
---|
138 | EXPECT_TRUE(smartPtr != NULL); |
---|
139 | EXPECT_TRUE(smartPtr != 0); |
---|
140 | EXPECT_TRUE(smartPtr); |
---|
141 | test->destroy(); |
---|
142 | } |
---|
143 | } |
---|