You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

base_test.cc 2.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <memory>
  17. #include "common/common_test.h"
  18. #include "utils/any.h"
  19. #include "base/base.h"
  20. #include "ir/anf.h"
  21. #include "utils/log_adapter.h"
  22. namespace mindspore {
  23. class TestNode : public UT::Common {
  24. public:
  25. TestNode() {}
  26. };
  27. class ChildA : public Base {
  28. public:
  29. ChildA() {}
  30. ~ChildA() {}
  31. MS_DECLARE_PARENT(ChildA, Base);
  32. std::string name() { return "ChildA"; }
  33. std::size_t hash() const override { return 1; }
  34. };
  35. class ChildAA : public ChildA {
  36. public:
  37. ChildAA() {}
  38. ~ChildAA() {}
  39. MS_DECLARE_PARENT(ChildAA, ChildA);
  40. std::size_t hash() const override { return 1; }
  41. std::string name() { return "ChildAA"; }
  42. };
  43. class ChildB : public Base {
  44. public:
  45. ChildB() {}
  46. ~ChildB() {}
  47. MS_DECLARE_PARENT(ChildB, Base);
  48. std::size_t hash() const override { return 1; }
  49. std::string name() { return "ChildB"; }
  50. };
  51. TEST_F(TestNode, test_dyn_cast) {
  52. auto aa = std::make_shared<ChildAA>();
  53. std::shared_ptr<Base> n = aa;
  54. MS_LOG(INFO) << "aa ptr_name: " << aa->name();
  55. MS_LOG(INFO) << "aa type_name: " << aa->type_name();
  56. MS_LOG(INFO) << "n ptr_name: " << demangle(typeid(n).name());
  57. MS_LOG(INFO) << "n type_name: " << n->type_name();
  58. ASSERT_TRUE(n != nullptr);
  59. ASSERT_EQ(std::string(n->type_name().c_str()), "ChildAA");
  60. auto a = dyn_cast<ChildA>(n);
  61. MS_LOG(INFO) << "a ptr_name: " << a->name();
  62. MS_LOG(INFO) << "a type_name: " << a->type_name();
  63. ASSERT_TRUE(a != nullptr);
  64. ASSERT_EQ(std::string(a->name()), "ChildA");
  65. ASSERT_EQ(std::string(a->type_name().c_str()), "ChildAA");
  66. auto b_null = dyn_cast<ChildB>(n);
  67. ASSERT_TRUE(b_null == nullptr);
  68. ChildA* pa = cast<ChildA>(n.get());
  69. ASSERT_TRUE(pa != nullptr);
  70. MS_LOG(INFO) << "a ptr_name: " << pa->name();
  71. MS_LOG(INFO) << "a type_name: " << pa->type_name();
  72. }
  73. TEST_F(TestNode, test_isa) {
  74. auto a = std::make_shared<ChildA>();
  75. BasePtr n = a;
  76. ASSERT_TRUE(n->isa<ChildA>() == true);
  77. ASSERT_TRUE(n->isa<ChildAA>() == false);
  78. auto aa = std::make_shared<ChildAA>();
  79. n = aa;
  80. ASSERT_TRUE(n->isa<ChildA>() == true);
  81. ASSERT_TRUE(n->isa<ChildAA>() == true);
  82. auto b = std::make_shared<ChildB>();
  83. n = b;
  84. ASSERT_TRUE(n->isa<ChildB>() == true);
  85. ASSERT_TRUE(n->isa<ChildA>() == false);
  86. ASSERT_TRUE(n->isa<ChildAA>() == false);
  87. }
  88. } // namespace mindspore