Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/cpp11_v2/test/core/object/ObjectListIteratorTest.cc @ 10920

Last change on this file since 10920 was 10920, checked in by landauf, 10 years ago

no static functions anymore in ObjectList. you need to instantiate an ObjectList to use it.
this allows for prettier for-loop syntax when iterating over an ObjectList of a specific context: for (T* object : ObjectList<T>(context)) { … }

  • Property svn:eol-style set to native
File size: 3.5 KB
Line 
1#include <gtest/gtest.h>
2#include <gmock/gmock.h>
3
4#include "core/object/ObjectListIterator.h"
5#include "core/class/OrxonoxClass.h"
6#include "core/class/OrxonoxInterface.h"
7#include "core/CoreIncludes.h"
8#include "core/module/ModuleInstance.h"
9
10namespace orxonox
11{
12    namespace
13    {
14        class TestInterface : virtual public OrxonoxInterface
15        {
16            public:
17                TestInterface() { RegisterObject(TestInterface); }
18        };
19
20        class TestClass : public OrxonoxClass, public TestInterface
21        {
22            public:
23                TestClass() { RegisterObject(TestClass); }
24                MOCK_METHOD0(test, void());
25        };
26
27        RegisterClassNoArgs(TestInterface);
28        RegisterClassNoArgs(TestClass);
29
30        // Fixture
31        class ObjectListIteratorTest : public ::testing::Test
32        {
33            public:
34                virtual void SetUp() override
35                {
36                    new IdentifierManager();
37                    ModuleInstance::getCurrentModuleInstance()->loadAllStaticallyInitializedInstances(StaticInitialization::IDENTIFIER);
38                    Context::setRootContext(new Context(nullptr));
39                }
40
41                virtual void TearDown() override
42                {
43                    Context::destroyRootContext();
44                    ModuleInstance::getCurrentModuleInstance()->unloadAllStaticallyInitializedInstances(StaticInitialization::IDENTIFIER);
45                    delete &IdentifierManager::getInstance();
46                }
47        };
48    }
49
50    TEST_F(ObjectListIteratorTest, CanCreateIterator)
51    {
52        ObjectListIterator<TestClass> it;
53    }
54
55    TEST_F(ObjectListIteratorTest, CanAssignIterator)
56    {
57        ObjectList<TestClass> list;
58        ObjectListIterator<TestClass> it = list.begin();
59    }
60
61    TEST_F(ObjectListIteratorTest, CanIterateOverEmptyList)
62    {
63        size_t i = 0;
64        ObjectList<TestClass> list;
65        for (ObjectListIterator<TestClass> it = list.begin(); it != list.end(); ++it)
66            ++i;
67        EXPECT_EQ(0u, i);
68    }
69
70    TEST_F(ObjectListIteratorTest, CanIterateOverFullList)
71    {
72        TestClass test1;
73        TestClass test2;
74        TestClass test3;
75        TestInterface interface;
76
77        size_t i = 0;
78        ObjectList<TestClass> list;
79        for (ObjectListIterator<TestClass> it = list.begin(); it != list.end(); ++it)
80        {
81            ++i;
82            if (i == 1u) EXPECT_EQ(&test1, *it);
83            if (i == 2u) EXPECT_EQ(&test2, *it);
84            if (i == 3u) EXPECT_EQ(&test3, *it);
85        }
86        EXPECT_EQ(3u, i);
87    }
88
89    TEST_F(ObjectListIteratorTest, CanIterateReverseOverFullList)
90    {
91        TestClass test1;
92        TestClass test2;
93        TestClass test3;
94        TestInterface interface;
95
96        size_t i = 0;
97        ObjectList<TestClass> list;
98        for (ObjectListIterator<TestClass> it = list.rbegin(); it != list.rend(); --it)
99        {
100            ++i;
101            if (i == 1u) EXPECT_EQ(&test3, *it);
102            if (i == 2u) EXPECT_EQ(&test2, *it);
103            if (i == 3u) EXPECT_EQ(&test1, *it);
104        }
105        EXPECT_EQ(3u, i);
106    }
107
108    TEST_F(ObjectListIteratorTest, CanCallObjects)
109    {
110        TestClass test1;
111        TestClass test2;
112        TestClass test3;
113        TestInterface interface;
114
115        EXPECT_CALL(test1, test());
116        EXPECT_CALL(test2, test());
117        EXPECT_CALL(test3, test());
118
119        ObjectList<TestClass> list;
120        for (ObjectListIterator<TestClass> it = list.begin(); it != list.end(); ++it)
121            it->test();
122    }
123}
Note: See TracBrowser for help on using the repository browser.