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" |
perkj | 0489e49 | 2016-10-20 00:24:01 -0700 | [diff] [blame] | 15 | |
| 16 | namespace rtc { |
| 17 | |
| 18 | namespace { |
| 19 | |
| 20 | class A { |
| 21 | public: |
| 22 | A() {} |
| 23 | |
| 24 | private: |
| 25 | RTC_DISALLOW_COPY_AND_ASSIGN(A); |
| 26 | }; |
| 27 | |
| 28 | class RefClass : public RefCountInterface { |
| 29 | public: |
| 30 | RefClass() {} |
| 31 | |
| 32 | protected: |
ehmaldonado | da8dcfb | 2017-01-04 07:11:23 -0800 | [diff] [blame] | 33 | ~RefClass() override {} |
perkj | 0489e49 | 2016-10-20 00:24:01 -0700 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | class RefClassWithRvalue : public RefCountInterface { |
| 37 | public: |
| 38 | explicit RefClassWithRvalue(std::unique_ptr<A> a) : a_(std::move(a)) {} |
| 39 | |
| 40 | protected: |
ehmaldonado | da8dcfb | 2017-01-04 07:11:23 -0800 | [diff] [blame] | 41 | ~RefClassWithRvalue() override {} |
perkj | 0489e49 | 2016-10-20 00:24:01 -0700 | [diff] [blame] | 42 | |
| 43 | public: |
| 44 | std::unique_ptr<A> a_; |
| 45 | }; |
| 46 | |
| 47 | class RefClassWithMixedValues : public RefCountInterface { |
| 48 | public: |
| 49 | RefClassWithMixedValues(std::unique_ptr<A> a, int b, const std::string& c) |
| 50 | : a_(std::move(a)), b_(b), c_(c) {} |
| 51 | |
| 52 | protected: |
ehmaldonado | da8dcfb | 2017-01-04 07:11:23 -0800 | [diff] [blame] | 53 | ~RefClassWithMixedValues() override {} |
perkj | 0489e49 | 2016-10-20 00:24:01 -0700 | [diff] [blame] | 54 | |
| 55 | public: |
| 56 | std::unique_ptr<A> a_; |
| 57 | int b_; |
| 58 | std::string c_; |
| 59 | }; |
| 60 | |
| 61 | } // namespace |
| 62 | |
| 63 | TEST(RefCountedObject, Basic) { |
| 64 | scoped_refptr<RefCountedObject<RefClass>> aref( |
| 65 | new RefCountedObject<RefClass>()); |
| 66 | EXPECT_TRUE(aref->HasOneRef()); |
| 67 | EXPECT_EQ(2, aref->AddRef()); |
| 68 | EXPECT_EQ(1, aref->Release()); |
| 69 | } |
| 70 | |
| 71 | TEST(RefCountedObject, SupportRValuesInCtor) { |
| 72 | std::unique_ptr<A> a(new A()); |
| 73 | scoped_refptr<RefClassWithRvalue> ref( |
| 74 | new RefCountedObject<RefClassWithRvalue>(std::move(a))); |
| 75 | EXPECT_TRUE(ref->a_.get() != nullptr); |
| 76 | EXPECT_TRUE(a.get() == nullptr); |
| 77 | } |
| 78 | |
| 79 | TEST(RefCountedObject, SupportMixedTypesInCtor) { |
| 80 | std::unique_ptr<A> a(new A()); |
| 81 | int b = 9; |
| 82 | std::string c = "hello"; |
| 83 | scoped_refptr<RefClassWithMixedValues> ref( |
| 84 | new RefCountedObject<RefClassWithMixedValues>(std::move(a), b, c)); |
| 85 | EXPECT_TRUE(ref->a_.get() != nullptr); |
| 86 | EXPECT_TRUE(a.get() == nullptr); |
| 87 | EXPECT_EQ(b, ref->b_); |
| 88 | EXPECT_EQ(c, ref->c_); |
| 89 | } |
| 90 | |
| 91 | } // namespace rtc |