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