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 18 #include "common/common_test.h" 19 #include "utils/any.h" 20 #include "base/base.h" 21 #include "ir/anf.h" 22 #include "utils/log_adapter.h" 23 24 namespace mindspore { 25 26 class TestNode : public UT::Common { 27 public: 28 TestNode() {} 29 }; 30 31 class ChildA : public Base { 32 public: 33 ChildA() {} 34 ~ChildA() {} 35 MS_DECLARE_PARENT(ChildA, Base); 36 std::string name() { return "ChildA"; } 37 std::size_t hash() const override { return 1; } 38 }; 39 class ChildAA : public ChildA { 40 public: 41 ChildAA() {} 42 ~ChildAA() {} 43 MS_DECLARE_PARENT(ChildAA, ChildA); 44 std::size_t hash() const override { return 1; } 45 std::string name() { return "ChildAA"; } 46 }; 47 48 class ChildB : public Base { 49 public: 50 ChildB() {} 51 ~ChildB() {} 52 MS_DECLARE_PARENT(ChildB, Base); 53 std::size_t hash() const override { return 1; } 54 std::string name() { return "ChildB"; } 55 }; 56 57 TEST_F(TestNode, test_dyn_cast) { 58 auto aa = std::make_shared<ChildAA>(); 59 std::shared_ptr<Base> n = aa; 60 MS_LOG(INFO) << "aa ptr_name: " << aa->name(); 61 MS_LOG(INFO) << "aa type_name: " << aa->type_name(); 62 MS_LOG(INFO) << "n ptr_name: " << demangle(typeid(n).name()); 63 MS_LOG(INFO) << "n type_name: " << n->type_name(); 64 ASSERT_TRUE(n != nullptr); 65 ASSERT_EQ(std::string(n->type_name().c_str()), "ChildAA"); 66 auto a = dyn_cast<ChildA>(n); 67 MS_LOG(INFO) << "a ptr_name: " << a->name(); 68 MS_LOG(INFO) << "a type_name: " << a->type_name(); 69 ASSERT_TRUE(a != nullptr); 70 ASSERT_EQ(std::string(a->name()), "ChildA"); 71 ASSERT_EQ(std::string(a->type_name().c_str()), "ChildAA"); 72 auto b_null = dyn_cast<ChildB>(n); 73 ASSERT_TRUE(b_null == nullptr); 74 75 ChildA* pa = cast<ChildA>(n.get()); 76 ASSERT_TRUE(pa != nullptr); 77 MS_LOG(INFO) << "a ptr_name: " << pa->name(); 78 MS_LOG(INFO) << "a type_name: " << pa->type_name(); 79 } 80 81 TEST_F(TestNode, test_isa) { 82 auto a = std::make_shared<ChildA>(); 83 BasePtr n = a; 84 ASSERT_TRUE(n->isa<ChildA>() == true); 85 ASSERT_TRUE(n->isa<ChildAA>() == false); 86 87 auto aa = std::make_shared<ChildAA>(); 88 n = aa; 89 ASSERT_TRUE(n->isa<ChildA>() == true); 90 ASSERT_TRUE(n->isa<ChildAA>() == true); 91 92 auto b = std::make_shared<ChildB>(); 93 n = b; 94 ASSERT_TRUE(n->isa<ChildB>() == true); 95 ASSERT_TRUE(n->isa<ChildA>() == false); 96 ASSERT_TRUE(n->isa<ChildAA>() == false); 97 } 98 99 } // namespace mindspore 100