blob: 4744525af60af0c5b45ba2c967fa376b76578c2f [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
11#include <string>
12
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "rtc_base/gunit.h"
14#include "rtc_base/refcount.h"
Niels Möller84255bb2017-10-06 13:43:23 +020015#include "rtc_base/refcountedobject.h"
perkj0489e492016-10-20 00:24:01 -070016
17namespace rtc {
18
19namespace {
20
21class A {
22 public:
23 A() {}
24
25 private:
26 RTC_DISALLOW_COPY_AND_ASSIGN(A);
27};
28
29class RefClass : public RefCountInterface {
30 public:
31 RefClass() {}
32
33 protected:
ehmaldonadoda8dcfb2017-01-04 07:11:23 -080034 ~RefClass() override {}
perkj0489e492016-10-20 00:24:01 -070035};
36
37class RefClassWithRvalue : public RefCountInterface {
38 public:
39 explicit RefClassWithRvalue(std::unique_ptr<A> a) : a_(std::move(a)) {}
40
41 protected:
ehmaldonadoda8dcfb2017-01-04 07:11:23 -080042 ~RefClassWithRvalue() override {}
perkj0489e492016-10-20 00:24:01 -070043
44 public:
45 std::unique_ptr<A> a_;
46};
47
48class 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:
ehmaldonadoda8dcfb2017-01-04 07:11:23 -080054 ~RefClassWithMixedValues() override {}
perkj0489e492016-10-20 00:24:01 -070055
56 public:
57 std::unique_ptr<A> a_;
58 int b_;
59 std::string c_;
60};
61
62} // namespace
63
Niels Möller6f72f562017-10-19 13:15:17 +020064TEST(RefCountedObject, HasOneRef) {
perkj0489e492016-10-20 00:24:01 -070065 scoped_refptr<RefCountedObject<RefClass>> aref(
66 new RefCountedObject<RefClass>());
67 EXPECT_TRUE(aref->HasOneRef());
Niels Möller6f72f562017-10-19 13:15:17 +020068 aref->AddRef();
69 EXPECT_FALSE(aref->HasOneRef());
70 EXPECT_EQ(aref->Release(), RefCountReleaseStatus::kOtherRefsRemained);
71 EXPECT_TRUE(aref->HasOneRef());
perkj0489e492016-10-20 00:24:01 -070072}
73
74TEST(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
82TEST(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