1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "ops/squeeze_builder.h"
17 
18 #include <gtest/gtest.h>
19 #include "nn_tensor.h"
20 #include "ops_test.h"
21 
22 using namespace testing;
23 using namespace testing::ext;
24 using namespace OHOS::NeuralNetworkRuntime::Ops;
25 
26 namespace OHOS {
27 namespace NeuralNetworkRuntime {
28 namespace UnitTest {
29 class SqueezeBuilderTest : public OpsTest {
30 protected:
31     void InitTensor(const std::vector<uint32_t>& inputsIndex,
32         const std::vector<uint32_t>& outputsIndex) override;
33     void SaveAxisTensor(OH_NN_DataType dataType, const std::vector<int32_t> &dim,
34         const OH_NN_QuantParam* quantParam, OH_NN_TensorType type);
35 
36 protected:
37     SqueezeBuilder m_builder;
38     std::vector<int64_t> m_expectAxisValue;
39 };
40 
SaveAxisTensor(OH_NN_DataType dataType,const std::vector<int32_t> & dim,const OH_NN_QuantParam * quantParam,OH_NN_TensorType type)41 void SqueezeBuilderTest::SaveAxisTensor(OH_NN_DataType dataType, const std::vector<int32_t> &dim,
42     const OH_NN_QuantParam* quantParam, OH_NN_TensorType type)
43 {
44     std::shared_ptr<NNTensor> axisTensor =TransToNNTensor(dataType, dim, quantParam, type);
45     int64_t* axisValue = new (std::nothrow) int64_t[1]{2};
46     EXPECT_NE(nullptr, axisValue);
47     axisTensor->SetBuffer(axisValue, sizeof(int64_t));
48     m_allTensors.emplace_back(axisTensor);
49     m_expectAxisValue.emplace_back(*axisValue);
50 }
51 
InitTensor(const std::vector<uint32_t> & inputsIndex,const std::vector<uint32_t> & outputsIndex)52 void SqueezeBuilderTest::InitTensor(const std::vector<uint32_t>& inputsIndex,
53     const std::vector<uint32_t>& outputsIndex)
54 {
55     std::vector<uint32_t> paramsIndex = { 2 };
56     std::vector<int32_t> inputDim = {3, 2, 1};
57     std::vector<int32_t> OutputDim = {3, 2};
58 
59     m_paramsIndex = paramsIndex;
60     SaveInputTensor(inputsIndex, OH_NN_FLOAT32, inputDim, nullptr);
61     SaveOutputTensor(outputsIndex, OH_NN_FLOAT32, OutputDim, nullptr);
62 }
63 
64 /**
65  * @tc.name: squeeze_build_001
66  * @tc.desc: Provide normal input, output, and parameters to verify the normal behavior of the Build function
67  * @tc.type: FUNC
68  */
69 HWTEST_F(SqueezeBuilderTest, squeeze_build_001, TestSize.Level0)
70 {
71     std::vector<uint32_t> inputsIndex = { 0 };
72     std::vector<uint32_t> outputsIndex = { 1 };
73     std::vector<int32_t> paramDim = {};
74 
75     InitTensor(inputsIndex, outputsIndex);
76     SaveAxisTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_SQUEEZE_AXIS);
77 
78     OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
79     EXPECT_EQ(OH_NN_SUCCESS, ret);
80 }
81 
82 /**
83  * @tc.name: squeeze_build_002
84  * @tc.desc: Call Build func twice to verify the abnormal behavior of the Build function
85  * @tc.type: FUNC
86  */
87 HWTEST_F(SqueezeBuilderTest, squeeze_build_002, TestSize.Level0)
88 {
89     std::vector<uint32_t> inputsIndex = { 0 };
90     std::vector<uint32_t> outputsIndex = { 1 };
91     std::vector<int32_t> paramDim = {};
92 
93     InitTensor(inputsIndex, outputsIndex);
94     SaveAxisTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_SQUEEZE_AXIS);
95 
96     EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
97     OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
98     EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, ret);
99 }
100 
101 /**
102  * @tc.name: squeeze_build_003
103  * @tc.desc: Provide one more than normal input to verify the abnormal behavior of the Build function
104  * @tc.type: FUNC
105  */
106 HWTEST_F(SqueezeBuilderTest, squeeze_build_003, TestSize.Level0)
107 {
108     std::vector<uint32_t> inputsIndex = { 0, 1, 2 };
109     std::vector<uint32_t> outputsIndex = { 3 };
110     std::vector<int32_t> paramDim = {};
111 
112     InitTensor(inputsIndex, outputsIndex);
113     SaveAxisTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_SQUEEZE_AXIS);
114 
115     OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
116     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
117 }
118 
119 /**
120  * @tc.name: squeeze_build_004
121  * @tc.desc: Provide one more than normal output to verify the abnormal behavior of the Build function
122  * @tc.type: FUNC
123  */
124 HWTEST_F(SqueezeBuilderTest, squeeze_build_004, TestSize.Level0)
125 {
126     std::vector<uint32_t> inputsIndex = { 0 };
127     std::vector<uint32_t> outputsIndex = { 1, 2 };
128     std::vector<int32_t> paramDim = {};
129 
130     InitTensor(inputsIndex, outputsIndex);
131     SaveAxisTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_SQUEEZE_AXIS);
132 
133     OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
134     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
135 }
136 
137 /**
138  * @tc.name: squeeze_build_005
139  * @tc.desc: Provide empty input, output, and parameters to verify the abnormal behavior of the Build function
140  * @tc.type: FUNC
141  */
142 HWTEST_F(SqueezeBuilderTest, squeeze_build_005, TestSize.Level0)
143 {
144     std::vector<uint32_t> inputsIndex = { 0, 1 };
145     std::vector<uint32_t> outputsIndex = { 2 };
146     std::vector<uint32_t> paramsIndex = { 3 };
147 
148     OH_NN_ReturnCode ret = m_builder.Build(paramsIndex, inputsIndex, outputsIndex, m_allTensors);
149     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
150 }
151 
152 /**
153  * @tc.name: squeeze_build_006
154  * @tc.desc: Provide empty output to verify the abnormal behavior of the Build function
155  * @tc.type: FUNC
156  */
157 HWTEST_F(SqueezeBuilderTest, squeeze_build_006, TestSize.Level0)
158 {
159     std::vector<uint32_t> inputsIndex = { 0 };
160     std::vector<uint32_t> outputsIndex = {};
161     std::vector<int32_t> paramDim = {};
162 
163     InitTensor(inputsIndex, outputsIndex);
164     SaveAxisTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_SQUEEZE_AXIS);
165 
166     OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
167     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
168 }
169 
170 /**
171  * @tc.name: squeeze_build_007
172  * @tc.desc: Provide param type error to verify the abnormal behavior of the Build function
173  * @tc.type: FUNC
174  */
175 HWTEST_F(SqueezeBuilderTest, squeeze_build_007, TestSize.Level0)
176 {
177     std::vector<uint32_t> inputsIndex = { 0 };
178     std::vector<uint32_t> outputsIndex = { 1 };
179     std::vector<int32_t> paramDim = {};
180 
181     InitTensor(inputsIndex, outputsIndex);
182     SaveAxisTensor(OH_NN_INT32, paramDim, nullptr, OH_NN_SQUEEZE_AXIS);
183 
184     OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
185     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
186 }
187 
188 /**
189  * @tc.name: squeeze_build_008
190  * @tc.desc: Provide axis parameter buffer is nullptr to verify the abnormal behavior of the Build function
191  * @tc.type: FUNC
192  */
193 HWTEST_F(SqueezeBuilderTest, squeeze_build_008, TestSize.Level0)
194 {
195     std::vector<uint32_t> inputsIndex = { 0 };
196     std::vector<uint32_t> outputsIndex = { 1 };
197     std::vector<int32_t> paramDim = {};
198 
199     InitTensor(inputsIndex, outputsIndex);
200 
201     std::shared_ptr<NNTensor> axisTensor =TransToNNTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_SQUEEZE_AXIS);
202     axisTensor->SetBuffer(nullptr, 0);
203     m_allTensors.emplace_back(axisTensor);
204 
205     OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
206     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
207 }
208 
209 /**
210  * @tc.name: squeeze_build_009
211  * @tc.desc: Provide invalid parameter type to verify the abnormal behavior of the Build function
212  * @tc.type: FUNC
213  */
214 HWTEST_F(SqueezeBuilderTest, squeeze_build_009, TestSize.Level0)
215 {
216     std::vector<uint32_t> inputsIndex = { 0 };
217     std::vector<uint32_t> outputsIndex = { 1 };
218     std::vector<int32_t> paramDim = {};
219 
220     InitTensor(inputsIndex, outputsIndex);
221     SaveAxisTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_SCALE_AXIS);
222 
223     OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
224     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
225 }
226 
227 /**
228  * @tc.name: squeeze_getprimitive_001
229  * @tc.desc: Verify the GetPrimitive function return nullptr
230  * @tc.type: FUNC
231  */
232 HWTEST_F(SqueezeBuilderTest, squeeze_getprimitive_001, TestSize.Level0)
233 {
234     LiteGraphTensorPtr primitive = m_builder.GetPrimitive();
235     LiteGraphTensorPtr expectPrimitive(nullptr, DestroyLiteGraphPrimitive);
236     EXPECT_EQ(primitive, expectPrimitive);
237 }
238 
239 /**
240  * @tc.name: squeeze_getprimitive_002
241  * @tc.desc: Verify the normal params return behavior of the getprimitive function
242  * @tc.type: FUNC
243  */
244 HWTEST_F(SqueezeBuilderTest, squeeze_getprimitive_002, TestSize.Level0)
245 {
246     std::vector<uint32_t> inputsIndex = { 0 };
247     std::vector<uint32_t> outputsIndex = { 1 };
248     std::vector<int32_t> paramDim = {};
249 
250     InitTensor(inputsIndex, outputsIndex);
251     SaveAxisTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_SQUEEZE_AXIS);
252 
253     EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
254     LiteGraphTensorPtr primitive = m_builder.GetPrimitive();
255     LiteGraphTensorPtr expectPrimitive(nullptr, DestroyLiteGraphPrimitive);
256     EXPECT_NE(primitive, expectPrimitive);
257 
258     auto returnValue = mindspore::lite::MindIR_Squeeze_GetAxis(primitive.get());
259     auto returnValueSize = returnValue.size();
260     for (size_t i = 0; i < returnValueSize; ++i) {
261         EXPECT_EQ(returnValue[i], m_expectAxisValue[i]);
262     }
263 }
264 } // namespace UnitTest
265 } // namespace NeuralNetworkRuntime
266 } // namespace OHOS