|
|
@ -16,21 +16,21 @@ |
|
|
|
* |
|
|
|
* |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
#include "src/core/lib/support/reference_counted_ptr.h" |
|
|
|
#include "src/core/lib/support/ref_counted_ptr.h" |
|
|
|
|
|
|
|
|
|
|
|
#include <gtest/gtest.h> |
|
|
|
#include <gtest/gtest.h> |
|
|
|
|
|
|
|
|
|
|
|
#include <grpc/support/log.h> |
|
|
|
#include <grpc/support/log.h> |
|
|
|
|
|
|
|
|
|
|
|
#include "src/core/lib/support/memory.h" |
|
|
|
#include "src/core/lib/support/memory.h" |
|
|
|
#include "src/core/lib/support/reference_counted.h" |
|
|
|
#include "src/core/lib/support/ref_counted.h" |
|
|
|
#include "test/core/util/test_config.h" |
|
|
|
#include "test/core/util/test_config.h" |
|
|
|
|
|
|
|
|
|
|
|
namespace grpc_core { |
|
|
|
namespace grpc_core { |
|
|
|
namespace testing { |
|
|
|
namespace testing { |
|
|
|
namespace { |
|
|
|
namespace { |
|
|
|
|
|
|
|
|
|
|
|
class Foo : public ReferenceCounted { |
|
|
|
class Foo : public RefCounted { |
|
|
|
public: |
|
|
|
public: |
|
|
|
Foo() : value_(0) {} |
|
|
|
Foo() : value_(0) {} |
|
|
|
|
|
|
|
|
|
|
@ -42,76 +42,76 @@ class Foo : public ReferenceCounted { |
|
|
|
int value_; |
|
|
|
int value_; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
TEST(ReferenceCountedPtr, DefaultConstructor) { ReferenceCountedPtr<Foo> foo; } |
|
|
|
TEST(RefCountedPtr, DefaultConstructor) { RefCountedPtr<Foo> foo; } |
|
|
|
|
|
|
|
|
|
|
|
TEST(ReferenceCountedPtr, ExplicitConstructorEmpty) { |
|
|
|
TEST(RefCountedPtr, ExplicitConstructorEmpty) { |
|
|
|
ReferenceCountedPtr<Foo> foo(nullptr); |
|
|
|
RefCountedPtr<Foo> foo(nullptr); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
TEST(ReferenceCountedPtr, ExplicitConstructor) { |
|
|
|
TEST(RefCountedPtr, ExplicitConstructor) { |
|
|
|
ReferenceCountedPtr<Foo> foo(New<Foo>()); |
|
|
|
RefCountedPtr<Foo> foo(New<Foo>()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
TEST(ReferenceCountedPtr, MoveConstructor) { |
|
|
|
TEST(RefCountedPtr, MoveConstructor) { |
|
|
|
ReferenceCountedPtr<Foo> foo(New<Foo>()); |
|
|
|
RefCountedPtr<Foo> foo(New<Foo>()); |
|
|
|
ReferenceCountedPtr<Foo> foo2(std::move(foo)); |
|
|
|
RefCountedPtr<Foo> foo2(std::move(foo)); |
|
|
|
EXPECT_EQ(nullptr, foo.get()); |
|
|
|
EXPECT_EQ(nullptr, foo.get()); |
|
|
|
EXPECT_NE(nullptr, foo2.get()); |
|
|
|
EXPECT_NE(nullptr, foo2.get()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
TEST(ReferenceCountedPtr, MoveAssignment) { |
|
|
|
TEST(RefCountedPtr, MoveAssignment) { |
|
|
|
ReferenceCountedPtr<Foo> foo(New<Foo>()); |
|
|
|
RefCountedPtr<Foo> foo(New<Foo>()); |
|
|
|
ReferenceCountedPtr<Foo> foo2 = std::move(foo); |
|
|
|
RefCountedPtr<Foo> foo2 = std::move(foo); |
|
|
|
EXPECT_EQ(nullptr, foo.get()); |
|
|
|
EXPECT_EQ(nullptr, foo.get()); |
|
|
|
EXPECT_NE(nullptr, foo2.get()); |
|
|
|
EXPECT_NE(nullptr, foo2.get()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
TEST(ReferenceCountedPtr, CopyConstructor) { |
|
|
|
TEST(RefCountedPtr, CopyConstructor) { |
|
|
|
ReferenceCountedPtr<Foo> foo(New<Foo>()); |
|
|
|
RefCountedPtr<Foo> foo(New<Foo>()); |
|
|
|
ReferenceCountedPtr<Foo> foo2(foo); |
|
|
|
RefCountedPtr<Foo> foo2(foo); |
|
|
|
EXPECT_NE(nullptr, foo.get()); |
|
|
|
EXPECT_NE(nullptr, foo.get()); |
|
|
|
EXPECT_EQ(foo.get(), foo2.get()); |
|
|
|
EXPECT_EQ(foo.get(), foo2.get()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
TEST(ReferenceCountedPtr, CopyAssignment) { |
|
|
|
TEST(RefCountedPtr, CopyAssignment) { |
|
|
|
ReferenceCountedPtr<Foo> foo(New<Foo>()); |
|
|
|
RefCountedPtr<Foo> foo(New<Foo>()); |
|
|
|
ReferenceCountedPtr<Foo> foo2 = foo; |
|
|
|
RefCountedPtr<Foo> foo2 = foo; |
|
|
|
EXPECT_NE(nullptr, foo.get()); |
|
|
|
EXPECT_NE(nullptr, foo.get()); |
|
|
|
EXPECT_EQ(foo.get(), foo2.get()); |
|
|
|
EXPECT_EQ(foo.get(), foo2.get()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
TEST(ReferenceCountedPtr, CopyAssignmentWhenEmpty) { |
|
|
|
TEST(RefCountedPtr, CopyAssignmentWhenEmpty) { |
|
|
|
ReferenceCountedPtr<Foo> foo; |
|
|
|
RefCountedPtr<Foo> foo; |
|
|
|
ReferenceCountedPtr<Foo> foo2; |
|
|
|
RefCountedPtr<Foo> foo2; |
|
|
|
foo2 = foo; |
|
|
|
foo2 = foo; |
|
|
|
EXPECT_EQ(nullptr, foo.get()); |
|
|
|
EXPECT_EQ(nullptr, foo.get()); |
|
|
|
EXPECT_EQ(nullptr, foo2.get()); |
|
|
|
EXPECT_EQ(nullptr, foo2.get()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
TEST(ReferenceCountedPtr, CopyAssignmentToSelf) { |
|
|
|
TEST(RefCountedPtr, CopyAssignmentToSelf) { |
|
|
|
ReferenceCountedPtr<Foo> foo(New<Foo>()); |
|
|
|
RefCountedPtr<Foo> foo(New<Foo>()); |
|
|
|
foo = foo; |
|
|
|
foo = foo; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
TEST(ReferenceCountedPtr, EnclosedScope) { |
|
|
|
TEST(RefCountedPtr, EnclosedScope) { |
|
|
|
ReferenceCountedPtr<Foo> foo(New<Foo>()); |
|
|
|
RefCountedPtr<Foo> foo(New<Foo>()); |
|
|
|
{ |
|
|
|
{ |
|
|
|
ReferenceCountedPtr<Foo> foo2(std::move(foo)); |
|
|
|
RefCountedPtr<Foo> foo2(std::move(foo)); |
|
|
|
EXPECT_EQ(nullptr, foo.get()); |
|
|
|
EXPECT_EQ(nullptr, foo.get()); |
|
|
|
EXPECT_NE(nullptr, foo2.get()); |
|
|
|
EXPECT_NE(nullptr, foo2.get()); |
|
|
|
} |
|
|
|
} |
|
|
|
EXPECT_EQ(nullptr, foo.get()); |
|
|
|
EXPECT_EQ(nullptr, foo.get()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
TEST(ReferenceCountedPtr, ResetFromNullToNonNull) { |
|
|
|
TEST(RefCountedPtr, ResetFromNullToNonNull) { |
|
|
|
ReferenceCountedPtr<Foo> foo; |
|
|
|
RefCountedPtr<Foo> foo; |
|
|
|
EXPECT_EQ(nullptr, foo.get()); |
|
|
|
EXPECT_EQ(nullptr, foo.get()); |
|
|
|
foo.reset(New<Foo>()); |
|
|
|
foo.reset(New<Foo>()); |
|
|
|
EXPECT_NE(nullptr, foo.get()); |
|
|
|
EXPECT_NE(nullptr, foo.get()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
TEST(ReferenceCountedPtr, ResetFromNonNullToNonNull) { |
|
|
|
TEST(RefCountedPtr, ResetFromNonNullToNonNull) { |
|
|
|
ReferenceCountedPtr<Foo> foo(New<Foo>()); |
|
|
|
RefCountedPtr<Foo> foo(New<Foo>()); |
|
|
|
EXPECT_NE(nullptr, foo.get()); |
|
|
|
EXPECT_NE(nullptr, foo.get()); |
|
|
|
Foo* original = foo.get(); |
|
|
|
Foo* original = foo.get(); |
|
|
|
foo.reset(New<Foo>()); |
|
|
|
foo.reset(New<Foo>()); |
|
|
@ -119,46 +119,46 @@ TEST(ReferenceCountedPtr, ResetFromNonNullToNonNull) { |
|
|
|
EXPECT_NE(original, foo.get()); |
|
|
|
EXPECT_NE(original, foo.get()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
TEST(ReferenceCountedPtr, ResetFromNonNullToNull) { |
|
|
|
TEST(RefCountedPtr, ResetFromNonNullToNull) { |
|
|
|
ReferenceCountedPtr<Foo> foo(New<Foo>()); |
|
|
|
RefCountedPtr<Foo> foo(New<Foo>()); |
|
|
|
EXPECT_NE(nullptr, foo.get()); |
|
|
|
EXPECT_NE(nullptr, foo.get()); |
|
|
|
foo.reset(); |
|
|
|
foo.reset(); |
|
|
|
EXPECT_EQ(nullptr, foo.get()); |
|
|
|
EXPECT_EQ(nullptr, foo.get()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
TEST(ReferenceCountedPtr, ResetFromNullToNull) { |
|
|
|
TEST(RefCountedPtr, ResetFromNullToNull) { |
|
|
|
ReferenceCountedPtr<Foo> foo; |
|
|
|
RefCountedPtr<Foo> foo; |
|
|
|
EXPECT_EQ(nullptr, foo.get()); |
|
|
|
EXPECT_EQ(nullptr, foo.get()); |
|
|
|
foo.reset(nullptr); |
|
|
|
foo.reset(nullptr); |
|
|
|
EXPECT_EQ(nullptr, foo.get()); |
|
|
|
EXPECT_EQ(nullptr, foo.get()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
TEST(ReferenceCountedPtr, DerefernceOperators) { |
|
|
|
TEST(RefCountedPtr, DerefernceOperators) { |
|
|
|
ReferenceCountedPtr<Foo> foo(New<Foo>()); |
|
|
|
RefCountedPtr<Foo> foo(New<Foo>()); |
|
|
|
foo->value(); |
|
|
|
foo->value(); |
|
|
|
Foo& foo_ref = *foo; |
|
|
|
Foo& foo_ref = *foo; |
|
|
|
foo_ref.value(); |
|
|
|
foo_ref.value(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
TEST(MakeReferenceCounted, NoArgs) { |
|
|
|
TEST(MakeRefCounted, NoArgs) { |
|
|
|
ReferenceCountedPtr<Foo> foo = MakeReferenceCounted<Foo>(); |
|
|
|
RefCountedPtr<Foo> foo = MakeRefCounted<Foo>(); |
|
|
|
EXPECT_EQ(0, foo->value()); |
|
|
|
EXPECT_EQ(0, foo->value()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
TEST(MakeReferenceCounted, Args) { |
|
|
|
TEST(MakeRefCounted, Args) { |
|
|
|
ReferenceCountedPtr<Foo> foo = MakeReferenceCounted<Foo>(3); |
|
|
|
RefCountedPtr<Foo> foo = MakeRefCounted<Foo>(3); |
|
|
|
EXPECT_EQ(3, foo->value()); |
|
|
|
EXPECT_EQ(3, foo->value()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
TraceFlag foo_tracer(true, "foo"); |
|
|
|
TraceFlag foo_tracer(true, "foo"); |
|
|
|
|
|
|
|
|
|
|
|
class FooWithTracing : public ReferenceCountedWithTracing { |
|
|
|
class FooWithTracing : public RefCountedWithTracing { |
|
|
|
public: |
|
|
|
public: |
|
|
|
FooWithTracing() : ReferenceCountedWithTracing(&foo_tracer) {} |
|
|
|
FooWithTracing() : RefCountedWithTracing(&foo_tracer) {} |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
TEST(ReferenceCountedPtr, ReferenceCountedWithTracing) { |
|
|
|
TEST(RefCountedPtr, RefCountedWithTracing) { |
|
|
|
ReferenceCountedPtr<FooWithTracing> foo(New<FooWithTracing>()); |
|
|
|
RefCountedPtr<FooWithTracing> foo(New<FooWithTracing>()); |
|
|
|
foo->Ref(DEBUG_LOCATION, "foo"); |
|
|
|
foo->Ref(DEBUG_LOCATION, "foo"); |
|
|
|
foo->Unref(DEBUG_LOCATION, "foo"); |
|
|
|
foo->Unref(DEBUG_LOCATION, "foo"); |
|
|
|
} |
|
|
|
} |