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/strided_slice_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 StridedSliceBuilderTest : public OpsTest {
30 protected:
31 void InitTensor(const std::vector<uint32_t>& inputsIndex,
32 const std::vector<uint32_t>& outputsIndex) override;
33 void SaveBeginMaskTensor(OH_NN_DataType dataType, const std::vector<int32_t> &dim,
34 const OH_NN_QuantParam* quantParam, OH_NN_TensorType type);
35 void SaveEndMaskTensor(OH_NN_DataType dataType, const std::vector<int32_t> &dim,
36 const OH_NN_QuantParam* quantParam, OH_NN_TensorType type);
37 void SaveEllipsisMaskTensor(OH_NN_DataType dataType, const std::vector<int32_t> &dim,
38 const OH_NN_QuantParam* quantParam, OH_NN_TensorType type);
39 void SaveNewAxisTensor(OH_NN_DataType dataType, const std::vector<int32_t> &dim,
40 const OH_NN_QuantParam* quantParam, OH_NN_TensorType type);
41 void SaveShrinkAxisTensor(OH_NN_DataType dataType, const std::vector<int32_t> &dim,
42 const OH_NN_QuantParam* quantParam, OH_NN_TensorType type);
43 void InitParams();
44
45 protected:
46 StridedSliceBuilder m_builder;
47 int64_t m_expectBeginMaskValue {0};
48 int64_t m_expectEndMaskValue {0};
49 int64_t m_expectEllipsisMaskValue {0};
50 int64_t m_expectNewAxisMaskValue {0};
51 int64_t m_expectShrinkAxisMaskValue {0};
52 };
53
SaveBeginMaskTensor(OH_NN_DataType dataType,const std::vector<int32_t> & dim,const OH_NN_QuantParam * quantParam,OH_NN_TensorType type)54 void StridedSliceBuilderTest::SaveBeginMaskTensor(OH_NN_DataType dataType,
55 const std::vector<int32_t> &dim, const OH_NN_QuantParam* quantParam, OH_NN_TensorType type)
56 {
57 std::shared_ptr<NNTensor> beginMaskTensor = TransToNNTensor(dataType, dim, quantParam, type);
58 int64_t* beginMaskValue = new (std::nothrow) int64_t[1]{0};
59 EXPECT_NE(nullptr, beginMaskValue);
60 beginMaskTensor->SetBuffer(beginMaskValue, sizeof(int64_t));
61 m_allTensors.emplace_back(beginMaskTensor);
62 m_expectBeginMaskValue = *beginMaskValue;
63 }
64
SaveEndMaskTensor(OH_NN_DataType dataType,const std::vector<int32_t> & dim,const OH_NN_QuantParam * quantParam,OH_NN_TensorType type)65 void StridedSliceBuilderTest::SaveEndMaskTensor(OH_NN_DataType dataType,
66 const std::vector<int32_t> &dim, const OH_NN_QuantParam* quantParam, OH_NN_TensorType type)
67 {
68 std::shared_ptr<NNTensor> endMaskTensor = TransToNNTensor(dataType, dim, quantParam, type);
69 int64_t* endMaskValue = new (std::nothrow) int64_t[1]{0};
70 EXPECT_NE(nullptr, endMaskValue);
71 endMaskTensor->SetBuffer(endMaskValue, sizeof(int64_t));
72 m_allTensors.emplace_back(endMaskTensor);
73 m_expectEndMaskValue = *endMaskValue;
74 }
75
SaveEllipsisMaskTensor(OH_NN_DataType dataType,const std::vector<int32_t> & dim,const OH_NN_QuantParam * quantParam,OH_NN_TensorType type)76 void StridedSliceBuilderTest::SaveEllipsisMaskTensor(OH_NN_DataType dataType,
77 const std::vector<int32_t> &dim, const OH_NN_QuantParam* quantParam, OH_NN_TensorType type)
78 {
79 std::shared_ptr<NNTensor> ellipsisMaskTensor = TransToNNTensor(dataType, dim, quantParam, type);
80 int64_t* ellipsisMaskValue = new (std::nothrow) int64_t[1]{0};
81 EXPECT_NE(nullptr, ellipsisMaskValue);
82 ellipsisMaskTensor->SetBuffer(ellipsisMaskValue, sizeof(int64_t));
83 m_allTensors.emplace_back(ellipsisMaskTensor);
84 m_expectEllipsisMaskValue = *ellipsisMaskValue;
85 }
86
SaveNewAxisTensor(OH_NN_DataType dataType,const std::vector<int32_t> & dim,const OH_NN_QuantParam * quantParam,OH_NN_TensorType type)87 void StridedSliceBuilderTest::SaveNewAxisTensor(OH_NN_DataType dataType,
88 const std::vector<int32_t> &dim, const OH_NN_QuantParam* quantParam, OH_NN_TensorType type)
89 {
90 std::shared_ptr<NNTensor> axisTensor = TransToNNTensor(dataType, dim, quantParam, type);
91 int64_t* newAxisMaskValue = new (std::nothrow) int64_t[1]{0};
92 EXPECT_NE(nullptr, newAxisMaskValue);
93 axisTensor->SetBuffer(newAxisMaskValue, sizeof(int64_t));
94 m_allTensors.emplace_back(axisTensor);
95 m_expectNewAxisMaskValue = *newAxisMaskValue;
96 }
97
SaveShrinkAxisTensor(OH_NN_DataType dataType,const std::vector<int32_t> & dim,const OH_NN_QuantParam * quantParam,OH_NN_TensorType type)98 void StridedSliceBuilderTest::SaveShrinkAxisTensor(OH_NN_DataType dataType,
99 const std::vector<int32_t> &dim, const OH_NN_QuantParam* quantParam, OH_NN_TensorType type)
100 {
101 std::shared_ptr<NNTensor> shrinkAxisMaskTensor = TransToNNTensor(dataType, dim, quantParam, type);
102 int64_t* shrinkAxisMaskValue = new (std::nothrow) int64_t[1]{0};
103 EXPECT_NE(nullptr, shrinkAxisMaskValue);
104 shrinkAxisMaskTensor->SetBuffer(shrinkAxisMaskValue, sizeof(int64_t));
105 m_allTensors.emplace_back(shrinkAxisMaskTensor);
106 m_expectShrinkAxisMaskValue = *shrinkAxisMaskValue;
107 }
108
InitTensor(const std::vector<uint32_t> & inputsIndex,const std::vector<uint32_t> & outputsIndex)109 void StridedSliceBuilderTest::InitTensor(const std::vector<uint32_t>& inputsIndex,
110 const std::vector<uint32_t>& outputsIndex)
111 {
112 std::vector<uint32_t> paramsIndex = { 5, 6, 7, 8, 9 };
113 std::vector<int32_t> inputDim = {3, 2, 3};
114 std::vector<int32_t> OutputDim = {1, 2, 2};
115
116 m_paramsIndex = paramsIndex;
117 SaveInputTensor(inputsIndex, OH_NN_FLOAT32, inputDim, nullptr);
118 SaveOutputTensor(outputsIndex, OH_NN_FLOAT32, OutputDim, nullptr);
119 }
120
InitParams()121 void StridedSliceBuilderTest::InitParams()
122 {
123 std::vector<int32_t> paramDim = {};
124 SaveBeginMaskTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STRIDED_SLICE_BEGIN_MASK);
125 SaveEndMaskTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STRIDED_SLICE_END_MASK);
126 SaveEllipsisMaskTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STRIDED_SLICE_ELLIPSIS_MASK);
127 SaveNewAxisTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STRIDED_SLICE_NEW_AXIS_MASK);
128 SaveShrinkAxisTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STRIDED_SLICE_SHRINK_AXIS_MASK);
129 }
130
131 /**
132 * @tc.name: stridedslice_build_001
133 * @tc.desc: Provide normal input, output, and parameters to verify the normal behavior of the Build function
134 * @tc.type: FUNC
135 */
136 HWTEST_F(StridedSliceBuilderTest, stridedslice_build_001, TestSize.Level0)
137 {
138 std::vector<uint32_t> inputsIndex = { 0, 1, 2, 3 };
139 std::vector<uint32_t> outputsIndex = { 4 };
140
141 InitTensor(inputsIndex, outputsIndex);
142 InitParams();
143
144 OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
145 EXPECT_EQ(OH_NN_SUCCESS, ret);
146 }
147
148 /**
149 * @tc.name: stridedslice_build_002
150 * @tc.desc: Call Build func twice to verify the abnormal behavior of the Build function
151 * @tc.type: FUNC
152 */
153 HWTEST_F(StridedSliceBuilderTest, stridedslice_build_002, TestSize.Level0)
154 {
155 std::vector<uint32_t> inputsIndex = { 0, 1, 2, 3 };
156 std::vector<uint32_t> outputsIndex = { 4 };
157
158 InitTensor(inputsIndex, outputsIndex);
159 InitParams();
160
161 EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
162 OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
163 EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, ret);
164 }
165
166 /**
167 * @tc.name: stridedslice_build_003
168 * @tc.desc: Provide one more than normal input to verify the abnormal behavior of the Build function
169 * @tc.type: FUNC
170 */
171 HWTEST_F(StridedSliceBuilderTest, stridedslice_build_003, TestSize.Level0)
172 {
173 std::vector<uint32_t> inputsIndex = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
174 std::vector<uint32_t> outputsIndex = { 10 };
175 std::vector<uint32_t> paramsIndex = { 11, 12, 13, 14, 15 };
176 std::vector<int32_t> inputDim = {3, 2, 3};
177 std::vector<int32_t> OutputDim = {1, 2, 2};
178
179 m_paramsIndex = paramsIndex;
180 SaveInputTensor(inputsIndex, OH_NN_FLOAT32, inputDim, nullptr);
181 SaveOutputTensor(outputsIndex, OH_NN_FLOAT32, OutputDim, nullptr);
182 InitParams();
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: stridedslice_build_004
190 * @tc.desc: Provide one more than normal output to verify the abnormal behavior of the Build function
191 * @tc.type: FUNC
192 */
193 HWTEST_F(StridedSliceBuilderTest, stridedslice_build_004, TestSize.Level0)
194 {
195 std::vector<uint32_t> inputsIndex = { 0, 1, 2, 3 };
196 std::vector<uint32_t> outputsIndex = { 4, 5 };
197 std::vector<uint32_t> paramsIndex = { 6, 7, 8, 9, 10 };
198 std::vector<int32_t> inputDim = {3, 2, 3};
199 std::vector<int32_t> OutputDim = {1, 2, 2};
200
201 m_paramsIndex = paramsIndex;
202 SaveInputTensor(inputsIndex, OH_NN_FLOAT32, inputDim, nullptr);
203 SaveOutputTensor(outputsIndex, OH_NN_FLOAT32, OutputDim, nullptr);
204 InitParams();
205
206 OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
207 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
208 }
209
210 /**
211 * @tc.name: stridedslice_build_005
212 * @tc.desc: Provide empty input, output, and parameters to verify the abnormal behavior of the Build function
213 * @tc.type: FUNC
214 */
215 HWTEST_F(StridedSliceBuilderTest, stridedslice_build_005, TestSize.Level0)
216 {
217 std::vector<uint32_t> inputsIndex = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
218 std::vector<uint32_t> outputsIndex = { 9 };
219 std::vector<uint32_t> paramsIndex = { 10, 11, 12, 13, 14 };
220
221 OH_NN_ReturnCode ret = m_builder.Build(paramsIndex, inputsIndex, outputsIndex, m_allTensors);
222 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
223 }
224
225 /**
226 * @tc.name: stridedslice_build_006
227 * @tc.desc:Provide empty output to verify the abnormal behavior of the Build function
228 * @tc.type: FUNC
229 */
230 HWTEST_F(StridedSliceBuilderTest, stridedslice_build_006, TestSize.Level0)
231 {
232 std::vector<uint32_t> inputsIndex = { 0, 1, 2, 3 };
233 std::vector<uint32_t> outputsIndex = {};
234 std::vector<uint32_t> paramsIndex = { 4, 5, 6, 7, 8 };
235 std::vector<int32_t> inputDim = {3, 2, 3};
236 std::vector<int32_t> OutputDim = {1, 2, 2};
237
238 m_paramsIndex = paramsIndex;
239 SaveInputTensor(inputsIndex, OH_NN_FLOAT32, inputDim, nullptr);
240 SaveOutputTensor(outputsIndex, OH_NN_FLOAT32, OutputDim, nullptr);
241 InitParams();
242
243 OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
244 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
245 }
246
247 /**
248 * @tc.name: stridedslice_build_007
249 * @tc.desc: Provide beginmask param type error to verify the abnormal behavior of the Build function
250 * @tc.type: FUNC
251 */
252 HWTEST_F(StridedSliceBuilderTest, stridedslice_build_007, TestSize.Level0)
253 {
254 std::vector<uint32_t> inputsIndex = { 0, 1, 2, 3 };
255 std::vector<uint32_t> outputsIndex = { 4 };
256 std::vector<uint32_t> paramsIndex = { 5, 6, 7, 8, 9 };
257 std::vector<int32_t> inputDim = {3, 2, 3};
258 std::vector<int32_t> OutputDim = {1, 2, 2};
259 std::vector<int32_t> paramDim = {};
260
261 m_paramsIndex = paramsIndex;
262 SaveInputTensor(inputsIndex, OH_NN_FLOAT32, inputDim, nullptr);
263 SaveOutputTensor(outputsIndex, OH_NN_FLOAT32, OutputDim, nullptr);
264 SaveBeginMaskTensor(OH_NN_INT32, paramDim, nullptr, OH_NN_STRIDED_SLICE_BEGIN_MASK);
265 SaveEndMaskTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STRIDED_SLICE_END_MASK);
266 SaveEllipsisMaskTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STRIDED_SLICE_ELLIPSIS_MASK);
267 SaveNewAxisTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STRIDED_SLICE_NEW_AXIS_MASK);
268 SaveShrinkAxisTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STRIDED_SLICE_SHRINK_AXIS_MASK);
269
270 OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
271 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
272 }
273
274 /**
275 * @tc.name: stridedslice_build_008
276 * @tc.desc: Provide endmask param type error to verify the abnormal behavior of the Build function
277 * @tc.type: FUNC
278 */
279 HWTEST_F(StridedSliceBuilderTest, stridedslice_build_008, TestSize.Level0)
280 {
281 std::vector<uint32_t> inputsIndex = { 0, 1, 2, 3 };
282 std::vector<uint32_t> outputsIndex = { 4 };
283 std::vector<int32_t> paramDim = {};
284
285 InitTensor(inputsIndex, outputsIndex);
286 SaveEndMaskTensor(OH_NN_INT32, paramDim, nullptr, OH_NN_STRIDED_SLICE_END_MASK);
287 SaveEllipsisMaskTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STRIDED_SLICE_ELLIPSIS_MASK);
288 SaveNewAxisTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STRIDED_SLICE_NEW_AXIS_MASK);
289 SaveShrinkAxisTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STRIDED_SLICE_SHRINK_AXIS_MASK);
290
291 OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
292 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
293 }
294
295 /**
296 * @tc.name: stridedslice_build_009
297 * @tc.desc: Provide ellipsismask param type error to verify the abnormal behavior of the Build function
298 * @tc.type: FUNC
299 */
300 HWTEST_F(StridedSliceBuilderTest, stridedslice_build_009, TestSize.Level0)
301 {
302 std::vector<uint32_t> inputsIndex = { 0, 1, 2, 3 };
303 std::vector<uint32_t> outputsIndex = { 4 };
304 std::vector<int32_t> paramDim = {};
305
306 InitTensor(inputsIndex, outputsIndex);
307 SaveEndMaskTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STRIDED_SLICE_END_MASK);
308 SaveEllipsisMaskTensor(OH_NN_INT32, paramDim, nullptr, OH_NN_STRIDED_SLICE_ELLIPSIS_MASK);
309 SaveNewAxisTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STRIDED_SLICE_NEW_AXIS_MASK);
310 SaveShrinkAxisTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STRIDED_SLICE_SHRINK_AXIS_MASK);
311
312 OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
313 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
314 }
315
316 /**
317 * @tc.name: stridedslice_build_010
318 * @tc.desc: Provide axis param type error to verify the abnormal behavior of the Build function
319 * @tc.type: FUNC
320 */
321 HWTEST_F(StridedSliceBuilderTest, stridedslice_build_010, TestSize.Level0)
322 {
323 std::vector<uint32_t> inputsIndex = { 0, 1, 2, 3 };
324 std::vector<uint32_t> outputsIndex = { 4 };
325 std::vector<int32_t> paramDim = {};
326
327 InitTensor(inputsIndex, outputsIndex);
328 SaveEndMaskTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STRIDED_SLICE_END_MASK);
329 SaveEllipsisMaskTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STRIDED_SLICE_ELLIPSIS_MASK);
330 SaveNewAxisTensor(OH_NN_INT32, paramDim, nullptr, OH_NN_STRIDED_SLICE_NEW_AXIS_MASK);
331 SaveShrinkAxisTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STRIDED_SLICE_SHRINK_AXIS_MASK);
332
333 OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
334 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
335 }
336
337 /**
338 * @tc.name: stridedslice_build_011
339 * @tc.desc: Provide shrinkaxis param type error to verify the abnormal behavior of the Build function
340 * @tc.type: FUNC
341 */
342 HWTEST_F(StridedSliceBuilderTest, stridedslice_build_011, TestSize.Level0)
343 {
344 std::vector<uint32_t> inputsIndex = { 0, 1, 2, 3 };
345 std::vector<uint32_t> outputsIndex = { 4 };
346 std::vector<int32_t> paramDim = {};
347
348 InitTensor(inputsIndex, outputsIndex);
349 SaveEndMaskTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STRIDED_SLICE_END_MASK);
350 SaveEllipsisMaskTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STRIDED_SLICE_ELLIPSIS_MASK);
351 SaveNewAxisTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STRIDED_SLICE_NEW_AXIS_MASK);
352 SaveShrinkAxisTensor(OH_NN_INT32, paramDim, nullptr, OH_NN_STRIDED_SLICE_SHRINK_AXIS_MASK);
353
354 OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
355 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
356 }
357
358
359 /**
360 * @tc.name: stridedslice_build_012
361 * @tc.desc: Provide begin mask parameter buffer is nullptr to verify the abnormal behavior of the Build function
362 * @tc.type: FUNC
363 */
364 HWTEST_F(StridedSliceBuilderTest, stridedslice_build_012, TestSize.Level0)
365 {
366 std::vector<uint32_t> inputsIndex = { 0, 1, 2, 3 };
367 std::vector<uint32_t> outputsIndex = { 4 };
368 std::vector<int32_t> paramDim = {};
369
370 InitTensor(inputsIndex, outputsIndex);
371 std::shared_ptr<NNTensor> beginMaskTensor = TransToNNTensor(OH_NN_INT64, paramDim, nullptr,
372 OH_NN_STRIDED_SLICE_BEGIN_MASK);
373 beginMaskTensor->SetBuffer(nullptr, 0);
374 m_allTensors.emplace_back(beginMaskTensor);
375
376 SaveEndMaskTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STRIDED_SLICE_END_MASK);
377 SaveEllipsisMaskTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STRIDED_SLICE_ELLIPSIS_MASK);
378 SaveNewAxisTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STRIDED_SLICE_NEW_AXIS_MASK);
379 SaveShrinkAxisTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STRIDED_SLICE_SHRINK_AXIS_MASK);
380
381 OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
382 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
383 }
384
385 /**
386 * @tc.name: stridedslice_build_013
387 * @tc.desc: Provide end mask parameter buffer is nullptr to verify the abnormal behavior of the Build function
388 * @tc.type: FUNC
389 */
390 HWTEST_F(StridedSliceBuilderTest, stridedslice_build_013, TestSize.Level0)
391 {
392 std::vector<uint32_t> inputsIndex = { 0, 1, 2, 3 };
393 std::vector<uint32_t> outputsIndex = { 4 };
394 std::vector<int32_t> paramDim = {};
395
396 InitTensor(inputsIndex, outputsIndex);
397 SaveBeginMaskTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STRIDED_SLICE_BEGIN_MASK);
398
399 std::shared_ptr<NNTensor> endMaskTensor = TransToNNTensor(OH_NN_INT64, paramDim, nullptr,
400 OH_NN_STRIDED_SLICE_END_MASK);
401 endMaskTensor->SetBuffer(nullptr, 0);
402 m_allTensors.emplace_back(endMaskTensor);
403
404 SaveEllipsisMaskTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STRIDED_SLICE_ELLIPSIS_MASK);
405 SaveNewAxisTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STRIDED_SLICE_NEW_AXIS_MASK);
406 SaveShrinkAxisTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STRIDED_SLICE_SHRINK_AXIS_MASK);
407
408 OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
409 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
410 }
411
412 /**
413 * @tc.name: stridedslice_build_014
414 * @tc.desc: Provide ellipsis mask parameter buffer is nullptr to verify the abnormal behavior of the Build function
415 * @tc.type: FUNC
416 */
417 HWTEST_F(StridedSliceBuilderTest, stridedslice_build_014, TestSize.Level0)
418 {
419 std::vector<uint32_t> inputsIndex = { 0, 1, 2, 3 };
420 std::vector<uint32_t> outputsIndex = { 4 };
421 std::vector<int32_t> paramDim = {};
422
423 InitTensor(inputsIndex, outputsIndex);
424 SaveBeginMaskTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STRIDED_SLICE_BEGIN_MASK);
425 SaveEndMaskTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STRIDED_SLICE_END_MASK);
426
427 std::shared_ptr<NNTensor> ellipsisMaskTensor = TransToNNTensor(OH_NN_INT64, paramDim, nullptr,
428 OH_NN_STRIDED_SLICE_ELLIPSIS_MASK);
429 ellipsisMaskTensor->SetBuffer(nullptr, 0);
430 m_allTensors.emplace_back(ellipsisMaskTensor);
431
432 SaveNewAxisTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STRIDED_SLICE_NEW_AXIS_MASK);
433 SaveShrinkAxisTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STRIDED_SLICE_SHRINK_AXIS_MASK);
434
435 OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
436 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
437 }
438
439 /**
440 * @tc.name: stridedslice_build_015
441 * @tc.desc: Provide new axis parameter buffer is nullptr to verify the abnormal behavior of the Build function
442 * @tc.type: FUNC
443 */
444 HWTEST_F(StridedSliceBuilderTest, stridedslice_build_015, TestSize.Level0)
445 {
446 std::vector<uint32_t> inputsIndex = { 0, 1, 2, 3 };
447 std::vector<uint32_t> outputsIndex = { 4 };
448 std::vector<int32_t> paramDim = {};
449
450 InitTensor(inputsIndex, outputsIndex);
451 SaveBeginMaskTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STRIDED_SLICE_BEGIN_MASK);
452 SaveEndMaskTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STRIDED_SLICE_END_MASK);
453 SaveEllipsisMaskTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STRIDED_SLICE_ELLIPSIS_MASK);
454
455 std::shared_ptr<NNTensor> axisTensor = TransToNNTensor(OH_NN_INT64, paramDim, nullptr,
456 OH_NN_STRIDED_SLICE_NEW_AXIS_MASK);
457 axisTensor->SetBuffer(nullptr, 0);
458 m_allTensors.emplace_back(axisTensor);
459
460 SaveShrinkAxisTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STRIDED_SLICE_SHRINK_AXIS_MASK);
461
462 OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
463 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
464 }
465
466 /**
467 * @tc.name: stridedslice_build_016
468 * @tc.desc: Provide shrink axis parameter buffer is nullptr to verify the abnormal behavior of the Build function
469 * @tc.type: FUNC
470 */
471 HWTEST_F(StridedSliceBuilderTest, stridedslice_build_016, TestSize.Level0)
472 {
473 std::vector<uint32_t> inputsIndex = { 0, 1, 2, 3 };
474 std::vector<uint32_t> outputsIndex = { 4 };
475 std::vector<int32_t> paramDim = {};
476
477 InitTensor(inputsIndex, outputsIndex);
478 SaveBeginMaskTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STRIDED_SLICE_BEGIN_MASK);
479 SaveEndMaskTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STRIDED_SLICE_END_MASK);
480 SaveEllipsisMaskTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STRIDED_SLICE_ELLIPSIS_MASK);
481 SaveNewAxisTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STRIDED_SLICE_NEW_AXIS_MASK);
482
483 std::shared_ptr<NNTensor> shrinkAxisTensor = TransToNNTensor(OH_NN_INT64, paramDim, nullptr,
484 OH_NN_STRIDED_SLICE_SHRINK_AXIS_MASK);
485 shrinkAxisTensor->SetBuffer(nullptr, 0);
486 m_allTensors.emplace_back(shrinkAxisTensor);
487
488 OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
489 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
490 }
491
492 /**
493 * @tc.name: stridedslice_build_017
494 * @tc.desc: Provide invalid parameter type to verify the abnormal behavior of the Build function
495 * @tc.type: FUNC
496 */
497 HWTEST_F(StridedSliceBuilderTest, stridedslice_build_017, TestSize.Level0)
498 {
499 std::vector<uint32_t> inputsIndex = { 0, 1, 2, 3 };
500 std::vector<uint32_t> outputsIndex = { 4 };
501 std::vector<int32_t> paramDim = {};
502
503 InitTensor(inputsIndex, outputsIndex);
504 SaveBeginMaskTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STRIDED_SLICE_BEGIN_MASK);
505 SaveEndMaskTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STRIDED_SLICE_END_MASK);
506 SaveEllipsisMaskTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STRIDED_SLICE_ELLIPSIS_MASK);
507 SaveNewAxisTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STRIDED_SLICE_NEW_AXIS_MASK);
508 SaveShrinkAxisTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_SCALE_AXIS);
509
510 OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
511 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
512 }
513
514 /**
515 * @tc.name: stridedslice_get_primitive_001
516 * @tc.desc: Verify the GetPrimitive function return nullptr
517 * @tc.type: FUNC
518 */
519 HWTEST_F(StridedSliceBuilderTest, stridedslice_get_primitive_001, TestSize.Level0)
520 {
521 LiteGraphTensorPtr primitive = m_builder.GetPrimitive();
522 LiteGraphTensorPtr expectPrimitive = { nullptr, DestroyLiteGraphPrimitive };
523 EXPECT_EQ(primitive, expectPrimitive);
524 }
525
526 /**
527 * @tc.name: stridedslice_get_primitive_002
528 * @tc.desc: Verify the normal params return behavior of the getprimitive function
529 * @tc.type: FUNC
530 */
531 HWTEST_F(StridedSliceBuilderTest, stridedslice_get_primitive_002, TestSize.Level0)
532 {
533 std::vector<uint32_t> inputsIndex = { 0, 1, 2, 3 };
534 std::vector<uint32_t> outputsIndex = { 4 };
535
536 InitTensor(inputsIndex, outputsIndex);
537 InitParams();
538
539 EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
540 LiteGraphTensorPtr primitive = m_builder.GetPrimitive();
541 LiteGraphTensorPtr expectPrimitive = { nullptr, DestroyLiteGraphPrimitive };
542 EXPECT_NE(primitive, expectPrimitive);
543
544 auto beginMaskReturn = mindspore::lite::MindIR_StridedSlice_GetBeginMask(primitive.get());
545 EXPECT_EQ(beginMaskReturn, m_expectBeginMaskValue);
546 auto endMaskReturn = mindspore::lite::MindIR_StridedSlice_GetEndMask(primitive.get());
547 EXPECT_EQ(endMaskReturn, m_expectEndMaskValue);
548 auto ellipsisMaskReturn = mindspore::lite::MindIR_StridedSlice_GetEllipsisMask(primitive.get());
549 EXPECT_EQ(ellipsisMaskReturn, m_expectEllipsisMaskValue);
550 auto newAxisMaskReturn = mindspore::lite::MindIR_StridedSlice_GetNewAxisMask(primitive.get());
551 EXPECT_EQ(newAxisMaskReturn, m_expectNewAxisMaskValue);
552 auto shrinkAxisMaskReturn = mindspore::lite::MindIR_StridedSlice_GetShrinkAxisMask(primitive.get());
553 EXPECT_EQ(shrinkAxisMaskReturn, m_expectShrinkAxisMaskValue);
554 }
555 } // namespace UnitTest
556 } // namespace NeuralNetworkRuntime
557 } // namespace OHOS
558