| 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 |  | 
|---|
| 10 | namespace 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() 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(IteratorTest, CanCreateIterator) | 
|---|
| 51 | { | 
|---|
| 52 | Iterator<TestInterface> it; | 
|---|
| 53 | } | 
|---|
| 54 |  | 
|---|
| 55 | TEST_F(IteratorTest, CanAssignIterator) | 
|---|
| 56 | { | 
|---|
| 57 | ObjectList<TestInterface> list; | 
|---|
| 58 | Iterator<TestInterface> it = list.begin(); | 
|---|
| 59 | } | 
|---|
| 60 |  | 
|---|
| 61 | TEST_F(IteratorTest, CanIterateOverEmptyList) | 
|---|
| 62 | { | 
|---|
| 63 | size_t i = 0; | 
|---|
| 64 | ObjectList<TestInterface> list; | 
|---|
| 65 | for (Iterator<TestInterface> it = list.begin(); it != list.end(); ++it) | 
|---|
| 66 | ++i; | 
|---|
| 67 | EXPECT_EQ(0u, i); | 
|---|
| 68 | } | 
|---|
| 69 |  | 
|---|
| 70 | TEST_F(IteratorTest, CanCallObjects) | 
|---|
| 71 | { | 
|---|
| 72 | TestClass test1; | 
|---|
| 73 | TestClass test2; | 
|---|
| 74 | TestClass test3; | 
|---|
| 75 |  | 
|---|
| 76 | EXPECT_CALL(test1, test()); | 
|---|
| 77 | EXPECT_CALL(test2, test()); | 
|---|
| 78 | EXPECT_CALL(test3, test()); | 
|---|
| 79 |  | 
|---|
| 80 | // iterate over interfaces but use a TestClass iterator - now we can call TestClass::test() | 
|---|
| 81 | ObjectList<TestInterface> list; | 
|---|
| 82 | for (Iterator<TestClass> it = list.begin(); it != list.end(); ++it) | 
|---|
| 83 | it->test(); | 
|---|
| 84 | } | 
|---|
| 85 |  | 
|---|
| 86 | TEST_F(IteratorTest, CanIterateOverInterfaceListWithInterfaceIterator) | 
|---|
| 87 | { | 
|---|
| 88 | TestClass testClass; | 
|---|
| 89 | TestInterface testInterface; | 
|---|
| 90 |  | 
|---|
| 91 | size_t i = 0; | 
|---|
| 92 | ObjectList<TestInterface> list; | 
|---|
| 93 | for (Iterator<TestInterface> it = list.begin(); it != list.end(); ++it) | 
|---|
| 94 | { | 
|---|
| 95 | ++i; | 
|---|
| 96 | if (i == 1u) EXPECT_EQ(&testClass, *it); | 
|---|
| 97 | if (i == 2u) EXPECT_EQ(&testInterface, *it); | 
|---|
| 98 | } | 
|---|
| 99 | EXPECT_EQ(2u, i); | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 | TEST_F(IteratorTest, CanIterateOverClassListWithClassIterator) | 
|---|
| 103 | { | 
|---|
| 104 | TestClass testClass; | 
|---|
| 105 | TestInterface testInterface; | 
|---|
| 106 |  | 
|---|
| 107 | size_t i = 0; | 
|---|
| 108 | ObjectList<TestClass> list; | 
|---|
| 109 | for (Iterator<TestClass> it = list.begin(); it != list.end(); ++it) | 
|---|
| 110 | { | 
|---|
| 111 | ++i; | 
|---|
| 112 | if (i == 1u) EXPECT_EQ(&testClass, *it); | 
|---|
| 113 | } | 
|---|
| 114 | EXPECT_EQ(1u, i); | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | TEST_F(IteratorTest, CanIterateOverInterfaceListWithClassIterator) | 
|---|
| 118 | { | 
|---|
| 119 | TestClass testClass; | 
|---|
| 120 | TestInterface testInterface; | 
|---|
| 121 |  | 
|---|
| 122 | size_t i = 0; | 
|---|
| 123 | ObjectList<TestInterface> list; | 
|---|
| 124 | for (Iterator<TestClass> it = list.begin(); it != list.end(); ++it) | 
|---|
| 125 | { | 
|---|
| 126 | ++i; | 
|---|
| 127 | if (i == 1u) EXPECT_EQ(&testClass, *it); | 
|---|
| 128 | if (i == 2u) EXPECT_EQ(nullptr, *it); | 
|---|
| 129 | } | 
|---|
| 130 | EXPECT_EQ(2u, i); | 
|---|
| 131 | } | 
|---|
| 132 |  | 
|---|
| 133 | TEST_F(IteratorTest, CanIterateOverClassListWithInterfaceIterator) | 
|---|
| 134 | { | 
|---|
| 135 | TestClass testClass; | 
|---|
| 136 | TestInterface testInterface; | 
|---|
| 137 |  | 
|---|
| 138 | size_t i = 0; | 
|---|
| 139 | ObjectList<TestClass> list; | 
|---|
| 140 | for (Iterator<TestInterface> it = list.begin(); it != list.end(); ++it) | 
|---|
| 141 | { | 
|---|
| 142 | ++i; | 
|---|
| 143 | if (i == 1u) EXPECT_EQ(&testClass, *it); | 
|---|
| 144 | } | 
|---|
| 145 | EXPECT_EQ(1u, i); | 
|---|
| 146 | } | 
|---|
| 147 | } | 
|---|