3 * Copyright 2017 gRPC authors.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 #include "src/core/lib/gprpp/orphanable.h"
21 #include <gtest/gtest.h>
23 #include "src/core/lib/gprpp/memory.h"
24 #include "test/core/util/test_config.h"
30 class Foo : public Orphanable {
33 explicit Foo(int value) : value_(value) {}
34 void Orphan() override { delete this; }
35 int value() const { return value_; }
41 TEST(Orphanable, Basic) {
46 TEST(OrphanablePtr, Basic) {
47 OrphanablePtr<Foo> foo(new Foo());
48 EXPECT_EQ(0, foo->value());
51 TEST(MakeOrphanable, DefaultConstructor) {
52 auto foo = MakeOrphanable<Foo>();
53 EXPECT_EQ(0, foo->value());
56 TEST(MakeOrphanable, WithParameters) {
57 auto foo = MakeOrphanable<Foo>(5);
58 EXPECT_EQ(5, foo->value());
61 class Bar : public InternallyRefCounted<Bar> {
64 explicit Bar(int value) : value_(value) {}
65 void Orphan() override { Unref(); }
66 int value() const { return value_; }
68 void StartWork() { self_ref_ = Ref(); }
69 void FinishWork() { self_ref_.reset(); }
73 RefCountedPtr<Bar> self_ref_;
76 TEST(OrphanablePtr, InternallyRefCounted) {
77 auto bar = MakeOrphanable<Bar>();
82 class Baz : public InternallyRefCounted<Baz> {
85 explicit Baz(int value) : InternallyRefCounted<Baz>("Baz"), value_(value) {}
86 void Orphan() override { Unref(); }
87 int value() const { return value_; }
89 void StartWork() { self_ref_ = Ref(DEBUG_LOCATION, "work"); }
91 // This is a little ugly, but it makes the logged ref and unref match up.
93 Unref(DEBUG_LOCATION, "work");
98 RefCountedPtr<Baz> self_ref_;
101 TEST(OrphanablePtr, InternallyRefCountedWithTracing) {
102 auto baz = MakeOrphanable<Baz>();
108 } // namespace testing
109 } // namespace grpc_core
111 int main(int argc, char** argv) {
112 grpc::testing::TestEnvironment env(argc, argv);
113 ::testing::InitGoogleTest(&argc, argv);
114 return RUN_ALL_TESTS();