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