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_v2_0.h"
29 #include "hdi_device_v2_1.h"
30 #include "hdi_prepared_model_v2_1.h"
31 #include "test/unittest/common/v2_0/mock_idevice.h"
32 #include "test/unittest/common/file_utils.h"
33
34 #include "lite_graph_to_hdi_model_v2_0.h"
35 #include "lite_graph_to_hdi_model_v2_1.h"
36 #include "device.h"
37 #include "interfaces/kits/c/neural_network_runtime/neural_network_runtime_type.h"
38 #include "common/log.h"
39 #include "nnbackend.h"
40 #include "ops_registry.h"
41 #include "transform.h"
42
43 using namespace testing;
44 using namespace testing::ext;
45 using namespace OHOS::NeuralNetworkRuntime;
46 using namespace OHOS::HDI::Nnrt::V2_1;
47
48 namespace MSLITE = mindspore::lite;
49
50 namespace OHOS {
51 namespace HDI {
52 namespace Nnrt {
53 namespace V2_1 {
54 class MockIDevice : public INnrtDevice {
55 public:
56 MOCK_METHOD1(GetDeviceName, int32_t(std::string&));
57 MOCK_METHOD1(GetVendorName, int32_t(std::string&));
58 MOCK_METHOD1(GetDeviceType, int32_t(DeviceType&));
59 MOCK_METHOD1(GetDeviceStatus, int32_t(DeviceStatus&));
60 MOCK_METHOD2(GetSupportedOperation, int32_t(const Model&, std::vector<bool>&));
61 MOCK_METHOD1(IsFloat16PrecisionSupported, int32_t(bool&));
62 MOCK_METHOD1(IsPerformanceModeSupported, int32_t(bool&));
63 MOCK_METHOD1(IsPrioritySupported, int32_t(bool&));
64 MOCK_METHOD1(IsDynamicInputSupported, int32_t(bool&));
65 MOCK_METHOD3(PrepareModel, int32_t(const Model&, const ModelConfig&, OHOS::sptr<IPreparedModel>&));
66 MOCK_METHOD1(IsModelCacheSupported, int32_t(bool&));
67 MOCK_METHOD3(PrepareModelFromModelCache, int32_t(const std::vector<SharedBuffer>&, const ModelConfig&,
68 OHOS::sptr<IPreparedModel>&));
69 MOCK_METHOD3(PrepareOfflineModel, int32_t(const std::vector<SharedBuffer>&, const ModelConfig&,
70 sptr<OHOS::HDI::Nnrt::V2_1::IPreparedModel>&));
71 MOCK_METHOD2(AllocateBuffer, int32_t(uint32_t, SharedBuffer&));
72 MOCK_METHOD1(ReleaseBuffer, int32_t(const SharedBuffer&));
73 MOCK_METHOD2(GetVersion, int32_t(uint32_t&, uint32_t&));
74 };
75
76 class MockIPreparedModel : public IPreparedModel {
77 public:
78 MOCK_METHOD1(ExportModelCache, int32_t(std::vector<SharedBuffer>&));
79 MOCK_METHOD3(Run, int32_t(const std::vector<IOTensor>&, const std::vector<IOTensor>&,
80 std::vector<std::vector<int32_t>>&));
81 MOCK_METHOD2(GetInputDimRanges, int32_t(std::vector<std::vector<uint32_t>>&, std::vector<std::vector<uint32_t>>&));
82 MOCK_METHOD2(GetVersion, int32_t(uint32_t&, uint32_t&));
83
84 static OH_NN_ReturnCode m_ExpectRetCode;
85 };
86 } // V2_1
87 } // Nnrt
88 } // HDI
89 } // OHOS
90
91 namespace mindspore {
92 namespace lite {
MindIR_LiteGraph_To_Model(const LiteGraph * lite_graph,const OHOS::HDI::Nnrt::V2_0::SharedBuffer & buffer)93 OHOS::HDI::Nnrt::V2_0::Model* MindIR_LiteGraph_To_Model(const LiteGraph* lite_graph,
94 const OHOS::HDI::Nnrt::V2_0::SharedBuffer& buffer)
95 {
96 return new (std::nothrow) OHOS::HDI::Nnrt::V2_0::Model();
97 }
98
LiteGraphDeleter(mindspore::lite::LiteGraph * liteGraph)99 void LiteGraphDeleter(mindspore::lite::LiteGraph* liteGraph)
100 {
101 MindIR_LiteGraph_Destroy(&liteGraph);
102 }
103
MindIR_Model_Destroy(OHOS::HDI::Nnrt::V2_0::Model ** model)104 void MindIR_Model_Destroy(OHOS::HDI::Nnrt::V2_0::Model** model)
105 {
106 if ((model != nullptr) && (*model != nullptr)) {
107 delete *model;
108 *model = nullptr;
109 }
110 }
111
MindIR_LiteGraph_GetConstTensorSize(const mindspore::lite::LiteGraph * lite_graph)112 size_t MindIR_LiteGraph_GetConstTensorSize(const mindspore::lite::LiteGraph* lite_graph)
113 {
114 return 1;
115 }
116 }
117 }
118
119 namespace OHOS {
120 namespace NeuralNetworkRuntime {
121 namespace UnitTest {
122 static const int DATA_VALUE = 1;
123 static const int DATA_NUM = 36;
124 static const int DIM_NUM = 3;
125
BuildLiteGraph(std::shared_ptr<mindspore::lite::LiteGraph> & model)126 void BuildLiteGraph(std::shared_ptr<mindspore::lite::LiteGraph>& model)
127 {
128 model->name_ = "testGraph";
129 model->input_indices_ = {0};
130 model->output_indices_ = {1};
131 const std::vector<mindspore::lite::QuantParam> quant_params {};
132 const std::vector<uint8_t> data(DATA_NUM, DATA_VALUE);
133 const std::vector<int32_t> dim = {DIM_NUM, DIM_NUM};
134
135 for (size_t indexInput = 0; indexInput < model->input_indices_.size(); ++indexInput) {
136 model->all_tensors_.emplace_back(mindspore::lite::MindIR_Tensor_Create());
137 }
138
139 for (size_t indexOutput = 0; indexOutput < model->output_indices_.size(); ++indexOutput) {
140 model->all_tensors_.emplace_back(mindspore::lite::MindIR_Tensor_Create());
141 }
142
143 mindspore::lite::LiteGraph::Node* node = new (std::nothrow) mindspore::lite::LiteGraph::Node();
144 node->name_ = "testNode";
145 auto activation_type = mindspore::lite::ACTIVATION_TYPE_NO_ACTIVATION;
146 node->primitive_ = mindspore::lite::MindIR_AddFusion_CreatePrimitive(activation_type);
147 model->all_nodes_.emplace_back(node);
148 }
149
150 class HDIDeviceTest : public testing::Test {
151 protected:
152 void GetBuffer(void*& buffer, size_t length);
153 OH_NN_ReturnCode PrepareModel(int32_t allocBufferType, int32_t prepareType);
154 OH_NN_ReturnCode PrepareModelv2(int32_t allocBufferType, int32_t prepareType);
155 };
156
GetBuffer(void * & buffer,size_t length)157 void HDIDeviceTest::GetBuffer(void*& buffer, size_t length)
158 {
159 std::string data = "ABCD";
160 const size_t dataLength = 100;
161 data.resize(dataLength, '+');
162
163 std::string filename = "/data/log/memory-001.dat";
164 FileUtils fileUtils(filename);
165 fileUtils.WriteFile(data);
166
167 int fd = open(filename.c_str(), O_RDWR);
168 EXPECT_NE(fd, -1);
169
170 const auto &memoryManager = MemoryManager::GetInstance();
171 buffer = memoryManager->MapMemory(fd, length);
172 EXPECT_NE(buffer, nullptr);
173
174 const char* result = static_cast<const char*>(buffer);
175 int index = 0;
176 EXPECT_EQ('A', result[index++]);
177 EXPECT_EQ('B', result[index++]);
178 EXPECT_EQ('C', result[index++]);
179 EXPECT_EQ('D', result[index++]);
180 close(fd);
181 }
182
PrepareModel(int32_t allocBufferType,int32_t prepareType)183 OH_NN_ReturnCode HDIDeviceTest::PrepareModel(int32_t allocBufferType, int32_t prepareType)
184 {
185 std::shared_ptr<mindspore::lite::LiteGraph> model = std::make_shared<mindspore::lite::LiteGraph>();
186 OHOS::sptr<V2_0::MockIDevice> sp = OHOS::sptr<V2_0::MockIDevice>(new (std::nothrow) V2_0::MockIDevice());
187 EXPECT_NE(sp, nullptr);
188
189 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(sp);
190 EXPECT_NE(hdiDevice, nullptr);
191
192 V2_0::SharedBuffer buffer {1, 1, 0, 1};
193 EXPECT_CALL(*sp, AllocateBuffer(::testing::_, ::testing::_))
194 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<1>(buffer), ::testing::Return(allocBufferType)));
195
196 std::shared_ptr<PreparedModel> preparedModel;
197 const int position = 2;
198 OHOS::sptr<V2_0::IPreparedModel> iPreparedModel =
199 OHOS::sptr<V2_0::MockIPreparedModel>(new (std::nothrow) V2_0::MockIPreparedModel());
200 EXPECT_CALL(*sp, PrepareModel(::testing::_, ::testing::_, ::testing::_))
201 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<position>(iPreparedModel),
202 ::testing::Return(prepareType)));
203
204 ModelConfig config;
205 OH_NN_ReturnCode result = hdiDevice->PrepareModel(model, config, preparedModel);
206 return result;
207 }
208
PrepareModelv2(int32_t allocBufferType,int32_t prepareType)209 OH_NN_ReturnCode HDIDeviceTest::PrepareModelv2(int32_t allocBufferType, int32_t prepareType)
210 {
211 std::shared_ptr<mindspore::lite::LiteGraph> model = std::make_shared<mindspore::lite::LiteGraph>();
212 OHOS::sptr<V2_1::MockIDevice> sp = OHOS::sptr<V2_1::MockIDevice>(new (std::nothrow) V2_1::MockIDevice());
213 EXPECT_NE(sp, nullptr);
214
215 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(sp);
216 EXPECT_NE(hdiDevice, nullptr);
217
218 V2_1::SharedBuffer buffer {1, 1, 0, 1};
219 EXPECT_CALL(*sp, AllocateBuffer(::testing::_, ::testing::_))
220 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<1>(buffer), ::testing::Return(allocBufferType)));
221
222 std::shared_ptr<PreparedModel> preparedModel;
223 const int position = 2;
224 OHOS::sptr<V2_1::IPreparedModel> iPreparedModel =
225 OHOS::sptr<V2_1::MockIPreparedModel>(new (std::nothrow) V2_1::MockIPreparedModel());
226 EXPECT_CALL(*sp, PrepareModel(::testing::_, ::testing::_, ::testing::_))
227 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<position>(iPreparedModel),
228 ::testing::Return(prepareType)));
229
230 ModelConfig config;
231 OH_NN_ReturnCode result = hdiDevice->PrepareModel(model, config, preparedModel);
232 return result;
233 }
234
235 class MockPreparedModel : public PreparedModel {
236 public:
237 MOCK_METHOD1(ExportModelCache, OH_NN_ReturnCode(std::vector<Buffer>&));
238 MOCK_METHOD4(Run, OH_NN_ReturnCode(const std::vector<IOTensor>&,
239 const std::vector<IOTensor>&,
240 std::vector<std::vector<int32_t>>&,
241 std::vector<bool>&));
242 MOCK_METHOD4(Run, OH_NN_ReturnCode(const std::vector<NN_Tensor*>&,
243 const std::vector<NN_Tensor*>&,
244 std::vector<std::vector<int32_t>>&,
245 std::vector<bool>&));
246 MOCK_CONST_METHOD1(GetModelID, OH_NN_ReturnCode(uint32_t&));
247 MOCK_METHOD2(GetInputDimRanges, OH_NN_ReturnCode(std::vector<std::vector<uint32_t>>&,
248 std::vector<std::vector<uint32_t>>&));
249 };
250
251 /* *
252 * @tc.name: hdidevice_constructor_001
253 * @tc.desc: Verify the Constructor function return object success.
254 * @tc.type: FUNC
255 */
256 HWTEST_F(HDIDeviceTest, hdidevice_constructor_001, TestSize.Level0)
257 {
258 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
259 EXPECT_NE(device, nullptr);
260 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
261 EXPECT_NE(hdiDevice, nullptr);
262 }
263
264 /* *
265 * @tc.name: hdidevice_getdevicename_001
266 * @tc.desc: Verify the GetDeviceName function validate device name success.
267 * @tc.type: FUNC
268 */
269 HWTEST_F(HDIDeviceTest, hdidevice_getdevicename_001, TestSize.Level0)
270 {
271 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
272 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
273 EXPECT_NE(hdiDevice, nullptr);
274 std::string deviceName = "MockDevice";
275 EXPECT_CALL(*((V2_0::MockIDevice *)device.GetRefPtr()), GetDeviceName(::testing::_))
276 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(deviceName), ::testing::Return(HDF_SUCCESS)));
277
278 const std::string expectDeviceName = "MockDevice";
279 std::string newDeviceName = "";
280 OH_NN_ReturnCode result = hdiDevice->GetDeviceName(newDeviceName);
281 EXPECT_EQ(OH_NN_SUCCESS, result);
282 EXPECT_EQ(expectDeviceName, newDeviceName);
283 }
284
285 /* *
286 * @tc.name: hdidevice_getdevicename_002
287 * @tc.desc: Verify the GetDeviceName function return unavailable device.
288 * @tc.type: FUNC
289 */
290 HWTEST_F(HDIDeviceTest, hdidevice_getdevicename_002, TestSize.Level0)
291 {
292 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
293 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
294 EXPECT_NE(hdiDevice, nullptr);
295 std::string deviceName = "MockDevice";
296 EXPECT_CALL(*((V2_0::MockIDevice *)device.GetRefPtr()), GetDeviceName(::testing::_))
297 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(deviceName), ::testing::Return(HDF_FAILURE)));
298 OH_NN_ReturnCode result = hdiDevice->GetDeviceName(deviceName);
299 EXPECT_EQ(OH_NN_UNAVAILABLE_DEVICE, result);
300 }
301
302 /* *
303 * @tc.name: hdidevice_getvendorname_001
304 * @tc.desc: Verify the GetVendorName function validate vendor name success.
305 * @tc.type: FUNC
306 */
307 HWTEST_F(HDIDeviceTest, hdidevice_getvendorname_001, TestSize.Level0)
308 {
309 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
310 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
311 EXPECT_NE(hdiDevice, nullptr);
312 std::string vendorName = "MockVendor";
313 EXPECT_CALL(*((V2_0::MockIDevice *)device.GetRefPtr()), GetVendorName(::testing::_))
314 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(vendorName), ::testing::Return(HDF_SUCCESS)));
315
316 const std::string expectDeviceName = "MockVendor";
317 std::string newVendorName = "";
318 OH_NN_ReturnCode result = hdiDevice->GetVendorName(newVendorName);
319 EXPECT_EQ(OH_NN_SUCCESS, result);
320 EXPECT_EQ(expectDeviceName, newVendorName);
321 }
322
323 /* *
324 * @tc.name: hdidevice_getvendorname_002
325 * @tc.desc: Verify the GetVendorName function return unavailable device.
326 * @tc.type: FUNC
327 */
328 HWTEST_F(HDIDeviceTest, hdidevice_getvendorname_002, TestSize.Level0)
329 {
330 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
331 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
332 EXPECT_NE(hdiDevice, nullptr);
333 std::string vendorName = "MockVendor";
334 EXPECT_CALL(*((V2_0::MockIDevice *)device.GetRefPtr()), GetVendorName(::testing::_))
335 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(vendorName), ::testing::Return(HDF_FAILURE)));
336 OH_NN_ReturnCode result = hdiDevice->GetVendorName(vendorName);
337 EXPECT_EQ(OH_NN_UNAVAILABLE_DEVICE, result);
338 }
339
340 /* *
341 * @tc.name: hdidevice_getversion_001
342 * @tc.desc: Verify the GetVersion function validate vendor name success.
343 * @tc.type: FUNC
344 */
345 HWTEST_F(HDIDeviceTest, hdidevice_getversion_001, TestSize.Level0)
346 {
347 LOGE("GetVersion hdidevice_getversion_001");
348 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
349 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
350 EXPECT_NE(hdiDevice, nullptr);
351 std::string vendorName = "MockVendor";
352 EXPECT_CALL(*((V2_0::MockIDevice *)device.GetRefPtr()), GetVersion(::testing::_, ::testing::_))
353 .WillRepeatedly(::testing::Return(HDF_SUCCESS));
354
355 const std::string expectDeviceName = "MockVendor";
356 std::string newVendorName = "";
357 OH_NN_ReturnCode result = hdiDevice->GetVersion(newVendorName);
358 EXPECT_EQ(OH_NN_SUCCESS, result);
359 }
360
361 /* *
362 * @tc.name: hdidevice_getversion_002
363 * @tc.desc: Verify the GetVersion function return unavailable device.
364 * @tc.type: FUNC
365 */
366 HWTEST_F(HDIDeviceTest, hdidevice_getversion_002, TestSize.Level0)
367 {
368 LOGE("GetVersion hdidevice_getversion_002");
369 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
370 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
371 EXPECT_NE(hdiDevice, nullptr);
372 std::string vendorName = "MockVendor";
373 EXPECT_CALL(*((V2_0::MockIDevice *)device.GetRefPtr()), GetVersion(::testing::_, ::testing::_))
374 .WillRepeatedly(::testing::Return(HDF_FAILURE));
375 OH_NN_ReturnCode result = hdiDevice->GetVersion(vendorName);
376 EXPECT_EQ(OH_NN_UNAVAILABLE_DEVICE, result);
377 }
378
379 /* *
380 * @tc.name: hdidevice_getdevicetype_001
381 * @tc.desc: Verify the GetDeviceType function validate device type success.
382 * @tc.type: FUNC
383 */
384 HWTEST_F(HDIDeviceTest, hdidevice_getdevicetype_001, TestSize.Level0)
385 {
386 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
387 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
388 EXPECT_NE(hdiDevice, nullptr);
389 V2_0::DeviceType iDeviceType = V2_0::DeviceType::CPU;
390 EXPECT_CALL(*((V2_0::MockIDevice *)device.GetRefPtr()), GetDeviceType(::testing::_))
391 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(iDeviceType), ::testing::Return(HDF_SUCCESS)));
392
393 OH_NN_DeviceType expectDeviceType = OH_NN_CPU;
394 OH_NN_DeviceType newDeviceType = OH_NN_CPU;
395 OH_NN_ReturnCode result = hdiDevice->GetDeviceType(newDeviceType);
396 EXPECT_EQ(OH_NN_SUCCESS, result);
397 EXPECT_EQ(expectDeviceType, newDeviceType);
398 }
399
400 /* *
401 * @tc.name: hdidevice_getdevicetype_002
402 * @tc.desc: Verify the GetDeviceType function return unavailable device.
403 * @tc.type: FUNC
404 */
405 HWTEST_F(HDIDeviceTest, hdidevice_getdevicetype_002, TestSize.Level0)
406 {
407 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
408 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
409 EXPECT_NE(hdiDevice, nullptr);
410
411 OH_NN_DeviceType deviceType = OH_NN_CPU;
412 V2_0::DeviceType iDeviceType = V2_0::DeviceType::CPU;
413 EXPECT_CALL(*((V2_0::MockIDevice *)device.GetRefPtr()), GetDeviceType(::testing::_))
414 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(iDeviceType), ::testing::Return(HDF_FAILURE)));
415 OH_NN_ReturnCode result = hdiDevice->GetDeviceType(deviceType);
416 EXPECT_EQ(OH_NN_UNAVAILABLE_DEVICE, result);
417 }
418
419 /* *
420 * @tc.name: hdidevice_getdevicetype_003
421 * @tc.desc: Verify the GetDeviceType function return unavailable device.
422 * @tc.type: FUNC
423 */
424 HWTEST_F(HDIDeviceTest, hdidevice_getdevicetype_003, TestSize.Level0)
425 {
426 LOGE("GetDeviceType hdidevice_getdevicetype_003");
427 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
428 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
429 EXPECT_NE(hdiDevice, nullptr);
430
431 OH_NN_DeviceType deviceType = OH_NN_CPU;
432 EXPECT_CALL(*((V2_0::MockIDevice *)device.GetRefPtr()), GetDeviceType(::testing::_))
__anon339dd2a20102(V2_0::DeviceType& type) 433 .WillRepeatedly(Invoke([](V2_0::DeviceType& type) {
434 // 这里直接修改传入的引用参数
435 type = V2_0::DeviceType::GPU;
436 return OH_NN_SUCCESS; // 假设成功的状态码
437 }));
438 OH_NN_ReturnCode result = hdiDevice->GetDeviceType(deviceType);
439 EXPECT_EQ(OH_NN_SUCCESS, result);
440 }
441
442 /* *
443 * @tc.name: hdidevice_getdevicetype_004
444 * @tc.desc: Verify the GetDeviceType function return unavailable device.
445 * @tc.type: FUNC
446 */
447 HWTEST_F(HDIDeviceTest, hdidevice_getdevicetype_004, TestSize.Level0)
448 {
449 LOGE("GetDeviceType hdidevice_getdevicetype_004");
450 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
451 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
452 EXPECT_NE(hdiDevice, nullptr);
453
454 OH_NN_DeviceType deviceType = OH_NN_CPU;
455 EXPECT_CALL(*((V2_0::MockIDevice *)device.GetRefPtr()), GetDeviceType(::testing::_))
__anon339dd2a20202(V2_0::DeviceType& type) 456 .WillRepeatedly(Invoke([](V2_0::DeviceType& type) {
457 // 这里直接修改传入的引用参数
458 type = V2_0::DeviceType::ACCELERATOR;
459 return OH_NN_SUCCESS; // 假设成功的状态码
460 }));
461 OH_NN_ReturnCode result = hdiDevice->GetDeviceType(deviceType);
462 EXPECT_EQ(OH_NN_SUCCESS, result);
463 }
464
465 /* *
466 * @tc.name: hdidevice_getdevicetype_005
467 * @tc.desc: Verify the GetDeviceType function return unavailable device.
468 * @tc.type: FUNC
469 */
470 HWTEST_F(HDIDeviceTest, hdidevice_getdevicetype_005, TestSize.Level0)
471 {
472 LOGE("GetDeviceType hdidevice_getdevicetype_005");
473 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
474 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
475 EXPECT_NE(hdiDevice, nullptr);
476
477 OH_NN_DeviceType deviceType = OH_NN_CPU;
478 EXPECT_CALL(*((V2_0::MockIDevice *)device.GetRefPtr()), GetDeviceType(::testing::_))
__anon339dd2a20302(V2_0::DeviceType& type) 479 .WillRepeatedly(Invoke([](V2_0::DeviceType& type) {
480 // 这里直接修改传入的引用参数
481 type = V2_0::DeviceType::OTHER;
482 return OH_NN_SUCCESS; // 假设成功的状态码
483 }));
484 OH_NN_ReturnCode result = hdiDevice->GetDeviceType(deviceType);
485 EXPECT_EQ(OH_NN_SUCCESS, result);
486 }
487
488 /* *
489 * @tc.name: hdidevice_getdevicestatus_001
490 * @tc.desc: Verify the GetDeviceStatus function validate device status success.
491 * @tc.type: FUNC
492 */
493 HWTEST_F(HDIDeviceTest, hdidevice_getdevicestatus_001, TestSize.Level0)
494 {
495 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
496 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
497 EXPECT_NE(hdiDevice, nullptr);
498
499 V2_0::DeviceStatus iDeviceStatus = V2_0::DeviceStatus::AVAILABLE;
500 EXPECT_CALL(*((V2_0::MockIDevice *)device.GetRefPtr()), GetDeviceStatus(::testing::_))
501 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(iDeviceStatus), ::testing::Return(HDF_SUCCESS)));
502
503 const DeviceStatus expectDeviceStatus = AVAILABLE;
504 DeviceStatus newDeviceStatus = AVAILABLE;
505 OH_NN_ReturnCode result = hdiDevice->GetDeviceStatus(newDeviceStatus);
506 EXPECT_EQ(OH_NN_SUCCESS, result);
507 EXPECT_EQ(expectDeviceStatus, newDeviceStatus);
508 }
509
510 /* *
511 * @tc.name: hdidevice_getdevicestatus_002
512 * @tc.desc: Verify the GetDeviceStatus function return unavailable device.
513 * @tc.type: FUNC
514 */
515 HWTEST_F(HDIDeviceTest, hdidevice_getdevicestatus_002, TestSize.Level0)
516 {
517 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
518 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
519 EXPECT_NE(hdiDevice, nullptr);
520 DeviceStatus deviceStatus = AVAILABLE;
521 V2_0::DeviceStatus iDeviceStatus = V2_0::DeviceStatus::AVAILABLE;
522 EXPECT_CALL(*((V2_0::MockIDevice *)device.GetRefPtr()), GetDeviceStatus(::testing::_))
523 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(iDeviceStatus), ::testing::Return(HDF_FAILURE)));
524 OH_NN_ReturnCode result = hdiDevice->GetDeviceStatus(deviceStatus);
525 EXPECT_EQ(OH_NN_UNAVAILABLE_DEVICE, result);
526 }
527
528 /* *
529 * @tc.name: hdidevice_getdevicestatus_003
530 * @tc.desc: Verify the GetDeviceStatus function validate device status success.
531 * @tc.type: FUNC
532 */
533 HWTEST_F(HDIDeviceTest, hdidevice_getdevicestatus_003, TestSize.Level0)
534 {
535 LOGE("GetDeviceStatus hdidevice_getdevicestatus_003");
536 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
537 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
538 EXPECT_NE(hdiDevice, nullptr);
539
540 EXPECT_CALL(*((V2_0::MockIDevice *)device.GetRefPtr()), GetDeviceStatus(::testing::_))
__anon339dd2a20402(V2_0::DeviceStatus& status) 541 .WillRepeatedly(Invoke([](V2_0::DeviceStatus& status) {
542 // 这里直接修改传入的引用参数
543 status = V2_0::DeviceStatus::BUSY;
544 return OH_NN_SUCCESS; // 假设成功的状态码
545 }));
546
547 DeviceStatus newDeviceStatus = AVAILABLE;
548 OH_NN_ReturnCode result = hdiDevice->GetDeviceStatus(newDeviceStatus);
549 EXPECT_EQ(OH_NN_SUCCESS, result);
550 }
551
552 /* *
553 * @tc.name: hdidevice_getdevicestatus_004
554 * @tc.desc: Verify the GetDeviceStatus function validate device status success.
555 * @tc.type: FUNC
556 */
557 HWTEST_F(HDIDeviceTest, hdidevice_getdevicestatus_004, TestSize.Level0)
558 {
559 LOGE("GetDeviceStatus hdidevice_getdevicestatus_004");
560 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
561 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
562 EXPECT_NE(hdiDevice, nullptr);
563
564 EXPECT_CALL(*((V2_0::MockIDevice *)device.GetRefPtr()), GetDeviceStatus(::testing::_))
__anon339dd2a20502(V2_0::DeviceStatus& status) 565 .WillRepeatedly(Invoke([](V2_0::DeviceStatus& status) {
566 // 这里直接修改传入的引用参数
567 status = V2_0::DeviceStatus::OFFLINE;
568 return OH_NN_SUCCESS; // 假设成功的状态码
569 }));
570
571 DeviceStatus newDeviceStatus = AVAILABLE;
572 OH_NN_ReturnCode result = hdiDevice->GetDeviceStatus(newDeviceStatus);
573 EXPECT_EQ(OH_NN_SUCCESS, result);
574 }
575
576 /* *
577 * @tc.name: hdidevice_getdevicestatus_005
578 * @tc.desc: Verify the GetDeviceStatus function validate device status success.
579 * @tc.type: FUNC
580 */
581 HWTEST_F(HDIDeviceTest, hdidevice_getdevicestatus_005, TestSize.Level0)
582 {
583 LOGE("GetDeviceStatus hdidevice_getdevicestatus_005");
584 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
585 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
586 EXPECT_NE(hdiDevice, nullptr);
587
588 EXPECT_CALL(*((V2_0::MockIDevice *)device.GetRefPtr()), GetDeviceStatus(::testing::_))
__anon339dd2a20602(V2_0::DeviceStatus& status) 589 .WillRepeatedly(Invoke([](V2_0::DeviceStatus& status) {
590 // 这里直接修改传入的引用参数
591 status = V2_0::DeviceStatus::UNKNOWN;
592 return OH_NN_SUCCESS; // 假设成功的状态码
593 }));
594
595 DeviceStatus newDeviceStatus = AVAILABLE;
596 OH_NN_ReturnCode result = hdiDevice->GetDeviceStatus(newDeviceStatus);
597 EXPECT_EQ(OH_NN_SUCCESS, result);
598 }
599
600 /* *
601 * @tc.name: hdidevice_getsupportedoperation_001
602 * @tc.desc: Verify the GetSupportedOperation function return success.
603 * @tc.type: FUNC
604 */
605 HWTEST_F(HDIDeviceTest, hdidevice_getsupportedoperation_001, TestSize.Level0)
606 {
607 std::vector<bool> ops {true};
608 std::shared_ptr<mindspore::lite::LiteGraph> model =
609 std::shared_ptr<mindspore::lite::LiteGraph>(new (std::nothrow) mindspore::lite::LiteGraph(),
610 mindspore::lite::LiteGraphDeleter);
611 EXPECT_NE(nullptr, model);
612 BuildLiteGraph(model);
613
614 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
615 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
616 EXPECT_NE(hdiDevice, nullptr);
617
618 V2_0::SharedBuffer buffer {1, 1, 0, 1};
619 EXPECT_CALL(*((V2_0::MockIDevice *)device.GetRefPtr()), AllocateBuffer(::testing::_, ::testing::_))
620 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<1>(buffer), ::testing::Return(HDF_SUCCESS)));
621
622 EXPECT_CALL(*((V2_0::MockIDevice *)device.GetRefPtr()), GetSupportedOperation(::testing::_, ::testing::_))
623 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<1>(ops), ::testing::Return(HDF_SUCCESS)));
624
625 std::vector<bool> newOps {true};
626 const std::vector<bool> expectOps {true};
627 OH_NN_ReturnCode result = hdiDevice->GetSupportedOperation(model, newOps);
628 EXPECT_EQ(OH_NN_FAILED, result);
629 auto expectOpsSize = expectOps.size();
630 for (size_t i = 0; i < expectOpsSize; ++i) {
631 EXPECT_EQ(expectOps[i], newOps[i]);
632 }
633 }
634
635 /* *
636 * @tc.name: hdidevice_getsupportedoperation_002
637 * @tc.desc: Verify the GetSupportedOperation function return failed in case of allocate buffer failure.
638 * @tc.type: FUNC
639 */
640 HWTEST_F(HDIDeviceTest, hdidevice_getsupportedoperation_002, TestSize.Level0)
641 {
642 std::vector<bool> ops;
643 std::shared_ptr<mindspore::lite::LiteGraph> model = std::make_shared<mindspore::lite::LiteGraph>();
644 EXPECT_NE(nullptr, model);
645 BuildLiteGraph(model);
646
647 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
648 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
649 EXPECT_NE(hdiDevice, nullptr);
650
651 V2_0::SharedBuffer buffer {1, 1, 0, 1};
652 EXPECT_CALL(*((V2_0::MockIDevice *)device.GetRefPtr()), AllocateBuffer(::testing::_, ::testing::_))
653 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<1>(buffer), ::testing::Return(HDF_FAILURE)));
654
655 OH_NN_ReturnCode result = hdiDevice->GetSupportedOperation(model, ops);
656 EXPECT_EQ(OH_NN_FAILED, result);
657 }
658
659 /* *
660 * @tc.name: hdidevice_getsupportedoperation_003
661 * @tc.desc: Verify the GetSupportedOperation function return nullptr.
662 * @tc.type: FUNC
663 */
664 HWTEST_F(HDIDeviceTest, hdidevice_getsupportedoperation_003, TestSize.Level0)
665 {
666 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
667 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
668 EXPECT_NE(hdiDevice, nullptr);
669
670 std::shared_ptr<const mindspore::lite::LiteGraph> model = nullptr;
671 std::vector<bool> ops;
672 OH_NN_ReturnCode result = hdiDevice->GetSupportedOperation(model, ops);
673 EXPECT_EQ(OH_NN_NULL_PTR, result);
674 }
675
676 /* *
677 * @tc.name: hdidevice_getsupportedoperation_004
678 * @tc.desc: Verify the GetSupportedOperation function return unavalidable device.
679 * @tc.type: FUNC
680 */
681 HWTEST_F(HDIDeviceTest, hdidevice_getsupportedoperation_004, TestSize.Level0)
682 {
683 std::vector<bool> ops {true};
684 std::shared_ptr<mindspore::lite::LiteGraph> model = std::make_shared<mindspore::lite::LiteGraph>();
685 EXPECT_NE(nullptr, model);
686 BuildLiteGraph(model);
687
688 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
689 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
690 EXPECT_NE(hdiDevice, nullptr);
691
692 V2_0::SharedBuffer buffer {2, 1, 0, 1};
693 EXPECT_CALL(*((V2_0::MockIDevice *)device.GetRefPtr()), AllocateBuffer(::testing::_, ::testing::_))
694 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<1>(buffer), ::testing::Return(HDF_SUCCESS)));
695
696 EXPECT_CALL(*((V2_0::MockIDevice *)device.GetRefPtr()), GetSupportedOperation(::testing::_, ::testing::_))
697 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<1>(ops), ::testing::Return(HDF_FAILURE)));
698
699 std::vector<bool> newOps {true};
700 OH_NN_ReturnCode result = hdiDevice->GetSupportedOperation(model, newOps);
701 EXPECT_EQ(OH_NN_FAILED, result);
702 }
703
704 /* *
705 * @tc.name: hdidevice_isfloat16precisionsupported_001
706 * @tc.desc: Verify the IsFloat16PrecisionSupported function return success.
707 * @tc.type: FUNC
708 */
709 HWTEST_F(HDIDeviceTest, hdidevice_isfloat16precisionsupported_001, TestSize.Level0)
710 {
711 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
712 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
713 EXPECT_NE(hdiDevice, nullptr);
714
715 bool isSupported = false;
716 EXPECT_CALL(*((V2_0::MockIDevice *)device.GetRefPtr()), IsFloat16PrecisionSupported(::testing::_))
717 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(isSupported), ::testing::Return(HDF_SUCCESS)));
718 OH_NN_ReturnCode result = hdiDevice->IsFloat16PrecisionSupported(isSupported);
719 EXPECT_EQ(OH_NN_SUCCESS, result);
720 }
721
722 /* *
723 * @tc.name: hdidevice_isfloat16precisionsupported_002
724 * @tc.desc: Verify the IsFloat16PrecisionSupported function return unavailable device.
725 * @tc.type: FUNC
726 */
727 HWTEST_F(HDIDeviceTest, hdidevice_isfloat16precisionsupported_002, TestSize.Level0)
728 {
729 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
730 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
731 EXPECT_NE(hdiDevice, nullptr);
732
733 bool isSupported = false;
734 EXPECT_CALL(*((V2_0::MockIDevice *)device.GetRefPtr()), IsFloat16PrecisionSupported(::testing::_))
735 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(isSupported), ::testing::Return(HDF_FAILURE)));
736 OH_NN_ReturnCode result = hdiDevice->IsFloat16PrecisionSupported(isSupported);
737 EXPECT_EQ(OH_NN_UNAVAILABLE_DEVICE, result);
738 }
739
740 /* *
741 * @tc.name: hdidevice_isperformancemodesupported_001
742 * @tc.desc: Verify the IsPerformanceModeSupported function return success.
743 * @tc.type: FUNC
744 */
745 HWTEST_F(HDIDeviceTest, hdidevice_isperformancemodesupported_001, TestSize.Level0)
746 {
747 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
748 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
749 EXPECT_NE(hdiDevice, nullptr);
750
751 bool isSupported = false;
752 EXPECT_CALL(*((V2_0::MockIDevice *)device.GetRefPtr()), IsPerformanceModeSupported(::testing::_))
753 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(isSupported), ::testing::Return(HDF_SUCCESS)));
754
755 bool newIsSupported = false;
756 const bool expectIsSupported = false;
757 OH_NN_ReturnCode result = hdiDevice->IsPerformanceModeSupported(newIsSupported);
758 EXPECT_EQ(OH_NN_SUCCESS, result);
759 EXPECT_EQ(expectIsSupported, newIsSupported);
760 }
761
762 /* *
763 * @tc.name: hdidevice_isperformancemodesupported_002
764 * @tc.desc: Verify the IsPerformanceModeSupported function return unavailable device.
765 * @tc.type: FUNC
766 */
767 HWTEST_F(HDIDeviceTest, hdidevice_isperformancemodesupported_002, TestSize.Level0)
768 {
769 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
770 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
771 EXPECT_NE(hdiDevice, nullptr);
772
773 bool isSupported = false;
774 EXPECT_CALL(*((V2_0::MockIDevice *)device.GetRefPtr()), IsPerformanceModeSupported(::testing::_))
775 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(isSupported), ::testing::Return(HDF_FAILURE)));
776 OH_NN_ReturnCode result = hdiDevice->IsPerformanceModeSupported(isSupported);
777 EXPECT_EQ(OH_NN_UNAVAILABLE_DEVICE, result);
778 }
779
780 /* *
781 * @tc.name: hdidevice_isprioritysupported_001
782 * @tc.desc: Verify the IsPrioritySupported function return success.
783 * @tc.type: FUNC
784 */
785 HWTEST_F(HDIDeviceTest, hdidevice_isprioritysupported_001, TestSize.Level0)
786 {
787 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
788 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
789 EXPECT_NE(hdiDevice, nullptr);
790
791 bool isSupported = false;
792 EXPECT_CALL(*((V2_0::MockIDevice *)device.GetRefPtr()), IsPrioritySupported(::testing::_))
793 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(isSupported), ::testing::Return(HDF_SUCCESS)));
794
795 bool newIsSupported = false;
796 bool expectIsSupported = false;
797 OH_NN_ReturnCode result = hdiDevice->IsPrioritySupported(newIsSupported);
798 EXPECT_EQ(OH_NN_SUCCESS, result);
799 EXPECT_EQ(newIsSupported, expectIsSupported);
800 }
801
802 /* *
803 * @tc.name: hdidevice_isprioritysupported_002
804 * @tc.desc: Verify the IsPrioritySupported function return unavailable device.
805 * @tc.type: FUNC
806 */
807 HWTEST_F(HDIDeviceTest, hdidevice_isprioritysupported_002, TestSize.Level0)
808 {
809 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
810 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
811 EXPECT_NE(hdiDevice, nullptr);
812
813 bool isSupported = false;
814 EXPECT_CALL(*((V2_0::MockIDevice *)device.GetRefPtr()), IsPrioritySupported(::testing::_))
815 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(isSupported), ::testing::Return(HDF_FAILURE)));
816 OH_NN_ReturnCode result = hdiDevice->IsPrioritySupported(isSupported);
817 EXPECT_EQ(OH_NN_UNAVAILABLE_DEVICE, result);
818 }
819
820 /* *
821 * @tc.name: hdidevice_isdynamicinputsupported_001
822 * @tc.desc: Verify the IsDynamicInputSupported function return success.
823 * @tc.type: FUNC
824 */
825 HWTEST_F(HDIDeviceTest, hdidevice_isdynamicinputsupported_001, TestSize.Level0)
826 {
827 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
828 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
829 EXPECT_NE(hdiDevice, nullptr);
830
831 bool isSupported = false;
832 EXPECT_CALL(*((V2_0::MockIDevice *)device.GetRefPtr()), IsDynamicInputSupported(::testing::_))
833 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(isSupported), ::testing::Return(HDF_SUCCESS)));
834
835 bool newIsSupported = false;
836 bool expectIsSupported = false;
837 OH_NN_ReturnCode result = hdiDevice->IsDynamicInputSupported(newIsSupported);
838 EXPECT_EQ(OH_NN_SUCCESS, result);
839 EXPECT_EQ(newIsSupported, expectIsSupported);
840 }
841
842 /* *
843 * @tc.name: hdidevice_isdynamicinputsupported_002
844 * @tc.desc: Verify the IsDynamicInputSupported function return unavailable device.
845 * @tc.type: FUNC
846 */
847 HWTEST_F(HDIDeviceTest, hdidevice_isdynamicinputsupported_002, TestSize.Level0)
848 {
849 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
850 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
851 EXPECT_NE(hdiDevice, nullptr);
852
853 bool isSupported = false;
854 EXPECT_CALL(*((V2_0::MockIDevice *)device.GetRefPtr()), IsDynamicInputSupported(::testing::_))
855 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(isSupported), ::testing::Return(HDF_FAILURE)));
856 OH_NN_ReturnCode result = hdiDevice->IsDynamicInputSupported(isSupported);
857 EXPECT_EQ(OH_NN_UNAVAILABLE_DEVICE, result);
858 }
859
860 /* *
861 * @tc.name: hdidevice_isdynamicinputsupported_001
862 * @tc.desc: Verify the IsModelCacheSupported function return success.
863 * @tc.type: FUNC
864 */
865 HWTEST_F(HDIDeviceTest, hdidevice_ismodelcachesupported_001, TestSize.Level0)
866 {
867 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
868 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
869 EXPECT_NE(hdiDevice, nullptr);
870
871 bool isSupported = false;
872 EXPECT_CALL(*((V2_0::MockIDevice *)device.GetRefPtr()), IsModelCacheSupported(::testing::_))
873 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(isSupported), ::testing::Return(HDF_SUCCESS)));
874
875 bool newIsSupported = false;
876 bool expectIsSupported = false;
877 OH_NN_ReturnCode result = hdiDevice->IsModelCacheSupported(newIsSupported);
878 EXPECT_EQ(OH_NN_SUCCESS, result);
879 EXPECT_EQ(expectIsSupported, newIsSupported);
880 }
881
882 /* *
883 * @tc.name: hdidevice_isdynamicinputsupported_002
884 * @tc.desc: Verify the IsModelCacheSupported function return unavailable device.
885 * @tc.type: FUNC
886 */
887 HWTEST_F(HDIDeviceTest, hdidevice_ismodelcachesupported_002, TestSize.Level0)
888 {
889 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
890 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
891 EXPECT_NE(hdiDevice, nullptr);
892
893 bool isSupported = false;
894 EXPECT_CALL(*((V2_0::MockIDevice *)device.GetRefPtr()), IsModelCacheSupported(::testing::_))
895 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(isSupported), ::testing::Return(HDF_FAILURE)));
896 OH_NN_ReturnCode result = hdiDevice->IsModelCacheSupported(isSupported);
897 EXPECT_EQ(OH_NN_UNAVAILABLE_DEVICE, result);
898 }
899
900 /* *
901 * @tc.name: hdidevice_preparemodel_001
902 * @tc.desc: Verify the PrepareModel function return success.
903 * @tc.type: FUNC
904 */
905 HWTEST_F(HDIDeviceTest, hdidevice_preparemodel_001, TestSize.Level0)
906 {
907 int32_t allocBufferType = HDF_SUCCESS;
908 int32_t prepareType = HDF_SUCCESS;
909 OH_NN_ReturnCode result = PrepareModel(allocBufferType, prepareType);
910 EXPECT_EQ(OH_NN_FAILED, result);
911 }
912
913 /* *
914 * @tc.name: hdidevice_preparemodel_002
915 * @tc.desc: Verify the PrepareModel function return invalid parameter.
916 * @tc.type: FUNC
917 */
918 HWTEST_F(HDIDeviceTest, hdidevice_preparemodel_002, TestSize.Level0)
919 {
920 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
921 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
922 EXPECT_NE(hdiDevice, nullptr);
923
924 std::shared_ptr<const mindspore::lite::LiteGraph> model = nullptr;
925 ModelConfig config;
926 std::shared_ptr<PreparedModel> preparedModel;
927 OH_NN_ReturnCode result = hdiDevice->PrepareModel(model, config, preparedModel);
928 EXPECT_EQ(OH_NN_INVALID_PARAMETER, result);
929 }
930
931 /* *
932 * @tc.name: hdidevice_preparemodel_003
933 * @tc.desc: Verify the PrepareModel function return failed.
934 * @tc.type: FUNC
935 */
936 HWTEST_F(HDIDeviceTest, hdidevice_preparemodel_003, TestSize.Level0)
937 {
938 int32_t allocBufferType = HDF_SUCCESS;
939 int32_t prepareType = HDF_FAILURE;
940 OH_NN_ReturnCode result = PrepareModel(allocBufferType, prepareType);
941 EXPECT_EQ(OH_NN_FAILED, result);
942 }
943
944 /* *
945 * @tc.name: hdidevice_preparemodel_004
946 * @tc.desc: Verify the PrepareModel function return failed.
947 * @tc.type: FUNC
948 */
949 HWTEST_F(HDIDeviceTest, hdidevice_preparemodel_004, TestSize.Level0)
950 {
951 int32_t allocBufferType = HDF_FAILURE;
952 int32_t prepareType = HDF_FAILURE;
953 OH_NN_ReturnCode result = PrepareModel(allocBufferType, prepareType);
954 EXPECT_EQ(OH_NN_FAILED, result);
955 }
956
957 /* *
958 * @tc.name: hdidevice_preparemodel_005
959 * @tc.desc: Verify the PrepareModel function return failed.
960 * @tc.type: FUNC
961 */
962 HWTEST_F(HDIDeviceTest, hdidevice_preparemodel_005, TestSize.Level0)
963 {
964 LOGE("PrepareModel hdidevice_preparemodel_005");
965 OHOS::sptr<V2_0::MockIDevice> sp = OHOS::sptr<V2_0::MockIDevice>(new (std::nothrow) V2_0::MockIDevice());
966 EXPECT_NE(sp, nullptr);
967
968 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(sp);
969 EXPECT_NE(hdiDevice, nullptr);
970
971 float dataArry[9] {0, 1, 2, 3, 4, 5, 6, 7, 8};
972 void* data = dataArry;
973 ModelConfig config;
974 std::shared_ptr<PreparedModel> preparedModel;
975 OH_NN_ReturnCode result = hdiDevice->PrepareModel(data, config, preparedModel);
976 EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, result);
977 }
978
979 /* *
980 * @tc.name: hdidevice_preparemodelfrommodelcache_001
981 * @tc.desc: Verify the PrepareModelFromModelCache function return success.
982 * @tc.type: FUNC
983 */
984 HWTEST_F(HDIDeviceTest, hdidevice_preparemodelfrommodelcache_001, TestSize.Level0)
985 {
986 size_t length = 100;
987 void *buffer = nullptr;
988 GetBuffer(buffer, length);
989
990 std::vector<Buffer> modelCache = { { buffer, 100 } };
991 ModelConfig config;
992
993 OHOS::sptr<V2_0::MockIDevice> sp = OHOS::sptr<V2_0::MockIDevice>(new (std::nothrow) V2_0::MockIDevice());
994 EXPECT_NE(sp, nullptr);
995
996 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(sp);
997 EXPECT_NE(hdiDevice, nullptr);
998
999 std::shared_ptr<PreparedModel> preparedModel;
1000
1001 OHOS::sptr<V2_0::IPreparedModel> iPreparedModel =
1002 OHOS::sptr<V2_0::MockIPreparedModel>(new (std::nothrow) V2_0::MockIPreparedModel());
1003 EXPECT_CALL(*sp, PrepareModelFromModelCache(::testing::_, ::testing::_, ::testing::_))
1004 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<2>(iPreparedModel), ::testing::Return(HDF_SUCCESS)));
1005
1006 bool isUpdatable = false;
1007 OH_NN_ReturnCode result = hdiDevice->PrepareModelFromModelCache(modelCache, config, preparedModel, isUpdatable);
1008 const auto &memoryManager = MemoryManager::GetInstance();
1009 memoryManager->UnMapMemory(buffer);
1010 EXPECT_EQ(OH_NN_SUCCESS, result);
1011 }
1012
1013 /* *
1014 * @tc.name: hdidevice_preparemodelfrommodelcache_002
1015 * @tc.desc: Verify the PrepareModelFromModelCache function return unavailable device.
1016 * @tc.type: FUNC
1017 */
1018 HWTEST_F(HDIDeviceTest, hdidevice_preparemodelfrommodelcache_002, TestSize.Level0)
1019 {
1020 size_t length = 100;
1021 void *buffer = nullptr;
1022 GetBuffer(buffer, length);
1023
1024 OHOS::sptr<V2_0::MockIDevice> sp = OHOS::sptr<V2_0::MockIDevice>(new (std::nothrow) V2_0::MockIDevice());
1025 EXPECT_NE(sp, nullptr);
1026
1027 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(sp);
1028 EXPECT_NE(hdiDevice, nullptr);
1029
1030 std::vector<Buffer> modelCache = { { buffer, 100 } };
1031 ModelConfig config;
1032 OHOS::sptr<V2_0::IPreparedModel> preModel =
1033 OHOS::sptr<V2_0::MockIPreparedModel>(new (std::nothrow) V2_0::MockIPreparedModel());
1034 EXPECT_NE(preModel, nullptr);
1035
1036 std::shared_ptr<PreparedModel> preparedModel = std::make_shared<HDIPreparedModelV2_0>(preModel);
1037
1038 OHOS::sptr<V2_0::IPreparedModel> iPreparedModel =
1039 OHOS::sptr<V2_0::MockIPreparedModel>(new (std::nothrow) V2_0::MockIPreparedModel);
1040 EXPECT_CALL(*sp, PrepareModelFromModelCache(::testing::_, ::testing::_, ::testing::_))
1041 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<2>(iPreparedModel), ::testing::Return(HDF_FAILURE)));
1042
1043 bool isUpdatable = false;
1044 OH_NN_ReturnCode result = hdiDevice->PrepareModelFromModelCache(modelCache, config, preparedModel, isUpdatable);
1045 EXPECT_EQ(OH_NN_FAILED, result);
1046 }
1047
1048 /* *
1049 * @tc.name: hdidevice_preparemodelfrommodelcache_003
1050 * @tc.desc: Verify the PrepareModelFromModelCache function return nullptr.
1051 * @tc.type: FUNC
1052 */
1053 HWTEST_F(HDIDeviceTest, hdidevice_preparemodelfrommodelcache_003, TestSize.Level0)
1054 {
1055 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
1056 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
1057 EXPECT_NE(hdiDevice, nullptr);
1058
1059 std::vector<Buffer> modelCache = { { nullptr, 0 } };
1060 ModelConfig config;
1061 std::shared_ptr<PreparedModel> preparedModel;
1062 bool isUpdatable = false;
1063 OH_NN_ReturnCode result = hdiDevice->PrepareModelFromModelCache(modelCache, config, preparedModel, isUpdatable);
1064 EXPECT_EQ(OH_NN_NULL_PTR, result);
1065 }
1066
1067 /* *
1068 * @tc.name: hdidevice_preparemodelfrommodelcache_004
1069 * @tc.desc: Verify the PrepareModelFromModelCache function return success.
1070 * @tc.type: FUNC
1071 */
1072 HWTEST_F(HDIDeviceTest, hdidevice_preparemodelfrommodelcache_004, TestSize.Level0)
1073 {
1074 LOGE("GetDeviceStatus hdidevice_getdevicestatus_005");
1075 size_t length = 100;
1076 void *buffer = nullptr;
1077 GetBuffer(buffer, length);
1078
1079 OHOS::sptr<V2_0::MockIDevice> sp = OHOS::sptr<V2_0::MockIDevice>(new (std::nothrow) V2_0::MockIDevice());
1080 EXPECT_NE(sp, nullptr);
1081
1082 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(sp);
1083 EXPECT_NE(hdiDevice, nullptr);
1084
1085 std::vector<Buffer> modelCache = { { buffer, 100 } };
1086 ModelConfig config;
1087 config.mode = OH_NN_PERFORMANCE_LOW;
1088 config.priority = OH_NN_PRIORITY_LOW;
1089 OHOS::sptr<V2_0::IPreparedModel> preModel =
1090 OHOS::sptr<V2_0::MockIPreparedModel>(new (std::nothrow) V2_0::MockIPreparedModel());
1091 EXPECT_NE(preModel, nullptr);
1092
1093 std::shared_ptr<PreparedModel> preparedModel = std::make_shared<HDIPreparedModelV2_0>(preModel);
1094
1095 OHOS::sptr<V2_0::IPreparedModel> iPreparedModel =
1096 OHOS::sptr<V2_0::MockIPreparedModel>(new (std::nothrow) V2_0::MockIPreparedModel);
1097 EXPECT_CALL(*sp, PrepareModelFromModelCache(::testing::_, ::testing::_, ::testing::_))
1098 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<2>(iPreparedModel), ::testing::Return(HDF_FAILURE)));
1099
1100 bool isUpdatable = false;
1101 OH_NN_ReturnCode result = hdiDevice->PrepareModelFromModelCache(modelCache, config, preparedModel, isUpdatable);
1102 EXPECT_EQ(OH_NN_FAILED, result);
1103 }
1104
1105 /* *
1106 * @tc.name: hdidevice_preparemodelfrommodelcache_005
1107 * @tc.desc: Verify the PrepareModelFromModelCache function return success.
1108 * @tc.type: FUNC
1109 */
1110 HWTEST_F(HDIDeviceTest, hdidevice_preparemodelfrommodelcache_005, TestSize.Level0)
1111 {
1112 LOGE("GetDeviceStatus hdidevice_preparemodelfrommodelcache_005");
1113 size_t length = 100;
1114 void *buffer = nullptr;
1115 GetBuffer(buffer, length);
1116
1117 OHOS::sptr<V2_0::MockIDevice> sp = OHOS::sptr<V2_0::MockIDevice>(new (std::nothrow) V2_0::MockIDevice());
1118 EXPECT_NE(sp, nullptr);
1119
1120 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(sp);
1121 EXPECT_NE(hdiDevice, nullptr);
1122
1123 std::vector<Buffer> modelCache = { { buffer, 100 } };
1124 ModelConfig config;
1125 config.mode = OH_NN_PERFORMANCE_MEDIUM;
1126 config.priority = OH_NN_PRIORITY_MEDIUM;
1127 OHOS::sptr<V2_0::IPreparedModel> preModel =
1128 OHOS::sptr<V2_0::MockIPreparedModel>(new (std::nothrow) V2_0::MockIPreparedModel());
1129 EXPECT_NE(preModel, nullptr);
1130
1131 std::shared_ptr<PreparedModel> preparedModel = std::make_shared<HDIPreparedModelV2_0>(preModel);
1132
1133 OHOS::sptr<V2_0::IPreparedModel> iPreparedModel =
1134 OHOS::sptr<V2_0::MockIPreparedModel>(new (std::nothrow) V2_0::MockIPreparedModel);
1135 EXPECT_CALL(*sp, PrepareModelFromModelCache(::testing::_, ::testing::_, ::testing::_))
1136 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<2>(iPreparedModel), ::testing::Return(HDF_FAILURE)));
1137
1138 bool isUpdatable = false;
1139 OH_NN_ReturnCode result = hdiDevice->PrepareModelFromModelCache(modelCache, config, preparedModel, isUpdatable);
1140 EXPECT_EQ(OH_NN_FAILED, result);
1141 }
1142
1143 /* *
1144 * @tc.name: hdidevice_preparemodelfrommodelcache_006
1145 * @tc.desc: Verify the PrepareModelFromModelCache function return success.
1146 * @tc.type: FUNC
1147 */
1148 HWTEST_F(HDIDeviceTest, hdidevice_preparemodelfrommodelcache_006, TestSize.Level0)
1149 {
1150 LOGE("GetDeviceStatus hdidevice_preparemodelfrommodelcache_006");
1151 size_t length = 100;
1152 void *buffer = nullptr;
1153 GetBuffer(buffer, length);
1154
1155 OHOS::sptr<V2_0::MockIDevice> sp = OHOS::sptr<V2_0::MockIDevice>(new (std::nothrow) V2_0::MockIDevice());
1156 EXPECT_NE(sp, nullptr);
1157
1158 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(sp);
1159 EXPECT_NE(hdiDevice, nullptr);
1160
1161 std::vector<Buffer> modelCache = { { buffer, 100 } };
1162 ModelConfig config;
1163 config.mode = OH_NN_PERFORMANCE_HIGH;
1164 config.priority = OH_NN_PRIORITY_HIGH;
1165 OHOS::sptr<V2_0::IPreparedModel> preModel =
1166 OHOS::sptr<V2_0::MockIPreparedModel>(new (std::nothrow) V2_0::MockIPreparedModel());
1167 EXPECT_NE(preModel, nullptr);
1168
1169 std::shared_ptr<PreparedModel> preparedModel = std::make_shared<HDIPreparedModelV2_0>(preModel);
1170
1171 OHOS::sptr<V2_0::IPreparedModel> iPreparedModel =
1172 OHOS::sptr<V2_0::MockIPreparedModel>(new (std::nothrow) V2_0::MockIPreparedModel);
1173 EXPECT_CALL(*sp, PrepareModelFromModelCache(::testing::_, ::testing::_, ::testing::_))
1174 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<2>(iPreparedModel), ::testing::Return(HDF_FAILURE)));
1175
1176 bool isUpdatable = false;
1177 OH_NN_ReturnCode result = hdiDevice->PrepareModelFromModelCache(modelCache, config, preparedModel, isUpdatable);
1178 EXPECT_EQ(OH_NN_FAILED, result);
1179 }
1180
1181 /* *
1182 * @tc.name: hdidevice_preparemodelfrommodelcache_007
1183 * @tc.desc: Verify the PrepareModelFromModelCache function return success.
1184 * @tc.type: FUNC
1185 */
1186 HWTEST_F(HDIDeviceTest, hdidevice_preparemodelfrommodelcache_007, TestSize.Level0)
1187 {
1188 LOGE("GetDeviceStatus hdidevice_preparemodelfrommodelcache_007");
1189 size_t length = 100;
1190 void *buffer = nullptr;
1191 GetBuffer(buffer, length);
1192
1193 OHOS::sptr<V2_0::MockIDevice> sp = OHOS::sptr<V2_0::MockIDevice>(new (std::nothrow) V2_0::MockIDevice());
1194 EXPECT_NE(sp, nullptr);
1195
1196 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(sp);
1197 EXPECT_NE(hdiDevice, nullptr);
1198
1199 std::vector<Buffer> modelCache = { { buffer, 100 } };
1200 ModelConfig config;
1201 config.mode = OH_NN_PERFORMANCE_EXTREME;
1202 OHOS::sptr<V2_0::IPreparedModel> preModel =
1203 OHOS::sptr<V2_0::MockIPreparedModel>(new (std::nothrow) V2_0::MockIPreparedModel());
1204 EXPECT_NE(preModel, nullptr);
1205
1206 std::shared_ptr<PreparedModel> preparedModel = std::make_shared<HDIPreparedModelV2_0>(preModel);
1207
1208 OHOS::sptr<V2_0::IPreparedModel> iPreparedModel =
1209 OHOS::sptr<V2_0::MockIPreparedModel>(new (std::nothrow) V2_0::MockIPreparedModel);
1210 EXPECT_CALL(*sp, PrepareModelFromModelCache(::testing::_, ::testing::_, ::testing::_))
1211 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<2>(iPreparedModel), ::testing::Return(HDF_FAILURE)));
1212
1213 bool isUpdatable = false;
1214 OH_NN_ReturnCode result = hdiDevice->PrepareModelFromModelCache(modelCache, config, preparedModel, isUpdatable);
1215 EXPECT_EQ(OH_NN_FAILED, result);
1216 }
1217
1218 /* *
1219 * @tc.name: hdidevice_allocatebuffer_001
1220 * @tc.desc: Verify the AllocateBuffer function return nullptr.
1221 * @tc.type: FUNC
1222 */
1223 HWTEST_F(HDIDeviceTest, hdidevice_allocatebuffer_001, TestSize.Level0)
1224 {
1225 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
1226 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
1227 EXPECT_NE(hdiDevice, nullptr);
1228
1229 V2_0::SharedBuffer buffer;
1230 EXPECT_CALL(*((V2_0::MockIDevice *)device.GetRefPtr()), AllocateBuffer(::testing::_, ::testing::_))
1231 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<1>(buffer), ::testing::Return(HDF_FAILURE)));
1232
1233 size_t length = 8;
1234 void *result = hdiDevice->AllocateBuffer(length);
1235 EXPECT_EQ(nullptr, result);
1236 hdiDevice->ReleaseBuffer(result);
1237 }
1238
1239 /* *
1240 * @tc.name: hdidevice_allocatebuffer_002
1241 * @tc.desc: Verify the AllocateBuffer function return nullptr and HDF_FAILURE.
1242 * @tc.type: FUNC
1243 */
1244 HWTEST_F(HDIDeviceTest, hdidevice_allocatebuffer_002, TestSize.Level0)
1245 {
1246 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
1247 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
1248 EXPECT_NE(hdiDevice, nullptr);
1249
1250 size_t length = 8;
1251 void *result = hdiDevice->AllocateBuffer(length);
1252 EXPECT_EQ(nullptr, result);
1253 hdiDevice->ReleaseBuffer(result);
1254 }
1255
1256 /* *
1257 * @tc.name: hdidevice_allocatebuffer_003
1258 * @tc.desc: Verify the AllocateBuffer function return nullptr in case of 0 size.
1259 * @tc.type: FUNC
1260 */
1261 HWTEST_F(HDIDeviceTest, hdidevice_allocatebuffer_003, TestSize.Level0)
1262 {
1263 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
1264 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
1265 EXPECT_NE(hdiDevice, nullptr);
1266
1267 size_t length = 0;
1268 void *result = hdiDevice->AllocateBuffer(length);
1269 EXPECT_EQ(nullptr, result);
1270 }
1271
1272 /* *
1273 * @tc.name: hdidevice_allocatebuffer_004
1274 * @tc.desc: Verify the AllocateBuffer function return nullptr in case of 0 size.
1275 * @tc.type: FUNC
1276 */
1277 HWTEST_F(HDIDeviceTest, hdidevice_allocatebuffer_004, TestSize.Level0)
1278 {
1279 LOGE("AllocateBuffer hdidevice_preparemodelfrommodelcache_007");
1280 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
1281 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
1282 EXPECT_NE(hdiDevice, nullptr);
1283
1284 size_t length = 0;
1285 int fd = 0;
1286 OH_NN_ReturnCode result = hdiDevice->AllocateBuffer(length, fd);
1287 EXPECT_EQ(OH_NN_INVALID_PARAMETER, result);
1288 }
1289
1290 /* *
1291 * @tc.name: hdidevice_allocatebuffer_005
1292 * @tc.desc: Verify the AllocateBuffer function return nullptr in case of 0 size.
1293 * @tc.type: FUNC
1294 */
1295 HWTEST_F(HDIDeviceTest, hdidevice_allocatebuffer_005, TestSize.Level0)
1296 {
1297 LOGE("AllocateBuffer hdidevice_allocatebuffer_005");
1298 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
1299 EXPECT_CALL(*((V2_0::MockIDevice *)device.GetRefPtr()), AllocateBuffer(::testing::_, ::testing::_))
1300 .WillRepeatedly(::testing::Return(HDF_FAILURE));
1301
1302 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
1303 EXPECT_NE(hdiDevice, nullptr);
1304
1305 size_t length = 1;
1306 int fd = 0;
1307 OH_NN_ReturnCode result = hdiDevice->AllocateBuffer(length, fd);
1308 EXPECT_EQ(OH_NN_MEMORY_ERROR, result);
1309 }
1310
1311 /* *
1312 * @tc.name: hdidevice_allocatebuffer_006
1313 * @tc.desc: Verify the AllocateBuffer function return nullptr in case of 0 size.
1314 * @tc.type: FUNC
1315 */
1316 HWTEST_F(HDIDeviceTest, hdidevice_allocatebuffer_006, TestSize.Level0)
1317 {
1318 LOGE("AllocateBuffer hdidevice_allocatebuffer_006");
1319 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
1320 EXPECT_CALL(*((V2_0::MockIDevice *)device.GetRefPtr()), AllocateBuffer(::testing::_, ::testing::_))
1321 .WillRepeatedly(::testing::Return(HDF_SUCCESS));
1322
1323 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
1324 EXPECT_NE(hdiDevice, nullptr);
1325
1326 size_t length = 1;
1327 int fd = 0;
1328 OH_NN_ReturnCode result = hdiDevice->AllocateBuffer(length, fd);
1329 EXPECT_EQ(OH_NN_SUCCESS, result);
1330 }
1331
1332 /* *
1333 * @tc.name: hdidevice_releasebuffer_001
1334 * @tc.desc: Verify the ReleaseBuffer function validate buffer success.
1335 * @tc.type: FUNC
1336 */
1337 HWTEST_F(HDIDeviceTest, hdidevice_releasebuffer_001, TestSize.Level0)
1338 {
1339 size_t length = 100;
1340 void *buffer = nullptr;
1341 GetBuffer(buffer, length);
1342
1343 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
1344 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
1345
1346 EXPECT_CALL(*((V2_0::MockIDevice *)device.GetRefPtr()), ReleaseBuffer(::testing::_))
1347 .WillRepeatedly(::testing::Return(HDF_SUCCESS));
1348
1349 EXPECT_NE(hdiDevice, nullptr);
1350 hdiDevice->ReleaseBuffer(buffer);
1351 const auto &memoryManager = MemoryManager::GetInstance();
1352 memoryManager->UnMapMemory(buffer);
1353 }
1354
1355 /* *
1356 * @tc.name: hdidevice_releasebuffer_002
1357 * @tc.desc: Verify the ReleaseBuffer function validate AllocateBuffer return nullptr.
1358 * @tc.type: FUNC
1359 */
1360 HWTEST_F(HDIDeviceTest, hdidevice_releasebuffer_002, TestSize.Level0)
1361 {
1362 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
1363 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
1364 EXPECT_NE(hdiDevice, nullptr);
1365
1366 V2_0::SharedBuffer sharedbuffer;
1367 EXPECT_CALL(*((V2_0::MockIDevice *)device.GetRefPtr()), AllocateBuffer(::testing::_, ::testing::_))
1368 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<1>(sharedbuffer), ::testing::Return(HDF_FAILURE)));
1369
1370 EXPECT_CALL(*((V2_0::MockIDevice *)device.GetRefPtr()), ReleaseBuffer(::testing::_))
1371 .WillRepeatedly(::testing::Return(HDF_FAILURE));
1372
1373 size_t length = 8;
1374 void *buffer = hdiDevice->AllocateBuffer(length);
1375 hdiDevice->ReleaseBuffer(buffer);
1376 }
1377
1378 /* *
1379 * @tc.name: hdidevice_releasebuffer_003
1380 * @tc.desc: Verify the ReleaseBuffer function validate param buffer is nullptr.
1381 * @tc.type: FUNC
1382 */
1383 HWTEST_F(HDIDeviceTest, hdidevice_releasebuffer_003, TestSize.Level0)
1384 {
1385 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
1386 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
1387 EXPECT_NE(hdiDevice, nullptr);
1388
1389 void *buffer = nullptr;
1390 hdiDevice->ReleaseBuffer(buffer);
1391 }
1392
1393 /* *
1394 * @tc.name: hdidevice_releasebuffer_004
1395 * @tc.desc: Verify the ReleaseBuffer function validate invalid buffer.
1396 * @tc.type: FUNC
1397 */
1398 HWTEST_F(HDIDeviceTest, hdidevice_releasebuffer_004, TestSize.Level0)
1399 {
1400 const size_t length = 100;
1401 auto* buffer = new(std::nothrow) char[length];
1402 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
1403 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
1404 EXPECT_NE(hdiDevice, nullptr);
1405
1406 hdiDevice->ReleaseBuffer(buffer);
1407 delete[] buffer;
1408 buffer = nullptr;
1409 }
1410
1411 /* *
1412 * @tc.name: hdidevice_releasebuffer_005
1413 * @tc.desc: Verify the ReleaseBuffer function validate moc object's ReleaseBuffer return failure.
1414 * @tc.type: FUNC
1415 */
1416 HWTEST_F(HDIDeviceTest, hdidevice_releasebuffer_005, TestSize.Level0)
1417 {
1418 size_t length = 100;
1419 void *buffer = nullptr;
1420 GetBuffer(buffer, length);
1421
1422 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
1423 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
1424 EXPECT_NE(hdiDevice, nullptr);
1425
1426 EXPECT_CALL(*((V2_0::MockIDevice *)device.GetRefPtr()), ReleaseBuffer(::testing::_))
1427 .WillRepeatedly(::testing::Return(HDF_FAILURE));
1428
1429 hdiDevice->ReleaseBuffer(buffer);
1430 const auto &memoryManager = MemoryManager::GetInstance();
1431 memoryManager->UnMapMemory(buffer);
1432 }
1433
1434 /* *
1435 * @tc.name: hdidevice_releasebuffer_007
1436 * @tc.desc: Verify the ReleaseBuffer function validate moc object's ReleaseBuffer return failure.
1437 * @tc.type: FUNC
1438 */
1439 HWTEST_F(HDIDeviceTest, hdidevice_releasebuffer_007, TestSize.Level0)
1440 {
1441 LOGE("ReleaseBuffer hdidevice_releasebuffer_007");
1442 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
1443 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
1444 EXPECT_NE(hdiDevice, nullptr);
1445
1446 EXPECT_CALL(*((V2_0::MockIDevice *)device.GetRefPtr()), ReleaseBuffer(::testing::_))
1447 .WillRepeatedly(::testing::Return(HDF_FAILURE));
1448
1449 int fd = 0;
1450 size_t length = 1;
1451 OH_NN_ReturnCode ret = hdiDevice->ReleaseBuffer(fd, length);
1452 EXPECT_EQ(OH_NN_FAILED, ret);
1453 }
1454
1455 /* *
1456 * @tc.name: hdidevice_releasebuffer_008
1457 * @tc.desc: Verify the ReleaseBuffer function validate moc object's ReleaseBuffer return failure.
1458 * @tc.type: FUNC
1459 */
1460 HWTEST_F(HDIDeviceTest, hdidevice_releasebuffer_008, TestSize.Level0)
1461 {
1462 LOGE("ReleaseBuffer hdidevice_releasebuffer_008");
1463 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
1464 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
1465 EXPECT_NE(hdiDevice, nullptr);
1466
1467 EXPECT_CALL(*((V2_0::MockIDevice *)device.GetRefPtr()), ReleaseBuffer(::testing::_))
1468 .WillRepeatedly(::testing::Return(HDF_SUCCESS));
1469
1470 int fd = 0;
1471 size_t length = 1;
1472 OH_NN_ReturnCode ret = hdiDevice->ReleaseBuffer(fd, length);
1473 EXPECT_EQ(OH_NN_SUCCESS, ret);
1474 }
1475
1476 /* *
1477 * @tc.name: hdidevice_allocatetensorbuffer_001
1478 * @tc.desc: Verify the ReleaseBuffer function validate moc object's ReleaseBuffer return failure.
1479 * @tc.type: FUNC
1480 */
1481 HWTEST_F(HDIDeviceTest, hdidevice_allocatetensorbuffer_001, TestSize.Level0)
1482 {
1483 LOGE("AllocateTensorBuffer hdidevice_allocatetensorbuffer_001");
1484 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
1485 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
1486 EXPECT_NE(hdiDevice, nullptr);
1487
1488 size_t length = 0;
1489 std::shared_ptr<TensorDesc> tensor;
1490 void* ret = hdiDevice->AllocateTensorBuffer(length, tensor);
1491 EXPECT_EQ(nullptr, ret);
1492 }
1493
1494 /* *
1495 * @tc.name: hdidevice_allocatetensorbuffer_002
1496 * @tc.desc: Verify the ReleaseBuffer function validate moc object's ReleaseBuffer return failure.
1497 * @tc.type: FUNC
1498 */
1499 HWTEST_F(HDIDeviceTest, hdidevice_allocatetensorbuffer_002, TestSize.Level0)
1500 {
1501 LOGE("AllocateTensorBuffer hdidevice_allocatetensorbuffer_002");
1502 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
1503 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
1504 EXPECT_NE(hdiDevice, nullptr);
1505
1506 size_t length = 0;
1507 std::shared_ptr<NNTensor> tensor;
1508 void* ret = hdiDevice->AllocateTensorBuffer(length, tensor);
1509 EXPECT_EQ(nullptr, ret);
1510 }
1511
1512 /* *
1513 * @tc.name: hdidevice_prepareofflinemodel_001
1514 * @tc.desc: Verify the ReleaseBuffer function validate moc object's ReleaseBuffer return failure.
1515 * @tc.type: FUNC
1516 */
1517 HWTEST_F(HDIDeviceTest, hdidevice_prepareofflinemodel_001, TestSize.Level0)
1518 {
1519 LOGE("PrepareOfflineModel hdidevice_prepareofflinemodel_001");
1520 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
1521 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
1522 EXPECT_NE(hdiDevice, nullptr);
1523
1524 ModelConfig config;
1525 std::shared_ptr<PreparedModel> preparedModel;
1526 OH_NN_ReturnCode ret = hdiDevice->PrepareOfflineModel(nullptr, config, preparedModel);
1527 EXPECT_EQ(OH_NN_NULL_PTR, ret);
1528 }
1529
1530 /* *
1531 * @tc.name: hdidevice_prepareofflinemodel_002
1532 * @tc.desc: Verify the ReleaseBuffer function validate moc object's ReleaseBuffer return failure.
1533 * @tc.type: FUNC
1534 */
1535 HWTEST_F(HDIDeviceTest, hdidevice_prepareofflinemodel_002, TestSize.Level0)
1536 {
1537 LOGE("PrepareOfflineModel hdidevice_prepareofflinemodel_002");
1538 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
1539 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
1540 EXPECT_NE(hdiDevice, nullptr);
1541
1542 std::shared_ptr<mindspore::lite::LiteGraph> model = std::make_shared<mindspore::lite::LiteGraph>();
1543 mindspore::lite::LiteGraph::Node node;
1544 mindspore::lite::LiteGraph::Node* testNode = &node;
1545 model->all_nodes_.emplace_back(testNode);
1546 ModelConfig config;
1547 std::shared_ptr<PreparedModel> preparedModel;
1548 OH_NN_ReturnCode ret = hdiDevice->PrepareOfflineModel(model, config, preparedModel);
1549 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
1550 }
1551
1552 /* *
1553 * @tc.name: hdidevice_prepareofflinemodel_003
1554 * @tc.desc: Verify the ReleaseBuffer function validate moc object's ReleaseBuffer return failure.
1555 * @tc.type: FUNC
1556 */
1557 HWTEST_F(HDIDeviceTest, hdidevice_prepareofflinemodel_003, TestSize.Level0)
1558 {
1559 LOGE("PrepareOfflineModel hdidevice_prepareofflinemodel_003");
1560 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
1561 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
1562 EXPECT_NE(hdiDevice, nullptr);
1563
1564 std::shared_ptr<mindspore::lite::LiteGraph> model = std::make_shared<mindspore::lite::LiteGraph>();
1565 mindspore::lite::LiteGraph::Node node;
1566 uint32_t indice = 0;
1567 node.input_indices_.emplace_back(indice);
1568 node.input_indices_.emplace_back(indice);
1569 mindspore::lite::LiteGraph::Node* testNode = &node;
1570 model->all_nodes_.emplace_back(testNode);
1571 model->all_tensors_.emplace_back(mindspore::lite::MindIR_Tensor_Create());
1572 model->all_tensors_.emplace_back(mindspore::lite::MindIR_Tensor_Create());
1573 ModelConfig config;
1574 std::shared_ptr<PreparedModel> preparedModel;
1575 OH_NN_ReturnCode ret = hdiDevice->PrepareOfflineModel(model, config, preparedModel);
1576 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
1577 }
1578
1579 /* *
1580 * @tc.name: hdidevice_prepareofflinemodel_004
1581 * @tc.desc: Verify the ReleaseBuffer function validate moc object's ReleaseBuffer return failure.
1582 * @tc.type: FUNC
1583 */
1584 HWTEST_F(HDIDeviceTest, hdidevice_prepareofflinemodel_004, TestSize.Level0)
1585 {
1586 LOGE("PrepareOfflineModel hdidevice_prepareofflinemodel_004");
1587 OHOS::sptr<V2_0::INnrtDevice> device = V2_0::INnrtDevice::Get(false);
1588 std::unique_ptr<HDIDeviceV2_0> hdiDevice = std::make_unique<HDIDeviceV2_0>(device);
1589 EXPECT_NE(hdiDevice, nullptr);
1590
1591 std::shared_ptr<mindspore::lite::LiteGraph> model = std::make_shared<mindspore::lite::LiteGraph>();
1592 mindspore::lite::LiteGraph::Node node;
1593 uint32_t indice = 0;
1594 node.input_indices_.emplace_back(indice);
1595 node.input_indices_.emplace_back(indice);
1596 mindspore::lite::LiteGraph::Node* testNode = &node;
1597 model->all_nodes_.emplace_back(testNode);
1598
1599 char a = 'a';
1600 mindspore::lite::DataType data_type = mindspore::lite::DataType::DATA_TYPE_INT32;
1601 int dim = 1;
1602 int32_t *dims = &dim;
1603 uint32_t dims_size = 1;
1604 mindspore::lite::Format format = mindspore::lite::Format::FORMAT_HWCK;
1605 uint8_t datas = 0;
1606 uint8_t *data = &datas;
1607 uint32_t data_size = 2;
1608 mindspore::lite::QuantParam quant_params;
1609 uint32_t quant_params_size = 0;
1610 mindspore::lite::TensorPtr ptr2 = mindspore::lite::MindIR_Tensor_Create(&a, data_type, dims, dims_size,
1611 format, data, data_size,
1612 &quant_params, quant_params_size);
1613 std::vector<uint8_t> offlineModel2 = mindspore::lite::MindIR_Tensor_GetData(ptr2);
1614
1615 model->all_tensors_.emplace_back(ptr2);
1616 model->all_tensors_.emplace_back(ptr2);
1617 ModelConfig config;
1618 std::shared_ptr<PreparedModel> preparedModel;
1619 OH_NN_ReturnCode ret = hdiDevice->PrepareOfflineModel(model, config, preparedModel);
1620 EXPECT_EQ(OH_NN_MEMORY_ERROR, ret);
1621
1622 testing::Mock::AllowLeak(device.GetRefPtr());
1623 }
1624
1625 /* *
1626 * @tc.name: hdidevice_V2_1_constructor_001
1627 * @tc.desc: Verify the Constructor function return object success.
1628 * @tc.type: FUNC
1629 */
1630 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_constructor_001, TestSize.Level0)
1631 {
1632 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
1633 EXPECT_NE(device, nullptr);
1634 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
1635 EXPECT_NE(hdiDevice, nullptr);
1636 }
1637
1638 /* *
1639 * @tc.name: hdidevice_V2_1_getdevicename_001
1640 * @tc.desc: Verify the GetDeviceName function validate device name success.
1641 * @tc.type: FUNC
1642 */
1643 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_getdevicename_001, TestSize.Level0)
1644 {
1645 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
1646 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
1647 EXPECT_NE(hdiDevice, nullptr);
1648 std::string deviceName = "MockDevice";
1649 EXPECT_CALL(*((V2_1::MockIDevice *)device.GetRefPtr()), GetDeviceName(::testing::_))
1650 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(deviceName), ::testing::Return(HDF_SUCCESS)));
1651
1652 const std::string expectDeviceName = "MockDevice";
1653 std::string newDeviceName = "";
1654 OH_NN_ReturnCode result = hdiDevice->GetDeviceName(newDeviceName);
1655 EXPECT_EQ(OH_NN_SUCCESS, result);
1656 EXPECT_EQ(expectDeviceName, newDeviceName);
1657 }
1658
1659 /* *
1660 * @tc.name: hdidevice_V2_1_getdevicename_002
1661 * @tc.desc: Verify the GetDeviceName function return unavailable device.
1662 * @tc.type: FUNC
1663 */
1664 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_getdevicename_002, TestSize.Level0)
1665 {
1666 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
1667 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
1668 EXPECT_NE(hdiDevice, nullptr);
1669 std::string deviceName = "MockDevice";
1670 EXPECT_CALL(*((V2_1::MockIDevice *)device.GetRefPtr()), GetDeviceName(::testing::_))
1671 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(deviceName), ::testing::Return(HDF_FAILURE)));
1672 OH_NN_ReturnCode result = hdiDevice->GetDeviceName(deviceName);
1673 EXPECT_EQ(OH_NN_UNAVAILABLE_DEVICE, result);
1674 }
1675
1676 /* *
1677 * @tc.name: hdidevice_V2_1_getvendorname_001
1678 * @tc.desc: Verify the GetVendorName function validate vendor name success.
1679 * @tc.type: FUNC
1680 */
1681 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_getvendorname_001, TestSize.Level0)
1682 {
1683 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
1684 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
1685 EXPECT_NE(hdiDevice, nullptr);
1686 std::string vendorName = "MockVendor";
1687 EXPECT_CALL(*((V2_1::MockIDevice *)device.GetRefPtr()), GetVendorName(::testing::_))
1688 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(vendorName), ::testing::Return(HDF_SUCCESS)));
1689
1690 const std::string expectDeviceName = "MockVendor";
1691 std::string newVendorName = "";
1692 OH_NN_ReturnCode result = hdiDevice->GetVendorName(newVendorName);
1693 EXPECT_EQ(OH_NN_SUCCESS, result);
1694 EXPECT_EQ(expectDeviceName, newVendorName);
1695 }
1696
1697 /* *
1698 * @tc.name: hdidevice_V2_1_getvendorname_002
1699 * @tc.desc: Verify the GetVendorName function return unavailable device.
1700 * @tc.type: FUNC
1701 */
1702 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_getvendorname_002, TestSize.Level0)
1703 {
1704 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
1705 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
1706 EXPECT_NE(hdiDevice, nullptr);
1707 std::string vendorName = "MockVendor";
1708 EXPECT_CALL(*((V2_1::MockIDevice *)device.GetRefPtr()), GetVendorName(::testing::_))
1709 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(vendorName), ::testing::Return(HDF_FAILURE)));
1710 OH_NN_ReturnCode result = hdiDevice->GetVendorName(vendorName);
1711 EXPECT_EQ(OH_NN_UNAVAILABLE_DEVICE, result);
1712 }
1713
1714 /* *
1715 * @tc.name: hdidevice_V2_1_getversion_001
1716 * @tc.desc: Verify the GetVersion function validate vendor name success.
1717 * @tc.type: FUNC
1718 */
1719 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_getversion_001, TestSize.Level0)
1720 {
1721 LOGE("GetVersion hdidevice_V2_1_getversion_001");
1722 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
1723 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
1724 EXPECT_NE(hdiDevice, nullptr);
1725 std::string vendorName = "MockVendor";
1726 EXPECT_CALL(*((V2_1::MockIDevice *)device.GetRefPtr()), GetVersion(::testing::_, ::testing::_))
1727 .WillRepeatedly(::testing::Return(HDF_SUCCESS));
1728
1729 const std::string expectDeviceName = "MockVendor";
1730 std::string newVendorName = "";
1731 OH_NN_ReturnCode result = hdiDevice->GetVersion(newVendorName);
1732 EXPECT_EQ(OH_NN_SUCCESS, result);
1733 }
1734
1735 /* *
1736 * @tc.name: hdidevice_V2_1_getversion_002
1737 * @tc.desc: Verify the GetVersion function return unavailable device.
1738 * @tc.type: FUNC
1739 */
1740 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_getversion_002, TestSize.Level0)
1741 {
1742 LOGE("GetVersion hdidevice_V2_1_getversion_002");
1743 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
1744 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
1745 EXPECT_NE(hdiDevice, nullptr);
1746 std::string vendorName = "MockVendor";
1747 EXPECT_CALL(*((V2_1::MockIDevice *)device.GetRefPtr()), GetVersion(::testing::_, ::testing::_))
1748 .WillRepeatedly(::testing::Return(HDF_FAILURE));
1749 OH_NN_ReturnCode result = hdiDevice->GetVersion(vendorName);
1750 EXPECT_EQ(OH_NN_UNAVAILABLE_DEVICE, result);
1751 }
1752
1753 /* *
1754 * @tc.name: hdidevice_V2_1_getdevicetype_001
1755 * @tc.desc: Verify the GetDeviceType function validate device type success.
1756 * @tc.type: FUNC
1757 */
1758 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_getdevicetype_001, TestSize.Level0)
1759 {
1760 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
1761 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
1762 EXPECT_NE(hdiDevice, nullptr);
1763 V2_1::DeviceType iDeviceType = V2_1::DeviceType::CPU;
1764 EXPECT_CALL(*((V2_1::MockIDevice *)device.GetRefPtr()), GetDeviceType(::testing::_))
1765 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(iDeviceType), ::testing::Return(HDF_SUCCESS)));
1766
1767 OH_NN_DeviceType expectDeviceType = OH_NN_CPU;
1768 OH_NN_DeviceType newDeviceType = OH_NN_CPU;
1769 OH_NN_ReturnCode result = hdiDevice->GetDeviceType(newDeviceType);
1770 EXPECT_EQ(OH_NN_SUCCESS, result);
1771 EXPECT_EQ(expectDeviceType, newDeviceType);
1772 }
1773
1774 /* *
1775 * @tc.name: hdidevice_V2_1_getdevicetype_002
1776 * @tc.desc: Verify the GetDeviceType function return unavailable device.
1777 * @tc.type: FUNC
1778 */
1779 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_getdevicetype_002, TestSize.Level0)
1780 {
1781 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
1782 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
1783 EXPECT_NE(hdiDevice, nullptr);
1784
1785 OH_NN_DeviceType deviceType = OH_NN_CPU;
1786 V2_1::DeviceType iDeviceType = V2_1::DeviceType::CPU;
1787 EXPECT_CALL(*((V2_1::MockIDevice *)device.GetRefPtr()), GetDeviceType(::testing::_))
1788 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(iDeviceType), ::testing::Return(HDF_FAILURE)));
1789 OH_NN_ReturnCode result = hdiDevice->GetDeviceType(deviceType);
1790 EXPECT_EQ(OH_NN_UNAVAILABLE_DEVICE, result);
1791 }
1792
1793 /* *
1794 * @tc.name: hdidevice_V2_1_getdevicetype_003
1795 * @tc.desc: Verify the GetDeviceType function return unavailable device.
1796 * @tc.type: FUNC
1797 */
1798 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_getdevicetype_003, TestSize.Level0)
1799 {
1800 LOGE("GetDeviceType hdidevice_V2_1_getdevicetype_003");
1801 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
1802 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
1803 EXPECT_NE(hdiDevice, nullptr);
1804
1805 OH_NN_DeviceType deviceType = OH_NN_CPU;
1806 EXPECT_CALL(*((V2_1::MockIDevice *)device.GetRefPtr()), GetDeviceType(::testing::_))
__anon339dd2a20702(V2_1::DeviceType& type) 1807 .WillRepeatedly(Invoke([](V2_1::DeviceType& type) {
1808 // 这里直接修改传入的引用参数
1809 type = V2_1::DeviceType::GPU;
1810 return OH_NN_SUCCESS; // 假设成功的状态码
1811 }));
1812 OH_NN_ReturnCode result = hdiDevice->GetDeviceType(deviceType);
1813 EXPECT_EQ(OH_NN_SUCCESS, result);
1814 }
1815
1816 /* *
1817 * @tc.name: hdidevice_V2_1_getdevicetype_004
1818 * @tc.desc: Verify the GetDeviceType function return unavailable device.
1819 * @tc.type: FUNC
1820 */
1821 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_getdevicetype_004, TestSize.Level0)
1822 {
1823 LOGE("GetDeviceType hdidevice_V2_1_getdevicetype_004");
1824 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
1825 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
1826 EXPECT_NE(hdiDevice, nullptr);
1827
1828 OH_NN_DeviceType deviceType = OH_NN_CPU;
1829 EXPECT_CALL(*((V2_1::MockIDevice *)device.GetRefPtr()), GetDeviceType(::testing::_))
__anon339dd2a20802(V2_1::DeviceType& type) 1830 .WillRepeatedly(Invoke([](V2_1::DeviceType& type) {
1831 // 这里直接修改传入的引用参数
1832 type = V2_1::DeviceType::ACCELERATOR;
1833 return OH_NN_SUCCESS; // 假设成功的状态码
1834 }));
1835 OH_NN_ReturnCode result = hdiDevice->GetDeviceType(deviceType);
1836 EXPECT_EQ(OH_NN_SUCCESS, result);
1837 }
1838
1839 /* *
1840 * @tc.name: hdidevice_V2_1_getdevicetype_005
1841 * @tc.desc: Verify the GetDeviceType function return unavailable device.
1842 * @tc.type: FUNC
1843 */
1844 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_getdevicetype_005, TestSize.Level0)
1845 {
1846 LOGE("GetDeviceType hdidevice_V2_1_getdevicetype_005");
1847 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
1848 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
1849 EXPECT_NE(hdiDevice, nullptr);
1850
1851 OH_NN_DeviceType deviceType = OH_NN_CPU;
1852 EXPECT_CALL(*((V2_1::MockIDevice *)device.GetRefPtr()), GetDeviceType(::testing::_))
__anon339dd2a20902(V2_1::DeviceType& type) 1853 .WillRepeatedly(Invoke([](V2_1::DeviceType& type) {
1854 // 这里直接修改传入的引用参数
1855 type = V2_1::DeviceType::OTHER;
1856 return OH_NN_SUCCESS; // 假设成功的状态码
1857 }));
1858 OH_NN_ReturnCode result = hdiDevice->GetDeviceType(deviceType);
1859 EXPECT_EQ(OH_NN_SUCCESS, result);
1860 }
1861
1862 /* *
1863 * @tc.name: hdidevice_V2_1_getdevicestatus_001
1864 * @tc.desc: Verify the GetDeviceStatus function validate device status success.
1865 * @tc.type: FUNC
1866 */
1867 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_getdevicestatus_001, TestSize.Level0)
1868 {
1869 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
1870 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
1871 EXPECT_NE(hdiDevice, nullptr);
1872
1873 V2_1::DeviceStatus iDeviceStatus = V2_1::DeviceStatus::AVAILABLE;
1874 EXPECT_CALL(*((V2_1::MockIDevice *)device.GetRefPtr()), GetDeviceStatus(::testing::_))
1875 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(iDeviceStatus), ::testing::Return(HDF_SUCCESS)));
1876
1877 const DeviceStatus expectDeviceStatus = AVAILABLE;
1878 DeviceStatus newDeviceStatus = AVAILABLE;
1879 OH_NN_ReturnCode result = hdiDevice->GetDeviceStatus(newDeviceStatus);
1880 EXPECT_EQ(OH_NN_SUCCESS, result);
1881 EXPECT_EQ(expectDeviceStatus, newDeviceStatus);
1882 }
1883
1884 /* *
1885 * @tc.name: hdidevice_V2_1_getdevicestatus_002
1886 * @tc.desc: Verify the GetDeviceStatus function return unavailable device.
1887 * @tc.type: FUNC
1888 */
1889 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_getdevicestatus_002, TestSize.Level0)
1890 {
1891 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
1892 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
1893 EXPECT_NE(hdiDevice, nullptr);
1894 DeviceStatus deviceStatus = AVAILABLE;
1895 V2_1::DeviceStatus iDeviceStatus = V2_1::DeviceStatus::AVAILABLE;
1896 EXPECT_CALL(*((V2_1::MockIDevice *)device.GetRefPtr()), GetDeviceStatus(::testing::_))
1897 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(iDeviceStatus), ::testing::Return(HDF_FAILURE)));
1898 OH_NN_ReturnCode result = hdiDevice->GetDeviceStatus(deviceStatus);
1899 EXPECT_EQ(OH_NN_UNAVAILABLE_DEVICE, result);
1900 }
1901
1902 /* *
1903 * @tc.name: hdidevice_V2_1_getdevicestatus_003
1904 * @tc.desc: Verify the GetDeviceStatus function validate device status success.
1905 * @tc.type: FUNC
1906 */
1907 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_getdevicestatus_003, TestSize.Level0)
1908 {
1909 LOGE("GetDeviceStatus hdidevice_V2_1_getdevicestatus_003");
1910 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
1911 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
1912 EXPECT_NE(hdiDevice, nullptr);
1913
1914 EXPECT_CALL(*((V2_1::MockIDevice *)device.GetRefPtr()), GetDeviceStatus(::testing::_))
__anon339dd2a20a02(V2_1::DeviceStatus& status) 1915 .WillRepeatedly(Invoke([](V2_1::DeviceStatus& status) {
1916 // 这里直接修改传入的引用参数
1917 status = V2_1::DeviceStatus::BUSY;
1918 return OH_NN_SUCCESS; // 假设成功的状态码
1919 }));
1920
1921 DeviceStatus newDeviceStatus = AVAILABLE;
1922 OH_NN_ReturnCode result = hdiDevice->GetDeviceStatus(newDeviceStatus);
1923 EXPECT_EQ(OH_NN_SUCCESS, result);
1924 }
1925
1926 /* *
1927 * @tc.name: hdidevice_V2_1_getdevicestatus_004
1928 * @tc.desc: Verify the GetDeviceStatus function validate device status success.
1929 * @tc.type: FUNC
1930 */
1931 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_getdevicestatus_004, TestSize.Level0)
1932 {
1933 LOGE("GetDeviceStatus hdidevice_V2_1_getdevicestatus_004");
1934 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
1935 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
1936 EXPECT_NE(hdiDevice, nullptr);
1937
1938 EXPECT_CALL(*((V2_1::MockIDevice *)device.GetRefPtr()), GetDeviceStatus(::testing::_))
__anon339dd2a20b02(V2_1::DeviceStatus& status) 1939 .WillRepeatedly(Invoke([](V2_1::DeviceStatus& status) {
1940 // 这里直接修改传入的引用参数
1941 status = V2_1::DeviceStatus::OFFLINE;
1942 return OH_NN_SUCCESS; // 假设成功的状态码
1943 }));
1944
1945 DeviceStatus newDeviceStatus = AVAILABLE;
1946 OH_NN_ReturnCode result = hdiDevice->GetDeviceStatus(newDeviceStatus);
1947 EXPECT_EQ(OH_NN_SUCCESS, result);
1948 }
1949
1950 /* *
1951 * @tc.name: hdidevice_V2_1_getdevicestatus_005
1952 * @tc.desc: Verify the GetDeviceStatus function validate device status success.
1953 * @tc.type: FUNC
1954 */
1955 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_getdevicestatus_005, TestSize.Level0)
1956 {
1957 LOGE("GetDeviceStatus hdidevice_V2_1_getdevicestatus_005");
1958 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
1959 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
1960 EXPECT_NE(hdiDevice, nullptr);
1961
1962 EXPECT_CALL(*((V2_1::MockIDevice *)device.GetRefPtr()), GetDeviceStatus(::testing::_))
__anon339dd2a20c02(V2_1::DeviceStatus& status) 1963 .WillRepeatedly(Invoke([](V2_1::DeviceStatus& status) {
1964 // 这里直接修改传入的引用参数
1965 status = V2_1::DeviceStatus::UNKNOWN;
1966 return OH_NN_SUCCESS; // 假设成功的状态码
1967 }));
1968
1969 DeviceStatus newDeviceStatus = AVAILABLE;
1970 OH_NN_ReturnCode result = hdiDevice->GetDeviceStatus(newDeviceStatus);
1971 EXPECT_EQ(OH_NN_SUCCESS, result);
1972 }
1973
1974 /* *
1975 * @tc.name: hdidevice_V2_1_getsupportedoperation_001
1976 * @tc.desc: Verify the GetSupportedOperation function return success.
1977 * @tc.type: FUNC
1978 */
1979 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_getsupportedoperation_001, TestSize.Level0)
1980 {
1981 std::vector<bool> ops {true};
1982 std::shared_ptr<mindspore::lite::LiteGraph> model = std::make_shared<mindspore::lite::LiteGraph>();
1983 EXPECT_NE(nullptr, model);
1984 BuildLiteGraph(model);
1985
1986 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
1987 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
1988 EXPECT_NE(hdiDevice, nullptr);
1989
1990 V2_1::SharedBuffer buffer {1, 1, 0, 1};
1991 EXPECT_CALL(*((V2_1::MockIDevice *)device.GetRefPtr()), AllocateBuffer(::testing::_, ::testing::_))
1992 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<1>(buffer), ::testing::Return(HDF_SUCCESS)));
1993
1994 EXPECT_CALL(*((V2_1::MockIDevice *)device.GetRefPtr()), GetSupportedOperation(::testing::_, ::testing::_))
1995 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<1>(ops), ::testing::Return(HDF_SUCCESS)));
1996
1997 std::vector<bool> newOps {true};
1998 const std::vector<bool> expectOps {true};
1999 OH_NN_ReturnCode result = hdiDevice->GetSupportedOperation(model, newOps);
2000 EXPECT_EQ(OH_NN_FAILED, result);
2001 auto expectOpsSize = expectOps.size();
2002 for (size_t i = 0; i < expectOpsSize; ++i) {
2003 EXPECT_EQ(expectOps[i], newOps[i]);
2004 }
2005 }
2006
2007 /* *
2008 * @tc.name: hdidevice_V2_1_getsupportedoperation_002
2009 * @tc.desc: Verify the GetSupportedOperation function return failed in case of allocate buffer failure.
2010 * @tc.type: FUNC
2011 */
2012 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_getsupportedoperation_002, TestSize.Level0)
2013 {
2014 std::vector<bool> ops;
2015 std::shared_ptr<mindspore::lite::LiteGraph> model = std::make_shared<mindspore::lite::LiteGraph>();
2016 EXPECT_NE(nullptr, model);
2017 BuildLiteGraph(model);
2018
2019 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
2020 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
2021 EXPECT_NE(hdiDevice, nullptr);
2022
2023 V2_1::SharedBuffer buffer {1, 1, 0, 1};
2024 EXPECT_CALL(*((V2_1::MockIDevice *)device.GetRefPtr()), AllocateBuffer(::testing::_, ::testing::_))
2025 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<1>(buffer), ::testing::Return(HDF_FAILURE)));
2026
2027 OH_NN_ReturnCode result = hdiDevice->GetSupportedOperation(model, ops);
2028 EXPECT_EQ(OH_NN_FAILED, result);
2029 }
2030
2031 /* *
2032 * @tc.name: hdidevice_V2_1_getsupportedoperation_003
2033 * @tc.desc: Verify the GetSupportedOperation function return nullptr.
2034 * @tc.type: FUNC
2035 */
2036 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_getsupportedoperation_003, TestSize.Level0)
2037 {
2038 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
2039 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
2040 EXPECT_NE(hdiDevice, nullptr);
2041
2042 std::shared_ptr<const mindspore::lite::LiteGraph> model = nullptr;
2043 std::vector<bool> ops;
2044 OH_NN_ReturnCode result = hdiDevice->GetSupportedOperation(model, ops);
2045 EXPECT_EQ(OH_NN_NULL_PTR, result);
2046 }
2047
2048 /* *
2049 * @tc.name: hdidevice_V2_1_getsupportedoperation_004
2050 * @tc.desc: Verify the GetSupportedOperation function return unavalidable device.
2051 * @tc.type: FUNC
2052 */
2053 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_getsupportedoperation_004, TestSize.Level0)
2054 {
2055 std::vector<bool> ops {true};
2056 std::shared_ptr<mindspore::lite::LiteGraph> model = std::make_shared<mindspore::lite::LiteGraph>();
2057 EXPECT_NE(nullptr, model);
2058 BuildLiteGraph(model);
2059
2060 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
2061 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
2062 EXPECT_NE(hdiDevice, nullptr);
2063
2064 V2_1::SharedBuffer buffer {2, 1, 0, 1};
2065 EXPECT_CALL(*((V2_1::MockIDevice *)device.GetRefPtr()), AllocateBuffer(::testing::_, ::testing::_))
2066 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<1>(buffer), ::testing::Return(HDF_SUCCESS)));
2067
2068 EXPECT_CALL(*((V2_1::MockIDevice *)device.GetRefPtr()), GetSupportedOperation(::testing::_, ::testing::_))
2069 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<1>(ops), ::testing::Return(HDF_FAILURE)));
2070
2071 std::vector<bool> newOps {true};
2072 OH_NN_ReturnCode result = hdiDevice->GetSupportedOperation(model, newOps);
2073 EXPECT_EQ(OH_NN_FAILED, result);
2074 }
2075
2076 /* *
2077 * @tc.name: hdidevice_V2_1_isfloat16precisionsupported_001
2078 * @tc.desc: Verify the IsFloat16PrecisionSupported function return success.
2079 * @tc.type: FUNC
2080 */
2081 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_isfloat16precisionsupported_001, TestSize.Level0)
2082 {
2083 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
2084 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
2085 EXPECT_NE(hdiDevice, nullptr);
2086
2087 bool isSupported = false;
2088 EXPECT_CALL(*((V2_1::MockIDevice *)device.GetRefPtr()), IsFloat16PrecisionSupported(::testing::_))
2089 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(isSupported), ::testing::Return(HDF_SUCCESS)));
2090 OH_NN_ReturnCode result = hdiDevice->IsFloat16PrecisionSupported(isSupported);
2091 EXPECT_EQ(OH_NN_SUCCESS, result);
2092 }
2093
2094 /* *
2095 * @tc.name: hdidevice_V2_1_isfloat16precisionsupported_002
2096 * @tc.desc: Verify the IsFloat16PrecisionSupported function return unavailable device.
2097 * @tc.type: FUNC
2098 */
2099 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_isfloat16precisionsupported_002, TestSize.Level0)
2100 {
2101 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
2102 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
2103 EXPECT_NE(hdiDevice, nullptr);
2104
2105 bool isSupported = false;
2106 EXPECT_CALL(*((V2_1::MockIDevice *)device.GetRefPtr()), IsFloat16PrecisionSupported(::testing::_))
2107 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(isSupported), ::testing::Return(HDF_FAILURE)));
2108 OH_NN_ReturnCode result = hdiDevice->IsFloat16PrecisionSupported(isSupported);
2109 EXPECT_EQ(OH_NN_UNAVAILABLE_DEVICE, result);
2110 }
2111
2112 /* *
2113 * @tc.name: hdidevice_V2_1_isperformancemodesupported_001
2114 * @tc.desc: Verify the IsPerformanceModeSupported function return success.
2115 * @tc.type: FUNC
2116 */
2117 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_isperformancemodesupported_001, TestSize.Level0)
2118 {
2119 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
2120 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
2121 EXPECT_NE(hdiDevice, nullptr);
2122
2123 bool isSupported = false;
2124 EXPECT_CALL(*((V2_1::MockIDevice *)device.GetRefPtr()), IsPerformanceModeSupported(::testing::_))
2125 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(isSupported), ::testing::Return(HDF_SUCCESS)));
2126
2127 bool newIsSupported = false;
2128 const bool expectIsSupported = false;
2129 OH_NN_ReturnCode result = hdiDevice->IsPerformanceModeSupported(newIsSupported);
2130 EXPECT_EQ(OH_NN_SUCCESS, result);
2131 EXPECT_EQ(expectIsSupported, newIsSupported);
2132 }
2133
2134 /* *
2135 * @tc.name: hdidevice_V2_1_isperformancemodesupported_002
2136 * @tc.desc: Verify the IsPerformanceModeSupported function return unavailable device.
2137 * @tc.type: FUNC
2138 */
2139 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_isperformancemodesupported_002, TestSize.Level0)
2140 {
2141 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
2142 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
2143 EXPECT_NE(hdiDevice, nullptr);
2144
2145 bool isSupported = false;
2146 EXPECT_CALL(*((V2_1::MockIDevice *)device.GetRefPtr()), IsPerformanceModeSupported(::testing::_))
2147 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(isSupported), ::testing::Return(HDF_FAILURE)));
2148 OH_NN_ReturnCode result = hdiDevice->IsPerformanceModeSupported(isSupported);
2149 EXPECT_EQ(OH_NN_UNAVAILABLE_DEVICE, result);
2150 }
2151
2152 /* *
2153 * @tc.name: hdidevice_V2_1_isprioritysupported_001
2154 * @tc.desc: Verify the IsPrioritySupported function return success.
2155 * @tc.type: FUNC
2156 */
2157 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_isprioritysupported_001, TestSize.Level0)
2158 {
2159 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
2160 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
2161 EXPECT_NE(hdiDevice, nullptr);
2162
2163 bool isSupported = false;
2164 EXPECT_CALL(*((V2_1::MockIDevice *)device.GetRefPtr()), IsPrioritySupported(::testing::_))
2165 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(isSupported), ::testing::Return(HDF_SUCCESS)));
2166
2167 bool newIsSupported = false;
2168 bool expectIsSupported = false;
2169 OH_NN_ReturnCode result = hdiDevice->IsPrioritySupported(newIsSupported);
2170 EXPECT_EQ(OH_NN_SUCCESS, result);
2171 EXPECT_EQ(newIsSupported, expectIsSupported);
2172 }
2173
2174 /* *
2175 * @tc.name: hdidevice_V2_1_isprioritysupported_002
2176 * @tc.desc: Verify the IsPrioritySupported function return unavailable device.
2177 * @tc.type: FUNC
2178 */
2179 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_isprioritysupported_002, TestSize.Level0)
2180 {
2181 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
2182 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
2183 EXPECT_NE(hdiDevice, nullptr);
2184
2185 bool isSupported = false;
2186 EXPECT_CALL(*((V2_1::MockIDevice *)device.GetRefPtr()), IsPrioritySupported(::testing::_))
2187 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(isSupported), ::testing::Return(HDF_FAILURE)));
2188 OH_NN_ReturnCode result = hdiDevice->IsPrioritySupported(isSupported);
2189 EXPECT_EQ(OH_NN_UNAVAILABLE_DEVICE, result);
2190 }
2191
2192 /* *
2193 * @tc.name: hdidevice_V2_1_isdynamicinputsupported_001
2194 * @tc.desc: Verify the IsDynamicInputSupported function return success.
2195 * @tc.type: FUNC
2196 */
2197 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_isdynamicinputsupported_001, TestSize.Level0)
2198 {
2199 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
2200 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
2201 EXPECT_NE(hdiDevice, nullptr);
2202
2203 bool isSupported = false;
2204 EXPECT_CALL(*((V2_1::MockIDevice *)device.GetRefPtr()), IsDynamicInputSupported(::testing::_))
2205 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(isSupported), ::testing::Return(HDF_SUCCESS)));
2206
2207 bool newIsSupported = false;
2208 bool expectIsSupported = false;
2209 OH_NN_ReturnCode result = hdiDevice->IsDynamicInputSupported(newIsSupported);
2210 EXPECT_EQ(OH_NN_SUCCESS, result);
2211 EXPECT_EQ(newIsSupported, expectIsSupported);
2212 }
2213
2214 /* *
2215 * @tc.name: hdidevice_V2_1_isdynamicinputsupported_002
2216 * @tc.desc: Verify the IsDynamicInputSupported function return unavailable device.
2217 * @tc.type: FUNC
2218 */
2219 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_isdynamicinputsupported_002, TestSize.Level0)
2220 {
2221 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
2222 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
2223 EXPECT_NE(hdiDevice, nullptr);
2224
2225 bool isSupported = false;
2226 EXPECT_CALL(*((V2_1::MockIDevice *)device.GetRefPtr()), IsDynamicInputSupported(::testing::_))
2227 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(isSupported), ::testing::Return(HDF_FAILURE)));
2228 OH_NN_ReturnCode result = hdiDevice->IsDynamicInputSupported(isSupported);
2229 EXPECT_EQ(OH_NN_UNAVAILABLE_DEVICE, result);
2230 }
2231
2232 /* *
2233 * @tc.name: hdidevice_V2_1_isdynamicinputsupported_001
2234 * @tc.desc: Verify the IsModelCacheSupported function return success.
2235 * @tc.type: FUNC
2236 */
2237 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_ismodelcachesupported_001, TestSize.Level0)
2238 {
2239 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
2240 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
2241 EXPECT_NE(hdiDevice, nullptr);
2242
2243 bool isSupported = false;
2244 EXPECT_CALL(*((V2_1::MockIDevice *)device.GetRefPtr()), IsModelCacheSupported(::testing::_))
2245 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(isSupported), ::testing::Return(HDF_SUCCESS)));
2246
2247 bool newIsSupported = false;
2248 bool expectIsSupported = false;
2249 OH_NN_ReturnCode result = hdiDevice->IsModelCacheSupported(newIsSupported);
2250 EXPECT_EQ(OH_NN_SUCCESS, result);
2251 EXPECT_EQ(expectIsSupported, newIsSupported);
2252 }
2253
2254 /* *
2255 * @tc.name: hdidevice_V2_1_isdynamicinputsupported_002
2256 * @tc.desc: Verify the IsModelCacheSupported function return unavailable device.
2257 * @tc.type: FUNC
2258 */
2259 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_ismodelcachesupported_002, TestSize.Level0)
2260 {
2261 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
2262 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
2263 EXPECT_NE(hdiDevice, nullptr);
2264
2265 bool isSupported = false;
2266 EXPECT_CALL(*((V2_1::MockIDevice *)device.GetRefPtr()), IsModelCacheSupported(::testing::_))
2267 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(isSupported), ::testing::Return(HDF_FAILURE)));
2268 OH_NN_ReturnCode result = hdiDevice->IsModelCacheSupported(isSupported);
2269 EXPECT_EQ(OH_NN_UNAVAILABLE_DEVICE, result);
2270 }
2271
2272 /* *
2273 * @tc.name: hdidevice_V2_1_preparemodel_001
2274 * @tc.desc: Verify the PrepareModel function return success.
2275 * @tc.type: FUNC
2276 */
2277 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_preparemodel_001, TestSize.Level0)
2278 {
2279 int32_t allocBufferType = HDF_SUCCESS;
2280 int32_t prepareType = HDF_SUCCESS;
2281 OH_NN_ReturnCode result = PrepareModelv2(allocBufferType, prepareType);
2282 EXPECT_EQ(OH_NN_FAILED, result);
2283 }
2284
2285 /* *
2286 * @tc.name: hdidevice_V2_1_preparemodel_002
2287 * @tc.desc: Verify the PrepareModel function return invalid parameter.
2288 * @tc.type: FUNC
2289 */
2290 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_preparemodel_002, TestSize.Level0)
2291 {
2292 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
2293 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
2294 EXPECT_NE(hdiDevice, nullptr);
2295
2296 std::shared_ptr<const mindspore::lite::LiteGraph> model = nullptr;
2297 ModelConfig config;
2298 std::shared_ptr<PreparedModel> preparedModel;
2299 OH_NN_ReturnCode result = hdiDevice->PrepareModel(model, config, preparedModel);
2300 EXPECT_EQ(OH_NN_INVALID_PARAMETER, result);
2301 }
2302
2303 /* *
2304 * @tc.name: hdidevice_V2_1_preparemodel_003
2305 * @tc.desc: Verify the PrepareModel function return failed.
2306 * @tc.type: FUNC
2307 */
2308 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_preparemodel_003, TestSize.Level0)
2309 {
2310 int32_t allocBufferType = HDF_SUCCESS;
2311 int32_t prepareType = HDF_FAILURE;
2312 OH_NN_ReturnCode result = PrepareModelv2(allocBufferType, prepareType);
2313 EXPECT_EQ(OH_NN_FAILED, result);
2314 }
2315
2316 /* *
2317 * @tc.name: hdidevice_V2_1_preparemodel_004
2318 * @tc.desc: Verify the PrepareModel function return failed.
2319 * @tc.type: FUNC
2320 */
2321 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_preparemodel_004, TestSize.Level0)
2322 {
2323 int32_t allocBufferType = HDF_FAILURE;
2324 int32_t prepareType = HDF_FAILURE;
2325 OH_NN_ReturnCode result = PrepareModelv2(allocBufferType, prepareType);
2326 EXPECT_EQ(OH_NN_FAILED, result);
2327 }
2328
2329 /* *
2330 * @tc.name: hdidevice_V2_1_preparemodel_005
2331 * @tc.desc: Verify the PrepareModel function return failed.
2332 * @tc.type: FUNC
2333 */
2334 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_preparemodel_005, TestSize.Level0)
2335 {
2336 LOGE("PrepareModel hdidevice_V2_1_preparemodel_005");
2337 OHOS::sptr<V2_1::MockIDevice> sp = OHOS::sptr<V2_1::MockIDevice>(new (std::nothrow) V2_1::MockIDevice());
2338 EXPECT_NE(sp, nullptr);
2339
2340 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(sp);
2341 EXPECT_NE(hdiDevice, nullptr);
2342
2343 float dataArry[9] {0, 1, 2, 3, 4, 5, 6, 7, 8};
2344 void* data = dataArry;
2345 ModelConfig config;
2346 std::shared_ptr<PreparedModel> preparedModel;
2347 OH_NN_ReturnCode result = hdiDevice->PrepareModel(data, config, preparedModel);
2348 EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, result);
2349 }
2350
2351 /* *
2352 * @tc.name: hdidevice_V2_1_preparemodelfrommodelcache_001
2353 * @tc.desc: Verify the PrepareModelFromModelCache function return success.
2354 * @tc.type: FUNC
2355 */
2356 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_preparemodelfrommodelcache_001, TestSize.Level0)
2357 {
2358 size_t length = 100;
2359 void *buffer = nullptr;
2360 GetBuffer(buffer, length);
2361
2362 std::vector<Buffer> modelCache = { { buffer, 100 } };
2363 ModelConfig config;
2364
2365 OHOS::sptr<V2_1::MockIDevice> sp = OHOS::sptr<V2_1::MockIDevice>(new (std::nothrow) V2_1::MockIDevice());
2366 EXPECT_NE(sp, nullptr);
2367
2368 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(sp);
2369 EXPECT_NE(hdiDevice, nullptr);
2370
2371 std::shared_ptr<PreparedModel> preparedModel;
2372
2373 OHOS::sptr<V2_1::IPreparedModel> iPreparedModel =
2374 OHOS::sptr<V2_1::MockIPreparedModel>(new (std::nothrow) V2_1::MockIPreparedModel());
2375 EXPECT_CALL(*sp, PrepareModelFromModelCache(::testing::_, ::testing::_, ::testing::_))
2376 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<2>(iPreparedModel), ::testing::Return(HDF_SUCCESS)));
2377
2378 bool isUpdatable = false;
2379 OH_NN_ReturnCode result = hdiDevice->PrepareModelFromModelCache(modelCache, config, preparedModel, isUpdatable);
2380 const auto &memoryManager = MemoryManager::GetInstance();
2381 memoryManager->UnMapMemory(buffer);
2382 EXPECT_EQ(OH_NN_SUCCESS, result);
2383 }
2384
2385 /* *
2386 * @tc.name: hdidevice_V2_1_preparemodelfrommodelcache_002
2387 * @tc.desc: Verify the PrepareModelFromModelCache function return unavailable device.
2388 * @tc.type: FUNC
2389 */
2390 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_preparemodelfrommodelcache_002, TestSize.Level0)
2391 {
2392 size_t length = 100;
2393 void *buffer = nullptr;
2394 GetBuffer(buffer, length);
2395
2396 OHOS::sptr<V2_1::MockIDevice> sp = OHOS::sptr<V2_1::MockIDevice>(new (std::nothrow) V2_1::MockIDevice());
2397 EXPECT_NE(sp, nullptr);
2398
2399 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(sp);
2400 EXPECT_NE(hdiDevice, nullptr);
2401
2402 std::vector<Buffer> modelCache = { { buffer, 100 } };
2403 ModelConfig config;
2404 OHOS::sptr<V2_1::IPreparedModel> preModel =
2405 OHOS::sptr<V2_1::MockIPreparedModel>(new (std::nothrow) V2_1::MockIPreparedModel());
2406 EXPECT_NE(preModel, nullptr);
2407
2408 std::shared_ptr<PreparedModel> preparedModel = std::make_shared<HDIPreparedModelV2_1>(preModel);
2409
2410 OHOS::sptr<V2_1::IPreparedModel> iPreparedModel =
2411 OHOS::sptr<V2_1::MockIPreparedModel>(new (std::nothrow) V2_1::MockIPreparedModel);
2412 EXPECT_CALL(*sp, PrepareModelFromModelCache(::testing::_, ::testing::_, ::testing::_))
2413 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<2>(iPreparedModel), ::testing::Return(HDF_FAILURE)));
2414
2415 bool isUpdatable = false;
2416 OH_NN_ReturnCode result = hdiDevice->PrepareModelFromModelCache(modelCache, config, preparedModel, isUpdatable);
2417 EXPECT_EQ(OH_NN_FAILED, result);
2418 }
2419
2420 /* *
2421 * @tc.name: hdidevice_V2_1_preparemodelfrommodelcache_003
2422 * @tc.desc: Verify the PrepareModelFromModelCache function return nullptr.
2423 * @tc.type: FUNC
2424 */
2425 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_preparemodelfrommodelcache_003, TestSize.Level0)
2426 {
2427 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
2428 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
2429 EXPECT_NE(hdiDevice, nullptr);
2430
2431 std::vector<Buffer> modelCache = { { nullptr, 0 } };
2432 ModelConfig config;
2433 std::shared_ptr<PreparedModel> preparedModel;
2434 bool isUpdatable = false;
2435 OH_NN_ReturnCode result = hdiDevice->PrepareModelFromModelCache(modelCache, config, preparedModel, isUpdatable);
2436 EXPECT_EQ(OH_NN_NULL_PTR, result);
2437 }
2438
2439 /* *
2440 * @tc.name: hdidevice_V2_1_preparemodelfrommodelcache_004
2441 * @tc.desc: Verify the PrepareModelFromModelCache function return success.
2442 * @tc.type: FUNC
2443 */
2444 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_preparemodelfrommodelcache_004, TestSize.Level0)
2445 {
2446 LOGE("GetDeviceStatus hdidevice_V2_1_getdevicestatus_005");
2447 size_t length = 100;
2448 void *buffer = nullptr;
2449 GetBuffer(buffer, length);
2450
2451 OHOS::sptr<V2_1::MockIDevice> sp = OHOS::sptr<V2_1::MockIDevice>(new (std::nothrow) V2_1::MockIDevice());
2452 EXPECT_NE(sp, nullptr);
2453
2454 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(sp);
2455 EXPECT_NE(hdiDevice, nullptr);
2456
2457 std::vector<Buffer> modelCache = { { buffer, 100 } };
2458 ModelConfig config;
2459 config.mode = OH_NN_PERFORMANCE_LOW;
2460 config.priority = OH_NN_PRIORITY_LOW;
2461 OHOS::sptr<V2_1::IPreparedModel> preModel =
2462 OHOS::sptr<V2_1::MockIPreparedModel>(new (std::nothrow) V2_1::MockIPreparedModel());
2463 EXPECT_NE(preModel, nullptr);
2464
2465 std::shared_ptr<PreparedModel> preparedModel = std::make_shared<HDIPreparedModelV2_1>(preModel);
2466
2467 OHOS::sptr<V2_1::IPreparedModel> iPreparedModel =
2468 OHOS::sptr<V2_1::MockIPreparedModel>(new (std::nothrow) V2_1::MockIPreparedModel);
2469 EXPECT_CALL(*sp, PrepareModelFromModelCache(::testing::_, ::testing::_, ::testing::_))
2470 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<2>(iPreparedModel), ::testing::Return(HDF_FAILURE)));
2471
2472 bool isUpdatable = false;
2473 OH_NN_ReturnCode result = hdiDevice->PrepareModelFromModelCache(modelCache, config, preparedModel, isUpdatable);
2474 EXPECT_EQ(OH_NN_FAILED, result);
2475 }
2476
2477 /* *
2478 * @tc.name: hdidevice_V2_1_preparemodelfrommodelcache_005
2479 * @tc.desc: Verify the PrepareModelFromModelCache function return success.
2480 * @tc.type: FUNC
2481 */
2482 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_preparemodelfrommodelcache_005, TestSize.Level0)
2483 {
2484 LOGE("GetDeviceStatus hdidevice_V2_1_preparemodelfrommodelcache_005");
2485 size_t length = 100;
2486 void *buffer = nullptr;
2487 GetBuffer(buffer, length);
2488
2489 OHOS::sptr<V2_1::MockIDevice> sp = OHOS::sptr<V2_1::MockIDevice>(new (std::nothrow) V2_1::MockIDevice());
2490 EXPECT_NE(sp, nullptr);
2491
2492 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(sp);
2493 EXPECT_NE(hdiDevice, nullptr);
2494
2495 std::vector<Buffer> modelCache = { { buffer, 100 } };
2496 ModelConfig config;
2497 config.mode = OH_NN_PERFORMANCE_MEDIUM;
2498 config.priority = OH_NN_PRIORITY_MEDIUM;
2499 OHOS::sptr<V2_1::IPreparedModel> preModel =
2500 OHOS::sptr<V2_1::MockIPreparedModel>(new (std::nothrow) V2_1::MockIPreparedModel());
2501 EXPECT_NE(preModel, nullptr);
2502
2503 std::shared_ptr<PreparedModel> preparedModel = std::make_shared<HDIPreparedModelV2_1>(preModel);
2504
2505 OHOS::sptr<V2_1::IPreparedModel> iPreparedModel =
2506 OHOS::sptr<V2_1::MockIPreparedModel>(new (std::nothrow) V2_1::MockIPreparedModel);
2507 EXPECT_CALL(*sp, PrepareModelFromModelCache(::testing::_, ::testing::_, ::testing::_))
2508 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<2>(iPreparedModel), ::testing::Return(HDF_FAILURE)));
2509
2510 bool isUpdatable = false;
2511 OH_NN_ReturnCode result = hdiDevice->PrepareModelFromModelCache(modelCache, config, preparedModel, isUpdatable);
2512 EXPECT_EQ(OH_NN_FAILED, result);
2513 }
2514
2515 /* *
2516 * @tc.name: hdidevice_V2_1_preparemodelfrommodelcache_006
2517 * @tc.desc: Verify the PrepareModelFromModelCache function return success.
2518 * @tc.type: FUNC
2519 */
2520 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_preparemodelfrommodelcache_006, TestSize.Level0)
2521 {
2522 LOGE("GetDeviceStatus hdidevice_V2_1_preparemodelfrommodelcache_006");
2523 size_t length = 100;
2524 void *buffer = nullptr;
2525 GetBuffer(buffer, length);
2526
2527 OHOS::sptr<V2_1::MockIDevice> sp = OHOS::sptr<V2_1::MockIDevice>(new (std::nothrow) V2_1::MockIDevice());
2528 EXPECT_NE(sp, nullptr);
2529
2530 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(sp);
2531 EXPECT_NE(hdiDevice, nullptr);
2532
2533 std::vector<Buffer> modelCache = { { buffer, 100 } };
2534 ModelConfig config;
2535 config.mode = OH_NN_PERFORMANCE_HIGH;
2536 config.priority = OH_NN_PRIORITY_HIGH;
2537 OHOS::sptr<V2_1::IPreparedModel> preModel =
2538 OHOS::sptr<V2_1::MockIPreparedModel>(new (std::nothrow) V2_1::MockIPreparedModel());
2539 EXPECT_NE(preModel, nullptr);
2540
2541 std::shared_ptr<PreparedModel> preparedModel = std::make_shared<HDIPreparedModelV2_1>(preModel);
2542
2543 OHOS::sptr<V2_1::IPreparedModel> iPreparedModel =
2544 OHOS::sptr<V2_1::MockIPreparedModel>(new (std::nothrow) V2_1::MockIPreparedModel);
2545 EXPECT_CALL(*sp, PrepareModelFromModelCache(::testing::_, ::testing::_, ::testing::_))
2546 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<2>(iPreparedModel), ::testing::Return(HDF_FAILURE)));
2547
2548 bool isUpdatable = false;
2549 OH_NN_ReturnCode result = hdiDevice->PrepareModelFromModelCache(modelCache, config, preparedModel, isUpdatable);
2550 EXPECT_EQ(OH_NN_FAILED, result);
2551 }
2552
2553 /* *
2554 * @tc.name: hdidevice_V2_1_preparemodelfrommodelcache_007
2555 * @tc.desc: Verify the PrepareModelFromModelCache function return success.
2556 * @tc.type: FUNC
2557 */
2558 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_preparemodelfrommodelcache_007, TestSize.Level0)
2559 {
2560 LOGE("GetDeviceStatus hdidevice_V2_1_preparemodelfrommodelcache_007");
2561 size_t length = 100;
2562 void *buffer = nullptr;
2563 GetBuffer(buffer, length);
2564
2565 OHOS::sptr<V2_1::MockIDevice> sp = OHOS::sptr<V2_1::MockIDevice>(new (std::nothrow) V2_1::MockIDevice());
2566 EXPECT_NE(sp, nullptr);
2567
2568 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(sp);
2569 EXPECT_NE(hdiDevice, nullptr);
2570
2571 std::vector<Buffer> modelCache = { { buffer, 100 } };
2572 ModelConfig config;
2573 config.mode = OH_NN_PERFORMANCE_EXTREME;
2574 OHOS::sptr<V2_1::IPreparedModel> preModel =
2575 OHOS::sptr<V2_1::MockIPreparedModel>(new (std::nothrow) V2_1::MockIPreparedModel());
2576 EXPECT_NE(preModel, nullptr);
2577
2578 std::shared_ptr<PreparedModel> preparedModel = std::make_shared<HDIPreparedModelV2_1>(preModel);
2579
2580 OHOS::sptr<V2_1::IPreparedModel> iPreparedModel =
2581 OHOS::sptr<V2_1::MockIPreparedModel>(new (std::nothrow) V2_1::MockIPreparedModel);
2582 EXPECT_CALL(*sp, PrepareModelFromModelCache(::testing::_, ::testing::_, ::testing::_))
2583 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<2>(iPreparedModel), ::testing::Return(HDF_FAILURE)));
2584
2585 bool isUpdatable = false;
2586 OH_NN_ReturnCode result = hdiDevice->PrepareModelFromModelCache(modelCache, config, preparedModel, isUpdatable);
2587 EXPECT_EQ(OH_NN_FAILED, result);
2588 }
2589
2590 /* *
2591 * @tc.name: hdidevice_V2_1_allocatebuffer_001
2592 * @tc.desc: Verify the AllocateBuffer function return nullptr.
2593 * @tc.type: FUNC
2594 */
2595 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_allocatebuffer_001, TestSize.Level0)
2596 {
2597 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
2598 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
2599 EXPECT_NE(hdiDevice, nullptr);
2600
2601 V2_1::SharedBuffer buffer;
2602 EXPECT_CALL(*((V2_1::MockIDevice *)device.GetRefPtr()), AllocateBuffer(::testing::_, ::testing::_))
2603 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<1>(buffer), ::testing::Return(HDF_FAILURE)));
2604
2605 size_t length = 8;
2606 void *result = hdiDevice->AllocateBuffer(length);
2607 EXPECT_EQ(nullptr, result);
2608 hdiDevice->ReleaseBuffer(result);
2609 }
2610
2611 /* *
2612 * @tc.name: hdidevice_V2_1_allocatebuffer_002
2613 * @tc.desc: Verify the AllocateBuffer function return nullptr and HDF_FAILURE.
2614 * @tc.type: FUNC
2615 */
2616 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_allocatebuffer_002, TestSize.Level0)
2617 {
2618 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
2619 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
2620 EXPECT_NE(hdiDevice, nullptr);
2621
2622 size_t length = 8;
2623 void *result = hdiDevice->AllocateBuffer(length);
2624 EXPECT_EQ(nullptr, result);
2625 hdiDevice->ReleaseBuffer(result);
2626 }
2627
2628 /* *
2629 * @tc.name: hdidevice_V2_1_allocatebuffer_003
2630 * @tc.desc: Verify the AllocateBuffer function return nullptr in case of 0 size.
2631 * @tc.type: FUNC
2632 */
2633 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_allocatebuffer_003, TestSize.Level0)
2634 {
2635 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
2636 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
2637 EXPECT_NE(hdiDevice, nullptr);
2638
2639 size_t length = 0;
2640 void *result = hdiDevice->AllocateBuffer(length);
2641 EXPECT_EQ(nullptr, result);
2642 }
2643
2644 /* *
2645 * @tc.name: hdidevice_V2_1_allocatebuffer_004
2646 * @tc.desc: Verify the AllocateBuffer function return nullptr in case of 0 size.
2647 * @tc.type: FUNC
2648 */
2649 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_allocatebuffer_004, TestSize.Level0)
2650 {
2651 LOGE("AllocateBuffer hdidevice_V2_1_preparemodelfrommodelcache_007");
2652 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
2653 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
2654 EXPECT_NE(hdiDevice, nullptr);
2655
2656 size_t length = 0;
2657 int fd = 0;
2658 OH_NN_ReturnCode result = hdiDevice->AllocateBuffer(length, fd);
2659 EXPECT_EQ(OH_NN_INVALID_PARAMETER, result);
2660 }
2661
2662 /* *
2663 * @tc.name: hdidevice_V2_1_allocatebuffer_005
2664 * @tc.desc: Verify the AllocateBuffer function return nullptr in case of 0 size.
2665 * @tc.type: FUNC
2666 */
2667 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_allocatebuffer_005, TestSize.Level0)
2668 {
2669 LOGE("AllocateBuffer hdidevice_V2_1_allocatebuffer_005");
2670 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
2671 EXPECT_CALL(*((V2_1::MockIDevice *)device.GetRefPtr()), AllocateBuffer(::testing::_, ::testing::_))
2672 .WillRepeatedly(::testing::Return(HDF_FAILURE));
2673
2674 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
2675 EXPECT_NE(hdiDevice, nullptr);
2676
2677 size_t length = 1;
2678 int fd = 0;
2679 OH_NN_ReturnCode result = hdiDevice->AllocateBuffer(length, fd);
2680 EXPECT_EQ(OH_NN_MEMORY_ERROR, result);
2681 }
2682
2683 /* *
2684 * @tc.name: hdidevice_V2_1_allocatebuffer_006
2685 * @tc.desc: Verify the AllocateBuffer function return nullptr in case of 0 size.
2686 * @tc.type: FUNC
2687 */
2688 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_allocatebuffer_006, TestSize.Level0)
2689 {
2690 LOGE("AllocateBuffer hdidevice_V2_1_allocatebuffer_006");
2691 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
2692 EXPECT_CALL(*((V2_1::MockIDevice *)device.GetRefPtr()), AllocateBuffer(::testing::_, ::testing::_))
2693 .WillRepeatedly(::testing::Return(HDF_SUCCESS));
2694
2695 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
2696 EXPECT_NE(hdiDevice, nullptr);
2697
2698 size_t length = 1;
2699 int fd = 0;
2700 OH_NN_ReturnCode result = hdiDevice->AllocateBuffer(length, fd);
2701 EXPECT_EQ(OH_NN_SUCCESS, result);
2702 }
2703
2704 /* *
2705 * @tc.name: hdidevice_V2_1_releasebuffer_001
2706 * @tc.desc: Verify the ReleaseBuffer function validate buffer success.
2707 * @tc.type: FUNC
2708 */
2709 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_releasebuffer_001, TestSize.Level0)
2710 {
2711 size_t length = 100;
2712 void *buffer = nullptr;
2713 GetBuffer(buffer, length);
2714
2715 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
2716 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
2717
2718 EXPECT_CALL(*((V2_1::MockIDevice *)device.GetRefPtr()), ReleaseBuffer(::testing::_))
2719 .WillRepeatedly(::testing::Return(HDF_SUCCESS));
2720
2721 EXPECT_NE(hdiDevice, nullptr);
2722 hdiDevice->ReleaseBuffer(buffer);
2723 const auto &memoryManager = MemoryManager::GetInstance();
2724 memoryManager->UnMapMemory(buffer);
2725 }
2726
2727 /* *
2728 * @tc.name: hdidevice_V2_1_releasebuffer_002
2729 * @tc.desc: Verify the ReleaseBuffer function validate AllocateBuffer return nullptr.
2730 * @tc.type: FUNC
2731 */
2732 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_releasebuffer_002, TestSize.Level0)
2733 {
2734 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
2735 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
2736 EXPECT_NE(hdiDevice, nullptr);
2737
2738 V2_1::SharedBuffer sharedbuffer;
2739 EXPECT_CALL(*((V2_1::MockIDevice *)device.GetRefPtr()), AllocateBuffer(::testing::_, ::testing::_))
2740 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<1>(sharedbuffer), ::testing::Return(HDF_FAILURE)));
2741
2742 EXPECT_CALL(*((V2_1::MockIDevice *)device.GetRefPtr()), ReleaseBuffer(::testing::_))
2743 .WillRepeatedly(::testing::Return(HDF_FAILURE));
2744
2745 size_t length = 8;
2746 void *buffer = hdiDevice->AllocateBuffer(length);
2747 hdiDevice->ReleaseBuffer(buffer);
2748 }
2749
2750 /* *
2751 * @tc.name: hdidevice_V2_1_releasebuffer_003
2752 * @tc.desc: Verify the ReleaseBuffer function validate param buffer is nullptr.
2753 * @tc.type: FUNC
2754 */
2755 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_releasebuffer_003, TestSize.Level0)
2756 {
2757 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
2758 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
2759 EXPECT_NE(hdiDevice, nullptr);
2760
2761 void *buffer = nullptr;
2762 hdiDevice->ReleaseBuffer(buffer);
2763 }
2764
2765 /* *
2766 * @tc.name: hdidevice_V2_1_releasebuffer_004
2767 * @tc.desc: Verify the ReleaseBuffer function validate invalid buffer.
2768 * @tc.type: FUNC
2769 */
2770 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_releasebuffer_004, TestSize.Level0)
2771 {
2772 const size_t length = 100;
2773 auto* buffer = new(std::nothrow) char[length];
2774 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
2775 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
2776 EXPECT_NE(hdiDevice, nullptr);
2777
2778 hdiDevice->ReleaseBuffer(buffer);
2779 delete[] buffer;
2780 buffer = nullptr;
2781 }
2782
2783 /* *
2784 * @tc.name: hdidevice_V2_1_releasebuffer_005
2785 * @tc.desc: Verify the ReleaseBuffer function validate moc object's ReleaseBuffer return failure.
2786 * @tc.type: FUNC
2787 */
2788 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_releasebuffer_005, TestSize.Level0)
2789 {
2790 size_t length = 100;
2791 void *buffer = nullptr;
2792 GetBuffer(buffer, length);
2793
2794 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
2795 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
2796 EXPECT_NE(hdiDevice, nullptr);
2797
2798 EXPECT_CALL(*((V2_1::MockIDevice *)device.GetRefPtr()), ReleaseBuffer(::testing::_))
2799 .WillRepeatedly(::testing::Return(HDF_FAILURE));
2800
2801 hdiDevice->ReleaseBuffer(buffer);
2802 const auto &memoryManager = MemoryManager::GetInstance();
2803 memoryManager->UnMapMemory(buffer);
2804 }
2805
2806 /* *
2807 * @tc.name: hdidevice_V2_1_releasebuffer_007
2808 * @tc.desc: Verify the ReleaseBuffer function validate moc object's ReleaseBuffer return failure.
2809 * @tc.type: FUNC
2810 */
2811 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_releasebuffer_007, TestSize.Level0)
2812 {
2813 LOGE("ReleaseBuffer hdidevice_V2_1_releasebuffer_007");
2814 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
2815 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
2816 EXPECT_NE(hdiDevice, nullptr);
2817
2818 EXPECT_CALL(*((V2_1::MockIDevice *)device.GetRefPtr()), ReleaseBuffer(::testing::_))
2819 .WillRepeatedly(::testing::Return(HDF_FAILURE));
2820
2821 int fd = 0;
2822 size_t length = 1;
2823 OH_NN_ReturnCode ret = hdiDevice->ReleaseBuffer(fd, length);
2824 EXPECT_EQ(OH_NN_FAILED, ret);
2825 }
2826
2827 /* *
2828 * @tc.name: hdidevice_V2_1_releasebuffer_008
2829 * @tc.desc: Verify the ReleaseBuffer function validate moc object's ReleaseBuffer return failure.
2830 * @tc.type: FUNC
2831 */
2832 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_releasebuffer_008, TestSize.Level0)
2833 {
2834 LOGE("ReleaseBuffer hdidevice_V2_1_releasebuffer_008");
2835 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
2836 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
2837 EXPECT_NE(hdiDevice, nullptr);
2838
2839 EXPECT_CALL(*((V2_1::MockIDevice *)device.GetRefPtr()), ReleaseBuffer(::testing::_))
2840 .WillRepeatedly(::testing::Return(HDF_SUCCESS));
2841
2842 int fd = 0;
2843 size_t length = 1;
2844 OH_NN_ReturnCode ret = hdiDevice->ReleaseBuffer(fd, length);
2845 EXPECT_EQ(OH_NN_SUCCESS, ret);
2846 }
2847
2848 /* *
2849 * @tc.name: hdidevice_V2_1_allocatetensorbuffer_001
2850 * @tc.desc: Verify the ReleaseBuffer function validate moc object's ReleaseBuffer return failure.
2851 * @tc.type: FUNC
2852 */
2853 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_allocatetensorbuffer_001, TestSize.Level0)
2854 {
2855 LOGE("AllocateTensorBuffer hdidevice_V2_1_allocatetensorbuffer_001");
2856 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
2857 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
2858 EXPECT_NE(hdiDevice, nullptr);
2859
2860 size_t length = 0;
2861 std::shared_ptr<TensorDesc> tensor;
2862 void* ret = hdiDevice->AllocateTensorBuffer(length, tensor);
2863 EXPECT_EQ(nullptr, ret);
2864 }
2865
2866 /* *
2867 * @tc.name: hdidevice_V2_1_allocatetensorbuffer_002
2868 * @tc.desc: Verify the ReleaseBuffer function validate moc object's ReleaseBuffer return failure.
2869 * @tc.type: FUNC
2870 */
2871 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_allocatetensorbuffer_002, TestSize.Level0)
2872 {
2873 LOGE("AllocateTensorBuffer hdidevice_V2_1_allocatetensorbuffer_002");
2874 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
2875 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
2876 EXPECT_NE(hdiDevice, nullptr);
2877
2878 size_t length = 0;
2879 std::shared_ptr<NNTensor> tensor;
2880 void* ret = hdiDevice->AllocateTensorBuffer(length, tensor);
2881 EXPECT_EQ(nullptr, ret);
2882 }
2883
2884 /* *
2885 * @tc.name: hdidevice_V2_1_prepareofflinemodel_001
2886 * @tc.desc: Verify the ReleaseBuffer function validate moc object's ReleaseBuffer return failure.
2887 * @tc.type: FUNC
2888 */
2889 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_prepareofflinemodel_001, TestSize.Level0)
2890 {
2891 LOGE("PrepareOfflineModel hdidevice_V2_1_prepareofflinemodel_001");
2892 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
2893 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
2894 EXPECT_NE(hdiDevice, nullptr);
2895
2896 ModelConfig config;
2897 std::shared_ptr<PreparedModel> preparedModel;
2898 OH_NN_ReturnCode ret = hdiDevice->PrepareOfflineModel(nullptr, config, preparedModel);
2899 EXPECT_EQ(OH_NN_NULL_PTR, ret);
2900 }
2901
2902 /* *
2903 * @tc.name: hdidevice_V2_1_prepareofflinemodel_002
2904 * @tc.desc: Verify the ReleaseBuffer function validate moc object's ReleaseBuffer return failure.
2905 * @tc.type: FUNC
2906 */
2907 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_prepareofflinemodel_002, TestSize.Level0)
2908 {
2909 LOGE("PrepareOfflineModel hdidevice_V2_1_prepareofflinemodel_002");
2910 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
2911 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
2912 EXPECT_NE(hdiDevice, nullptr);
2913
2914 std::shared_ptr<mindspore::lite::LiteGraph> model = std::make_shared<mindspore::lite::LiteGraph>();
2915 mindspore::lite::LiteGraph::Node node;
2916 mindspore::lite::LiteGraph::Node* testNode = &node;
2917 model->all_nodes_.emplace_back(testNode);
2918 ModelConfig config;
2919 std::shared_ptr<PreparedModel> preparedModel;
2920 OH_NN_ReturnCode ret = hdiDevice->PrepareOfflineModel(model, config, preparedModel);
2921 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
2922 }
2923
2924 /* *
2925 * @tc.name: hdidevice_V2_1_prepareofflinemodel_003
2926 * @tc.desc: Verify the ReleaseBuffer function validate moc object's ReleaseBuffer return failure.
2927 * @tc.type: FUNC
2928 */
2929 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_prepareofflinemodel_003, TestSize.Level0)
2930 {
2931 LOGE("PrepareOfflineModel hdidevice_V2_1_prepareofflinemodel_003");
2932 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
2933 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
2934 EXPECT_NE(hdiDevice, nullptr);
2935
2936 std::shared_ptr<mindspore::lite::LiteGraph> model = std::make_shared<mindspore::lite::LiteGraph>();
2937 mindspore::lite::LiteGraph::Node node;
2938 uint32_t indice = 0;
2939 node.input_indices_.emplace_back(indice);
2940 node.input_indices_.emplace_back(indice);
2941 mindspore::lite::LiteGraph::Node* testNode = &node;
2942 model->all_nodes_.emplace_back(testNode);
2943 model->all_tensors_.emplace_back(mindspore::lite::MindIR_Tensor_Create());
2944 model->all_tensors_.emplace_back(mindspore::lite::MindIR_Tensor_Create());
2945 ModelConfig config;
2946 std::shared_ptr<PreparedModel> preparedModel;
2947 OH_NN_ReturnCode ret = hdiDevice->PrepareOfflineModel(model, config, preparedModel);
2948 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
2949 }
2950
2951 /* *
2952 * @tc.name: hdidevice_V2_1_prepareofflinemodel_004
2953 * @tc.desc: Verify the ReleaseBuffer function validate moc object's ReleaseBuffer return failure.
2954 * @tc.type: FUNC
2955 */
2956 HWTEST_F(HDIDeviceTest, hdidevice_V2_1_prepareofflinemodel_004, TestSize.Level0)
2957 {
2958 LOGE("PrepareOfflineModel hdidevice_V2_1_prepareofflinemodel_004");
2959 sptr<INnrtDevice> device = sptr<MockIDevice>(new (std::nothrow) MockIDevice());
2960 std::unique_ptr<HDIDeviceV2_1> hdiDevice = std::make_unique<HDIDeviceV2_1>(device);
2961 EXPECT_NE(hdiDevice, nullptr);
2962
2963 std::shared_ptr<mindspore::lite::LiteGraph> model = std::make_shared<mindspore::lite::LiteGraph>();
2964 mindspore::lite::LiteGraph::Node node;
2965 uint32_t indice = 0;
2966 node.input_indices_.emplace_back(indice);
2967 node.input_indices_.emplace_back(indice);
2968 mindspore::lite::LiteGraph::Node* testNode = &node;
2969 model->all_nodes_.emplace_back(testNode);
2970
2971 char a = 'a';
2972 mindspore::lite::DataType data_type = mindspore::lite::DataType::DATA_TYPE_INT32;
2973 int dim = 1;
2974 int32_t *dims = &dim;
2975 uint32_t dims_size = 1;
2976 mindspore::lite::Format format = mindspore::lite::Format::FORMAT_HWCK;
2977 uint8_t datas = 0;
2978 uint8_t *data = &datas;
2979 uint32_t data_size = 2;
2980 mindspore::lite::QuantParam quant_params;
2981 uint32_t quant_params_size = 0;
2982 mindspore::lite::TensorPtr ptr2 = mindspore::lite::MindIR_Tensor_Create(&a, data_type, dims, dims_size,
2983 format, data, data_size,
2984 &quant_params, quant_params_size);
2985 std::vector<uint8_t> offlineModel2 = mindspore::lite::MindIR_Tensor_GetData(ptr2);
2986
2987 model->all_tensors_.emplace_back(ptr2);
2988 model->all_tensors_.emplace_back(ptr2);
2989 ModelConfig config;
2990 std::shared_ptr<PreparedModel> preparedModel;
2991 OH_NN_ReturnCode ret = hdiDevice->PrepareOfflineModel(model, config, preparedModel);
2992 EXPECT_EQ(OH_NN_MEMORY_ERROR, ret);
2993
2994 testing::Mock::AllowLeak(device.GetRefPtr());
2995 }
2996 } // namespace UnitTest
2997 } // namespace NeuralNetworkRuntime
2998 } // namespace OHOS