Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/wiimote/test/util/SharedPtrTest.cc @ 10422

Last change on this file since 10422 was 9114, checked in by landauf, 13 years ago

added tests for several classes and functions in util

  • Property svn:eol-style set to native
File size: 4.4 KB
Line 
1#include <gtest/gtest.h>
2#include <gmock/gmock.h>
3#include "util/SharedPtr.h"
4
5namespace orxonox
6{
7    namespace
8    {
9        class TestClass
10        {
11            public:
12                TestClass() : value_(0) {}
13                virtual ~TestClass() {}
14
15                void setValue(int value) { this->value_ = value; }
16                int getValue() const { return this->value_; }
17
18            private:
19                int value_;
20        };
21
22        class TestChildClass : public TestClass
23        {
24            public:
25                TestChildClass() {}
26                virtual ~TestChildClass() {}
27        };
28
29        class TestClassMock : public TestClass
30        {
31            public:
32                TestClassMock() {}
33                ~TestClassMock() { objectDestroyed(); }
34
35                MOCK_METHOD0(objectDestroyed, void());
36        };
37    }
38
39    TEST(SharedPtr, ConstructorDefault)
40    {
41        SharedPtr<TestClass> test;
42        EXPECT_EQ(0, test.get());
43    }
44
45    TEST(SharedPtr, ConstructorPointer)
46    {
47        TestClass* pointer = new TestClass();
48
49        SharedPtr<TestClass> test = pointer;
50        EXPECT_EQ(pointer, test.get());
51    }
52
53    TEST(SharedPtr, ConstructorChildPointer)
54    {
55        TestChildClass* pointer = new TestChildClass();
56
57        SharedPtr<TestClass> test = pointer;
58        EXPECT_EQ(pointer, test.get());
59    }
60
61    TEST(SharedPtr, ConstructorOther)
62    {
63        TestClass* pointer = new TestClass();
64
65        SharedPtr<TestClass> other = pointer;
66        EXPECT_EQ(pointer, other.get());
67
68        SharedPtr<TestClass> test = other;
69        EXPECT_EQ(pointer, test.get());
70    }
71
72    TEST(SharedPtr, ConstructorOtherChild)
73    {
74        TestChildClass* pointer = new TestChildClass();
75
76        SharedPtr<TestChildClass> other = pointer;
77        EXPECT_EQ(pointer, other.get());
78
79        SharedPtr<TestClass> test = other;
80        EXPECT_EQ(pointer, test.get());
81    }
82
83    TEST(SharedPtr, Assign)
84    {
85        TestClass* pointer = new TestClass();
86
87        SharedPtr<TestClass> other = pointer;
88        EXPECT_EQ(pointer, other.get());
89
90        SharedPtr<TestClass> test;
91        EXPECT_EQ(0, test.get());
92
93        test = other;
94        EXPECT_EQ(pointer, test.get());
95    }
96
97    TEST(SharedPtr, AssignChild)
98    {
99        TestChildClass* pointer = new TestChildClass();
100
101        SharedPtr<TestChildClass> other = pointer;
102        EXPECT_EQ(pointer, other.get());
103
104        SharedPtr<TestClass> test;
105        EXPECT_EQ(0, test.get());
106
107        test = other;
108        EXPECT_EQ(pointer, test.get());
109    }
110
111    TEST(SharedPtr, Cast)
112    {
113        TestChildClass* pointer = new TestChildClass();
114
115        SharedPtr<TestChildClass> other = pointer;
116        EXPECT_EQ(pointer, other.get());
117
118        SharedPtr<TestClass> test = other.cast<TestClass>();
119        EXPECT_EQ(pointer, test.get());
120    }
121
122    TEST(SharedPtr, Access)
123    {
124        TestClass* pointer = new TestClass();
125
126        SharedPtr<TestClass> test = pointer;
127        EXPECT_EQ(pointer, test.get());
128
129        EXPECT_EQ(0, test->getValue());
130        test->setValue(5);
131        EXPECT_EQ(5, test->getValue());
132        EXPECT_EQ(5, (*test).getValue());
133    }
134
135    TEST(SharedPtr, Boolean)
136    {
137        SharedPtr<TestClass> test;
138        EXPECT_EQ(0, test.get());
139        EXPECT_FALSE(test);
140
141        TestClass* pointer = new TestClass();
142
143        test = pointer;
144        EXPECT_EQ(pointer, test.get());
145        EXPECT_TRUE(test);
146    }
147
148    TEST(SharedPtr, Swap)
149    {
150        TestClass* pointer1 = new TestClass();
151        TestClass* pointer2 = new TestClass();
152
153        SharedPtr<TestClass> test1 = pointer1;
154        SharedPtr<TestClass> test2 = pointer2;
155
156        EXPECT_EQ(pointer1, test1.get());
157        EXPECT_EQ(pointer2, test2.get());
158
159        test1.swap(test2);
160
161        EXPECT_EQ(pointer2, test1.get());
162        EXPECT_EQ(pointer1, test2.get());
163    }
164
165    TEST(SharedPtr, ObjectDestroyedOnePointer)
166    {
167        TestClassMock* pointer = new TestClassMock();
168        SharedPtr<TestClass> test = pointer;
169
170        EXPECT_CALL(*pointer, objectDestroyed()).Times(1);
171    }
172
173    TEST(SharedPtr, ObjectDestroyedManyPointers)
174    {
175        TestClassMock* pointer = new TestClassMock();
176
177        SharedPtr<TestClass> test = pointer;
178        std::vector<SharedPtr<TestClass> > tests;
179        for (size_t i = 0; i < 100; ++i)
180            tests.push_back(SharedPtr<TestClass>(test));
181
182        EXPECT_CALL(*pointer, objectDestroyed()).Times(1);
183    }
184}
Note: See TracBrowser for help on using the repository browser.