perkj | 0489e49 | 2016-10-20 00:24:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include <string> |
| 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "rtc_base/gunit.h" |
| 14 | #include "rtc_base/refcount.h" |
Niels Möller | 84255bb | 2017-10-06 13:43:23 +0200 | [diff] [blame] | 15 | #include "rtc_base/refcountedobject.h" |
perkj | 0489e49 | 2016-10-20 00:24:01 -0700 | [diff] [blame] | 16 | |
| 17 | namespace rtc { |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | class A { |
| 22 | public: |
| 23 | A() {} |
| 24 | |
| 25 | private: |
| 26 | RTC_DISALLOW_COPY_AND_ASSIGN(A); |
| 27 | }; |
| 28 | |
| 29 | class RefClass : public RefCountInterface { |
| 30 | public: |
| 31 | RefClass() {} |
| 32 | |
| 33 | protected: |
ehmaldonado | da8dcfb | 2017-01-04 07:11:23 -0800 | [diff] [blame] | 34 | ~RefClass() override {} |
perkj | 0489e49 | 2016-10-20 00:24:01 -0700 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | class RefClassWithRvalue : public RefCountInterface { |
| 38 | public: |
| 39 | explicit RefClassWithRvalue(std::unique_ptr<A> a) : a_(std::move(a)) {} |
| 40 | |
| 41 | protected: |
ehmaldonado | da8dcfb | 2017-01-04 07:11:23 -0800 | [diff] [blame] | 42 | ~RefClassWithRvalue() override {} |
perkj | 0489e49 | 2016-10-20 00:24:01 -0700 | [diff] [blame] | 43 | |
| 44 | public: |
| 45 | std::unique_ptr<A> a_; |
| 46 | }; |
| 47 | |
| 48 | class RefClassWithMixedValues : public RefCountInterface { |
| 49 | public: |
| 50 | RefClassWithMixedValues(std::unique_ptr<A> a, int b, const std::string& c) |
| 51 | : a_(std::move(a)), b_(b), c_(c) {} |
| 52 | |
| 53 | protected: |
ehmaldonado | da8dcfb | 2017-01-04 07:11:23 -0800 | [diff] [blame] | 54 | ~RefClassWithMixedValues() override {} |
perkj | 0489e49 | 2016-10-20 00:24:01 -0700 | [diff] [blame] | 55 | |
| 56 | public: |
| 57 | std::unique_ptr<A> a_; |
| 58 | int b_; |
| 59 | std::string c_; |
| 60 | }; |
| 61 | |
| 62 | } // namespace |
| 63 | |
Niels Möller | 6f72f56 | 2017-10-19 13:15:17 +0200 | [diff] [blame] | 64 | TEST(RefCountedObject, HasOneRef) { |
perkj | 0489e49 | 2016-10-20 00:24:01 -0700 | [diff] [blame] | 65 | scoped_refptr<RefCountedObject<RefClass>> aref( |
| 66 | new RefCountedObject<RefClass>()); |
| 67 | EXPECT_TRUE(aref->HasOneRef()); |
Niels Möller | 6f72f56 | 2017-10-19 13:15:17 +0200 | [diff] [blame] | 68 | aref->AddRef(); |
| 69 | EXPECT_FALSE(aref->HasOneRef()); |
| 70 | EXPECT_EQ(aref->Release(), RefCountReleaseStatus::kOtherRefsRemained); |
| 71 | EXPECT_TRUE(aref->HasOneRef()); |
perkj | 0489e49 | 2016-10-20 00:24:01 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | TEST(RefCountedObject, SupportRValuesInCtor) { |
| 75 | std::unique_ptr<A> a(new A()); |
| 76 | scoped_refptr<RefClassWithRvalue> ref( |
| 77 | new RefCountedObject<RefClassWithRvalue>(std::move(a))); |
| 78 | EXPECT_TRUE(ref->a_.get() != nullptr); |
| 79 | EXPECT_TRUE(a.get() == nullptr); |
| 80 | } |
| 81 | |
| 82 | TEST(RefCountedObject, SupportMixedTypesInCtor) { |
| 83 | std::unique_ptr<A> a(new A()); |
| 84 | int b = 9; |
| 85 | std::string c = "hello"; |
| 86 | scoped_refptr<RefClassWithMixedValues> ref( |
| 87 | new RefCountedObject<RefClassWithMixedValues>(std::move(a), b, c)); |
| 88 | EXPECT_TRUE(ref->a_.get() != nullptr); |
| 89 | EXPECT_TRUE(a.get() == nullptr); |
| 90 | EXPECT_EQ(b, ref->b_); |
| 91 | EXPECT_EQ(c, ref->c_); |
| 92 | } |
| 93 | |
| 94 | } // namespace rtc |