blob: 9b475e2102973e0f4d5c8ac5f4b676c0b9658f10 [file] [log] [blame]
perkj0489e492016-10-20 00:24:01 -07001/*
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
Yves Gerey3e707812018-11-28 16:47:49 +010011#include <memory>
perkj0489e492016-10-20 00:24:01 -070012#include <string>
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <utility>
perkj0489e492016-10-20 00:24:01 -070014
Steve Anton10542f22019-01-11 09:11:00 -080015#include "rtc_base/ref_count.h"
16#include "rtc_base/ref_counted_object.h"
Yves Gerey3e707812018-11-28 16:47:49 +010017#include "rtc_base/scoped_ref_ptr.h"
18#include "test/gtest.h"
perkj0489e492016-10-20 00:24:01 -070019
20namespace rtc {
21
22namespace {
23
24class A {
25 public:
26 A() {}
27
28 private:
29 RTC_DISALLOW_COPY_AND_ASSIGN(A);
30};
31
32class RefClass : public RefCountInterface {
33 public:
34 RefClass() {}
35
36 protected:
ehmaldonadoda8dcfb2017-01-04 07:11:23 -080037 ~RefClass() override {}
perkj0489e492016-10-20 00:24:01 -070038};
39
40class RefClassWithRvalue : public RefCountInterface {
41 public:
42 explicit RefClassWithRvalue(std::unique_ptr<A> a) : a_(std::move(a)) {}
43
44 protected:
ehmaldonadoda8dcfb2017-01-04 07:11:23 -080045 ~RefClassWithRvalue() override {}
perkj0489e492016-10-20 00:24:01 -070046
47 public:
48 std::unique_ptr<A> a_;
49};
50
51class RefClassWithMixedValues : public RefCountInterface {
52 public:
53 RefClassWithMixedValues(std::unique_ptr<A> a, int b, const std::string& c)
54 : a_(std::move(a)), b_(b), c_(c) {}
55
56 protected:
ehmaldonadoda8dcfb2017-01-04 07:11:23 -080057 ~RefClassWithMixedValues() override {}
perkj0489e492016-10-20 00:24:01 -070058
59 public:
60 std::unique_ptr<A> a_;
61 int b_;
62 std::string c_;
63};
64
65} // namespace
66
Niels Möller6f72f562017-10-19 13:15:17 +020067TEST(RefCountedObject, HasOneRef) {
perkj0489e492016-10-20 00:24:01 -070068 scoped_refptr<RefCountedObject<RefClass>> aref(
69 new RefCountedObject<RefClass>());
70 EXPECT_TRUE(aref->HasOneRef());
Niels Möller6f72f562017-10-19 13:15:17 +020071 aref->AddRef();
72 EXPECT_FALSE(aref->HasOneRef());
73 EXPECT_EQ(aref->Release(), RefCountReleaseStatus::kOtherRefsRemained);
74 EXPECT_TRUE(aref->HasOneRef());
perkj0489e492016-10-20 00:24:01 -070075}
76
77TEST(RefCountedObject, SupportRValuesInCtor) {
78 std::unique_ptr<A> a(new A());
79 scoped_refptr<RefClassWithRvalue> ref(
80 new RefCountedObject<RefClassWithRvalue>(std::move(a)));
81 EXPECT_TRUE(ref->a_.get() != nullptr);
82 EXPECT_TRUE(a.get() == nullptr);
83}
84
85TEST(RefCountedObject, SupportMixedTypesInCtor) {
86 std::unique_ptr<A> a(new A());
87 int b = 9;
88 std::string c = "hello";
89 scoped_refptr<RefClassWithMixedValues> ref(
90 new RefCountedObject<RefClassWithMixedValues>(std::move(a), b, c));
91 EXPECT_TRUE(ref->a_.get() != nullptr);
92 EXPECT_TRUE(a.get() == nullptr);
93 EXPECT_EQ(b, ref->b_);
94 EXPECT_EQ(c, ref->c_);
95}
96
97} // namespace rtc