Changeset 11054 for code/branches/cpp11_v3/test/util/SharedPtrTest.cc
- Timestamp:
- Jan 10, 2016, 1:54:11 PM (9 years ago)
- Location:
- code/branches/cpp11_v3
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/cpp11_v3
- Property svn:mergeinfo changed
-
code/branches/cpp11_v3/test/util/SharedPtrTest.cc
r9114 r11054 1 #include <memory> 2 #include <utility> 1 3 #include <gtest/gtest.h> 2 4 #include <gmock/gmock.h> 3 #include "util/SharedPtr.h"4 5 5 6 namespace orxonox … … 27 28 }; 28 29 29 class TestC lassMock : public TestClass30 class TestChildClassMock : public TestChildClass 30 31 { 31 32 public: 32 TestC lassMock() {}33 ~TestC lassMock() { objectDestroyed(); }33 TestChildClassMock() {} 34 ~TestChildClassMock() { objectDestroyed(); } 34 35 35 36 MOCK_METHOD0(objectDestroyed, void()); … … 39 40 TEST(SharedPtr, ConstructorDefault) 40 41 { 41 SharedPtr<TestClass> test;42 EXPECT_EQ( 0, test.get());42 std::shared_ptr<TestClass> test; 43 EXPECT_EQ(nullptr, test.get()); 43 44 } 44 45 … … 47 48 TestClass* pointer = new TestClass(); 48 49 49 SharedPtr<TestClass> test = pointer; 50 EXPECT_EQ(pointer, test.get()); 50 std::shared_ptr<TestClass> test(pointer); 51 EXPECT_EQ(pointer, test.get()); 52 EXPECT_EQ(pointer, &(*test)); 51 53 } 52 54 … … 55 57 TestChildClass* pointer = new TestChildClass(); 56 58 57 SharedPtr<TestClass> test = pointer;59 std::shared_ptr<TestClass> test(pointer); 58 60 EXPECT_EQ(pointer, test.get()); 59 61 } … … 63 65 TestClass* pointer = new TestClass(); 64 66 65 SharedPtr<TestClass> other = pointer;66 EXPECT_EQ(pointer, other.get()); 67 68 SharedPtr<TestClass> test = other;67 std::shared_ptr<TestClass> other(pointer); 68 EXPECT_EQ(pointer, other.get()); 69 70 std::shared_ptr<TestClass> test = other; 69 71 EXPECT_EQ(pointer, test.get()); 70 72 } … … 74 76 TestChildClass* pointer = new TestChildClass(); 75 77 76 SharedPtr<TestChildClass> other = pointer;77 EXPECT_EQ(pointer, other.get()); 78 79 SharedPtr<TestClass> test = other;78 std::shared_ptr<TestChildClass> other(pointer); 79 EXPECT_EQ(pointer, other.get()); 80 81 std::shared_ptr<TestClass> test = other; 80 82 EXPECT_EQ(pointer, test.get()); 81 83 } … … 85 87 TestClass* pointer = new TestClass(); 86 88 87 SharedPtr<TestClass> other = pointer;88 EXPECT_EQ(pointer, other.get()); 89 90 SharedPtr<TestClass> test;91 EXPECT_EQ( 0, test.get());89 std::shared_ptr<TestClass> other(pointer); 90 EXPECT_EQ(pointer, other.get()); 91 92 std::shared_ptr<TestClass> test; 93 EXPECT_EQ(nullptr, test.get()); 92 94 93 95 test = other; … … 99 101 TestChildClass* pointer = new TestChildClass(); 100 102 101 SharedPtr<TestChildClass> other = pointer;102 EXPECT_EQ(pointer, other.get()); 103 104 SharedPtr<TestClass> test;105 EXPECT_EQ( 0, test.get());103 std::shared_ptr<TestChildClass> other(pointer); 104 EXPECT_EQ(pointer, other.get()); 105 106 std::shared_ptr<TestClass> test; 107 EXPECT_EQ(nullptr, test.get()); 106 108 107 109 test = other; … … 113 115 TestChildClass* pointer = new TestChildClass(); 114 116 115 SharedPtr<TestChildClass> other = pointer;116 EXPECT_EQ(pointer, other.get()); 117 118 SharedPtr<TestClass> test = other.cast<TestClass>();117 std::shared_ptr<TestChildClass> other(pointer); 118 EXPECT_EQ(pointer, other.get()); 119 120 std::shared_ptr<TestClass> test = std::static_pointer_cast<TestClass>(other); 119 121 EXPECT_EQ(pointer, test.get()); 120 122 } … … 124 126 TestClass* pointer = new TestClass(); 125 127 126 SharedPtr<TestClass> test = pointer;128 std::shared_ptr<TestClass> test(pointer); 127 129 EXPECT_EQ(pointer, test.get()); 128 130 … … 135 137 TEST(SharedPtr, Boolean) 136 138 { 137 SharedPtr<TestClass> test;138 EXPECT_EQ( 0, test.get());139 std::shared_ptr<TestClass> test; 140 EXPECT_EQ(nullptr, test.get()); 139 141 EXPECT_FALSE(test); 140 142 141 143 TestClass* pointer = new TestClass(); 142 144 143 test = pointer;144 EXPECT_EQ(pointer, test.get()); 145 EXPECT_TRUE( test);145 test.reset(pointer); 146 EXPECT_EQ(pointer, test.get()); 147 EXPECT_TRUE(static_cast<bool>(test)); 146 148 } 147 149 … … 151 153 TestClass* pointer2 = new TestClass(); 152 154 153 SharedPtr<TestClass> test1 = pointer1;154 SharedPtr<TestClass> test2 = pointer2;155 std::shared_ptr<TestClass> test1(pointer1); 156 std::shared_ptr<TestClass> test2(pointer2); 155 157 156 158 EXPECT_EQ(pointer1, test1.get()); … … 165 167 TEST(SharedPtr, ObjectDestroyedOnePointer) 166 168 { 167 TestC lassMock* pointer = new TestClassMock();168 SharedPtr<TestClass> test = pointer;169 TestChildClassMock* pointer = new TestChildClassMock(); 170 std::shared_ptr<TestClass> test(pointer); 169 171 170 172 EXPECT_CALL(*pointer, objectDestroyed()).Times(1); … … 173 175 TEST(SharedPtr, ObjectDestroyedManyPointers) 174 176 { 175 TestC lassMock* pointer = new TestClassMock();176 177 SharedPtr<TestClass> test = pointer;178 std::vector< SharedPtr<TestClass>> tests;177 TestChildClassMock* pointer = new TestChildClassMock(); 178 179 std::shared_ptr<TestClass> test(pointer); 180 std::vector<std::shared_ptr<TestClass>> tests; 179 181 for (size_t i = 0; i < 100; ++i) 180 tests.push_back(SharedPtr<TestClass>(test)); 182 tests.push_back(std::shared_ptr<TestClass>(test)); 183 184 EXPECT_CALL(*pointer, objectDestroyed()).Times(1); 185 } 186 187 TEST(SharedPtr, TestConstructors) 188 { 189 { 190 TestChildClassMock* pointer = new TestChildClassMock(); 191 192 // default 193 std::shared_ptr<TestChildClass> sharedPtr1; 194 EXPECT_EQ(nullptr, sharedPtr1.get()); 195 196 // pointer 197 std::shared_ptr<TestChildClass> sharedPtr2(pointer); 198 EXPECT_EQ(pointer, sharedPtr2.get()); 199 200 // copy 201 std::shared_ptr<TestChildClass> sharedPtr3(sharedPtr2); 202 EXPECT_EQ(pointer, sharedPtr3.get()); 203 204 // move 205 std::shared_ptr<TestChildClass> sharedPtr4(std::move(sharedPtr3)); 206 EXPECT_EQ(pointer, sharedPtr4.get()); 207 208 // other 209 std::shared_ptr<TestClass> sharedPtr5(sharedPtr4); 210 EXPECT_EQ(pointer, sharedPtr5.get()); 211 212 EXPECT_CALL(*pointer, objectDestroyed()).Times(1); 213 } 214 } 215 216 TEST(SharedPtr, TestConstructors2) 217 { 218 { 219 TestChildClassMock* pointer = new TestChildClassMock(); 220 221 // default 222 std::shared_ptr<TestChildClass> sharedPtr1; 223 EXPECT_EQ(nullptr, sharedPtr1.get()); 224 225 // pointer 226 std::shared_ptr<TestChildClass> sharedPtr2(pointer); 227 EXPECT_EQ(pointer, sharedPtr2.get()); 228 229 // copy 230 std::shared_ptr<TestChildClass> sharedPtr3 = sharedPtr2; 231 EXPECT_EQ(pointer, sharedPtr3.get()); 232 233 // move 234 std::shared_ptr<TestChildClass> sharedPtr4 = std::move(sharedPtr3); 235 EXPECT_EQ(pointer, sharedPtr4.get()); 236 237 // other 238 std::shared_ptr<TestClass> sharedPtr5 = sharedPtr4; 239 EXPECT_EQ(pointer, sharedPtr5.get()); 240 241 EXPECT_CALL(*pointer, objectDestroyed()).Times(1); 242 } 243 } 244 245 TEST(SharedPtr, TestAssignments) 246 { 247 TestChildClassMock* pointer = new TestChildClassMock(); 248 249 std::shared_ptr<TestChildClass> sharedPtr1(pointer); 250 EXPECT_EQ(pointer, sharedPtr1.get()); 251 252 // copy 253 std::shared_ptr<TestChildClass> sharedPtr2; 254 sharedPtr2 = sharedPtr1; 255 EXPECT_EQ(pointer, sharedPtr2.get()); 256 257 // move 258 std::shared_ptr<TestChildClass> sharedPtr3; 259 sharedPtr3 = std::move(sharedPtr2); 260 EXPECT_EQ(pointer, sharedPtr3.get()); 261 262 // other 263 std::shared_ptr<TestClass> sharedPtr4; 264 sharedPtr4 = sharedPtr3; 265 EXPECT_EQ(pointer, sharedPtr4.get()); 181 266 182 267 EXPECT_CALL(*pointer, objectDestroyed()).Times(1);
Note: See TracChangeset
for help on using the changeset viewer.