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 "compilation_test.h"
17
18 #include <fstream>
19
20 #include "mindir.h"
21
22 #include "test/unittest/common/mock_idevice.h"
23
24 using namespace OHOS::NeuralNetworkRuntime;
25 using namespace OHOS::HDI::Nnrt::V1_0;
26
27 namespace OHOS {
28 namespace NeuralNetworkRuntime {
29 namespace UnitTest {
30 static const int DATA_VALUE = 1;
31 static const int DATA_NUM = 36;
32 static const int DIM_NUM = 3;
BuildModelGraph(NeuralNetworkRuntime::InnerModel & innerModel)33 OH_NN_ReturnCode CompilationTest::BuildModelGraph(NeuralNetworkRuntime::InnerModel& innerModel)
34 {
35 // liteGraph is released internally by innerModel
36 mindspore::lite::LiteGraph* liteGraph = new (std::nothrow) mindspore::lite::LiteGraph;
37 EXPECT_NE(nullptr, liteGraph);
38
39 liteGraph->name_ = "testGraph";
40 liteGraph->input_indices_ = {0};
41 liteGraph->output_indices_ = {1};
42 liteGraph->all_tensors_ = {nullptr};
43 const std::vector<mindspore::lite::QuantParam> quant_params {};
44 const std::vector<uint8_t> data(DATA_NUM, DATA_VALUE);
45 const std::vector<int32_t> dim = {DIM_NUM, DIM_NUM};
46
47 for (size_t indexInput = 0; indexInput < liteGraph->input_indices_.size(); ++indexInput) {
48 liteGraph->all_tensors_.emplace_back(mindspore::lite::MindIR_Tensor_Create(liteGraph->name_,
49 mindspore::lite::DATA_TYPE_FLOAT32, dim, mindspore::lite::FORMAT_NCHW, data, quant_params));
50 }
51
52 for (size_t indexOutput = 0; indexOutput < liteGraph->output_indices_.size(); ++indexOutput) {
53 liteGraph->all_tensors_.emplace_back(mindspore::lite::MindIR_Tensor_Create(liteGraph->name_,
54 mindspore::lite::DATA_TYPE_FLOAT32, dim, mindspore::lite::FORMAT_NCHW, data, quant_params));
55 }
56
57 OH_NN_ReturnCode ret = innerModel.BuildFromLiteGraph(liteGraph);
58 return ret;
59 }
60
SetConfig(Compilation & compilationTest)61 void CompilationTest::SetConfig(Compilation& compilationTest)
62 {
63 std::size_t deviceId = 1;
64 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.SetDevice(deviceId));
65 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.SetPerformance(OH_NN_PERFORMANCE_EXTREME));
66 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.SetPriority(OH_NN_PRIORITY_HIGH));
67 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.SetEnableFp16(true));
68 }
69
WriteFile(uint64_t version,uint64_t fileNumber,std::size_t cacheDeviceId)70 void CompilationTest::WriteFile(uint64_t version, uint64_t fileNumber, std::size_t cacheDeviceId)
71 {
72 uint64_t cacheSize = 4;
73 uint64_t writeSize = 7;
74 uint64_t cacheInfo[7] = {};
75 auto cacheInfoPtr = cacheInfo;
76 *cacheInfoPtr++ = fileNumber;
77 *cacheInfoPtr++ = version;
78 *cacheInfoPtr++ = cacheDeviceId;
79 for (uint64_t i = 0; i < cacheSize; ++i) {
80 *cacheInfoPtr++ = i;
81 }
82 std::ofstream inFile("cache_info.nncache", std::ios::binary | std::ios::out | std::ios::trunc);
83 inFile.write(reinterpret_cast<const char*>(cacheInfo), writeSize * sizeof(uint64_t));
84 inFile.close();
85 }
86
BuildCompilation(InnerModel & innerModel)87 void CompilationTest::BuildCompilation(InnerModel& innerModel)
88 {
89 EXPECT_EQ(OH_NN_SUCCESS, BuildModelGraph(innerModel));
90 Compilation compilationTest(&innerModel);
91 SetConfig(compilationTest);
92 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.SetCacheDir("./", 1));
93 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.Build());
94 }
95
96 /*
97 * @tc.name: compilation_set_device_001
98 * @tc.desc: Verify the set deviceId after compilation finish of the SetDevice function.
99 * @tc.type: FUNC
100 */
101 HWTEST_F(CompilationTest, compilation_set_device_001, testing::ext::TestSize.Level0)
102 {
103 InnerModel innerModel;
104 EXPECT_EQ(OH_NN_SUCCESS, BuildModelGraph(innerModel));
105 Compilation compilationTest(&innerModel);
106 SetConfig(compilationTest);
107 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.Build());
108
109 std::size_t deviceId = 1;
110 OH_NN_ReturnCode ret = compilationTest.SetDevice(deviceId);
111 EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, ret);
112 }
113
114 /*
115 * @tc.name: compilation_set_device_002
116 * @tc.desc: Verify the deviceId does not exist of the SetDevice function.
117 * @tc.type: FUNC
118 */
119 HWTEST_F(CompilationTest, compilation_set_device_002, testing::ext::TestSize.Level0)
120 {
121 InnerModel innerModel;
122 EXPECT_EQ(OH_NN_SUCCESS, BuildModelGraph(innerModel));
123 Compilation compilationTest(&innerModel);
124 size_t deviceId = 0;
125 OH_NN_ReturnCode ret = compilationTest.SetDevice(deviceId);
126 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
127 }
128
129 /*
130 * @tc.name: compilation_set_device_003
131 * @tc.desc: Verify the error happened when getting supported operation of the SetDevice function.
132 * @tc.type: FUNC
133 */
134 HWTEST_F(CompilationTest, compilation_set_device_003, testing::ext::TestSize.Level0)
135 {
136 InnerModel innerModel;
137 Compilation compilationTest(&innerModel);
138 std::size_t deviceId = 1;
139 OH_NN_ReturnCode ret = compilationTest.SetDevice(deviceId);
140 EXPECT_EQ(OH_NN_NULL_PTR, ret);
141 }
142
143 /*
144 * @tc.name: compilation_set_device_004
145 * @tc.desc: Verify the current device not support the model of the SetDevice function.
146 * @tc.type: FUNC
147 */
148 HWTEST_F(CompilationTest, compilation_set_device_004, testing::ext::TestSize.Level0)
149 {
150 InnerModel innerModel;
151 EXPECT_EQ(OH_NN_SUCCESS, BuildModelGraph(innerModel));
152 Compilation compilationTest(&innerModel);
153 std::size_t deviceId = 1;
154 MockIPreparedModel::m_ExpectRetCode = OH_NN_SUCCESS;
155 OH_NN_ReturnCode ret = compilationTest.SetDevice(deviceId);
156 EXPECT_EQ(OH_NN_FAILED, ret);
157 }
158
159 /*
160 * @tc.name: compilation_set_device_005
161 * @tc.desc: Verify the error happened when checking whether device supports dynamic input of the SetDevice function.
162 * @tc.type: FUNC
163 */
164 HWTEST_F(CompilationTest, compilation_set_device_005, testing::ext::TestSize.Level0)
165 {
166 InnerModel innerModel;
167 EXPECT_EQ(OH_NN_SUCCESS, BuildModelGraph(innerModel));
168 Compilation compilationTest(&innerModel);
169 std::size_t deviceId = 1;
170 MockIPreparedModel::m_ExpectRetCode = OH_NN_FAILED;
171 OH_NN_ReturnCode ret = compilationTest.SetDevice(deviceId);
172 EXPECT_EQ(OH_NN_FAILED, ret);
173 }
174
175 /*
176 * @tc.name: compilation_set_device_006
177 * @tc.desc: Verify the device does not support dynamic shape inputs of the SetDevice function.
178 * @tc.type: FUNC
179 */
180 HWTEST_F(CompilationTest, compilation_set_device_006, testing::ext::TestSize.Level0)
181 {
182 InnerModel innerModel;
183 EXPECT_EQ(OH_NN_SUCCESS, BuildModelGraph(innerModel));
184 Compilation compilationTest(&innerModel);
185 std::size_t deviceId = 1;
186 MockIPreparedModel::m_ExpectRetCode = OH_NN_INVALID_PATH;
187 OH_NN_ReturnCode ret = compilationTest.SetDevice(deviceId);
188 EXPECT_EQ(OH_NN_FAILED, ret);
189 }
190
191 /*
192 * @tc.name: compilation_set_device_007
193 * @tc.desc: Verify the set normal deviceId of the SetDevice function.
194 * @tc.type: FUNC
195 */
196 HWTEST_F(CompilationTest, compilation_set_device_007, testing::ext::TestSize.Level0)
197 {
198 InnerModel innerModel;
199 EXPECT_EQ(OH_NN_SUCCESS, BuildModelGraph(innerModel));
200 Compilation compilationTest(&innerModel);
201 std::size_t deviceId = 1;
202 OH_NN_ReturnCode ret = compilationTest.SetDevice(deviceId);
203 EXPECT_EQ(OH_NN_SUCCESS, ret);
204 }
205
206 /*
207 * @tc.name: compilation_set_cachedir_001
208 * @tc.desc: Verify the set cache after compilation finish of the SetCacheDir function.
209 * @tc.type: FUNC
210 */
211 HWTEST_F(CompilationTest, compilation_set_cachedir_001, testing::ext::TestSize.Level0)
212 {
213 InnerModel innerModel;
214 EXPECT_EQ(OH_NN_SUCCESS, BuildModelGraph(innerModel));
215 Compilation compilationTest(&innerModel);
216 SetConfig(compilationTest);
217 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.Build());
218
219 std::size_t deviceId = 1;
220 EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, compilationTest.SetDevice(deviceId));
221
222 OH_NN_ReturnCode ret = compilationTest.SetCacheDir("../", 1);
223 EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, ret);
224 }
225
226 /*
227 * @tc.name: compilation_set_cachedir_002
228 * @tc.desc: Verify the not set device of the SetCacheDir function.
229 * @tc.type: FUNC
230 */
231 HWTEST_F(CompilationTest, compilation_set_cachedir_002, testing::ext::TestSize.Level0)
232 {
233 InnerModel innerModel;
234 Compilation compilationTest(&innerModel);
235 OH_NN_ReturnCode ret = compilationTest.SetCacheDir("../", 1);
236 EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, ret);
237 }
238
239 /*
240 * @tc.name: compilation_set_cachedir_003
241 * @tc.desc: Verify the Fail to query whether the device is available to save cache model of the SetCacheDir function.
242 * @tc.type: FUNC
243 */
244 HWTEST_F(CompilationTest, compilation_set_cachedir_003, testing::ext::TestSize.Level0)
245 {
246 InnerModel innerModel;
247 EXPECT_EQ(OH_NN_SUCCESS, BuildModelGraph(innerModel));
248 Compilation compilationTest(&innerModel);
249 std::size_t deviceId = 1;
250 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.SetDevice(deviceId));
251
252 MockIPreparedModel::m_ExpectRetCode = OH_NN_FAILED;
253 OH_NN_ReturnCode ret = compilationTest.SetCacheDir("../", 1);
254 EXPECT_EQ(OH_NN_FAILED, ret);
255 }
256
257 /*
258 * @tc.name: compilation_set_cachedir_004
259 * @tc.desc: Verify the device is unavailable to save cache model of the SetCacheDir function.
260 * @tc.type: FUNC
261 */
262 HWTEST_F(CompilationTest, compilation_set_cachedir_004, testing::ext::TestSize.Level0)
263 {
264 InnerModel innerModel;
265 EXPECT_EQ(OH_NN_SUCCESS, BuildModelGraph(innerModel));
266 Compilation compilationTest(&innerModel);
267 std::size_t deviceId = 1;
268 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.SetDevice(deviceId));
269
270 MockIPreparedModel::m_ExpectRetCode = OH_NN_SUCCESS;
271 OH_NN_ReturnCode ret = compilationTest.SetCacheDir("../", 1);
272 EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, ret);
273 }
274
275 /*
276 * @tc.name: compilation_set_cachedir_005
277 * @tc.desc: Verify the cache model path is invalid of the SetCacheDir function.
278 * @tc.type: FUNC
279 */
280 HWTEST_F(CompilationTest, compilation_set_cachedir_005, testing::ext::TestSize.Level0)
281 {
282 InnerModel innerModel;
283 EXPECT_EQ(OH_NN_SUCCESS, BuildModelGraph(innerModel));
284 Compilation compilationTest(&innerModel);
285 std::size_t deviceId = 1;
286 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.SetDevice(deviceId));
287 OH_NN_ReturnCode ret = compilationTest.SetCacheDir("../compilation_test.cpp", 1);
288 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
289 }
290
291 /*
292 * @tc.name: compilation_set_cachedir_006
293 * @tc.desc: Verify the cache model path is not a directory of the SetCacheDir function.
294 * @tc.type: FUNC
295 */
296 HWTEST_F(CompilationTest, compilation_set_cachedir_006, testing::ext::TestSize.Level0)
297 {
298 InnerModel innerModel;
299 EXPECT_EQ(OH_NN_SUCCESS, BuildModelGraph(innerModel));
300 Compilation compilationTest(&innerModel);
301 std::size_t deviceId = 1;
302 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.SetDevice(deviceId));
303 OH_NN_ReturnCode ret = compilationTest.SetCacheDir("./CompilationTest", 1);
304 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
305 }
306
307 /*
308 * @tc.name: compilation_set_cachedir_007
309 * @tc.desc: Verify the success of the SetCacheDir function.
310 * @tc.type: FUNC
311 */
312 HWTEST_F(CompilationTest, compilation_set_cachedir_007, testing::ext::TestSize.Level0)
313 {
314 InnerModel innerModel;
315 EXPECT_EQ(OH_NN_SUCCESS, BuildModelGraph(innerModel));
316 Compilation compilationTest(&innerModel);
317 std::size_t deviceId = 1;
318 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.SetDevice(deviceId));
319 OH_NN_ReturnCode ret = compilationTest.SetCacheDir("../", 1);
320 EXPECT_EQ(OH_NN_SUCCESS, ret);
321 }
322
323 /*
324 * @tc.name: compilation_set_performance_001
325 * @tc.desc: Verify the set performance after compilation finish of the SetPerformance function.
326 * @tc.type: FUNC
327 */
328 HWTEST_F(CompilationTest, compilation_set_performance_001, testing::ext::TestSize.Level0)
329 {
330 InnerModel innerModel;
331 EXPECT_EQ(OH_NN_SUCCESS, BuildModelGraph(innerModel));
332 Compilation compilationTest(&innerModel);
333
334 SetConfig(compilationTest);
335 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.Build());
336
337 std::size_t deviceId = 1;
338 EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, compilationTest.SetDevice(deviceId));
339
340 OH_NN_ReturnCode ret = compilationTest.SetPerformance(OH_NN_PERFORMANCE_NONE);
341 EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, ret);
342 }
343
344 /*
345 * @tc.name: compilation_set_performance_002
346 * @tc.desc: Verify the set performance before set device of the SetPerformance function.
347 * @tc.type: FUNC
348 */
349 HWTEST_F(CompilationTest, compilation_set_performance_002, testing::ext::TestSize.Level0)
350 {
351 InnerModel innerModel;
352 Compilation compilationTest(&innerModel);
353 OH_NN_ReturnCode ret = compilationTest.SetPerformance(OH_NN_PERFORMANCE_NONE);
354 EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, ret);
355 }
356
357 /*
358 * @tc.name: compilation_set_performance_003
359 * @tc.desc: Verify the call device failed of the SetPerformance function.
360 * @tc.type: FUNC
361 */
362 HWTEST_F(CompilationTest, compilation_set_performance_003, testing::ext::TestSize.Level0)
363 {
364 InnerModel innerModel;
365 EXPECT_EQ(OH_NN_SUCCESS, BuildModelGraph(innerModel));
366 Compilation compilationTest(&innerModel);
367
368 std::size_t deviceId = 1;
369 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.SetDevice(deviceId));
370
371 MockIPreparedModel::m_ExpectRetCode = OH_NN_FAILED;
372 OH_NN_ReturnCode ret = compilationTest.SetPerformance(OH_NN_PERFORMANCE_NONE);
373 EXPECT_EQ(OH_NN_FAILED, ret);
374 }
375
376 /*
377 * @tc.name: compilation_set_performance_004
378 * @tc.desc: Verify the device is not support performance setting of the SetPerformance function.
379 * @tc.type: FUNC
380 */
381 HWTEST_F(CompilationTest, compilation_set_performance_004, testing::ext::TestSize.Level0)
382 {
383 InnerModel innerModel;
384 EXPECT_EQ(OH_NN_SUCCESS, BuildModelGraph(innerModel));
385 Compilation compilationTest(&innerModel);
386
387 std::size_t deviceId = 1;
388 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.SetDevice(deviceId));
389
390 MockIPreparedModel::m_ExpectRetCode = OH_NN_SUCCESS;
391 OH_NN_ReturnCode ret = compilationTest.SetPerformance(OH_NN_PERFORMANCE_NONE);
392 EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, ret);
393 }
394
395 /*
396 * @tc.name: compilation_set_performance_005
397 * @tc.desc: Verify the passed invalid performance of the SetPerformance function.
398 * @tc.type: FUNC
399 */
400 HWTEST_F(CompilationTest, compilation_set_performance_005, testing::ext::TestSize.Level0)
401 {
402 InnerModel innerModel;
403 EXPECT_EQ(OH_NN_SUCCESS, BuildModelGraph(innerModel));
404 Compilation compilationTest(&innerModel);
405
406 std::size_t deviceId = 1;
407 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.SetDevice(deviceId));
408
409 OH_NN_PerformanceMode performance = static_cast<OH_NN_PerformanceMode>(5);
410 OH_NN_ReturnCode ret = compilationTest.SetPerformance(performance);
411 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
412 }
413
414 /*
415 * @tc.name: compilation_set_performance_006
416 * @tc.desc: Verify the success of the SetPerformance function.
417 * @tc.type: FUNC
418 */
419 HWTEST_F(CompilationTest, compilation_set_performance_006, testing::ext::TestSize.Level0)
420 {
421 InnerModel innerModel;
422 EXPECT_EQ(OH_NN_SUCCESS, BuildModelGraph(innerModel));
423 Compilation compilationTest(&innerModel);
424
425 std::size_t deviceId = 1;
426 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.SetDevice(deviceId));
427
428 OH_NN_ReturnCode ret = compilationTest.SetPerformance(OH_NN_PERFORMANCE_NONE);
429 EXPECT_EQ(OH_NN_SUCCESS, ret);
430 }
431
432 /*
433 * @tc.name: compilation_set_priority_001
434 * @tc.desc: Verify the set priority after compilation finish of the SetPriority function.
435 * @tc.type: FUNC
436 */
437 HWTEST_F(CompilationTest, compilation_set_priority_001, testing::ext::TestSize.Level0)
438 {
439 InnerModel innerModel;
440 EXPECT_EQ(OH_NN_SUCCESS, BuildModelGraph(innerModel));
441 Compilation compilationTest(&innerModel);
442
443 SetConfig(compilationTest);
444 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.Build());
445
446 std::size_t deviceId = 1;
447 EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, compilationTest.SetDevice(deviceId));
448
449 OH_NN_ReturnCode ret = compilationTest.SetPriority(OH_NN_PRIORITY_LOW);
450 EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, ret);
451 }
452
453 /*
454 * @tc.name: compilation_set_priority_002
455 * @tc.desc: Verify the set priority before set device of the SetPriority function.
456 * @tc.type: FUNC
457 */
458 HWTEST_F(CompilationTest, compilation_set_priority_002, testing::ext::TestSize.Level0)
459 {
460 InnerModel innerModel;
461 Compilation compilationTest(&innerModel);
462
463 OH_NN_ReturnCode ret = compilationTest.SetPriority(OH_NN_PRIORITY_LOW);
464 EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, ret);
465 }
466
467 /*
468 * @tc.name: compilation_set_priority_003
469 * @tc.desc: Verify the call device failed of the SetPriority function.
470 * @tc.type: FUNC
471 */
472 HWTEST_F(CompilationTest, compilation_set_priority_003, testing::ext::TestSize.Level0)
473 {
474 InnerModel innerModel;
475 EXPECT_EQ(OH_NN_SUCCESS, BuildModelGraph(innerModel));
476 Compilation compilationTest(&innerModel);
477
478 std::size_t deviceId = 1;
479 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.SetDevice(deviceId));
480
481 MockIPreparedModel::m_ExpectRetCode = OH_NN_INVALID_PARAMETER;
482 OH_NN_ReturnCode ret = compilationTest.SetPriority(OH_NN_PRIORITY_LOW);
483 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
484 }
485
486 /*
487 * @tc.name: compilation_set_priority_004
488 * @tc.desc: Verify the device is not support priority setting of the SetPriority function.
489 * @tc.type: FUNC
490 */
491 HWTEST_F(CompilationTest, compilation_set_priority_004, testing::ext::TestSize.Level0)
492 {
493 InnerModel innerModel;
494 EXPECT_EQ(OH_NN_SUCCESS, BuildModelGraph(innerModel));
495 Compilation compilationTest(&innerModel);
496
497 std::size_t deviceId = 1;
498 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.SetDevice(deviceId));
499
500 MockIPreparedModel::m_ExpectRetCode = OH_NN_SUCCESS;
501 OH_NN_ReturnCode ret = compilationTest.SetPriority(OH_NN_PRIORITY_LOW);
502 EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, ret);
503 }
504
505 /*
506 * @tc.name: compilation_set_priority_005
507 * @tc.desc: Verify the passed invalid priority of the SetPriority function.
508 * @tc.type: FUNC
509 */
510 HWTEST_F(CompilationTest, compilation_set_priority_005, testing::ext::TestSize.Level0)
511 {
512 InnerModel innerModel;
513 EXPECT_EQ(OH_NN_SUCCESS, BuildModelGraph(innerModel));
514 Compilation compilationTest(&innerModel);
515
516 std::size_t deviceId = 1;
517 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.SetDevice(deviceId));
518
519 OH_NN_Priority priority = static_cast<OH_NN_Priority>(5);;
520 OH_NN_ReturnCode ret = compilationTest.SetPriority(priority);
521 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
522 }
523
524 /*
525 * @tc.name: compilation_set_priority_006
526 * @tc.desc: Verify the success of the SetPriority function.
527 * @tc.type: FUNC
528 */
529 HWTEST_F(CompilationTest, compilation_set_priority_006, testing::ext::TestSize.Level0)
530 {
531 InnerModel innerModel;
532 EXPECT_EQ(OH_NN_SUCCESS, BuildModelGraph(innerModel));
533 Compilation compilationTest(&innerModel);
534
535 std::size_t deviceId = 1;
536 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.SetDevice(deviceId));
537
538 OH_NN_ReturnCode ret = compilationTest.SetPriority(OH_NN_PRIORITY_LOW);
539 EXPECT_EQ(OH_NN_SUCCESS, ret);
540 }
541
542 /*
543 * @tc.name: compilation_set_enable_fp16_001
544 * @tc.desc: Verify the enable float16 after compilation finish of the SetEnableFp16 function.
545 * @tc.type: FUNC
546 */
547 HWTEST_F(CompilationTest, compilation_set_enable_fp16_001, testing::ext::TestSize.Level0)
548 {
549 InnerModel innerModel;
550 EXPECT_EQ(OH_NN_SUCCESS, BuildModelGraph(innerModel));
551 Compilation compilationTest(&innerModel);
552
553 SetConfig(compilationTest);
554 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.Build());
555
556 std::size_t deviceId = 1;
557 EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, compilationTest.SetDevice(deviceId));
558
559 OH_NN_ReturnCode ret = compilationTest.SetEnableFp16(true);
560 EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, ret);
561 }
562
563 /*
564 * @tc.name: compilation_set_enable_fp16_002
565 * @tc.desc: Verify the set enable fp16 before set device of the SetEnableFp16 function.
566 * @tc.type: FUNC
567 */
568 HWTEST_F(CompilationTest, compilation_set_enable_fp16_002, testing::ext::TestSize.Level0)
569 {
570 InnerModel innerModel;
571 Compilation compilationTest(&innerModel);
572
573 OH_NN_ReturnCode ret = compilationTest.SetEnableFp16(true);
574 EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, ret);
575 }
576
577 /*
578 * @tc.name: compilation_set_enable_fp16_003
579 * @tc.desc: Verify the call device failed of the SetEnableFp16 function.
580 * @tc.type: FUNC
581 */
582 HWTEST_F(CompilationTest, compilation_set_enable_fp16_003, testing::ext::TestSize.Level0)
583 {
584 InnerModel innerModel;
585 EXPECT_EQ(OH_NN_SUCCESS, BuildModelGraph(innerModel));
586 Compilation compilationTest(&innerModel);
587
588 std::size_t deviceId = 1;
589 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.SetDevice(deviceId));
590
591 MockIPreparedModel::m_ExpectRetCode = OH_NN_MEMORY_ERROR;
592 OH_NN_ReturnCode ret = compilationTest.SetEnableFp16(true);
593 EXPECT_EQ(OH_NN_MEMORY_ERROR, ret);
594 }
595
596 /*
597 * @tc.name: compilation_set_enable_fp16_004
598 * @tc.desc: Verify the device is not support float16 precision setting of the SetEnableFp16 function.
599 * @tc.type: FUNC
600 */
601 HWTEST_F(CompilationTest, compilation_set_enable_fp16_004, testing::ext::TestSize.Level0)
602 {
603 InnerModel innerModel;
604 EXPECT_EQ(OH_NN_SUCCESS, BuildModelGraph(innerModel));
605 Compilation compilationTest(&innerModel);
606
607 std::size_t deviceId = 1;
608 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.SetDevice(deviceId));
609
610 MockIPreparedModel::m_ExpectRetCode = OH_NN_SUCCESS;
611 OH_NN_ReturnCode ret = compilationTest.SetEnableFp16(true);
612 EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, ret);
613 }
614
615 /*
616 * @tc.name: compilation_set_enable_fp16_005
617 * @tc.desc: Verify the success of the SetEnableFp16 function.
618 * @tc.type: FUNC
619 */
620 HWTEST_F(CompilationTest, compilation_set_enable_fp16_005, testing::ext::TestSize.Level0)
621 {
622 InnerModel innerModel;
623 EXPECT_EQ(OH_NN_SUCCESS, BuildModelGraph(innerModel));
624 Compilation compilationTest(&innerModel);
625
626 std::size_t deviceId = 1;
627 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.SetDevice(deviceId));
628
629 OH_NN_ReturnCode ret = compilationTest.SetEnableFp16(true);
630 EXPECT_EQ(OH_NN_SUCCESS, ret);
631 }
632
633 /*
634 * @tc.name: compilation_get_input_tensors_001
635 * @tc.desc: Verify the normal input tensors of the GetInputTensors function.
636 * @tc.type: FUNC
637 */
638 HWTEST_F(CompilationTest, compilation_get_input_tensors_001, testing::ext::TestSize.Level0)
639 {
640 InnerModel innerModel;
641 Compilation compilationTest(&innerModel);
642 EXPECT_EQ(innerModel.GetInputTensors(), compilationTest.GetInputTensors());
643 }
644
645 /*
646 * @tc.name: compilation_get_output_tensors_001
647 * @tc.desc: Verify the normal output tensors of the GetOutputTensors function.
648 * @tc.type: FUNC
649 */
650 HWTEST_F(CompilationTest, compilation_get_output_tensors_001, testing::ext::TestSize.Level0)
651 {
652 InnerModel innerModel;
653 Compilation compilationTest(&innerModel);
654 EXPECT_EQ(innerModel.GetOutputTensors(), compilationTest.GetOutputTensors());
655 }
656
657 /*
658 * @tc.name: compilation_get_execution_plan_001
659 * @tc.desc: Verify the passed nullptr of the GetExecutionPlan function.
660 * @tc.type: FUNC
661 */
662 HWTEST_F(CompilationTest, compilation_get_execution_plan_001, testing::ext::TestSize.Level0)
663 {
664 InnerModel innerModel;
665 Compilation compilationTest(&innerModel);
666 EXPECT_EQ(nullptr, compilationTest.GetExecutionPlan());
667 }
668
669 /*
670 * @tc.name: compilation_is_dynamic_shape_001
671 * @tc.desc: Verify the input tensor is empth of the IsDynamicShape function.
672 * @tc.type: FUNC
673 */
674 HWTEST_F(CompilationTest, compilation_is_dynamic_shape_001, testing::ext::TestSize.Level0)
675 {
676 InnerModel innerModel;
677 Compilation compilationTest(&innerModel);
678 EXPECT_EQ(false, compilationTest.IsDynamicShape());
679 }
680
681 /*
682 * @tc.name: compilation_is_dynamic_shape_002
683 * @tc.desc: Verify the return true of the IsDynamicShape function.
684 * @tc.type: FUNC
685 */
686 HWTEST_F(CompilationTest, compilation_is_dynamic_shape_002, testing::ext::TestSize.Level0)
687 {
688 InnerModel innerModel;
689 EXPECT_EQ(OH_NN_SUCCESS, BuildModelGraph(innerModel));
690 Compilation compilationTest(&innerModel);
691 EXPECT_EQ(true, compilationTest.IsDynamicShape());
692 }
693
694 /*
695 * @tc.name: compilation_is_dynamic_shape_003
696 * @tc.desc: Verify the return false of the IsDynamicShape function.
697 * @tc.type: FUNC
698 */
699 HWTEST_F(CompilationTest, compilation_is_dynamic_shape_003, testing::ext::TestSize.Level0)
700 {
701 InnerModel innerModel;
702 EXPECT_EQ(OH_NN_SUCCESS, BuildModelGraph(innerModel));
703 Compilation compilationTest(&innerModel);
704 MockIPreparedModel::m_ExpectRetCode = OH_NN_FAILED;
705 EXPECT_EQ(false, compilationTest.IsDynamicShape());
706 }
707
708 /*
709 * @tc.name: compilation_is_build_001
710 * @tc.desc: Verify return false of the IsBuild function.
711 * @tc.type: FUNC
712 */
713 HWTEST_F(CompilationTest, compilation_is_build_001, testing::ext::TestSize.Level0)
714 {
715 InnerModel innerModel;
716 Compilation compilationTest(&innerModel);
717 EXPECT_EQ(false, compilationTest.IsBuild());
718 }
719
720 /*
721 * @tc.name: compilation_build_001
722 * @tc.desc: Verify the build after compilation finish of the Build function.
723 * @tc.type: FUNC
724 */
725 HWTEST_F(CompilationTest, compilation_build_001, testing::ext::TestSize.Level0)
726 {
727 InnerModel innerModel;
728 EXPECT_EQ(OH_NN_SUCCESS, BuildModelGraph(innerModel));
729 Compilation compilationTest(&innerModel);
730 SetConfig(compilationTest);
731 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.Build());
732
733 OH_NN_ReturnCode ret = compilationTest.Build();
734 EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, ret);
735 }
736
737 /*
738 * @tc.name: compilation_build_002
739 * @tc.desc: Verify the not set device of the Build function.
740 * @tc.type: FUNC
741 */
742 HWTEST_F(CompilationTest, compilation_build_002, testing::ext::TestSize.Level0)
743 {
744 InnerModel innerModel;
745 EXPECT_EQ(OH_NN_SUCCESS, BuildModelGraph(innerModel));
746 Compilation compilationTest(&innerModel);
747 OH_NN_ReturnCode ret = compilationTest.Build();
748 EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, ret);
749 }
750
751 /*
752 * @tc.name: compilation_build_003
753 * @tc.desc: Verify the preparing model failed of the Build function without set cache path.
754 * @tc.type: FUNC
755 */
756 HWTEST_F(CompilationTest, compilation_build_003, testing::ext::TestSize.Level0)
757 {
758 InnerModel innerModel;
759 Compilation compilationTest(&innerModel);
760 MockIPreparedModel::m_ExpectRetCode = OH_NN_INVALID_FILE;
761 SetConfig(compilationTest);
762 OH_NN_ReturnCode ret = compilationTest.Build();
763 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
764 }
765
766 /*
767 * @tc.name: compilation_build_004
768 * @tc.desc: Verify the success of the Build function without set cache path.
769 * @tc.type: FUNC
770 */
771 HWTEST_F(CompilationTest, compilation_build_004, testing::ext::TestSize.Level0)
772 {
773 InnerModel innerModel;
774 EXPECT_EQ(OH_NN_SUCCESS, BuildModelGraph(innerModel));
775 Compilation compilationTest(&innerModel);
776 SetConfig(compilationTest);
777 OH_NN_ReturnCode ret = compilationTest.Build();
778 EXPECT_EQ(OH_NN_SUCCESS, ret);
779 }
780
781 /*
782 * @tc.name: compilation_build_005
783 * @tc.desc: Verify the preparing model failed of the Build function without cache file.
784 * @tc.type: FUNC
785 */
786 HWTEST_F(CompilationTest, compilation_build_005, testing::ext::TestSize.Level0)
787 {
788 InnerModel innerModel;
789 Compilation compilationTest(&innerModel);
790 std::size_t deviceId = 1;
791 MockIPreparedModel::m_ExpectRetCode = OH_NN_INVALID_FILE;
792 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.SetDevice(deviceId));
793 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.SetCacheDir("./", 1));
794 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.SetEnableFp16(true));
795 OH_NN_ReturnCode ret = compilationTest.Build();
796 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
797 }
798
799 /*
800 * @tc.name: compilation_build_006
801 * @tc.desc: Verify the export model cache failed of the Build function without cache file.
802 * @tc.type: FUNC
803 */
804 HWTEST_F(CompilationTest, compilation_build_006, testing::ext::TestSize.Level0)
805 {
806 InnerModel innerModel;
807 EXPECT_EQ(OH_NN_SUCCESS, BuildModelGraph(innerModel));
808 Compilation compilationTest(&innerModel);
809 std::size_t deviceId = 1;
810 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.SetDevice(deviceId));
811 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.SetCacheDir("./", 1));
812 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.SetEnableFp16(true));
813
814 MockIPreparedModel::m_ExpectRetCode = OH_NN_FAILED;
815 OH_NN_ReturnCode ret = compilationTest.Build();
816 EXPECT_EQ(OH_NN_FAILED, ret);
817 }
818
819 /*
820 * @tc.name: compilation_build_007
821 * @tc.desc: Verify the model cache file is invalid to generating cache mode of the Build function without cache file.
822 * @tc.type: FUNC
823 */
824 HWTEST_F(CompilationTest, compilation_build_007, testing::ext::TestSize.Level0)
825 {
826 InnerModel innerModel;
827 EXPECT_EQ(OH_NN_SUCCESS, BuildModelGraph(innerModel));
828 Compilation compilationTest(&innerModel);
829 std::size_t deviceId = 1;
830 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.SetDevice(deviceId));
831 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.SetCacheDir("/sys", 1));
832 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.SetEnableFp16(true));
833 OH_NN_ReturnCode ret = compilationTest.Build();
834 EXPECT_EQ(OH_NN_INVALID_FILE, ret);
835 }
836
837 /*
838 * @tc.name: compilation_build_008
839 * @tc.desc: Verify the success to generating cache mode of the Build function without cache file.
840 * @tc.type: FUNC
841 */
842 HWTEST_F(CompilationTest, compilation_build_008, testing::ext::TestSize.Level0)
843 {
844 InnerModel innerModel;
845 EXPECT_EQ(OH_NN_SUCCESS, BuildModelGraph(innerModel));
846 Compilation compilationTest(&innerModel);
847 std::size_t deviceId = 1;
848 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.SetDevice(deviceId));
849 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.SetCacheDir("./", 1));
850 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.SetEnableFp16(true));
851 OH_NN_ReturnCode ret = compilationTest.Build();
852 EXPECT_EQ(0, remove("0.nncache"));
853 EXPECT_EQ(0, remove("1.nncache"));
854 EXPECT_EQ(0, remove("cache_info.nncache"));
855 EXPECT_EQ(OH_NN_SUCCESS, ret);
856 }
857
858 /*
859 * @tc.name: compilation_build_009
860 * @tc.desc: Verify the Fail to get the content of info cache file of the Build.
861 * @tc.type: FUNC
862 */
863 HWTEST_F(CompilationTest, compilation_build_009, testing::ext::TestSize.Level0)
864 {
865 InnerModel innerModel;
866 BuildCompilation(innerModel);
867
868 Compilation compilationTest(&innerModel);
869 SetConfig(compilationTest);
870 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.SetCacheDir("./", 1));
871
872 std::ofstream createFile("cache_info.nncache");
873 createFile.close();
874 OH_NN_ReturnCode ret = compilationTest.Build();
875 EXPECT_EQ(0, remove("0.nncache"));
876 EXPECT_EQ(0, remove("1.nncache"));
877 EXPECT_EQ(0, remove("cache_info.nncache"));
878 EXPECT_EQ(OH_NN_INVALID_FILE, ret);
879 }
880
881 /*
882 * @tc.name: compilation_build_010
883 * @tc.desc: Verify the deviceId in the cache files is different from current deviceId of the Build function.
884 * @tc.type: FUNC
885 */
886 HWTEST_F(CompilationTest, compilation_build_010, testing::ext::TestSize.Level0)
887 {
888 InnerModel innerModel;
889 BuildCompilation(innerModel);
890 WriteFile(1, 4, 2);
891
892 Compilation compilationTest(&innerModel);
893 SetConfig(compilationTest);
894 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.SetCacheDir("./", 1));
895
896 OH_NN_ReturnCode ret = compilationTest.Build();
897 EXPECT_EQ(0, remove("0.nncache"));
898 EXPECT_EQ(0, remove("1.nncache"));
899 EXPECT_EQ(0, remove("cache_info.nncache"));
900 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
901 }
902
903 /*
904 * @tc.name: compilation_build_011
905 * @tc.desc: Verify the info cache file has been changed of the Build function.
906 * @tc.type: FUNC
907 */
908 HWTEST_F(CompilationTest, compilation_build_011, testing::ext::TestSize.Level0)
909 {
910 InnerModel innerModel;
911 BuildCompilation(innerModel);
912 WriteFile(1, 100, 1);
913
914 Compilation compilationTest(&innerModel);
915 SetConfig(compilationTest);
916 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.SetCacheDir("./", 1));
917
918 OH_NN_ReturnCode ret = compilationTest.Build();
919 EXPECT_EQ(0, remove("0.nncache"));
920 EXPECT_EQ(0, remove("1.nncache"));
921 EXPECT_EQ(0, remove("cache_info.nncache"));
922 EXPECT_EQ(OH_NN_INVALID_FILE, ret);
923 }
924
925 /*
926 * @tc.name: compilation_build_012
927 * @tc.desc: Verify the Preparing model failed of the Build function model version is greater than cached versio.
928 * @tc.type: FUNC
929 */
930 HWTEST_F(CompilationTest, compilation_build_012, testing::ext::TestSize.Level0)
931 {
932 InnerModel innerModel;
933 Compilation compilationTest(&innerModel);
934 MockIPreparedModel::m_ExpectRetCode = OH_NN_INVALID_FILE;
935 SetConfig(compilationTest);
936 WriteFile(0, 4, 1);
937
938 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.SetCacheDir("./", 1));
939
940 std::ofstream inFile("0.nncache", std::ios::binary | std::ios::out | std::ios::trunc);
941 inFile.close();
942
943 OH_NN_ReturnCode ret = compilationTest.Build();
944 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
945 }
946
947 /*
948 * @tc.name: compilation_build_013
949 * @tc.desc: Verify that the build function return success message with model version is greater than cached version
950 * @tc.type: FUNC
951 */
952 HWTEST_F(CompilationTest, compilation_build_013, testing::ext::TestSize.Level0)
953 {
954 InnerModel innerModel;
955 EXPECT_EQ(OH_NN_SUCCESS, BuildModelGraph(innerModel));
956 Compilation compilationTest(&innerModel);
957 SetConfig(compilationTest);
958 WriteFile(0, 1, 1);
959
960 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.SetCacheDir("./", 1));
961
962 std::ofstream inFile("0.nncache", std::ios::binary | std::ios::out | std::ios::trunc);
963 inFile.close();
964
965 OH_NN_ReturnCode ret = compilationTest.Build();
966 EXPECT_EQ(0, remove("0.nncache"));
967 EXPECT_EQ(0, remove("1.nncache"));
968 EXPECT_EQ(0, remove("cache_info.nncache"));
969 EXPECT_EQ(OH_NN_SUCCESS, ret);
970 }
971
972 /*
973 * @tc.name: compilation_build_014
974 * @tc.desc: Verify the model version is less than version cache of the Build function.
975 * @tc.type: FUNC
976 */
977 HWTEST_F(CompilationTest, compilation_build_014, testing::ext::TestSize.Level0)
978 {
979 InnerModel innerModel;
980 BuildCompilation(innerModel);
981 WriteFile(3, 4, 1);
982
983 Compilation compilationTest(&innerModel);
984 SetConfig(compilationTest);
985
986 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.SetCacheDir("./", 1));
987
988 OH_NN_ReturnCode ret = compilationTest.Build();
989 EXPECT_EQ(0, remove("0.nncache"));
990 EXPECT_EQ(0, remove("1.nncache"));
991 EXPECT_EQ(0, remove("cache_info.nncache"));
992 EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, ret);
993 }
994
995 /*
996 * @tc.name: compilation_build_015
997 * @tc.desc: Verify the checking cache model failed of the Build function with release buffer.
998 * @tc.type: FUNC
999 */
1000 HWTEST_F(CompilationTest, compilation_build_015, testing::ext::TestSize.Level0)
1001 {
1002 InnerModel innerModel;
1003 BuildCompilation(innerModel);
1004 EXPECT_EQ(0, remove("1.nncache"));
1005
1006 Compilation compilationTest(&innerModel);
1007 SetConfig(compilationTest);
1008 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.SetCacheDir("./", 1));
1009
1010 OH_NN_ReturnCode ret = compilationTest.Build();
1011 EXPECT_EQ(0, remove("0.nncache"));
1012 EXPECT_EQ(0, remove("cache_info.nncache"));
1013 EXPECT_EQ(OH_NN_INVALID_FILE, ret);
1014 }
1015
1016 /*
1017 * @tc.name: compilation_build_016
1018 * @tc.desc: Verify the get cache file length of the Build function.
1019 * @tc.type: FUNC
1020 */
1021 HWTEST_F(CompilationTest, compilation_build_016, testing::ext::TestSize.Level0)
1022 {
1023 InnerModel innerModel;
1024 BuildCompilation(innerModel);
1025
1026 Compilation compilationTest(&innerModel);
1027 SetConfig(compilationTest);
1028 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.SetCacheDir("./", 1));
1029
1030 std::ofstream inFile("0.nncache", std::ios::binary | std::ios::out | std::ios::trunc);
1031 inFile.close();
1032
1033 OH_NN_ReturnCode ret = compilationTest.Build();
1034 EXPECT_EQ(0, remove("0.nncache"));
1035 EXPECT_EQ(0, remove("1.nncache"));
1036 EXPECT_EQ(0, remove("cache_info.nncache"));
1037 EXPECT_EQ(OH_NN_INVALID_FILE, ret);
1038 }
1039
1040 /*
1041 * @tc.name: compilation_build_017
1042 * @tc.desc: Verify the fail to create file buffer of the Build function.
1043 * @tc.type: FUNC
1044 */
1045 HWTEST_F(CompilationTest, compilation_build_017, testing::ext::TestSize.Level0)
1046 {
1047 InnerModel innerModel;
1048 BuildCompilation(innerModel);
1049
1050 Compilation compilationTest(&innerModel);
1051 SetConfig(compilationTest);
1052 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.SetCacheDir("./", 1));
1053
1054 MockIPreparedModel::m_ExpectRetCode = OH_NN_NULL_PTR;
1055 OH_NN_ReturnCode ret = compilationTest.Build();
1056 EXPECT_EQ(0, remove("0.nncache"));
1057 EXPECT_EQ(0, remove("1.nncache"));
1058 EXPECT_EQ(0, remove("cache_info.nncache"));
1059 EXPECT_EQ(OH_NN_NULL_PTR, ret);
1060 }
1061
1062 /*
1063 * @tc.name: compilation_build_018
1064 * @tc.desc: Verify the cache model file has been changed of the Build function.
1065 * @tc.type: FUNC
1066 */
1067 HWTEST_F(CompilationTest, compilation_build_018, testing::ext::TestSize.Level0)
1068 {
1069 InnerModel innerModel;
1070 BuildCompilation(innerModel);
1071
1072 Compilation compilationTest(&innerModel);
1073 SetConfig(compilationTest);
1074 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.SetCacheDir("./", 1));
1075
1076 uint64_t version = 1;
1077 uint64_t fileNumber = 1;
1078 std::size_t cacheDeviceId = 1;
1079 uint64_t cacheInfo[7] = {};
1080 auto cacheInfoPtr = cacheInfo;
1081 *cacheInfoPtr++ = fileNumber;
1082 *cacheInfoPtr++ = version;
1083 *cacheInfoPtr++ = cacheDeviceId;
1084 for (uint64_t i = 0; i < 4; ++i) {
1085 *cacheInfoPtr++ = i;
1086 }
1087
1088 std::ofstream onFile("0.nncache", std::ios::binary | std::ios::out | std::ios::trunc);
1089 onFile.write(reinterpret_cast<const char*>(cacheInfo), 7 * sizeof(uint64_t));
1090 onFile.close();
1091
1092 OH_NN_ReturnCode ret = compilationTest.Build();
1093 EXPECT_EQ(0, remove("0.nncache"));
1094 EXPECT_EQ(0, remove("1.nncache"));
1095 EXPECT_EQ(0, remove("cache_info.nncache"));
1096 EXPECT_EQ(OH_NN_INVALID_FILE, ret);
1097 }
1098
1099 /*
1100 * @tc.name: compilation_build_019
1101 * @tc.desc: Verify the preparing model from cache failed of the Build function with load cache build.
1102 * @tc.type: FUNC
1103 */
1104 HWTEST_F(CompilationTest, compilation_build_019, testing::ext::TestSize.Level0)
1105 {
1106 InnerModel innerModel;
1107 BuildCompilation(innerModel);
1108
1109 Compilation compilationTest(&innerModel);
1110 SetConfig(compilationTest);
1111 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.SetCacheDir("./", 1));
1112
1113 MockIPreparedModel::m_ExpectRetCode = OH_NN_FAILED;
1114 OH_NN_ReturnCode ret = compilationTest.Build();
1115 EXPECT_EQ(0, remove("0.nncache"));
1116 EXPECT_EQ(0, remove("1.nncache"));
1117 EXPECT_EQ(0, remove("cache_info.nncache"));
1118 EXPECT_EQ(OH_NN_FAILED, ret);
1119 }
1120
1121 /*
1122 * @tc.name: compilation_build_020
1123 * @tc.desc: Verify the success of the Build function with load cache build.
1124 * @tc.type: FUNC
1125 */
1126 HWTEST_F(CompilationTest, compilation_build_020, testing::ext::TestSize.Level0)
1127 {
1128 InnerModel innerModel;
1129 BuildCompilation(innerModel);
1130
1131 Compilation compilationTest(&innerModel);
1132 SetConfig(compilationTest);
1133 EXPECT_EQ(OH_NN_SUCCESS, compilationTest.SetCacheDir("./", 1));
1134
1135 OH_NN_ReturnCode ret = compilationTest.Build();
1136 EXPECT_EQ(0, remove("0.nncache"));
1137 EXPECT_EQ(0, remove("1.nncache"));
1138 EXPECT_EQ(0, remove("cache_info.nncache"));
1139 EXPECT_EQ(OH_NN_SUCCESS, ret);
1140 }
1141 } // namespace UnitTest
1142 } // namespace NeuralNetworkRuntime
1143 } // namespace OHOS
1144