• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <vector>
17 #include <memory>
18 #include "common/common_test.h"
19 #include "ops/assert.h"
20 #include "ir/dtype/type.h"
21 #include "ir/value.h"
22 #include "abstract/dshape.h"
23 #include "utils/tensor_construct_utils.h"
24 
25 namespace mindspore {
26 namespace ops {
27 
28 namespace {
29 template <typename T>
SetTensorData(void * data,T num,size_t data_length)30 void SetTensorData(void *data, T num, size_t data_length) {
31   MS_EXCEPTION_IF_NULL(data);
32   auto tensor_data = reinterpret_cast<T *>(data);
33   MS_EXCEPTION_IF_NULL(tensor_data);
34   for (size_t index = 0; index < data_length; ++index) {
35     *tensor_data = num;
36     ++tensor_data;
37   }
38 }
39 }  // namespace
40 
41 class TestAssert : public UT::Common {
42  public:
TestAssert()43   TestAssert() {}
SetUp()44   void SetUp() {}
TearDown()45   void TearDown() {}
46 };
47 
TEST_F(TestAssert,test_ops_assert1)48 TEST_F(TestAssert, test_ops_assert1) {
49   auto assert = std::make_shared<Assert>();
50   assert->Init(3);
51   EXPECT_EQ(assert->get_summarize(), 3);
52   std::vector<ValuePtr> inputs_ = {TensorConstructUtils::CreateOnesTensor(kNumberTypeFloat32, std::vector<int64_t>{1})};
53   auto condition = MakeValue(std::vector<bool>{true});
54   auto inputs = std::make_shared<ValueTuple>(inputs_);
55   auto abstract = assert->Infer({condition->ToAbstract(), inputs->ToAbstract()});
56   MS_EXCEPTION_IF_NULL(abstract);
57   EXPECT_EQ(abstract->isa<abstract::AbstractTensor>(), true);
58   auto shape_ptr = abstract->BuildShape();
59   MS_EXCEPTION_IF_NULL(shape_ptr);
60   EXPECT_EQ(shape_ptr->isa<abstract::Shape>(), true);
61   auto shape = shape_ptr->cast<abstract::ShapePtr>();
62   MS_EXCEPTION_IF_NULL(shape);
63   auto shape_vec = shape->shape();
64   EXPECT_EQ(shape_vec.size(), 1);
65   EXPECT_EQ(shape_vec[0], 1);
66   auto type = abstract->BuildType();
67   MS_EXCEPTION_IF_NULL(type);
68   EXPECT_EQ(type->isa<TensorType>(), true);
69   auto tensor_type = type->cast<TensorTypePtr>();
70   MS_EXCEPTION_IF_NULL(tensor_type);
71   auto data_type = tensor_type->element();
72   MS_EXCEPTION_IF_NULL(data_type);
73   EXPECT_EQ(data_type->type_id(), kNumberTypeInt32);
74 }
75 
TEST_F(TestAssert,test_ops_assert2)76 TEST_F(TestAssert, test_ops_assert2) {
77   auto assert = std::make_shared<Assert>();
78   assert->Init(3);
79   EXPECT_EQ(assert->get_summarize(), 3);
80   std::vector<ValuePtr> inputs_ = {TensorConstructUtils::CreateOnesTensor(kNumberTypeFloat32, std::vector<int64_t>{1})};
81   auto tensor = std::make_shared<tensor::Tensor>(kNumberTypeBool, std::vector<int64_t>{1});
82   MS_EXCEPTION_IF_NULL(tensor);
83   auto mem_size = IntToSize(tensor->ElementsNum());
84   SetTensorData<bool>(tensor->data_c(), true, mem_size);
85   auto inputs = std::make_shared<ValueTuple>(inputs_);
86   auto abstract = assert->Infer({tensor->ToAbstract(), inputs->ToAbstract()});
87   MS_EXCEPTION_IF_NULL(abstract);
88   EXPECT_EQ(abstract->isa<abstract::AbstractTensor>(), true);
89   auto shape_ptr = abstract->BuildShape();
90   MS_EXCEPTION_IF_NULL(shape_ptr);
91   EXPECT_EQ(shape_ptr->isa<abstract::Shape>(), true);
92   auto shape = shape_ptr->cast<abstract::ShapePtr>();
93   MS_EXCEPTION_IF_NULL(shape);
94   auto shape_vec = shape->shape();
95   EXPECT_EQ(shape_vec.size(), 1);
96   EXPECT_EQ(shape_vec[0], 1);
97   auto type = abstract->BuildType();
98   MS_EXCEPTION_IF_NULL(type);
99   EXPECT_EQ(type->isa<TensorType>(), true);
100   auto tensor_type = type->cast<TensorTypePtr>();
101   MS_EXCEPTION_IF_NULL(tensor_type);
102   auto data_type = tensor_type->element();
103   MS_EXCEPTION_IF_NULL(data_type);
104   EXPECT_EQ(data_type->type_id(), kNumberTypeInt32);
105 }
106 
107 }  // namespace ops
108 }  // namespace mindspore
109