blob: 688b9215c11188e98056ac049142bc920f34eb5c [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"
perkj0489e492016-10-20 00:24:01 -070015
16namespace rtc {
17
18namespace {
19
20class A {
21 public:
22 A() {}
23
24 private:
25 RTC_DISALLOW_COPY_AND_ASSIGN(A);
26};
27
28class RefClass : public RefCountInterface {
29 public:
30 RefClass() {}
31
32 protected:
ehmaldonadoda8dcfb2017-01-04 07:11:23 -080033 ~RefClass() override {}
perkj0489e492016-10-20 00:24:01 -070034};
35
36class RefClassWithRvalue : public RefCountInterface {
37 public:
38 explicit RefClassWithRvalue(std::unique_ptr<A> a) : a_(std::move(a)) {}
39
40 protected:
ehmaldonadoda8dcfb2017-01-04 07:11:23 -080041 ~RefClassWithRvalue() override {}
perkj0489e492016-10-20 00:24:01 -070042
43 public:
44 std::unique_ptr<A> a_;
45};
46
47class 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:
ehmaldonadoda8dcfb2017-01-04 07:11:23 -080053 ~RefClassWithMixedValues() override {}
perkj0489e492016-10-20 00:24:01 -070054
55 public:
56 std::unique_ptr<A> a_;
57 int b_;
58 std::string c_;
59};
60
61} // namespace
62
63TEST(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
71TEST(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
79TEST(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