1 /*
2  * Copyright (c) 2024 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 <vector>
17 #include <string>
18 #include <iostream>
19 #include "nncore_utils.h"
20 
21 using namespace testing::ext;
22 using namespace OHOS::NeuralNetworkRuntime::Test;
23 class CropTest : public testing::Test {};
24 
25 struct CropModel1 {
26     const std::vector<int32_t> input_shape = {2, 4, 4};
27     const std::vector<int32_t> shape_shape = {3};
28     const std::vector<int32_t> output_shape = {2, 2, 2};
29 
30     int64_t axisValue[1] = {0};
31     std::vector<int64_t> offsetsValue = {0, 1, 1};
32     float inputValue[2][4][4] = {1};
33     float shapeValue[3] = {2, 2, 2};
34     float outputValue[2][2][2] = {0};
35 
36     OHNNOperandTest input = {OH_NN_FLOAT32, OH_NN_TENSOR, input_shape, inputValue, 32*sizeof(float)};
37     OHNNOperandTest shape = {OH_NN_FLOAT32, OH_NN_TENSOR, shape_shape, shapeValue, 3*sizeof(float)};
38     OHNNOperandTest output = {OH_NN_FLOAT32, OH_NN_TENSOR, output_shape, outputValue, 8*sizeof(float)};
39     OHNNOperandTest axis = {OH_NN_INT64, OH_NN_CROP_AXIS, {1}, axisValue, sizeof(int64_t)};
40     OHNNOperandTest offsets = {OH_NN_INT64, OH_NN_CROP_OFFSET, {3}, &offsetsValue, 3*sizeof(int64_t)};
41     OHNNGraphArgs graphArgs = {.operationType = OH_NN_OPS_CROP,
42                                .operands = {input, shape, output, axis, offsets},
43                                .paramIndices = {3, 4},
44                                .inputIndices = {0, 1},
45                                .outputIndices = {2}};
46 };
47 
48 struct CropModel2 {
49     const std::vector<int32_t> input_shape = {3, 2, 2};
50     const std::vector<int32_t> shape_shape = {3};
51     const std::vector<int32_t> output_shape = {3, 2, 2};
52 
53     int64_t axisValue[1] = {1};
54     std::vector<int64_t> offsetsValue = {0, 2, 2};
55     float inputValue[3][2][2] = {1};
56     float shapeValue[3] = {3, 2, 2};
57     float outputValue[3][2][2] = {0};
58 
59     OHNNOperandTest input = {OH_NN_FLOAT32, OH_NN_TENSOR, input_shape, inputValue, 12*sizeof(float)};
60     OHNNOperandTest shape = {OH_NN_FLOAT32, OH_NN_TENSOR, shape_shape, shapeValue, 3*sizeof(float)};
61     OHNNOperandTest output = {OH_NN_FLOAT32, OH_NN_TENSOR, output_shape, outputValue, 12*sizeof(float)};
62     OHNNOperandTest axis = {OH_NN_INT64, OH_NN_CROP_AXIS, {1}, axisValue, sizeof(int64_t)};
63     OHNNOperandTest offsets = {OH_NN_INT64, OH_NN_CROP_OFFSET, {3}, &offsetsValue, 3*sizeof(int64_t)};
64     OHNNGraphArgs graphArgs = {.operationType = OH_NN_OPS_CROP,
65                                .operands = {input, shape, output, axis, offsets},
66                                .paramIndices = {3, 4},
67                                .inputIndices = {0, 1},
68                                .outputIndices = {2}};
69 };
70 
71 struct CropModel3 {
72     const std::vector<int32_t> input_shape = {3, 2, 2};
73     const std::vector<int32_t> shape_shape = {3};
74     const std::vector<int32_t> output_shape = {4, 2, 2};
75 
76     int64_t axisValue[1] = {1};
77     std::vector<int64_t> offsetsValue = {0, 1, 1};
78     float inputValue[3][2][2] = {1};
79     float shapeValue[3] = {4, 2, 2};
80     float outputValue[4][2][2] = {0};
81 
82     OHNNOperandTest input = {OH_NN_FLOAT32, OH_NN_TENSOR, input_shape, inputValue, 12*sizeof(float)};
83     OHNNOperandTest shape = {OH_NN_FLOAT32, OH_NN_TENSOR, shape_shape, shapeValue, 3*sizeof(float)};
84     OHNNOperandTest output = {OH_NN_FLOAT32, OH_NN_TENSOR, output_shape, outputValue, 16*sizeof(float)};
85     OHNNOperandTest axis = {OH_NN_INT64, OH_NN_CROP_AXIS, {1}, axisValue, sizeof(int64_t)};
86     OHNNOperandTest offsets = {OH_NN_INT64, OH_NN_CROP_OFFSET, {3}, &offsetsValue, 3*sizeof(int64_t)};
87     OHNNGraphArgs graphArgs = {.operationType = OH_NN_OPS_CROP,
88                                .operands = {input, shape, output, axis, offsets},
89                                .paramIndices = {3, 4},
90                                .inputIndices = {0, 1},
91                                .outputIndices = {2}};
92 };
93 
94 /**
95  * @tc.number : SUB_AI_NNRt_Func_North_Crop_Build_01
96  * @tc.desc: CropModel1模型build测试
97  * @tc.type: FUNC
98  */
HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Build_01, Function | MediumTest | Level1)99 HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Build_01, Function | MediumTest | Level1)
100 {
101     OH_NNModel *model = OH_NNModel_Construct();
102     EXPECT_NE(nullptr, model);
103 
104     CropModel1 cropModel;
105     OHNNGraphArgs graphArgs = cropModel.graphArgs;
106     EXPECT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
107 
108     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
109     EXPECT_NE(nullptr, compilation);
110 
111     OHNNCompileParam compileParam{
112         .performanceMode = OH_NN_PERFORMANCE_HIGH,
113         .priority = OH_NN_PRIORITY_HIGH,
114     };
115     EXPECT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
116 
117     OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
118     EXPECT_NE(nullptr, executor);
119 
120     Free(model, compilation, executor);
121 }
122 
123 /**
124  * @tc.number : SUB_AI_NNRt_Func_North_Crop_Build_02
125  * @tc.desc: CropModel2模型build测试
126  * @tc.type: FUNC
127  */
HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Build_02, Function | MediumTest | Level1)128 HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Build_02, Function | MediumTest | Level1)
129 {
130     OH_NNModel *model = OH_NNModel_Construct();
131     EXPECT_NE(nullptr, model);
132 
133     CropModel2 cropModel;
134     OHNNGraphArgs graphArgs = cropModel.graphArgs;
135     EXPECT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
136 
137     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
138     EXPECT_NE(nullptr, compilation);
139 
140     OHNNCompileParam compileParam{
141         .performanceMode = OH_NN_PERFORMANCE_HIGH,
142         .priority = OH_NN_PRIORITY_HIGH,
143     };
144     EXPECT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
145 
146     OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
147     EXPECT_NE(nullptr, executor);
148 
149     Free(model, compilation, executor);
150 }
151 
152 /**
153  * @tc.number : SUB_AI_NNRt_Func_North_Crop_Build_03
154  * @tc.desc: CropModel3模型build测试
155  * @tc.type: FUNC
156  */
HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Build_03, Function | MediumTest | Level1)157 HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Build_03, Function | MediumTest | Level1)
158 {
159     OH_NNModel *model = OH_NNModel_Construct();
160     EXPECT_NE(nullptr, model);
161 
162     CropModel3 cropModel;
163     OHNNGraphArgs graphArgs = cropModel.graphArgs;
164     EXPECT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
165 
166     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
167     EXPECT_NE(nullptr, compilation);
168 
169     OHNNCompileParam compileParam{
170         .performanceMode = OH_NN_PERFORMANCE_HIGH,
171         .priority = OH_NN_PRIORITY_HIGH,
172     };
173     EXPECT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
174 
175     OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
176     EXPECT_NE(nullptr, executor);
177 
178     Free(model, compilation, executor);
179 }
180 
181 /**
182  * @tc.number : SUB_AI_NNRt_Func_North_Crop_Build_04
183  * @tc.desc: CropModel1模型输入Tensor+1进行build测试
184  * @tc.type: FUNC
185  */
HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Build_04, Function | MediumTest | Level2)186 HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Build_04, Function | MediumTest | Level2)
187 {
188     OH_NNModel *model = OH_NNModel_Construct();
189     EXPECT_NE(nullptr, model);
190 
191     CropModel1 cropModel;
192     OHNNGraphArgs graphArgs = cropModel.graphArgs;
193     graphArgs.operands = {cropModel.input, cropModel.input, cropModel.shape, cropModel.output,
194                           cropModel.axis, cropModel.offsets};
195     graphArgs.inputIndices = {0, 1, 2};
196     graphArgs.outputIndices = {3};
197     graphArgs.paramIndices = {4, 5};
198     EXPECT_EQ(OH_NN_INVALID_PARAMETER, BuildSingleOpGraph(model, graphArgs));
199 
200     Free(model, nullptr, nullptr);
201 }
202 
203 /**
204  * @tc.number : SUB_AI_NNRt_Func_North_Crop_Build_05
205  * @tc.desc: CropModel1模型输出Tensor+1进行build测试
206  * @tc.type: FUNC
207  */
HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Build_05, Function | MediumTest | Level2)208 HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Build_05, Function | MediumTest | Level2)
209 {
210     OH_NNModel *model = OH_NNModel_Construct();
211     EXPECT_NE(nullptr, model);
212 
213     CropModel1 cropModel;
214     OHNNGraphArgs graphArgs = cropModel.graphArgs;
215     graphArgs.operands = {cropModel.input, cropModel.shape, cropModel.output, cropModel.output,
216                           cropModel.axis, cropModel.offsets};
217     graphArgs.inputIndices = {0, 1};
218     graphArgs.outputIndices = {2, 3};
219     graphArgs.paramIndices = {4, 5};
220     EXPECT_EQ(OH_NN_INVALID_PARAMETER, BuildSingleOpGraph(model, graphArgs));
221 
222     Free(model, nullptr, nullptr);
223 }
224 
225 /**
226  * @tc.number : SUB_AI_NNRt_Func_North_Crop_Build_06
227  * @tc.desc: CropModel1模型传入非法参数进行build测试
228  * @tc.type: FUNC
229  */
HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Build_06, Function | MediumTest | Level2)230 HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Build_06, Function | MediumTest | Level2)
231 {
232     OH_NNModel *model = OH_NNModel_Construct();
233     EXPECT_NE(nullptr, model);
234 
235     CropModel1 cropModel;
236     OHNNGraphArgs graphArgs = cropModel.graphArgs;
237 
238     int8_t activationValue = OH_NN_FUSED_NONE;
239     OHNNOperandTest activation = {OH_NN_INT8, OH_NN_ADD_ACTIVATIONTYPE, {}, &activationValue, sizeof(int8_t)};
240     graphArgs.operands = {cropModel.input, cropModel.shape, cropModel.output, cropModel.axis,
241                           cropModel.offsets, activation};
242     graphArgs.paramIndices = {3, 4, 5};
243     EXPECT_EQ(OH_NN_INVALID_PARAMETER, BuildSingleOpGraph(model, graphArgs));
244 
245     Free(model, nullptr, nullptr);
246 }
247 
248 /**
249  * @tc.number : SUB_AI_NNRt_Func_North_Crop_Model_Finish_01
250  * @tc.desc: 模型构图,未添加操作数
251  * @tc.type: FUNC
252  */
HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Model_Finish_01, Function | MediumTest | Level2)253 HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Model_Finish_01, Function | MediumTest | Level2)
254 {
255     OH_NNModel *model = OH_NNModel_Construct();
256     EXPECT_NE(nullptr, model);
257 
258     OHNNGraphArgs graphArgs;
259     EXPECT_EQ(OH_NN_INVALID_PARAMETER, SingleModelBuildEndStep(model, graphArgs));
260 
261     Free(model, nullptr, nullptr);
262 }
263 
264 /**
265  * @tc.number : SUB_AI_NNRt_Func_North_Crop_Model_Finish_02
266  * @tc.desc: 模型构图,未设置输入输出
267  * @tc.type: FUNC
268  */
HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Model_Finish_02, Function | MediumTest | Level2)269 HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Model_Finish_02, Function | MediumTest | Level2)
270 {
271     OH_NNModel *model = OH_NNModel_Construct();
272     EXPECT_NE(nullptr, model);
273 
274     CropModel1 cropModel;
275     OHNNGraphArgs graphArgs = cropModel.graphArgs;
276     graphArgs.specifyIO = false;
277     EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, BuildSingleOpGraph(model, graphArgs));
278 
279     Free(model, nullptr, nullptr);
280 }
281 
282 /**
283  * @tc.number : SUB_AI_NNRt_Func_North_Crop_Model_Finish_03
284  * @tc.desc: 模型构图,设置输入输出,构图成功
285  * @tc.type: FUNC
286  */
HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Model_Finish_03, Function | MediumTest | Level1)287 HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Model_Finish_03, Function | MediumTest | Level1)
288 {
289     OH_NNModel *model = OH_NNModel_Construct();
290     EXPECT_NE(nullptr, model);
291 
292     CropModel1 cropModel;
293     OHNNGraphArgs graphArgs = cropModel.graphArgs;
294     EXPECT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
295 
296     Free(model, nullptr, nullptr);
297 }
298 
299 /**
300  * @tc.number : SUB_AI_NNRt_Func_North_Crop_Model_SetOperandValue_01
301  * @tc.desc: 设置操作数值,操作数不存在
302  * @tc.type: FUNC
303  */
HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Model_SetOperandValue_01, Function | MediumTest | Level2)304 HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Model_SetOperandValue_01, Function | MediumTest | Level2)
305 {
306     OH_NNModel *model = OH_NNModel_Construct();
307     EXPECT_NE(nullptr, model);
308 
309     CropModel1 cropModel;
310     OHNNGraphArgs graphArgs = cropModel.graphArgs;
311 
312     NN_TensorDesc* tensorDesc = nullptr;
313     std::vector<NN_TensorDesc*> tensorDescVec;
314 
315     for (size_t i = 0; i < graphArgs.operands.size(); i++) {
316         const OHNNOperandTest &operandTem = graphArgs.operands[i];
317         tensorDesc = createTensorDesc(operandTem.shape.data(),
318                                       (uint32_t) operandTem.shape.size(),
319                                       operandTem.dataType, operandTem.format);
320         tensorDescVec.emplace_back(tensorDesc);
321         EXPECT_EQ(OH_NN_SUCCESS, OH_NNModel_AddTensorToModel(model, tensorDesc));
322         EXPECT_EQ(OH_NN_SUCCESS, OH_NNModel_SetTensorType(model, i, operandTem.type));
323 
324         if (std::find(graphArgs.paramIndices.begin(), graphArgs.paramIndices.end(), i) !=
325             graphArgs.paramIndices.end()) {
326             EXPECT_EQ(OH_NN_INVALID_PARAMETER, OH_NNModel_SetTensorData(
327                 model, 1000+i, operandTem.data, operandTem.length));
328         }
329     }
330 
331     FreeTensorDescVec(tensorDescVec);
332     Free(model, nullptr, nullptr);
333 }
334 
335 /**
336  * @tc.number : SUB_AI_NNRt_Func_North_Crop_Model_SetOperandValue_02
337  * @tc.desc: 设置操作数值,buffer为nullptr
338  * @tc.type: FUNC
339  */
HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Model_SetOperandValue_02, Function | MediumTest | Level2)340 HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Model_SetOperandValue_02, Function | MediumTest | Level2)
341 {
342     OH_NNModel *model = OH_NNModel_Construct();
343     EXPECT_NE(nullptr, model);
344 
345     CropModel1 cropModel;
346     OHNNGraphArgs graphArgs = cropModel.graphArgs;
347 
348     NN_TensorDesc* tensorDesc = nullptr;
349     std::vector<NN_TensorDesc*> tensorDescVec;
350 
351     for (size_t i = 0; i < graphArgs.operands.size(); i++) {
352         const OHNNOperandTest &operandTem = graphArgs.operands[i];
353         tensorDesc = createTensorDesc(operandTem.shape.data(),
354                                       (uint32_t) operandTem.shape.size(),
355                                       operandTem.dataType, operandTem.format);
356         tensorDescVec.emplace_back(tensorDesc);
357         EXPECT_EQ(OH_NN_SUCCESS, OH_NNModel_AddTensorToModel(model, tensorDesc));
358         EXPECT_EQ(OH_NN_SUCCESS, OH_NNModel_SetTensorType(model, i, operandTem.type));
359 
360         if (std::find(graphArgs.paramIndices.begin(), graphArgs.paramIndices.end(), i) !=
361             graphArgs.paramIndices.end()) {
362             EXPECT_EQ(OH_NN_INVALID_PARAMETER, OH_NNModel_SetTensorData(model, i, nullptr, operandTem.length));
363         }
364     }
365 
366     FreeTensorDescVec(tensorDescVec);
367     Free(model, nullptr, nullptr);
368 }
369 
370 /**
371  * @tc.number : SUB_AI_NNRt_Func_North_Crop_Model_SetOperandValue_03
372  * @tc.desc: 设置操作数值,length为0
373  * @tc.type: FUNC
374  */
HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Model_SetOperandValue_03, Function | MediumTest | Level2)375 HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Model_SetOperandValue_03, Function | MediumTest | Level2)
376 {
377     OH_NNModel *model = OH_NNModel_Construct();
378     EXPECT_NE(nullptr, model);
379 
380     CropModel1 cropModel;
381     OHNNGraphArgs graphArgs = cropModel.graphArgs;
382 
383     NN_TensorDesc* tensorDesc = nullptr;
384     std::vector<NN_TensorDesc*> tensorDescVec;
385 
386     for (size_t i = 0; i < graphArgs.operands.size(); i++) {
387         const OHNNOperandTest &operandTem = graphArgs.operands[i];
388         tensorDesc = createTensorDesc(operandTem.shape.data(),
389                                       (uint32_t) operandTem.shape.size(),
390                                       operandTem.dataType, operandTem.format);
391         tensorDescVec.emplace_back(tensorDesc);
392         EXPECT_EQ(OH_NN_SUCCESS, OH_NNModel_AddTensorToModel(model, tensorDesc));
393         EXPECT_EQ(OH_NN_SUCCESS, OH_NNModel_SetTensorType(model, i, operandTem.type));
394 
395         if (std::find(graphArgs.paramIndices.begin(), graphArgs.paramIndices.end(), i) !=
396             graphArgs.paramIndices.end()) {
397             EXPECT_EQ(OH_NN_INVALID_PARAMETER, OH_NNModel_SetTensorData(model, 1000+i, operandTem.data, 0));
398         }
399     }
400 
401     FreeTensorDescVec(tensorDescVec);
402     Free(model, nullptr, nullptr);
403 }
404 
405 /**
406  * @tc.number : SUB_AI_NNRt_Func_North_Crop_Model_SpecifyInputsAndOutputs_01
407  * @tc.desc: 设置输入输出,inputIndices为nullptr
408  * @tc.type: FUNC
409  */
HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Model_SpecifyInputsAndOutputs_01, Function | MediumTest | Level2)410 HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Model_SpecifyInputsAndOutputs_01,
411          Function | MediumTest | Level2)
412 {
413     OH_NNModel *model = OH_NNModel_Construct();
414     EXPECT_NE(nullptr, model);
415 
416     CropModel1 cropModel;
417     OHNNGraphArgs graphArgs = cropModel.graphArgs;
418     graphArgs.specifyIO = false;
419     graphArgs.build = false;
420     EXPECT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
421 
422     auto inputIndices = TransformUInt32Array(graphArgs.inputIndices);
423     auto outputIndices = TransformUInt32Array(graphArgs.outputIndices);
424     EXPECT_EQ(OH_NN_INVALID_PARAMETER, OH_NNModel_SpecifyInputsAndOutputs(model, nullptr, &outputIndices));
425 
426     Free(model, nullptr, nullptr);
427 }
428 
429 /**
430  * @tc.number : SUB_AI_NNRt_Func_North_Crop_Model_SpecifyInputsAndOutputs_02
431  * @tc.desc: 设置输入输出,inputindices中data为nullptr
432  * @tc.type: FUNC
433  */
HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Model_SpecifyInputsAndOutputs_02, Function | MediumTest | Level2)434 HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Model_SpecifyInputsAndOutputs_02,
435          Function | MediumTest | Level2)
436 {
437     OH_NNModel *model = OH_NNModel_Construct();
438     EXPECT_NE(nullptr, model);
439 
440     CropModel1 cropModel;
441     OHNNGraphArgs graphArgs = cropModel.graphArgs;
442     graphArgs.specifyIO = false;
443     graphArgs.build = false;
444     EXPECT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
445 
446     auto inputIndices = TransformUInt32Array(graphArgs.inputIndices);
447     auto outputIndices = TransformUInt32Array(graphArgs.outputIndices);
448     inputIndices.data = nullptr;
449     EXPECT_EQ(OH_NN_INVALID_PARAMETER, OH_NNModel_SpecifyInputsAndOutputs(model, &inputIndices, &outputIndices));
450 
451     Free(model, nullptr, nullptr);
452 }
453 
454 /**
455  * @tc.number : SUB_AI_NNRt_Func_North_Crop_Model_SpecifyInputsAndOutputs_03
456  * @tc.desc: 设置输入输出,inputindices中data对应序号不存在
457  * @tc.type: FUNC
458  */
HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Model_SpecifyInputsAndOutputs_03, Function | MediumTest | Level2)459 HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Model_SpecifyInputsAndOutputs_03,
460          Function | MediumTest | Level2)
461 {
462     OH_NNModel *model = OH_NNModel_Construct();
463     EXPECT_NE(nullptr, model);
464 
465     CropModel1 cropModel;
466     OHNNGraphArgs graphArgs = cropModel.graphArgs;
467     graphArgs.specifyIO = false;
468     graphArgs.build = false;
469     EXPECT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
470 
471     graphArgs.inputIndices = {100000};
472     auto inputIndices = TransformUInt32Array(graphArgs.inputIndices);
473     auto outputIndices = TransformUInt32Array(graphArgs.outputIndices);
474     EXPECT_EQ(OH_NN_INVALID_PARAMETER, OH_NNModel_SpecifyInputsAndOutputs(model, &inputIndices, &outputIndices));
475 
476     Free(model, nullptr, nullptr);
477 }
478 
479 /**
480  * @tc.number : SUB_AI_NNRt_Func_North_Crop_Model_SpecifyInputsAndOutputs_04
481  * @tc.desc: 设置输入输出,inputindices中size为0
482  * @tc.type: FUNC
483  */
HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Model_SpecifyInputsAndOutputs_04, Function | MediumTest | Level2)484 HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Model_SpecifyInputsAndOutputs_04,
485          Function | MediumTest | Level2)
486 {
487     OH_NNModel *model = OH_NNModel_Construct();
488     EXPECT_NE(nullptr, model);
489 
490     CropModel1 cropModel;
491     OHNNGraphArgs graphArgs = cropModel.graphArgs;
492     graphArgs.specifyIO = false;
493     graphArgs.build = false;
494     EXPECT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
495 
496     auto inputIndices = TransformUInt32Array(graphArgs.inputIndices);
497     auto outputIndices = TransformUInt32Array(graphArgs.outputIndices);
498     inputIndices.size = 0;
499     EXPECT_EQ(OH_NN_INVALID_PARAMETER, OH_NNModel_SpecifyInputsAndOutputs(model, &inputIndices, &outputIndices));
500 
501     Free(model, nullptr, nullptr);
502 }
503 
504 /**
505  * @tc.number : SUB_AI_NNRt_Func_North_Crop_Model_SpecifyInputsAndOutputs_05
506  * @tc.desc: 设置输入输出,outputindices为nullptr
507  * @tc.type: FUNC
508  */
HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Model_SpecifyInputsAndOutputs_05, Function | MediumTest | Level2)509 HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Model_SpecifyInputsAndOutputs_05,
510          Function | MediumTest | Level2)
511 {
512     OH_NNModel *model = OH_NNModel_Construct();
513     EXPECT_NE(nullptr, model);
514 
515     CropModel1 cropModel;
516     OHNNGraphArgs graphArgs = cropModel.graphArgs;
517     graphArgs.specifyIO = false;
518     graphArgs.build = false;
519     EXPECT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
520 
521     auto inputIndices = TransformUInt32Array(graphArgs.inputIndices);
522     auto outputIndices = TransformUInt32Array(graphArgs.outputIndices);
523     EXPECT_EQ(OH_NN_INVALID_PARAMETER, OH_NNModel_SpecifyInputsAndOutputs(model, &inputIndices, nullptr));
524 
525     Free(model, nullptr, nullptr);
526 }
527 
528 /**
529  * @tc.number : SUB_AI_NNRt_Func_North_Crop_Model_SpecifyInputsAndOutputs_06
530  * @tc.desc: 设置输入输出,outputindices中data为nullptr
531  * @tc.type: FUNC
532  */
HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Model_SpecifyInputsAndOutputs_06, Function | MediumTest | Level2)533 HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Model_SpecifyInputsAndOutputs_06,
534          Function | MediumTest | Level2)
535 {
536     OH_NNModel *model = OH_NNModel_Construct();
537     EXPECT_NE(nullptr, model);
538 
539     CropModel1 cropModel;
540     OHNNGraphArgs graphArgs = cropModel.graphArgs;
541     graphArgs.specifyIO = false;
542     graphArgs.build = false;
543     EXPECT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
544 
545     auto inputIndices = TransformUInt32Array(graphArgs.inputIndices);
546     auto outputIndices = TransformUInt32Array(graphArgs.outputIndices);
547     outputIndices.data = nullptr;
548     EXPECT_EQ(OH_NN_INVALID_PARAMETER, OH_NNModel_SpecifyInputsAndOutputs(model, &inputIndices, &outputIndices));
549 
550     Free(model, nullptr, nullptr);
551 }
552 
553 /**
554  * @tc.number : SUB_AI_NNRt_Func_North_Crop_Model_SpecifyInputsAndOutputs_07
555  * @tc.desc: 设置输入输出,outputindices中data对应序号不存在
556  * @tc.type: FUNC
557  */
HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Model_SpecifyInputsAndOutputs_07, Function | MediumTest | Level2)558 HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Model_SpecifyInputsAndOutputs_07,
559          Function | MediumTest | Level2)
560 {
561     OH_NNModel *model = OH_NNModel_Construct();
562     EXPECT_NE(nullptr, model);
563 
564     CropModel1 cropModel;
565     OHNNGraphArgs graphArgs = cropModel.graphArgs;
566     graphArgs.specifyIO = false;
567     graphArgs.build = false;
568     EXPECT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
569 
570     graphArgs.outputIndices = {100000};
571     auto inputIndices = TransformUInt32Array(graphArgs.inputIndices);
572     auto outputIndices = TransformUInt32Array(graphArgs.outputIndices);
573     EXPECT_EQ(OH_NN_INVALID_PARAMETER, OH_NNModel_SpecifyInputsAndOutputs(model, &inputIndices, &outputIndices));
574 
575     Free(model, nullptr, nullptr);
576 }
577 
578 /**
579  * @tc.number : SUB_AI_NNRt_Func_North_Crop_Model_SpecifyInputsAndOutputs_08
580  * @tc.desc: 设置输入输出,outputindices中size为0
581  * @tc.type: FUNC
582  */
HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Model_SpecifyInputsAndOutputs_08, Function | MediumTest | Level2)583 HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Model_SpecifyInputsAndOutputs_08,
584          Function | MediumTest | Level2)
585 {
586     OH_NNModel *model = OH_NNModel_Construct();
587     EXPECT_NE(nullptr, model);
588 
589     CropModel1 cropModel;
590     OHNNGraphArgs graphArgs = cropModel.graphArgs;
591     graphArgs.specifyIO = false;
592     graphArgs.build = false;
593     EXPECT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
594 
595     auto inputIndices = TransformUInt32Array(graphArgs.inputIndices);
596     auto outputIndices = TransformUInt32Array(graphArgs.outputIndices);
597     outputIndices.size = 0;
598     EXPECT_EQ(OH_NN_INVALID_PARAMETER, OH_NNModel_SpecifyInputsAndOutputs(model, &inputIndices, &outputIndices));
599 
600     Free(model, nullptr, nullptr);
601 }
602 
603 /**
604  * @tc.number : SUB_AI_NNRt_Func_North_Crop_Model_AddOperation_01
605  * @tc.desc: 添加算子,paramindices为nullptr
606  * @tc.type: FUNC
607  */
HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Model_AddOperation_01, Function | MediumTest | Level2)608 HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Model_AddOperation_01, Function | MediumTest | Level2)
609 {
610     OH_NNModel *model = OH_NNModel_Construct();
611     EXPECT_NE(nullptr, model);
612 
613     CropModel1 cropModel;
614     OHNNGraphArgs graphArgs = cropModel.graphArgs;
615     graphArgs.addOperation = false;
616     graphArgs.specifyIO = false;
617     graphArgs.build = false;
618     EXPECT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
619 
620     auto inputIndices = TransformUInt32Array(graphArgs.inputIndices);
621     auto outputIndices = TransformUInt32Array(graphArgs.outputIndices);
622     EXPECT_EQ(OH_NN_INVALID_PARAMETER, OH_NNModel_AddOperation(model, graphArgs.operationType,
623                                                                nullptr, &inputIndices, &outputIndices));
624 
625     Free(model, nullptr, nullptr);
626 }
627 
628 /**
629  * @tc.number : SUB_AI_NNRt_Func_North_Crop_Model_AddOperation_02
630  * @tc.desc: 添加算子,paramindices中data为nullptr
631  * @tc.type: FUNC
632  */
HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Model_AddOperation_02, Function | MediumTest | Level2)633 HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Model_AddOperation_02, Function | MediumTest | Level2)
634 {
635     OH_NNModel *model = OH_NNModel_Construct();
636     EXPECT_NE(nullptr, model);
637 
638     CropModel1 cropModel;
639     OHNNGraphArgs graphArgs = cropModel.graphArgs;
640     graphArgs.addOperation = false;
641     graphArgs.specifyIO = false;
642     graphArgs.build = false;
643     EXPECT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
644 
645     auto paramIndices = TransformUInt32Array(graphArgs.paramIndices);
646     auto inputIndices = TransformUInt32Array(graphArgs.inputIndices);
647     auto outputIndices = TransformUInt32Array(graphArgs.outputIndices);
648     paramIndices.data = nullptr;
649     EXPECT_EQ(OH_NN_INVALID_PARAMETER, OH_NNModel_AddOperation(model, graphArgs.operationType,
650                                                                &paramIndices, &inputIndices, &outputIndices));
651 
652     Free(model, nullptr, nullptr);
653 }
654 
655 /**
656  * @tc.number : SUB_AI_NNRt_Func_North_Crop_Model_AddOperation_03
657  * @tc.desc: 添加算子,paramindices中data对应序号不存在
658  * @tc.type: FUNC
659  */
HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Model_AddOperation_03, Function | MediumTest | Level2)660 HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Model_AddOperation_03, Function | MediumTest | Level2)
661 {
662     OH_NNModel *model = OH_NNModel_Construct();
663     EXPECT_NE(nullptr, model);
664 
665     CropModel1 cropModel;
666     OHNNGraphArgs graphArgs = cropModel.graphArgs;
667     graphArgs.addOperation = false;
668     graphArgs.specifyIO = false;
669     graphArgs.build = false;
670     EXPECT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
671 
672     graphArgs.paramIndices = {100000};
673     auto paramIndices = TransformUInt32Array(graphArgs.paramIndices);
674     auto inputIndices = TransformUInt32Array(graphArgs.inputIndices);
675     auto outputIndices = TransformUInt32Array(graphArgs.outputIndices);
676     EXPECT_EQ(OH_NN_INVALID_PARAMETER, OH_NNModel_AddOperation(model, graphArgs.operationType,
677                                                                &paramIndices, &inputIndices, &outputIndices));
678 
679     Free(model, nullptr, nullptr);
680 }
681 
682 /**
683  * @tc.number : SUB_AI_NNRt_Func_North_Crop_Model_AddOperation_04
684  * @tc.desc: 添加算子,paramindices中size为0
685  * @tc.type: FUNC
686  */
HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Model_AddOperation_04, Function | MediumTest | Level2)687 HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Model_AddOperation_04, Function | MediumTest | Level2)
688 {
689     OH_NNModel *model = OH_NNModel_Construct();
690     EXPECT_NE(nullptr, model);
691 
692     CropModel1 cropModel;
693     OHNNGraphArgs graphArgs = cropModel.graphArgs;
694     graphArgs.addOperation = false;
695     graphArgs.specifyIO = false;
696     graphArgs.build = false;
697     EXPECT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
698 
699     auto paramIndices = TransformUInt32Array(graphArgs.paramIndices);
700     auto inputIndices = TransformUInt32Array(graphArgs.inputIndices);
701     auto outputIndices = TransformUInt32Array(graphArgs.outputIndices);
702     paramIndices.size = 0;
703     EXPECT_EQ(OH_NN_INVALID_PARAMETER, OH_NNModel_AddOperation(model, graphArgs.operationType,
704                                                                &paramIndices, &inputIndices, &outputIndices));
705 
706     Free(model, nullptr, nullptr);
707 }
708 
709 /**
710  * @tc.number : SUB_AI_NNRt_Func_North_Crop_Model_AddOperation_05
711  * @tc.desc: 添加算子,inputindices为nullptr
712  * @tc.type: FUNC
713  */
HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Model_AddOperation_05, Function | MediumTest | Level2)714 HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Model_AddOperation_05, Function | MediumTest | Level2)
715 {
716     OH_NNModel *model = OH_NNModel_Construct();
717     EXPECT_NE(nullptr, model);
718 
719     CropModel1 cropModel;
720     OHNNGraphArgs graphArgs = cropModel.graphArgs;
721     graphArgs.addOperation = false;
722     graphArgs.specifyIO = false;
723     graphArgs.build = false;
724     EXPECT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
725 
726     auto paramIndices = TransformUInt32Array(graphArgs.paramIndices);
727     auto outputIndices = TransformUInt32Array(graphArgs.outputIndices);
728     EXPECT_EQ(OH_NN_INVALID_PARAMETER, OH_NNModel_AddOperation(model, graphArgs.operationType,
729                                                                &paramIndices, nullptr, &outputIndices));
730 
731     Free(model, nullptr, nullptr);
732 }
733 
734 /**
735  * @tc.number : SUB_AI_NNRt_Func_North_Crop_Model_AddOperation_06
736  * @tc.desc: 添加算子,inputindices中data为nullptr
737  * @tc.type: FUNC
738  */
HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Model_AddOperation_06, Function | MediumTest | Level2)739 HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Model_AddOperation_06, Function | MediumTest | Level2)
740 {
741     OH_NNModel *model = OH_NNModel_Construct();
742     EXPECT_NE(nullptr, model);
743 
744     CropModel1 cropModel;
745     OHNNGraphArgs graphArgs = cropModel.graphArgs;
746     graphArgs.addOperation = false;
747     graphArgs.specifyIO = false;
748     graphArgs.build = false;
749     EXPECT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
750 
751     auto paramIndices = TransformUInt32Array(graphArgs.paramIndices);
752     auto inputIndices = TransformUInt32Array(graphArgs.inputIndices);
753     auto outputIndices = TransformUInt32Array(graphArgs.outputIndices);
754     inputIndices.data = nullptr;
755     EXPECT_EQ(OH_NN_INVALID_PARAMETER, OH_NNModel_AddOperation(model, graphArgs.operationType,
756                                                                &paramIndices, &inputIndices, &outputIndices));
757 
758     Free(model, nullptr, nullptr);
759 }
760 
761 /**
762  * @tc.number : SUB_AI_NNRt_Func_North_Crop_Model_AddOperation_07
763  * @tc.desc: 添加算子,inputindices中data对应序号不存在
764  * @tc.type: FUNC
765  */
HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Model_AddOperation_07, Function | MediumTest | Level2)766 HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Model_AddOperation_07, Function | MediumTest | Level2)
767 {
768     OH_NNModel *model = OH_NNModel_Construct();
769     EXPECT_NE(nullptr, model);
770 
771     CropModel1 cropModel;
772     OHNNGraphArgs graphArgs = cropModel.graphArgs;
773     graphArgs.addOperation = false;
774     graphArgs.specifyIO = false;
775     graphArgs.build = false;
776     EXPECT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
777 
778     graphArgs.inputIndices = {100000};
779     auto paramIndices = TransformUInt32Array(graphArgs.paramIndices);
780     auto inputIndices = TransformUInt32Array(graphArgs.inputIndices);
781     auto outputIndices = TransformUInt32Array(graphArgs.outputIndices);
782     EXPECT_EQ(OH_NN_INVALID_PARAMETER, OH_NNModel_AddOperation(model, graphArgs.operationType,
783                                                                &paramIndices, &inputIndices, &outputIndices));
784 
785     Free(model, nullptr, nullptr);
786 }
787 
788 /**
789  * @tc.number : SUB_AI_NNRt_Func_North_Crop_Model_AddOperation_08
790  * @tc.desc: 添加算子,inputindices中size为0
791  * @tc.type: FUNC
792  */
HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Model_AddOperation_08, Function | MediumTest | Level2)793 HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Model_AddOperation_08, Function | MediumTest | Level2)
794 {
795     OH_NNModel *model = OH_NNModel_Construct();
796     EXPECT_NE(nullptr, model);
797 
798     CropModel1 cropModel;
799     OHNNGraphArgs graphArgs = cropModel.graphArgs;
800     graphArgs.addOperation = false;
801     graphArgs.specifyIO = false;
802     graphArgs.build = false;
803     EXPECT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
804 
805     auto paramIndices = TransformUInt32Array(graphArgs.paramIndices);
806     auto inputIndices = TransformUInt32Array(graphArgs.inputIndices);
807     auto outputIndices = TransformUInt32Array(graphArgs.outputIndices);
808     inputIndices.size = 0;
809     EXPECT_EQ(OH_NN_INVALID_PARAMETER, OH_NNModel_AddOperation(model, graphArgs.operationType,
810                                                                &paramIndices, &inputIndices, &outputIndices));
811 
812     Free(model, nullptr, nullptr);
813 }
814 
815 /**
816  * @tc.number : SUB_AI_NNRt_Func_North_Crop_Model_AddOperation_09
817  * @tc.desc: 添加算子,outputindices为nullptr
818  * @tc.type: FUNC
819  */
HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Model_AddOperation_09, Function | MediumTest | Level2)820 HWTEST_F(CropTest, SUB_AI_NNRt_Func_North_Crop_Model_AddOperation_09, Function | MediumTest | Level2)
821 {
822     OH_NNModel *model = OH_NNModel_Construct();
823     EXPECT_NE(nullptr, model);
824 
825     CropModel1 cropModel;
826     OHNNGraphArgs graphArgs = cropModel.graphArgs;
827     graphArgs.addOperation = false;
828     graphArgs.specifyIO = false;
829     graphArgs.build = false;
830     EXPECT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
831 
832     auto paramIndices = TransformUInt32Array(graphArgs.paramIndices);
833     auto inputIndices = TransformUInt32Array(graphArgs.inputIndices);
834     EXPECT_EQ(OH_NN_INVALID_PARAMETER, OH_NNModel_AddOperation(nullptr, graphArgs.operationType,
835                                                                &paramIndices, &inputIndices, nullptr));
836 
837     Free(model, nullptr, nullptr);
838 }