Debian Codegraph @

test/core/gprpp/orphanable_test.cc

raw
1 /*
2 *
3 * Copyright 2017 gRPC authors.
4 *
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
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 *
17 */
18
19 #include "src/core/lib/gprpp/orphanable.h"
20
21 #include "gtest/gtest.h"
22
23 #include "test/core/util/test_config.h"
24
25 namespace grpc_core {
26 namespace testing {
27 namespace {
28
29 class Foo : public Orphanable {
30 public:
31 Foo() : Foo(0) {}
32 explicit Foo(int value) : value_(value) {}
33 void Orphan() override { delete this; }
34 int value() const { return value_; }
35
36 private:
37 int value_;
38 };
39
40 TEST(Orphanable, Basic) {
41 Foo* foo = new Foo();
42 foo->Orphan();
43 }
44
45 TEST(OrphanablePtr, Basic) {
46 OrphanablePtr<Foo> foo(new Foo());
47 EXPECT_EQ(0, foo->value());
48 }
49
50 TEST(MakeOrphanable, DefaultConstructor) {
51 auto foo = MakeOrphanable<Foo>();
52 EXPECT_EQ(0, foo->value());
53 }
54
55 TEST(MakeOrphanable, WithParameters) {
56 auto foo = MakeOrphanable<Foo>(5);
57 EXPECT_EQ(5, foo->value());
58 }
59
60 class Bar : public InternallyRefCounted<Bar> {
61 public:
62 Bar() : Bar(0) {}
63 explicit Bar(int value) : value_(value) {}
64 void Orphan() override { Unref(); }
65 int value() const { return value_; }
66
67 void StartWork() { self_ref_ = Ref(); }
68 void FinishWork() { self_ref_.reset(); }
69
70 private:
71 int value_;
72 RefCountedPtr<Bar> self_ref_;
73 };
74
75 TEST(OrphanablePtr, InternallyRefCounted) {
76 auto bar = MakeOrphanable<Bar>();
77 bar->StartWork();
78 bar->FinishWork();
79 }
80
81 class Baz : public InternallyRefCounted<Baz> {
82 public:
83 Baz() : Baz(0) {}
84 explicit Baz(int value) : InternallyRefCounted<Baz>("Baz"), value_(value) {}
85 void Orphan() override { Unref(); }
86 int value() const { return value_; }
87
88 void StartWork() { self_ref_ = Ref(DEBUG_LOCATION, "work"); }
89 void FinishWork() {
90 // This is a little ugly, but it makes the logged ref and unref match up.
91 self_ref_.release();
92 Unref(DEBUG_LOCATION, "work");
93 }
94
95 private:
96 int value_;
97 RefCountedPtr<Baz> self_ref_;
98 };
99
100 TEST(OrphanablePtr, InternallyRefCountedWithTracing) {
101 auto baz = MakeOrphanable<Baz>();
102 baz->StartWork();
103 baz->FinishWork();
104 }
105
106 } // namespace
107 } // namespace testing
108 } // namespace grpc_core
109
110 int main(int argc, char** argv) {
111 grpc::testing::TestEnvironment env(&argc, argv);
112 ::testing::InitGoogleTest(&argc, argv);
113 return RUN_ALL_TESTS();
114 }