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 | } |
---|