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/range.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 class TestRange : public UT::Common {
28 public:
TestRange()29 TestRange() {}
SetUp()30 void SetUp() {}
TearDown()31 void TearDown() {}
32 };
33
TEST_F(TestRange,test_ops_range1)34 TEST_F(TestRange, test_ops_range1) {
35 auto range = std::make_shared<Range>();
36 range->Init(1, 3, 34, 4);
37 EXPECT_EQ(range->get_d_type(), 1);
38 EXPECT_EQ(range->get_start(), 3);
39 EXPECT_EQ(range->get_limit(), 34);
40 EXPECT_EQ(range->get_delta(), 4);
41 range->set_d_type(1);
42 range->set_start(3);
43 range->set_limit(34);
44 range->set_delta(4);
45 auto abstract = range->Infer({});
46 MS_EXCEPTION_IF_NULL(abstract);
47 EXPECT_EQ(abstract->isa<abstract::AbstractTensor>(), true);
48 auto shape_ptr = abstract->BuildShape();
49 MS_EXCEPTION_IF_NULL(shape_ptr);
50 EXPECT_EQ(shape_ptr->isa<abstract::Shape>(), true);
51 auto shape = shape_ptr->cast<abstract::ShapePtr>();
52 MS_EXCEPTION_IF_NULL(shape);
53 auto shape_vec = shape->shape();
54 auto type = abstract->BuildType();
55 MS_EXCEPTION_IF_NULL(type);
56 EXPECT_EQ(type->isa<TensorType>(), true);
57 auto tensor_type = type->cast<TensorTypePtr>();
58 MS_EXCEPTION_IF_NULL(tensor_type);
59 auto data_type = tensor_type->element();
60 MS_EXCEPTION_IF_NULL(data_type);
61 EXPECT_EQ(data_type->type_id(), kNumberTypeInt32);
62 EXPECT_EQ(shape_vec.size(), 1);
63 EXPECT_EQ(shape_vec[0], 8);
64 EXPECT_EQ(range->get_d_type(), 1);
65 EXPECT_EQ(range->get_start(), 3);
66 EXPECT_EQ(range->get_limit(), 34);
67 EXPECT_EQ(range->get_delta(), 4);
68 }
69
TEST_F(TestRange,test_ops_range2)70 TEST_F(TestRange, test_ops_range2) {
71 auto range = std::make_shared<Range>();
72 range->Init(1, 1, 1, 1);
73 EXPECT_EQ(range->get_d_type(), 1);
74 EXPECT_EQ(range->get_start(), 1);
75 EXPECT_EQ(range->get_limit(), 1);
76 EXPECT_EQ(range->get_delta(), 1);
77 range->set_d_type(1);
78 range->set_start(1);
79 range->set_limit(1);
80 range->set_delta(1);
81 auto tensor_x1 = std::make_shared<tensor::Tensor>(kNumberTypeFloat32, std::vector<int64_t>{1});
82 auto tensor_x2 = std::make_shared<tensor::Tensor>(kNumberTypeFloat32, std::vector<int64_t>{1});
83 auto tensor_x3 = std::make_shared<tensor::Tensor>(kNumberTypeFloat32, std::vector<int64_t>{1});
84 MS_EXCEPTION_IF_NULL(tensor_x1);
85 MS_EXCEPTION_IF_NULL(tensor_x2);
86 MS_EXCEPTION_IF_NULL(tensor_x3);
87 auto data_x1 = tensor_x1->data_c();
88 MS_EXCEPTION_IF_NULL(data_x1);
89 auto val_x1 = reinterpret_cast<float *>(data_x1);
90 *val_x1 = 1.0;
91 auto data_x2 = tensor_x2->data_c();
92 MS_EXCEPTION_IF_NULL(data_x2);
93 auto val_x2 = reinterpret_cast<float *>(data_x2);
94 *val_x2 = 42.0;
95 auto data_x3 = tensor_x3->data_c();
96 MS_EXCEPTION_IF_NULL(data_x3);
97 auto val_x3 = reinterpret_cast<float *>(data_x3);
98 *val_x3 = 3.0;
99 auto abstract = range->Infer({tensor_x1->ToAbstract(), tensor_x2->ToAbstract(), tensor_x3->ToAbstract()});
100 MS_EXCEPTION_IF_NULL(abstract);
101 EXPECT_EQ(abstract->isa<abstract::AbstractTensor>(), true);
102 auto shape_ptr = abstract->BuildShape();
103 MS_EXCEPTION_IF_NULL(shape_ptr);
104 EXPECT_EQ(shape_ptr->isa<abstract::Shape>(), true);
105 auto shape = shape_ptr->cast<abstract::ShapePtr>();
106 MS_EXCEPTION_IF_NULL(shape);
107 auto shape_vec = shape->shape();
108 auto type = abstract->BuildType();
109 MS_EXCEPTION_IF_NULL(type);
110 EXPECT_EQ(type->isa<TensorType>(), true);
111 auto tensor_type = type->cast<TensorTypePtr>();
112 MS_EXCEPTION_IF_NULL(tensor_type);
113 auto data_type = tensor_type->element();
114 MS_EXCEPTION_IF_NULL(data_type);
115 EXPECT_EQ(data_type->type_id(), kNumberTypeFloat32);
116 EXPECT_EQ(shape_vec.size(), 1);
117 EXPECT_EQ(shape_vec[0], 14);
118 EXPECT_EQ(range->get_d_type(), 1);
119 EXPECT_EQ(range->get_start(), 1);
120 EXPECT_EQ(range->get_limit(), 1);
121 EXPECT_EQ(range->get_delta(), 1);
122 }
123 } // namespace ops
124 } // namespace mindspore
125