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