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 <iostream> 17 #include <sstream> 18 #include <memory> 19 #include <algorithm> 20 #include <unordered_map> 21 22 #include "common/common_test.h" 23 #include "ir/value.h" 24 #include "abstract/abstract_value.h" 25 #include "utils/log_adapter.h" 26 27 namespace mindspore { 28 using AbstractScalar = abstract::AbstractScalar; 29 using AbstractTuple = abstract::AbstractTuple; 30 using AbstractBasePtrList = abstract::AbstractBasePtrList; 31 32 class TestValue : public UT::Common { 33 public: 34 TestValue() {} 35 }; 36 37 TEST_F(TestValue, test_int64) { 38 auto i64a = std::make_shared<Int64Imm>(2); 39 ASSERT_TRUE(i64a != nullptr); 40 } 41 42 TEST_F(TestValue, testToAbstract) { 43 ValuePtr boolv = std::make_shared<BoolImm>(true); 44 AbstractBasePtr boola = std::make_shared<AbstractScalar>(true); 45 AbstractBasePtr ret = boolv->ToAbstract(); 46 ASSERT_TRUE(ret); 47 ASSERT_EQ(*(ret), *(boola)); 48 ASSERT_FALSE(*(ret) == *(std::make_shared<AbstractScalar>(false))); 49 ASSERT_FALSE(*(ret) == *(std::make_shared<AbstractScalar>(static_cast<int64_t>(2)))); 50 51 ValuePtr i64v = std::make_shared<Int64Imm>(2); 52 AbstractBasePtr i64a = std::make_shared<AbstractScalar>(static_cast<int64_t>(2)); 53 ret = i64v->ToAbstract(); 54 ASSERT_TRUE(ret); 55 ASSERT_EQ(*(ret), *(i64a)); 56 57 ValuePtr f32v = std::make_shared<FP32Imm>(1.0); 58 AbstractBasePtr f32a = std::make_shared<AbstractScalar>(1.0f); 59 ret = f32v->ToAbstract(); 60 ASSERT_TRUE(ret); 61 ASSERT_EQ(*(ret), *(f32a)); 62 63 ValuePtr sv = std::make_shared<StringImm>("_"); 64 AbstractBasePtr sa = std::make_shared<AbstractScalar>(std::string("_")); 65 ret = sv->ToAbstract(); 66 ASSERT_TRUE(ret); 67 ASSERT_EQ(*(ret), *(sa)); 68 69 ValuePtr vv = std::make_shared<AnyValue>(); 70 AbstractBasePtr va = std::make_shared<AbstractScalar>(); 71 ret = vv->ToAbstract(); 72 ASSERT_TRUE(ret); 73 ASSERT_EQ(*(ret), *(va)); 74 75 ValuePtr tv = std::make_shared<ValueTuple>(std::vector<ValuePtr>({boolv, i64v, f32v, sv, vv})); 76 AbstractBasePtr ta = std::make_shared<AbstractTuple>(AbstractBasePtrList({boola, i64a, f32a, sa, va})); 77 ret = tv->ToAbstract(); 78 ASSERT_TRUE(ret); 79 ASSERT_EQ(*(ret), *(ta)); 80 81 ValuePtr rv = std::make_shared<RefKey>("net.weight"); 82 abstract::AbstractRefKeyPtr ra = std::make_shared<abstract::AbstractRefKey>(); 83 ra->set_value(rv); 84 ret = rv->ToAbstract(); 85 ASSERT_TRUE(ret); 86 ASSERT_EQ(*(ret), *(ra)); 87 } 88 89 TEST_F(TestValue, GetValue) { 90 ValuePtr fv = MakeValue("test"); 91 const char* fv_c = GetValue<const char*>(fv); 92 MS_LOG(INFO) << "" << fv_c; 93 MS_LOG(INFO) << "" << GetValue<const char*>(fv); 94 ASSERT_TRUE(fv_c != nullptr); 95 } 96 } // namespace mindspore 97