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 "executor_test.h"
17
18 #include "common/scoped_trace.h"
19 #include "frameworks/native/compilation.h"
20 #include "frameworks/native/inner_model.h"
21 #include "test/unittest/common/mock_idevice.h"
22
23 using namespace OHOS::NeuralNetworkRuntime;
24 using namespace OHOS::NeuralNetworkRuntime::Ops;
25 using namespace OHOS::HDI::Nnrt::V1_0;
26 using namespace OHOS::HiviewDFX;
27
28 namespace OHOS {
29 namespace NeuralNetworkRuntime {
30 namespace UnitTest {
31 using NNTensorPtr = std::shared_ptr<NNTensor>;
32
BuildLiteGraph(const std::vector<int32_t> dim,const std::vector<int32_t> dimOut)33 MSLITE::LiteGraph* ExecutorTest::BuildLiteGraph(const std::vector<int32_t> dim, const std::vector<int32_t> dimOut)
34 {
35 MSLITE::LiteGraph* liteGraph = new (std::nothrow) MSLITE::LiteGraph();
36 if (liteGraph == nullptr) {
37 LOGE("liteGraph build failed");
38 return nullptr;
39 }
40 liteGraph->name_ = "testGraph";
41 liteGraph->input_indices_.emplace_back(0);
42 liteGraph->output_indices_.emplace_back(1);
43 const std::vector<MSLITE::QuantParam> quant_params;
44
45 for (size_t indexInput = 0; indexInput < liteGraph->input_indices_.size(); ++indexInput) {
46 const std::vector<uint8_t> data(36, 1);
47 void* liteGraphTensor1 = MSLITE::MindIR_Tensor_Create(liteGraph->name_,
48 MSLITE::DATA_TYPE_FLOAT32, dim, MSLITE::FORMAT_NCHW, data, quant_params);
49 liteGraph->all_tensors_.emplace_back(liteGraphTensor1);
50 }
51
52 for (size_t indexOutput = 0; indexOutput < liteGraph->output_indices_.size(); ++indexOutput) {
53 const std::vector<uint8_t> dataOut(36, 1);
54 void* liteGraphTensor2 = MSLITE::MindIR_Tensor_Create(liteGraph->name_,
55 MSLITE::DATA_TYPE_FLOAT32, dimOut, MSLITE::FORMAT_NCHW, dataOut, quant_params);
56 liteGraph->all_tensors_.emplace_back(liteGraphTensor2);
57 }
58
59 return liteGraph;
60 }
61
SetTensor(OH_NN_DataType dataType,uint32_t dimensionCount,const int32_t * dimensions,const OH_NN_QuantParam * quantParam,OH_NN_TensorType type)62 OH_NN_Tensor ExecutorTest::SetTensor(OH_NN_DataType dataType, uint32_t dimensionCount, const int32_t *dimensions,
63 const OH_NN_QuantParam *quantParam, OH_NN_TensorType type)
64 {
65 OH_NN_Tensor tensor;
66 tensor.dataType = dataType;
67 tensor.dimensionCount = dimensionCount;
68 tensor.dimensions = dimensions;
69 tensor.quantParam = quantParam;
70 tensor.type = type;
71
72 return tensor;
73 }
74
SetMermory(OH_NN_Memory ** & memory)75 void ExecutorTest::SetMermory(OH_NN_Memory** &memory)
76 {
77 float dataArry[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
78 void* const data = dataArry;
79 OH_NN_Memory memoryPtr = {data, 9 * sizeof(float)};
80 OH_NN_Memory* ptr = &memoryPtr;
81 memory = &ptr;
82 }
83
84 /*
85 * @tc.name: executor_set_input_001
86 * @tc.desc: Verify that the SetInput function returns a successful message.
87 * @tc.type: FUNC
88 */
89 HWTEST_F(ExecutorTest, executor_set_input_001, testing::ext::TestSize.Level0)
90 {
91 const MSLITE::LiteGraph* liteGraphTest = BuildLiteGraph(m_dim, m_dimOut);
92 InnerModel innerModel;
93 innerModel.BuildFromLiteGraph(liteGraphTest);
94 Compilation compilation(&innerModel);
95 Executor executorTest(&compilation);
96
97 OH_NN_Tensor tensor = SetTensor(OH_NN_FLOAT32, m_dimensionCount, m_dimArry, nullptr, OH_NN_TENSOR);
98 void* buffer = m_dataArry;
99 size_t length = 9 * sizeof(float);
100
101 OH_NN_ReturnCode ret = executorTest.SetInput(m_index, tensor, buffer, length);
102 EXPECT_EQ(OH_NN_SUCCESS, ret);
103 }
104
105 /*
106 * @tc.name: executor_set_input_002
107 * @tc.desc: Verify that the SetInput function returns a failed message with out-of-range index.
108 * @tc.type: FUNC
109 */
110 HWTEST_F(ExecutorTest, executor_set_input_002, testing::ext::TestSize.Level0)
111 {
112 const MSLITE::LiteGraph* liteGraphTest = BuildLiteGraph(m_dim, m_dimOut);
113 InnerModel innerModel;
114 innerModel.BuildFromLiteGraph(liteGraphTest);
115 Compilation compilation(&innerModel);
116 Executor executorTest(&compilation);
117
118 m_index = 6;
119 OH_NN_Tensor tensor = SetTensor(OH_NN_FLOAT32, m_dimensionCount, m_dimArry, nullptr, OH_NN_TENSOR);
120 size_t length = 9 * sizeof(float);
121 void* buffer = m_dataArry;
122
123 OH_NN_ReturnCode ret = executorTest.SetInput(m_index, tensor, buffer, length);
124 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
125 }
126
127 /*
128 * @tc.name: executor_set_input_003
129 * @tc.desc: Verify that the SetInput function returns a failed message with dynamic shape.
130 * @tc.type: FUNC
131 */
132 HWTEST_F(ExecutorTest, executor_set_input_003, testing::ext::TestSize.Level0)
133 {
134 const MSLITE::LiteGraph* liteGraphTest = BuildLiteGraph(m_dim, m_dimOut);
135 InnerModel innerModel;
136 innerModel.BuildFromLiteGraph(liteGraphTest);
137 Compilation compilation(&innerModel);
138 Executor executorTest(&compilation);
139
140 const int dim = -1;
141 m_dimensionCount = 1;
142 OH_NN_Tensor tensor = SetTensor(OH_NN_FLOAT32, m_dimensionCount, &dim, nullptr, OH_NN_TENSOR);
143 size_t length = 1 * sizeof(float);
144 float data = 0;
145 void* buffer = &data;
146
147 OH_NN_ReturnCode ret = executorTest.SetInput(m_index, tensor, buffer, length);
148 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
149 }
150
151 /*
152 * @tc.name: executor_set_input_004
153 * @tc.desc: Verify that the SetInput function returns a failed message with invalid tensor's dataType.
154 * @tc.type: FUNC
155 */
156 HWTEST_F(ExecutorTest, executor_set_input_004, testing::ext::TestSize.Level0)
157 {
158 const MSLITE::LiteGraph* liteGraphTest = BuildLiteGraph(m_dim, m_dimOut);
159 InnerModel innerModel;
160 innerModel.BuildFromLiteGraph(liteGraphTest);
161 Compilation compilation(&innerModel);
162 Executor executorTest(&compilation);
163
164 OH_NN_Tensor tensor = SetTensor(OH_NN_INT64, m_dimensionCount, m_dimArry, nullptr, OH_NN_TENSOR);
165 void* buffer = m_dataArry;
166 size_t length = 9 * sizeof(float);
167
168 OH_NN_ReturnCode ret = executorTest.SetInput(m_index, tensor, buffer, length);
169 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
170 }
171
172 /*
173 * @tc.name: executor_set_input_005
174 * @tc.desc: Verify that the SetInput function returns a failed message with invalid length.
175 * @tc.type: FUNC
176 */
177 HWTEST_F(ExecutorTest, executor_set_input_005, testing::ext::TestSize.Level0)
178 {
179 const MSLITE::LiteGraph* liteGraphTest = BuildLiteGraph(m_dim, m_dimOut);
180 InnerModel innerModel;
181 innerModel.BuildFromLiteGraph(liteGraphTest);
182 Compilation compilation(&innerModel);
183 Executor executorTest(&compilation);
184
185
186 OH_NN_Tensor tensor = SetTensor(OH_NN_FLOAT32, m_dimensionCount, m_dimArry, nullptr, OH_NN_TENSOR);
187 size_t length = 1 * sizeof(float);
188 void* buffer = m_dataArry;
189
190 OH_NN_ReturnCode ret = executorTest.SetInput(m_index, tensor, buffer, length);
191 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
192 }
193
194 /*
195 * @tc.name: executor_set_input_006
196 * @tc.desc: Verify that the SetInput function returns a failed message with allocating buffer is unsuccessfully.
197 * @tc.type: FUNC
198 */
199 HWTEST_F(ExecutorTest, executor_set_input_006, testing::ext::TestSize.Level0)
200 {
201 HDI::Nnrt::V1_0::MockIPreparedModel::m_ExpectRetCode = OH_NN_INVALID_PARAMETER;
202 const MSLITE::LiteGraph* liteGraphTest = BuildLiteGraph(m_dim, m_dimOut);
203 InnerModel innerModel;
204 innerModel.BuildFromLiteGraph(liteGraphTest);
205 Compilation compilation(&innerModel);
206 Executor executorTest(&compilation);
207
208 OH_NN_Tensor tensor = SetTensor(OH_NN_FLOAT32, m_dimensionCount, m_dimArry, nullptr, OH_NN_TENSOR);
209 void* buffer = m_dataArry;
210 size_t length = 9 * sizeof(float);
211
212 OH_NN_ReturnCode ret = executorTest.SetInput(m_index, tensor, buffer, length);
213 EXPECT_EQ(OH_NN_MEMORY_ERROR, ret);
214 }
215
216 /*
217 * @tc.name: executor_set_input_007
218 * @tc.desc: Verify that the SetInput function returns a failed message with empty buffer.
219 * @tc.type: FUNC
220 */
221 HWTEST_F(ExecutorTest, executor_set_input_007, testing::ext::TestSize.Level0)
222 {
223 const MSLITE::LiteGraph* liteGraphTest = BuildLiteGraph(m_dim, m_dimOut);
224 InnerModel innerModel;
225 innerModel.BuildFromLiteGraph(liteGraphTest);
226 Compilation compilation(&innerModel);
227 Executor executorTest(&compilation);
228
229 OH_NN_Tensor tensor = SetTensor(OH_NN_FLOAT32, m_dimensionCount, m_dimArry, nullptr, OH_NN_TENSOR);
230 void* buffer = nullptr;
231 size_t length = 9 * sizeof(float);
232
233 OH_NN_ReturnCode ret = executorTest.SetInput(m_index, tensor, buffer, length);
234 EXPECT_EQ(OH_NN_MEMORY_ERROR, ret);
235 }
236
237 /*
238 * @tc.name: executor_set_input_008
239 * @tc.desc: Verify that the SetInput function returns a successful message with dataLength <= curBufferLength.
240 * @tc.type: FUNC
241 */
242 HWTEST_F(ExecutorTest, executor_set_input_008, testing::ext::TestSize.Level0)
243 {
244 const MSLITE::LiteGraph* liteGraphTest = BuildLiteGraph(m_dim, m_dimOut);
245 InnerModel innerModel;
246 innerModel.BuildFromLiteGraph(liteGraphTest);
247 Compilation compilation(&innerModel);
248 Executor executorTest(&compilation);
249
250 OH_NN_Tensor tensor = SetTensor(OH_NN_FLOAT32, m_dimensionCount, m_dimArry, nullptr, OH_NN_TENSOR);
251 float dataArry[15] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
252 void* buffer = dataArry;
253 size_t length = 9 * sizeof(float);
254
255 EXPECT_EQ(OH_NN_SUCCESS, executorTest.SetInput(m_index, tensor, buffer, length));
256
257 float expectArry[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
258 void* expectBuffer = expectArry;
259 OH_NN_ReturnCode ret = executorTest.SetInput(m_index, tensor, expectBuffer, length);
260 EXPECT_EQ(OH_NN_SUCCESS, ret);
261 }
262
263 /*
264 * @tc.name: executor_set_input_009
265 * @tc.desc: Verify that the SetInput function returns a failed message with length less than dataLength.
266 * @tc.type: FUNC
267 */
268 HWTEST_F(ExecutorTest, executor_set_input_009, testing::ext::TestSize.Level0)
269 {
270 const MSLITE::LiteGraph* liteGraphTest = BuildLiteGraph(m_dim, m_dimOut);
271 InnerModel innerModel;
272 innerModel.BuildFromLiteGraph(liteGraphTest);
273 Compilation compilation(&innerModel);
274 Executor executorTest(&compilation);
275
276 OH_NN_Tensor tensor = SetTensor(OH_NN_FLOAT32, m_dimensionCount, m_dimArry, nullptr, OH_NN_TENSOR);
277 void* const data = m_dataArry;
278 OH_NN_Memory memory = {data, 9 * sizeof(float)};
279
280 EXPECT_EQ(OH_NN_SUCCESS, executorTest.SetInputFromMemory(m_index, tensor, memory));
281
282 float expectData = 0;
283 void* buffer = &expectData;
284 size_t length = 1 * sizeof(float);
285
286 OH_NN_ReturnCode ret = executorTest.SetInput(m_index, tensor, buffer, length);
287 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
288 }
289
290 /*
291 * @tc.name: executor_set_input_010
292 * @tc.desc: Verify that the SetInput function returns a failed message with BuildFromOHNNTensor unsuccessfully.
293 * @tc.type: FUNC
294 */
295 HWTEST_F(ExecutorTest, executor_set_input_010, testing::ext::TestSize.Level0)
296 {
297 const MSLITE::LiteGraph* liteGraphTest = BuildLiteGraph(m_dim, m_dimOut);
298 InnerModel innerModel;
299 innerModel.BuildFromLiteGraph(liteGraphTest);
300 Compilation compilation(&innerModel);
301 Executor executorTest(&compilation);
302
303 m_dimensionCount = 0;
304 OH_NN_Tensor tensor = SetTensor(OH_NN_UNKNOWN, m_dimensionCount, m_dimArry, nullptr, OH_NN_TENSOR);
305 void* buffer = m_dataArry;
306 size_t length = 9 * sizeof(float);
307
308 OH_NN_ReturnCode ret = executorTest.SetInput(m_index, tensor, buffer, length);
309 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
310 }
311
312 /*
313 * @tc.name: executor_set_input_011
314 * @tc.desc: Verify that the SetInput function returns a successful message with dataLength <= curBufferLength.
315 * @tc.type: FUNC
316 */
317 HWTEST_F(ExecutorTest, executor_set_input_011, testing::ext::TestSize.Level0)
318 {
319 const std::vector<int32_t> expectDim = {3, -1};
320 const MSLITE::LiteGraph* liteGraphTest = BuildLiteGraph(expectDim, m_dimOut);
321 InnerModel innerModel;
322 innerModel.BuildFromLiteGraph(liteGraphTest);
323 Compilation compilation(&innerModel);
324 Executor executorTest(&compilation);
325
326 OH_NN_Tensor tensor = SetTensor(OH_NN_FLOAT32, m_dimensionCount, m_dimArry, nullptr, OH_NN_TENSOR);
327 void* buffer = m_dataArry;
328 size_t length = 9 * sizeof(float);
329 EXPECT_EQ(OH_NN_SUCCESS, executorTest.SetInput(m_index, tensor, buffer, length));
330
331 const int32_t testDim[2] = {3, 5};
332 OH_NN_Tensor expectTensor = SetTensor(OH_NN_FLOAT32, m_dimensionCount, testDim, nullptr, OH_NN_TENSOR);
333 size_t expectLength = 15 * sizeof(float);
334 float expectArry[15] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
335 void* expectBuffer = expectArry;
336 OH_NN_ReturnCode ret = executorTest.SetInput(m_index, expectTensor, expectBuffer, expectLength);
337 EXPECT_EQ(OH_NN_SUCCESS, ret);
338 }
339
340 /*
341 * @tc.name: executor_set_input_from_memory_001
342 * @tc.desc: Verify that the SetInputFromMemory function returns a successful message.
343 * @tc.type: FUNC
344 */
345 HWTEST_F(ExecutorTest, executor_set_input_from_memory_001, testing::ext::TestSize.Level0)
346 {
347 const MSLITE::LiteGraph* liteGraphTest = BuildLiteGraph(m_dim, m_dimOut);
348 InnerModel innerModel;
349 innerModel.BuildFromLiteGraph(liteGraphTest);
350 Compilation compilation(&innerModel);
351 Executor executorTest(&compilation);
352
353 OH_NN_Tensor tensor = SetTensor(OH_NN_FLOAT32, m_dimensionCount, m_dimArry, nullptr, OH_NN_TENSOR);
354 void* const data = m_dataArry;
355 OH_NN_Memory memory = {data, 9 * sizeof(float)};
356
357 OH_NN_ReturnCode ret = executorTest.SetInputFromMemory(m_index, tensor, memory);
358 EXPECT_EQ(OH_NN_SUCCESS, ret);
359 }
360
361 /*
362 * @tc.name: executor_set_input_from_memory_002
363 * @tc.desc: Verify that the SetInputFromMemory function returns a failed message with out-of-range index.
364 * @tc.type: FUNC
365 */
366 HWTEST_F(ExecutorTest, executor_set_input_from_memory_002, testing::ext::TestSize.Level0)
367 {
368 const MSLITE::LiteGraph* liteGraphTest = BuildLiteGraph(m_dim, m_dimOut);
369 InnerModel innerModel;
370 innerModel.BuildFromLiteGraph(liteGraphTest);
371 Compilation compilation(&innerModel);
372 Executor executorTest(&compilation);
373
374 m_index = 6;
375 OH_NN_Tensor tensor = SetTensor(OH_NN_FLOAT32, m_dimensionCount, m_dimArry, nullptr, OH_NN_TENSOR);
376 void* const data = m_dataArry;
377 OH_NN_Memory memory = {data, 9 * sizeof(float)};
378
379 OH_NN_ReturnCode ret = executorTest.SetInputFromMemory(m_index, tensor, memory);
380 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
381 }
382
383 /*
384 * @tc.name: executor_set_input_from_memory_003
385 * @tc.desc: Verify that the SetInputFromMemory function returns a failed message with dynamic shape.
386 * @tc.type: FUNC
387 */
388 HWTEST_F(ExecutorTest, executor_set_input_from_memory_003, testing::ext::TestSize.Level0)
389 {
390 const MSLITE::LiteGraph* liteGraphTest = BuildLiteGraph(m_dim, m_dimOut);
391 InnerModel innerModel;
392 innerModel.BuildFromLiteGraph(liteGraphTest);
393 Compilation compilation(&innerModel);
394 Executor executorTest(&compilation);
395
396 const int dim = -1;
397 OH_NN_Tensor tensor;
398 tensor.dataType = OH_NN_FLOAT32;
399 tensor.dimensionCount = 1;
400 tensor.dimensions = &dim;
401 tensor.quantParam = nullptr;
402 tensor.type = OH_NN_TENSOR;
403 float value = 0;
404 void* const data = &value;
405 OH_NN_Memory memory = {data, 1 * sizeof(float)};
406
407 OH_NN_ReturnCode ret = executorTest.SetInputFromMemory(m_index, tensor, memory);
408 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
409 }
410
411 /*
412 * @tc.name: executor_set_input_from_memory_004
413 * @tc.desc: Verify that the SetInputFromMemory function returns a failed message with invalid tensor's dataType.
414 * @tc.type: FUNC
415 */
416 HWTEST_F(ExecutorTest, executor_set_input_from_memory_004, testing::ext::TestSize.Level0)
417 {
418 const MSLITE::LiteGraph* liteGraphTest = BuildLiteGraph(m_dim, m_dimOut);
419 InnerModel innerModel;
420 innerModel.BuildFromLiteGraph(liteGraphTest);
421 Compilation compilation(&innerModel);
422 Executor executorTest(&compilation);
423
424 OH_NN_Tensor tensor = SetTensor(OH_NN_INT64, m_dimensionCount, m_dimArry, nullptr, OH_NN_TENSOR);
425 void* const data = m_dataArry;
426 OH_NN_Memory memory = {data, 9 * sizeof(float)};
427
428 OH_NN_ReturnCode ret = executorTest.SetInputFromMemory(m_index, tensor, memory);
429 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
430 }
431
432 /*
433 * @tc.name: executor_set_input_from_memory_005
434 * @tc.desc: Verify that the SetInput function returns a failed message with invalid memory.length.
435 * @tc.type: FUNC
436 */
437 HWTEST_F(ExecutorTest, executor_set_input_from_memory_005, testing::ext::TestSize.Level0)
438 {
439 const MSLITE::LiteGraph* liteGraphTest = BuildLiteGraph(m_dim, m_dimOut);
440 InnerModel innerModel;
441 innerModel.BuildFromLiteGraph(liteGraphTest);
442 Compilation compilation(&innerModel);
443 Executor executorTest(&compilation);
444
445 OH_NN_Tensor tensor = SetTensor(OH_NN_FLOAT32, m_dimensionCount, m_dimArry, nullptr, OH_NN_TENSOR);
446 void* const data = m_dataArry;
447 OH_NN_Memory memory = {data, 1 * sizeof(float)};
448
449 OH_NN_ReturnCode ret = executorTest.SetInputFromMemory(m_index, tensor, memory);
450 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
451 }
452
453 /*
454 * @tc.name: executor_set_output_001
455 * @tc.desc: Verify that the SetOutput function returns a successful message.
456 * @tc.type: FUNC
457 */
458 HWTEST_F(ExecutorTest, executor_set_output_001, testing::ext::TestSize.Level0)
459 {
460 const MSLITE::LiteGraph* liteGraphTest = BuildLiteGraph(m_dim, m_dimOut);
461 InnerModel innerModel;
462 innerModel.BuildFromLiteGraph(liteGraphTest);
463 Compilation compilation(&innerModel);
464 Executor executorTest(&compilation);
465
466 size_t length = 9 * sizeof(float);
467 void* buffer = m_dataArry;
468
469 OH_NN_ReturnCode ret = executorTest.SetOutput(m_index, buffer, length);
470 EXPECT_EQ(OH_NN_SUCCESS, ret);
471 }
472
473 /*
474 * @tc.name: executor_set_output_002
475 * @tc.desc: Verify that the SetOutput function returns a failed message with out-of-range index.
476 * @tc.type: FUNC
477 */
478 HWTEST_F(ExecutorTest, executor_set_output_002, testing::ext::TestSize.Level0)
479 {
480 InnerModel innerModel;
481 Compilation compilation(&innerModel);
482 Executor executorTest(&compilation);
483
484 m_index = 6;
485 size_t length = 9 * sizeof(float);
486 void* buffer = m_dataArry;
487
488 OH_NN_ReturnCode ret = executorTest.SetOutput(m_index, buffer, length);
489 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
490 }
491
492 /*
493 * @tc.name: executor_set_output_003
494 * @tc.desc: Verify that the SetOutput function returns a failed message with invalid length.
495 * @tc.type: FUNC
496 */
497 HWTEST_F(ExecutorTest, executor_set_output_003, testing::ext::TestSize.Level0)
498 {
499 InnerModel innerModel;
500 Compilation compilation(&innerModel);
501 Executor executorTest(&compilation);
502
503 size_t length = 2 * sizeof(float);
504 void* buffer = m_dataArry;
505
506 OH_NN_ReturnCode ret = executorTest.SetOutput(m_index, buffer, length);
507 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
508 }
509
510 /*
511 * @tc.name: executor_set_output_004
512 * @tc.desc: Verify that the SetOutput function returns a failed message with allocating buffer is failed.
513 * @tc.type: FUNC
514 */
515 HWTEST_F(ExecutorTest, executor_set_output_004, testing::ext::TestSize.Level0)
516 {
517 HDI::Nnrt::V1_0::MockIPreparedModel::m_ExpectRetCode = OH_NN_INVALID_PARAMETER;
518 const MSLITE::LiteGraph* liteGraphTest = BuildLiteGraph(m_dim, m_dimOut);
519 InnerModel innerModel;
520 innerModel.BuildFromLiteGraph(liteGraphTest);
521 Compilation compilation(&innerModel);
522 Executor executorTest(&compilation);
523
524 size_t length = 9 * sizeof(float);
525 void* buffer = m_dataArry;
526
527 OH_NN_ReturnCode ret = executorTest.SetOutput(m_index, buffer, length);
528 EXPECT_EQ(OH_NN_MEMORY_ERROR, ret);
529 }
530
531 /*
532 * @tc.name: executor_set_output_005
533 * @tc.desc: Verify that the SetOutput function returns a successful message.
534 * @tc.type: FUNC
535 */
536 HWTEST_F(ExecutorTest, executor_set_output_005, testing::ext::TestSize.Level0)
537 {
538 const MSLITE::LiteGraph* liteGraphTest = BuildLiteGraph(m_dim, m_dimOut);
539 InnerModel innerModel;
540 innerModel.BuildFromLiteGraph(liteGraphTest);
541 Compilation compilation(&innerModel);
542 Executor executorTest(&compilation);
543
544 void* const data = m_dataArry;
545 OH_NN_Memory memory = {data, 9 * sizeof(float)};
546 EXPECT_EQ(OH_NN_SUCCESS, executorTest.SetOutputFromMemory(m_index, memory));
547
548 size_t length = 1 * sizeof(float);
549 float expectData = 0;
550 void* buffer = &expectData;
551 OH_NN_ReturnCode ret = executorTest.SetOutput(m_index, buffer, length);
552 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
553 }
554
555 /*
556 * @tc.name: executor_set_output_006
557 * @tc.desc: Verify that the SetOutput function returns a successful message with length <= curBufferLength.
558 * @tc.type: FUNC
559 */
560 HWTEST_F(ExecutorTest, executor_set_output_006, testing::ext::TestSize.Level0)
561 {
562 const MSLITE::LiteGraph* liteGraphTest = BuildLiteGraph(m_dim, m_dimOut);
563 InnerModel innerModel;
564 innerModel.BuildFromLiteGraph(liteGraphTest);
565 Compilation compilation(&innerModel);
566 Executor executorTest(&compilation);
567
568 size_t length = 9 * sizeof(float);
569 void* buffer = m_dataArry;
570 EXPECT_EQ(OH_NN_SUCCESS, executorTest.SetOutput(m_index, buffer, length));
571
572 float expectDataArry[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
573 void* expectBuffer = expectDataArry;
574 OH_NN_ReturnCode ret = executorTest.SetOutput(m_index, expectBuffer, length);
575 EXPECT_EQ(OH_NN_SUCCESS, ret);
576 }
577
578 /*
579 * @tc.name: executor_set_output_007
580 * @tc.desc: Verify that the SetOutput function returns a successful message with length > curBufferLength.
581 * @tc.type: FUNC
582 */
583 HWTEST_F(ExecutorTest, executor_set_output_007, testing::ext::TestSize.Level0)
584 {
585 const MSLITE::LiteGraph* liteGraphTest = BuildLiteGraph(m_dim, m_dimOut);
586 InnerModel innerModel;
587 innerModel.BuildFromLiteGraph(liteGraphTest);
588 Compilation compilation(&innerModel);
589 Executor executorTest(&compilation);
590
591 size_t length = 9 * sizeof(float);
592 void* buffer = m_dataArry;
593 EXPECT_EQ(OH_NN_SUCCESS, executorTest.SetOutput(m_index, buffer, length));
594
595 size_t expectLength = 15 * sizeof(float);
596 float expectDataArry[15] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
597 void* expectBuffer = expectDataArry;
598 OH_NN_ReturnCode ret = executorTest.SetOutput(m_index, expectBuffer, expectLength);
599 EXPECT_EQ(OH_NN_SUCCESS, ret);
600 }
601
602 /*
603 * @tc.name: executor_set_output_from_memory_001
604 * @tc.desc: Verify that the SetOutputFromMemory function returns a successful message.
605 * @tc.type: FUNC
606 */
607 HWTEST_F(ExecutorTest, executor_set_output_from_memory_001, testing::ext::TestSize.Level0)
608 {
609 const MSLITE::LiteGraph* liteGraphTest = BuildLiteGraph(m_dim, m_dimOut);
610 InnerModel innerModel;
611 innerModel.BuildFromLiteGraph(liteGraphTest);
612 Compilation compilation(&innerModel);
613 Executor executorTest(&compilation);
614
615 void* const data = m_dataArry;
616 OH_NN_Memory memory = {data, 9 * sizeof(float)};
617
618 OH_NN_ReturnCode ret = executorTest.SetOutputFromMemory(m_index, memory);
619 EXPECT_EQ(OH_NN_SUCCESS, ret);
620 }
621
622 /*
623 * @tc.name: executor_set_output_from_memory_002
624 * @tc.desc: Verify that the SetOutputFromMemory function returns a failed message with out-of-range index.
625 * @tc.type: FUNC
626 */
627 HWTEST_F(ExecutorTest, executor_set_output_from_memory_002, testing::ext::TestSize.Level0)
628 {
629 const MSLITE::LiteGraph* liteGraphTest = BuildLiteGraph(m_dim, m_dimOut);
630 InnerModel innerModel;
631 innerModel.BuildFromLiteGraph(liteGraphTest);
632 Compilation compilation(&innerModel);
633 Executor executorTest(&compilation);
634
635 m_index = 6;
636 void* const data = m_dataArry;
637 OH_NN_Memory memory = {data, 9 * sizeof(float)};
638
639 OH_NN_ReturnCode ret = executorTest.SetOutputFromMemory(m_index, memory);
640 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
641 }
642
643 /*
644 * @tc.name: executor_set_output_from_memory_003
645 * @tc.desc: Verify that the SetOutputFromMemory function returns a failed message with invalid memory.length.
646 * @tc.type: FUNC
647 */
648 HWTEST_F(ExecutorTest, executor_set_output_from_memory_003, testing::ext::TestSize.Level0)
649 {
650 InnerModel innerModel;
651 Compilation compilation(&innerModel);
652 Executor executorTest(&compilation);
653
654 void* const data = m_dataArry;
655 OH_NN_Memory memory = {data, 0};
656
657 OH_NN_ReturnCode ret = executorTest.SetOutputFromMemory(m_index, memory);
658 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
659 }
660
661 /*
662 * @tc.name: executor_set_output_from_memory_004
663 * @tc.desc: Verify that the SetOutputFromMemory function returns a failed message with memory.length < dataLength.
664 * @tc.type: FUNC
665 */
666 HWTEST_F(ExecutorTest, executor_set_output_from_memory_004, testing::ext::TestSize.Level0)
667 {
668 const std::vector<int32_t> expectDim = {4, 4};
669 const MSLITE::LiteGraph* liteGraphTest = BuildLiteGraph(m_dim, expectDim);
670 InnerModel innerModel;
671 innerModel.BuildFromLiteGraph(liteGraphTest);
672 Compilation compilation(&innerModel);
673 Executor executorTest(&compilation);
674
675 void* const data = m_dataArry;
676 OH_NN_Memory memory = {data, 9 * sizeof(float)};
677
678 OH_NN_ReturnCode ret = executorTest.SetOutputFromMemory(m_index, memory);
679 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
680 }
681
682 /*
683 * @tc.name: executor_set_output_from_memory_005
684 * @tc.desc: Verify that the SetOutputFromMemory function returns a successful message.
685 * @tc.type: FUNC
686 */
687 HWTEST_F(ExecutorTest, executor_set_output_from_memory_005, testing::ext::TestSize.Level0)
688 {
689 const MSLITE::LiteGraph* liteGraphTest = BuildLiteGraph(m_dim, m_dimOut);
690 InnerModel innerModel;
691 innerModel.BuildFromLiteGraph(liteGraphTest);
692 Compilation compilation(&innerModel);
693 Executor executorTest(&compilation);
694
695 size_t length = 9 * sizeof(float);
696 void* buffer = m_dataArry;
697
698 EXPECT_EQ(OH_NN_SUCCESS, executorTest.SetOutput(m_index, buffer, length));
699 void* const data = m_dataArry;
700 OH_NN_Memory memory = {data, 9 * sizeof(float)};
701
702 OH_NN_ReturnCode ret = executorTest.SetOutputFromMemory(m_index, memory);
703 EXPECT_EQ(OH_NN_SUCCESS, ret);
704 }
705
706 /*
707 * @tc.name: executor_get_output_dimensions_001
708 * @tc.desc: Verify that the GetOutputShape function returns a successful message.
709 * @tc.type: FUNC
710 */
711 HWTEST_F(ExecutorTest, executor_get_output_dimensions_001, testing::ext::TestSize.Level0)
712 {
713 const MSLITE::LiteGraph* liteGraphTest = BuildLiteGraph(m_dim, m_dimOut);
714 InnerModel innerModel;
715 innerModel.BuildFromLiteGraph(liteGraphTest);
716 Compilation compilation(&innerModel);
717 Executor executorTest(&compilation);
718
719 OH_NN_Tensor tensor = SetTensor(OH_NN_FLOAT32, m_dimensionCount, m_dimArry, nullptr, OH_NN_TENSOR);
720 size_t length = 9 * sizeof(float);
721 void* buffer = m_dataArry;
722
723 EXPECT_EQ(OH_NN_SUCCESS, executorTest.SetInput(m_index, tensor, buffer, length));
724 EXPECT_EQ(OH_NN_SUCCESS, executorTest.SetOutput(m_index, buffer, length));
725 EXPECT_EQ(OH_NN_SUCCESS, executorTest.Run());
726
727 int32_t expectDim[2] = {3, 3};
728 int32_t* ptr = expectDim;
729 int32_t** dimensions = &ptr;
730 uint32_t dimensionCount = 2;
731
732 OH_NN_ReturnCode ret = executorTest.GetOutputShape(m_index, dimensions, dimensionCount);
733 EXPECT_EQ(OH_NN_SUCCESS, ret);
734 }
735
736 /*
737 * @tc.name: executor_get_output_dimensions_002
738 * @tc.desc: Verify that the GetOutputShape function returns a failed message without run.
739 * @tc.type: FUNC
740 */
741 HWTEST_F(ExecutorTest, executor_get_output_dimensions_002, testing::ext::TestSize.Level0)
742 {
743 size_t length = 9 * sizeof(float);
744 void* buffer = m_dataArry;
745 const MSLITE::LiteGraph* liteGraphTest = BuildLiteGraph(m_dim, m_dimOut);
746 InnerModel innerModel;
747 innerModel.BuildFromLiteGraph(liteGraphTest);
748 Compilation compilation(&innerModel);
749 Executor executorTest(&compilation);
750
751 OH_NN_Tensor tensor = SetTensor(OH_NN_FLOAT32, m_dimensionCount, m_dimArry, nullptr, OH_NN_TENSOR);
752
753 EXPECT_EQ(OH_NN_SUCCESS, executorTest.SetInput(m_index, tensor, buffer, length));
754 EXPECT_EQ(OH_NN_SUCCESS, executorTest.SetOutput(m_index, buffer, length));
755
756 int32_t testDim[2] = {3, 3};
757 int32_t* ptr = testDim;
758 int32_t** dimensions = &ptr;
759 uint32_t dimensionCount = 2;
760
761 OH_NN_ReturnCode ret = executorTest.GetOutputShape(m_index, dimensions, dimensionCount);
762 EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, ret);
763 }
764
765 /*
766 * @tc.name: executor_get_output_dimensions_003
767 * @tc.desc: Verify that the GetOutputShape function returns a failed message with out-of-range index.
768 * @tc.type: FUNC
769 */
770 HWTEST_F(ExecutorTest, executor_get_output_dimensions_003, testing::ext::TestSize.Level0)
771 {
772 const MSLITE::LiteGraph* liteGraphTest = BuildLiteGraph(m_dim, m_dimOut);
773 InnerModel innerModel;
774 innerModel.BuildFromLiteGraph(liteGraphTest);
775 Compilation compilation(&innerModel);
776 Executor executorTest(&compilation);
777
778 void* buffer = m_dataArry;
779 size_t length = 9 * sizeof(float);
780 OH_NN_Tensor tensor = SetTensor(OH_NN_FLOAT32, m_dimensionCount, m_dimArry, nullptr, OH_NN_TENSOR);
781
782 EXPECT_EQ(OH_NN_SUCCESS, executorTest.SetInput(m_index, tensor, buffer, length));
783 EXPECT_EQ(OH_NN_SUCCESS, executorTest.SetOutput(m_index, buffer, length));
784 EXPECT_EQ(OH_NN_SUCCESS, executorTest.Run());
785
786 uint32_t testIndex = 6;
787 int32_t testDim[2] = {3, 3};
788 int32_t* ptr = testDim;
789 int32_t** dimensions = &ptr;
790 uint32_t dimensionCount = 2;
791
792 OH_NN_ReturnCode ret = executorTest.GetOutputShape(testIndex, dimensions, dimensionCount);
793 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
794 }
795
796 /*
797 * @tc.name: executor_create_input_memory_001
798 * @tc.desc: Verify that the CreateInputMemory function returns a successful message.
799 * @tc.type: FUNC
800 */
801 HWTEST_F(ExecutorTest, executor_create_input_memory_001, testing::ext::TestSize.Level0)
802 {
803 const MSLITE::LiteGraph* liteGraphTest = BuildLiteGraph(m_dim, m_dimOut);
804 InnerModel innerModel;
805 innerModel.BuildFromLiteGraph(liteGraphTest);
806 Compilation compilation(&innerModel);
807 Executor executorTest(&compilation);
808
809 OH_NN_Memory** memory = nullptr;
810 SetMermory(memory);
811 size_t length = 9 * sizeof(float);
812
813 OH_NN_ReturnCode ret = executorTest.CreateInputMemory(m_index, length, memory);
814 EXPECT_EQ(OH_NN_SUCCESS, ret);
815 }
816
817 /*
818 * @tc.name: executor_create_input_memory_002
819 * @tc.desc: Verify that the CreateInputMemory function returns a failed message with out-of-range index.
820 * @tc.type: FUNC
821 */
822 HWTEST_F(ExecutorTest, executor_create_input_memory_002, testing::ext::TestSize.Level0)
823 {
824 const MSLITE::LiteGraph* liteGraphTest = BuildLiteGraph(m_dim, m_dimOut);
825 InnerModel innerModel;
826 innerModel.BuildFromLiteGraph(liteGraphTest);
827 Compilation compilation(&innerModel);
828 Executor executorTest(&compilation);
829
830 size_t length = 9 * sizeof(float);
831 OH_NN_Memory** memory = nullptr;
832 SetMermory(memory);
833
834 m_index = 6;
835 OH_NN_ReturnCode ret = executorTest.CreateInputMemory(m_index, length, memory);
836 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
837 }
838
839 /*
840 * @tc.name: executor_create_input_memory_003
841 * @tc.desc: Verify that the CreateInputMemory function returns a failed message with allocating buffer unsuccessfully.
842 * @tc.type: FUNC
843 */
844 HWTEST_F(ExecutorTest, executor_create_input_memory_003, testing::ext::TestSize.Level0)
845 {
846 InnerModel innerModel;
847 HDI::Nnrt::V1_0::MockIPreparedModel::m_ExpectRetCode = OH_NN_INVALID_PARAMETER;
848 const MSLITE::LiteGraph* liteGraphTest = BuildLiteGraph(m_dim, m_dimOut);
849 innerModel.BuildFromLiteGraph(liteGraphTest);
850 Compilation compilation(&innerModel);
851 Executor executorTest(&compilation);
852
853 size_t length = 9 * sizeof(float);
854 OH_NN_Memory** memory = nullptr;
855 SetMermory(memory);
856
857 OH_NN_ReturnCode ret = executorTest.CreateInputMemory(m_index, length, memory);
858 EXPECT_EQ(OH_NN_MEMORY_ERROR, ret);
859 }
860
861 /*
862 * @tc.name: executor_destroy_input_memory_001
863 * @tc.desc: Verify that the DestroyInputMemory function returns a successful message.
864 * @tc.type: FUNC
865 */
866 HWTEST_F(ExecutorTest, executor_destroy_input_memory_001, testing::ext::TestSize.Level0)
867 {
868 const MSLITE::LiteGraph* liteGraphTest = BuildLiteGraph(m_dim, m_dimOut);
869 InnerModel innerModel;
870 innerModel.BuildFromLiteGraph(liteGraphTest);
871 Compilation compilation(&innerModel);
872 Executor executorTest(&compilation);
873
874 size_t length = 9 * sizeof(float);
875 float dataArry[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
876 void* const data = dataArry;
877 OH_NN_Memory memoryPtr = {data, 9 * sizeof(float)};
878 OH_NN_Memory* ptr = &memoryPtr;
879 OH_NN_Memory** memory = &ptr;
880
881 EXPECT_EQ(OH_NN_SUCCESS, executorTest.CreateInputMemory(m_index, length, memory));
882 OH_NN_ReturnCode ret = executorTest.DestroyInputMemory(m_index, memory);
883 EXPECT_EQ(OH_NN_SUCCESS, ret);
884 }
885
886 /*
887 * @tc.name: executor_destroy_input_memory_002
888 * @tc.desc: Verify that the DestroyInputMemory function returns a failed message with out-of-range index.
889 * @tc.type: FUNC
890 */
891 HWTEST_F(ExecutorTest, executor_destroy_input_memory_002, testing::ext::TestSize.Level0)
892 {
893 const MSLITE::LiteGraph* liteGraphTest = BuildLiteGraph(m_dim, m_dimOut);
894 InnerModel innerModel;
895 innerModel.BuildFromLiteGraph(liteGraphTest);
896 Compilation compilation(&innerModel);
897 Executor executorTest(&compilation);
898
899 size_t length = 9 * sizeof(float);
900 OH_NN_Memory** memory = nullptr;
901 SetMermory(memory);
902
903 uint32_t testIndex = 6;
904 EXPECT_EQ(OH_NN_SUCCESS, executorTest.CreateInputMemory(m_index, length, memory));
905 OH_NN_ReturnCode ret = executorTest.DestroyInputMemory(testIndex, memory);
906 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
907 }
908
909 /*
910 * @tc.name: executor_destroy_input_memory_003
911 * @tc.desc: Verify that the DestroyInputMemory function returns a failed message without creating memory.
912 * @tc.type: FUNC
913 */
914 HWTEST_F(ExecutorTest, executor_destroy_input_memory_003, testing::ext::TestSize.Level0)
915 {
916 const MSLITE::LiteGraph* liteGraphTest = BuildLiteGraph(m_dim, m_dimOut);
917 InnerModel innerModel;
918 innerModel.BuildFromLiteGraph(liteGraphTest);
919 Compilation compilation(&innerModel);
920 Executor executorTest(&compilation);
921
922 OH_NN_Memory** memory = nullptr;
923 SetMermory(memory);
924
925 OH_NN_ReturnCode ret = executorTest.DestroyInputMemory(m_index, memory);
926 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
927 }
928
929 /*
930 * @tc.name: executor_destroy_input_memory_004
931 * @tc.desc: Verify that the DestroyInputMemory function returns a failed message with invalid memory.data.
932 * @tc.type: FUNC
933 */
934 HWTEST_F(ExecutorTest, executor_destroy_input_memory_004, testing::ext::TestSize.Level0)
935 {
936 const MSLITE::LiteGraph* liteGraphTest = BuildLiteGraph(m_dim, m_dimOut);
937 InnerModel innerModel;
938 innerModel.BuildFromLiteGraph(liteGraphTest);
939 Compilation compilation(&innerModel);
940 Executor executorTest(&compilation);
941
942 size_t length = 9 * sizeof(float);
943 OH_NN_Memory** memory = nullptr;
944 SetMermory(memory);
945
946 EXPECT_EQ(OH_NN_SUCCESS, executorTest.CreateInputMemory(m_index, length, memory));
947
948 float arry[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
949 void* const expectData = arry;
950 OH_NN_Memory mptr = {expectData, 9 * sizeof(float)};
951 OH_NN_Memory* expectPtr = &mptr;
952 OH_NN_Memory** expectMemory = &expectPtr;
953
954 OH_NN_ReturnCode ret = executorTest.DestroyInputMemory(m_index, expectMemory);
955 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
956 }
957
958 /*
959 * @tc.name: executor_create_output_memory_001
960 * @tc.desc: Verify that the CreateOutputMemory function returns a successful message.
961 * @tc.type: FUNC
962 */
963 HWTEST_F(ExecutorTest, executor_create_output_memory_001, testing::ext::TestSize.Level0)
964 {
965 const MSLITE::LiteGraph* liteGraphTest = BuildLiteGraph(m_dim, m_dimOut);
966 InnerModel innerModel;
967 innerModel.BuildFromLiteGraph(liteGraphTest);
968 Compilation compilation(&innerModel);
969 Executor executorTest(&compilation);
970
971 size_t length = 9 * sizeof(float);
972 OH_NN_Memory** memory = nullptr;
973 SetMermory(memory);
974
975 OH_NN_ReturnCode ret = executorTest.CreateOutputMemory(m_index, length, memory);
976 EXPECT_EQ(OH_NN_SUCCESS, ret);
977 }
978
979 /*
980 * @tc.name: executor_create_output_memory_002
981 * @tc.desc: Verify that the CreateOutputMemory function returns a failed message with out-of-range index.
982 * @tc.type: FUNC
983 */
984 HWTEST_F(ExecutorTest, executor_create_output_memory_002, testing::ext::TestSize.Level0)
985 {
986 const MSLITE::LiteGraph* liteGraphTest = BuildLiteGraph(m_dim, m_dimOut);
987 InnerModel innerModel;
988 innerModel.BuildFromLiteGraph(liteGraphTest);
989 Compilation compilation(&innerModel);
990 Executor executorTest(&compilation);
991
992 m_index = 6;
993 size_t length = 9 * sizeof(float);
994 OH_NN_Memory** memory = nullptr;
995 SetMermory(memory);
996
997 OH_NN_ReturnCode ret = executorTest.CreateOutputMemory(m_index, length, memory);
998 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
999 }
1000
1001 /*
1002 * @tc.name: executor_create_output_memory_003
1003 * @tc.desc: Verify that the CreateOutputMemory function returns a failed message with allocating buffer unsuccessfully.
1004 * @tc.type: FUNC
1005 */
1006 HWTEST_F(ExecutorTest, executor_create_output_memory_003, testing::ext::TestSize.Level0)
1007 {
1008 HDI::Nnrt::V1_0::MockIPreparedModel::m_ExpectRetCode = OH_NN_INVALID_PARAMETER;
1009 const MSLITE::LiteGraph* liteGraphTest = BuildLiteGraph(m_dim, m_dimOut);
1010 InnerModel innerModel;
1011 innerModel.BuildFromLiteGraph(liteGraphTest);
1012 Compilation compilation(&innerModel);
1013 Executor executorTest(&compilation);
1014
1015 size_t length = 9 * sizeof(float);
1016 OH_NN_Memory** memory = nullptr;
1017 SetMermory(memory);
1018
1019 OH_NN_ReturnCode ret = executorTest.CreateOutputMemory(m_index, length, memory);
1020 EXPECT_EQ(OH_NN_MEMORY_ERROR, ret);
1021 }
1022
1023 /*
1024 * @tc.name: executor_destroy_output_memory_001
1025 * @tc.desc: Verify that the DestroyOutputMemory function returns a successful message.
1026 * @tc.type: FUNC
1027 */
1028 HWTEST_F(ExecutorTest, executor_destroy_output_memory_001, testing::ext::TestSize.Level0)
1029 {
1030 InnerModel innerModel;
1031 const MSLITE::LiteGraph* liteGraphTest = BuildLiteGraph(m_dim, m_dimOut);
1032 innerModel.BuildFromLiteGraph(liteGraphTest);
1033 Compilation compilation(&innerModel);
1034 Executor executorTest(&compilation);
1035
1036 size_t length = 9 * sizeof(float);
1037 OH_NN_Memory** memory = nullptr;
1038 SetMermory(memory);
1039
1040 EXPECT_EQ(OH_NN_SUCCESS, executorTest.CreateOutputMemory(m_index, length, memory));
1041 OH_NN_ReturnCode ret = executorTest.DestroyOutputMemory(m_index, memory);
1042 EXPECT_EQ(OH_NN_SUCCESS, ret);
1043 }
1044
1045 /*
1046 * @tc.name: executor_destroy_output_memory_002
1047 * @tc.desc: Verify that the DestroyOutputMemory function returns a failed message with out-of-range index.
1048 * @tc.type: FUNC
1049 */
1050 HWTEST_F(ExecutorTest, executor_destroy_output_memory_002, testing::ext::TestSize.Level0)
1051 {
1052 const MSLITE::LiteGraph* liteGraphTest = BuildLiteGraph(m_dim, m_dimOut);
1053 InnerModel innerModel;
1054 innerModel.BuildFromLiteGraph(liteGraphTest);
1055 Compilation compilation(&innerModel);
1056 Executor executorTest(&compilation);
1057
1058 uint32_t testIndex = 6;
1059 size_t length = 9 * sizeof(float);
1060 OH_NN_Memory** memory = nullptr;
1061 SetMermory(memory);
1062
1063 EXPECT_EQ(OH_NN_SUCCESS, executorTest.CreateOutputMemory(m_index, length, memory));
1064 OH_NN_ReturnCode ret = executorTest.DestroyOutputMemory(testIndex, memory);
1065 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
1066 }
1067
1068 /*
1069 * @tc.name: executor_destroy_output_memory_003
1070 * @tc.desc: Verify that the DestroyOutputMemory function returns a failed message without creating memory.
1071 * @tc.type: FUNC
1072 */
1073 HWTEST_F(ExecutorTest, executor_destroy_output_memory_003, testing::ext::TestSize.Level0)
1074 {
1075 const MSLITE::LiteGraph* liteGraphTest = BuildLiteGraph(m_dim, m_dimOut);
1076 InnerModel innerModel;
1077 innerModel.BuildFromLiteGraph(liteGraphTest);
1078 Compilation compilation(&innerModel);
1079 Executor executorTest(&compilation);
1080
1081 OH_NN_Memory** memory = nullptr;
1082 SetMermory(memory);
1083
1084 OH_NN_ReturnCode ret = executorTest.DestroyOutputMemory(m_index, memory);
1085 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
1086 }
1087
1088 /*
1089 * @tc.name: executor_destroy_output_memory_004
1090 * @tc.desc: Verify that the DestroyOutputMemory function returns a failed message with invalid memory.data.
1091 * @tc.type: FUNC
1092 */
1093 HWTEST_F(ExecutorTest, executor_destroy_output_memory_004, testing::ext::TestSize.Level0)
1094 {
1095 const MSLITE::LiteGraph* liteGraphTest = BuildLiteGraph(m_dim, m_dimOut);
1096 InnerModel innerModel;
1097 innerModel.BuildFromLiteGraph(liteGraphTest);
1098 Compilation compilation(&innerModel);
1099 Executor executorTest(&compilation);
1100
1101 size_t length = 9 * sizeof(float);
1102 OH_NN_Memory** memory = nullptr;
1103 SetMermory(memory);
1104
1105 EXPECT_EQ(OH_NN_SUCCESS, executorTest.CreateOutputMemory(m_index, length, memory));
1106
1107 float arry[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
1108 void* const expectData = arry;
1109 OH_NN_Memory mptr = {expectData, 9 * sizeof(float)};
1110 OH_NN_Memory* expectPtr = &mptr;
1111 OH_NN_Memory** expectMemory = &expectPtr;
1112
1113 OH_NN_ReturnCode ret = executorTest.DestroyOutputMemory(m_index, expectMemory);
1114 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
1115 }
1116
1117 /*
1118 * @tc.name: executor_run_test_001
1119 * @tc.desc: Verify that the Run function returns a successful message.
1120 * @tc.type: FUNC
1121 */
1122 HWTEST_F(ExecutorTest, executor_run_test_001, testing::ext::TestSize.Level0)
1123 {
1124 HiviewDFX::HiTraceId traceId = HiTraceChain::Begin("executor_run_test_001", HITRACE_FLAG_TP_INFO);
1125 const MSLITE::LiteGraph* liteGraphTest = BuildLiteGraph(m_dim, m_dimOut);
1126 InnerModel innerModel;
1127 innerModel.BuildFromLiteGraph(liteGraphTest);
1128 Compilation compilation(&innerModel);
1129 Executor executorTest(&compilation);
1130
1131 size_t length = 9 * sizeof(float);
1132 OH_NN_Tensor tensor = SetTensor(OH_NN_FLOAT32, m_dimensionCount, m_dimArry, nullptr, OH_NN_TENSOR);
1133 void* buffer = m_dataArry;
1134
1135 EXPECT_EQ(OH_NN_SUCCESS, executorTest.SetInput(m_index, tensor, buffer, length));
1136 EXPECT_EQ(OH_NN_SUCCESS, executorTest.SetOutput(m_index, buffer, length));
1137 OH_NN_ReturnCode ret = executorTest.Run();
1138 HiTraceChain::End(traceId);
1139 EXPECT_EQ(OH_NN_SUCCESS, ret);
1140 }
1141
1142 /*
1143 * @tc.name: executor_run_test_002
1144 * @tc.desc: Verify that the DestroyOutputMemory function returns a failed message without SetInput.
1145 * @tc.type: FUNC
1146 */
1147 HWTEST_F(ExecutorTest, executor_run_test_002, testing::ext::TestSize.Level0)
1148 {
1149 const MSLITE::LiteGraph* liteGraphTest = BuildLiteGraph(m_dim, m_dimOut);
1150 InnerModel innerModel;
1151 innerModel.BuildFromLiteGraph(liteGraphTest);
1152 Compilation compilation(&innerModel);
1153 Executor executorTest(&compilation);
1154
1155 OH_NN_ReturnCode ret = executorTest.Run();
1156 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
1157 }
1158
1159 /*
1160 * @tc.name: executor_run_test_003
1161 * @tc.desc: Verify that the DestroyOutputMemory function returns a failed message without SetOutput.
1162 * @tc.type: FUNC
1163 */
1164 HWTEST_F(ExecutorTest, executor_run_test_003, testing::ext::TestSize.Level0)
1165 {
1166 const MSLITE::LiteGraph* liteGraphTest = BuildLiteGraph(m_dim, m_dimOut);
1167 InnerModel innerModel;
1168 innerModel.BuildFromLiteGraph(liteGraphTest);
1169 Compilation compilation(&innerModel);
1170 Executor executorTest(&compilation);
1171
1172 OH_NN_Tensor tensor = SetTensor(OH_NN_FLOAT32, m_dimensionCount, m_dimArry, nullptr, OH_NN_TENSOR);
1173 void* buffer = m_dataArry;
1174 size_t length = 9 * sizeof(float);
1175
1176 EXPECT_EQ(OH_NN_SUCCESS, executorTest.SetInput(m_index, tensor, buffer, length));
1177 OH_NN_ReturnCode ret = executorTest.Run();
1178 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
1179 }
1180
1181 /*
1182 * @tc.name: executor_run_test_004
1183 * @tc.desc: Verify that the DestroyOutputMemory function returns a failed message with failed executionPlan.Run.
1184 * @tc.type: FUNC
1185 */
1186 HWTEST_F(ExecutorTest, executor_run_test_004, testing::ext::TestSize.Level0)
1187 {
1188 HDI::Nnrt::V1_0::MockIPreparedModel::m_ExpectRetCode = OH_NN_FAILED;
1189 const MSLITE::LiteGraph* liteGraphTest = BuildLiteGraph(m_dim, m_dimOut);
1190 InnerModel innerModel;
1191 innerModel.BuildFromLiteGraph(liteGraphTest);
1192 Compilation compilation(&innerModel);
1193 Executor executorTest(&compilation);
1194
1195 OH_NN_Tensor tensor = SetTensor(OH_NN_FLOAT32, m_dimensionCount, m_dimArry, nullptr, OH_NN_TENSOR);
1196 void* buffer = m_dataArry;
1197 size_t length = 9 * sizeof(float);
1198
1199 EXPECT_EQ(OH_NN_SUCCESS, executorTest.SetInput(m_index, tensor, buffer, length));
1200 EXPECT_EQ(OH_NN_SUCCESS, executorTest.SetOutput(m_index, buffer, length));
1201 OH_NN_ReturnCode ret = executorTest.Run();
1202 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
1203 }
1204 } // namespace UnitTest
1205 } // namespace NeuralNetworkRuntime
1206 } // namespace OHOS