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.

value_test.cc 3.2 kB

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