Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 10744


Ignore:
Timestamp:
Nov 1, 2015, 9:03:05 PM (9 years ago)
Author:
landauf
Message:

added tests and fixed constructors of StrongPtr and WeakPtr

Location:
code/branches/cpp11_v2
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • code/branches/cpp11_v2/src/libraries/core/object/StrongPtr.h

    r10624 r10744  
    158158            /// Copy-constructor for strong pointers to objects of another class.
    159159            template <class O>
    160             inline StrongPtr(const StrongPtr<O>& other) : pointer_(other.get()), base_(other.base_)
     160            inline StrongPtr(const StrongPtr<O>& other) : pointer_(other.get()), base_(other.getBase())
    161161            {
    162162                if (this->base_)
  • code/branches/cpp11_v2/src/libraries/core/object/WeakPtr.h

    r10624 r10744  
    118118            /// Copy-constructor for weak pointers to objects of another class.
    119119            template <class O>
    120             inline WeakPtr(const WeakPtr<O>& other) : pointer_(other.get()), base_(other.base_), callback_(0)
     120            inline WeakPtr(const WeakPtr<O>& other) : pointer_(other.get()), base_(other.getBase()), callback_(0)
    121121            {
    122122                this->registerAsDestructionListener(this->base_);
  • code/branches/cpp11_v2/test/core/object/StrongPtrTest.cc

    r10624 r10744  
     1#include <utility>
    12#include <gtest/gtest.h>
    23#include "core/object/StrongPtr.h"
     
    2324        StrongPtr<DestroyableTest> strongPtr = test;
    2425        EXPECT_EQ(test, strongPtr.get());
     26        EXPECT_EQ(test, &(*strongPtr));
     27        EXPECT_EQ(static_cast<Destroyable*>(test), strongPtr.getBase());
     28        test->destroy();
     29    }
     30
     31    TEST(StrongPtrTest, CanHaveMultiplePointers)
     32    {
     33        bool bla;
     34        DestroyableTest* test = new DestroyableTest(bla);
     35        StrongPtr<DestroyableTest> strongPtr1 = test;
     36        StrongPtr<DestroyableTest> strongPtr2 = test;
     37        StrongPtr<DestroyableTest> strongPtr3 = test;
     38        EXPECT_EQ(test, strongPtr1.get());
     39        EXPECT_EQ(test, strongPtr2.get());
     40        EXPECT_EQ(test, strongPtr3.get());
     41        EXPECT_EQ(3u, test->getReferenceCount());
    2542        test->destroy();
    2643    }
     
    141158        test->destroy();
    142159    }
     160
     161    TEST(StrongPtrTest, TestConstructors)
     162    {
     163        bool bla;
     164        DestroyableTest* test = new DestroyableTest(bla);
     165
     166        // default
     167        StrongPtr<DestroyableTest> strongPtr1;
     168        EXPECT_EQ(NULL, strongPtr1.get());
     169
     170        // pointer
     171        StrongPtr<DestroyableTest> strongPtr2a = test;
     172        StrongPtr<DestroyableTest> strongPtr2b(test);
     173        EXPECT_EQ(test, strongPtr2a.get());
     174        EXPECT_EQ(test, strongPtr2b.get());
     175
     176        // copy
     177        StrongPtr<DestroyableTest> strongPtr3a = strongPtr2a;
     178        StrongPtr<DestroyableTest> strongPtr3b(strongPtr2b);
     179        EXPECT_EQ(test, strongPtr3a.get());
     180        EXPECT_EQ(test, strongPtr3b.get());
     181
     182        // move
     183        StrongPtr<DestroyableTest> strongPtr4a = std::move(strongPtr3a);
     184        StrongPtr<DestroyableTest> strongPtr4b(std::move(strongPtr3b));
     185        EXPECT_EQ(test, strongPtr4a.get());
     186        EXPECT_EQ(test, strongPtr4b.get());
     187
     188        // other
     189        StrongPtr<Destroyable> strongPtr5a = strongPtr4a;
     190        StrongPtr<Destroyable> strongPtr5b(strongPtr4b);
     191        EXPECT_EQ(test, strongPtr5a.get());
     192        EXPECT_EQ(test, strongPtr5b.get());
     193
     194        // weakPtr
     195        WeakPtr<DestroyableTest> weakPtr = test;
     196        StrongPtr<DestroyableTest> strongPtr6a = weakPtr;
     197        StrongPtr<DestroyableTest> strongPtr6b(weakPtr);
     198        EXPECT_EQ(test, strongPtr6a.get());
     199        EXPECT_EQ(test, strongPtr6b.get());
     200
     201        // weakPtr other
     202        StrongPtr<Destroyable> strongPtr7a = weakPtr;
     203        StrongPtr<Destroyable> strongPtr7b(weakPtr);
     204        EXPECT_EQ(test, strongPtr7a.get());
     205        EXPECT_EQ(test, strongPtr7b.get());
     206
     207        test->destroy();
     208    }
     209
     210    TEST(StrongPtrTest, TestAssignments)
     211    {
     212        bool bla;
     213        DestroyableTest* test = new DestroyableTest(bla);
     214
     215        // pointer
     216        StrongPtr<DestroyableTest> strongPtr1;
     217        strongPtr1 = test;
     218        EXPECT_EQ(test, strongPtr1.get());
     219
     220        // copy
     221        StrongPtr<DestroyableTest> strongPtr2;
     222        strongPtr2 = strongPtr1;
     223        EXPECT_EQ(test, strongPtr2.get());
     224
     225        // move
     226        StrongPtr<DestroyableTest> strongPtr3;
     227        strongPtr3 = std::move(strongPtr2);
     228        EXPECT_EQ(test, strongPtr3.get());
     229
     230        // other
     231        StrongPtr<Destroyable> strongPtr4;
     232        strongPtr4 = strongPtr3;
     233        EXPECT_EQ(test, strongPtr4.get());
     234
     235        // weakPtr
     236        WeakPtr<DestroyableTest> weakPtr = test;
     237        StrongPtr<DestroyableTest> strongPtr5;
     238        strongPtr5 = weakPtr;
     239        EXPECT_EQ(test, strongPtr5.get());
     240
     241        // weakPtr other
     242        StrongPtr<Destroyable> strongPtr6;
     243        strongPtr6 = weakPtr;
     244        EXPECT_EQ(test, strongPtr6.get());
     245
     246        test->destroy();
     247    }
     248
     249    TEST(StrongPtrTest, TestStrongPtrInVector)
     250    {
     251        bool bla;
     252        DestroyableTest* test = new DestroyableTest(bla);
     253
     254        std::vector<StrongPtr<DestroyableTest>> vector;
     255        vector.push_back(test);
     256        vector.push_back(test);
     257        vector.push_back(test);
     258
     259        ASSERT_EQ(3, vector.size());
     260        EXPECT_EQ(test, vector[0].get());
     261        EXPECT_EQ(test, vector[1].get());
     262        EXPECT_EQ(test, vector[2].get());
     263        EXPECT_EQ(3u, test->getReferenceCount());
     264
     265        vector.clear();
     266        EXPECT_EQ(0u, test->getReferenceCount());
     267
     268        test->destroy();
     269    }
     270
     271    TEST(StrongPtrTest, TestReset)
     272    {
     273        bool bla;
     274        DestroyableTest* test = new DestroyableTest(bla);
     275        StrongPtr<DestroyableTest> strongPtr(test);
     276        EXPECT_EQ(test, strongPtr.get());
     277        EXPECT_EQ(1u, test->getReferenceCount());
     278        strongPtr.reset();
     279        EXPECT_EQ(NULL, strongPtr.get());
     280        EXPECT_EQ(0u, test->getReferenceCount());
     281
     282        test->destroy();
     283    }
     284
     285    TEST(StrongPtrTest, TestSwap)
     286    {
     287        bool bla;
     288        DestroyableTest* test1 = new DestroyableTest(bla);
     289        DestroyableTest* test2 = new DestroyableTest(bla);
     290
     291        StrongPtr<DestroyableTest> strongPtr1(test1);
     292        StrongPtr<DestroyableTest> strongPtr2(test2);
     293        EXPECT_EQ(test1, strongPtr1.get());
     294        EXPECT_EQ(test2, strongPtr2.get());
     295        EXPECT_EQ(1u, test1->getReferenceCount());
     296        EXPECT_EQ(1u, test2->getReferenceCount());
     297
     298        strongPtr1.swap(strongPtr2);
     299
     300        EXPECT_EQ(test2, strongPtr1.get());
     301        EXPECT_EQ(test1, strongPtr2.get());
     302        EXPECT_EQ(1u, test1->getReferenceCount());
     303        EXPECT_EQ(1u, test2->getReferenceCount());
     304
     305        test1->destroy();
     306        test2->destroy();
     307    }
    143308}
  • code/branches/cpp11_v2/test/core/object/WeakPtrTest.cc

    r10624 r10744  
     1#include <utility>
    12#include <gtest/gtest.h>
    23#include <gmock/gmock.h>
     
    2425        WeakPtr<DestroyableTest> weakPtr = test;
    2526        EXPECT_EQ(test, weakPtr.get());
     27        EXPECT_EQ(test, &(*weakPtr));
     28        EXPECT_EQ(static_cast<Destroyable*>(test), weakPtr.getBase());
    2629        test->destroy();
    2730    }
     
    3437        test->destroy();
    3538        EXPECT_EQ(NULL, 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(NULL, weakPtr1.get());
     52        EXPECT_EQ(NULL, weakPtr2.get());
     53        EXPECT_EQ(NULL, weakPtr3.get());
    3654    }
    3755
     
    94112        test->destroy();
    95113    }
     114
     115    TEST(WeakPtrTest, TestConstructors)
     116    {
     117        DestroyableTest* test = new DestroyableTest();
     118
     119        // default
     120        WeakPtr<DestroyableTest> weakPtr1;
     121        EXPECT_EQ(NULL, 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(3, 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(NULL, vector[0].get());
     194        EXPECT_EQ(NULL, vector[1].get());
     195        EXPECT_EQ(NULL, 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(NULL, 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    }
    96227}
  • code/branches/cpp11_v2/test/util/SharedPtrTest.cc

    r9114 r10744  
     1#include <utility>
    12#include <gtest/gtest.h>
    23#include <gmock/gmock.h>
     
    2728        };
    2829
    29         class TestClassMock : public TestClass
     30        class TestChildClassMock : public TestChildClass
    3031        {
    3132            public:
    32                 TestClassMock() {}
    33                 ~TestClassMock() { objectDestroyed(); }
     33                TestChildClassMock() {}
     34                ~TestChildClassMock() { objectDestroyed(); }
    3435
    3536                MOCK_METHOD0(objectDestroyed, void());
     
    4950        SharedPtr<TestClass> test = pointer;
    5051        EXPECT_EQ(pointer, test.get());
     52        EXPECT_EQ(pointer, &(*test));
    5153    }
    5254
     
    165167    TEST(SharedPtr, ObjectDestroyedOnePointer)
    166168    {
    167         TestClassMock* pointer = new TestClassMock();
     169        TestChildClassMock* pointer = new TestChildClassMock();
    168170        SharedPtr<TestClass> test = pointer;
    169171
     
    173175    TEST(SharedPtr, ObjectDestroyedManyPointers)
    174176    {
    175         TestClassMock* pointer = new TestClassMock();
     177        TestChildClassMock* pointer = new TestChildClassMock();
    176178
    177179        SharedPtr<TestClass> test = pointer;
     
    182184        EXPECT_CALL(*pointer, objectDestroyed()).Times(1);
    183185    }
     186
     187    TEST(SharedPtr, TestConstructors)
     188    {
     189        {
     190        TestChildClassMock* pointer = new TestChildClassMock();
     191
     192        // default
     193        SharedPtr<TestChildClass> sharedPtr1;
     194        EXPECT_EQ(NULL, sharedPtr1.get());
     195
     196        // pointer
     197        SharedPtr<TestChildClass> sharedPtr2(pointer);
     198        EXPECT_EQ(pointer, sharedPtr2.get());
     199
     200        // copy
     201        SharedPtr<TestChildClass> sharedPtr3(sharedPtr2);
     202        EXPECT_EQ(pointer, sharedPtr3.get());
     203
     204        // move
     205        SharedPtr<TestChildClass> sharedPtr4(std::move(sharedPtr3));
     206        EXPECT_EQ(pointer, sharedPtr4.get());
     207
     208        // other
     209        SharedPtr<TestClass> sharedPtr5(sharedPtr4);
     210        EXPECT_EQ(pointer, sharedPtr5.get());
     211
     212        EXPECT_CALL(*pointer, objectDestroyed()).Times(1);
     213        }
     214    }
     215
     216    TEST(SharedPtr, TestConstructors2)
     217    {
     218        {
     219        TestChildClassMock* pointer = new TestChildClassMock();
     220
     221        // default
     222        SharedPtr<TestChildClass> sharedPtr1;
     223        EXPECT_EQ(NULL, sharedPtr1.get());
     224
     225        // pointer
     226        SharedPtr<TestChildClass> sharedPtr2 = pointer;
     227        EXPECT_EQ(pointer, sharedPtr2.get());
     228
     229        // copy
     230        SharedPtr<TestChildClass> sharedPtr3 = sharedPtr2;
     231        EXPECT_EQ(pointer, sharedPtr3.get());
     232
     233        // move
     234        SharedPtr<TestChildClass> sharedPtr4 = std::move(sharedPtr3);
     235        EXPECT_EQ(pointer, sharedPtr4.get());
     236
     237        // other
     238        SharedPtr<TestClass> sharedPtr5 = sharedPtr4;
     239        EXPECT_EQ(pointer, sharedPtr5.get());
     240
     241        EXPECT_CALL(*pointer, objectDestroyed()).Times(1);
     242        }
     243    }
     244
     245    TEST(SharedPtr, TestAssignments)
     246    {
     247        TestChildClassMock* pointer = new TestChildClassMock();
     248
     249        SharedPtr<TestChildClass> sharedPtr1(pointer);
     250        EXPECT_EQ(pointer, sharedPtr1.get());
     251
     252        // copy
     253        SharedPtr<TestChildClass> sharedPtr2;
     254        sharedPtr2 = sharedPtr1;
     255        EXPECT_EQ(pointer, sharedPtr2.get());
     256
     257        // move
     258        SharedPtr<TestChildClass> sharedPtr3;
     259        sharedPtr3 = std::move(sharedPtr2);
     260        EXPECT_EQ(pointer, sharedPtr3.get());
     261
     262        // other
     263        SharedPtr<TestClass> sharedPtr4;
     264        sharedPtr4 = sharedPtr3;
     265        EXPECT_EQ(pointer, sharedPtr4.get());
     266
     267        EXPECT_CALL(*pointer, objectDestroyed()).Times(1);
     268    }
    184269}
Note: See TracChangeset for help on using the changeset viewer.