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
RevLine 
[9603]1#include <gtest/gtest.h>
[10555]2#include "core/object/StrongPtr.h"
[9603]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
[10555]19    TEST(StrongPtrTest, CanReferenceObject)
[9603]20    {
21        bool bla;
22        DestroyableTest* test = new DestroyableTest(bla);
[10555]23        StrongPtr<DestroyableTest> strongPtr = test;
24        EXPECT_EQ(test, strongPtr.get());
[9603]25        test->destroy();
26    }
27
[10555]28    TEST(StrongPtrTest, IncreasesReferenceCount)
[9603]29    {
30        bool bla;
31        DestroyableTest* test = new DestroyableTest(bla);
32        EXPECT_EQ(0u, test->getReferenceCount());
33        {
[10555]34            StrongPtr<DestroyableTest> strongPtr = test;
[9603]35            EXPECT_EQ(1u, test->getReferenceCount());
36        }
37        EXPECT_EQ(0u, test->getReferenceCount());
38        test->destroy();
39    }
40
[10555]41    TEST(StrongPtrTest, DestroyDeletesInstance)
[9603]42    {
43        bool destroyed = false;
44        DestroyableTest* test = new DestroyableTest(destroyed);
45        EXPECT_FALSE(destroyed);
46        test->destroy();
47        EXPECT_TRUE(destroyed);
48    }
49
[10555]50    TEST(StrongPtrTest, PreventsDestruction)
[9603]51    {
52        bool destroyed = false;
53        DestroyableTest* test = new DestroyableTest(destroyed);
54        EXPECT_FALSE(destroyed);
[10555]55        StrongPtr<DestroyableTest> strongPtr = test;
[9603]56        test->destroy();
57        EXPECT_FALSE(destroyed);
58    }
59
[10555]60    TEST(StrongPtrTest, DestroysIfStrongPtrRemoved)
[9603]61    {
62        bool destroyed = false;
63        DestroyableTest* test = new DestroyableTest(destroyed);
64        EXPECT_FALSE(destroyed);
65        {
[10555]66            StrongPtr<DestroyableTest> strongPtr = test;
[9603]67            test->destroy();
68            EXPECT_FALSE(destroyed);
69        }
70        EXPECT_TRUE(destroyed);
71    }
72
[10555]73    TEST(StrongPtrTest, DestroysIfAllStrongPtrsRemoved)
[9603]74    {
75        bool destroyed = false;
76        DestroyableTest* test = new DestroyableTest(destroyed);
77        EXPECT_FALSE(destroyed);
78        {
[10555]79            StrongPtr<DestroyableTest> strongPtr1 = test;
[9603]80            {
[10555]81                StrongPtr<DestroyableTest> strongPtr2 = test;
[9603]82                {
[10555]83                    StrongPtr<DestroyableTest> strongPtr3 = test;
[9603]84                    test->destroy();
85                    EXPECT_FALSE(destroyed);
86                }
87                EXPECT_FALSE(destroyed);
88            }
89            EXPECT_FALSE(destroyed);
90        }
91        EXPECT_TRUE(destroyed);
92    }
[10359]93
[10555]94    void isNull(const StrongPtr<DestroyableTest> strongPtr)
[10359]95    {
[10555]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);
[10359]102    }
103
[10555]104    TEST(StrongPtrTest, IsNull)
[10359]105    {
106        {
[10555]107            StrongPtr<DestroyableTest> strongPtr;
108            isNull(strongPtr);
[10359]109        }
110        {
[10555]111            StrongPtr<DestroyableTest> strongPtr = NULL;
112            isNull(strongPtr);
[10359]113        }
114        {
[10555]115            StrongPtr<DestroyableTest> strongPtr;
116            strongPtr = NULL;
117            isNull(strongPtr);
[10359]118        }
119        {
[10555]120            StrongPtr<DestroyableTest> strongPtr = 0;
121            isNull(strongPtr);
[10359]122        }
123        {
[10555]124            StrongPtr<DestroyableTest> strongPtr;
125            strongPtr = 0;
126            isNull(strongPtr);
[10359]127        }
128    }
129
[10555]130    TEST(StrongPtrTest, IsNotNull)
[10359]131    {
132        bool destroyed = false;
133        DestroyableTest* test = new DestroyableTest(destroyed);
[10555]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);
[10359]141        test->destroy();
142    }
[9603]143}
Note: See TracBrowser for help on using the repository browser.