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 /* Test of gpr synchronization support. */
20
21 #include "src/core/lib/gprpp/manual_constructor.h"
22 #include <grpc/support/alloc.h>
23 #include <grpc/support/log.h>
24 #include <grpc/support/sync.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <cstring>
28
29 #include "test/core/util/test_config.h"
30
31 class A {
32 public:
A()33 A() {}
~A()34 virtual ~A() {}
foo()35 virtual const char* foo() { return "A_foo"; }
bar()36 virtual const char* bar() { return "A_bar"; }
37 };
38
39 class B : public A {
40 public:
B()41 B() {}
~B()42 ~B() {}
foo()43 const char* foo() override { return "B_foo"; }
get_junk()44 char get_junk() { return junk[0]; }
45
46 private:
47 char junk[1000];
48 };
49
50 class C : public B {
51 public:
C()52 C() {}
~C()53 ~C() {}
bar()54 virtual const char* bar() { return "C_bar"; }
get_more_junk()55 char get_more_junk() { return more_junk[0]; }
56
57 private:
58 char more_junk[1000];
59 };
60
61 class D : public A {
62 public:
bar()63 virtual const char* bar() { return "D_bar"; }
64 };
65
basic_test()66 static void basic_test() {
67 grpc_core::PolymorphicManualConstructor<A, B> poly;
68 poly.Init<B>();
69 GPR_ASSERT(!strcmp(poly->foo(), "B_foo"));
70 GPR_ASSERT(!strcmp(poly->bar(), "A_bar"));
71 }
72
complex_test()73 static void complex_test() {
74 grpc_core::PolymorphicManualConstructor<A, B, C, D> polyB;
75 polyB.Init<B>();
76 GPR_ASSERT(!strcmp(polyB->foo(), "B_foo"));
77 GPR_ASSERT(!strcmp(polyB->bar(), "A_bar"));
78
79 grpc_core::PolymorphicManualConstructor<A, B, C, D> polyC;
80 polyC.Init<C>();
81 GPR_ASSERT(!strcmp(polyC->foo(), "B_foo"));
82 GPR_ASSERT(!strcmp(polyC->bar(), "C_bar"));
83
84 grpc_core::PolymorphicManualConstructor<A, B, C, D> polyD;
85 polyD.Init<D>();
86 GPR_ASSERT(!strcmp(polyD->foo(), "A_foo"));
87 GPR_ASSERT(!strcmp(polyD->bar(), "D_bar"));
88 }
89
90 /* ------------------------------------------------- */
91
main(int argc,char * argv[])92 int main(int argc, char* argv[]) {
93 grpc::testing::TestEnvironment env(argc, argv);
94 basic_test();
95 complex_test();
96 return 0;
97 }
98