Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core7/test/core/object/StrongPtrTest.cc @ 10555

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

renamed SmartPtr to StrongPtr (now we have weak and strong pointers)

  • Property svn:eol-style set to native
File size: 3.9 KB
Line 
1#include <gtest/gtest.h>
2#include "core/object/StrongPtr.h"
3
4namespace 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(StrongPtrTest, CanReferenceObject)
20    {
21        bool bla;
22        DestroyableTest* test = new DestroyableTest(bla);
23        StrongPtr<DestroyableTest> strongPtr = test;
24        EXPECT_EQ(test, strongPtr.get());
25        test->destroy();
26    }
27
28    TEST(StrongPtrTest, IncreasesReferenceCount)
29    {
30        bool bla;
31        DestroyableTest* test = new DestroyableTest(bla);
32        EXPECT_EQ(0u, test->getReferenceCount());
33        {
34            StrongPtr<DestroyableTest> strongPtr = test;
35            EXPECT_EQ(1u, test->getReferenceCount());
36        }
37        EXPECT_EQ(0u, test->getReferenceCount());
38        test->destroy();
39    }
40
41    TEST(StrongPtrTest, 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(StrongPtrTest, PreventsDestruction)
51    {
52        bool destroyed = false;
53        DestroyableTest* test = new DestroyableTest(destroyed);
54        EXPECT_FALSE(destroyed);
55        StrongPtr<DestroyableTest> strongPtr = test;
56        test->destroy();
57        EXPECT_FALSE(destroyed);
58    }
59
60    TEST(StrongPtrTest, DestroysIfStrongPtrRemoved)
61    {
62        bool destroyed = false;
63        DestroyableTest* test = new DestroyableTest(destroyed);
64        EXPECT_FALSE(destroyed);
65        {
66            StrongPtr<DestroyableTest> strongPtr = test;
67            test->destroy();
68            EXPECT_FALSE(destroyed);
69        }
70        EXPECT_TRUE(destroyed);
71    }
72
73    TEST(StrongPtrTest, DestroysIfAllStrongPtrsRemoved)
74    {
75        bool destroyed = false;
76        DestroyableTest* test = new DestroyableTest(destroyed);
77        EXPECT_FALSE(destroyed);
78        {
79            StrongPtr<DestroyableTest> strongPtr1 = test;
80            {
81                StrongPtr<DestroyableTest> strongPtr2 = test;
82                {
83                    StrongPtr<DestroyableTest> strongPtr3 = 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 StrongPtr<DestroyableTest> strongPtr)
95    {
96        EXPECT_TRUE(strongPtr == NULL);
97        EXPECT_TRUE(strongPtr == 0);
98        EXPECT_TRUE(!strongPtr);
99        EXPECT_FALSE(strongPtr != NULL);
100        EXPECT_FALSE(strongPtr != 0);
101        EXPECT_FALSE(strongPtr);
102    }
103
104    TEST(StrongPtrTest, IsNull)
105    {
106        {
107            StrongPtr<DestroyableTest> strongPtr;
108            isNull(strongPtr);
109        }
110        {
111            StrongPtr<DestroyableTest> strongPtr = NULL;
112            isNull(strongPtr);
113        }
114        {
115            StrongPtr<DestroyableTest> strongPtr;
116            strongPtr = NULL;
117            isNull(strongPtr);
118        }
119        {
120            StrongPtr<DestroyableTest> strongPtr = 0;
121            isNull(strongPtr);
122        }
123        {
124            StrongPtr<DestroyableTest> strongPtr;
125            strongPtr = 0;
126            isNull(strongPtr);
127        }
128    }
129
130    TEST(StrongPtrTest, IsNotNull)
131    {
132        bool destroyed = false;
133        DestroyableTest* test = new DestroyableTest(destroyed);
134        StrongPtr<DestroyableTest> strongPtr = test;
135        EXPECT_FALSE(strongPtr == NULL);
136        EXPECT_FALSE(strongPtr == 0);
137        EXPECT_FALSE(!strongPtr);
138        EXPECT_TRUE(strongPtr != NULL);
139        EXPECT_TRUE(strongPtr != 0);
140        EXPECT_TRUE(strongPtr);
141        test->destroy();
142    }
143}
Note: See TracBrowser for help on using the repository browser.