- Timestamp:
- Nov 1, 2015, 9:03:05 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/cpp11_v2/test/core/object/StrongPtrTest.cc
r10624 r10744 1 #include <utility> 1 2 #include <gtest/gtest.h> 2 3 #include "core/object/StrongPtr.h" … … 23 24 StrongPtr<DestroyableTest> strongPtr = test; 24 25 EXPECT_EQ(test, strongPtr.get()); 26 EXPECT_EQ(test, &(*strongPtr)); 27 EXPECT_EQ(static_cast<Destroyable*>(test), strongPtr.getBase()); 28 test->destroy(); 29 } 30 31 TEST(StrongPtrTest, CanHaveMultiplePointers) 32 { 33 bool bla; 34 DestroyableTest* test = new DestroyableTest(bla); 35 StrongPtr<DestroyableTest> strongPtr1 = test; 36 StrongPtr<DestroyableTest> strongPtr2 = test; 37 StrongPtr<DestroyableTest> strongPtr3 = test; 38 EXPECT_EQ(test, strongPtr1.get()); 39 EXPECT_EQ(test, strongPtr2.get()); 40 EXPECT_EQ(test, strongPtr3.get()); 41 EXPECT_EQ(3u, test->getReferenceCount()); 25 42 test->destroy(); 26 43 } … … 141 158 test->destroy(); 142 159 } 160 161 TEST(StrongPtrTest, TestConstructors) 162 { 163 bool bla; 164 DestroyableTest* test = new DestroyableTest(bla); 165 166 // default 167 StrongPtr<DestroyableTest> strongPtr1; 168 EXPECT_EQ(NULL, strongPtr1.get()); 169 170 // pointer 171 StrongPtr<DestroyableTest> strongPtr2a = test; 172 StrongPtr<DestroyableTest> strongPtr2b(test); 173 EXPECT_EQ(test, strongPtr2a.get()); 174 EXPECT_EQ(test, strongPtr2b.get()); 175 176 // copy 177 StrongPtr<DestroyableTest> strongPtr3a = strongPtr2a; 178 StrongPtr<DestroyableTest> strongPtr3b(strongPtr2b); 179 EXPECT_EQ(test, strongPtr3a.get()); 180 EXPECT_EQ(test, strongPtr3b.get()); 181 182 // move 183 StrongPtr<DestroyableTest> strongPtr4a = std::move(strongPtr3a); 184 StrongPtr<DestroyableTest> strongPtr4b(std::move(strongPtr3b)); 185 EXPECT_EQ(test, strongPtr4a.get()); 186 EXPECT_EQ(test, strongPtr4b.get()); 187 188 // other 189 StrongPtr<Destroyable> strongPtr5a = strongPtr4a; 190 StrongPtr<Destroyable> strongPtr5b(strongPtr4b); 191 EXPECT_EQ(test, strongPtr5a.get()); 192 EXPECT_EQ(test, strongPtr5b.get()); 193 194 // weakPtr 195 WeakPtr<DestroyableTest> weakPtr = test; 196 StrongPtr<DestroyableTest> strongPtr6a = weakPtr; 197 StrongPtr<DestroyableTest> strongPtr6b(weakPtr); 198 EXPECT_EQ(test, strongPtr6a.get()); 199 EXPECT_EQ(test, strongPtr6b.get()); 200 201 // weakPtr other 202 StrongPtr<Destroyable> strongPtr7a = weakPtr; 203 StrongPtr<Destroyable> strongPtr7b(weakPtr); 204 EXPECT_EQ(test, strongPtr7a.get()); 205 EXPECT_EQ(test, strongPtr7b.get()); 206 207 test->destroy(); 208 } 209 210 TEST(StrongPtrTest, TestAssignments) 211 { 212 bool bla; 213 DestroyableTest* test = new DestroyableTest(bla); 214 215 // pointer 216 StrongPtr<DestroyableTest> strongPtr1; 217 strongPtr1 = test; 218 EXPECT_EQ(test, strongPtr1.get()); 219 220 // copy 221 StrongPtr<DestroyableTest> strongPtr2; 222 strongPtr2 = strongPtr1; 223 EXPECT_EQ(test, strongPtr2.get()); 224 225 // move 226 StrongPtr<DestroyableTest> strongPtr3; 227 strongPtr3 = std::move(strongPtr2); 228 EXPECT_EQ(test, strongPtr3.get()); 229 230 // other 231 StrongPtr<Destroyable> strongPtr4; 232 strongPtr4 = strongPtr3; 233 EXPECT_EQ(test, strongPtr4.get()); 234 235 // weakPtr 236 WeakPtr<DestroyableTest> weakPtr = test; 237 StrongPtr<DestroyableTest> strongPtr5; 238 strongPtr5 = weakPtr; 239 EXPECT_EQ(test, strongPtr5.get()); 240 241 // weakPtr other 242 StrongPtr<Destroyable> strongPtr6; 243 strongPtr6 = weakPtr; 244 EXPECT_EQ(test, strongPtr6.get()); 245 246 test->destroy(); 247 } 248 249 TEST(StrongPtrTest, TestStrongPtrInVector) 250 { 251 bool bla; 252 DestroyableTest* test = new DestroyableTest(bla); 253 254 std::vector<StrongPtr<DestroyableTest>> vector; 255 vector.push_back(test); 256 vector.push_back(test); 257 vector.push_back(test); 258 259 ASSERT_EQ(3, vector.size()); 260 EXPECT_EQ(test, vector[0].get()); 261 EXPECT_EQ(test, vector[1].get()); 262 EXPECT_EQ(test, vector[2].get()); 263 EXPECT_EQ(3u, test->getReferenceCount()); 264 265 vector.clear(); 266 EXPECT_EQ(0u, test->getReferenceCount()); 267 268 test->destroy(); 269 } 270 271 TEST(StrongPtrTest, TestReset) 272 { 273 bool bla; 274 DestroyableTest* test = new DestroyableTest(bla); 275 StrongPtr<DestroyableTest> strongPtr(test); 276 EXPECT_EQ(test, strongPtr.get()); 277 EXPECT_EQ(1u, test->getReferenceCount()); 278 strongPtr.reset(); 279 EXPECT_EQ(NULL, strongPtr.get()); 280 EXPECT_EQ(0u, test->getReferenceCount()); 281 282 test->destroy(); 283 } 284 285 TEST(StrongPtrTest, TestSwap) 286 { 287 bool bla; 288 DestroyableTest* test1 = new DestroyableTest(bla); 289 DestroyableTest* test2 = new DestroyableTest(bla); 290 291 StrongPtr<DestroyableTest> strongPtr1(test1); 292 StrongPtr<DestroyableTest> strongPtr2(test2); 293 EXPECT_EQ(test1, strongPtr1.get()); 294 EXPECT_EQ(test2, strongPtr2.get()); 295 EXPECT_EQ(1u, test1->getReferenceCount()); 296 EXPECT_EQ(1u, test2->getReferenceCount()); 297 298 strongPtr1.swap(strongPtr2); 299 300 EXPECT_EQ(test2, strongPtr1.get()); 301 EXPECT_EQ(test1, strongPtr2.get()); 302 EXPECT_EQ(1u, test1->getReferenceCount()); 303 EXPECT_EQ(1u, test2->getReferenceCount()); 304 305 test1->destroy(); 306 test2->destroy(); 307 } 143 308 }
Note: See TracChangeset
for help on using the changeset viewer.