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 <gtest/gtest.h> 17 #include <gmock/gmock.h> 18 19 #include "nnbackend.h" 20 #include "device.h" 21 #include "interfaces/kits/c/neural_network_runtime/neural_network_runtime_type.h" 22 #include "backend_manager.h" 23 24 using namespace testing; 25 using namespace testing::ext; 26 using namespace OHOS::NeuralNetworkRuntime; 27 28 namespace OHOS { 29 namespace NeuralNetworkRuntime { 30 namespace UnitTest { 31 class NNBackendTest : public testing::Test { 32 public: 33 NNBackendTest() = default; 34 ~NNBackendTest() = default; 35 }; 36 37 class MockIDevice : public Device { 38 public: 39 MOCK_METHOD1(GetDeviceName, OH_NN_ReturnCode(std::string&)); 40 MOCK_METHOD1(GetVendorName, OH_NN_ReturnCode(std::string&)); 41 MOCK_METHOD1(GetVersion, OH_NN_ReturnCode(std::string&)); 42 MOCK_METHOD1(GetDeviceType, OH_NN_ReturnCode(OH_NN_DeviceType&)); 43 MOCK_METHOD1(GetDeviceStatus, OH_NN_ReturnCode(DeviceStatus&)); 44 MOCK_METHOD2(GetSupportedOperation, OH_NN_ReturnCode(std::shared_ptr<const mindspore::lite::LiteGraph>, 45 std::vector<bool>&)); 46 MOCK_METHOD1(IsFloat16PrecisionSupported, OH_NN_ReturnCode(bool&)); 47 MOCK_METHOD1(IsPerformanceModeSupported, OH_NN_ReturnCode(bool&)); 48 MOCK_METHOD1(IsPrioritySupported, OH_NN_ReturnCode(bool&)); 49 MOCK_METHOD1(IsDynamicInputSupported, OH_NN_ReturnCode(bool&)); 50 MOCK_METHOD1(IsModelCacheSupported, OH_NN_ReturnCode(bool&)); 51 MOCK_METHOD3(PrepareModel, OH_NN_ReturnCode(std::shared_ptr<const mindspore::lite::LiteGraph>, 52 const ModelConfig&, 53 std::shared_ptr<PreparedModel>&)); 54 MOCK_METHOD3(PrepareModel, OH_NN_ReturnCode(const void*, 55 const ModelConfig&, 56 std::shared_ptr<PreparedModel>&)); 57 MOCK_METHOD4(PrepareModelFromModelCache, OH_NN_ReturnCode(const std::vector<Buffer>&, 58 const ModelConfig&, 59 std::shared_ptr<PreparedModel>&, 60 bool&)); 61 MOCK_METHOD3(PrepareOfflineModel, OH_NN_ReturnCode(std::shared_ptr<const mindspore::lite::LiteGraph>, 62 const ModelConfig&, 63 std::shared_ptr<PreparedModel>&)); 64 MOCK_METHOD1(AllocateBuffer, void*(size_t)); 65 MOCK_METHOD2(AllocateTensorBuffer, void*(size_t, std::shared_ptr<TensorDesc>)); 66 MOCK_METHOD2(AllocateTensorBuffer, void*(size_t, std::shared_ptr<NNTensor>)); 67 MOCK_METHOD1(ReleaseBuffer, OH_NN_ReturnCode(const void*)); 68 MOCK_METHOD2(AllocateBuffer, OH_NN_ReturnCode(size_t, int&)); 69 MOCK_METHOD2(ReleaseBuffer, OH_NN_ReturnCode(int, size_t)); 70 }; 71 72 /** 73 * @tc.name: nnbackendtest_construct_001 74 * @tc.desc: Verify the QuantParams function return nullptr in case of fd -1. 75 * @tc.type: FUNC 76 */ 77 HWTEST_F(NNBackendTest, nnbackendtest_construct_001, TestSize.Level0) 78 { 79 size_t backendID = 1; 80 std::shared_ptr<MockIDevice> device = std::make_shared<MockIDevice>(); 81 std::unique_ptr<NNBackend> hdiDevice = std::make_unique<NNBackend>(device, backendID); 82 EXPECT_NE(hdiDevice, nullptr); 83 84 testing::Mock::AllowLeak(device.get()); 85 } 86 87 /** 88 * @tc.name: nnbackendtest_getbackendname_001 89 * @tc.desc: Verify the QuantParams function return nullptr in case of fd -1. 90 * @tc.type: FUNC 91 */ 92 HWTEST_F(NNBackendTest, nnbackendtest_getbackendname_001, TestSize.Level0) 93 { 94 size_t backendID = 1; 95 std::unique_ptr<NNBackend> hdiDevice = std::make_unique<NNBackend>(nullptr, backendID); 96 std::string backendName = "mock"; 97 EXPECT_EQ(OH_NN_FAILED, hdiDevice->GetBackendName(backendName)); 98 } 99 100 /** 101 * @tc.name: nnbackendtest_getbackendname_002 102 * @tc.desc: Verify the QuantParams function return nullptr in case of fd -1. 103 * @tc.type: FUNC 104 */ 105 HWTEST_F(NNBackendTest, nnbackendtest_getbackendname_002, TestSize.Level0) 106 { 107 size_t backendID = 1; 108 std::shared_ptr<MockIDevice> device = std::make_shared<MockIDevice>(); 109 110 std::string backendName = "mock"; 111 112 EXPECT_CALL(*((MockIDevice *) device.get()), GetDeviceName(::testing::_)) 113 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(backendName), ::testing::Return(OH_NN_FAILED))); 114 115 std::unique_ptr<NNBackend> hdiDevice = std::make_unique<NNBackend>(device, backendID); 116 EXPECT_EQ(OH_NN_FAILED, hdiDevice->GetBackendName(backendName)); 117 118 testing::Mock::AllowLeak(device.get()); 119 } 120 121 /** 122 * @tc.name: nnbackendtest_getbackendname_005 123 * @tc.desc: Verify the QuantParams function return nullptr in case of fd -1. 124 * @tc.type: FUNC 125 */ 126 HWTEST_F(NNBackendTest, nnbackendtest_getbackendname_005, TestSize.Level0) 127 { 128 size_t backendID = 1; 129 std::shared_ptr<MockIDevice> device = std::make_shared<MockIDevice>(); 130 131 std::string backendName = "mock"; 132 EXPECT_CALL(*((MockIDevice *) device.get()), GetDeviceName(::testing::_)) 133 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(backendName), ::testing::Return(OH_NN_SUCCESS))); 134 135 EXPECT_CALL(*((MockIDevice *) device.get()), GetVendorName(::testing::_)) 136 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(backendName), ::testing::Return(OH_NN_FAILED))); 137 138 std::unique_ptr<NNBackend> hdiDevice = std::make_unique<NNBackend>(device, backendID); 139 EXPECT_EQ(OH_NN_FAILED, hdiDevice->GetBackendName(backendName)); 140 141 testing::Mock::AllowLeak(device.get()); 142 } 143 144 /** 145 * @tc.name: nnbackendtest_getbackendname_007 146 * @tc.desc: Verify the QuantParams function return nullptr in case of fd -1. 147 * @tc.type: FUNC 148 */ 149 HWTEST_F(NNBackendTest, nnbackendtest_getbackendname_007, TestSize.Level0) 150 { 151 size_t backendID = 1; 152 std::shared_ptr<MockIDevice> device = std::make_shared<MockIDevice>(); 153 154 std::string backendName = "mock"; 155 EXPECT_CALL(*((MockIDevice *) device.get()), GetDeviceName(::testing::_)) 156 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(backendName), ::testing::Return(OH_NN_SUCCESS))); 157 158 EXPECT_CALL(*((MockIDevice *) device.get()), GetVendorName(::testing::_)) 159 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(backendName), ::testing::Return(OH_NN_SUCCESS))); 160 161 EXPECT_CALL(*((MockIDevice *) device.get()), GetVersion(::testing::_)) 162 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(backendName), ::testing::Return(OH_NN_FAILED))); 163 164 std::unique_ptr<NNBackend> hdiDevice = std::make_unique<NNBackend>(device, backendID); 165 EXPECT_EQ(OH_NN_FAILED, hdiDevice->GetBackendName(backendName)); 166 167 testing::Mock::AllowLeak(device.get()); 168 } 169 170 /** 171 * @tc.name: nnbackendtest_getbackendname_008 172 * @tc.desc: Verify the QuantParams function return nullptr in case of fd -1. 173 * @tc.type: FUNC 174 */ 175 HWTEST_F(NNBackendTest, nnbackendtest_getbackendname_008, TestSize.Level0) 176 { 177 size_t backendID = 1; 178 std::shared_ptr<MockIDevice> device = std::make_shared<MockIDevice>(); 179 180 std::string backendName = "mock"; 181 EXPECT_CALL(*((MockIDevice *) device.get()), GetDeviceName(::testing::_)) 182 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(backendName), ::testing::Return(OH_NN_SUCCESS))); 183 184 EXPECT_CALL(*((MockIDevice *) device.get()), GetVendorName(::testing::_)) 185 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(backendName), ::testing::Return(OH_NN_SUCCESS))); 186 187 EXPECT_CALL(*((MockIDevice *) device.get()), GetVersion(::testing::_)) 188 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(backendName), ::testing::Return(OH_NN_SUCCESS))); 189 190 std::unique_ptr<NNBackend> hdiDevice = std::make_unique<NNBackend>(device, backendID); 191 EXPECT_EQ(OH_NN_SUCCESS, hdiDevice->GetBackendName(backendName)); 192 193 testing::Mock::AllowLeak(device.get()); 194 } 195 196 /** 197 * @tc.name: nnbackendtest_getgackendtype_001 198 * @tc.desc: Verify the QuantParams function return nullptr in case of fd -1. 199 * @tc.type: FUNC 200 */ 201 HWTEST_F(NNBackendTest, nnbackendtest_getgackendtype_001, TestSize.Level0) 202 { 203 size_t backendID = 1; 204 205 OH_NN_DeviceType backendName = OH_NN_OTHERS; 206 207 std::unique_ptr<NNBackend> hdiDevice = std::make_unique<NNBackend>(nullptr, backendID); 208 EXPECT_EQ(OH_NN_FAILED, hdiDevice->GetBackendType(backendName)); 209 } 210 211 /** 212 * @tc.name: nnbackendtest_getgackendtype_002 213 * @tc.desc: Verify the QuantParams function return nullptr in case of fd -1. 214 * @tc.type: FUNC 215 */ 216 HWTEST_F(NNBackendTest, nnbackendtest_getgackendtype_002, TestSize.Level0) 217 { 218 size_t backendID = 1; 219 std::shared_ptr<MockIDevice> device = std::make_shared<MockIDevice>(); 220 221 OH_NN_DeviceType backendName = OH_NN_OTHERS; 222 EXPECT_CALL(*((MockIDevice *) device.get()), GetDeviceType(::testing::_)) 223 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(backendName), ::testing::Return(OH_NN_FAILED))); 224 225 std::unique_ptr<NNBackend> hdiDevice = std::make_unique<NNBackend>(device, backendID); 226 EXPECT_EQ(OH_NN_FAILED, hdiDevice->GetBackendType(backendName)); 227 228 testing::Mock::AllowLeak(device.get()); 229 } 230 231 /** 232 * @tc.name: nnbackendtest_getgackendtype_003 233 * @tc.desc: Verify the QuantParams function return nullptr in case of fd -1. 234 * @tc.type: FUNC 235 */ 236 HWTEST_F(NNBackendTest, nnbackendtest_getgackendtype_003, TestSize.Level0) 237 { 238 size_t backendID = 1; 239 std::shared_ptr<MockIDevice> device = std::make_shared<MockIDevice>(); 240 241 OH_NN_DeviceType backendName = OH_NN_OTHERS; 242 EXPECT_CALL(*((MockIDevice *) device.get()), GetDeviceType(::testing::_)) 243 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(backendName), ::testing::Return(OH_NN_SUCCESS))); 244 245 std::unique_ptr<NNBackend> hdiDevice = std::make_unique<NNBackend>(device, backendID); 246 EXPECT_EQ(OH_NN_SUCCESS, hdiDevice->GetBackendType(backendName)); 247 248 testing::Mock::AllowLeak(device.get()); 249 } 250 251 /** 252 * @tc.name: nnbackendtest_getbackendstatus_001 253 * @tc.desc: Verify the QuantParams function return nullptr in case of fd -1. 254 * @tc.type: FUNC 255 */ 256 HWTEST_F(NNBackendTest, nnbackendtest_getbackendstatus_001, TestSize.Level0) 257 { 258 size_t backendID = 1; 259 260 DeviceStatus backendName = UNKNOWN; 261 262 std::unique_ptr<NNBackend> hdiDevice = std::make_unique<NNBackend>(nullptr, backendID); 263 EXPECT_EQ(OH_NN_FAILED, hdiDevice->GetBackendStatus(backendName)); 264 } 265 266 /** 267 * @tc.name: nnbackendtest_getbackendstatus_002 268 * @tc.desc: Verify the QuantParams function return nullptr in case of fd -1. 269 * @tc.type: FUNC 270 */ 271 HWTEST_F(NNBackendTest, nnbackendtest_getbackendstatus_002, TestSize.Level0) 272 { 273 size_t backendID = 1; 274 std::shared_ptr<MockIDevice> device = std::make_shared<MockIDevice>(); 275 276 DeviceStatus backendName = UNKNOWN; 277 EXPECT_CALL(*((MockIDevice *) device.get()), GetDeviceStatus(::testing::_)) 278 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(backendName), ::testing::Return(OH_NN_FAILED))); 279 280 std::unique_ptr<NNBackend> hdiDevice = std::make_unique<NNBackend>(device, backendID); 281 EXPECT_EQ(OH_NN_FAILED, hdiDevice->GetBackendStatus(backendName)); 282 283 testing::Mock::AllowLeak(device.get()); 284 } 285 286 /** 287 * @tc.name: nnbackendtest_getbackendstatus_003 288 * @tc.desc: Verify the QuantParams function return nullptr in case of fd -1. 289 * @tc.type: FUNC 290 */ 291 HWTEST_F(NNBackendTest, nnbackendtest_getbackendstatus_003, TestSize.Level0) 292 { 293 size_t backendID = 1; 294 std::shared_ptr<MockIDevice> device = std::make_shared<MockIDevice>(); 295 296 DeviceStatus backendName = UNKNOWN; 297 EXPECT_CALL(*((MockIDevice *) device.get()), GetDeviceStatus(::testing::_)) 298 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(backendName), ::testing::Return(OH_NN_SUCCESS))); 299 300 std::unique_ptr<NNBackend> hdiDevice = std::make_unique<NNBackend>(device, backendID); 301 EXPECT_EQ(OH_NN_SUCCESS, hdiDevice->GetBackendStatus(backendName)); 302 303 testing::Mock::AllowLeak(device.get()); 304 } 305 306 /** 307 * @tc.name: nnbackendtest_createcompiler_001 308 * @tc.desc: Verify the QuantParams function return nullptr in case of fd -1. 309 * @tc.type: FUNC 310 */ 311 HWTEST_F(NNBackendTest, nnbackendtest_createcompiler_001, TestSize.Level0) 312 { 313 size_t backendID = 1; 314 315 Compilation backendName; 316 Compilation* compilation = &backendName; 317 318 std::unique_ptr<NNBackend> hdiDevice = std::make_unique<NNBackend>(nullptr, backendID); 319 EXPECT_NE(nullptr, hdiDevice->CreateCompiler(compilation)); 320 } 321 322 /** 323 * @tc.name: nnbackendtest_createcompiler_002 324 * @tc.desc: Verify the QuantParams function return nullptr in case of fd -1. 325 * @tc.type: FUNC 326 */ 327 HWTEST_F(NNBackendTest, nnbackendtest_createcompiler_002, TestSize.Level0) 328 { 329 size_t backendID = 1; 330 std::shared_ptr<MockIDevice> device = std::make_shared<MockIDevice>(); 331 332 Compilation backendName; 333 char a = 'a'; 334 backendName.offlineModelPath = &a; 335 char b = 'b'; 336 backendName.offlineModelBuffer.first = &b; 337 backendName.offlineModelBuffer.second = static_cast<size_t>(0); 338 Compilation* compilation = &backendName; 339 340 std::unique_ptr<NNBackend> hdiDevice = std::make_unique<NNBackend>(device, backendID); 341 EXPECT_EQ(nullptr, hdiDevice->CreateCompiler(compilation)); 342 343 testing::Mock::AllowLeak(device.get()); 344 } 345 346 /** 347 * @tc.name: nnbackendtest_destroycompiler_001 348 * @tc.desc: Verify the QuantParams function return nullptr in case of fd -1. 349 * @tc.type: FUNC 350 */ 351 HWTEST_F(NNBackendTest, nnbackendtest_destroycompiler_001, TestSize.Level0) 352 { 353 size_t backendID = 1; 354 355 std::unique_ptr<NNBackend> hdiDevice = std::make_unique<NNBackend>(nullptr, backendID); 356 EXPECT_EQ(OH_NN_INVALID_PARAMETER, hdiDevice->DestroyCompiler(nullptr)); 357 } 358 359 /** 360 * @tc.name: nnbackendtest_destroycompiler_002 361 * @tc.desc: Verify the QuantParams function return nullptr in case of fd -1. 362 * @tc.type: FUNC 363 */ 364 HWTEST_F(NNBackendTest, nnbackendtest_destroycompiler_002, TestSize.Level0) 365 { 366 size_t backendID = 1; 367 std::shared_ptr<MockIDevice> device = std::make_shared<MockIDevice>(); 368 369 NNCompiler* nncompiler = new (std::nothrow) NNCompiler(device, backendID); 370 371 std::unique_ptr<NNBackend> hdiDevice = std::make_unique<NNBackend>(device, backendID); 372 EXPECT_EQ(OH_NN_SUCCESS, hdiDevice->DestroyCompiler(nncompiler)); 373 374 testing::Mock::AllowLeak(device.get()); 375 } 376 377 /** 378 * @tc.name: nnbackendtest_CreateExecutor_001 379 * @tc.desc: Verify the QuantParams function return nullptr in case of fd -1. 380 * @tc.type: FUNC 381 */ 382 HWTEST_F(NNBackendTest, nnbackendtest_CreateExecutor_001, TestSize.Level0) 383 { 384 size_t backendID = 1; 385 std::shared_ptr<MockIDevice> device = std::make_shared<MockIDevice>(); 386 387 std::unique_ptr<NNBackend> hdiDevice = std::make_unique<NNBackend>(device, backendID); 388 EXPECT_EQ(nullptr, hdiDevice->CreateExecutor(nullptr)); 389 390 testing::Mock::AllowLeak(device.get()); 391 } 392 393 /** 394 * @tc.name: nnbackendtest_CreateExecutor_002 395 * @tc.desc: Verify the QuantParams function return nullptr in case of fd -1. 396 * @tc.type: FUNC 397 */ 398 HWTEST_F(NNBackendTest, nnbackendtest_CreateExecutor_002, TestSize.Level0) 399 { 400 size_t backendID = 1; 401 std::shared_ptr<MockIDevice> device = std::make_shared<MockIDevice>(); 402 403 Compilation backendName; 404 Compilation* compilation = &backendName; 405 406 std::unique_ptr<NNBackend> hdiDevice = std::make_unique<NNBackend>(device, backendID); 407 EXPECT_EQ(nullptr, hdiDevice->CreateExecutor(compilation)); 408 409 testing::Mock::AllowLeak(device.get()); 410 } 411 412 /** 413 * @tc.name: nnbackendtest_CreateExecutor_003 414 * @tc.desc: Verify the QuantParams function return nullptr in case of fd -1. 415 * @tc.type: FUNC 416 */ 417 HWTEST_F(NNBackendTest, nnbackendtest_CreateExecutor_003, TestSize.Level0) 418 { 419 size_t backendID = 1; 420 std::shared_ptr<MockIDevice> device = std::make_shared<MockIDevice>(); 421 422 Compilation *compilation = new (std::nothrow) Compilation(); 423 424 std::unique_ptr<NNBackend> hdiDevice = std::make_unique<NNBackend>(device, backendID); 425 EXPECT_EQ(nullptr, hdiDevice->CreateExecutor(compilation)); 426 427 testing::Mock::AllowLeak(device.get()); 428 } 429 430 /** 431 * @tc.name: nnbackendtest_DestroyExecutor_001 432 * @tc.desc: Verify the QuantParams function return nullptr in case of fd -1. 433 * @tc.type: FUNC 434 */ 435 HWTEST_F(NNBackendTest, nnbackendtest_DestroyExecutor_001, TestSize.Level0) 436 { 437 size_t backendID = 1; 438 std::shared_ptr<MockIDevice> device = std::make_shared<MockIDevice>(); 439 440 std::unique_ptr<NNBackend> hdiDevice = std::make_unique<NNBackend>(device, backendID); 441 EXPECT_EQ(OH_NN_INVALID_PARAMETER, hdiDevice->DestroyExecutor(nullptr)); 442 443 testing::Mock::AllowLeak(device.get()); 444 } 445 446 /** 447 * @tc.name: nnbackendtest_createtensor_001 448 * @tc.desc: Verify the QuantParams function return nullptr in case of fd -1. 449 * @tc.type: FUNC 450 */ 451 HWTEST_F(NNBackendTest, nnbackendtest_createtensor_001, TestSize.Level0) 452 { 453 size_t backendID = 1; 454 std::shared_ptr<MockIDevice> device = std::make_shared<MockIDevice>(); 455 456 std::unique_ptr<NNBackend> hdiDevice = std::make_unique<NNBackend>(device, backendID); 457 EXPECT_EQ(nullptr, hdiDevice->CreateTensor(nullptr)); 458 459 testing::Mock::AllowLeak(device.get()); 460 } 461 462 /** 463 * @tc.name: nnbackendtest_createtensor_002 464 * @tc.desc: Verify the QuantParams function return nullptr in case of fd -1. 465 * @tc.type: FUNC 466 */ 467 HWTEST_F(NNBackendTest, nnbackendtest_createtensor_002, TestSize.Level0) 468 { 469 size_t backendID = 1; 470 std::shared_ptr<MockIDevice> device = std::make_shared<MockIDevice>(); 471 TensorDesc desc; 472 TensorDesc* tensorDesc = &desc; 473 474 std::unique_ptr<NNBackend> hdiDevice = std::make_unique<NNBackend>(device, backendID); 475 EXPECT_NE(nullptr, hdiDevice->CreateTensor(tensorDesc)); 476 477 testing::Mock::AllowLeak(device.get()); 478 } 479 480 /** 481 * @tc.name: nnbackendtest_destroytensor_001 482 * @tc.desc: Verify the QuantParams function return nullptr in case of fd -1. 483 * @tc.type: FUNC 484 */ 485 HWTEST_F(NNBackendTest, nnbackendtest_destroytensor_001, TestSize.Level0) 486 { 487 size_t backendID = 1; 488 std::shared_ptr<MockIDevice> device = std::make_shared<MockIDevice>(); 489 490 std::unique_ptr<NNBackend> hdiDevice = std::make_unique<NNBackend>(device, backendID); 491 EXPECT_EQ(OH_NN_INVALID_PARAMETER, hdiDevice->DestroyTensor(nullptr)); 492 493 testing::Mock::AllowLeak(device.get()); 494 } 495 496 /** 497 * @tc.name: nnbackendtest_getdevice_001 498 * @tc.desc: Verify the QuantParams function return nullptr in case of fd -1. 499 * @tc.type: FUNC 500 */ 501 HWTEST_F(NNBackendTest, nnbackendtest_getdevice_001, TestSize.Level0) 502 { 503 size_t backendID = 1; 504 std::shared_ptr<MockIDevice> device = std::make_shared<MockIDevice>(); 505 506 std::unique_ptr<NNBackend> hdiDevice = std::make_unique<NNBackend>(nullptr, backendID); 507 EXPECT_EQ(nullptr, hdiDevice->GetDevice()); 508 509 testing::Mock::AllowLeak(device.get()); 510 } 511 512 /** 513 * @tc.name: nnbackendtest_getsupportedoperation_001 514 * @tc.desc: Verify the QuantParams function return nullptr in case of fd -1. 515 * @tc.type: FUNC 516 */ 517 HWTEST_F(NNBackendTest, nnbackendtest_getsupportedoperation_001, TestSize.Level0) 518 { 519 size_t backendID = 1; 520 std::shared_ptr<MockIDevice> device = std::make_shared<MockIDevice>(); 521 522 std::shared_ptr<const mindspore::lite::LiteGraph> model = nullptr; 523 std::vector<bool> ops; 524 525 std::unique_ptr<NNBackend> hdiDevice = std::make_unique<NNBackend>(nullptr, backendID); 526 EXPECT_EQ(OH_NN_INVALID_PARAMETER, hdiDevice->GetSupportedOperation(model, ops)); 527 528 testing::Mock::AllowLeak(device.get()); 529 } 530 531 /** 532 * @tc.name: nnbackendtest_getsupportedoperation_002 533 * @tc.desc: Verify the QuantParams function return nullptr in case of fd -1. 534 * @tc.type: FUNC 535 */ 536 HWTEST_F(NNBackendTest, nnbackendtest_getsupportedoperation_002, TestSize.Level0) 537 { 538 size_t backendID = 1; 539 std::shared_ptr<MockIDevice> device = std::make_shared<MockIDevice>(); 540 541 std::vector<bool> ops; 542 std::shared_ptr<mindspore::lite::LiteGraph> model = std::make_shared<mindspore::lite::LiteGraph>(); 543 544 std::unique_ptr<NNBackend> hdiDevice = std::make_unique<NNBackend>(nullptr, backendID); 545 EXPECT_EQ(OH_NN_FAILED, hdiDevice->GetSupportedOperation(model, ops)); 546 547 testing::Mock::AllowLeak(device.get()); 548 } 549 550 /** 551 * @tc.name: nnbackendtest_getsupportedoperation_003 552 * @tc.desc: Verify the QuantParams function return nullptr in case of fd -1. 553 * @tc.type: FUNC 554 */ 555 HWTEST_F(NNBackendTest, nnbackendtest_getsupportedoperation_003, TestSize.Level0) 556 { 557 size_t backendID = 1; 558 std::shared_ptr<MockIDevice> device = std::make_shared<MockIDevice>(); 559 560 std::vector<bool> ops; 561 std::shared_ptr<mindspore::lite::LiteGraph> model = std::make_shared<mindspore::lite::LiteGraph>(); 562 563 EXPECT_CALL(*((MockIDevice *) device.get()), GetSupportedOperation(::testing::_, ::testing::_)) 564 .WillRepeatedly(::testing::Return(OH_NN_FAILED)); 565 566 std::unique_ptr<NNBackend> hdiDevice = std::make_unique<NNBackend>(nullptr, backendID); 567 EXPECT_EQ(OH_NN_FAILED, hdiDevice->GetSupportedOperation(model, ops)); 568 569 testing::Mock::AllowLeak(device.get()); 570 } 571 572 /** 573 * @tc.name: nnbackendtest_getsupportedoperation_004 574 * @tc.desc: Verify the QuantParams function return nullptr in case of fd -1. 575 * @tc.type: FUNC 576 */ 577 HWTEST_F(NNBackendTest, nnbackendtest_getsupportedoperation_004, TestSize.Level0) 578 { 579 size_t backendID = 1; 580 std::shared_ptr<MockIDevice> device = std::make_shared<MockIDevice>(); 581 582 std::vector<bool> ops; 583 std::shared_ptr<mindspore::lite::LiteGraph> model = std::make_shared<mindspore::lite::LiteGraph>(); 584 585 EXPECT_CALL(*((MockIDevice *) device.get()), GetSupportedOperation(::testing::_, ::testing::_)) 586 .WillRepeatedly(::testing::Return(OH_NN_SUCCESS)); 587 588 std::unique_ptr<NNBackend> hdiDevice = std::make_unique<NNBackend>(nullptr, backendID); 589 EXPECT_EQ(OH_NN_FAILED, hdiDevice->GetSupportedOperation(model, ops)); 590 591 testing::Mock::AllowLeak(device.get()); 592 } 593 } // namespace UnitTest 594 } // namespace NeuralNetworkRuntime 595 } // namespace OHOS