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