1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
/*
* Copyright 2016 The WebRTC Project Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "rtc_base/ref_counted_object.h"
#include <memory>
#include <string>
#include <type_traits>
#include <utility>
#include "absl/strings/string_view.h"
#include "api/make_ref_counted.h"
#include "api/scoped_refptr.h"
#include "rtc_base/ref_count.h"
#include "test/gtest.h"
namespace webrtc {
namespace {
class A {
public:
A() {}
A(const A&) = delete;
A& operator=(const A&) = delete;
};
class RefClass : public RefCountInterface {
public:
RefClass() {}
protected:
~RefClass() override {}
};
class RefClassWithRvalue : public RefCountInterface {
public:
explicit RefClassWithRvalue(std::unique_ptr<A> a) : a_(std::move(a)) {}
protected:
~RefClassWithRvalue() override {}
public:
std::unique_ptr<A> a_;
};
class RefClassWithMixedValues : public RefCountInterface {
public:
RefClassWithMixedValues(std::unique_ptr<A> a, int b, absl::string_view c)
: a_(std::move(a)), b_(b), c_(c) {}
protected:
~RefClassWithMixedValues() override {}
public:
std::unique_ptr<A> a_;
int b_;
std::string c_;
};
class Foo {
public:
Foo() {}
Foo(int i, int j) : foo_(i + j) {}
int foo_ = 0;
};
class FooItf : public RefCountInterface {
public:
FooItf() {}
FooItf(int i, int j) : foo_(i + j) {}
int foo_ = 0;
};
} // namespace
TEST(RefCountedObject, HasOneRef) {
scoped_refptr<RefCountedObject<RefClass>> aref(
new RefCountedObject<RefClass>());
EXPECT_TRUE(aref->HasOneRef());
aref->AddRef();
EXPECT_FALSE(aref->HasOneRef());
EXPECT_EQ(aref->Release(), RefCountReleaseStatus::kOtherRefsRemained);
EXPECT_TRUE(aref->HasOneRef());
}
TEST(RefCountedObject, SupportRValuesInCtor) {
std::unique_ptr<A> a(new A());
scoped_refptr<RefClassWithRvalue> ref(
new RefCountedObject<RefClassWithRvalue>(std::move(a)));
EXPECT_TRUE(ref->a_.get() != nullptr);
EXPECT_TRUE(a.get() == nullptr);
}
TEST(RefCountedObject, SupportMixedTypesInCtor) {
std::unique_ptr<A> a(new A());
int b = 9;
std::string c = "hello";
scoped_refptr<RefClassWithMixedValues> ref(
new RefCountedObject<RefClassWithMixedValues>(std::move(a), b, c));
EXPECT_TRUE(ref->a_.get() != nullptr);
EXPECT_TRUE(a.get() == nullptr);
EXPECT_EQ(b, ref->b_);
EXPECT_EQ(c, ref->c_);
}
TEST(FinalRefCountedObject, CanWrapIntoScopedRefptr) {
using WrappedTyped = FinalRefCountedObject<A>;
static_assert(!std::is_polymorphic<WrappedTyped>::value, "");
scoped_refptr<WrappedTyped> ref(new WrappedTyped());
EXPECT_TRUE(ref.get());
EXPECT_TRUE(ref->HasOneRef());
// Test reference counter is updated on some simple operations.
scoped_refptr<WrappedTyped> ref2 = ref;
EXPECT_FALSE(ref->HasOneRef());
EXPECT_FALSE(ref2->HasOneRef());
ref = nullptr;
EXPECT_TRUE(ref2->HasOneRef());
}
TEST(FinalRefCountedObject, CanCreateFromMovedType) {
class MoveOnly {
public:
MoveOnly(int a) : a_(a) {}
MoveOnly(MoveOnly&&) = default;
int a() { return a_; }
private:
int a_;
};
MoveOnly foo(5);
auto ref = make_ref_counted<MoveOnly>(std::move(foo));
EXPECT_EQ(ref->a(), 5);
}
// This test is mostly a compile-time test for scoped_refptr compatibility.
TEST(RefCounted, SmartPointers) {
// Sanity compile-time tests. FooItf is virtual, Foo is not, FooItf inherits
// from RefCountInterface, Foo does not.
static_assert(std::is_base_of<RefCountInterface, FooItf>::value, "");
static_assert(!std::is_base_of<RefCountInterface, Foo>::value, "");
static_assert(std::is_polymorphic<FooItf>::value, "");
static_assert(!std::is_polymorphic<Foo>::value, "");
{
// Test with FooItf, a class that inherits from RefCountInterface.
// Check that we get a valid FooItf reference counted object.
auto p = make_ref_counted<FooItf>(2, 3);
EXPECT_NE(p.get(), nullptr);
EXPECT_EQ(p->foo_, 5); // the FooItf ctor just stores 2+3 in foo_.
// Declaring what should result in the same type as `p` is of.
scoped_refptr<FooItf> p2 = p;
}
{
// Same for `Foo`
auto p = make_ref_counted<Foo>(2, 3);
EXPECT_NE(p.get(), nullptr);
EXPECT_EQ(p->foo_, 5);
scoped_refptr<FinalRefCountedObject<Foo>> p2 = p;
}
}
} // namespace webrtc
|