1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker * Copyright 2019 The WebRTC Project Authors. All rights reserved.
3*d9f75844SAndroid Build Coastguard Worker *
4*d9f75844SAndroid Build Coastguard Worker * Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker * that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker * tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker * in the file PATENTS. All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker * be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker */
10*d9f75844SAndroid Build Coastguard Worker
11*d9f75844SAndroid Build Coastguard Worker #include "api/scoped_refptr.h"
12*d9f75844SAndroid Build Coastguard Worker
13*d9f75844SAndroid Build Coastguard Worker #include <utility>
14*d9f75844SAndroid Build Coastguard Worker #include <vector>
15*d9f75844SAndroid Build Coastguard Worker
16*d9f75844SAndroid Build Coastguard Worker #include "test/gtest.h"
17*d9f75844SAndroid Build Coastguard Worker
18*d9f75844SAndroid Build Coastguard Worker namespace rtc {
19*d9f75844SAndroid Build Coastguard Worker namespace {
20*d9f75844SAndroid Build Coastguard Worker
21*d9f75844SAndroid Build Coastguard Worker struct FunctionsCalled {
22*d9f75844SAndroid Build Coastguard Worker int addref = 0;
23*d9f75844SAndroid Build Coastguard Worker int release = 0;
24*d9f75844SAndroid Build Coastguard Worker };
25*d9f75844SAndroid Build Coastguard Worker
26*d9f75844SAndroid Build Coastguard Worker class ScopedRefCounted {
27*d9f75844SAndroid Build Coastguard Worker public:
ScopedRefCounted(FunctionsCalled * called)28*d9f75844SAndroid Build Coastguard Worker explicit ScopedRefCounted(FunctionsCalled* called) : called_(*called) {}
29*d9f75844SAndroid Build Coastguard Worker ScopedRefCounted(const ScopedRefCounted&) = delete;
30*d9f75844SAndroid Build Coastguard Worker ScopedRefCounted& operator=(const ScopedRefCounted&) = delete;
31*d9f75844SAndroid Build Coastguard Worker
AddRef()32*d9f75844SAndroid Build Coastguard Worker void AddRef() {
33*d9f75844SAndroid Build Coastguard Worker ++called_.addref;
34*d9f75844SAndroid Build Coastguard Worker ++ref_count_;
35*d9f75844SAndroid Build Coastguard Worker }
Release()36*d9f75844SAndroid Build Coastguard Worker void Release() {
37*d9f75844SAndroid Build Coastguard Worker ++called_.release;
38*d9f75844SAndroid Build Coastguard Worker if (0 == --ref_count_)
39*d9f75844SAndroid Build Coastguard Worker delete this;
40*d9f75844SAndroid Build Coastguard Worker }
41*d9f75844SAndroid Build Coastguard Worker
42*d9f75844SAndroid Build Coastguard Worker private:
43*d9f75844SAndroid Build Coastguard Worker ~ScopedRefCounted() = default;
44*d9f75844SAndroid Build Coastguard Worker
45*d9f75844SAndroid Build Coastguard Worker FunctionsCalled& called_;
46*d9f75844SAndroid Build Coastguard Worker int ref_count_ = 0;
47*d9f75844SAndroid Build Coastguard Worker };
48*d9f75844SAndroid Build Coastguard Worker
TEST(ScopedRefptrTest,IsCopyConstructable)49*d9f75844SAndroid Build Coastguard Worker TEST(ScopedRefptrTest, IsCopyConstructable) {
50*d9f75844SAndroid Build Coastguard Worker FunctionsCalled called;
51*d9f75844SAndroid Build Coastguard Worker scoped_refptr<ScopedRefCounted> ptr(new ScopedRefCounted(&called));
52*d9f75844SAndroid Build Coastguard Worker scoped_refptr<ScopedRefCounted> another_ptr = ptr;
53*d9f75844SAndroid Build Coastguard Worker
54*d9f75844SAndroid Build Coastguard Worker EXPECT_TRUE(ptr);
55*d9f75844SAndroid Build Coastguard Worker EXPECT_TRUE(another_ptr);
56*d9f75844SAndroid Build Coastguard Worker EXPECT_EQ(called.addref, 2);
57*d9f75844SAndroid Build Coastguard Worker }
58*d9f75844SAndroid Build Coastguard Worker
TEST(ScopedRefptrTest,IsCopyAssignable)59*d9f75844SAndroid Build Coastguard Worker TEST(ScopedRefptrTest, IsCopyAssignable) {
60*d9f75844SAndroid Build Coastguard Worker FunctionsCalled called;
61*d9f75844SAndroid Build Coastguard Worker scoped_refptr<ScopedRefCounted> another_ptr;
62*d9f75844SAndroid Build Coastguard Worker scoped_refptr<ScopedRefCounted> ptr(new ScopedRefCounted(&called));
63*d9f75844SAndroid Build Coastguard Worker another_ptr = ptr;
64*d9f75844SAndroid Build Coastguard Worker
65*d9f75844SAndroid Build Coastguard Worker EXPECT_TRUE(ptr);
66*d9f75844SAndroid Build Coastguard Worker EXPECT_TRUE(another_ptr);
67*d9f75844SAndroid Build Coastguard Worker EXPECT_EQ(called.addref, 2);
68*d9f75844SAndroid Build Coastguard Worker }
69*d9f75844SAndroid Build Coastguard Worker
TEST(ScopedRefptrTest,IsMoveConstructableWithoutExtraAddRefRelease)70*d9f75844SAndroid Build Coastguard Worker TEST(ScopedRefptrTest, IsMoveConstructableWithoutExtraAddRefRelease) {
71*d9f75844SAndroid Build Coastguard Worker FunctionsCalled called;
72*d9f75844SAndroid Build Coastguard Worker scoped_refptr<ScopedRefCounted> ptr(new ScopedRefCounted(&called));
73*d9f75844SAndroid Build Coastguard Worker scoped_refptr<ScopedRefCounted> another_ptr = std::move(ptr);
74*d9f75844SAndroid Build Coastguard Worker
75*d9f75844SAndroid Build Coastguard Worker EXPECT_FALSE(ptr);
76*d9f75844SAndroid Build Coastguard Worker EXPECT_TRUE(another_ptr);
77*d9f75844SAndroid Build Coastguard Worker EXPECT_EQ(called.addref, 1);
78*d9f75844SAndroid Build Coastguard Worker EXPECT_EQ(called.release, 0);
79*d9f75844SAndroid Build Coastguard Worker }
80*d9f75844SAndroid Build Coastguard Worker
TEST(ScopedRefptrTest,IsMoveAssignableWithoutExtraAddRefRelease)81*d9f75844SAndroid Build Coastguard Worker TEST(ScopedRefptrTest, IsMoveAssignableWithoutExtraAddRefRelease) {
82*d9f75844SAndroid Build Coastguard Worker FunctionsCalled called;
83*d9f75844SAndroid Build Coastguard Worker scoped_refptr<ScopedRefCounted> another_ptr;
84*d9f75844SAndroid Build Coastguard Worker scoped_refptr<ScopedRefCounted> ptr(new ScopedRefCounted(&called));
85*d9f75844SAndroid Build Coastguard Worker another_ptr = std::move(ptr);
86*d9f75844SAndroid Build Coastguard Worker
87*d9f75844SAndroid Build Coastguard Worker EXPECT_FALSE(ptr);
88*d9f75844SAndroid Build Coastguard Worker EXPECT_TRUE(another_ptr);
89*d9f75844SAndroid Build Coastguard Worker EXPECT_EQ(called.addref, 1);
90*d9f75844SAndroid Build Coastguard Worker EXPECT_EQ(called.release, 0);
91*d9f75844SAndroid Build Coastguard Worker }
92*d9f75844SAndroid Build Coastguard Worker
TEST(ScopedRefptrTest,MovableDuringVectorReallocation)93*d9f75844SAndroid Build Coastguard Worker TEST(ScopedRefptrTest, MovableDuringVectorReallocation) {
94*d9f75844SAndroid Build Coastguard Worker static_assert(
95*d9f75844SAndroid Build Coastguard Worker std::is_nothrow_move_constructible<scoped_refptr<ScopedRefCounted>>(),
96*d9f75844SAndroid Build Coastguard Worker "");
97*d9f75844SAndroid Build Coastguard Worker // Test below describes a scenario where it is helpful for move constructor
98*d9f75844SAndroid Build Coastguard Worker // to be noexcept.
99*d9f75844SAndroid Build Coastguard Worker FunctionsCalled called;
100*d9f75844SAndroid Build Coastguard Worker std::vector<scoped_refptr<ScopedRefCounted>> ptrs;
101*d9f75844SAndroid Build Coastguard Worker ptrs.reserve(1);
102*d9f75844SAndroid Build Coastguard Worker // Insert more elements than reserved to provoke reallocation.
103*d9f75844SAndroid Build Coastguard Worker ptrs.emplace_back(new ScopedRefCounted(&called));
104*d9f75844SAndroid Build Coastguard Worker ptrs.emplace_back(new ScopedRefCounted(&called));
105*d9f75844SAndroid Build Coastguard Worker
106*d9f75844SAndroid Build Coastguard Worker EXPECT_EQ(called.addref, 2);
107*d9f75844SAndroid Build Coastguard Worker EXPECT_EQ(called.release, 0);
108*d9f75844SAndroid Build Coastguard Worker }
109*d9f75844SAndroid Build Coastguard Worker
110*d9f75844SAndroid Build Coastguard Worker } // namespace
111*d9f75844SAndroid Build Coastguard Worker } // namespace rtc
112