23 #include <gmock/gmock.h>
24 #include <gtest/gtest.h>
33 class Foo :
public RefCounted<Foo> {
37 "PolymorphicRefCount doesn't have a virtual dtor");
41 TEST(RefCounted, Basic) {
46 TEST(RefCounted, ExtraRef) {
48 RefCountedPtr<Foo> foop =
foo->Ref();
54 class Value :
public RefCounted<Value, PolymorphicRefCount, kUnrefNoDelete> {
57 registry->emplace(
this);
66 void GarbageCollectRegistry(
std::set<std::unique_ptr<Value>>* registry) {
67 for (
auto it = registry->begin();
it != registry->end();) {
68 RefCountedPtr<Value>
v = (*it)->RefIfNonZero();
75 it = registry->erase(
it);
80 TEST(RefCounted, NoDeleteUponUnref) {
81 std::set<std::unique_ptr<Value>> registry;
83 auto v1 = MakeRefCounted<Value>(1, ®istry);
84 auto v2 = MakeRefCounted<Value>(2, ®istry);
91 GarbageCollectRegistry(®istry);
98 GarbageCollectRegistry(®istry);
103 GarbageCollectRegistry(®istry);
107 class ValueInExternalAllocation
108 :
public RefCounted<ValueInExternalAllocation, PolymorphicRefCount,
119 TEST(RefCounted, CallDtorUponUnref) {
120 std::aligned_storage<
sizeof(ValueInExternalAllocation),
122 RefCountedPtr<ValueInExternalAllocation>
value(
123 new (&
storage) ValueInExternalAllocation(5));
127 class FooNonPolymorphic
128 :
public RefCounted<FooNonPolymorphic, NonPolymorphicRefCount> {
130 FooNonPolymorphic() {
132 "NonPolymorphicRefCount has a virtual dtor");
136 TEST(RefCountedNonPolymorphic, Basic) {
137 FooNonPolymorphic*
foo =
new FooNonPolymorphic();
141 TEST(RefCountedNonPolymorphic, ExtraRef) {
142 FooNonPolymorphic*
foo =
new FooNonPolymorphic();
143 RefCountedPtr<FooNonPolymorphic> foop =
foo->Ref();
149 class FooWithTracing :
public RefCounted<FooWithTracing> {
151 FooWithTracing() : RefCounted(
"Foo") {}
154 TEST(RefCountedWithTracing, Basic) {
155 FooWithTracing*
foo =
new FooWithTracing();
166 class FooNonPolymorphicWithTracing
167 :
public RefCounted<FooNonPolymorphicWithTracing, NonPolymorphicRefCount> {
169 FooNonPolymorphicWithTracing() : RefCounted(
"FooNonPolymorphicWithTracing") {}
172 TEST(RefCountedNonPolymorphicWithTracing, Basic) {
173 FooNonPolymorphicWithTracing*
foo =
new FooNonPolymorphicWithTracing();
174 RefCountedPtr<FooNonPolymorphicWithTracing> foop =
189 int main(
int argc,
char** argv) {