Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/cpp11_v2/test/core/object/IteratorTest.cc @ 10765

Last change on this file since 10765 was 10765, checked in by landauf, 8 years ago

replace 'NULL' by 'nullptr'

  • Property svn:eol-style set to native
File size: 4.1 KB
Line 
1#include <gtest/gtest.h>
2#include <gmock/gmock.h>
3
4#include "core/object/Iterator.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 IteratorTest : public ::testing::Test
32        {
33            public:
34                virtual void SetUp()
35                {
36                    new IdentifierManager();
37                    ModuleInstance::getCurrentModuleInstance()->loadAllStaticallyInitializedInstances(StaticInitialization::IDENTIFIER);
38                    Context::setRootContext(new Context(nullptr));
39                }
40
41                virtual void TearDown()
42                {
43                    Context::destroyRootContext();
44                    ModuleInstance::getCurrentModuleInstance()->unloadAllStaticallyInitializedInstances(StaticInitialization::IDENTIFIER);
45                    delete &IdentifierManager::getInstance();
46                }
47        };
48    }
49
50    TEST_F(IteratorTest, CanCreateIterator)
51    {
52        Iterator<TestInterface> it;
53    }
54
55    TEST_F(IteratorTest, CanAssignIterator)
56    {
57        Iterator<TestInterface> it = ObjectList<TestInterface>::begin();
58    }
59
60    TEST_F(IteratorTest, CanIterateOverEmptyList)
61    {
62        size_t i = 0;
63        for (Iterator<TestInterface> it = ObjectList<TestInterface>::begin(); it != ObjectList<TestInterface>::end(); ++it)
64            ++i;
65        EXPECT_EQ(0u, i);
66    }
67
68    TEST_F(IteratorTest, CanCallObjects)
69    {
70        TestClass test1;
71        TestClass test2;
72        TestClass test3;
73
74        EXPECT_CALL(test1, test());
75        EXPECT_CALL(test2, test());
76        EXPECT_CALL(test3, test());
77
78        // iterate over interfaces but use a TestClass iterator - now we can call TestClass::test()
79        for (Iterator<TestClass> it = ObjectList<TestInterface>::begin(); it != ObjectList<TestInterface>::end(); ++it)
80            it->test();
81    }
82
83    TEST_F(IteratorTest, CanIterateOverInterfaceListWithInterfaceIterator)
84    {
85        TestClass testClass;
86        TestInterface testInterface;
87
88        size_t i = 0;
89        for (Iterator<TestInterface> it = ObjectList<TestInterface>::begin(); it != ObjectList<TestInterface>::end(); ++it)
90        {
91            ++i;
92            if (i == 1u) EXPECT_EQ(&testClass, *it);
93            if (i == 2u) EXPECT_EQ(&testInterface, *it);
94        }
95        EXPECT_EQ(2u, i);
96    }
97
98    TEST_F(IteratorTest, CanIterateOverClassListWithClassIterator)
99    {
100        TestClass testClass;
101        TestInterface testInterface;
102
103        size_t i = 0;
104        for (Iterator<TestClass> it = ObjectList<TestClass>::begin(); it != ObjectList<TestClass>::end(); ++it)
105        {
106            ++i;
107            if (i == 1u) EXPECT_EQ(&testClass, *it);
108        }
109        EXPECT_EQ(1u, i);
110    }
111
112    TEST_F(IteratorTest, CanIterateOverInterfaceListWithClassIterator)
113    {
114        TestClass testClass;
115        TestInterface testInterface;
116
117        size_t i = 0;
118        for (Iterator<TestClass> it = ObjectList<TestInterface>::begin(); it != ObjectList<TestInterface>::end(); ++it)
119        {
120            ++i;
121            if (i == 1u) EXPECT_EQ(&testClass, *it);
122            if (i == 2u) EXPECT_EQ(nullptr, *it);
123        }
124        EXPECT_EQ(2u, i);
125    }
126
127    TEST_F(IteratorTest, CanIterateOverClassListWithInterfaceIterator)
128    {
129        TestClass testClass;
130        TestInterface testInterface;
131
132        size_t i = 0;
133        for (Iterator<TestInterface> it = ObjectList<TestClass>::begin(); it != ObjectList<TestClass>::end(); ++it)
134        {
135            ++i;
136            if (i == 1u) EXPECT_EQ(&testClass, *it);
137        }
138        EXPECT_EQ(1u, i);
139    }
140}
Note: See TracBrowser for help on using the repository browser.