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 <sys/mman.h>
17 #include <unistd.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21 #include <cstdlib>
22 
23 #include <hdf_base.h>
24 #include <refbase.h>
25 #include <gtest/gtest.h>
26 #include <gmock/gmock.h>
27 
28 #include "hdi_device_v1_0.h"
29 #include "test/unittest/common/v1_0/mock_idevice.h"
30 #include "test/unittest/common/file_utils.h"
31 
32 using namespace testing;
33 using namespace testing::ext;
34 using namespace OHOS::NeuralNetworkRuntime;
35 
36 namespace mindspore {
37 namespace lite {
MindIR_LiteGraph_To_Model(const LiteGraph * lite_graph,const OHOS::HDI::Nnrt::V1_0::SharedBuffer & buffer)38 OHOS::HDI::Nnrt::V1_0::Model* MindIR_LiteGraph_To_Model(const LiteGraph* lite_graph,
39     const OHOS::HDI::Nnrt::V1_0::SharedBuffer& buffer)
40 {
41     return new (std::nothrow) OHOS::HDI::Nnrt::V1_0::Model();
42 }
43 
MindIR_Model_Destroy(OHOS::HDI::Nnrt::V1_0::Model ** model)44 void MindIR_Model_Destroy(OHOS::HDI::Nnrt::V1_0::Model** model)
45 {
46     if ((model != nullptr) && (*model != nullptr)) {
47         delete *model;
48         *model = nullptr;
49     }
50 }
51 
MindIR_LiteGraph_GetConstTensorSize(const mindspore::lite::LiteGraph * lite_graph)52 size_t MindIR_LiteGraph_GetConstTensorSize(const mindspore::lite::LiteGraph* lite_graph)
53 {
54     return 1;
55 }
56 }
57 }
58 
59 namespace OHOS {
60 namespace NeuralNetworkRuntime {
61 namespace UnitTest {
62 class HDIDeviceTest : public testing::Test {
63 protected:
64     void GetBuffer(void*& buffer, size_t length);
65     OH_NN_ReturnCode PrepareModel(int32_t allocBufferType, int32_t prepareType);
66 };
67 
GetBuffer(void * & buffer,size_t length)68 void HDIDeviceTest::GetBuffer(void*& buffer, size_t length)
69 {
70     std::string data = "ABCD";
71     const size_t dataLength = 100;
72     data.resize(dataLength, '+');
73 
74     std::string filename = "/data/log/memory-001.dat";
75     FileUtils fileUtils(filename);
76     fileUtils.WriteFile(data);
77 
78     int fd = open(filename.c_str(), O_RDWR);
79     EXPECT_NE(fd, -1);
80 
81     const auto &memoryManager = MemoryManager::GetInstance();
82     buffer = memoryManager->MapMemory(fd, length);
83     EXPECT_NE(buffer, nullptr);
84 
85     const char* result = static_cast<const char*>(buffer);
86     int index = 0;
87     EXPECT_EQ('A', result[index++]);
88     EXPECT_EQ('B', result[index++]);
89     EXPECT_EQ('C', result[index++]);
90     EXPECT_EQ('D', result[index++]);
91     close(fd);
92 }
93 
PrepareModel(int32_t allocBufferType,int32_t prepareType)94 OH_NN_ReturnCode HDIDeviceTest::PrepareModel(int32_t allocBufferType, int32_t prepareType)
95 {
96     std::shared_ptr<mindspore::lite::LiteGraph> model = std::make_shared<mindspore::lite::LiteGraph>();
97     OHOS::sptr<V1_0::MockIDevice> sp = OHOS::sptr<V1_0::MockIDevice>(new (std::nothrow) V1_0::MockIDevice());
98     EXPECT_NE(sp, nullptr);
99 
100     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(sp);
101     EXPECT_NE(hdiDevice, nullptr);
102 
103     V1_0::SharedBuffer buffer {1, 1, 0, 1};
104     EXPECT_CALL(*sp, AllocateBuffer(::testing::_, ::testing::_))
105         .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<1>(buffer), ::testing::Return(allocBufferType)));
106 
107     std::shared_ptr<PreparedModel> preparedModel;
108     const int position = 2;
109     OHOS::sptr<V1_0::IPreparedModel> iPreparedModel =
110         OHOS::sptr<V1_0::MockIPreparedModel>(new (std::nothrow) V1_0::MockIPreparedModel());
111     EXPECT_CALL(*sp, PrepareModel(::testing::_, ::testing::_, ::testing::_))
112         .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<position>(iPreparedModel),
113         ::testing::Return(prepareType)));
114 
115     ModelConfig config;
116     OH_NN_ReturnCode result = hdiDevice->PrepareModel(model, config, preparedModel);
117     return result;
118 }
119 
120 /* *
121  * @tc.name: hdidevice_constructor_001
122  * @tc.desc: Verify the Constructor function return object success.
123  * @tc.type: FUNC
124  */
125 HWTEST_F(HDIDeviceTest, hdidevice_constructor_001, TestSize.Level0)
126 {
127     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
128     EXPECT_NE(device, nullptr);
129     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
130     EXPECT_NE(hdiDevice, nullptr);
131 }
132 
133 /* *
134  * @tc.name: hdidevice_getdevicename_001
135  * @tc.desc: Verify the GetDeviceName function validate device name success.
136  * @tc.type: FUNC
137  */
138 HWTEST_F(HDIDeviceTest, hdidevice_getdevicename_001, TestSize.Level0)
139 {
140     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
141     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
142     EXPECT_NE(hdiDevice, nullptr);
143     std::string deviceName = "MockDevice";
144     EXPECT_CALL(*((V1_0::MockIDevice *)device.GetRefPtr()), GetDeviceName(::testing::_))
145         .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(deviceName), ::testing::Return(HDF_SUCCESS)));
146 
147     const std::string expectDeviceName = "MockDevice";
148     std::string newDeviceName = "";
149     OH_NN_ReturnCode result = hdiDevice->GetDeviceName(newDeviceName);
150     EXPECT_EQ(OH_NN_SUCCESS, result);
151     EXPECT_EQ(expectDeviceName, newDeviceName);
152 }
153 
154 /* *
155  * @tc.name: hdidevice_getdevicename_002
156  * @tc.desc: Verify the GetDeviceName function return unavailable device.
157  * @tc.type: FUNC
158  */
159 HWTEST_F(HDIDeviceTest, hdidevice_getdevicename_002, TestSize.Level0)
160 {
161     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
162     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
163     EXPECT_NE(hdiDevice, nullptr);
164     std::string deviceName = "MockDevice";
165     EXPECT_CALL(*((V1_0::MockIDevice *)device.GetRefPtr()), GetDeviceName(::testing::_))
166         .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(deviceName), ::testing::Return(HDF_FAILURE)));
167     OH_NN_ReturnCode result = hdiDevice->GetDeviceName(deviceName);
168     EXPECT_EQ(OH_NN_UNAVAILABLE_DEVICE, result);
169 }
170 
171 /* *
172  * @tc.name: hdidevice_getvendorname_001
173  * @tc.desc: Verify the GetVendorName function validate vendor name success.
174  * @tc.type: FUNC
175  */
176 HWTEST_F(HDIDeviceTest, hdidevice_getvendorname_001, TestSize.Level0)
177 {
178     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
179     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
180     EXPECT_NE(hdiDevice, nullptr);
181     std::string vendorName = "MockVendor";
182     EXPECT_CALL(*((V1_0::MockIDevice *)device.GetRefPtr()), GetVendorName(::testing::_))
183         .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(vendorName), ::testing::Return(HDF_SUCCESS)));
184 
185     const std::string expectDeviceName = "MockVendor";
186     std::string newVendorName = "";
187     OH_NN_ReturnCode result = hdiDevice->GetVendorName(newVendorName);
188     EXPECT_EQ(OH_NN_SUCCESS, result);
189     EXPECT_EQ(expectDeviceName, newVendorName);
190 }
191 
192 /* *
193  * @tc.name: hdidevice_getvendorname_002
194  * @tc.desc: Verify the GetVendorName function return unavailable device.
195  * @tc.type: FUNC
196  */
197 HWTEST_F(HDIDeviceTest, hdidevice_getvendorname_002, TestSize.Level0)
198 {
199     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
200     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
201     EXPECT_NE(hdiDevice, nullptr);
202     std::string vendorName = "MockVendor";
203     EXPECT_CALL(*((V1_0::MockIDevice *)device.GetRefPtr()), GetVendorName(::testing::_))
204         .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(vendorName), ::testing::Return(HDF_FAILURE)));
205     OH_NN_ReturnCode result = hdiDevice->GetVendorName(vendorName);
206     EXPECT_EQ(OH_NN_UNAVAILABLE_DEVICE, result);
207 }
208 
209 /* *
210  * @tc.name: hdidevice_getversion_001
211  * @tc.desc: Verify the GetVersion function validate vendor name success.
212  * @tc.type: FUNC
213  */
214 HWTEST_F(HDIDeviceTest, hdidevice_getversion_001, TestSize.Level0)
215 {
216     LOGE("GetVersion hdidevice_getversion_001");
217     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
218     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
219     EXPECT_NE(hdiDevice, nullptr);
220     std::string vendorName = "MockVendor";
221     EXPECT_CALL(*((V1_0::MockIDevice *)device.GetRefPtr()), GetVersion(::testing::_, ::testing::_))
222         .WillRepeatedly(::testing::Return(HDF_SUCCESS));
223 
224     const std::string expectDeviceName = "MockVendor";
225     std::string newVendorName = "";
226     OH_NN_ReturnCode result = hdiDevice->GetVersion(newVendorName);
227     EXPECT_EQ(OH_NN_SUCCESS, result);
228 }
229 
230 /* *
231  * @tc.name: hdidevice_getversion_002
232  * @tc.desc: Verify the GetVersion function return unavailable device.
233  * @tc.type: FUNC
234  */
235 HWTEST_F(HDIDeviceTest, hdidevice_getversion_002, TestSize.Level0)
236 {
237     LOGE("GetVersion hdidevice_getversion_002");
238     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
239     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
240     EXPECT_NE(hdiDevice, nullptr);
241     std::string vendorName = "MockVendor";
242     EXPECT_CALL(*((V1_0::MockIDevice *)device.GetRefPtr()), GetVersion(::testing::_, ::testing::_))
243         .WillRepeatedly(::testing::Return(HDF_FAILURE));
244     OH_NN_ReturnCode result = hdiDevice->GetVersion(vendorName);
245     EXPECT_EQ(OH_NN_UNAVAILABLE_DEVICE, result);
246 }
247 
248 /* *
249  * @tc.name: hdidevice_getdevicetype_001
250  * @tc.desc: Verify the GetDeviceType function validate device type success.
251  * @tc.type: FUNC
252  */
253 HWTEST_F(HDIDeviceTest, hdidevice_getdevicetype_001, TestSize.Level0)
254 {
255     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
256     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
257     EXPECT_NE(hdiDevice, nullptr);
258     V1_0::DeviceType iDeviceType = V1_0::DeviceType::CPU;
259     EXPECT_CALL(*((V1_0::MockIDevice *)device.GetRefPtr()), GetDeviceType(::testing::_))
260         .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(iDeviceType), ::testing::Return(HDF_SUCCESS)));
261 
262     OH_NN_DeviceType expectDeviceType = OH_NN_CPU;
263     OH_NN_DeviceType newDeviceType = OH_NN_CPU;
264     OH_NN_ReturnCode result = hdiDevice->GetDeviceType(newDeviceType);
265     EXPECT_EQ(OH_NN_SUCCESS, result);
266     EXPECT_EQ(expectDeviceType, newDeviceType);
267 }
268 
269 /* *
270  * @tc.name: hdidevice_getdevicetype_002
271  * @tc.desc: Verify the GetDeviceType function return unavailable device.
272  * @tc.type: FUNC
273  */
274 HWTEST_F(HDIDeviceTest, hdidevice_getdevicetype_002, TestSize.Level0)
275 {
276     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
277     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
278     EXPECT_NE(hdiDevice, nullptr);
279 
280     OH_NN_DeviceType deviceType = OH_NN_CPU;
281     V1_0::DeviceType iDeviceType = V1_0::DeviceType::CPU;
282     EXPECT_CALL(*((V1_0::MockIDevice *)device.GetRefPtr()), GetDeviceType(::testing::_))
283         .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(iDeviceType), ::testing::Return(HDF_FAILURE)));
284     OH_NN_ReturnCode result = hdiDevice->GetDeviceType(deviceType);
285     EXPECT_EQ(OH_NN_UNAVAILABLE_DEVICE, result);
286 }
287 
288 /* *
289  * @tc.name: hdidevice_getdevicetype_003
290  * @tc.desc: Verify the GetDeviceType function return unavailable device.
291  * @tc.type: FUNC
292  */
293 HWTEST_F(HDIDeviceTest, hdidevice_getdevicetype_003, TestSize.Level0)
294 {
295     LOGE("GetDeviceType hdidevice_getdevicetype_003");
296     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
297     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
298     EXPECT_NE(hdiDevice, nullptr);
299 
300     OH_NN_DeviceType deviceType = OH_NN_CPU;
301     EXPECT_CALL(*((V1_0::MockIDevice *)device.GetRefPtr()), GetDeviceType(::testing::_))
__anonfb6d8e810102(V1_0::DeviceType& type) 302         .WillRepeatedly(Invoke([](V1_0::DeviceType& type) {
303                 // 这里直接修改传入的引用参数
304                 type = V1_0::DeviceType::GPU;
305                 return OH_NN_SUCCESS; // 假设成功的状态码
306             }));
307     OH_NN_ReturnCode result = hdiDevice->GetDeviceType(deviceType);
308     EXPECT_EQ(OH_NN_SUCCESS, result);
309 }
310 
311 /* *
312  * @tc.name: hdidevice_getdevicetype_004
313  * @tc.desc: Verify the GetDeviceType function return unavailable device.
314  * @tc.type: FUNC
315  */
316 HWTEST_F(HDIDeviceTest, hdidevice_getdevicetype_004, TestSize.Level0)
317 {
318     LOGE("GetDeviceType hdidevice_getdevicetype_004");
319     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
320     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
321     EXPECT_NE(hdiDevice, nullptr);
322 
323     OH_NN_DeviceType deviceType = OH_NN_CPU;
324     EXPECT_CALL(*((V1_0::MockIDevice *)device.GetRefPtr()), GetDeviceType(::testing::_))
__anonfb6d8e810202(V1_0::DeviceType& type) 325         .WillRepeatedly(Invoke([](V1_0::DeviceType& type) {
326                 // 这里直接修改传入的引用参数
327                 type = V1_0::DeviceType::ACCELERATOR;
328                 return OH_NN_SUCCESS; // 假设成功的状态码
329             }));
330     OH_NN_ReturnCode result = hdiDevice->GetDeviceType(deviceType);
331     EXPECT_EQ(OH_NN_SUCCESS, result);
332 }
333 
334 /* *
335  * @tc.name: hdidevice_getdevicetype_005
336  * @tc.desc: Verify the GetDeviceType function return unavailable device.
337  * @tc.type: FUNC
338  */
339 HWTEST_F(HDIDeviceTest, hdidevice_getdevicetype_005, TestSize.Level0)
340 {
341     LOGE("GetDeviceType hdidevice_getdevicetype_005");
342     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
343     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
344     EXPECT_NE(hdiDevice, nullptr);
345 
346     OH_NN_DeviceType deviceType = OH_NN_CPU;
347     EXPECT_CALL(*((V1_0::MockIDevice *)device.GetRefPtr()), GetDeviceType(::testing::_))
__anonfb6d8e810302(V1_0::DeviceType& type) 348         .WillRepeatedly(Invoke([](V1_0::DeviceType& type) {
349                 // 这里直接修改传入的引用参数
350                 type = V1_0::DeviceType::OTHER;
351                 return OH_NN_SUCCESS; // 假设成功的状态码
352             }));
353     OH_NN_ReturnCode result = hdiDevice->GetDeviceType(deviceType);
354     EXPECT_EQ(OH_NN_SUCCESS, result);
355 }
356 
357 /* *
358  * @tc.name: hdidevice_getdevicestatus_001
359  * @tc.desc: Verify the GetDeviceStatus function validate device status success.
360  * @tc.type: FUNC
361  */
362 HWTEST_F(HDIDeviceTest, hdidevice_getdevicestatus_001, TestSize.Level0)
363 {
364     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
365     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
366     EXPECT_NE(hdiDevice, nullptr);
367 
368     V1_0::DeviceStatus iDeviceStatus = V1_0::DeviceStatus::AVAILABLE;
369     EXPECT_CALL(*((V1_0::MockIDevice *)device.GetRefPtr()), GetDeviceStatus(::testing::_))
370         .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(iDeviceStatus), ::testing::Return(HDF_SUCCESS)));
371 
372     const DeviceStatus expectDeviceStatus = AVAILABLE;
373     DeviceStatus newDeviceStatus = AVAILABLE;
374     OH_NN_ReturnCode result = hdiDevice->GetDeviceStatus(newDeviceStatus);
375     EXPECT_EQ(OH_NN_SUCCESS, result);
376     EXPECT_EQ(expectDeviceStatus, newDeviceStatus);
377 }
378 
379 /* *
380  * @tc.name: hdidevice_getdevicestatus_002
381  * @tc.desc: Verify the GetDeviceStatus function return unavailable device.
382  * @tc.type: FUNC
383  */
384 HWTEST_F(HDIDeviceTest, hdidevice_getdevicestatus_002, TestSize.Level0)
385 {
386     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
387     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
388     EXPECT_NE(hdiDevice, nullptr);
389     DeviceStatus deviceStatus = AVAILABLE;
390     V1_0::DeviceStatus iDeviceStatus = V1_0::DeviceStatus::AVAILABLE;
391     EXPECT_CALL(*((V1_0::MockIDevice *)device.GetRefPtr()), GetDeviceStatus(::testing::_))
392         .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(iDeviceStatus), ::testing::Return(HDF_FAILURE)));
393     OH_NN_ReturnCode result = hdiDevice->GetDeviceStatus(deviceStatus);
394     EXPECT_EQ(OH_NN_UNAVAILABLE_DEVICE, result);
395 }
396 
397 /* *
398  * @tc.name: hdidevice_getdevicestatus_003
399  * @tc.desc: Verify the GetDeviceStatus function validate device status success.
400  * @tc.type: FUNC
401  */
402 HWTEST_F(HDIDeviceTest, hdidevice_getdevicestatus_003, TestSize.Level0)
403 {
404     LOGE("GetDeviceStatus hdidevice_getdevicestatus_003");
405     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
406     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
407     EXPECT_NE(hdiDevice, nullptr);
408 
409     EXPECT_CALL(*((V1_0::MockIDevice *)device.GetRefPtr()), GetDeviceStatus(::testing::_))
__anonfb6d8e810402(V1_0::DeviceStatus& status) 410         .WillRepeatedly(Invoke([](V1_0::DeviceStatus& status) {
411                 // 这里直接修改传入的引用参数
412                 status = V1_0::DeviceStatus::BUSY;
413                 return OH_NN_SUCCESS; // 假设成功的状态码
414             }));
415 
416     DeviceStatus newDeviceStatus = AVAILABLE;
417     OH_NN_ReturnCode result = hdiDevice->GetDeviceStatus(newDeviceStatus);
418     EXPECT_EQ(OH_NN_SUCCESS, result);
419 }
420 
421 /* *
422  * @tc.name: hdidevice_getdevicestatus_004
423  * @tc.desc: Verify the GetDeviceStatus function validate device status success.
424  * @tc.type: FUNC
425  */
426 HWTEST_F(HDIDeviceTest, hdidevice_getdevicestatus_004, TestSize.Level0)
427 {
428     LOGE("GetDeviceStatus hdidevice_getdevicestatus_004");
429     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
430     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
431     EXPECT_NE(hdiDevice, nullptr);
432 
433     EXPECT_CALL(*((V1_0::MockIDevice *)device.GetRefPtr()), GetDeviceStatus(::testing::_))
__anonfb6d8e810502(V1_0::DeviceStatus& status) 434         .WillRepeatedly(Invoke([](V1_0::DeviceStatus& status) {
435                 // 这里直接修改传入的引用参数
436                 status = V1_0::DeviceStatus::OFFLINE;
437                 return OH_NN_SUCCESS; // 假设成功的状态码
438             }));
439 
440     DeviceStatus newDeviceStatus = AVAILABLE;
441     OH_NN_ReturnCode result = hdiDevice->GetDeviceStatus(newDeviceStatus);
442     EXPECT_EQ(OH_NN_SUCCESS, result);
443 }
444 
445 /* *
446  * @tc.name: hdidevice_getdevicestatus_005
447  * @tc.desc: Verify the GetDeviceStatus function validate device status success.
448  * @tc.type: FUNC
449  */
450 HWTEST_F(HDIDeviceTest, hdidevice_getdevicestatus_005, TestSize.Level0)
451 {
452     LOGE("GetDeviceStatus hdidevice_getdevicestatus_005");
453     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
454     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
455     EXPECT_NE(hdiDevice, nullptr);
456 
457     EXPECT_CALL(*((V1_0::MockIDevice *)device.GetRefPtr()), GetDeviceStatus(::testing::_))
__anonfb6d8e810602(V1_0::DeviceStatus& status) 458         .WillRepeatedly(Invoke([](V1_0::DeviceStatus& status) {
459                 // 这里直接修改传入的引用参数
460                 status = V1_0::DeviceStatus::UNKNOWN;
461                 return OH_NN_SUCCESS; // 假设成功的状态码
462             }));
463 
464     DeviceStatus newDeviceStatus = AVAILABLE;
465     OH_NN_ReturnCode result = hdiDevice->GetDeviceStatus(newDeviceStatus);
466     EXPECT_EQ(OH_NN_SUCCESS, result);
467 }
468 
469 /* *
470  * @tc.name: hdidevice_getsupportedoperation_001
471  * @tc.desc: Verify the GetSupportedOperation function return success.
472  * @tc.type: FUNC
473  */
474 HWTEST_F(HDIDeviceTest, hdidevice_getsupportedoperation_001, TestSize.Level0)
475 {
476     std::vector<bool> ops {true};
477     std::shared_ptr<mindspore::lite::LiteGraph> model = std::make_shared<mindspore::lite::LiteGraph>();
478     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
479     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
480     EXPECT_NE(hdiDevice, nullptr);
481 
482     V1_0::SharedBuffer buffer {1, 1, 0, 1};
483     EXPECT_CALL(*((V1_0::MockIDevice *)device.GetRefPtr()), AllocateBuffer(::testing::_, ::testing::_))
484         .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<1>(buffer), ::testing::Return(HDF_SUCCESS)));
485 
486     EXPECT_CALL(*((V1_0::MockIDevice *)device.GetRefPtr()), GetSupportedOperation(::testing::_, ::testing::_))
487         .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<1>(ops), ::testing::Return(HDF_SUCCESS)));
488 
489     std::vector<bool> newOps {true};
490     const std::vector<bool> expectOps {true};
491     OH_NN_ReturnCode result = hdiDevice->GetSupportedOperation(model, newOps);
492     EXPECT_EQ(OH_NN_FAILED, result);
493     auto expectOpsSize = expectOps.size();
494     for (size_t i = 0; i < expectOpsSize; ++i) {
495         EXPECT_EQ(expectOps[i], newOps[i]);
496     }
497 }
498 
499 /* *
500  * @tc.name: hdidevice_getsupportedoperation_002
501  * @tc.desc: Verify the GetSupportedOperation function return failed in case of allocate buffer failure.
502  * @tc.type: FUNC
503  */
504 HWTEST_F(HDIDeviceTest, hdidevice_getsupportedoperation_002, TestSize.Level0)
505 {
506     std::vector<bool> ops;
507     std::shared_ptr<mindspore::lite::LiteGraph> model = std::make_shared<mindspore::lite::LiteGraph>();
508     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
509     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
510     EXPECT_NE(hdiDevice, nullptr);
511 
512     V1_0::SharedBuffer buffer {1, 1, 0, 1};
513     EXPECT_CALL(*((V1_0::MockIDevice *)device.GetRefPtr()), AllocateBuffer(::testing::_, ::testing::_))
514         .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<1>(buffer), ::testing::Return(HDF_FAILURE)));
515 
516     OH_NN_ReturnCode result = hdiDevice->GetSupportedOperation(model, ops);
517     EXPECT_EQ(OH_NN_FAILED, result);
518 }
519 
520 /* *
521  * @tc.name: hdidevice_getsupportedoperation_003
522  * @tc.desc: Verify the GetSupportedOperation function return nullptr.
523  * @tc.type: FUNC
524  */
525 HWTEST_F(HDIDeviceTest, hdidevice_getsupportedoperation_003, TestSize.Level0)
526 {
527     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
528     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
529     EXPECT_NE(hdiDevice, nullptr);
530 
531     std::shared_ptr<const mindspore::lite::LiteGraph> model = nullptr;
532     std::vector<bool> ops;
533     OH_NN_ReturnCode result = hdiDevice->GetSupportedOperation(model, ops);
534     EXPECT_EQ(OH_NN_NULL_PTR, result);
535 }
536 
537 /* *
538  * @tc.name: hdidevice_getsupportedoperation_004
539  * @tc.desc: Verify the GetSupportedOperation function return unavalidable device.
540  * @tc.type: FUNC
541  */
542 HWTEST_F(HDIDeviceTest, hdidevice_getsupportedoperation_004, TestSize.Level0)
543 {
544     std::vector<bool> ops {true};
545     std::shared_ptr<mindspore::lite::LiteGraph> model = std::make_shared<mindspore::lite::LiteGraph>();
546     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
547     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
548     EXPECT_NE(hdiDevice, nullptr);
549 
550     V1_0::SharedBuffer buffer {2, 1, 0, 1};
551     EXPECT_CALL(*((V1_0::MockIDevice *)device.GetRefPtr()), AllocateBuffer(::testing::_, ::testing::_))
552         .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<1>(buffer), ::testing::Return(HDF_SUCCESS)));
553 
554     EXPECT_CALL(*((V1_0::MockIDevice *)device.GetRefPtr()), GetSupportedOperation(::testing::_, ::testing::_))
555         .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<1>(ops), ::testing::Return(HDF_FAILURE)));
556 
557     std::vector<bool> newOps {true};
558     OH_NN_ReturnCode result = hdiDevice->GetSupportedOperation(model, newOps);
559     EXPECT_EQ(OH_NN_FAILED, result);
560 }
561 
562 /* *
563  * @tc.name: hdidevice_isfloat16precisionsupported_001
564  * @tc.desc: Verify the IsFloat16PrecisionSupported function return success.
565  * @tc.type: FUNC
566  */
567 HWTEST_F(HDIDeviceTest, hdidevice_isfloat16precisionsupported_001, TestSize.Level0)
568 {
569     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
570     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
571     EXPECT_NE(hdiDevice, nullptr);
572 
573     bool isSupported = false;
574     EXPECT_CALL(*((V1_0::MockIDevice *)device.GetRefPtr()), IsFloat16PrecisionSupported(::testing::_))
575         .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(isSupported), ::testing::Return(HDF_SUCCESS)));
576     OH_NN_ReturnCode result = hdiDevice->IsFloat16PrecisionSupported(isSupported);
577     EXPECT_EQ(OH_NN_SUCCESS, result);
578 }
579 
580 /* *
581  * @tc.name: hdidevice_isfloat16precisionsupported_002
582  * @tc.desc: Verify the IsFloat16PrecisionSupported function return unavailable device.
583  * @tc.type: FUNC
584  */
585 HWTEST_F(HDIDeviceTest, hdidevice_isfloat16precisionsupported_002, TestSize.Level0)
586 {
587     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
588     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
589     EXPECT_NE(hdiDevice, nullptr);
590 
591     bool isSupported = false;
592     EXPECT_CALL(*((V1_0::MockIDevice *)device.GetRefPtr()), IsFloat16PrecisionSupported(::testing::_))
593         .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(isSupported), ::testing::Return(HDF_FAILURE)));
594     OH_NN_ReturnCode result = hdiDevice->IsFloat16PrecisionSupported(isSupported);
595     EXPECT_EQ(OH_NN_UNAVAILABLE_DEVICE, result);
596 }
597 
598 /* *
599  * @tc.name: hdidevice_isperformancemodesupported_001
600  * @tc.desc: Verify the IsPerformanceModeSupported function return success.
601  * @tc.type: FUNC
602  */
603 HWTEST_F(HDIDeviceTest, hdidevice_isperformancemodesupported_001, TestSize.Level0)
604 {
605     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
606     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
607     EXPECT_NE(hdiDevice, nullptr);
608 
609     bool isSupported = false;
610     EXPECT_CALL(*((V1_0::MockIDevice *)device.GetRefPtr()), IsPerformanceModeSupported(::testing::_))
611         .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(isSupported), ::testing::Return(HDF_SUCCESS)));
612 
613     bool newIsSupported = false;
614     const bool expectIsSupported = false;
615     OH_NN_ReturnCode result = hdiDevice->IsPerformanceModeSupported(newIsSupported);
616     EXPECT_EQ(OH_NN_SUCCESS, result);
617     EXPECT_EQ(expectIsSupported, newIsSupported);
618 }
619 
620 /* *
621  * @tc.name: hdidevice_isperformancemodesupported_002
622  * @tc.desc: Verify the IsPerformanceModeSupported function return unavailable device.
623  * @tc.type: FUNC
624  */
625 HWTEST_F(HDIDeviceTest, hdidevice_isperformancemodesupported_002, TestSize.Level0)
626 {
627     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
628     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
629     EXPECT_NE(hdiDevice, nullptr);
630 
631     bool isSupported = false;
632     EXPECT_CALL(*((V1_0::MockIDevice *)device.GetRefPtr()), IsPerformanceModeSupported(::testing::_))
633         .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(isSupported), ::testing::Return(HDF_FAILURE)));
634     OH_NN_ReturnCode result = hdiDevice->IsPerformanceModeSupported(isSupported);
635     EXPECT_EQ(OH_NN_UNAVAILABLE_DEVICE, result);
636 }
637 
638 /* *
639  * @tc.name: hdidevice_isprioritysupported_001
640  * @tc.desc: Verify the IsPrioritySupported function return success.
641  * @tc.type: FUNC
642  */
643 HWTEST_F(HDIDeviceTest, hdidevice_isprioritysupported_001, TestSize.Level0)
644 {
645     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
646     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
647     EXPECT_NE(hdiDevice, nullptr);
648 
649     bool isSupported = false;
650     EXPECT_CALL(*((V1_0::MockIDevice *)device.GetRefPtr()), IsPrioritySupported(::testing::_))
651         .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(isSupported), ::testing::Return(HDF_SUCCESS)));
652 
653     bool newIsSupported = false;
654     bool expectIsSupported = false;
655     OH_NN_ReturnCode result = hdiDevice->IsPrioritySupported(newIsSupported);
656     EXPECT_EQ(OH_NN_SUCCESS, result);
657     EXPECT_EQ(newIsSupported, expectIsSupported);
658 }
659 
660 /* *
661  * @tc.name: hdidevice_isprioritysupported_002
662  * @tc.desc: Verify the IsPrioritySupported function return unavailable device.
663  * @tc.type: FUNC
664  */
665 HWTEST_F(HDIDeviceTest, hdidevice_isprioritysupported_002, TestSize.Level0)
666 {
667     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
668     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
669     EXPECT_NE(hdiDevice, nullptr);
670 
671     bool isSupported = false;
672     EXPECT_CALL(*((V1_0::MockIDevice *)device.GetRefPtr()), IsPrioritySupported(::testing::_))
673         .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(isSupported), ::testing::Return(HDF_FAILURE)));
674     OH_NN_ReturnCode result = hdiDevice->IsPrioritySupported(isSupported);
675     EXPECT_EQ(OH_NN_UNAVAILABLE_DEVICE, result);
676 }
677 
678 /* *
679  * @tc.name: hdidevice_isdynamicinputsupported_001
680  * @tc.desc: Verify the IsDynamicInputSupported function return success.
681  * @tc.type: FUNC
682  */
683 HWTEST_F(HDIDeviceTest, hdidevice_isdynamicinputsupported_001, TestSize.Level0)
684 {
685     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
686     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
687     EXPECT_NE(hdiDevice, nullptr);
688 
689     bool isSupported = false;
690     EXPECT_CALL(*((V1_0::MockIDevice *)device.GetRefPtr()), IsDynamicInputSupported(::testing::_))
691         .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(isSupported), ::testing::Return(HDF_SUCCESS)));
692 
693     bool newIsSupported = false;
694     bool expectIsSupported = false;
695     OH_NN_ReturnCode result = hdiDevice->IsDynamicInputSupported(newIsSupported);
696     EXPECT_EQ(OH_NN_SUCCESS, result);
697     EXPECT_EQ(newIsSupported, expectIsSupported);
698 }
699 
700 /* *
701  * @tc.name: hdidevice_isdynamicinputsupported_002
702  * @tc.desc: Verify the IsDynamicInputSupported function return unavailable device.
703  * @tc.type: FUNC
704  */
705 HWTEST_F(HDIDeviceTest, hdidevice_isdynamicinputsupported_002, TestSize.Level0)
706 {
707     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
708     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
709     EXPECT_NE(hdiDevice, nullptr);
710 
711     bool isSupported = false;
712     EXPECT_CALL(*((V1_0::MockIDevice *)device.GetRefPtr()), IsDynamicInputSupported(::testing::_))
713         .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(isSupported), ::testing::Return(HDF_FAILURE)));
714     OH_NN_ReturnCode result = hdiDevice->IsDynamicInputSupported(isSupported);
715     EXPECT_EQ(OH_NN_UNAVAILABLE_DEVICE, result);
716 }
717 
718 /* *
719  * @tc.name: hdidevice_isdynamicinputsupported_001
720  * @tc.desc: Verify the IsModelCacheSupported function return success.
721  * @tc.type: FUNC
722  */
723 HWTEST_F(HDIDeviceTest, hdidevice_ismodelcachesupported_001, TestSize.Level0)
724 {
725     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
726     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
727     EXPECT_NE(hdiDevice, nullptr);
728 
729     bool isSupported = false;
730     EXPECT_CALL(*((V1_0::MockIDevice *)device.GetRefPtr()), IsModelCacheSupported(::testing::_))
731         .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(isSupported), ::testing::Return(HDF_SUCCESS)));
732 
733     bool newIsSupported = false;
734     bool expectIsSupported = false;
735     OH_NN_ReturnCode result = hdiDevice->IsModelCacheSupported(newIsSupported);
736     EXPECT_EQ(OH_NN_SUCCESS, result);
737     EXPECT_EQ(expectIsSupported, newIsSupported);
738 }
739 
740 /* *
741  * @tc.name: hdidevice_isdynamicinputsupported_002
742  * @tc.desc: Verify the IsModelCacheSupported function return unavailable device.
743  * @tc.type: FUNC
744  */
745 HWTEST_F(HDIDeviceTest, hdidevice_ismodelcachesupported_002, TestSize.Level0)
746 {
747     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
748     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
749     EXPECT_NE(hdiDevice, nullptr);
750 
751     bool isSupported = false;
752     EXPECT_CALL(*((V1_0::MockIDevice *)device.GetRefPtr()), IsModelCacheSupported(::testing::_))
753         .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(isSupported), ::testing::Return(HDF_FAILURE)));
754     OH_NN_ReturnCode result = hdiDevice->IsModelCacheSupported(isSupported);
755     EXPECT_EQ(OH_NN_UNAVAILABLE_DEVICE, result);
756 }
757 
758 /* *
759  * @tc.name: hdidevice_preparemodel_001
760  * @tc.desc: Verify the PrepareModel function return success.
761  * @tc.type: FUNC
762  */
763 HWTEST_F(HDIDeviceTest, hdidevice_preparemodel_001, TestSize.Level0)
764 {
765     int32_t allocBufferType = HDF_SUCCESS;
766     int32_t prepareType = HDF_SUCCESS;
767     OH_NN_ReturnCode result = PrepareModel(allocBufferType, prepareType);
768     EXPECT_EQ(OH_NN_FAILED, result);
769 }
770 
771 /* *
772  * @tc.name: hdidevice_preparemodel_002
773  * @tc.desc: Verify the PrepareModel function return invalid parameter.
774  * @tc.type: FUNC
775  */
776 HWTEST_F(HDIDeviceTest, hdidevice_preparemodel_002, TestSize.Level0)
777 {
778     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
779     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
780     EXPECT_NE(hdiDevice, nullptr);
781 
782     std::shared_ptr<const mindspore::lite::LiteGraph> model = nullptr;
783     ModelConfig config;
784     std::shared_ptr<PreparedModel> preparedModel;
785     OH_NN_ReturnCode result = hdiDevice->PrepareModel(model, config, preparedModel);
786     EXPECT_EQ(OH_NN_INVALID_PARAMETER, result);
787 }
788 
789 /* *
790  * @tc.name: hdidevice_preparemodel_003
791  * @tc.desc: Verify the PrepareModel function return failed.
792  * @tc.type: FUNC
793  */
794 HWTEST_F(HDIDeviceTest, hdidevice_preparemodel_003, TestSize.Level0)
795 {
796     int32_t allocBufferType = HDF_SUCCESS;
797     int32_t prepareType = HDF_FAILURE;
798     OH_NN_ReturnCode result = PrepareModel(allocBufferType, prepareType);
799     EXPECT_EQ(OH_NN_FAILED, result);
800 }
801 
802 /* *
803  * @tc.name: hdidevice_preparemodel_004
804  * @tc.desc: Verify the PrepareModel function return failed.
805  * @tc.type: FUNC
806  */
807 HWTEST_F(HDIDeviceTest, hdidevice_preparemodel_004, TestSize.Level0)
808 {
809     int32_t allocBufferType = HDF_FAILURE;
810     int32_t prepareType = HDF_FAILURE;
811     OH_NN_ReturnCode result = PrepareModel(allocBufferType, prepareType);
812     EXPECT_EQ(OH_NN_FAILED, result);
813 }
814 
815 /* *
816  * @tc.name: hdidevice_preparemodel_005
817  * @tc.desc: Verify the PrepareModel function return failed.
818  * @tc.type: FUNC
819  */
820 HWTEST_F(HDIDeviceTest, hdidevice_preparemodel_005, TestSize.Level0)
821 {
822     LOGE("PrepareModel hdidevice_preparemodel_005");
823     OHOS::sptr<V1_0::MockIDevice> sp = OHOS::sptr<V1_0::MockIDevice>(new (std::nothrow) V1_0::MockIDevice());
824     EXPECT_NE(sp, nullptr);
825 
826     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(sp);
827     EXPECT_NE(hdiDevice, nullptr);
828 
829     float dataArry[9] {0, 1, 2, 3, 4, 5, 6, 7, 8};
830     void* data = dataArry;
831     ModelConfig config;
832     std::shared_ptr<PreparedModel> preparedModel;
833     OH_NN_ReturnCode result = hdiDevice->PrepareModel(data, config, preparedModel);
834     EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, result);
835 }
836 
837 /* *
838  * @tc.name: hdidevice_preparemodelfrommodelcache_001
839  * @tc.desc: Verify the PrepareModelFromModelCache function return success.
840  * @tc.type: FUNC
841  */
842 HWTEST_F(HDIDeviceTest, hdidevice_preparemodelfrommodelcache_001, TestSize.Level0)
843 {
844     size_t length = 100;
845     void *buffer = nullptr;
846     GetBuffer(buffer, length);
847 
848     std::vector<Buffer> modelCache = { { buffer, 100 } };
849     ModelConfig config;
850 
851     OHOS::sptr<V1_0::MockIDevice> sp = OHOS::sptr<V1_0::MockIDevice>(new (std::nothrow) V1_0::MockIDevice());
852     EXPECT_NE(sp, nullptr);
853 
854     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(sp);
855     EXPECT_NE(hdiDevice, nullptr);
856 
857     std::shared_ptr<PreparedModel> preparedModel;
858 
859     OHOS::sptr<V1_0::IPreparedModel> iPreparedModel =
860         OHOS::sptr<V1_0::MockIPreparedModel>(new (std::nothrow) V1_0::MockIPreparedModel());
861     EXPECT_CALL(*sp, PrepareModelFromModelCache(::testing::_, ::testing::_, ::testing::_))
862         .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<2>(iPreparedModel), ::testing::Return(HDF_SUCCESS)));
863 
864     bool isUpdatable = false;
865     OH_NN_ReturnCode result = hdiDevice->PrepareModelFromModelCache(modelCache, config, preparedModel, isUpdatable);
866     const auto &memoryManager = MemoryManager::GetInstance();
867     memoryManager->UnMapMemory(buffer);
868     EXPECT_EQ(OH_NN_SUCCESS, result);
869 }
870 
871 /* *
872  * @tc.name: hdidevice_preparemodelfrommodelcache_002
873  * @tc.desc: Verify the PrepareModelFromModelCache function return unavailable device.
874  * @tc.type: FUNC
875  */
876 HWTEST_F(HDIDeviceTest, hdidevice_preparemodelfrommodelcache_002, TestSize.Level0)
877 {
878     size_t length = 100;
879     void *buffer = nullptr;
880     GetBuffer(buffer, length);
881 
882     OHOS::sptr<V1_0::MockIDevice> sp = OHOS::sptr<V1_0::MockIDevice>(new (std::nothrow) V1_0::MockIDevice());
883     EXPECT_NE(sp, nullptr);
884 
885     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(sp);
886     EXPECT_NE(hdiDevice, nullptr);
887 
888     std::vector<Buffer> modelCache = { { buffer, 100 } };
889     ModelConfig config;
890     OHOS::sptr<V1_0::IPreparedModel> preModel =
891         OHOS::sptr<V1_0::MockIPreparedModel>(new (std::nothrow) V1_0::MockIPreparedModel());
892     EXPECT_NE(preModel, nullptr);
893 
894     std::shared_ptr<PreparedModel> preparedModel = std::make_shared<HDIPreparedModelV1_0>(preModel);
895 
896     OHOS::sptr<V1_0::IPreparedModel> iPreparedModel =
897         OHOS::sptr<V1_0::MockIPreparedModel>(new (std::nothrow) V1_0::MockIPreparedModel);
898     EXPECT_CALL(*sp, PrepareModelFromModelCache(::testing::_, ::testing::_, ::testing::_))
899         .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<2>(iPreparedModel), ::testing::Return(HDF_FAILURE)));
900 
901     bool isUpdatable = false;
902     OH_NN_ReturnCode result = hdiDevice->PrepareModelFromModelCache(modelCache, config, preparedModel, isUpdatable);
903     EXPECT_EQ(OH_NN_UNAVAILABLE_DEVICE, result);
904 }
905 
906 /* *
907  * @tc.name: hdidevice_preparemodelfrommodelcache_003
908  * @tc.desc: Verify the PrepareModelFromModelCache function return nullptr.
909  * @tc.type: FUNC
910  */
911 HWTEST_F(HDIDeviceTest, hdidevice_preparemodelfrommodelcache_003, TestSize.Level0)
912 {
913     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
914     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
915     EXPECT_NE(hdiDevice, nullptr);
916 
917     std::vector<Buffer> modelCache = { { nullptr, 0 } };
918     ModelConfig config;
919     std::shared_ptr<PreparedModel> preparedModel;
920     bool isUpdatable = false;
921     OH_NN_ReturnCode result = hdiDevice->PrepareModelFromModelCache(modelCache, config, preparedModel, isUpdatable);
922     EXPECT_EQ(OH_NN_NULL_PTR, result);
923 }
924 
925 /* *
926  * @tc.name: hdidevice_preparemodelfrommodelcache_004
927  * @tc.desc: Verify the PrepareModelFromModelCache function return success.
928  * @tc.type: FUNC
929  */
930 HWTEST_F(HDIDeviceTest, hdidevice_preparemodelfrommodelcache_004, TestSize.Level0)
931 {
932     LOGE("GetDeviceStatus hdidevice_getdevicestatus_005");
933     size_t length = 100;
934     void *buffer = nullptr;
935     GetBuffer(buffer, length);
936 
937     OHOS::sptr<V1_0::MockIDevice> sp = OHOS::sptr<V1_0::MockIDevice>(new (std::nothrow) V1_0::MockIDevice());
938     EXPECT_NE(sp, nullptr);
939 
940     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(sp);
941     EXPECT_NE(hdiDevice, nullptr);
942 
943     std::vector<Buffer> modelCache = { { buffer, 100 } };
944     ModelConfig config;
945     config.mode = OH_NN_PERFORMANCE_LOW;
946     config.priority = OH_NN_PRIORITY_LOW;
947     OHOS::sptr<V1_0::IPreparedModel> preModel =
948         OHOS::sptr<V1_0::MockIPreparedModel>(new (std::nothrow) V1_0::MockIPreparedModel());
949     EXPECT_NE(preModel, nullptr);
950 
951     std::shared_ptr<PreparedModel> preparedModel = std::make_shared<HDIPreparedModelV1_0>(preModel);
952 
953     OHOS::sptr<V1_0::IPreparedModel> iPreparedModel =
954         OHOS::sptr<V1_0::MockIPreparedModel>(new (std::nothrow) V1_0::MockIPreparedModel);
955     EXPECT_CALL(*sp, PrepareModelFromModelCache(::testing::_, ::testing::_, ::testing::_))
956         .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<2>(iPreparedModel), ::testing::Return(HDF_FAILURE)));
957 
958     bool isUpdatable = false;
959     OH_NN_ReturnCode result = hdiDevice->PrepareModelFromModelCache(modelCache, config, preparedModel, isUpdatable);
960     EXPECT_EQ(OH_NN_UNAVAILABLE_DEVICE, result);
961 }
962 
963 /* *
964  * @tc.name: hdidevice_preparemodelfrommodelcache_005
965  * @tc.desc: Verify the PrepareModelFromModelCache function return success.
966  * @tc.type: FUNC
967  */
968 HWTEST_F(HDIDeviceTest, hdidevice_preparemodelfrommodelcache_005, TestSize.Level0)
969 {
970     LOGE("GetDeviceStatus hdidevice_preparemodelfrommodelcache_005");
971     size_t length = 100;
972     void *buffer = nullptr;
973     GetBuffer(buffer, length);
974 
975     OHOS::sptr<V1_0::MockIDevice> sp = OHOS::sptr<V1_0::MockIDevice>(new (std::nothrow) V1_0::MockIDevice());
976     EXPECT_NE(sp, nullptr);
977 
978     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(sp);
979     EXPECT_NE(hdiDevice, nullptr);
980 
981     std::vector<Buffer> modelCache = { { buffer, 100 } };
982     ModelConfig config;
983     config.mode = OH_NN_PERFORMANCE_MEDIUM;
984     config.priority = OH_NN_PRIORITY_MEDIUM;
985     OHOS::sptr<V1_0::IPreparedModel> preModel =
986         OHOS::sptr<V1_0::MockIPreparedModel>(new (std::nothrow) V1_0::MockIPreparedModel());
987     EXPECT_NE(preModel, nullptr);
988 
989     std::shared_ptr<PreparedModel> preparedModel = std::make_shared<HDIPreparedModelV1_0>(preModel);
990 
991     OHOS::sptr<V1_0::IPreparedModel> iPreparedModel =
992         OHOS::sptr<V1_0::MockIPreparedModel>(new (std::nothrow) V1_0::MockIPreparedModel);
993     EXPECT_CALL(*sp, PrepareModelFromModelCache(::testing::_, ::testing::_, ::testing::_))
994         .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<2>(iPreparedModel), ::testing::Return(HDF_FAILURE)));
995 
996     bool isUpdatable = false;
997     OH_NN_ReturnCode result = hdiDevice->PrepareModelFromModelCache(modelCache, config, preparedModel, isUpdatable);
998     EXPECT_EQ(OH_NN_UNAVAILABLE_DEVICE, result);
999 }
1000 
1001 /* *
1002  * @tc.name: hdidevice_preparemodelfrommodelcache_006
1003  * @tc.desc: Verify the PrepareModelFromModelCache function return success.
1004  * @tc.type: FUNC
1005  */
1006 HWTEST_F(HDIDeviceTest, hdidevice_preparemodelfrommodelcache_006, TestSize.Level0)
1007 {
1008     LOGE("GetDeviceStatus hdidevice_preparemodelfrommodelcache_006");
1009     size_t length = 100;
1010     void *buffer = nullptr;
1011     GetBuffer(buffer, length);
1012 
1013     OHOS::sptr<V1_0::MockIDevice> sp = OHOS::sptr<V1_0::MockIDevice>(new (std::nothrow) V1_0::MockIDevice());
1014     EXPECT_NE(sp, nullptr);
1015 
1016     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(sp);
1017     EXPECT_NE(hdiDevice, nullptr);
1018 
1019     std::vector<Buffer> modelCache = { { buffer, 100 } };
1020     ModelConfig config;
1021     config.mode = OH_NN_PERFORMANCE_HIGH;
1022     config.priority = OH_NN_PRIORITY_HIGH;
1023     OHOS::sptr<V1_0::IPreparedModel> preModel =
1024         OHOS::sptr<V1_0::MockIPreparedModel>(new (std::nothrow) V1_0::MockIPreparedModel());
1025     EXPECT_NE(preModel, nullptr);
1026 
1027     std::shared_ptr<PreparedModel> preparedModel = std::make_shared<HDIPreparedModelV1_0>(preModel);
1028 
1029     OHOS::sptr<V1_0::IPreparedModel> iPreparedModel =
1030         OHOS::sptr<V1_0::MockIPreparedModel>(new (std::nothrow) V1_0::MockIPreparedModel);
1031     EXPECT_CALL(*sp, PrepareModelFromModelCache(::testing::_, ::testing::_, ::testing::_))
1032         .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<2>(iPreparedModel), ::testing::Return(HDF_FAILURE)));
1033 
1034     bool isUpdatable = false;
1035     OH_NN_ReturnCode result = hdiDevice->PrepareModelFromModelCache(modelCache, config, preparedModel, isUpdatable);
1036     EXPECT_EQ(OH_NN_UNAVAILABLE_DEVICE, result);
1037 }
1038 
1039 /* *
1040  * @tc.name: hdidevice_preparemodelfrommodelcache_007
1041  * @tc.desc: Verify the PrepareModelFromModelCache function return success.
1042  * @tc.type: FUNC
1043  */
1044 HWTEST_F(HDIDeviceTest, hdidevice_preparemodelfrommodelcache_007, TestSize.Level0)
1045 {
1046     LOGE("GetDeviceStatus hdidevice_preparemodelfrommodelcache_007");
1047     size_t length = 100;
1048     void *buffer = nullptr;
1049     GetBuffer(buffer, length);
1050 
1051     OHOS::sptr<V1_0::MockIDevice> sp = OHOS::sptr<V1_0::MockIDevice>(new (std::nothrow) V1_0::MockIDevice());
1052     EXPECT_NE(sp, nullptr);
1053 
1054     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(sp);
1055     EXPECT_NE(hdiDevice, nullptr);
1056 
1057     std::vector<Buffer> modelCache = { { buffer, 100 } };
1058     ModelConfig config;
1059     config.mode = OH_NN_PERFORMANCE_EXTREME;
1060     OHOS::sptr<V1_0::IPreparedModel> preModel =
1061         OHOS::sptr<V1_0::MockIPreparedModel>(new (std::nothrow) V1_0::MockIPreparedModel());
1062     EXPECT_NE(preModel, nullptr);
1063 
1064     std::shared_ptr<PreparedModel> preparedModel = std::make_shared<HDIPreparedModelV1_0>(preModel);
1065 
1066     OHOS::sptr<V1_0::IPreparedModel> iPreparedModel =
1067         OHOS::sptr<V1_0::MockIPreparedModel>(new (std::nothrow) V1_0::MockIPreparedModel);
1068     EXPECT_CALL(*sp, PrepareModelFromModelCache(::testing::_, ::testing::_, ::testing::_))
1069         .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<2>(iPreparedModel), ::testing::Return(HDF_FAILURE)));
1070 
1071     bool isUpdatable = false;
1072     OH_NN_ReturnCode result = hdiDevice->PrepareModelFromModelCache(modelCache, config, preparedModel, isUpdatable);
1073     EXPECT_EQ(OH_NN_UNAVAILABLE_DEVICE, result);
1074 }
1075 
1076 /* *
1077  * @tc.name: hdidevice_allocatebuffer_001
1078  * @tc.desc: Verify the AllocateBuffer function return nullptr.
1079  * @tc.type: FUNC
1080  */
1081 HWTEST_F(HDIDeviceTest, hdidevice_allocatebuffer_001, TestSize.Level0)
1082 {
1083     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
1084     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
1085     EXPECT_NE(hdiDevice, nullptr);
1086 
1087     V1_0::SharedBuffer buffer;
1088     EXPECT_CALL(*((V1_0::MockIDevice *)device.GetRefPtr()), AllocateBuffer(::testing::_, ::testing::_))
1089         .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<1>(buffer), ::testing::Return(HDF_FAILURE)));
1090 
1091     size_t length = 8;
1092     void *result = hdiDevice->AllocateBuffer(length);
1093     EXPECT_EQ(nullptr, result);
1094     hdiDevice->ReleaseBuffer(result);
1095 }
1096 
1097 /* *
1098  * @tc.name: hdidevice_allocatebuffer_002
1099  * @tc.desc: Verify the AllocateBuffer function return nullptr and HDF_FAILURE.
1100  * @tc.type: FUNC
1101  */
1102 HWTEST_F(HDIDeviceTest, hdidevice_allocatebuffer_002, TestSize.Level0)
1103 {
1104     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
1105     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
1106     EXPECT_NE(hdiDevice, nullptr);
1107 
1108     size_t length = 8;
1109     void *result = hdiDevice->AllocateBuffer(length);
1110     EXPECT_EQ(nullptr, result);
1111     hdiDevice->ReleaseBuffer(result);
1112 }
1113 
1114 /* *
1115  * @tc.name: hdidevice_allocatebuffer_003
1116  * @tc.desc: Verify the AllocateBuffer function return nullptr in case of 0 size.
1117  * @tc.type: FUNC
1118  */
1119 HWTEST_F(HDIDeviceTest, hdidevice_allocatebuffer_003, TestSize.Level0)
1120 {
1121     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
1122     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
1123     EXPECT_NE(hdiDevice, nullptr);
1124 
1125     size_t length = 0;
1126     void *result = hdiDevice->AllocateBuffer(length);
1127     EXPECT_EQ(nullptr, result);
1128 }
1129 
1130 /* *
1131  * @tc.name: hdidevice_allocatebuffer_004
1132  * @tc.desc: Verify the AllocateBuffer function return nullptr in case of 0 size.
1133  * @tc.type: FUNC
1134  */
1135 HWTEST_F(HDIDeviceTest, hdidevice_allocatebuffer_004, TestSize.Level0)
1136 {
1137     LOGE("AllocateBuffer hdidevice_preparemodelfrommodelcache_007");
1138     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
1139     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
1140     EXPECT_NE(hdiDevice, nullptr);
1141 
1142     size_t length = 0;
1143     int fd = 0;
1144     OH_NN_ReturnCode result = hdiDevice->AllocateBuffer(length, fd);
1145     EXPECT_EQ(OH_NN_INVALID_PARAMETER, result);
1146 }
1147 
1148 /* *
1149  * @tc.name: hdidevice_allocatebuffer_005
1150  * @tc.desc: Verify the AllocateBuffer function return nullptr in case of 0 size.
1151  * @tc.type: FUNC
1152  */
1153 HWTEST_F(HDIDeviceTest, hdidevice_allocatebuffer_005, TestSize.Level0)
1154 {
1155     LOGE("AllocateBuffer hdidevice_allocatebuffer_005");
1156     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
1157     EXPECT_CALL(*((V1_0::MockIDevice *)device.GetRefPtr()), AllocateBuffer(::testing::_, ::testing::_))
1158         .WillRepeatedly(::testing::Return(HDF_FAILURE));
1159 
1160     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
1161     EXPECT_NE(hdiDevice, nullptr);
1162 
1163     size_t length = 1;
1164     int fd = 0;
1165     OH_NN_ReturnCode result = hdiDevice->AllocateBuffer(length, fd);
1166     EXPECT_EQ(OH_NN_MEMORY_ERROR, result);
1167 }
1168 
1169 /* *
1170  * @tc.name: hdidevice_allocatebuffer_006
1171  * @tc.desc: Verify the AllocateBuffer function return nullptr in case of 0 size.
1172  * @tc.type: FUNC
1173  */
1174 HWTEST_F(HDIDeviceTest, hdidevice_allocatebuffer_006, TestSize.Level0)
1175 {
1176     LOGE("AllocateBuffer hdidevice_allocatebuffer_006");
1177     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
1178     EXPECT_CALL(*((V1_0::MockIDevice *)device.GetRefPtr()), AllocateBuffer(::testing::_, ::testing::_))
1179         .WillRepeatedly(::testing::Return(HDF_SUCCESS));
1180 
1181     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
1182     EXPECT_NE(hdiDevice, nullptr);
1183 
1184     size_t length = 1;
1185     int fd = 0;
1186     OH_NN_ReturnCode result = hdiDevice->AllocateBuffer(length, fd);
1187     EXPECT_EQ(OH_NN_SUCCESS, result);
1188 }
1189 
1190 /* *
1191  * @tc.name: hdidevice_releasebuffer_001
1192  * @tc.desc: Verify the ReleaseBuffer function validate buffer success.
1193  * @tc.type: FUNC
1194  */
1195 HWTEST_F(HDIDeviceTest, hdidevice_releasebuffer_001, TestSize.Level0)
1196 {
1197     size_t length = 100;
1198     void *buffer = nullptr;
1199     GetBuffer(buffer, length);
1200 
1201     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
1202     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
1203 
1204     EXPECT_CALL(*((V1_0::MockIDevice *)device.GetRefPtr()), ReleaseBuffer(::testing::_))
1205         .WillRepeatedly(::testing::Return(HDF_SUCCESS));
1206 
1207     EXPECT_NE(hdiDevice, nullptr);
1208     hdiDevice->ReleaseBuffer(buffer);
1209     const auto &memoryManager = MemoryManager::GetInstance();
1210     memoryManager->UnMapMemory(buffer);
1211 }
1212 
1213 /* *
1214  * @tc.name: hdidevice_releasebuffer_002
1215  * @tc.desc: Verify the ReleaseBuffer function validate AllocateBuffer return nullptr.
1216  * @tc.type: FUNC
1217  */
1218 HWTEST_F(HDIDeviceTest, hdidevice_releasebuffer_002, TestSize.Level0)
1219 {
1220     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
1221     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
1222     EXPECT_NE(hdiDevice, nullptr);
1223 
1224     V1_0::SharedBuffer sharedbuffer;
1225     EXPECT_CALL(*((V1_0::MockIDevice *)device.GetRefPtr()), AllocateBuffer(::testing::_, ::testing::_))
1226         .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<1>(sharedbuffer), ::testing::Return(HDF_FAILURE)));
1227 
1228     EXPECT_CALL(*((V1_0::MockIDevice *)device.GetRefPtr()), ReleaseBuffer(::testing::_))
1229         .WillRepeatedly(::testing::Return(HDF_FAILURE));
1230 
1231     size_t length = 8;
1232     void *buffer = hdiDevice->AllocateBuffer(length);
1233     hdiDevice->ReleaseBuffer(buffer);
1234 }
1235 
1236 /* *
1237  * @tc.name: hdidevice_releasebuffer_003
1238  * @tc.desc: Verify the ReleaseBuffer function validate param buffer is nullptr.
1239  * @tc.type: FUNC
1240  */
1241 HWTEST_F(HDIDeviceTest, hdidevice_releasebuffer_003, TestSize.Level0)
1242 {
1243     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
1244     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
1245     EXPECT_NE(hdiDevice, nullptr);
1246 
1247     void *buffer = nullptr;
1248     hdiDevice->ReleaseBuffer(buffer);
1249 }
1250 
1251 /* *
1252  * @tc.name: hdidevice_releasebuffer_004
1253  * @tc.desc: Verify the ReleaseBuffer function validate invalid buffer.
1254  * @tc.type: FUNC
1255  */
1256 HWTEST_F(HDIDeviceTest, hdidevice_releasebuffer_004, TestSize.Level0)
1257 {
1258     const size_t length = 100;
1259     auto* buffer = new(std::nothrow) char[length];
1260     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
1261     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
1262     EXPECT_NE(hdiDevice, nullptr);
1263 
1264     hdiDevice->ReleaseBuffer(buffer);
1265     delete[] buffer;
1266     buffer = nullptr;
1267 }
1268 
1269 /* *
1270  * @tc.name: hdidevice_releasebuffer_005
1271  * @tc.desc: Verify the ReleaseBuffer function validate moc object's ReleaseBuffer return failure.
1272  * @tc.type: FUNC
1273  */
1274 HWTEST_F(HDIDeviceTest, hdidevice_releasebuffer_005, TestSize.Level0)
1275 {
1276     size_t length = 100;
1277     void *buffer = nullptr;
1278     GetBuffer(buffer, length);
1279 
1280     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
1281     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
1282     EXPECT_NE(hdiDevice, nullptr);
1283 
1284     EXPECT_CALL(*((V1_0::MockIDevice *)device.GetRefPtr()), ReleaseBuffer(::testing::_))
1285         .WillRepeatedly(::testing::Return(HDF_FAILURE));
1286 
1287     hdiDevice->ReleaseBuffer(buffer);
1288     const auto &memoryManager = MemoryManager::GetInstance();
1289     memoryManager->UnMapMemory(buffer);
1290 }
1291 
1292 /* *
1293  * @tc.name: hdidevice_releasebuffer_007
1294  * @tc.desc: Verify the ReleaseBuffer function validate moc object's ReleaseBuffer return failure.
1295  * @tc.type: FUNC
1296  */
1297 HWTEST_F(HDIDeviceTest, hdidevice_releasebuffer_007, TestSize.Level0)
1298 {
1299     LOGE("ReleaseBuffer hdidevice_releasebuffer_007");
1300     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
1301     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
1302     EXPECT_NE(hdiDevice, nullptr);
1303 
1304     EXPECT_CALL(*((V1_0::MockIDevice *)device.GetRefPtr()), ReleaseBuffer(::testing::_))
1305         .WillRepeatedly(::testing::Return(HDF_FAILURE));
1306 
1307     int fd = 0;
1308     size_t length = 1;
1309     OH_NN_ReturnCode ret = hdiDevice->ReleaseBuffer(fd, length);
1310     EXPECT_EQ(OH_NN_MEMORY_ERROR, ret);
1311 }
1312 
1313 /* *
1314  * @tc.name: hdidevice_releasebuffer_008
1315  * @tc.desc: Verify the ReleaseBuffer function validate moc object's ReleaseBuffer return failure.
1316  * @tc.type: FUNC
1317  */
1318 HWTEST_F(HDIDeviceTest, hdidevice_releasebuffer_008, TestSize.Level0)
1319 {
1320     LOGE("ReleaseBuffer hdidevice_releasebuffer_008");
1321     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
1322     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
1323     EXPECT_NE(hdiDevice, nullptr);
1324 
1325     EXPECT_CALL(*((V1_0::MockIDevice *)device.GetRefPtr()), ReleaseBuffer(::testing::_))
1326         .WillRepeatedly(::testing::Return(HDF_SUCCESS));
1327 
1328     int fd = 0;
1329     size_t length = 1;
1330     OH_NN_ReturnCode ret = hdiDevice->ReleaseBuffer(fd, length);
1331     EXPECT_EQ(OH_NN_SUCCESS, ret);
1332 }
1333 
1334 /* *
1335  * @tc.name: hdidevice_allocatetensorbuffer_001
1336  * @tc.desc: Verify the ReleaseBuffer function validate moc object's ReleaseBuffer return failure.
1337  * @tc.type: FUNC
1338  */
1339 HWTEST_F(HDIDeviceTest, hdidevice_allocatetensorbuffer_001, TestSize.Level0)
1340 {
1341     LOGE("AllocateTensorBuffer hdidevice_allocatetensorbuffer_001");
1342     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
1343     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
1344     EXPECT_NE(hdiDevice, nullptr);
1345 
1346     size_t length = 0;
1347     std::shared_ptr<TensorDesc> tensor;
1348     void* ret = hdiDevice->AllocateTensorBuffer(length, tensor);
1349     EXPECT_EQ(nullptr, ret);
1350 }
1351 
1352 /* *
1353  * @tc.name: hdidevice_allocatetensorbuffer_002
1354  * @tc.desc: Verify the ReleaseBuffer function validate moc object's ReleaseBuffer return failure.
1355  * @tc.type: FUNC
1356  */
1357 HWTEST_F(HDIDeviceTest, hdidevice_allocatetensorbuffer_002, TestSize.Level0)
1358 {
1359     LOGE("AllocateTensorBuffer hdidevice_allocatetensorbuffer_002");
1360     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
1361     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
1362     EXPECT_NE(hdiDevice, nullptr);
1363 
1364     size_t length = 0;
1365     std::shared_ptr<NNTensor> tensor;
1366     void* ret = hdiDevice->AllocateTensorBuffer(length, tensor);
1367     EXPECT_EQ(nullptr, ret);
1368 }
1369 
1370 /* *
1371  * @tc.name: hdidevice_prepareofflinemodel_001
1372  * @tc.desc: Verify the ReleaseBuffer function validate moc object's ReleaseBuffer return failure.
1373  * @tc.type: FUNC
1374  */
1375 HWTEST_F(HDIDeviceTest, hdidevice_prepareofflinemodel_001, TestSize.Level0)
1376 {
1377     LOGE("PrepareOfflineModel hdidevice_prepareofflinemodel_001");
1378     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
1379     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
1380     EXPECT_NE(hdiDevice, nullptr);
1381 
1382     ModelConfig config;
1383     std::shared_ptr<PreparedModel> preparedModel;
1384     OH_NN_ReturnCode ret = hdiDevice->PrepareOfflineModel(nullptr, config, preparedModel);
1385     EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, ret);
1386 }
1387 
1388 /* *
1389  * @tc.name: hdidevice_prepareofflinemodel_002
1390  * @tc.desc: Verify the ReleaseBuffer function validate moc object's ReleaseBuffer return failure.
1391  * @tc.type: FUNC
1392  */
1393 HWTEST_F(HDIDeviceTest, hdidevice_prepareofflinemodel_002, TestSize.Level0)
1394 {
1395     LOGE("PrepareOfflineModel hdidevice_prepareofflinemodel_002");
1396     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
1397     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
1398     EXPECT_NE(hdiDevice, nullptr);
1399 
1400     std::shared_ptr<mindspore::lite::LiteGraph> model = std::make_shared<mindspore::lite::LiteGraph>();
1401     mindspore::lite::LiteGraph::Node node;
1402     mindspore::lite::LiteGraph::Node* testNode = &node;
1403     model->all_nodes_.emplace_back(testNode);
1404     ModelConfig config;
1405     std::shared_ptr<PreparedModel> preparedModel;
1406     OH_NN_ReturnCode ret = hdiDevice->PrepareOfflineModel(model, config, preparedModel);
1407     EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, ret);
1408 }
1409 
1410 /* *
1411  * @tc.name: hdidevice_prepareofflinemodel_003
1412  * @tc.desc: Verify the ReleaseBuffer function validate moc object's ReleaseBuffer return failure.
1413  * @tc.type: FUNC
1414  */
1415 HWTEST_F(HDIDeviceTest, hdidevice_prepareofflinemodel_003, TestSize.Level0)
1416 {
1417     LOGE("PrepareOfflineModel hdidevice_prepareofflinemodel_003");
1418     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
1419     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
1420     EXPECT_NE(hdiDevice, nullptr);
1421 
1422     std::shared_ptr<mindspore::lite::LiteGraph> model = std::make_shared<mindspore::lite::LiteGraph>();
1423     mindspore::lite::LiteGraph::Node node;
1424     uint32_t indice = 0;
1425     node.input_indices_.emplace_back(indice);
1426     node.input_indices_.emplace_back(indice);
1427     mindspore::lite::LiteGraph::Node* testNode = &node;
1428     model->all_nodes_.emplace_back(testNode);
1429     model->all_tensors_.emplace_back(mindspore::lite::MindIR_Tensor_Create());
1430     model->all_tensors_.emplace_back(mindspore::lite::MindIR_Tensor_Create());
1431     ModelConfig config;
1432     std::shared_ptr<PreparedModel> preparedModel;
1433     OH_NN_ReturnCode ret = hdiDevice->PrepareOfflineModel(model, config, preparedModel);
1434     EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, ret);
1435 }
1436 
1437 /* *
1438  * @tc.name: hdidevice_prepareofflinemodel_004
1439  * @tc.desc: Verify the ReleaseBuffer function validate moc object's ReleaseBuffer return failure.
1440  * @tc.type: FUNC
1441  */
1442 HWTEST_F(HDIDeviceTest, hdidevice_prepareofflinemodel_004, TestSize.Level0)
1443 {
1444     LOGE("PrepareOfflineModel hdidevice_prepareofflinemodel_004");
1445     OHOS::sptr<V1_0::INnrtDevice> device = V1_0::INnrtDevice::Get(false);
1446     std::unique_ptr<HDIDeviceV1_0> hdiDevice = std::make_unique<HDIDeviceV1_0>(device);
1447     EXPECT_NE(hdiDevice, nullptr);
1448 
1449     std::shared_ptr<mindspore::lite::LiteGraph> model = std::make_shared<mindspore::lite::LiteGraph>();
1450     mindspore::lite::LiteGraph::Node node;
1451     uint32_t indice = 0;
1452     node.input_indices_.emplace_back(indice);
1453     node.input_indices_.emplace_back(indice);
1454     mindspore::lite::LiteGraph::Node* testNode = &node;
1455     model->all_nodes_.emplace_back(testNode);
1456     model->all_tensors_.emplace_back(mindspore::lite::MindIR_Tensor_Create());
1457     model->all_tensors_.emplace_back(mindspore::lite::MindIR_Tensor_Create());
1458     ModelConfig config;
1459     std::shared_ptr<PreparedModel> preparedModel;
1460     OH_NN_ReturnCode ret = hdiDevice->PrepareOfflineModel(model, config, preparedModel);
1461     EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, ret);
1462 }
1463 } // namespace UnitTest
1464 } // namespace NeuralNetworkRuntime
1465 } // namespace OHOS