From 4f2b0fdadfb1e1eb6c796c40ec5aabfbb348aa89 Mon Sep 17 00:00:00 2001 From: "Mark D. Roth" Date: Fri, 19 Jan 2018 12:12:23 -0800 Subject: Rename 'gpr++' directories to 'gprpp'. --- test/core/gpr++/BUILD | 95 --------------- test/core/gpr++/inlined_vector_test.cc | 74 ------------ test/core/gpr++/manual_constructor_test.cc | 99 --------------- test/core/gpr++/memory_test.cc | 74 ------------ test/core/gpr++/orphanable_test.cc | 114 ------------------ test/core/gpr++/ref_counted_ptr_test.cc | 185 ----------------------------- test/core/gpr++/ref_counted_test.cc | 74 ------------ test/core/gprpp/BUILD | 96 +++++++++++++++ test/core/gprpp/inlined_vector_test.cc | 74 ++++++++++++ test/core/gprpp/manual_constructor_test.cc | 99 +++++++++++++++ test/core/gprpp/memory_test.cc | 74 ++++++++++++ test/core/gprpp/orphanable_test.cc | 114 ++++++++++++++++++ test/core/gprpp/ref_counted_ptr_test.cc | 185 +++++++++++++++++++++++++++++ test/core/gprpp/ref_counted_test.cc | 74 ++++++++++++ 14 files changed, 716 insertions(+), 715 deletions(-) delete mode 100644 test/core/gpr++/BUILD delete mode 100644 test/core/gpr++/inlined_vector_test.cc delete mode 100644 test/core/gpr++/manual_constructor_test.cc delete mode 100644 test/core/gpr++/memory_test.cc delete mode 100644 test/core/gpr++/orphanable_test.cc delete mode 100644 test/core/gpr++/ref_counted_ptr_test.cc delete mode 100644 test/core/gpr++/ref_counted_test.cc create mode 100644 test/core/gprpp/BUILD create mode 100644 test/core/gprpp/inlined_vector_test.cc create mode 100644 test/core/gprpp/manual_constructor_test.cc create mode 100644 test/core/gprpp/memory_test.cc create mode 100644 test/core/gprpp/orphanable_test.cc create mode 100644 test/core/gprpp/ref_counted_ptr_test.cc create mode 100644 test/core/gprpp/ref_counted_test.cc (limited to 'test') diff --git a/test/core/gpr++/BUILD b/test/core/gpr++/BUILD deleted file mode 100644 index 93324a378b..0000000000 --- a/test/core/gpr++/BUILD +++ /dev/null @@ -1,95 +0,0 @@ -# Copyright 2016 gRPC authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -load("//bazel:grpc_build_system.bzl", "grpc_cc_library", "grpc_cc_test", "grpc_cc_binary", "grpc_package") - -licenses(["notice"]) # Apache v2 - -grpc_package(name = "test/core/gpr++") - -grpc_cc_test( - name = "manual_constructor_test", - srcs = ["manual_constructor_test.cc"], - language = "C++", - deps = [ - "//:gpr", - "//test/core/util:gpr_test_util", - ], -) - -grpc_cc_test( - name = "memory_test", - srcs = ["memory_test.cc"], - external_deps = [ - "gtest", - ], - language = "C++", - deps = [ - "//:grpc", - "//test/core/util:gpr_test_util", - ], -) - -grpc_cc_test( - name = "inlined_vector_test", - srcs = ["inlined_vector_test.cc"], - external_deps = [ - "gtest", - ], - language = "C++", - deps = [ - "//:grpc", - "//test/core/util:gpr_test_util", - ], -) - -grpc_cc_test( - name = "orphanable_test", - srcs = ["orphanable_test.cc"], - language = "C++", - deps = [ - "//:orphanable", - "//test/core/util:gpr_test_util", - ], - external_deps = [ - "gtest", - ], -) - -grpc_cc_test( - name = "ref_counted_test", - srcs = ["ref_counted_test.cc"], - language = "C++", - deps = [ - "//:ref_counted", - "//test/core/util:gpr_test_util", - ], - external_deps = [ - "gtest", - ], -) - -grpc_cc_test( - name = "ref_counted_ptr_test", - srcs = ["ref_counted_ptr_test.cc"], - language = "C++", - deps = [ - "//:ref_counted", - "//:ref_counted_ptr", - "//test/core/util:gpr_test_util", - ], - external_deps = [ - "gtest", - ], -) diff --git a/test/core/gpr++/inlined_vector_test.cc b/test/core/gpr++/inlined_vector_test.cc deleted file mode 100644 index 09d5453a56..0000000000 --- a/test/core/gpr++/inlined_vector_test.cc +++ /dev/null @@ -1,74 +0,0 @@ -/* - * - * Copyright 2017 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include "src/core/lib/gpr++/inlined_vector.h" -#include -#include "src/core/lib/gpr++/memory.h" -#include "test/core/util/test_config.h" - -namespace grpc_core { -namespace testing { - -TEST(InlinedVectorTest, CreateAndIterate) { - const int kNumElements = 9; - InlinedVector v; - for (int i = 0; i < kNumElements; ++i) { - v.push_back(i); - } - EXPECT_EQ(static_cast(kNumElements), v.size()); - for (int i = 0; i < kNumElements; ++i) { - EXPECT_EQ(i, v[i]); - } -} - -TEST(InlinedVectorTest, ValuesAreInlined) { - const int kNumElements = 5; - InlinedVector v; - for (int i = 0; i < kNumElements; ++i) { - v.push_back(i); - } - EXPECT_EQ(static_cast(kNumElements), v.size()); - for (int i = 0; i < kNumElements; ++i) { - EXPECT_EQ(i, v[i]); - } -} - -TEST(InlinedVectorTest, PushBackWithMove) { - InlinedVector, 1> v; - UniquePtr i = MakeUnique(3); - v.push_back(std::move(i)); - EXPECT_EQ(nullptr, i.get()); - EXPECT_EQ(1UL, v.size()); - EXPECT_EQ(3, *v[0]); -} - -TEST(InlinedVectorTest, EmplaceBack) { - InlinedVector, 1> v; - v.emplace_back(New(3)); - EXPECT_EQ(1UL, v.size()); - EXPECT_EQ(3, *v[0]); -} - -} // namespace testing -} // namespace grpc_core - -int main(int argc, char** argv) { - grpc_test_init(argc, argv); - ::testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} diff --git a/test/core/gpr++/manual_constructor_test.cc b/test/core/gpr++/manual_constructor_test.cc deleted file mode 100644 index e049b793f6..0000000000 --- a/test/core/gpr++/manual_constructor_test.cc +++ /dev/null @@ -1,99 +0,0 @@ -/* - * - * Copyright 2017 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -/* Test of gpr synchronization support. */ - -#include "src/core/lib/gpr++/manual_constructor.h" -#include -#include -#include -#include -#include -#include -#include -#include "src/core/lib/gpr++/abstract.h" -#include "test/core/util/test_config.h" - -class A { - public: - A() {} - virtual ~A() {} - virtual const char* foo() { return "A_foo"; } - virtual const char* bar() { return "A_bar"; } - GRPC_ABSTRACT_BASE_CLASS -}; - -class B : public A { - public: - B() {} - ~B() {} - const char* foo() override { return "B_foo"; } - char get_junk() { return junk[0]; } - - private: - char junk[1000]; -}; - -class C : public B { - public: - C() {} - ~C() {} - virtual const char* bar() { return "C_bar"; } - char get_more_junk() { return more_junk[0]; } - - private: - char more_junk[1000]; -}; - -class D : public A { - public: - virtual const char* bar() { return "D_bar"; } -}; - -static void basic_test() { - grpc_core::PolymorphicManualConstructor poly; - poly.Init(); - GPR_ASSERT(!strcmp(poly->foo(), "B_foo")); - GPR_ASSERT(!strcmp(poly->bar(), "A_bar")); -} - -static void complex_test() { - grpc_core::PolymorphicManualConstructor polyB; - polyB.Init(); - GPR_ASSERT(!strcmp(polyB->foo(), "B_foo")); - GPR_ASSERT(!strcmp(polyB->bar(), "A_bar")); - - grpc_core::PolymorphicManualConstructor polyC; - polyC.Init(); - GPR_ASSERT(!strcmp(polyC->foo(), "B_foo")); - GPR_ASSERT(!strcmp(polyC->bar(), "C_bar")); - - grpc_core::PolymorphicManualConstructor polyD; - polyD.Init(); - GPR_ASSERT(!strcmp(polyD->foo(), "A_foo")); - GPR_ASSERT(!strcmp(polyD->bar(), "D_bar")); -} - -/* ------------------------------------------------- */ - -int main(int argc, char* argv[]) { - grpc_test_init(argc, argv); - basic_test(); - complex_test(); - return 0; -} diff --git a/test/core/gpr++/memory_test.cc b/test/core/gpr++/memory_test.cc deleted file mode 100644 index 3553e119e3..0000000000 --- a/test/core/gpr++/memory_test.cc +++ /dev/null @@ -1,74 +0,0 @@ -/* - * - * Copyright 2017 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include "src/core/lib/gpr++/memory.h" -#include -#include "test/core/util/test_config.h" - -namespace grpc_core { -namespace testing { - -struct Foo { - Foo(int p, int q) : a(p), b(q) {} - int a; - int b; -}; - -TEST(MemoryTest, NewDeleteTest) { Delete(New()); } - -TEST(MemoryTest, NewDeleteWithArgTest) { - int* i = New(42); - EXPECT_EQ(42, *i); - Delete(i); -} - -TEST(MemoryTest, NewDeleteWithArgsTest) { - Foo* p = New(1, 2); - EXPECT_EQ(1, p->a); - EXPECT_EQ(2, p->b); - Delete(p); -} - -TEST(MemoryTest, MakeUniqueTest) { MakeUnique(); } - -TEST(MemoryTest, MakeUniqueWithArgTest) { - auto i = MakeUnique(42); - EXPECT_EQ(42, *i); -} - -TEST(MemoryTest, UniquePtrWithCustomDeleter) { - int n = 0; - class IncrementingDeleter { - public: - void operator()(int* p) { ++*p; } - }; - { - UniquePtr p(&n); - EXPECT_EQ(0, n); - } - EXPECT_EQ(1, n); -} - -} // namespace testing -} // namespace grpc_core - -int main(int argc, char** argv) { - grpc_test_init(argc, argv); - ::testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} diff --git a/test/core/gpr++/orphanable_test.cc b/test/core/gpr++/orphanable_test.cc deleted file mode 100644 index 4513d25da1..0000000000 --- a/test/core/gpr++/orphanable_test.cc +++ /dev/null @@ -1,114 +0,0 @@ -/* - * - * Copyright 2017 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include "src/core/lib/gpr++/orphanable.h" - -#include - -#include "src/core/lib/gpr++/memory.h" -#include "test/core/util/test_config.h" - -namespace grpc_core { -namespace testing { -namespace { - -class Foo : public Orphanable { - public: - Foo() : Foo(0) {} - explicit Foo(int value) : value_(value) {} - void Orphan() override { Delete(this); } - int value() const { return value_; } - - private: - int value_; -}; - -TEST(Orphanable, Basic) { - Foo* foo = New(); - foo->Orphan(); -} - -TEST(OrphanablePtr, Basic) { - OrphanablePtr foo(New()); - EXPECT_EQ(0, foo->value()); -} - -TEST(MakeOrphanable, DefaultConstructor) { - auto foo = MakeOrphanable(); - EXPECT_EQ(0, foo->value()); -} - -TEST(MakeOrphanable, WithParameters) { - auto foo = MakeOrphanable(5); - EXPECT_EQ(5, foo->value()); -} - -class Bar : public InternallyRefCounted { - public: - Bar() : Bar(0) {} - explicit Bar(int value) : value_(value) {} - void Orphan() override { Unref(); } - int value() const { return value_; } - - void StartWork() { Ref(); } - void FinishWork() { Unref(); } - - private: - int value_; -}; - -TEST(OrphanablePtr, InternallyRefCounted) { - auto bar = MakeOrphanable(); - bar->StartWork(); - bar->FinishWork(); -} - -// Note: We use DebugOnlyTraceFlag instead of TraceFlag to ensure that -// things build properly in both debug and non-debug cases. -DebugOnlyTraceFlag baz_tracer(true, "baz"); - -class Baz : public InternallyRefCountedWithTracing { - public: - Baz() : Baz(0) {} - explicit Baz(int value) - : InternallyRefCountedWithTracing(&baz_tracer), value_(value) {} - void Orphan() override { Unref(); } - int value() const { return value_; } - - void StartWork() { Ref(DEBUG_LOCATION, "work"); } - void FinishWork() { Unref(DEBUG_LOCATION, "work"); } - - private: - int value_; -}; - -TEST(OrphanablePtr, InternallyRefCountedWithTracing) { - auto baz = MakeOrphanable(); - baz->StartWork(); - baz->FinishWork(); -} - -} // namespace -} // namespace testing -} // namespace grpc_core - -int main(int argc, char** argv) { - grpc_test_init(argc, argv); - ::testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} diff --git a/test/core/gpr++/ref_counted_ptr_test.cc b/test/core/gpr++/ref_counted_ptr_test.cc deleted file mode 100644 index e897b2859c..0000000000 --- a/test/core/gpr++/ref_counted_ptr_test.cc +++ /dev/null @@ -1,185 +0,0 @@ -/* - * - * Copyright 2017 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include "src/core/lib/gpr++/ref_counted_ptr.h" - -#include - -#include - -#include "src/core/lib/gpr++/memory.h" -#include "src/core/lib/gpr++/ref_counted.h" -#include "test/core/util/test_config.h" - -namespace grpc_core { -namespace testing { -namespace { - -class Foo : public RefCounted { - public: - Foo() : value_(0) {} - - explicit Foo(int value) : value_(value) {} - - int value() const { return value_; } - - private: - int value_; -}; - -TEST(RefCountedPtr, DefaultConstructor) { RefCountedPtr foo; } - -TEST(RefCountedPtr, ExplicitConstructorEmpty) { - RefCountedPtr foo(nullptr); -} - -TEST(RefCountedPtr, ExplicitConstructor) { RefCountedPtr foo(New()); } - -TEST(RefCountedPtr, MoveConstructor) { - RefCountedPtr foo(New()); - RefCountedPtr foo2(std::move(foo)); - EXPECT_EQ(nullptr, foo.get()); - EXPECT_NE(nullptr, foo2.get()); -} - -TEST(RefCountedPtr, MoveAssignment) { - RefCountedPtr foo(New()); - RefCountedPtr foo2 = std::move(foo); - EXPECT_EQ(nullptr, foo.get()); - EXPECT_NE(nullptr, foo2.get()); -} - -TEST(RefCountedPtr, CopyConstructor) { - RefCountedPtr foo(New()); - RefCountedPtr foo2(foo); - EXPECT_NE(nullptr, foo.get()); - EXPECT_EQ(foo.get(), foo2.get()); -} - -TEST(RefCountedPtr, CopyAssignment) { - RefCountedPtr foo(New()); - RefCountedPtr foo2 = foo; - EXPECT_NE(nullptr, foo.get()); - EXPECT_EQ(foo.get(), foo2.get()); -} - -TEST(RefCountedPtr, CopyAssignmentWhenEmpty) { - RefCountedPtr foo; - RefCountedPtr foo2; - foo2 = foo; - EXPECT_EQ(nullptr, foo.get()); - EXPECT_EQ(nullptr, foo2.get()); -} - -TEST(RefCountedPtr, CopyAssignmentToSelf) { - RefCountedPtr foo(New()); - foo = foo; -} - -TEST(RefCountedPtr, EnclosedScope) { - RefCountedPtr foo(New()); - { - RefCountedPtr foo2(std::move(foo)); - EXPECT_EQ(nullptr, foo.get()); - EXPECT_NE(nullptr, foo2.get()); - } - EXPECT_EQ(nullptr, foo.get()); -} - -TEST(RefCountedPtr, ResetFromNullToNonNull) { - RefCountedPtr foo; - EXPECT_EQ(nullptr, foo.get()); - foo.reset(New()); - EXPECT_NE(nullptr, foo.get()); -} - -TEST(RefCountedPtr, ResetFromNonNullToNonNull) { - RefCountedPtr foo(New()); - EXPECT_NE(nullptr, foo.get()); - Foo* original = foo.get(); - foo.reset(New()); - EXPECT_NE(nullptr, foo.get()); - EXPECT_NE(original, foo.get()); -} - -TEST(RefCountedPtr, ResetFromNonNullToNull) { - RefCountedPtr foo(New()); - EXPECT_NE(nullptr, foo.get()); - foo.reset(); - EXPECT_EQ(nullptr, foo.get()); -} - -TEST(RefCountedPtr, ResetFromNullToNull) { - RefCountedPtr foo; - EXPECT_EQ(nullptr, foo.get()); - foo.reset(nullptr); - EXPECT_EQ(nullptr, foo.get()); -} - -TEST(RefCountedPtr, DerefernceOperators) { - RefCountedPtr foo(New()); - foo->value(); - Foo& foo_ref = *foo; - foo_ref.value(); -} - -TEST(RefCountedPtr, EqualityOperators) { - RefCountedPtr foo(New()); - RefCountedPtr bar = foo; - RefCountedPtr empty; - // Test equality between RefCountedPtrs. - EXPECT_EQ(foo, bar); - EXPECT_NE(foo, empty); - // Test equality with bare pointers. - EXPECT_EQ(foo, foo.get()); - EXPECT_EQ(empty, nullptr); - EXPECT_NE(foo, nullptr); -} - -TEST(MakeRefCounted, NoArgs) { - RefCountedPtr foo = MakeRefCounted(); - EXPECT_EQ(0, foo->value()); -} - -TEST(MakeRefCounted, Args) { - RefCountedPtr foo = MakeRefCounted(3); - EXPECT_EQ(3, foo->value()); -} - -TraceFlag foo_tracer(true, "foo"); - -class FooWithTracing : public RefCountedWithTracing { - public: - FooWithTracing() : RefCountedWithTracing(&foo_tracer) {} -}; - -TEST(RefCountedPtr, RefCountedWithTracing) { - RefCountedPtr foo(New()); - foo->Ref(DEBUG_LOCATION, "foo"); - foo->Unref(DEBUG_LOCATION, "foo"); -} - -} // namespace -} // namespace testing -} // namespace grpc_core - -int main(int argc, char** argv) { - grpc_test_init(argc, argv); - ::testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} diff --git a/test/core/gpr++/ref_counted_test.cc b/test/core/gpr++/ref_counted_test.cc deleted file mode 100644 index 568ec51c64..0000000000 --- a/test/core/gpr++/ref_counted_test.cc +++ /dev/null @@ -1,74 +0,0 @@ -/* - * - * Copyright 2017 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include "src/core/lib/gpr++/ref_counted.h" - -#include - -#include "src/core/lib/gpr++/memory.h" -#include "test/core/util/test_config.h" - -namespace grpc_core { -namespace testing { -namespace { - -class Foo : public RefCounted { - public: - Foo() {} -}; - -TEST(RefCounted, Basic) { - Foo* foo = New(); - foo->Unref(); -} - -TEST(RefCounted, ExtraRef) { - Foo* foo = New(); - foo->Ref(); - foo->Unref(); - foo->Unref(); -} - -// Note: We use DebugOnlyTraceFlag instead of TraceFlag to ensure that -// things build properly in both debug and non-debug cases. -DebugOnlyTraceFlag foo_tracer(true, "foo"); - -class FooWithTracing : public RefCountedWithTracing { - public: - FooWithTracing() : RefCountedWithTracing(&foo_tracer) {} -}; - -TEST(RefCountedWithTracing, Basic) { - FooWithTracing* foo = New(); - foo->Ref(DEBUG_LOCATION, "extra_ref"); - foo->Unref(DEBUG_LOCATION, "extra_ref"); - // Can use the no-argument methods, too. - foo->Ref(); - foo->Unref(); - foo->Unref(DEBUG_LOCATION, "original_ref"); -} - -} // namespace -} // namespace testing -} // namespace grpc_core - -int main(int argc, char** argv) { - grpc_test_init(argc, argv); - ::testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} diff --git a/test/core/gprpp/BUILD b/test/core/gprpp/BUILD new file mode 100644 index 0000000000..1c11e0bdb5 --- /dev/null +++ b/test/core/gprpp/BUILD @@ -0,0 +1,96 @@ +# Copyright 2016 gRPC authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +load("//bazel:grpc_build_system.bzl", "grpc_cc_library", "grpc_cc_test", "grpc_cc_binary", "grpc_package") + +licenses(["notice"]) # Apache v2 + +grpc_package(name = "test/core/gprpp") + +grpc_cc_test( + name = "manual_constructor_test", + srcs = ["manual_constructor_test.cc"], + language = "C++", + deps = [ + "//:gpr", + "//:gpr++_base", + "//test/core/util:gpr_test_util", + ], +) + +grpc_cc_test( + name = "memory_test", + srcs = ["memory_test.cc"], + external_deps = [ + "gtest", + ], + language = "C++", + deps = [ + "//:gpr++_base", + "//test/core/util:gpr_test_util", + ], +) + +grpc_cc_test( + name = "inlined_vector_test", + srcs = ["inlined_vector_test.cc"], + external_deps = [ + "gtest", + ], + language = "C++", + deps = [ + "//:inlined_vector", + "//test/core/util:gpr_test_util", + ], +) + +grpc_cc_test( + name = "orphanable_test", + srcs = ["orphanable_test.cc"], + language = "C++", + deps = [ + "//:orphanable", + "//test/core/util:gpr_test_util", + ], + external_deps = [ + "gtest", + ], +) + +grpc_cc_test( + name = "ref_counted_test", + srcs = ["ref_counted_test.cc"], + language = "C++", + deps = [ + "//:ref_counted", + "//test/core/util:gpr_test_util", + ], + external_deps = [ + "gtest", + ], +) + +grpc_cc_test( + name = "ref_counted_ptr_test", + srcs = ["ref_counted_ptr_test.cc"], + language = "C++", + deps = [ + "//:ref_counted", + "//:ref_counted_ptr", + "//test/core/util:gpr_test_util", + ], + external_deps = [ + "gtest", + ], +) diff --git a/test/core/gprpp/inlined_vector_test.cc b/test/core/gprpp/inlined_vector_test.cc new file mode 100644 index 0000000000..0e712dafe4 --- /dev/null +++ b/test/core/gprpp/inlined_vector_test.cc @@ -0,0 +1,74 @@ +/* + * + * Copyright 2017 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include "src/core/lib/gprpp/inlined_vector.h" +#include +#include "src/core/lib/gprpp/memory.h" +#include "test/core/util/test_config.h" + +namespace grpc_core { +namespace testing { + +TEST(InlinedVectorTest, CreateAndIterate) { + const int kNumElements = 9; + InlinedVector v; + for (int i = 0; i < kNumElements; ++i) { + v.push_back(i); + } + EXPECT_EQ(static_cast(kNumElements), v.size()); + for (int i = 0; i < kNumElements; ++i) { + EXPECT_EQ(i, v[i]); + } +} + +TEST(InlinedVectorTest, ValuesAreInlined) { + const int kNumElements = 5; + InlinedVector v; + for (int i = 0; i < kNumElements; ++i) { + v.push_back(i); + } + EXPECT_EQ(static_cast(kNumElements), v.size()); + for (int i = 0; i < kNumElements; ++i) { + EXPECT_EQ(i, v[i]); + } +} + +TEST(InlinedVectorTest, PushBackWithMove) { + InlinedVector, 1> v; + UniquePtr i = MakeUnique(3); + v.push_back(std::move(i)); + EXPECT_EQ(nullptr, i.get()); + EXPECT_EQ(1UL, v.size()); + EXPECT_EQ(3, *v[0]); +} + +TEST(InlinedVectorTest, EmplaceBack) { + InlinedVector, 1> v; + v.emplace_back(New(3)); + EXPECT_EQ(1UL, v.size()); + EXPECT_EQ(3, *v[0]); +} + +} // namespace testing +} // namespace grpc_core + +int main(int argc, char** argv) { + grpc_test_init(argc, argv); + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/test/core/gprpp/manual_constructor_test.cc b/test/core/gprpp/manual_constructor_test.cc new file mode 100644 index 0000000000..f06c3cab06 --- /dev/null +++ b/test/core/gprpp/manual_constructor_test.cc @@ -0,0 +1,99 @@ +/* + * + * Copyright 2017 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +/* Test of gpr synchronization support. */ + +#include "src/core/lib/gprpp/manual_constructor.h" +#include +#include +#include +#include +#include +#include +#include +#include "src/core/lib/gprpp/abstract.h" +#include "test/core/util/test_config.h" + +class A { + public: + A() {} + virtual ~A() {} + virtual const char* foo() { return "A_foo"; } + virtual const char* bar() { return "A_bar"; } + GRPC_ABSTRACT_BASE_CLASS +}; + +class B : public A { + public: + B() {} + ~B() {} + const char* foo() override { return "B_foo"; } + char get_junk() { return junk[0]; } + + private: + char junk[1000]; +}; + +class C : public B { + public: + C() {} + ~C() {} + virtual const char* bar() { return "C_bar"; } + char get_more_junk() { return more_junk[0]; } + + private: + char more_junk[1000]; +}; + +class D : public A { + public: + virtual const char* bar() { return "D_bar"; } +}; + +static void basic_test() { + grpc_core::PolymorphicManualConstructor poly; + poly.Init(); + GPR_ASSERT(!strcmp(poly->foo(), "B_foo")); + GPR_ASSERT(!strcmp(poly->bar(), "A_bar")); +} + +static void complex_test() { + grpc_core::PolymorphicManualConstructor polyB; + polyB.Init(); + GPR_ASSERT(!strcmp(polyB->foo(), "B_foo")); + GPR_ASSERT(!strcmp(polyB->bar(), "A_bar")); + + grpc_core::PolymorphicManualConstructor polyC; + polyC.Init(); + GPR_ASSERT(!strcmp(polyC->foo(), "B_foo")); + GPR_ASSERT(!strcmp(polyC->bar(), "C_bar")); + + grpc_core::PolymorphicManualConstructor polyD; + polyD.Init(); + GPR_ASSERT(!strcmp(polyD->foo(), "A_foo")); + GPR_ASSERT(!strcmp(polyD->bar(), "D_bar")); +} + +/* ------------------------------------------------- */ + +int main(int argc, char* argv[]) { + grpc_test_init(argc, argv); + basic_test(); + complex_test(); + return 0; +} diff --git a/test/core/gprpp/memory_test.cc b/test/core/gprpp/memory_test.cc new file mode 100644 index 0000000000..180c36fad7 --- /dev/null +++ b/test/core/gprpp/memory_test.cc @@ -0,0 +1,74 @@ +/* + * + * Copyright 2017 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include "src/core/lib/gprpp/memory.h" +#include +#include "test/core/util/test_config.h" + +namespace grpc_core { +namespace testing { + +struct Foo { + Foo(int p, int q) : a(p), b(q) {} + int a; + int b; +}; + +TEST(MemoryTest, NewDeleteTest) { Delete(New()); } + +TEST(MemoryTest, NewDeleteWithArgTest) { + int* i = New(42); + EXPECT_EQ(42, *i); + Delete(i); +} + +TEST(MemoryTest, NewDeleteWithArgsTest) { + Foo* p = New(1, 2); + EXPECT_EQ(1, p->a); + EXPECT_EQ(2, p->b); + Delete(p); +} + +TEST(MemoryTest, MakeUniqueTest) { MakeUnique(); } + +TEST(MemoryTest, MakeUniqueWithArgTest) { + auto i = MakeUnique(42); + EXPECT_EQ(42, *i); +} + +TEST(MemoryTest, UniquePtrWithCustomDeleter) { + int n = 0; + class IncrementingDeleter { + public: + void operator()(int* p) { ++*p; } + }; + { + UniquePtr p(&n); + EXPECT_EQ(0, n); + } + EXPECT_EQ(1, n); +} + +} // namespace testing +} // namespace grpc_core + +int main(int argc, char** argv) { + grpc_test_init(argc, argv); + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/test/core/gprpp/orphanable_test.cc b/test/core/gprpp/orphanable_test.cc new file mode 100644 index 0000000000..ff2f6d8bc2 --- /dev/null +++ b/test/core/gprpp/orphanable_test.cc @@ -0,0 +1,114 @@ +/* + * + * Copyright 2017 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include "src/core/lib/gprpp/orphanable.h" + +#include + +#include "src/core/lib/gprpp/memory.h" +#include "test/core/util/test_config.h" + +namespace grpc_core { +namespace testing { +namespace { + +class Foo : public Orphanable { + public: + Foo() : Foo(0) {} + explicit Foo(int value) : value_(value) {} + void Orphan() override { Delete(this); } + int value() const { return value_; } + + private: + int value_; +}; + +TEST(Orphanable, Basic) { + Foo* foo = New(); + foo->Orphan(); +} + +TEST(OrphanablePtr, Basic) { + OrphanablePtr foo(New()); + EXPECT_EQ(0, foo->value()); +} + +TEST(MakeOrphanable, DefaultConstructor) { + auto foo = MakeOrphanable(); + EXPECT_EQ(0, foo->value()); +} + +TEST(MakeOrphanable, WithParameters) { + auto foo = MakeOrphanable(5); + EXPECT_EQ(5, foo->value()); +} + +class Bar : public InternallyRefCounted { + public: + Bar() : Bar(0) {} + explicit Bar(int value) : value_(value) {} + void Orphan() override { Unref(); } + int value() const { return value_; } + + void StartWork() { Ref(); } + void FinishWork() { Unref(); } + + private: + int value_; +}; + +TEST(OrphanablePtr, InternallyRefCounted) { + auto bar = MakeOrphanable(); + bar->StartWork(); + bar->FinishWork(); +} + +// Note: We use DebugOnlyTraceFlag instead of TraceFlag to ensure that +// things build properly in both debug and non-debug cases. +DebugOnlyTraceFlag baz_tracer(true, "baz"); + +class Baz : public InternallyRefCountedWithTracing { + public: + Baz() : Baz(0) {} + explicit Baz(int value) + : InternallyRefCountedWithTracing(&baz_tracer), value_(value) {} + void Orphan() override { Unref(); } + int value() const { return value_; } + + void StartWork() { Ref(DEBUG_LOCATION, "work"); } + void FinishWork() { Unref(DEBUG_LOCATION, "work"); } + + private: + int value_; +}; + +TEST(OrphanablePtr, InternallyRefCountedWithTracing) { + auto baz = MakeOrphanable(); + baz->StartWork(); + baz->FinishWork(); +} + +} // namespace +} // namespace testing +} // namespace grpc_core + +int main(int argc, char** argv) { + grpc_test_init(argc, argv); + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/test/core/gprpp/ref_counted_ptr_test.cc b/test/core/gprpp/ref_counted_ptr_test.cc new file mode 100644 index 0000000000..f1f13f3183 --- /dev/null +++ b/test/core/gprpp/ref_counted_ptr_test.cc @@ -0,0 +1,185 @@ +/* + * + * Copyright 2017 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include "src/core/lib/gprpp/ref_counted_ptr.h" + +#include + +#include + +#include "src/core/lib/gprpp/memory.h" +#include "src/core/lib/gprpp/ref_counted.h" +#include "test/core/util/test_config.h" + +namespace grpc_core { +namespace testing { +namespace { + +class Foo : public RefCounted { + public: + Foo() : value_(0) {} + + explicit Foo(int value) : value_(value) {} + + int value() const { return value_; } + + private: + int value_; +}; + +TEST(RefCountedPtr, DefaultConstructor) { RefCountedPtr foo; } + +TEST(RefCountedPtr, ExplicitConstructorEmpty) { + RefCountedPtr foo(nullptr); +} + +TEST(RefCountedPtr, ExplicitConstructor) { RefCountedPtr foo(New()); } + +TEST(RefCountedPtr, MoveConstructor) { + RefCountedPtr foo(New()); + RefCountedPtr foo2(std::move(foo)); + EXPECT_EQ(nullptr, foo.get()); + EXPECT_NE(nullptr, foo2.get()); +} + +TEST(RefCountedPtr, MoveAssignment) { + RefCountedPtr foo(New()); + RefCountedPtr foo2 = std::move(foo); + EXPECT_EQ(nullptr, foo.get()); + EXPECT_NE(nullptr, foo2.get()); +} + +TEST(RefCountedPtr, CopyConstructor) { + RefCountedPtr foo(New()); + RefCountedPtr foo2(foo); + EXPECT_NE(nullptr, foo.get()); + EXPECT_EQ(foo.get(), foo2.get()); +} + +TEST(RefCountedPtr, CopyAssignment) { + RefCountedPtr foo(New()); + RefCountedPtr foo2 = foo; + EXPECT_NE(nullptr, foo.get()); + EXPECT_EQ(foo.get(), foo2.get()); +} + +TEST(RefCountedPtr, CopyAssignmentWhenEmpty) { + RefCountedPtr foo; + RefCountedPtr foo2; + foo2 = foo; + EXPECT_EQ(nullptr, foo.get()); + EXPECT_EQ(nullptr, foo2.get()); +} + +TEST(RefCountedPtr, CopyAssignmentToSelf) { + RefCountedPtr foo(New()); + foo = foo; +} + +TEST(RefCountedPtr, EnclosedScope) { + RefCountedPtr foo(New()); + { + RefCountedPtr foo2(std::move(foo)); + EXPECT_EQ(nullptr, foo.get()); + EXPECT_NE(nullptr, foo2.get()); + } + EXPECT_EQ(nullptr, foo.get()); +} + +TEST(RefCountedPtr, ResetFromNullToNonNull) { + RefCountedPtr foo; + EXPECT_EQ(nullptr, foo.get()); + foo.reset(New()); + EXPECT_NE(nullptr, foo.get()); +} + +TEST(RefCountedPtr, ResetFromNonNullToNonNull) { + RefCountedPtr foo(New()); + EXPECT_NE(nullptr, foo.get()); + Foo* original = foo.get(); + foo.reset(New()); + EXPECT_NE(nullptr, foo.get()); + EXPECT_NE(original, foo.get()); +} + +TEST(RefCountedPtr, ResetFromNonNullToNull) { + RefCountedPtr foo(New()); + EXPECT_NE(nullptr, foo.get()); + foo.reset(); + EXPECT_EQ(nullptr, foo.get()); +} + +TEST(RefCountedPtr, ResetFromNullToNull) { + RefCountedPtr foo; + EXPECT_EQ(nullptr, foo.get()); + foo.reset(nullptr); + EXPECT_EQ(nullptr, foo.get()); +} + +TEST(RefCountedPtr, DerefernceOperators) { + RefCountedPtr foo(New()); + foo->value(); + Foo& foo_ref = *foo; + foo_ref.value(); +} + +TEST(RefCountedPtr, EqualityOperators) { + RefCountedPtr foo(New()); + RefCountedPtr bar = foo; + RefCountedPtr empty; + // Test equality between RefCountedPtrs. + EXPECT_EQ(foo, bar); + EXPECT_NE(foo, empty); + // Test equality with bare pointers. + EXPECT_EQ(foo, foo.get()); + EXPECT_EQ(empty, nullptr); + EXPECT_NE(foo, nullptr); +} + +TEST(MakeRefCounted, NoArgs) { + RefCountedPtr foo = MakeRefCounted(); + EXPECT_EQ(0, foo->value()); +} + +TEST(MakeRefCounted, Args) { + RefCountedPtr foo = MakeRefCounted(3); + EXPECT_EQ(3, foo->value()); +} + +TraceFlag foo_tracer(true, "foo"); + +class FooWithTracing : public RefCountedWithTracing { + public: + FooWithTracing() : RefCountedWithTracing(&foo_tracer) {} +}; + +TEST(RefCountedPtr, RefCountedWithTracing) { + RefCountedPtr foo(New()); + foo->Ref(DEBUG_LOCATION, "foo"); + foo->Unref(DEBUG_LOCATION, "foo"); +} + +} // namespace +} // namespace testing +} // namespace grpc_core + +int main(int argc, char** argv) { + grpc_test_init(argc, argv); + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/test/core/gprpp/ref_counted_test.cc b/test/core/gprpp/ref_counted_test.cc new file mode 100644 index 0000000000..b1b0fee5c0 --- /dev/null +++ b/test/core/gprpp/ref_counted_test.cc @@ -0,0 +1,74 @@ +/* + * + * Copyright 2017 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include "src/core/lib/gprpp/ref_counted.h" + +#include + +#include "src/core/lib/gprpp/memory.h" +#include "test/core/util/test_config.h" + +namespace grpc_core { +namespace testing { +namespace { + +class Foo : public RefCounted { + public: + Foo() {} +}; + +TEST(RefCounted, Basic) { + Foo* foo = New(); + foo->Unref(); +} + +TEST(RefCounted, ExtraRef) { + Foo* foo = New(); + foo->Ref(); + foo->Unref(); + foo->Unref(); +} + +// Note: We use DebugOnlyTraceFlag instead of TraceFlag to ensure that +// things build properly in both debug and non-debug cases. +DebugOnlyTraceFlag foo_tracer(true, "foo"); + +class FooWithTracing : public RefCountedWithTracing { + public: + FooWithTracing() : RefCountedWithTracing(&foo_tracer) {} +}; + +TEST(RefCountedWithTracing, Basic) { + FooWithTracing* foo = New(); + foo->Ref(DEBUG_LOCATION, "extra_ref"); + foo->Unref(DEBUG_LOCATION, "extra_ref"); + // Can use the no-argument methods, too. + foo->Ref(); + foo->Unref(); + foo->Unref(DEBUG_LOCATION, "original_ref"); +} + +} // namespace +} // namespace testing +} // namespace grpc_core + +int main(int argc, char** argv) { + grpc_test_init(argc, argv); + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} -- cgit v1.2.3