manual_constructor_test.cc 2.4 KB

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