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 <gmock/gmock.h>
17 #include <gtest/gtest.h>
18
19 #include "common/utils.h"
20 #include "common/log.h"
21 #include "frameworks/native/nn_tensor.h"
22 #include "frameworks/native/inner_model.h"
23
24 using namespace testing;
25 using namespace testing::ext;
26 using namespace OHOS::NeuralNetworkRuntime;
27
28 namespace NNRT {
29 namespace UnitTest {
30 class InnerModelTest : public testing::Test {
31 public:
32 void SetLiteGraph(mindspore::lite::LiteGraph* liteGraph);
33 void SetTensors();
34 void SetIndices();
35
36 public:
37 InnerModel m_innerModelTest;
38
39 std::vector<int32_t> m_dimInput{3, 3};
40 std::vector<int32_t> m_dimOutput{3, 3};
41 std::vector<uint32_t> m_inputIndices{0};
42 std::vector<uint32_t> m_outputIndices{1};
43
44 OH_NN_OperationType m_opType{OH_NN_OPS_ADD};
45
46 OH_NN_UInt32Array m_inputs;
47 OH_NN_UInt32Array m_outputs;
48 OH_NN_UInt32Array m_params;
49
50 uint32_t m_paramIndexs[1]{3};
51 uint32_t m_inputIndexs[2]{0, 1};
52 uint32_t m_outputIndexs[1]{2};
53 };
54
SetLiteGraph(mindspore::lite::LiteGraph * liteGraph)55 void InnerModelTest::SetLiteGraph(mindspore::lite::LiteGraph* liteGraph)
56 {
57 liteGraph->name_ = "testGraph";
58 liteGraph->input_indices_ = m_inputIndices;
59 liteGraph->output_indices_ = m_outputIndices;
60
61 const std::vector<mindspore::lite::QuantParam> quant_params {};
62
63 for (size_t indexInput = 0; indexInput < liteGraph->input_indices_.size(); ++indexInput) {
64 const std::vector<uint8_t> data(36, 1);
65 liteGraph->all_tensors_.emplace_back(mindspore::lite::MindIR_Tensor_Create(liteGraph->name_,
66 mindspore::lite::DATA_TYPE_FLOAT32, m_dimInput, mindspore::lite::FORMAT_NCHW, data, quant_params));
67 }
68
69 for (size_t indexOutput = 0; indexOutput < liteGraph->output_indices_.size(); ++indexOutput) {
70 const std::vector<uint8_t> dataOut(36, 1);
71 liteGraph->all_tensors_.emplace_back(mindspore::lite::MindIR_Tensor_Create(liteGraph->name_,
72 mindspore::lite::DATA_TYPE_FLOAT32, m_dimOutput, mindspore::lite::FORMAT_NCHW, dataOut, quant_params));
73 }
74 }
75
SetTensors()76 void InnerModelTest::SetTensors()
77 {
78 const int dim[2] = {2, 2};
79 const OH_NN_Tensor& tensor = {OH_NN_FLOAT32, 2, dim, nullptr, OH_NN_TENSOR};
80
81 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.AddTensor(tensor));
82 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.AddTensor(tensor));
83 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.AddTensor(tensor));
84
85 const OH_NN_Tensor& tensorParam = {OH_NN_INT8, 0, nullptr, nullptr, OH_NN_ADD_ACTIVATIONTYPE};
86 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.AddTensor(tensorParam));
87 }
88
SetIndices()89 void InnerModelTest::SetIndices()
90 {
91 m_params.data = m_paramIndexs;
92 m_params.size = sizeof(m_paramIndexs) / sizeof(uint32_t);
93
94 m_inputs.data = m_inputIndexs;
95 m_inputs.size = sizeof(m_inputIndexs) / sizeof(uint32_t);
96
97 m_outputs.data = m_outputIndexs;
98 m_outputs.size = sizeof(m_outputIndexs) / sizeof(uint32_t);
99 }
100
101 /**
102 * @tc.name: inner_model_construct_nntensor_from_litegraph_001
103 * @tc.desc: Verify the input_indices is empty of the construct_nntensor_from_litegraph function
104 * @tc.type: FUNC
105 */
106 HWTEST_F(InnerModelTest, inner_model_construct_nntensor_from_litegraph_001, TestSize.Level1)
107 {
108 mindspore::lite::LiteGraph* liteGraph = new (std::nothrow) mindspore::lite::LiteGraph();
109 EXPECT_NE(nullptr, liteGraph);
110 m_inputIndices = {};
111
112 SetLiteGraph(liteGraph);
113 EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_innerModelTest.BuildFromLiteGraph(liteGraph));
114 mindspore::lite::MindIR_LiteGraph_Destroy(&liteGraph);
115 }
116
117 /**
118 * @tc.name: inner_model_construct_nntensor_from_litegraph_002
119 * @tc.desc: Verify the input_indices is out of bounds of the construct_nntensor_from_litegraph function
120 * @tc.type: FUNC
121 */
122 HWTEST_F(InnerModelTest, inner_model_construct_nntensor_from_litegraph_002, TestSize.Level1)
123 {
124 mindspore::lite::LiteGraph* liteGraph = new (std::nothrow) mindspore::lite::LiteGraph();
125 EXPECT_NE(nullptr, liteGraph);
126 m_inputIndices = {6};
127
128 SetLiteGraph(liteGraph);
129 EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_innerModelTest.BuildFromLiteGraph(liteGraph));
130 mindspore::lite::MindIR_LiteGraph_Destroy(&liteGraph);
131 }
132
133 /**
134 * @tc.name: inner_model_construct_nntensor_from_litegraph_003
135 * @tc.desc: Verify the success of the construct_nntensor_from_litegraph function
136 * @tc.type: FUNC
137 */
138 HWTEST_F(InnerModelTest, inner_model_construct_nntensor_from_litegraph_003, TestSize.Level1)
139 {
140 mindspore::lite::LiteGraph* liteGraph = new (std::nothrow) mindspore::lite::LiteGraph();
141 EXPECT_NE(nullptr, liteGraph);
142
143 SetLiteGraph(liteGraph);
144 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.BuildFromLiteGraph(liteGraph));
145 }
146
147 /**
148 * @tc.name: inner_model_construct_nntensor_from_litegraph_004
149 * @tc.desc: Verify the nntensor build failed nullptr return of the construct_nntensor_from_litegraph function
150 * @tc.type: FUNC
151 */
152 HWTEST_F(InnerModelTest, inner_model_construct_nntensor_from_litegraph_004, TestSize.Level1)
153 {
154 mindspore::lite::LiteGraph* liteGraph = new (std::nothrow) mindspore::lite::LiteGraph();
155 EXPECT_NE(nullptr, liteGraph);
156 m_dimInput = {3, -3};
157
158 SetLiteGraph(liteGraph);
159 EXPECT_EQ(OH_NN_NULL_PTR, m_innerModelTest.BuildFromLiteGraph(liteGraph));
160 mindspore::lite::MindIR_LiteGraph_Destroy(&liteGraph);
161 }
162
163 /**
164 * @tc.name: inner_model_construct_nntensor_from_litegraph_005
165 * @tc.desc: Verify the output indices out of bounds of the construct_nntensor_from_litegraph function
166 * @tc.type: FUNC
167 */
168 HWTEST_F(InnerModelTest, inner_model_construct_nntensor_from_litegraph_005, TestSize.Level1)
169 {
170 mindspore::lite::LiteGraph* liteGraph = new (std::nothrow) mindspore::lite::LiteGraph();
171 EXPECT_NE(nullptr, liteGraph);
172 m_outputIndices = {6};
173
174 SetLiteGraph(liteGraph);
175 EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_innerModelTest.BuildFromLiteGraph(liteGraph));
176 mindspore::lite::MindIR_LiteGraph_Destroy(&liteGraph);
177 }
178
179 /**
180 * @tc.name: inner_model_build_from_lite_graph_001
181 * @tc.desc: Verify the litegraph is nullptr of the build_from_lite_graph function
182 * @tc.type: FUNC
183 */
184 HWTEST_F(InnerModelTest, inner_model_build_from_lite_graph_001, TestSize.Level1)
185 {
186 EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_innerModelTest.BuildFromLiteGraph(nullptr));
187 }
188
189 /**
190 * @tc.name: inner_model_build_from_lite_graph_002
191 * @tc.desc: Verify the buildfromlitegraph twice forbidden of the build_from_lite_graph function
192 * @tc.type: FUNC
193 */
194 HWTEST_F(InnerModelTest, inner_model_build_from_lite_graph_002, TestSize.Level1)
195 {
196 mindspore::lite::LiteGraph* liteGraph = new (std::nothrow) mindspore::lite::LiteGraph();
197 EXPECT_NE(nullptr, liteGraph);
198
199 SetLiteGraph(liteGraph);
200 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.BuildFromLiteGraph(liteGraph));
201 EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, m_innerModelTest.BuildFromLiteGraph(liteGraph));
202 }
203
204 /**
205 * @tc.name: inner_model_build_from_lite_graph_003
206 * @tc.desc: Verify the litegraph->alltensors is empty of the build_from_lite_graph function
207 * @tc.type: FUNC
208 */
209 HWTEST_F(InnerModelTest, inner_model_build_from_lite_graph_003, TestSize.Level1)
210 {
211 mindspore::lite::LiteGraph* liteGraph = new (std::nothrow) mindspore::lite::LiteGraph();
212 EXPECT_NE(nullptr, liteGraph);
213 liteGraph->name_ = "testGraph";
214 liteGraph->input_indices_ = {0};
215 liteGraph->output_indices_ = {1};
216
217 const int32_t dimInput[2] = {2, 2};
218 const OH_NN_Tensor& tensor = {OH_NN_INT8, 2, dimInput, nullptr, OH_NN_TENSOR};
219 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.AddTensor(tensor));
220
221 EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, m_innerModelTest.BuildFromLiteGraph(liteGraph));
222 mindspore::lite::MindIR_LiteGraph_Destroy(&liteGraph);
223 }
224
225
226 /**
227 * @tc.name: inner_model_add_tensor_001
228 * @tc.desc: Verify the success of the addtensor function
229 * @tc.type: FUNC
230 */
231 HWTEST_F(InnerModelTest, inner_model_add_tensor_001, TestSize.Level1)
232 {
233 const int32_t dimInput[2] = {2, 2};
234 const OH_NN_Tensor& tensor = {OH_NN_INT8, 2, dimInput, nullptr, OH_NN_TENSOR};
235 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.AddTensor(tensor));
236 }
237
238 /**
239 * @tc.name: inner_model_add_tensor_002
240 * @tc.desc: Verify the addtensor after buildfromlitegraph of the addtensor function
241 * @tc.type: FUNC
242 */
243 HWTEST_F(InnerModelTest, inner_model_add_tensor_002, TestSize.Level1)
244 {
245 mindspore::lite::LiteGraph* liteGraph = new (std::nothrow) mindspore::lite::LiteGraph();
246 EXPECT_NE(nullptr, liteGraph);
247 SetLiteGraph(liteGraph);
248 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.BuildFromLiteGraph(liteGraph));
249
250 const int32_t dimInput[2] = {2, 2};
251 const OH_NN_Tensor& tensor = {OH_NN_INT8, 2, dimInput, nullptr, OH_NN_TENSOR};
252 EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, m_innerModelTest.AddTensor(tensor));
253 }
254
255 /**
256 * @tc.name: inner_model_add_tensor_003
257 * @tc.desc: Verify the buildfromnntensor failed of the addtensor function
258 * @tc.type: FUNC
259 */
260 HWTEST_F(InnerModelTest, inner_model_add_tensor_003, TestSize.Level1)
261 {
262 const int32_t dimInput[2] = {2, -2};
263 const OH_NN_Tensor& tensor = {OH_NN_INT8, 2, dimInput, nullptr, OH_NN_TENSOR};
264 EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_innerModelTest.AddTensor(tensor));
265 }
266
267
268 /**
269 * @tc.name: inner_model_set_tensor_value_001
270 * @tc.desc: Verify the success of the set_tensor_value function
271 * @tc.type: FUNC
272 */
273 HWTEST_F(InnerModelTest, inner_model_set_tensor_value_001, TestSize.Level1)
274 {
275 SetTensors();
276
277 uint32_t index = 3;
278 const int8_t activation = 0;
279 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.SetTensorValue(index,
280 static_cast<const void *>(&activation), sizeof(int8_t)));
281 }
282
283 /**
284 * @tc.name: inner_model_set_tensor_value_002
285 * @tc.desc: Verify the index out of bounds of the set_tensor_value function
286 * @tc.type: FUNC
287 */
288 HWTEST_F(InnerModelTest, inner_model_set_tensor_value_002, TestSize.Level1)
289 {
290 SetTensors();
291
292 uint32_t index = 6;
293 const int8_t activation = 0;
294 EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_innerModelTest.SetTensorValue(index,
295 static_cast<const void *>(&activation), sizeof(int8_t)));
296 }
297
298 /**
299 * @tc.name: inner_model_set_tensor_value_003
300 * @tc.desc: Verify the buffer value is nullptr of the set_tensor_value function
301 * @tc.type: FUNC
302 */
303 HWTEST_F(InnerModelTest, inner_model_set_tensor_value_003, TestSize.Level1)
304 {
305 SetTensors();
306
307 uint32_t index = 3;
308 const int8_t activation = 0;
309 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.SetTensorValue(index,
310 nullptr, sizeof(activation)));
311 }
312
313 /**
314 * @tc.name: inner_model_set_tensor_value_004
315 * @tc.desc: Verify the length invalid of the set_tensor_value function
316 * @tc.type: FUNC
317 */
318 HWTEST_F(InnerModelTest, inner_model_set_tensor_value_004, TestSize.Level1)
319 {
320 SetTensors();
321
322 uint32_t index = 3;
323 const int8_t activation = 0;
324 EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_innerModelTest.SetTensorValue(index,
325 static_cast<const void *>(&activation), 0));
326 }
327
328 /**
329 * @tc.name: inner_model_set_tensor_value_005
330 * @tc.desc: Verify the after buildgraph of the set_tensor_value function
331 * @tc.type: FUNC
332 */
333 HWTEST_F(InnerModelTest, inner_model_set_tensor_value_005, TestSize.Level1)
334 {
335 uint32_t index = 3;
336 const int8_t activation = 0;
337
338 mindspore::lite::LiteGraph* liteGraph = new (std::nothrow) mindspore::lite::LiteGraph();
339 EXPECT_NE(nullptr, liteGraph);
340 SetLiteGraph(liteGraph);
341 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.BuildFromLiteGraph(liteGraph));
342
343 EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, m_innerModelTest.SetTensorValue(index,
344 static_cast<const void *>(&activation), sizeof(int8_t)));
345 }
346
347 /**
348 * @tc.name: inner_model_set_tensor_value_006
349 * @tc.desc: Verify the set value twice of the set_tensor_value function
350 * @tc.type: FUNC
351 */
352 HWTEST_F(InnerModelTest, inner_model_set_tensor_value_006, TestSize.Level1)
353 {
354 SetTensors();
355
356 uint32_t index = 3;
357 const int8_t activation = 0;
358 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.SetTensorValue(index,
359 static_cast<const void *>(&activation), sizeof(int8_t)));
360
361 EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_innerModelTest.SetTensorValue(index,
362 static_cast<const void *>(&activation), sizeof(int8_t)));
363 }
364
365 /**
366 * @tc.name: inner_model_set_tensor_value_007
367 * @tc.desc: Verify the tensor dynamicShape of the set_tensor_value function
368 * @tc.type: FUNC
369 */
370 HWTEST_F(InnerModelTest, inner_model_set_tensor_value_007, TestSize.Level1)
371 {
372 const int32_t dimInput[2] = {2, -1};
373 const OH_NN_Tensor& tensor = {OH_NN_FLOAT32, 2, dimInput, nullptr, OH_NN_TENSOR};
374
375 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.AddTensor(tensor));
376 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.AddTensor(tensor));
377 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.AddTensor(tensor));
378 const OH_NN_Tensor& tensorParam = {OH_NN_INT8, 0, nullptr, nullptr, OH_NN_ADD_ACTIVATIONTYPE};
379 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.AddTensor(tensorParam));
380
381 uint32_t index = 0;
382 float x[4] = {0, 1, 2, 3};
383 EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, m_innerModelTest.SetTensorValue(index,
384 x, sizeof(x)- 1));
385 }
386
387 /**
388 * @tc.name: inner_model_add_operation_001
389 * @tc.desc: Verify the success of the addoperation function
390 * @tc.type: FUNC
391 */
392 HWTEST_F(InnerModelTest, inner_model_add_operation_001, TestSize.Level1)
393 {
394 SetIndices();
395
396 SetTensors();
397
398 uint32_t index = 3;
399 const int8_t activation = 0;
400 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.SetTensorValue(index,
401 static_cast<const void *>(&activation), sizeof(int8_t)));
402
403 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.AddOperation(m_opType, m_params, m_inputs, m_outputs));
404 }
405
406 /**
407 * @tc.name: inner_model_add_operation_002
408 * @tc.desc: Verify the after buildgraph of the addtensor function
409 * @tc.type: FUNC
410 */
411 HWTEST_F(InnerModelTest, inner_model_add_operation_002, TestSize.Level1)
412 {
413 OH_NN_OperationType m_opType = OH_NN_OPS_ADD;
414 OH_NN_UInt32Array m_inputs;
415 OH_NN_UInt32Array m_outputs;
416 OH_NN_UInt32Array m_params;
417
418 mindspore::lite::LiteGraph* liteGraph = new (std::nothrow) mindspore::lite::LiteGraph();
419 EXPECT_NE(nullptr, liteGraph);
420 SetLiteGraph(liteGraph);
421 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.BuildFromLiteGraph(liteGraph));
422
423 EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, m_innerModelTest.AddOperation(m_opType, m_params, m_inputs,
424 m_outputs));
425 }
426
427 /**
428 * @tc.name: inner_model_add_operation_003
429 * @tc.desc: Verify the without set buffer of the addtensor function
430 * @tc.type: FUNC
431 */
432 HWTEST_F(InnerModelTest, inner_model_add_operation_003, TestSize.Level1)
433 {
434 SetIndices();
435 SetTensors();
436
437 EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_innerModelTest.AddOperation(m_opType, m_params, m_inputs, m_outputs));
438 }
439
440 /**
441 * @tc.name: inner_model_add_operation_004
442 * @tc.desc: Verify the output indices equal to input indices of the addtensor function
443 * @tc.type: FUNC
444 */
445 HWTEST_F(InnerModelTest, inner_model_add_operation_004, TestSize.Level1)
446 {
447 m_outputIndexs[0] = 0;
448
449 SetIndices();
450 SetTensors();
451
452 EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_innerModelTest.AddOperation(m_opType, m_params, m_inputs, m_outputs));
453 }
454
455 /**
456 * @tc.name: inner_model_add_operation_005
457 * @tc.desc: Verify the optype invalid of the addtensor function
458 * @tc.type: FUNC
459 */
460 HWTEST_F(InnerModelTest, inner_model_add_operation_005, TestSize.Level1)
461 {
462 m_opType = OH_NN_OperationType(99);
463
464 SetIndices();
465 SetTensors();
466
467 EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_innerModelTest.AddOperation(m_opType, m_params, m_inputs, m_outputs));
468 }
469
470 /**
471 * @tc.name: inner_model_add_operation_006
472 * @tc.desc: Verify the input indices out of bounds of the addoperation function
473 * @tc.type: FUNC
474 */
475 HWTEST_F(InnerModelTest, inner_model_add_operation_006, TestSize.Level1)
476 {
477 m_inputIndexs[1] = 6;
478
479 SetIndices();
480 SetTensors();
481
482 uint32_t index = 3;
483 const int8_t activation = 0;
484 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.SetTensorValue(index,
485 static_cast<const void *>(&activation), sizeof(int8_t)));
486 EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_innerModelTest.AddOperation(m_opType, m_params, m_inputs, m_outputs));
487 }
488
489 /**
490 * @tc.name: inner_model_add_operation_007
491 * @tc.desc: Verify the param indices out of bounds of the addoperation function
492 * @tc.type: FUNC
493 */
494 HWTEST_F(InnerModelTest, inner_model_add_operation_007, TestSize.Level1)
495 {
496 m_paramIndexs[0] = 6;
497
498 SetIndices();
499 SetTensors();
500
501 uint32_t index = 3;
502 const int8_t activation = 0;
503 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.SetTensorValue(index,
504 static_cast<const void *>(&activation), sizeof(int8_t)));
505 EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_innerModelTest.AddOperation(m_opType, m_params, m_inputs, m_outputs));
506 }
507
508 /**
509 * @tc.name: inner_model_add_operation_008
510 * @tc.desc: Verify the input indices size is 0 of the addoperation function
511 * @tc.type: FUNC
512 */
513 HWTEST_F(InnerModelTest, inner_model_add_operation_008, TestSize.Level1)
514 {
515 SetIndices();
516
517 m_inputs.size = 0;
518 m_inputs.data = nullptr;
519 SetTensors();
520
521 uint32_t index = 3;
522 const int8_t activation = 0;
523 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.SetTensorValue(index,
524 static_cast<const void *>(&activation), sizeof(int8_t)));
525
526 EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_innerModelTest.AddOperation(m_opType, m_params, m_inputs, m_outputs));
527 }
528
529 /**
530 * @tc.name: inner_model_add_operation_009
531 * @tc.desc: Verify the output indices size is 0 of the addoperation function
532 * @tc.type: FUNC
533 */
534 HWTEST_F(InnerModelTest, inner_model_add_operation_009, TestSize.Level1)
535 {
536 SetIndices();
537
538 m_outputs.size = 0;
539 m_outputs.data = nullptr;
540 SetTensors();
541
542 uint32_t index = 3;
543 const int8_t activation = 0;
544 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.SetTensorValue(index,
545 static_cast<const void *>(&activation), sizeof(int8_t)));
546
547 EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_innerModelTest.AddOperation(m_opType, m_params, m_inputs, m_outputs));
548 }
549
550 /**
551 * @tc.name: inner_model_add_operation_010
552 * @tc.desc: Verify the ops build failed of the addoperation function
553 * @tc.type: FUNC
554 */
555 HWTEST_F(InnerModelTest, inner_model_add_operation_010, TestSize.Level1)
556 {
557 SetIndices();
558
559 const int32_t dimInput1[2] = {2, 2};
560 const OH_NN_Tensor& tensor = {OH_NN_FLOAT32, 2, dimInput1, nullptr, OH_NN_TENSOR};
561 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.AddTensor(tensor));
562 const int32_t dimInput2[2] = {2, 2};
563 const OH_NN_Tensor& tensor1 = {OH_NN_FLOAT32, 2, dimInput2, nullptr, OH_NN_TENSOR};
564 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.AddTensor(tensor1));
565 const int32_t dimOutput[2] = {2, 2};
566 const OH_NN_Tensor& tensor2 = {OH_NN_FLOAT32, 2, dimOutput, nullptr, OH_NN_TENSOR};
567 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.AddTensor(tensor2));
568 const OH_NN_Tensor& tensor3 = {OH_NN_INT8, 0, nullptr, nullptr, OH_NN_DIV_ACTIVATIONTYPE};
569 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.AddTensor(tensor3));
570
571 uint32_t index = 3;
572 const int8_t activation = 0;
573 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.SetTensorValue(index,
574 static_cast<const void *>(&activation), sizeof(int8_t)));
575
576 EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_innerModelTest.AddOperation(m_opType, m_params, m_inputs, m_outputs));
577 }
578
579 /**
580 * @tc.name: inner_model_specify_inputs_and_outputs_001
581 * @tc.desc: Verify the success of the specify_inputs_and_outputs function
582 * @tc.type: FUNC
583 */
584 HWTEST_F(InnerModelTest, inner_model_specify_inputs_and_outputs_001, TestSize.Level1)
585 {
586 SetIndices();
587 SetTensors();
588
589 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.SpecifyInputsAndOutputs(m_inputs, m_outputs));
590
591 std::vector<std::shared_ptr<NNTensor>> inTensors = m_innerModelTest.GetInputTensors();
592 EXPECT_EQ(inTensors.size(), m_inputs.size);
593 std::vector<std::shared_ptr<NNTensor>> outTensors = m_innerModelTest.GetOutputTensors();
594 EXPECT_EQ(outTensors.size(), m_outputs.size);
595 }
596
597 /**
598 * @tc.name: inner_model_specify_inputs_and_outputs_002
599 * @tc.desc: Verify the after buildgraph of the specify_inputs_and_outputs function
600 * @tc.type: FUNC
601 */
602 HWTEST_F(InnerModelTest, inner_model_specify_inputs_and_outputs_002, TestSize.Level1)
603 {
604 OH_NN_UInt32Array inputs;
605 OH_NN_UInt32Array outputs;
606 inputs.data = m_inputIndexs;
607 inputs.size = sizeof(m_inputIndexs) / sizeof(uint32_t);
608 outputs.data = nullptr;
609 outputs.size = sizeof(m_outputIndexs) / sizeof(uint32_t);
610
611 mindspore::lite::LiteGraph* liteGraph = new (std::nothrow) mindspore::lite::LiteGraph();
612 EXPECT_NE(nullptr, liteGraph);
613 SetLiteGraph(liteGraph);
614 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.BuildFromLiteGraph(liteGraph));
615
616 EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, m_innerModelTest.SpecifyInputsAndOutputs(inputs, outputs));
617 }
618
619 /**
620 * @tc.name: inner_model_specify_inputs_and_outputs_003
621 * @tc.desc: Verify the output indices is nullptr but length not 0 of the specify_inputs_and_outputs function
622 * @tc.type: FUNC
623 */
624 HWTEST_F(InnerModelTest, inner_model_specify_inputs_and_outputs_003, TestSize.Level1)
625 {
626 SetIndices();
627
628 m_outputs.data = nullptr;
629 SetTensors();
630
631 EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_innerModelTest.SpecifyInputsAndOutputs(m_inputs, m_outputs));
632 }
633
634 /**
635 * @tc.name: inner_model_specify_inputs_and_outputs_004
636 * @tc.desc: Verify the specift twice of the specify_inputs_and_outputs function
637 * @tc.type: FUNC
638 */
639 HWTEST_F(InnerModelTest, inner_model_specify_inputs_and_outputs_004, TestSize.Level1)
640 {
641 SetIndices();
642 SetTensors();
643
644 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.SpecifyInputsAndOutputs(m_inputs, m_outputs));
645
646 EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, m_innerModelTest.SpecifyInputsAndOutputs(m_inputs, m_outputs));
647 }
648
649 /**
650 * @tc.name: inner_model_build_001
651 * @tc.desc: Verify the success of the build function
652 * @tc.type: FUNC
653 */
654 HWTEST_F(InnerModelTest, inner_model_build_001, TestSize.Level1)
655 {
656 SetIndices();
657 SetTensors();
658
659 uint32_t index = 3;
660 const int8_t activation = 0;
661 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.SetTensorValue(index,
662 static_cast<const void *>(&activation), sizeof(int8_t)));
663
664 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.AddOperation(m_opType, m_params, m_inputs, m_outputs));
665 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.SpecifyInputsAndOutputs(m_inputs, m_outputs));
666 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.Build());
667 EXPECT_EQ(true, m_innerModelTest.IsBuild());
668 }
669
670 /**
671 * @tc.name: inner_model_build_002
672 * @tc.desc: Verify the build twice forbidden of the build function
673 * @tc.type: FUNC
674 */
675 HWTEST_F(InnerModelTest, inner_model_build_002, TestSize.Level1)
676 {
677 SetIndices();
678 SetTensors();
679
680 uint32_t index = 3;
681 const int8_t activation = 0;
682 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.SetTensorValue(index,
683 static_cast<const void *>(&activation), sizeof(int8_t)));
684
685 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.AddOperation(m_opType, m_params, m_inputs, m_outputs));
686 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.SpecifyInputsAndOutputs(m_inputs, m_outputs));
687 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.Build());
688 EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, m_innerModelTest.Build());
689 }
690
691 /**
692 * @tc.name: inner_model_build_003
693 * @tc.desc: Verify the params not match optype of the build function
694 * @tc.type: FUNC
695 */
696 HWTEST_F(InnerModelTest, inner_model_build_003, TestSize.Level1)
697 {
698 OH_NN_OperationType m_opType = OH_NN_OPS_DIV;
699
700 SetIndices();
701
702 const int dim[2] = {2, 2};
703 const OH_NN_Tensor& tensor = {OH_NN_FLOAT32, 2, dim, nullptr, OH_NN_TENSOR};
704 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.AddTensor(tensor));
705 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.AddTensor(tensor));
706 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.AddTensor(tensor));
707 const OH_NN_Tensor& tensorParam = {OH_NN_INT8, 0, nullptr, nullptr, OH_NN_DIV_ACTIVATIONTYPE};
708 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.AddTensor(tensorParam));
709
710 uint32_t index = 3;
711 const int8_t activation = 0;
712 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.SetTensorValue(index,
713 static_cast<const void *>(&activation), sizeof(int8_t)));
714 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.SpecifyInputsAndOutputs(m_inputs, m_outputs));
715 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.AddOperation(m_opType, m_params, m_inputs, m_outputs));
716 EXPECT_EQ(OH_NN_FAILED, m_innerModelTest.Build());
717 }
718
719 /**
720 * @tc.name: inner_model_build_004
721 * @tc.desc: Verify the success of the build function
722 * @tc.type: FUNC
723 */
724 HWTEST_F(InnerModelTest, inner_model_build_004, TestSize.Level1)
725 {
726 SetIndices();
727 SetTensors();
728
729 uint32_t index = 3;
730 const int8_t activation = 0;
731 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.SetTensorValue(index,
732 static_cast<const void *>(&activation), sizeof(int8_t)));
733
734 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.AddOperation(m_opType, m_params, m_inputs, m_outputs));
735 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.SpecifyInputsAndOutputs(m_inputs, m_outputs));
736 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.Build());
737 }
738
739 /**
740 * @tc.name: inner_model_get_supported_operation_001
741 * @tc.desc: Verify the success of the get_supported_operation function
742 * @tc.type: FUNC
743 */
744 HWTEST_F(InnerModelTest, inner_model_get_supported_operation_001, TestSize.Level1)
745 {
746 const bool *isSupported = nullptr;
747 uint32_t opCount = 1;
748
749 SetIndices();
750 SetTensors();
751
752 uint32_t index = 3;
753 const int8_t activation = 0;
754 size_t deviceID = 10;
755 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.SetTensorValue(index,
756 static_cast<const void *>(&activation), sizeof(int8_t)));
757
758 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.AddOperation(m_opType, m_params, m_inputs, m_outputs));
759 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.SpecifyInputsAndOutputs(m_inputs, m_outputs));
760 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.Build());
761 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.GetSupportedOperations(deviceID, &isSupported, opCount));
762 }
763
764 /**
765 * @tc.name: inner_model_get_supported_operation_002
766 * @tc.desc: Verify the mock hdi device result of the get_supported_operation function
767 * @tc.type: FUNC
768 */
769 HWTEST_F(InnerModelTest, inner_model_get_supported_operation_002, TestSize.Level1)
770 {
771 size_t deviceID = 10;
772 const bool *isSupported = nullptr;
773 uint32_t opCount = 1;
774
775 mindspore::lite::LiteGraph* liteGraph = new (std::nothrow) mindspore::lite::LiteGraph();
776 EXPECT_NE(nullptr, liteGraph);
777 SetLiteGraph(liteGraph);
778 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.BuildFromLiteGraph(liteGraph));
779
780 EXPECT_EQ(OH_NN_UNAVALIDABLE_DEVICE, m_innerModelTest.GetSupportedOperations(deviceID, &isSupported, opCount));
781 }
782
783 /**
784 * @tc.name: inner_model_get_supported_operation_003
785 * @tc.desc: Verify the mock device manager of the get_supported_operation function
786 * @tc.type: FUNC
787 */
788 HWTEST_F(InnerModelTest, inner_model_get_supported_operation_003, TestSize.Level1)
789 {
790 const bool *isSupported = nullptr;
791 uint32_t opCount = 1;
792
793 SetIndices();
794 SetTensors();
795
796 uint32_t index = 3;
797 const int8_t activation = 0;
798 size_t deviceID = 0;
799 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.SetTensorValue(index,
800 static_cast<const void *>(&activation), sizeof(int8_t)));
801
802 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.AddOperation(m_opType, m_params, m_inputs, m_outputs));
803 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.SpecifyInputsAndOutputs(m_inputs, m_outputs));
804 EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.Build());
805 EXPECT_EQ(OH_NN_FAILED, m_innerModelTest.GetSupportedOperations(deviceID, &isSupported, opCount));
806
807 std::shared_ptr<mindspore::lite::LiteGraph> liteGraph = m_innerModelTest.GetLiteGraphs();
808 EXPECT_EQ(liteGraph->name_, "NNR_Model");
809 }
810
811 /**
812 * @tc.name: inner_model_get_supported_operation_004
813 * @tc.desc: Verify the before build of the get_supported_operation function
814 * @tc.type: FUNC
815 */
816 HWTEST_F(InnerModelTest, inner_model_get_supported_operation_004, TestSize.Level1)
817 {
818 size_t deviceID = 10;
819 const bool *isSupported = nullptr;
820 uint32_t opCount = 1;
821
822 EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, m_innerModelTest.GetSupportedOperations(deviceID, &isSupported, opCount));
823 }
824 } // namespace UnitTest
825 } // namespace NNRT
826