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.

baseref_test.cc 3.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 <memory>
  18. #include "common/common_test.h"
  19. #include "ir/anf.h"
  20. #include "base/base_ref.h"
  21. namespace mindspore {
  22. namespace utils {
  23. class TestBaseRef : public UT::Common {
  24. public:
  25. TestBaseRef() {}
  26. virtual void SetUp() {}
  27. virtual void TearDown() {}
  28. };
  29. TEST_F(TestBaseRef, TestScalar) {
  30. BaseRef a = static_cast<int64_t>(1);
  31. BaseRef b = 1.0;
  32. if (isa<int64_t>(a)) {
  33. ASSERT_EQ(cast<int64_t>(a), 1);
  34. Int64ImmPtr c = cast<Int64ImmPtr>(a);
  35. ASSERT_EQ(cast<int64_t>(c), 1);
  36. }
  37. ASSERT_TRUE(isa<Int64Imm>(a));
  38. ASSERT_TRUE(isa<BaseRef>(a));
  39. ASSERT_TRUE(isa<double>(b));
  40. ASSERT_TRUE(isa<FP64Imm>(b));
  41. BaseRef c = static_cast<int64_t>(1);
  42. ASSERT_EQ(a == c, true);
  43. }
  44. void func(const BaseRef& sexp) {
  45. if (isa<VectorRef>(sexp)) {
  46. const VectorRef& a = cast<VectorRef>(sexp);
  47. for (size_t i = 0; i < a.size(); i++) {
  48. BaseRef v = a[i];
  49. MS_LOG(INFO) << "for is i:" << i << ", " << v.ToString() << "\n";
  50. }
  51. MS_LOG(INFO) << "in func is valuesequeue:" << sexp.ToString() << "\n";
  52. }
  53. }
  54. TEST_F(TestBaseRef, TestNode) {
  55. AnfNodePtr anf = NewValueNode(static_cast<int64_t>(1));
  56. BaseRef d = anf;
  57. MS_LOG(INFO) << "anf typeid:" << dyn_cast<AnfNode>(anf).get();
  58. MS_LOG(INFO) << "anf typeid:" << NewValueNode(static_cast<int64_t>(1))->tid();
  59. MS_LOG(INFO) << "node reftypeid:" << d.tid();
  60. ASSERT_EQ(isa<AnfNodePtr>(d), true);
  61. ASSERT_EQ(isa<AnfNode>(d), true);
  62. ASSERT_EQ(isa<ValueNode>(d), true);
  63. AnfNodePtr c = cast<ValueNodePtr>(d);
  64. ASSERT_NE(c, nullptr);
  65. }
  66. TEST_F(TestBaseRef, TestVector) {
  67. AnfNodePtr anf = NewValueNode(static_cast<int64_t>(1));
  68. VectorRef a({static_cast<int64_t>(1), static_cast<int64_t>(2), anf, NewValueNode(static_cast<int64_t>(1))});
  69. ASSERT_TRUE(isa<VectorRef>(a));
  70. func(a);
  71. BaseRef b;
  72. b = static_cast<int64_t>(1);
  73. ASSERT_TRUE(isa<int64_t>(b));
  74. std::vector<int64_t> int64({1, 2, 3});
  75. VectorRef k;
  76. k.insert(k.end(), int64.begin(), int64.end());
  77. k = a;
  78. func(k);
  79. BaseRef c = std::make_shared<VectorRef>(a);
  80. BaseRef c1 = std::make_shared<VectorRef>(a);
  81. ASSERT_TRUE(c == c1);
  82. ASSERT_TRUE(isa<VectorRef>(c));
  83. VectorRefPtr d = cast<VectorRefPtr>(c);
  84. ASSERT_TRUE(isa<VectorRef>(d));
  85. VectorRef e1({static_cast<int64_t>(1), static_cast<int64_t>(2), anf});
  86. VectorRef e({static_cast<int64_t>(1), static_cast<int64_t>(2), anf});
  87. ASSERT_EQ(e1 == e, true);
  88. }
  89. } // namespace utils
  90. } // namespace mindspore