| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 // This file tests both ref_counted.h and ref_ptr.h (which the former includes). | |
| 6 // TODO(vtl): Possibly we could separate these tests out better, since a lot of | |
| 7 // it is actually testing |RefPtr|. | |
| 8 | |
| 9 #include "mojo/edk/system/ref_counted.h" | |
| 10 | |
| 11 #include "build/build_config.h" | |
| 12 #include "mojo/public/cpp/system/macros.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | |
| 14 | |
| 15 #if defined(__clang__) | |
| 16 #define ALLOW_PESSIMIZING_MOVE(code_line) \ | |
| 17 _Pragma("clang diagnostic push") \ | |
| 18 _Pragma("clang diagnostic ignored \"-Wpessimizing-move\"") code_line; \ | |
| 19 _Pragma("clang diagnostic pop") | |
| 20 #else | |
| 21 #define ALLOW_PESSIMIZING_MOVE(code_line) code_line; | |
| 22 #endif | |
| 23 | |
| 24 #if defined(__clang__) | |
| 25 #define ALLOW_SELF_MOVE(code_line) \ | |
| 26 _Pragma("clang diagnostic push") \ | |
| 27 _Pragma("clang diagnostic ignored \"-Wself-move\"") code_line; \ | |
| 28 _Pragma("clang diagnostic pop") | |
| 29 #else | |
| 30 #define ALLOW_SELF_MOVE(code_line) code_line; | |
| 31 #endif | |
| 32 | |
| 33 namespace mojo { | |
| 34 namespace system { | |
| 35 namespace { | |
| 36 | |
| 37 class MyClass : public RefCountedThreadSafe<MyClass> { | |
| 38 protected: | |
| 39 MyClass(MyClass** created, bool* was_destroyed) | |
| 40 : was_destroyed_(was_destroyed) { | |
| 41 if (created) | |
| 42 *created = this; | |
| 43 } | |
| 44 virtual ~MyClass() { | |
| 45 if (was_destroyed_) | |
| 46 *was_destroyed_ = true; | |
| 47 } | |
| 48 | |
| 49 private: | |
| 50 FRIEND_REF_COUNTED_THREAD_SAFE(MyClass); | |
| 51 FRIEND_MAKE_REF_COUNTED(MyClass); | |
| 52 | |
| 53 bool* was_destroyed_; | |
| 54 | |
| 55 MOJO_DISALLOW_COPY_AND_ASSIGN(MyClass); | |
| 56 }; | |
| 57 | |
| 58 class MySubclass final : public MyClass { | |
| 59 private: | |
| 60 FRIEND_REF_COUNTED_THREAD_SAFE(MySubclass); | |
| 61 FRIEND_MAKE_REF_COUNTED(MySubclass); | |
| 62 | |
| 63 MySubclass(MySubclass** created, bool* was_destroyed) | |
| 64 : MyClass(nullptr, was_destroyed) { | |
| 65 if (created) | |
| 66 *created = this; | |
| 67 } | |
| 68 ~MySubclass() override {} | |
| 69 | |
| 70 MOJO_DISALLOW_COPY_AND_ASSIGN(MySubclass); | |
| 71 }; | |
| 72 | |
| 73 TEST(RefCountedTest, Constructors) { | |
| 74 bool was_destroyed; | |
| 75 | |
| 76 { | |
| 77 // Default. | |
| 78 RefPtr<MyClass> r; | |
| 79 EXPECT_TRUE(r.get() == nullptr); | |
| 80 EXPECT_FALSE(r); | |
| 81 } | |
| 82 | |
| 83 { | |
| 84 // Nullptr. | |
| 85 RefPtr<MyClass> r(nullptr); | |
| 86 EXPECT_TRUE(r.get() == nullptr); | |
| 87 EXPECT_FALSE(r); | |
| 88 } | |
| 89 | |
| 90 { | |
| 91 MyClass* created = nullptr; | |
| 92 was_destroyed = false; | |
| 93 // Adopt, then RVO. | |
| 94 RefPtr<MyClass> r(MakeRefCounted<MyClass>(&created, &was_destroyed)); | |
| 95 EXPECT_TRUE(created); | |
| 96 EXPECT_EQ(created, r.get()); | |
| 97 EXPECT_TRUE(r); | |
| 98 EXPECT_FALSE(was_destroyed); | |
| 99 } | |
| 100 EXPECT_TRUE(was_destroyed); | |
| 101 | |
| 102 { | |
| 103 MyClass* created = nullptr; | |
| 104 was_destroyed = false; | |
| 105 // Adopt, then move. | |
| 106 ALLOW_PESSIMIZING_MOVE(RefPtr<MyClass> r( | |
| 107 std::move(MakeRefCounted<MyClass>(&created, &was_destroyed)))) | |
| 108 EXPECT_TRUE(created); | |
| 109 EXPECT_EQ(created, r.get()); | |
| 110 EXPECT_TRUE(r); | |
| 111 EXPECT_FALSE(was_destroyed); | |
| 112 } | |
| 113 EXPECT_TRUE(was_destroyed); | |
| 114 | |
| 115 { | |
| 116 MyClass* created = nullptr; | |
| 117 was_destroyed = false; | |
| 118 RefPtr<MyClass> r1(MakeRefCounted<MyClass>(&created, &was_destroyed)); | |
| 119 // Copy. | |
| 120 RefPtr<MyClass> r2(r1); | |
| 121 EXPECT_TRUE(created); | |
| 122 EXPECT_EQ(created, r1.get()); | |
| 123 EXPECT_EQ(created, r2.get()); | |
| 124 EXPECT_TRUE(r1); | |
| 125 EXPECT_TRUE(r2); | |
| 126 EXPECT_FALSE(was_destroyed); | |
| 127 } | |
| 128 EXPECT_TRUE(was_destroyed); | |
| 129 | |
| 130 { | |
| 131 MyClass* created = nullptr; | |
| 132 was_destroyed = false; | |
| 133 RefPtr<MyClass> r1(MakeRefCounted<MyClass>(&created, &was_destroyed)); | |
| 134 // From raw pointer. | |
| 135 RefPtr<MyClass> r2(created); | |
| 136 EXPECT_TRUE(created); | |
| 137 EXPECT_EQ(created, r1.get()); | |
| 138 EXPECT_EQ(created, r2.get()); | |
| 139 EXPECT_TRUE(r1); | |
| 140 EXPECT_TRUE(r2); | |
| 141 EXPECT_FALSE(was_destroyed); | |
| 142 } | |
| 143 EXPECT_TRUE(was_destroyed); | |
| 144 | |
| 145 { | |
| 146 MySubclass* created = nullptr; | |
| 147 was_destroyed = false; | |
| 148 // Adopt, then "move". | |
| 149 RefPtr<MyClass> r(MakeRefCounted<MySubclass>(&created, &was_destroyed)); | |
| 150 EXPECT_TRUE(created); | |
| 151 EXPECT_EQ(static_cast<MyClass*>(created), r.get()); | |
| 152 EXPECT_TRUE(r); | |
| 153 EXPECT_FALSE(was_destroyed); | |
| 154 } | |
| 155 EXPECT_TRUE(was_destroyed); | |
| 156 | |
| 157 { | |
| 158 MySubclass* created = nullptr; | |
| 159 was_destroyed = false; | |
| 160 // Adopt, then "move". | |
| 161 ALLOW_PESSIMIZING_MOVE(RefPtr<MyClass> r( | |
| 162 std::move(MakeRefCounted<MySubclass>(&created, &was_destroyed)))) | |
| 163 EXPECT_TRUE(created); | |
| 164 EXPECT_EQ(static_cast<MyClass*>(created), r.get()); | |
| 165 EXPECT_TRUE(r); | |
| 166 EXPECT_FALSE(was_destroyed); | |
| 167 } | |
| 168 EXPECT_TRUE(was_destroyed); | |
| 169 | |
| 170 { | |
| 171 MySubclass* created = nullptr; | |
| 172 was_destroyed = false; | |
| 173 RefPtr<MySubclass> r1(MakeRefCounted<MySubclass>(&created, &was_destroyed)); | |
| 174 // "Copy". | |
| 175 RefPtr<MyClass> r2(r1); | |
| 176 EXPECT_TRUE(created); | |
| 177 EXPECT_EQ(static_cast<MyClass*>(created), r1.get()); | |
| 178 EXPECT_EQ(static_cast<MyClass*>(created), r2.get()); | |
| 179 EXPECT_TRUE(r1); | |
| 180 EXPECT_TRUE(r2); | |
| 181 EXPECT_FALSE(was_destroyed); | |
| 182 } | |
| 183 EXPECT_TRUE(was_destroyed); | |
| 184 | |
| 185 { | |
| 186 MySubclass* created = nullptr; | |
| 187 was_destroyed = false; | |
| 188 RefPtr<MySubclass> r1(MakeRefCounted<MySubclass>(&created, &was_destroyed)); | |
| 189 // From raw pointer. | |
| 190 RefPtr<MyClass> r2(created); | |
| 191 EXPECT_TRUE(created); | |
| 192 EXPECT_EQ(static_cast<MyClass*>(created), r1.get()); | |
| 193 EXPECT_EQ(static_cast<MyClass*>(created), r2.get()); | |
| 194 EXPECT_TRUE(r1); | |
| 195 EXPECT_TRUE(r2); | |
| 196 EXPECT_FALSE(was_destroyed); | |
| 197 } | |
| 198 EXPECT_TRUE(was_destroyed); | |
| 199 } | |
| 200 | |
| 201 TEST(RefCountedTest, NullAssignmentToNull) { | |
| 202 RefPtr<MyClass> r1; | |
| 203 // No-op null assignment using |nullptr|. | |
| 204 r1 = nullptr; | |
| 205 EXPECT_TRUE(r1.get() == nullptr); | |
| 206 EXPECT_FALSE(r1); | |
| 207 | |
| 208 RefPtr<MyClass> r2; | |
| 209 // No-op null assignment using copy constructor. | |
| 210 r1 = r2; | |
| 211 EXPECT_TRUE(r1.get() == nullptr); | |
| 212 EXPECT_TRUE(r2.get() == nullptr); | |
| 213 EXPECT_FALSE(r1); | |
| 214 EXPECT_FALSE(r2); | |
| 215 | |
| 216 // No-op null assignment using move constructor. | |
| 217 r1 = std::move(r2); | |
| 218 EXPECT_TRUE(r1.get() == nullptr); | |
| 219 EXPECT_TRUE(r2.get() == nullptr); | |
| 220 EXPECT_FALSE(r1); | |
| 221 EXPECT_FALSE(r2); | |
| 222 | |
| 223 RefPtr<MySubclass> r3; | |
| 224 // No-op null assignment using "copy" constructor. | |
| 225 r1 = r3; | |
| 226 EXPECT_TRUE(r1.get() == nullptr); | |
| 227 EXPECT_TRUE(r3.get() == nullptr); | |
| 228 EXPECT_FALSE(r1); | |
| 229 EXPECT_FALSE(r3); | |
| 230 | |
| 231 // No-op null assignment using "move" constructor. | |
| 232 r1 = std::move(r3); | |
| 233 EXPECT_TRUE(r1.get() == nullptr); | |
| 234 EXPECT_TRUE(r3.get() == nullptr); | |
| 235 EXPECT_FALSE(r1); | |
| 236 EXPECT_FALSE(r3); | |
| 237 } | |
| 238 | |
| 239 TEST(RefCountedTest, NonNullAssignmentToNull) { | |
| 240 bool was_destroyed; | |
| 241 | |
| 242 { | |
| 243 MyClass* created = nullptr; | |
| 244 was_destroyed = false; | |
| 245 RefPtr<MyClass> r1(MakeRefCounted<MyClass>(&created, &was_destroyed)); | |
| 246 RefPtr<MyClass> r2; | |
| 247 // Copy assignment (to null ref pointer). | |
| 248 r2 = r1; | |
| 249 EXPECT_EQ(created, r1.get()); | |
| 250 EXPECT_EQ(created, r2.get()); | |
| 251 EXPECT_TRUE(r1); | |
| 252 EXPECT_TRUE(r2); | |
| 253 EXPECT_FALSE(was_destroyed); | |
| 254 } | |
| 255 EXPECT_TRUE(was_destroyed); | |
| 256 | |
| 257 { | |
| 258 MyClass* created = nullptr; | |
| 259 was_destroyed = false; | |
| 260 RefPtr<MyClass> r1(MakeRefCounted<MyClass>(&created, &was_destroyed)); | |
| 261 RefPtr<MyClass> r2; | |
| 262 // Move assignment (to null ref pointer). | |
| 263 r2 = std::move(r1); | |
| 264 EXPECT_TRUE(r1.get() == nullptr); | |
| 265 EXPECT_EQ(created, r2.get()); | |
| 266 EXPECT_FALSE(r1); | |
| 267 EXPECT_TRUE(r2); | |
| 268 EXPECT_FALSE(was_destroyed); | |
| 269 } | |
| 270 EXPECT_TRUE(was_destroyed); | |
| 271 | |
| 272 { | |
| 273 MySubclass* created = nullptr; | |
| 274 was_destroyed = false; | |
| 275 RefPtr<MySubclass> r1(MakeRefCounted<MySubclass>(&created, &was_destroyed)); | |
| 276 RefPtr<MyClass> r2; | |
| 277 // "Copy" assignment (to null ref pointer). | |
| 278 r2 = r1; | |
| 279 EXPECT_EQ(created, r1.get()); | |
| 280 EXPECT_EQ(static_cast<MyClass*>(created), r2.get()); | |
| 281 EXPECT_TRUE(r1); | |
| 282 EXPECT_TRUE(r2); | |
| 283 EXPECT_FALSE(was_destroyed); | |
| 284 } | |
| 285 EXPECT_TRUE(was_destroyed); | |
| 286 | |
| 287 { | |
| 288 MySubclass* created = nullptr; | |
| 289 was_destroyed = false; | |
| 290 RefPtr<MySubclass> r1(MakeRefCounted<MySubclass>(&created, &was_destroyed)); | |
| 291 RefPtr<MyClass> r2; | |
| 292 // "Move" assignment (to null ref pointer). | |
| 293 r2 = std::move(r1); | |
| 294 EXPECT_TRUE(r1.get() == nullptr); | |
| 295 EXPECT_EQ(static_cast<MyClass*>(created), r2.get()); | |
| 296 EXPECT_FALSE(r1); | |
| 297 EXPECT_TRUE(r2); | |
| 298 EXPECT_FALSE(was_destroyed); | |
| 299 } | |
| 300 EXPECT_TRUE(was_destroyed); | |
| 301 } | |
| 302 | |
| 303 TEST(RefCountedTest, NullAssignmentToNonNull) { | |
| 304 bool was_destroyed = false; | |
| 305 RefPtr<MyClass> r1(MakeRefCounted<MyClass>(nullptr, &was_destroyed)); | |
| 306 // Null assignment (to non-null ref pointer) using |nullptr|. | |
| 307 r1 = nullptr; | |
| 308 EXPECT_TRUE(r1.get() == nullptr); | |
| 309 EXPECT_FALSE(r1); | |
| 310 EXPECT_TRUE(was_destroyed); | |
| 311 | |
| 312 was_destroyed = false; | |
| 313 r1 = MakeRefCounted<MyClass>(nullptr, &was_destroyed); | |
| 314 RefPtr<MyClass> r2; | |
| 315 // Null assignment (to non-null ref pointer) using copy constructor. | |
| 316 r1 = r2; | |
| 317 EXPECT_TRUE(r1.get() == nullptr); | |
| 318 EXPECT_TRUE(r2.get() == nullptr); | |
| 319 EXPECT_FALSE(r1); | |
| 320 EXPECT_FALSE(r2); | |
| 321 EXPECT_TRUE(was_destroyed); | |
| 322 | |
| 323 was_destroyed = false; | |
| 324 r1 = MakeRefCounted<MyClass>(nullptr, &was_destroyed); | |
| 325 // Null assignment using move constructor. | |
| 326 r1 = std::move(r2); | |
| 327 EXPECT_TRUE(r1.get() == nullptr); | |
| 328 EXPECT_TRUE(r2.get() == nullptr); | |
| 329 EXPECT_FALSE(r1); | |
| 330 EXPECT_FALSE(r2); | |
| 331 EXPECT_TRUE(was_destroyed); | |
| 332 | |
| 333 was_destroyed = false; | |
| 334 r1 = MakeRefCounted<MyClass>(nullptr, &was_destroyed); | |
| 335 RefPtr<MySubclass> r3; | |
| 336 // Null assignment (to non-null ref pointer) using "copy" constructor. | |
| 337 r1 = r3; | |
| 338 EXPECT_TRUE(r1.get() == nullptr); | |
| 339 EXPECT_TRUE(r3.get() == nullptr); | |
| 340 EXPECT_FALSE(r1); | |
| 341 EXPECT_FALSE(r3); | |
| 342 EXPECT_TRUE(was_destroyed); | |
| 343 | |
| 344 was_destroyed = false; | |
| 345 r1 = MakeRefCounted<MyClass>(nullptr, &was_destroyed); | |
| 346 // Null assignment (to non-null ref pointer) using "move" constructor. | |
| 347 r1 = std::move(r3); | |
| 348 EXPECT_TRUE(r1.get() == nullptr); | |
| 349 EXPECT_TRUE(r3.get() == nullptr); | |
| 350 EXPECT_FALSE(r1); | |
| 351 EXPECT_FALSE(r3); | |
| 352 EXPECT_TRUE(was_destroyed); | |
| 353 } | |
| 354 | |
| 355 TEST(RefCountedTest, NonNullAssignmentToNonNull) { | |
| 356 bool was_destroyed1; | |
| 357 bool was_destroyed2; | |
| 358 | |
| 359 { | |
| 360 was_destroyed1 = false; | |
| 361 was_destroyed2 = false; | |
| 362 RefPtr<MyClass> r1(MakeRefCounted<MyClass>(nullptr, &was_destroyed1)); | |
| 363 RefPtr<MyClass> r2(MakeRefCounted<MyClass>(nullptr, &was_destroyed2)); | |
| 364 // Copy assignment (to non-null ref pointer). | |
| 365 r2 = r1; | |
| 366 EXPECT_EQ(r1.get(), r2.get()); | |
| 367 EXPECT_TRUE(r1); | |
| 368 EXPECT_TRUE(r2); | |
| 369 EXPECT_FALSE(was_destroyed1); | |
| 370 EXPECT_TRUE(was_destroyed2); | |
| 371 } | |
| 372 EXPECT_TRUE(was_destroyed1); | |
| 373 | |
| 374 { | |
| 375 was_destroyed1 = false; | |
| 376 was_destroyed2 = false; | |
| 377 RefPtr<MyClass> r1(MakeRefCounted<MyClass>(nullptr, &was_destroyed1)); | |
| 378 RefPtr<MyClass> r2(MakeRefCounted<MyClass>(nullptr, &was_destroyed2)); | |
| 379 // Move assignment (to non-null ref pointer). | |
| 380 r2 = std::move(r1); | |
| 381 EXPECT_TRUE(r1.get() == nullptr); | |
| 382 EXPECT_FALSE(r2.get() == nullptr); | |
| 383 EXPECT_FALSE(r1); | |
| 384 EXPECT_TRUE(r2); | |
| 385 EXPECT_FALSE(was_destroyed1); | |
| 386 EXPECT_TRUE(was_destroyed2); | |
| 387 } | |
| 388 EXPECT_TRUE(was_destroyed1); | |
| 389 | |
| 390 { | |
| 391 was_destroyed1 = false; | |
| 392 was_destroyed2 = false; | |
| 393 RefPtr<MySubclass> r1(MakeRefCounted<MySubclass>(nullptr, &was_destroyed1)); | |
| 394 RefPtr<MyClass> r2(MakeRefCounted<MyClass>(nullptr, &was_destroyed2)); | |
| 395 // "Copy" assignment (to non-null ref pointer). | |
| 396 r2 = r1; | |
| 397 EXPECT_EQ(r1.get(), r2.get()); | |
| 398 EXPECT_TRUE(r1); | |
| 399 EXPECT_TRUE(r2); | |
| 400 EXPECT_FALSE(was_destroyed1); | |
| 401 EXPECT_TRUE(was_destroyed2); | |
| 402 } | |
| 403 EXPECT_TRUE(was_destroyed1); | |
| 404 | |
| 405 { | |
| 406 was_destroyed1 = false; | |
| 407 was_destroyed2 = false; | |
| 408 RefPtr<MySubclass> r1(MakeRefCounted<MySubclass>(nullptr, &was_destroyed1)); | |
| 409 RefPtr<MyClass> r2(MakeRefCounted<MyClass>(nullptr, &was_destroyed2)); | |
| 410 // Move assignment (to non-null ref pointer). | |
| 411 r2 = std::move(r1); | |
| 412 EXPECT_TRUE(r1.get() == nullptr); | |
| 413 EXPECT_FALSE(r2.get() == nullptr); | |
| 414 EXPECT_FALSE(r1); | |
| 415 EXPECT_TRUE(r2); | |
| 416 EXPECT_FALSE(was_destroyed1); | |
| 417 EXPECT_TRUE(was_destroyed2); | |
| 418 } | |
| 419 EXPECT_TRUE(was_destroyed1); | |
| 420 } | |
| 421 | |
| 422 TEST(RefCountedTest, SelfAssignment) { | |
| 423 bool was_destroyed; | |
| 424 | |
| 425 { | |
| 426 MyClass* created = nullptr; | |
| 427 was_destroyed = false; | |
| 428 RefPtr<MyClass> r(MakeRefCounted<MyClass>(&created, &was_destroyed)); | |
| 429 // Copy. | |
| 430 r = r; | |
| 431 EXPECT_EQ(created, r.get()); | |
| 432 EXPECT_FALSE(was_destroyed); | |
| 433 } | |
| 434 EXPECT_TRUE(was_destroyed); | |
| 435 | |
| 436 { | |
| 437 MyClass* created = nullptr; | |
| 438 was_destroyed = false; | |
| 439 RefPtr<MyClass> r(MakeRefCounted<MyClass>(&created, &was_destroyed)); | |
| 440 // Move. | |
| 441 ALLOW_SELF_MOVE(r = std::move(r)) | |
| 442 EXPECT_EQ(created, r.get()); | |
| 443 EXPECT_FALSE(was_destroyed); | |
| 444 } | |
| 445 EXPECT_TRUE(was_destroyed); | |
| 446 } | |
| 447 | |
| 448 TEST(RefCountedTest, Swap) { | |
| 449 MyClass* created1 = nullptr; | |
| 450 bool was_destroyed1 = false; | |
| 451 RefPtr<MyClass> r1(MakeRefCounted<MyClass>(&created1, &was_destroyed1)); | |
| 452 EXPECT_TRUE(created1); | |
| 453 EXPECT_EQ(created1, r1.get()); | |
| 454 | |
| 455 MyClass* created2 = nullptr; | |
| 456 bool was_destroyed2 = false; | |
| 457 RefPtr<MyClass> r2(MakeRefCounted<MyClass>(&created2, &was_destroyed2)); | |
| 458 EXPECT_TRUE(created2); | |
| 459 EXPECT_EQ(created2, r2.get()); | |
| 460 EXPECT_NE(created1, created2); | |
| 461 | |
| 462 r1.swap(r2); | |
| 463 EXPECT_EQ(created2, r1.get()); | |
| 464 EXPECT_EQ(created1, r2.get()); | |
| 465 } | |
| 466 | |
| 467 TEST(RefCountedTest, GetAndDereferenceOperators) { | |
| 468 // Note: We check here that .get(), operator*, and operator-> are const, but | |
| 469 // return non-const pointers/refs. | |
| 470 | |
| 471 MyClass* created = nullptr; | |
| 472 const RefPtr<MyClass> r(MakeRefCounted<MyClass>(&created, nullptr)); | |
| 473 MyClass* ptr = r.get(); // Assign to non-const pointer. | |
| 474 EXPECT_EQ(created, ptr); | |
| 475 ptr = r.operator->(); // Assign to non-const pointer. | |
| 476 EXPECT_EQ(created, ptr); | |
| 477 MyClass& ref = *r; // "Assign" to non-const reference. | |
| 478 EXPECT_EQ(created, &ref); | |
| 479 } | |
| 480 | |
| 481 // You can manually call |AddRef()| and |Release()| if you want. | |
| 482 TEST(RefCountedTest, AddRefRelease) { | |
| 483 MyClass* created = nullptr; | |
| 484 bool was_destroyed = false; | |
| 485 { | |
| 486 RefPtr<MyClass> r(MakeRefCounted<MyClass>(&created, &was_destroyed)); | |
| 487 EXPECT_EQ(created, r.get()); | |
| 488 created->AddRef(); | |
| 489 } | |
| 490 EXPECT_FALSE(was_destroyed); | |
| 491 created->Release(); | |
| 492 EXPECT_TRUE(was_destroyed); | |
| 493 } | |
| 494 | |
| 495 TEST(RefCountedTest, Mix) { | |
| 496 MySubclass* created = nullptr; | |
| 497 bool was_destroyed = false; | |
| 498 RefPtr<MySubclass> r1(MakeRefCounted<MySubclass>(&created, &was_destroyed)); | |
| 499 ASSERT_FALSE(was_destroyed); | |
| 500 created->AssertHasOneRef(); | |
| 501 | |
| 502 RefPtr<MySubclass> r2 = r1; | |
| 503 ASSERT_FALSE(was_destroyed); | |
| 504 | |
| 505 r1 = nullptr; | |
| 506 ASSERT_FALSE(was_destroyed); | |
| 507 created->AssertHasOneRef(); | |
| 508 | |
| 509 { | |
| 510 RefPtr<MyClass> r3 = r2; | |
| 511 { | |
| 512 RefPtr<MyClass> r4(r3); | |
| 513 r2 = nullptr; | |
| 514 ASSERT_FALSE(was_destroyed); | |
| 515 } | |
| 516 ASSERT_FALSE(was_destroyed); | |
| 517 created->AssertHasOneRef(); | |
| 518 | |
| 519 r1 = RefPtr<MySubclass>(static_cast<MySubclass*>(r3.get())); | |
| 520 ASSERT_FALSE(was_destroyed); | |
| 521 } | |
| 522 ASSERT_FALSE(was_destroyed); | |
| 523 created->AssertHasOneRef(); | |
| 524 | |
| 525 EXPECT_EQ(created, r1.get()); | |
| 526 | |
| 527 r1 = nullptr; | |
| 528 EXPECT_TRUE(was_destroyed); | |
| 529 } | |
| 530 | |
| 531 class MyPublicClass : public RefCountedThreadSafe<MyPublicClass> { | |
| 532 public: | |
| 533 // Overloaded constructors work with |MakeRefCounted()|. | |
| 534 MyPublicClass() : has_num_(false), num_(0) {} | |
| 535 explicit MyPublicClass(int num) : has_num_(true), num_(num) {} | |
| 536 | |
| 537 ~MyPublicClass() {} | |
| 538 | |
| 539 bool has_num() const { return has_num_; } | |
| 540 int num() const { return num_; } | |
| 541 | |
| 542 private: | |
| 543 bool has_num_; | |
| 544 int num_; | |
| 545 | |
| 546 MOJO_DISALLOW_COPY_AND_ASSIGN(MyPublicClass); | |
| 547 }; | |
| 548 | |
| 549 // You can also just keep constructors and destructors public. Make sure that | |
| 550 // works (mostly that it compiles). | |
| 551 TEST(RefCountedTest, PublicCtorAndDtor) { | |
| 552 RefPtr<MyPublicClass> r1 = MakeRefCounted<MyPublicClass>(); | |
| 553 ASSERT_TRUE(r1); | |
| 554 EXPECT_FALSE(r1->has_num()); | |
| 555 | |
| 556 RefPtr<MyPublicClass> r2 = MakeRefCounted<MyPublicClass>(123); | |
| 557 ASSERT_TRUE(r2); | |
| 558 EXPECT_TRUE(r2->has_num()); | |
| 559 EXPECT_EQ(123, r2->num()); | |
| 560 EXPECT_NE(r1.get(), r2.get()); | |
| 561 | |
| 562 r1 = r2; | |
| 563 EXPECT_TRUE(r1->has_num()); | |
| 564 EXPECT_EQ(123, r1->num()); | |
| 565 EXPECT_EQ(r1.get(), r2.get()); | |
| 566 | |
| 567 r2 = nullptr; | |
| 568 EXPECT_FALSE(r2); | |
| 569 EXPECT_TRUE(r1->has_num()); | |
| 570 EXPECT_EQ(123, r1->num()); | |
| 571 | |
| 572 r1 = nullptr; | |
| 573 EXPECT_FALSE(r1); | |
| 574 } | |
| 575 | |
| 576 #ifndef NDEBUG | |
| 577 #if defined(OS_ANDROID) | |
| 578 // TODO(vtl): On Android, death tests don't seem to work properly with | |
| 579 // |assert()| (which presumably calls |abort()|. | |
| 580 #define MAYBE_DebugChecks DISABLED_DebugChecks | |
| 581 #else | |
| 582 #define MAYBE_DebugChecks DebugChecks | |
| 583 #endif | |
| 584 // The danger with having a public constructor or destructor is that certain | |
| 585 // things will compile. You should get some protection by assertions in Debug | |
| 586 // builds. | |
| 587 TEST(RefCountedTest, MAYBE_DebugChecks) { | |
| 588 { | |
| 589 MyPublicClass* p = new MyPublicClass(); | |
| 590 EXPECT_DEATH_IF_SUPPORTED(delete p, "!adoption_required_"); | |
| 591 } | |
| 592 | |
| 593 { | |
| 594 MyPublicClass* p = new MyPublicClass(); | |
| 595 EXPECT_DEATH_IF_SUPPORTED(RefPtr<MyPublicClass> r(p), | |
| 596 "!adoption_required_"); | |
| 597 } | |
| 598 | |
| 599 { | |
| 600 RefPtr<MyPublicClass> r(MakeRefCounted<MyPublicClass>()); | |
| 601 EXPECT_DEATH_IF_SUPPORTED(delete r.get(), "destruction_started_"); | |
| 602 } | |
| 603 } | |
| 604 #endif | |
| 605 | |
| 606 // TODO(vtl): Add (threaded) stress tests. | |
| 607 | |
| 608 } // namespace | |
| 609 } // namespace system | |
| 610 } // namespace mojo | |
| OLD | NEW |