xref: /aosp_15_r20/external/grpc-grpc/test/core/gprpp/down_cast_test.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 // Copyright 2024 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "src/core/lib/gprpp/down_cast.h"
16 
17 #include "gtest/gtest.h"
18 
19 namespace grpc_core {
20 namespace {
21 
22 class Base {
23  public:
24   virtual ~Base() = default;
25 };
26 
27 class Derived : public Base {
28  public:
29   int i = 3;
30 };
31 
TEST(DownCastTest,DownCast)32 TEST(DownCastTest, DownCast) {
33   Derived d;
34   Base* b = &d;
35   EXPECT_EQ(DownCast<Derived*>(b)->i, 3);
36   EXPECT_EQ(DownCast<Derived&>(*b).i, 3);
37 }
38 
39 }  // namespace
40 }  // namespace grpc_core
41 
main(int argc,char ** argv)42 int main(int argc, char** argv) {
43   ::testing::InitGoogleTest(&argc, argv);
44   return RUN_ALL_TESTS();
45 }
46