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