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