1 /*
2  * Copyright (c) 2023 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/depth_to_space_builder.h"
17 
18 #include "ops_test.h"
19 
20 using namespace testing;
21 using namespace testing::ext;
22 using namespace OHOS::NeuralNetworkRuntime::Ops;
23 
24 namespace OHOS {
25 namespace NeuralNetworkRuntime {
26 namespace UnitTest {
27 class DepthToSpaceBuilderTest : public OpsTest {
28 public:
29     void SetUp() override;
30     void TearDown() override;
31 
32 protected:
33     void SaveBlockSize(OH_NN_DataType dataType,
34         const std::vector<int32_t> &dim,  const OH_NN_QuantParam* quantParam, OH_NN_TensorType type);
35     void SaveMode(OH_NN_DataType dataType,
36         const std::vector<int32_t> &dim,  const OH_NN_QuantParam* quantParam, OH_NN_TensorType type);
37 
38 protected:
39     DepthToSpaceBuilder m_builder;
40     std::vector<uint32_t> m_inputs {0};
41     std::vector<uint32_t> m_outputs {1};
42     std::vector<uint32_t> m_params {2, 3};
43     std::vector<int32_t> m_inputDim {1, 12, 1, 1};
44     std::vector<int32_t> m_outputDim {1, 3, 2, 2};
45     std::vector<int32_t> m_paramDim {};
46 };
47 
SetUp()48 void DepthToSpaceBuilderTest::SetUp() {}
49 
TearDown()50 void DepthToSpaceBuilderTest::TearDown() {}
51 
SaveBlockSize(OH_NN_DataType dataType,const std::vector<int32_t> & dim,const OH_NN_QuantParam * quantParam,OH_NN_TensorType type)52 void DepthToSpaceBuilderTest::SaveBlockSize(OH_NN_DataType dataType,
53     const std::vector<int32_t> &dim, const OH_NN_QuantParam* quantParam, OH_NN_TensorType type)
54 {
55     std::shared_ptr<NNTensor> blockSizeTensor = TransToNNTensor(dataType, dim, quantParam, type);
56     int64_t* blockSizeValue = new (std::nothrow) int64_t[1] {2};
57     EXPECT_NE(nullptr, blockSizeValue);
58     blockSizeTensor->SetBuffer(blockSizeValue, sizeof(int64_t));
59     m_allTensors.emplace_back(blockSizeTensor);
60 }
61 
SaveMode(OH_NN_DataType dataType,const std::vector<int32_t> & dim,const OH_NN_QuantParam * quantParam,OH_NN_TensorType type)62 void DepthToSpaceBuilderTest::SaveMode(OH_NN_DataType dataType,
63     const std::vector<int32_t> &dim, const OH_NN_QuantParam* quantParam, OH_NN_TensorType type)
64 {
65     std::shared_ptr<NNTensor> modeTensor = TransToNNTensor(dataType, dim, quantParam, type);
66     int32_t* modeValue = new (std::nothrow) int32_t[1] {0};
67     EXPECT_NE(nullptr, modeValue);
68     modeTensor->SetBuffer(modeValue, sizeof(int32_t));
69     m_allTensors.emplace_back(modeTensor);
70 }
71 
72 /**
73  * @tc.name: depth_to_space_build_001
74  * @tc.desc: Verify that the build function returns a successful message.
75  * @tc.type: FUNC
76  */
77 HWTEST_F(DepthToSpaceBuilderTest, depth_to_space_build_001, TestSize.Level1)
78 {
79     SaveInputTensor(m_inputs, OH_NN_INT32, m_inputDim, nullptr);
80     SaveOutputTensor(m_outputs, OH_NN_INT32, m_outputDim, nullptr);
81     SaveBlockSize(OH_NN_INT64, m_paramDim, nullptr, OH_NN_DEPTH_TO_SPACE_BLOCK_SIZE);
82     SaveMode(OH_NN_INT32, m_paramDim, nullptr, OH_NN_DEPTH_TO_SPACE_MODE);
83 
84     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
85     EXPECT_EQ(OH_NN_SUCCESS, ret);
86 }
87 
88 /**
89  * @tc.name: depth_to_space_build_002
90  * @tc.desc: Verify that the build function returns a failed message with true m_isBuild.
91  * @tc.type: FUNC
92  */
93 HWTEST_F(DepthToSpaceBuilderTest, depth_to_space_build_002, TestSize.Level1)
94 {
95     SaveInputTensor(m_inputs, OH_NN_INT32, m_inputDim, nullptr);
96     SaveOutputTensor(m_outputs, OH_NN_INT32, m_outputDim, nullptr);
97     SaveBlockSize(OH_NN_INT64, m_paramDim, nullptr, OH_NN_DEPTH_TO_SPACE_BLOCK_SIZE);
98     SaveMode(OH_NN_INT32, m_paramDim, nullptr, OH_NN_DEPTH_TO_SPACE_MODE);
99 
100     EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors));
101     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
102     EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, ret);
103 }
104 
105 /**
106  * @tc.name: depth_to_space_build_003
107  * @tc.desc: Verify that the build function returns a failed message with invalided input.
108  * @tc.type: FUNC
109  */
110 HWTEST_F(DepthToSpaceBuilderTest, depth_to_space_build_003, TestSize.Level1)
111 {
112     m_inputs = {0, 1};
113     m_outputs = {2};
114     m_params = {3, 4};
115 
116     SaveInputTensor(m_inputs, OH_NN_INT32, m_inputDim, nullptr);
117     SaveOutputTensor(m_outputs, OH_NN_INT32, m_outputDim, nullptr);
118     SaveBlockSize(OH_NN_INT64, m_paramDim, nullptr, OH_NN_DEPTH_TO_SPACE_BLOCK_SIZE);
119     SaveMode(OH_NN_INT32, m_paramDim, nullptr, OH_NN_DEPTH_TO_SPACE_MODE);
120 
121     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
122     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
123 }
124 
125 /**
126  * @tc.name: depth_to_space_build_004
127  * @tc.desc: Verify that the build function returns a failed message with invalided output.
128  * @tc.type: FUNC
129  */
130 HWTEST_F(DepthToSpaceBuilderTest, depth_to_space_build_004, TestSize.Level1)
131 {
132     m_outputs = {1, 2};
133     m_params = {3, 4};
134 
135     SaveInputTensor(m_inputs, OH_NN_INT32, m_inputDim, nullptr);
136     SaveOutputTensor(m_outputs, OH_NN_INT32, m_outputDim, nullptr);
137     SaveBlockSize(OH_NN_INT64, m_paramDim, nullptr, OH_NN_DEPTH_TO_SPACE_BLOCK_SIZE);
138     SaveMode(OH_NN_INT32, m_paramDim, nullptr, OH_NN_DEPTH_TO_SPACE_MODE);
139 
140     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
141     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
142 }
143 
144 /**
145  * @tc.name: depth_to_space_build_005
146  * @tc.desc: Verify that the build function returns a failed message with empty allTensor.
147  * @tc.type: FUNC
148  */
149 HWTEST_F(DepthToSpaceBuilderTest, depth_to_space_build_005, TestSize.Level1)
150 {
151     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputs, m_outputs, m_allTensors);
152     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
153 }
154 
155 /**
156  * @tc.name: depth_to_space_build_006
157  * @tc.desc: Verify that the build function returns a failed message without output tensor.
158  * @tc.type: FUNC
159  */
160 HWTEST_F(DepthToSpaceBuilderTest, depth_to_space_build_006, TestSize.Level1)
161 {
162     SaveInputTensor(m_inputs, OH_NN_INT32, m_inputDim, nullptr);
163 
164     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputs, m_allTensors);
165     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
166 }
167 
168 /**
169  * @tc.name: depth_to_space_build_007
170  * @tc.desc: Verify that the build function returns a failed message with invalid blockSize's dataType.
171  * @tc.type: FUNC
172  */
173 HWTEST_F(DepthToSpaceBuilderTest, depth_to_space_build_007, TestSize.Level1)
174 {
175     SaveInputTensor(m_inputs, OH_NN_INT32, m_inputDim, nullptr);
176     SaveOutputTensor(m_outputs, OH_NN_INT32, m_outputDim, nullptr);
177 
178     std::shared_ptr<NNTensor> blockSizeTensor = TransToNNTensor(OH_NN_FLOAT32, m_paramDim,
179         nullptr, OH_NN_DEPTH_TO_SPACE_BLOCK_SIZE);
__anon44e13b730102null180     float* blockSizeValue = new (std::nothrow) float[1] {2.0f};
181     EXPECT_NE(nullptr, blockSizeValue);
182     blockSizeTensor->SetBuffer(blockSizeValue, sizeof(float));
183     m_allTensors.emplace_back(blockSizeTensor);
184     SaveMode(OH_NN_INT32, m_paramDim, nullptr, OH_NN_DEPTH_TO_SPACE_MODE);
185 
186     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
187     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
188 }
189 
190 /**
191  * @tc.name: depth_to_space_build_008
192  * @tc.desc: Verify that the build function returns a failed message with invalid mode's dataType.
193  * @tc.type: FUNC
194  */
195 HWTEST_F(DepthToSpaceBuilderTest, depth_to_space_build_008, TestSize.Level1)
196 {
197     SaveInputTensor(m_inputs, OH_NN_INT32, m_inputDim, nullptr);
198     SaveOutputTensor(m_outputs, OH_NN_INT32, m_outputDim, nullptr);
199 
200     SaveBlockSize(OH_NN_INT64, m_paramDim, nullptr, OH_NN_DEPTH_TO_SPACE_BLOCK_SIZE);
201     std::shared_ptr<NNTensor> modeTensor = TransToNNTensor(OH_NN_FLOAT32, m_paramDim,
202         nullptr, OH_NN_DEPTH_TO_SPACE_MODE);
__anon44e13b730202null203     float* modeValue = new (std::nothrow) float[1] {0.0f};
204     EXPECT_NE(nullptr, modeValue);
205     modeTensor->SetBuffer(modeValue, sizeof(modeValue));
206     m_allTensors.emplace_back(modeTensor);
207 
208     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
209     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
210 }
211 
212 /**
213  * @tc.name: depth_to_space_build_009
214  * @tc.desc: Verify that the build function returns a failed message with passing invalid blockSize param.
215  * @tc.type: FUNC
216  */
217 HWTEST_F(DepthToSpaceBuilderTest, depth_to_space_build_009, TestSize.Level1)
218 {
219     SaveInputTensor(m_inputs, OH_NN_INT32, m_inputDim, nullptr);
220     SaveOutputTensor(m_outputs, OH_NN_INT32, m_outputDim, nullptr);
221     SaveBlockSize(OH_NN_INT64, m_paramDim, nullptr, OH_NN_MUL_ACTIVATION_TYPE);
222     SaveMode(OH_NN_INT32, m_paramDim, nullptr, OH_NN_DEPTH_TO_SPACE_MODE);
223 
224     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
225     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
226 }
227 
228 /**
229  * @tc.name: depth_to_space_build_010
230  * @tc.desc: Verify that the build function returns a failed message with passing invalid mode param.
231  * @tc.type: FUNC
232  */
233 HWTEST_F(DepthToSpaceBuilderTest, depth_to_space_build_010, TestSize.Level1)
234 {
235     SaveInputTensor(m_inputs, OH_NN_INT32, m_inputDim, nullptr);
236     SaveOutputTensor(m_outputs, OH_NN_INT32, m_outputDim, nullptr);
237     SaveBlockSize(OH_NN_INT64, m_paramDim, nullptr, OH_NN_DEPTH_TO_SPACE_BLOCK_SIZE);
238     SaveMode(OH_NN_INT32, m_paramDim, nullptr, OH_NN_MUL_ACTIVATION_TYPE);
239 
240     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
241     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
242 }
243 
244 /**
245  * @tc.name: depth_to_space_build_011
246  * @tc.desc: Verify that the build function returns a failed message without set buffer for blockSize.
247  * @tc.type: FUNC
248  */
249 HWTEST_F(DepthToSpaceBuilderTest, depth_to_space_build_011, TestSize.Level1)
250 {
251     SaveInputTensor(m_inputs, OH_NN_INT32, m_inputDim, nullptr);
252     SaveOutputTensor(m_outputs, OH_NN_INT32, m_outputDim, nullptr);
253 
254     std::shared_ptr<NNTensor> blockSizeTensor = TransToNNTensor(OH_NN_INT64, m_paramDim,
255         nullptr, OH_NN_DEPTH_TO_SPACE_BLOCK_SIZE);
256     m_allTensors.emplace_back(blockSizeTensor);
257     SaveMode(OH_NN_INT32, m_paramDim, nullptr, OH_NN_DEPTH_TO_SPACE_MODE);
258 
259     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
260     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
261 }
262 
263 /**
264  * @tc.name: depth_to_space_build_012
265  * @tc.desc: Verify that the build function returns a failed message without set buffer for mode.
266  * @tc.type: FUNC
267  */
268 HWTEST_F(DepthToSpaceBuilderTest, depth_to_space_build_012, TestSize.Level1)
269 {
270     SaveInputTensor(m_inputs, OH_NN_INT32, m_inputDim, nullptr);
271     SaveOutputTensor(m_outputs, OH_NN_INT32, m_outputDim, nullptr);
272 
273     SaveBlockSize(OH_NN_INT64, m_paramDim, nullptr, OH_NN_DEPTH_TO_SPACE_BLOCK_SIZE);
274     std::shared_ptr<NNTensor> modeTensor = TransToNNTensor(OH_NN_INT32, m_paramDim,
275         nullptr, OH_NN_DEPTH_TO_SPACE_MODE);
276     m_allTensors.emplace_back(modeTensor);
277 
278     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
279     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
280 }
281 
282 /**
283  * @tc.name: depth_to_space_getprimitive_001
284  * @tc.desc: Verify that the getPrimitive function returns a successful message
285  * @tc.type: FUNC
286  */
287 HWTEST_F(DepthToSpaceBuilderTest, depth_to_space_getprimitive_001, TestSize.Level1)
288 {
289     SaveInputTensor(m_inputs, OH_NN_INT32, m_inputDim, nullptr);
290     SaveOutputTensor(m_outputs, OH_NN_INT32, m_outputDim, nullptr);
291     SaveBlockSize(OH_NN_INT64, m_paramDim, nullptr, OH_NN_DEPTH_TO_SPACE_BLOCK_SIZE);
292     SaveMode(OH_NN_INT32, m_paramDim, nullptr, OH_NN_DEPTH_TO_SPACE_MODE);
293 
294     int64_t blockSizeValue = 2;
295     std::string modeValue = "DCR";
296     EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors));
297 
298     LiteGraphPrimitvePtr primitive = m_builder.GetPrimitive();
299     LiteGraphPrimitvePtr expectPrimitive(nullptr, DestroyLiteGraphPrimitive);
300     EXPECT_NE(expectPrimitive, primitive);
301 
302     auto returnBlockSizeValue = mindspore::lite::MindIR_DepthToSpace_GetBlockSize(primitive.get());
303     EXPECT_EQ(returnBlockSizeValue, blockSizeValue);
304     auto returnModeValue = mindspore::lite::MindIR_DepthToSpace_GetMode(primitive.get());
305     EXPECT_EQ(returnModeValue, modeValue);
306 }
307 
308 /**
309  * @tc.name: depth_to_space_getprimitive_002
310  * @tc.desc: Verify that the getPrimitive function returns a failed message without build.
311  * @tc.type: FUNC
312  */
313 HWTEST_F(DepthToSpaceBuilderTest, depth_to_space_getprimitive_002, TestSize.Level1)
314 {
315     LiteGraphPrimitvePtr primitive = m_builder.GetPrimitive();
316     LiteGraphPrimitvePtr expectPrimitive(nullptr, DestroyLiteGraphPrimitive);
317     EXPECT_EQ(expectPrimitive, primitive);
318 }
319 }
320 }
321 }