已合并
fix: fix fe clean code #2615
lushiming0创建于 6月4日
fix: fix fe clean code #2615
已合并
共 9 个文件变更+1467-35
| @@ -4,7 +4,7 @@ | |||
| 4 | "errClass": "FE Errors", | 4 | "errClass": "FE Errors", |
| 5 | "errTitle": "Compilation_Error", | 5 | "errTitle": "Compilation_Error", |
| 6 | "ErrCode": "E20001", | 6 | "ErrCode": "E20001", |
| 7 | - "ErrMessage": "Operator %s compilation failed.Optype is %s.", | 7 | + "ErrMessage": "Operator %s compilation failed. Optype is %s.", |
| 8 | "Arglist": "pass_name,pass_type", | 8 | "Arglist": "pass_name,pass_type", |
| 9 | "suggestion": { | 9 | "suggestion": { |
| 10 | "Possible Cause": "1. The operator has an invalid argument. 2. The operator implementation logic is abnormal.", | 10 | "Possible Cause": "1. The operator has an invalid argument. 2. The operator implementation logic is abnormal.", |
| @@ -353,7 +353,7 @@ | |||
| 353 | } | 353 | } |
| 354 | }, | 354 | }, |
| 355 | { | 355 | { |
| 356 | - "errClass": " TEFusion Errors", | 356 | + "errClass": "TEFusion Errors", |
| 357 | "errTitle": "Environment_Error_Import_Python_Module_Failed", | 357 | "errTitle": "Environment_Error_Import_Python_Module_Failed", |
| 358 | "ErrCode": "E40020", | 358 | "ErrCode": "E40020", |
| 359 | "ErrMessage": "Failed to import Python module %s. Reason: %s.", | 359 | "ErrMessage": "Failed to import Python module %s. Reason: %s.", |
| @@ -379,7 +379,7 @@ | |||
| 379 | "errTitle": "Invalid_Argument", | 379 | "errTitle": "Invalid_Argument", |
| 380 | "ErrCode": "E40022", | 380 | "ErrCode": "E40022", |
| 381 | "ErrMessage": "Value %s for parameter %s is invalid. The value must be %s.", | 381 | "ErrMessage": "Value %s for parameter %s is invalid. The value must be %s.", |
| 382 | - "Arglist": "invalid_value,argument,valid_range ", | 382 | + "Arglist": "invalid_value,argument,valid_range", |
| 383 | "suggestion": { | 383 | "suggestion": { |
| 384 | "Possible Cause": "N/A", | 384 | "Possible Cause": "N/A", |
| 385 | "Solution": "N/A" | 385 | "Solution": "N/A" |
| @@ -58,14 +58,11 @@ file(GLOB FE_SRC_CPP_1 RELATIVE ${CMAKE_CURRENT_LIST_DIR} | |||
| 58 | 58 | ||
| 59 | add_library(platform SHARED | 59 | add_library(platform SHARED |
| 60 | ${FE_SRC_CPP_1} | 60 | ${FE_SRC_CPP_1} |
| 61 | - ${PROTO_SRCS} | 61 | + ${PROTO_SRCS}) |
| 62 | - ${RUNTIME_DIR}/src/dfx/error_manager | ||
| 63 | - ${RUNTIME_DIR}/include/dfx/base) | ||
| 64 | 62 | ||
| 65 | 63 | ||
| 66 | add_dependencies(platform platform_infos_proto) | 64 | add_dependencies(platform platform_infos_proto) |
| 67 | target_include_directories(platform PRIVATE | 65 | target_include_directories(platform PRIVATE |
| 68 | - # ${CMAKE_CURRENT_LIST_DIR}/../dfx/include/log/toolchain | ||
| 69 | ${CMAKE_CURRENT_LIST_DIR} | 66 | ${CMAKE_CURRENT_LIST_DIR} |
| 70 | ${CMAKE_CURRENT_LIST_DIR}/proto/google | 67 | ${CMAKE_CURRENT_LIST_DIR}/proto/google |
| 71 | ${RUNTIME_DIR}/pkg_inc | 68 | ${RUNTIME_DIR}/pkg_inc |
| @@ -90,9 +87,7 @@ target_compile_definitions(platform PRIVATE | |||
| 90 | 87 | ||
| 91 | ADD_LIBRARY(platform_static STATIC | 88 | ADD_LIBRARY(platform_static STATIC |
| 92 | ${STATICPLATFORMFILE} | 89 | ${STATICPLATFORMFILE} |
| 93 | - ${PROTO_SRCS} | 90 | + ${PROTO_SRCS}) |
| 94 | - ${RUNTIME_DIR}/src/dfx/error_manager | ||
| 95 | - ${RUNTIME_DIR}/include/dfx/base) | ||
| 96 | add_dependencies(platform_static platform_infos_proto) | 91 | add_dependencies(platform_static platform_infos_proto) |
| 97 | target_include_directories(platform_static PRIVATE | 92 | target_include_directories(platform_static PRIVATE |
| 98 | # ${CMAKE_CURRENT_LIST_DIR}/../dfx/include/log/toolchain | 93 | # ${CMAKE_CURRENT_LIST_DIR}/../dfx/include/log/toolchain |
| @@ -12,7 +12,7 @@ CCEC_VECTOR_version=dav-c310-vec | |||
| 12 | Compiler_aicpu_support_os=true | 12 | Compiler_aicpu_support_os=true |
| 13 | NpuArch=3510 | 13 | NpuArch=3510 |
| 14 | Chip_type=15 | 14 | Chip_type=15 |
| 15 | - | 15 | + |
| 16 | [DieInfo] | 16 | [DieInfo] |
| 17 | io_die_num=2 | 17 | io_die_num=2 |
| 18 | 18 | ||
| @@ -13,6 +13,7 @@ | |||
| 13 | 13 | ||
| 14 | 14 | ||
| 15 | 15 | ||
| 16 | + | ||
| 16 | 17 | ||
| 17 | 18 | ||
| 18 | 19 | ||
| @@ -204,9 +205,9 @@ void PlatformInfoManager::ParseVersion(std::map<std::string, std::string> &versi | |||
| 204 | platform_info_temp.str_info.ccec_aiv_version = ccec_aiv_version->second; | 205 | platform_info_temp.str_info.ccec_aiv_version = ccec_aiv_version->second; |
| 205 | } | 206 | } |
| 206 | 207 | ||
| 207 | - std::map<std::string, std::string>::const_iterator is_support_a_icpu = version_map.find(STR_IS_SUPPORT_AICPU); | 208 | + std::map<std::string, std::string>::const_iterator is_support_aicpu = version_map.find(STR_IS_SUPPORT_AICPU); |
| 208 | - if (is_support_a_icpu != version_map.end()) { | 209 | + if (is_support_aicpu != version_map.end()) { |
| 209 | - platform_info_temp.str_info.is_support_ai_cpu_compiler = is_support_a_icpu->second; | 210 | + platform_info_temp.str_info.is_support_ai_cpu_compiler = is_support_aicpu->second; |
| 210 | } | 211 | } |
| 211 | 212 | ||
| 212 | std::map<std::string, std::string>::const_iterator short_soc_version = version_map.find(SHORT_SOC_VERSION); | 213 | std::map<std::string, std::string>::const_iterator short_soc_version = version_map.find(SHORT_SOC_VERSION); |
| @@ -386,29 +387,54 @@ void PlatformInfoManager::ParseUBOfAICoreSpec(std::map<std::string, std::string> | |||
| 386 | } | 387 | } |
| 387 | } | 388 | } |
| 388 | 389 | ||
| 389 | -void PlatformInfoManager::ParseUnzipOfAICoreSpec(std::map<std::string, std::string> &ai_core_spec_map, PlatformInfo &platform_info_temp) { | 390 | +void PlatformInfoManager::ParseUnzipOfAICoreSpec(std::map<std::string, std::string> &ai_core_spec_map, |
| 391 | + PlatformInfo &platform_info_temp) { | ||
| 390 | try { | 392 | try { |
| 391 | std::map<std::string, std::string>::const_iterator unzip_engines = ai_core_spec_map.find(CONVERT(unzip_engines)); | 393 | std::map<std::string, std::string>::const_iterator unzip_engines = ai_core_spec_map.find(CONVERT(unzip_engines)); |
| 392 | if (unzip_engines != ai_core_spec_map.end()) { | 394 | if (unzip_engines != ai_core_spec_map.end()) { |
| 393 | - platform_info_temp.ai_core_spec.unzip_engines = static_cast<uint32_t>(stoll(unzip_engines->second)); | 395 | + unsigned long value = std::stoul(unzip_engines->second); |
| 396 | + if (value > UINT32_MAX) { | ||
| 397 | + PF_LOGE("unzip_engines value [%lu] exceeds uint32 range", value); | ||
| 398 | + return; | ||
| 399 | + } | ||
| 400 | + platform_info_temp.ai_core_spec.unzip_engines = static_cast<uint32_t>(value); | ||
| 394 | } | 401 | } |
| 395 | 402 | ||
| 396 | std::map<std::string, std::string>::const_iterator unzip_max_ratios = ai_core_spec_map.find(CONVERT(unzip_max_ratios)); | 403 | std::map<std::string, std::string>::const_iterator unzip_max_ratios = ai_core_spec_map.find(CONVERT(unzip_max_ratios)); |
| 397 | if (unzip_max_ratios != ai_core_spec_map.end()) { | 404 | if (unzip_max_ratios != ai_core_spec_map.end()) { |
| 398 | - platform_info_temp.ai_core_spec.unzip_max_ratios = static_cast<uint32_t>(stoll(unzip_max_ratios->second)); | 405 | + unsigned long value = std::stoul(unzip_max_ratios->second); |
| 406 | + if (value > UINT32_MAX) { | ||
| 407 | + PF_LOGE("unzip_max_ratios value [%lu] exceeds uint32 range", value); | ||
| 408 | + return; | ||
| 409 | + } | ||
| 410 | + platform_info_temp.ai_core_spec.unzip_max_ratios = static_cast<uint32_t>(value); | ||
| 399 | } | 411 | } |
| 400 | 412 | ||
| 401 | std::map<std::string, std::string>::const_iterator unzip_channels = ai_core_spec_map.find(CONVERT(unzip_channels)); | 413 | std::map<std::string, std::string>::const_iterator unzip_channels = ai_core_spec_map.find(CONVERT(unzip_channels)); |
| 402 | if (unzip_channels != ai_core_spec_map.end()) { | 414 | if (unzip_channels != ai_core_spec_map.end()) { |
| 403 | - platform_info_temp.ai_core_spec.unzip_channels = static_cast<uint32_t>(stoll(unzip_channels->second)); | 415 | + unsigned long value = std::stoul(unzip_channels->second); |
| 416 | + if (value > UINT32_MAX) { | ||
| 417 | + PF_LOGE("unzip_channels value [%lu] exceeds uint32 range", value); | ||
| 418 | + return; | ||
| 419 | + } | ||
| 420 | + platform_info_temp.ai_core_spec.unzip_channels = static_cast<uint32_t>(value); | ||
| 404 | } | 421 | } |
| 405 | 422 | ||
| 406 | std::map<std::string, std::string>::const_iterator unzip_is_tight = ai_core_spec_map.find(CONVERT(unzip_is_tight)); | 423 | std::map<std::string, std::string>::const_iterator unzip_is_tight = ai_core_spec_map.find(CONVERT(unzip_is_tight)); |
| 407 | if (unzip_is_tight != ai_core_spec_map.end()) { | 424 | if (unzip_is_tight != ai_core_spec_map.end()) { |
| 408 | - platform_info_temp.ai_core_spec.unzip_is_tight = static_cast<uint8_t>(stoll(unzip_is_tight->second)); | 425 | + unsigned long value = std::stoul(unzip_is_tight->second); |
| 426 | + if (value > UINT8_MAX) { | ||
| 427 | + PF_LOGE("unzip_is_tight value [%lu] exceeds uint8 range", value); | ||
| 428 | + return; | ||
| 429 | + } | ||
| 430 | + platform_info_temp.ai_core_spec.unzip_is_tight = static_cast<uint8_t>(value); | ||
| 409 | } | 431 | } |
| 410 | - } catch (...) { | 432 | + } catch (const std::invalid_argument &e) { |
| 411 | - PF_LOGE("Failed to load AICoreSpecs."); | 433 | + PF_LOGE("Invalid argument in ParseUnzipOfAICoreSpec: %s", e.what()); |
| 434 | + return; | ||
| 435 | + } catch (const std::out_of_range &e) { | ||
| 436 | + PF_LOGE("Out of range in ParseUnzipOfAICoreSpec: %s", e.what()); | ||
| 437 | + return; | ||
| 412 | } | 438 | } |
| 413 | } | 439 | } |
| 414 | 440 | ||
| @@ -421,18 +447,64 @@ void PlatformInfoManager::ParseAICoreSpec(std::map<std::string, std::string> &ai | |||
| 421 | return; | 447 | return; |
| 422 | } | 448 | } |
| 423 | 449 | ||
| 424 | -static double CalculateFraction(std::string fraction) { | 450 | +static double CalculateFraction(const std::string &fraction) { |
| 451 | + if (fraction.empty()) { | ||
| 452 | + PF_LOGE("CalculateFraction: input string is empty."); | ||
| 453 | + return 0.0; | ||
| 454 | + } | ||
| 455 | + | ||
| 425 | size_t pos = fraction.find('/'); | 456 | size_t pos = fraction.find('/'); |
| 426 | try { | 457 | try { |
| 427 | if (pos == std::string::npos) { | 458 | if (pos == std::string::npos) { |
| 428 | - return static_cast<double>(stod(fraction)); | 459 | + return std::stod(fraction); |
| 429 | } | 460 | } |
| 430 | 461 | ||
| 431 | - double numerator = static_cast<double>(stod(fraction.substr(0, pos - 1))); | 462 | + if (pos == 0 || pos == fraction.length() - 1) { |
| 432 | - double denominator = static_cast<double>(stod(fraction.substr(pos + 1))); | 463 | + PF_LOGE("CalculateFraction: invalid fraction format [%s], '/' at boundary.", fraction.c_str()); |
| 433 | - return (numerator / denominator); | 464 | + return 0.0; |
| 465 | + } | ||
| 466 | + | ||
| 467 | + std::string denominator_str = fraction.substr(pos + 1); | ||
| 468 | + | ||
| 469 | + // 在转换前检查字符串是否表示零值(避免浮点数直接比较) | ||
| 470 | + std::string trimmed_denominator = denominator_str; | ||
| 471 | + size_t start = trimmed_denominator.find_first_not_of(" \t"); | ||
| 472 | + size_t end = trimmed_denominator.find_last_not_of(" \t"); | ||
| 473 | + if (start != std::string::npos && end != std::string::npos) { | ||
| 474 | + trimmed_denominator = trimmed_denominator.substr(start, end - start + 1); | ||
| 475 | + } | ||
| 476 | + if (trimmed_denominator == "0" || trimmed_denominator == "0.0" || | ||
| 477 | + trimmed_denominator == "0." || trimmed_denominator == "-0" || | ||
| 478 | + trimmed_denominator == "-0.0" || trimmed_denominator == "-0.") { | ||
| 479 | + PF_LOGE("CalculateFraction: denominator is zero in [%s].", fraction.c_str()); | ||
| 480 | + return 0.0; | ||
| 481 | + } | ||
| 482 | + | ||
| 483 | + double denominator = std::stod(denominator_str); | ||
| 484 | + | ||
| 485 | + // 使用 EPSILON 检查接近零的情况(避免直接比较浮点数) | ||
| 486 | + constexpr double EPSILON = 1e-10; | ||
| 487 | + if (std::abs(denominator) < EPSILON) { | ||
| 488 | + PF_LOGE("CalculateFraction: denominator is too small [%s].", fraction.c_str()); | ||
| 489 | + return 0.0; | ||
| 490 | + } | ||
| 491 | + | ||
| 492 | + double numerator = std::stod(fraction.substr(0, pos)); | ||
| 493 | + double result = numerator / denominator; | ||
| 494 | + | ||
| 495 | + // 检查结果是否有效 | ||
| 496 | + if (std::isinf(result) || std::isnan(result)) { | ||
| 497 | + PF_LOGE("CalculateFraction: invalid result for [%s].", fraction.c_str()); | ||
| 498 | + return 0.0; | ||
| 499 | + } | ||
| 500 | + | ||
| 501 | + return result; | ||
| 502 | + } catch (const std::exception &e) { | ||
| 503 | + PF_LOGE("CalculateFraction: failed to parse [%s], error: %s.", fraction.c_str(), e.what()); | ||
| 504 | + return 0.0; | ||
| 434 | } catch (...) { | 505 | } catch (...) { |
| 435 | - return 0; | 506 | + PF_LOGE("CalculateFraction: unknown exception parsing [%s].", fraction.c_str()); |
| 507 | + return 0.0; | ||
| 436 | } | 508 | } |
| 437 | } | 509 | } |
| 438 | 510 | ||
| @@ -670,19 +742,34 @@ void PlatformInfoManager::ParseVectorCoreMemoryRates(std::map<std::string, std:: | |||
| 670 | } | 742 | } |
| 671 | } | 743 | } |
| 672 | 744 | ||
| 673 | -void PlatformInfoManager::ParseCPUCache(std::map<std::string, std::string> &CPUCacheMap, PlatformInfo &platform_info_temp) { | 745 | +void PlatformInfoManager::ParseCPUCache(std::map<std::string, std::string> &CPUCacheMap, |
| 746 | + PlatformInfo &platform_info_temp) { | ||
| 674 | try { | 747 | try { |
| 675 | std::map<std::string, std::string>::const_iterator AICPUSyncBySW = CPUCacheMap.find(CONVERT(AICPUSyncBySW)); | 748 | std::map<std::string, std::string>::const_iterator AICPUSyncBySW = CPUCacheMap.find(CONVERT(AICPUSyncBySW)); |
| 676 | if (AICPUSyncBySW != CPUCacheMap.end()) { | 749 | if (AICPUSyncBySW != CPUCacheMap.end()) { |
| 677 | - platform_info_temp.cpucache.AICPUSyncBySW = static_cast<uint32_t>(stod(AICPUSyncBySW->second)); | 750 | + unsigned long value = std::stoul(AICPUSyncBySW->second); |
| 751 | + if (value > UINT32_MAX) { | ||
| 752 | + PF_LOGE("AICPUSyncBySW value [%lu] exceeds uint32 range", value); | ||
| 753 | + return; | ||
| 754 | + } | ||
| 755 | + platform_info_temp.cpucache.AICPUSyncBySW = static_cast<uint32_t>(value); | ||
| 678 | } | 756 | } |
| 679 | 757 | ||
| 680 | std::map<std::string, std::string>::const_iterator TSCPUSyncBySW = CPUCacheMap.find(CONVERT(TSCPUSyncBySW)); | 758 | std::map<std::string, std::string>::const_iterator TSCPUSyncBySW = CPUCacheMap.find(CONVERT(TSCPUSyncBySW)); |
| 681 | if (TSCPUSyncBySW != CPUCacheMap.end()) { | 759 | if (TSCPUSyncBySW != CPUCacheMap.end()) { |
| 682 | - platform_info_temp.cpucache.TSCPUSyncBySW = static_cast<uint32_t>(stod(TSCPUSyncBySW->second)); | 760 | + unsigned long value = std::stoul(TSCPUSyncBySW->second); |
| 761 | + if (value > UINT32_MAX) { | ||
| 762 | + PF_LOGE("TSCPUSyncBySW value [%lu] exceeds uint32 range", value); | ||
| 763 | + return; | ||
| 764 | + } | ||
| 765 | + platform_info_temp.cpucache.TSCPUSyncBySW = static_cast<uint32_t>(value); | ||
| 683 | } | 766 | } |
| 684 | - } catch (...) { | 767 | + } catch (const std::invalid_argument &e) { |
| 685 | - PF_LOGE("Failed to load CPU cache."); | 768 | + PF_LOGE("Invalid argument in ParseCPUCache: %s", e.what()); |
| 769 | + return; | ||
| 770 | + } catch (const std::out_of_range &e) { | ||
| 771 | + PF_LOGE("Out of range in ParseCPUCache: %s", e.what()); | ||
| 772 | + return; | ||
| 686 | } | 773 | } |
| 687 | } | 774 | } |
| 688 | 775 | ||
| @@ -18,11 +18,15 @@ | |||
| 18 | 18 | ||
| 19 | 19 | ||
| 20 | 20 | ||
| 21 | +using char_t = char; | ||
| 21 | /** Assigned FE name in log */ | 22 | /** Assigned FE name in log */ |
| 22 | -const std::string PF_MODULE_NAME = "FE"; | 23 | +constexpr const char_t *const PF_MODULE_NAME = "FE"; |
| 23 | 24 | ||
| 24 | inline pid_t PltGetTid() { | 25 | inline pid_t PltGetTid() { |
| 25 | - thread_local static pid_t tid = syscall(__NR_gettid); | 26 | + thread_local static pid_t tid = []() { |
| 27 | + const auto ret = syscall(__NR_gettid); | ||
| 28 | + return (ret == -1) ? getpid() : static_cast<pid_t>(ret); // 失败时降级为 PID | ||
| 29 | + }(); | ||
| 26 | return tid; | 30 | return tid; |
| 27 | } | 31 | } |
| 28 | 32 | ||
| @@ -16,7 +16,10 @@ set(PLATFORM_UT_FILES | |||
| 16 | ${CMAKE_CURRENT_LIST_DIR}/testcase/platform_manager_instance_utest.cpp | 16 | ${CMAKE_CURRENT_LIST_DIR}/testcase/platform_manager_instance_utest.cpp |
| 17 | ${CMAKE_CURRENT_LIST_DIR}/testcase/platform_manager_ge_instance_utest.cpp | 17 | ${CMAKE_CURRENT_LIST_DIR}/testcase/platform_manager_ge_instance_utest.cpp |
| 18 | ${CMAKE_CURRENT_LIST_DIR}/testcase/platform_manager_v2_instance_utest.cpp | 18 | ${CMAKE_CURRENT_LIST_DIR}/testcase/platform_manager_v2_instance_utest.cpp |
| 19 | - ${CMAKE_CURRENT_LIST_DIR}/testcase/platform_manager_acl_utest.cpp) | 19 | + ${CMAKE_CURRENT_LIST_DIR}/testcase/platform_manager_acl_utest.cpp |
| 20 | + ${CMAKE_CURRENT_LIST_DIR}/testcase/platform_infos_def_utest.cpp | ||
| 21 | + ${CMAKE_CURRENT_LIST_DIR}/testcase/platform_infos_utils_utest.cpp | ||
| 22 | + ${CMAKE_CURRENT_LIST_DIR}/testcase/platform_info_exception_utest.cpp) | ||
| 20 | 23 | ||
| 21 | add_executable(platform_utest | 24 | add_executable(platform_utest |
| 22 | ${SLOG_STUB_FILES} | 25 | ${SLOG_STUB_FILES} |
| @@ -0,0 +1,561 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2025 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | + | ||
| 12 | + | ||
| 13 | + | ||
| 14 | + | ||
| 15 | + | ||
| 16 | + | ||
| 17 | + | ||
| 18 | + | ||
| 19 | + | ||
| 20 | + | ||
| 21 | +namespace fe { | ||
| 22 | + | ||
| 23 | +class PlatformInfoExceptionUTest : public testing::Test { | ||
| 24 | +protected: | ||
| 25 | + void SetUp() { | ||
| 26 | + PlatformInfoManager::Instance().InitializePlatformInfo(); | ||
| 27 | + } | ||
| 28 | + | ||
| 29 | + void TearDown() { | ||
| 30 | + PlatformInfoManager::Instance().Finalize(); | ||
| 31 | + GlobalMockObject::verify(); | ||
| 32 | + } | ||
| 33 | +}; | ||
| 34 | + | ||
| 35 | +TEST_F(PlatformInfoExceptionUTest, CalculateFraction_ValidFraction_Success) { | ||
| 36 | + std::map<std::string, std::string> memory_rates; | ||
| 37 | + memory_rates["ub_to_l2_rate"] = "3/4"; | ||
| 38 | + PlatformInfo info; | ||
| 39 | + PlatformInfoManager::Instance().ParseUBOfAICoreMemoryRates(memory_rates, info); | ||
| 40 | + EXPECT_DOUBLE_EQ(info.ai_core_memory_rates.ub_to_l2_rate, 0.75); | ||
| 41 | +} | ||
| 42 | + | ||
| 43 | +TEST_F(PlatformInfoExceptionUTest, CalculateFraction_InvalidFraction_ReturnsZero) { | ||
| 44 | + std::map<std::string, std::string> memory_rates; | ||
| 45 | + memory_rates["ub_to_l2_rate"] = "/invalid"; | ||
| 46 | + PlatformInfo info; | ||
| 47 | + PlatformInfoManager::Instance().ParseUBOfAICoreMemoryRates(memory_rates, info); | ||
| 48 | + EXPECT_DOUBLE_EQ(info.ai_core_memory_rates.ub_to_l2_rate, 0.0); | ||
| 49 | +} | ||
| 50 | + | ||
| 51 | +TEST_F(PlatformInfoExceptionUTest, CalculateFraction_ZeroDenominator_ReturnsZero) { | ||
| 52 | + std::map<std::string, std::string> memory_rates; | ||
| 53 | + memory_rates["ub_to_l2_rate"] = "10/0"; | ||
| 54 | + PlatformInfo info; | ||
| 55 | + PlatformInfoManager::Instance().ParseUBOfAICoreMemoryRates(memory_rates, info); | ||
| 56 | + EXPECT_DOUBLE_EQ(info.ai_core_memory_rates.ub_to_l2_rate, 0.0); | ||
| 57 | +} | ||
| 58 | + | ||
| 59 | +TEST_F(PlatformInfoExceptionUTest, CalculateFraction_EmptyFraction_ReturnsZero) { | ||
| 60 | + std::map<std::string, std::string> memory_rates; | ||
| 61 | + memory_rates["ub_to_l2_rate"] = ""; | ||
| 62 | + PlatformInfo info; | ||
| 63 | + PlatformInfoManager::Instance().ParseUBOfAICoreMemoryRates(memory_rates, info); | ||
| 64 | +} | ||
| 65 | + | ||
| 66 | +TEST_F(PlatformInfoExceptionUTest, CalculateFraction_SlashAtEnd_ReturnsZero) { | ||
| 67 | + std::map<std::string, std::string> memory_rates; | ||
| 68 | + memory_rates["ub_to_l2_rate"] = "10/"; | ||
| 69 | + PlatformInfo info; | ||
| 70 | + PlatformInfoManager::Instance().ParseUBOfAICoreMemoryRates(memory_rates, info); | ||
| 71 | +} | ||
| 72 | + | ||
| 73 | +TEST_F(PlatformInfoExceptionUTest, ParseUnzipOfAICoreSpec_ExceedsRange_ReturnsFalse) { | ||
| 74 | + std::map<std::string, std::string> core_spec; | ||
| 75 | + core_spec["unzip_engines"] = "4294967296"; | ||
| 76 | + PlatformInfo info; | ||
| 77 | + PlatformInfoManager::Instance().ParseUnzipOfAICoreSpec(core_spec, info); | ||
| 78 | +} | ||
| 79 | + | ||
| 80 | +TEST_F(PlatformInfoExceptionUTest, ParseUnzipOfAICoreSpec_InvalidString_ReturnsFalse) { | ||
| 81 | + std::map<std::string, std::string> core_spec; | ||
| 82 | + core_spec["unzip_engines"] = "not_a_number"; | ||
| 83 | + PlatformInfo info; | ||
| 84 | + PlatformInfoManager::Instance().ParseUnzipOfAICoreSpec(core_spec, info); | ||
| 85 | +} | ||
| 86 | + | ||
| 87 | +TEST_F(PlatformInfoExceptionUTest, ParseUBOfAICoreSpec_ExceedsRange_ReturnsFalse) { | ||
| 88 | + std::map<std::string, std::string> core_spec; | ||
| 89 | + core_spec["ub_size"] = "18446744073709551616"; | ||
| 90 | + PlatformInfo info; | ||
| 91 | + PlatformInfoManager::Instance().ParseUBOfAICoreSpec(core_spec, info); | ||
| 92 | +} | ||
| 93 | + | ||
| 94 | +TEST_F(PlatformInfoExceptionUTest, ParseBufferOfAICoreSpec_ExceedsRange_ReturnsFalse) { | ||
| 95 | + std::map<std::string, std::string> core_spec; | ||
| 96 | + core_spec["buffer_size"] = "4294967296"; | ||
| 97 | + PlatformInfo info; | ||
| 98 | + PlatformInfoManager::Instance().ParseBufferOfAICoreSpec(core_spec, info); | ||
| 99 | +} | ||
| 100 | + | ||
| 101 | +TEST_F(PlatformInfoExceptionUTest, ParseCubeOfAICoreSpec_ExceedsRange_ReturnsFalse) { | ||
| 102 | + std::map<std::string, std::string> core_spec; | ||
| 103 | + core_spec["cube_size"] = "18446744073709551616"; | ||
| 104 | + PlatformInfo info; | ||
| 105 | + PlatformInfoManager::Instance().ParseCubeOfAICoreSpec(core_spec, info); | ||
| 106 | +} | ||
| 107 | + | ||
| 108 | +TEST_F(PlatformInfoExceptionUTest, SetCoreNumByCoreType_InvalidNumber_CatchesException) { | ||
| 109 | + PlatFormInfos platform_infos; | ||
| 110 | + platform_infos.Init(); | ||
| 111 | + std::map<std::string, std::string> soc_info; | ||
| 112 | + soc_info["ai_core_cnt"] = "invalid_number"; | ||
| 113 | + platform_infos.SetPlatformRes("SoCInfo", soc_info); | ||
| 114 | + platform_infos.SetCoreNumByCoreType("AICore"); | ||
| 115 | +} | ||
| 116 | + | ||
| 117 | +TEST_F(PlatformInfoExceptionUTest, SetCoreNumByCoreType_MixVectorCore_Success) { | ||
| 118 | + PlatFormInfos platform_infos; | ||
| 119 | + platform_infos.Init(); | ||
| 120 | + std::map<std::string, std::string> soc_info; | ||
| 121 | + soc_info["ai_core_cnt"] = "32"; | ||
| 122 | + soc_info["mix_vector_core_cnt"] = "8"; | ||
| 123 | + platform_infos.SetPlatformRes("SoCInfo", soc_info); | ||
| 124 | + platform_infos.SetCoreNumByCoreType("MIX_VECTOR_CORE"); | ||
| 125 | + EXPECT_EQ(platform_infos.core_num_, 8); | ||
| 126 | +} | ||
| 127 | + | ||
| 128 | +TEST_F(PlatformInfoExceptionUTest, ParseSocInfo_InvalidString_CatchesException) { | ||
| 129 | + std::map<std::string, std::string> soc_info; | ||
| 130 | + soc_info["ai_core_cnt"] = "not_a_number"; | ||
| 131 | + PlatformInfo info; | ||
| 132 | + PlatformInfoManager::Instance().ParseSocInfo(soc_info, info); | ||
| 133 | +} | ||
| 134 | + | ||
| 135 | +TEST_F(PlatformInfoExceptionUTest, GetPlatformRes_WithLock_Success) { | ||
| 136 | + PlatFormInfos platform_infos; | ||
| 137 | + platform_infos.Init(); | ||
| 138 | + | ||
| 139 | + std::map<std::string, std::string> res_map; | ||
| 140 | + res_map["key1"] = "value1"; | ||
| 141 | + platform_infos.SetPlatformRes("TestSection", res_map); | ||
| 142 | + | ||
| 143 | + std::map<std::string, std::string> result; | ||
| 144 | + EXPECT_TRUE(platform_infos.GetPlatformResWithLock("TestSection", result)); | ||
| 145 | + EXPECT_EQ(result["key1"], "value1"); | ||
| 146 | +} | ||
| 147 | + | ||
| 148 | +TEST_F(PlatformInfoExceptionUTest, GetPlatformRes_MapMap_WithLock_Success) { | ||
| 149 | + PlatFormInfos platform_infos; | ||
| 150 | + platform_infos.Init(); | ||
| 151 | + | ||
| 152 | + std::map<std::string, std::map<std::string, std::string>> res_map_map; | ||
| 153 | + std::map<std::string, std::string> inner_map; | ||
| 154 | + inner_map["inner_key"] = "inner_value"; | ||
| 155 | + res_map_map["outer_key"] = inner_map; | ||
| 156 | + | ||
| 157 | + std::map<std::string, std::map<std::string, std::string>> result; | ||
| 158 | + EXPECT_TRUE(platform_infos.GetPlatformResWithLock(result)); | ||
| 159 | +} | ||
| 160 | + | ||
| 161 | +TEST_F(PlatformInfoExceptionUTest, SetPlatformRes_WithLock_Success) { | ||
| 162 | + PlatFormInfos platform_infos; | ||
| 163 | + platform_infos.Init(); | ||
| 164 | + | ||
| 165 | + std::map<std::string, std::string> res; | ||
| 166 | + res["key1"] = "value1"; | ||
| 167 | + res["key2"] = "value2"; | ||
| 168 | + platform_infos.SetPlatformResWithLock("TestLabel", res); | ||
| 169 | +} | ||
| 170 | + | ||
| 171 | +TEST_F(PlatformInfoExceptionUTest, LoadFromBuffer_NullBuffer_ReturnsTrueInUT) { | ||
| 172 | + PlatFormInfos platform_infos; | ||
| 173 | + platform_infos.Init(); | ||
| 174 | + | ||
| 175 | + | ||
| 176 | + EXPECT_TRUE(platform_infos.LoadFromBuffer(nullptr, 0)); | ||
| 177 | + | ||
| 178 | + EXPECT_FALSE(platform_infos.LoadFromBuffer(nullptr, 0)); | ||
| 179 | + | ||
| 180 | +} | ||
| 181 | + | ||
| 182 | +TEST_F(PlatformInfoExceptionUTest, LoadFromBuffer_ShortBuffer_ReturnsTrueInUT) { | ||
| 183 | + PlatFormInfos platform_infos; | ||
| 184 | + platform_infos.Init(); | ||
| 185 | + | ||
| 186 | + const char *buffer = "short"; | ||
| 187 | + | ||
| 188 | + EXPECT_TRUE(platform_infos.LoadFromBuffer(buffer, strlen(buffer))); | ||
| 189 | + | ||
| 190 | + EXPECT_FALSE(platform_infos.LoadFromBuffer(buffer, strlen(buffer))); | ||
| 191 | + | ||
| 192 | +} | ||
| 193 | + | ||
| 194 | +TEST_F(PlatformInfoExceptionUTest, GetLocalMemSize_L2_Success) { | ||
| 195 | + PlatFormInfos platform_infos; | ||
| 196 | + platform_infos.Init(); | ||
| 197 | + | ||
| 198 | + std::map<std::string, std::string> core_spec; | ||
| 199 | + core_spec["l2_size"] = "1048576"; | ||
| 200 | + platform_infos.SetPlatformRes("AICoreSpec", core_spec); | ||
| 201 | + | ||
| 202 | + uint64_t size = 0; | ||
| 203 | + platform_infos.GetLocalMemSize(LocalMemType::L2, size); | ||
| 204 | +} | ||
| 205 | + | ||
| 206 | +TEST_F(PlatformInfoExceptionUTest, GetLocalMemBw_L1_Success) { | ||
| 207 | + PlatFormInfos platform_infos; | ||
| 208 | + platform_infos.Init(); | ||
| 209 | + | ||
| 210 | + std::map<std::string, std::string> memory_rates; | ||
| 211 | + memory_rates["l1_rate"] = "1.5"; | ||
| 212 | + platform_infos.SetPlatformRes("AICoreMemoryRates", memory_rates); | ||
| 213 | + | ||
| 214 | + uint64_t bw = 0; | ||
| 215 | + platform_infos.GetLocalMemBw(LocalMemType::L1, bw); | ||
| 216 | +} | ||
| 217 | + | ||
| 218 | +TEST_F(PlatformInfoExceptionUTest, GetLocalMemBw_L2_Success) { | ||
| 219 | + PlatFormInfos platform_infos; | ||
| 220 | + platform_infos.Init(); | ||
| 221 | + | ||
| 222 | + std::map<std::string, std::string> memory_rates; | ||
| 223 | + memory_rates["l2_rate"] = "2.0"; | ||
| 224 | + platform_infos.SetPlatformRes("AICoreMemoryRates", memory_rates); | ||
| 225 | + | ||
| 226 | + uint64_t bw = 0; | ||
| 227 | + platform_infos.GetLocalMemBw(LocalMemType::L2, bw); | ||
| 228 | +} | ||
| 229 | + | ||
| 230 | +TEST_F(PlatformInfoExceptionUTest, GetCoreNumByType_AIV_Success) { | ||
| 231 | + PlatFormInfos platform_infos; | ||
| 232 | + platform_infos.Init(); | ||
| 233 | + | ||
| 234 | + std::map<std::string, std::string> soc_info; | ||
| 235 | + soc_info["vector_core_cnt"] = "16"; | ||
| 236 | + platform_infos.SetPlatformRes("SoCInfo", soc_info); | ||
| 237 | + | ||
| 238 | + EXPECT_EQ(platform_infos.GetCoreNumByType("AIV"), 16); | ||
| 239 | +} | ||
| 240 | + | ||
| 241 | +TEST_F(PlatformInfoExceptionUTest, GetCoreNumByType_MixAICORE_ReturnsAICoreCnt) { | ||
| 242 | + PlatFormInfos platform_infos; | ||
| 243 | + platform_infos.Init(); | ||
| 244 | + | ||
| 245 | + std::map<std::string, std::string> soc_info; | ||
| 246 | + soc_info["ai_core_cnt"] = "32"; | ||
| 247 | + platform_infos.SetPlatformRes("SoCInfo", soc_info); | ||
| 248 | + | ||
| 249 | + EXPECT_EQ(platform_infos.GetCoreNumByType("MIX_AICORE"), 32); | ||
| 250 | +} | ||
| 251 | + | ||
| 252 | +TEST_F(PlatformInfoExceptionUTest, ParseVersion_InvalidFormat_ReturnsFalse) { | ||
| 253 | + std::map<std::string, std::string> version_map; | ||
| 254 | + version_map["invalid_key"] = "value"; | ||
| 255 | + std::string soc_version = "Ascend910"; | ||
| 256 | + PlatformInfo info; | ||
| 257 | + PlatformInfoManager::Instance().ParseVersion(version_map, soc_version, info); | ||
| 258 | +} | ||
| 259 | + | ||
| 260 | +TEST_F(PlatformInfoExceptionUTest, ParseAICoreMemoryRates_DDRRate_Success) { | ||
| 261 | + std::map<std::string, std::string> memory_rates; | ||
| 262 | + memory_rates["ddr_rate"] = "3.5"; | ||
| 263 | + memory_rates["ddr_read_rate"] = "2.0"; | ||
| 264 | + memory_rates["ddr_write_rate"] = "1.5"; | ||
| 265 | + PlatformInfo info; | ||
| 266 | + PlatformInfoManager::Instance().ParseBufferOfAICoreMemoryRates(memory_rates, info); | ||
| 267 | + EXPECT_DOUBLE_EQ(info.ai_core_memory_rates.ddr_rate, 3.5); | ||
| 268 | + EXPECT_DOUBLE_EQ(info.ai_core_memory_rates.ddr_read_rate, 2.0); | ||
| 269 | + EXPECT_DOUBLE_EQ(info.ai_core_memory_rates.ddr_write_rate, 1.5); | ||
| 270 | +} | ||
| 271 | + | ||
| 272 | +TEST_F(PlatformInfoExceptionUTest, ParseAICoreMemoryRates_L2Rate_Success) { | ||
| 273 | + std::map<std::string, std::string> memory_rates; | ||
| 274 | + memory_rates["l2_rate"] = "2.8"; | ||
| 275 | + memory_rates["l2_read_rate"] = "1.4"; | ||
| 276 | + memory_rates["l2_write_rate"] = "1.4"; | ||
| 277 | + PlatformInfo info; | ||
| 278 | + PlatformInfoManager::Instance().ParseBufferOfAICoreMemoryRates(memory_rates, info); | ||
| 279 | + EXPECT_DOUBLE_EQ(info.ai_core_memory_rates.l2_rate, 2.8); | ||
| 280 | + EXPECT_DOUBLE_EQ(info.ai_core_memory_rates.l2_read_rate, 1.4); | ||
| 281 | + EXPECT_DOUBLE_EQ(info.ai_core_memory_rates.l2_write_rate, 1.4); | ||
| 282 | +} | ||
| 283 | + | ||
| 284 | +TEST_F(PlatformInfoExceptionUTest, ParseSoftwareSpec_Success) { | ||
| 285 | + std::map<std::string, std::string> software_spec; | ||
| 286 | + software_spec["cubebit_num"] = "128"; | ||
| 287 | + software_spec["line_num"] = "64"; | ||
| 288 | + PlatformInfo info; | ||
| 289 | + PlatformInfoManager::Instance().ParseSoftwareSpec(software_spec, info); | ||
| 290 | +} | ||
| 291 | + | ||
| 292 | +TEST_F(PlatformInfoExceptionUTest, ParseCPUCache_Success) { | ||
| 293 | + std::map<std::string, std::string> cpu_cache; | ||
| 294 | + cpu_cache["l1_cache_size"] = "65536"; | ||
| 295 | + cpu_cache["l2_cache_size"] = "131072"; | ||
| 296 | + PlatformInfo info; | ||
| 297 | + PlatformInfoManager::Instance().ParseCPUCache(cpu_cache, info); | ||
| 298 | +} | ||
| 299 | + | ||
| 300 | +TEST_F(PlatformInfoExceptionUTest, ParseVectorCoreSpec_Success) { | ||
| 301 | + std::map<std::string, std::string> vector_core_spec; | ||
| 302 | + vector_core_spec["vector_core_cnt"] = "16"; | ||
| 303 | + PlatformInfo info; | ||
| 304 | + PlatformInfoManager::Instance().ParseVectorCoreSpec(vector_core_spec, info); | ||
| 305 | +} | ||
| 306 | + | ||
| 307 | +TEST_F(PlatformInfoExceptionUTest, ParseVectorCoreMemoryRates_Success) { | ||
| 308 | + std::map<std::string, std::string> vector_memory_rates; | ||
| 309 | + vector_memory_rates["ddr_rate"] = "2.5"; | ||
| 310 | + vector_memory_rates["l2_rate"] = "1.8"; | ||
| 311 | + vector_memory_rates["ub_to_l2_rate"] = "3.0"; | ||
| 312 | + PlatformInfo info; | ||
| 313 | + PlatformInfoManager::Instance().ParseVectorCoreMemoryRates(vector_memory_rates, info); | ||
| 314 | +} | ||
| 315 | + | ||
| 316 | +TEST_F(PlatformInfoExceptionUTest, UpdatePlatformInfos_Success) { | ||
| 317 | + PlatFormInfos platform_infos; | ||
| 318 | + platform_infos.Init(); | ||
| 319 | + platform_infos.core_num_ = 8; | ||
| 320 | + | ||
| 321 | + PlatformInfoManager::Instance().UpdatePlatformInfos(platform_infos); | ||
| 322 | +} | ||
| 323 | + | ||
| 324 | +TEST_F(PlatformInfoExceptionUTest, UpdatePlatformInfos_WithSocVersion_Success) { | ||
| 325 | + PlatFormInfos platform_infos; | ||
| 326 | + platform_infos.Init(); | ||
| 327 | + | ||
| 328 | + PlatformInfoManager::Instance().UpdatePlatformInfos("Ascend910B", platform_infos); | ||
| 329 | +} | ||
| 330 | + | ||
| 331 | +TEST_F(PlatformInfoExceptionUTest, InitRuntimePlatformInfos_Success) { | ||
| 332 | + PlatformInfoManager::Instance().InitRuntimePlatformInfos("Ascend910B"); | ||
| 333 | +} | ||
| 334 | + | ||
| 335 | +TEST_F(PlatformInfoExceptionUTest, GetRuntimePlatformInfosByDevice_Success) { | ||
| 336 | + PlatFormInfos platform_infos; | ||
| 337 | + EXPECT_EQ(PlatformInfoManager::Instance().GetRuntimePlatformInfosByDevice(0, platform_infos, false), 0); | ||
| 338 | +} | ||
| 339 | + | ||
| 340 | +TEST_F(PlatformInfoExceptionUTest, UpdateRuntimePlatformInfosByDevice_Success) { | ||
| 341 | + PlatFormInfos platform_infos; | ||
| 342 | + platform_infos.Init(); | ||
| 343 | + PlatformInfoManager::Instance().UpdateRuntimePlatformInfosByDevice(0, platform_infos); | ||
| 344 | +} | ||
| 345 | + | ||
| 346 | +TEST_F(PlatformInfoExceptionUTest, GetPlatformInstanceByDevice_Success) { | ||
| 347 | + OptionalInfos opti_infos; | ||
| 348 | + opti_infos.Init(); | ||
| 349 | + opti_infos.SetSocVersion("Ascend910B1"); | ||
| 350 | + PlatformInfoManager::Instance().SetOptionalCompilationInfo(opti_infos); | ||
| 351 | + | ||
| 352 | + PlatFormInfos platform_infos; | ||
| 353 | + EXPECT_EQ(PlatformInfoManager::Instance().GetPlatformInstanceByDevice(0, platform_infos), 0); | ||
| 354 | +} | ||
| 355 | + | ||
| 356 | +TEST_F(PlatformInfoExceptionUTest, GetPlatformInstanceByDevice_EmptySocVersion_Failed) { | ||
| 357 | + OptionalInfos opti_infos; | ||
| 358 | + opti_infos.Init(); | ||
| 359 | + PlatformInfoManager::Instance().SetOptionalCompilationInfo(opti_infos); | ||
| 360 | + | ||
| 361 | + PlatFormInfos platform_infos; | ||
| 362 | + EXPECT_EQ(PlatformInfoManager::Instance().GetPlatformInstanceByDevice(0, platform_infos), 0xFFFFFFFF); | ||
| 363 | +} | ||
| 364 | + | ||
| 365 | +// 新增测试:覆盖 CalculateFraction 更多边界情况 | ||
| 366 | +TEST_F(PlatformInfoExceptionUTest, CalculateFraction_NegativeDenominator_Success) { | ||
| 367 | + std::map<std::string, std::string> memory_rates; | ||
| 368 | + memory_rates["ub_to_l2_rate"] = "10/-2"; | ||
| 369 | + PlatformInfo info; | ||
| 370 | + PlatformInfoManager::Instance().ParseUBOfAICoreMemoryRates(memory_rates, info); | ||
| 371 | +} | ||
| 372 | + | ||
| 373 | +TEST_F(PlatformInfoExceptionUTest, CalculateFraction_NegativeNumerator_Success) { | ||
| 374 | + std::map<std::string, std::string> memory_rates; | ||
| 375 | + memory_rates["ub_to_l2_rate"] = "-10/2"; | ||
| 376 | + PlatformInfo info; | ||
| 377 | + PlatformInfoManager::Instance().ParseUBOfAICoreMemoryRates(memory_rates, info); | ||
| 378 | +} | ||
| 379 | + | ||
| 380 | +TEST_F(PlatformInfoExceptionUTest, CalculateFraction_BothNegative_Success) { | ||
| 381 | + std::map<std::string, std::string> memory_rates; | ||
| 382 | + memory_rates["ub_to_l2_rate"] = "-10/-2"; | ||
| 383 | + PlatformInfo info; | ||
| 384 | + PlatformInfoManager::Instance().ParseUBOfAICoreMemoryRates(memory_rates, info); | ||
| 385 | +} | ||
| 386 | + | ||
| 387 | +TEST_F(PlatformInfoExceptionUTest, CalculateFraction_VerySmallDenominator_ReturnsZero) { | ||
| 388 | + std::map<std::string, std::string> memory_rates; | ||
| 389 | + memory_rates["ub_to_l2_rate"] = "10/1e-15"; | ||
| 390 | + PlatformInfo info; | ||
| 391 | + PlatformInfoManager::Instance().ParseUBOfAICoreMemoryRates(memory_rates, info); | ||
| 392 | +} | ||
| 393 | + | ||
| 394 | +TEST_F(PlatformInfoExceptionUTest, CalculateFraction_SpaceAroundDenominator_Success) { | ||
| 395 | + std::map<std::string, std::string> memory_rates; | ||
| 396 | + memory_rates["ub_to_l2_rate"] = "10/ 2 "; | ||
| 397 | + PlatformInfo info; | ||
| 398 | + PlatformInfoManager::Instance().ParseUBOfAICoreMemoryRates(memory_rates, info); | ||
| 399 | +} | ||
| 400 | + | ||
| 401 | +// 新增测试:覆盖 Parse 函数的异常分支 | ||
| 402 | +TEST_F(PlatformInfoExceptionUTest, ParseSocInfo_AllFields_Success) { | ||
| 403 | + std::map<std::string, std::string> soc_info; | ||
| 404 | + soc_info["ai_core_cnt"] = "32"; | ||
| 405 | + soc_info["vector_core_cnt"] = "16"; | ||
| 406 | + soc_info["ai_cpu_cnt"] = "8"; | ||
| 407 | + soc_info["memory_type"] = "0"; | ||
| 408 | + soc_info["memory_size"] = "1048576"; | ||
| 409 | + soc_info["l2_type"] = "0"; | ||
| 410 | + soc_info["l2_size"] = "524288"; | ||
| 411 | + soc_info["l2_page_num"] = "1024"; | ||
| 412 | + soc_info["task_num"] = "64"; | ||
| 413 | + soc_info["arch_type"] = "2201"; | ||
| 414 | + soc_info["chip_type"] = "10"; | ||
| 415 | + PlatformInfo info; | ||
| 416 | + PlatformInfoManager::Instance().ParseSocInfo(soc_info, info); | ||
| 417 | +} | ||
| 418 | + | ||
| 419 | +TEST_F(PlatformInfoExceptionUTest, ParseAICoreSpec_AllFields_Success) { | ||
| 420 | + std::map<std::string, std::string> core_spec; | ||
| 421 | + core_spec["cube_freq"] = "1.5"; | ||
| 422 | + core_spec["cube_m_size"] = "16"; | ||
| 423 | + core_spec["cube_n_size"] = "16"; | ||
| 424 | + core_spec["cube_k_size"] = "16"; | ||
| 425 | + core_spec["vec_calc_size"] = "256"; | ||
| 426 | + core_spec["l0_a_size"] = "1024"; | ||
| 427 | + core_spec["l0_b_size"] = "1024"; | ||
| 428 | + core_spec["l0_c_size"] = "1024"; | ||
| 429 | + core_spec["l1_size"] = "4096"; | ||
| 430 | + core_spec["smask_buffer"] = "512"; | ||
| 431 | + PlatformInfo info; | ||
| 432 | + PlatformInfoManager::Instance().ParseAICoreSpec(core_spec, info); | ||
| 433 | +} | ||
| 434 | + | ||
| 435 | +TEST_F(PlatformInfoExceptionUTest, ParseUBOfAICoreSpec_AllFields_Success) { | ||
| 436 | + std::map<std::string, std::string> ub_spec; | ||
| 437 | + ub_spec["ub_size"] = "65536"; | ||
| 438 | + ub_spec["ubblock_size"] = "4096"; | ||
| 439 | + ub_spec["ubbank_size"] = "256"; | ||
| 440 | + ub_spec["ubbank_num"] = "32"; | ||
| 441 | + ub_spec["ubburst_in_one_block"] = "8"; | ||
| 442 | + ub_spec["ubbank_group_num"] = "4"; | ||
| 443 | + PlatformInfo info; | ||
| 444 | + PlatformInfoManager::Instance().ParseUBOfAICoreSpec(ub_spec, info); | ||
| 445 | +} | ||
| 446 | + | ||
| 447 | +TEST_F(PlatformInfoExceptionUTest, ParseVectorCoreSpec_AllFields_Success) { | ||
| 448 | + std::map<std::string, std::string> vector_spec; | ||
| 449 | + vector_spec["vec_freq"] = "2.0"; | ||
| 450 | + vector_spec["vec_calc_size"] = "512"; | ||
| 451 | + vector_spec["smask_buffer"] = "1024"; | ||
| 452 | + vector_spec["ub_size"] = "32768"; | ||
| 453 | + vector_spec["ubblock_size"] = "2048"; | ||
| 454 | + vector_spec["ubbank_size"] = "128"; | ||
| 455 | + vector_spec["ubbank_num"] = "16"; | ||
| 456 | + vector_spec["ubburst_in_one_block"] = "4"; | ||
| 457 | + vector_spec["ubbank_group_num"] = "2"; | ||
| 458 | + vector_spec["vector_reg_size"] = "64"; | ||
| 459 | + vector_spec["predicate_reg_size"] = "32"; | ||
| 460 | + vector_spec["address_reg_size"] = "16"; | ||
| 461 | + vector_spec["alignment_reg_size"] = "8"; | ||
| 462 | + PlatformInfo info; | ||
| 463 | + PlatformInfoManager::Instance().ParseVectorCoreSpec(vector_spec, info); | ||
| 464 | +} | ||
| 465 | + | ||
| 466 | +// 新增测试:覆盖 EnsureSocVersionLoaded 异常分支 | ||
| 467 | +TEST_F(PlatformInfoExceptionUTest, EnsureSocVersionLoaded_Success) { | ||
| 468 | + std::string soc_version = "Ascend910B1"; | ||
| 469 | + EXPECT_EQ(PlatformInfoManager::Instance().EnsureSocVersionLoaded(soc_version), 0); | ||
| 470 | +} | ||
| 471 | + | ||
| 472 | +// 新增测试:覆盖 ParseUnzipOfAICoreSpec 超范围分支 | ||
| 473 | +TEST_F(PlatformInfoExceptionUTest, ParseUnzipOfAICoreSpec_ExceedsMaxRatios_Returns) { | ||
| 474 | + std::map<std::string, std::string> core_spec; | ||
| 475 | + core_spec["unzip_max_ratios"] = "4294967296"; // UINT32_MAX + 1 | ||
| 476 | + PlatformInfo info; | ||
| 477 | + PlatformInfoManager::Instance().ParseUnzipOfAICoreSpec(core_spec, info); | ||
| 478 | +} | ||
| 479 | + | ||
| 480 | +TEST_F(PlatformInfoExceptionUTest, ParseUnzipOfAICoreSpec_ExceedsChannels_Returns) { | ||
| 481 | + std::map<std::string, std::string> core_spec; | ||
| 482 | + core_spec["unzip_channels"] = "4294967296"; | ||
| 483 | + PlatformInfo info; | ||
| 484 | + PlatformInfoManager::Instance().ParseUnzipOfAICoreSpec(core_spec, info); | ||
| 485 | +} | ||
| 486 | + | ||
| 487 | +TEST_F(PlatformInfoExceptionUTest, ParseUnzipOfAICoreSpec_ExceedsIsTight_Returns) { | ||
| 488 | + std::map<std::string, std::string> core_spec; | ||
| 489 | + core_spec["unzip_is_tight"] = "256"; // UINT8_MAX + 1 | ||
| 490 | + PlatformInfo info; | ||
| 491 | + PlatformInfoManager::Instance().ParseUnzipOfAICoreSpec(core_spec, info); | ||
| 492 | +} | ||
| 493 | + | ||
| 494 | +TEST_F(PlatformInfoExceptionUTest, ParseUnzipOfAICoreSpec_OutOfRangeException) { | ||
| 495 | + std::map<std::string, std::string> core_spec; | ||
| 496 | + core_spec["unzip_engines"] = "999999999999999999999999"; // 超大值触发out_of_range | ||
| 497 | + PlatformInfo info; | ||
| 498 | + PlatformInfoManager::Instance().ParseUnzipOfAICoreSpec(core_spec, info); | ||
| 499 | +} | ||
| 500 | + | ||
| 501 | +// 新增测试:覆盖 CalculateFraction 异常分支 | ||
| 502 | +TEST_F(PlatformInfoExceptionUTest, CalculateFraction_InvalidInputFormat) { | ||
| 503 | + std::map<std::string, std::string> memory_rates; | ||
| 504 | + memory_rates["ub_to_l2_rate"] = "not_a_number/2"; | ||
| 505 | + PlatformInfo info; | ||
| 506 | + PlatformInfoManager::Instance().ParseUBOfAICoreMemoryRates(memory_rates, info); | ||
| 507 | +} | ||
| 508 | + | ||
| 509 | +TEST_F(PlatformInfoExceptionUTest, CalculateFraction_ExceptionInParse) { | ||
| 510 | + std::map<std::string, std::string> memory_rates; | ||
| 511 | + memory_rates["ub_to_l2_rate"] = "abc"; | ||
| 512 | + PlatformInfo info; | ||
| 513 | + PlatformInfoManager::Instance().ParseUBOfAICoreMemoryRates(memory_rates, info); | ||
| 514 | +} | ||
| 515 | + | ||
| 516 | +// 新增测试:覆盖 ParseBufferOfAICoreMemoryRates 异常分支 | ||
| 517 | +TEST_F(PlatformInfoExceptionUTest, ParseBufferOfAICoreMemoryRates_InvalidValue) { | ||
| 518 | + std::map<std::string, std::string> memory_rates; | ||
| 519 | + memory_rates["ddr_rate"] = "invalid_string"; | ||
| 520 | + PlatformInfo info; | ||
| 521 | + PlatformInfoManager::Instance().ParseBufferOfAICoreMemoryRates(memory_rates, info); | ||
| 522 | +} | ||
| 523 | + | ||
| 524 | +// 新增测试:覆盖 ParseUBOfAICoreMemoryRates 异常分支 | ||
| 525 | +TEST_F(PlatformInfoExceptionUTest, ParseUBOfAICoreMemoryRates_InvalidUbToL1) { | ||
| 526 | + std::map<std::string, std::string> memory_rates; | ||
| 527 | + memory_rates["ub_to_l1_rate"] = "invalid_value"; | ||
| 528 | + PlatformInfo info; | ||
| 529 | + PlatformInfoManager::Instance().ParseUBOfAICoreMemoryRates(memory_rates, info); | ||
| 530 | +} | ||
| 531 | + | ||
| 532 | +// 新增测试:覆盖其他Parse函数的异常分支 | ||
| 533 | +TEST_F(PlatformInfoExceptionUTest, ParseCubeOfAICoreSpec_ExceedsRange) { | ||
| 534 | + std::map<std::string, std::string> core_spec; | ||
| 535 | + core_spec["cube_m_size"] = "18446744073709551616"; | ||
| 536 | + PlatformInfo info; | ||
| 537 | + PlatformInfoManager::Instance().ParseCubeOfAICoreSpec(core_spec, info); | ||
| 538 | +} | ||
| 539 | + | ||
| 540 | +TEST_F(PlatformInfoExceptionUTest, ParseBufferOfAICoreSpec_ExceedsRange) { | ||
| 541 | + std::map<std::string, std::string> core_spec; | ||
| 542 | + core_spec["buffer_size"] = "4294967296"; | ||
| 543 | + PlatformInfo info; | ||
| 544 | + PlatformInfoManager::Instance().ParseBufferOfAICoreSpec(core_spec, info); | ||
| 545 | +} | ||
| 546 | + | ||
| 547 | +TEST_F(PlatformInfoExceptionUTest, ParseUBOfAICoreSpec_ExceedsRange) { | ||
| 548 | + std::map<std::string, std::string> ub_spec; | ||
| 549 | + ub_spec["ub_size"] = "18446744073709551616"; | ||
| 550 | + PlatformInfo info; | ||
| 551 | + PlatformInfoManager::Instance().ParseUBOfAICoreSpec(ub_spec, info); | ||
| 552 | +} | ||
| 553 | + | ||
| 554 | +TEST_F(PlatformInfoExceptionUTest, ParseVectorCoreMemoryRates_Exception) { | ||
| 555 | + std::map<std::string, std::string> memory_rates; | ||
| 556 | + memory_rates["ddr_rate"] = "invalid"; | ||
| 557 | + PlatformInfo info; | ||
| 558 | + PlatformInfoManager::Instance().ParseVectorCoreMemoryRates(memory_rates, info); | ||
| 559 | +} | ||
| 560 | + | ||
| 561 | +} | ||
| @@ -0,0 +1,574 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2025 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | + | ||
| 12 | + | ||
| 13 | + | ||
| 14 | + | ||
| 15 | + | ||
| 16 | + | ||
| 17 | + | ||
| 18 | + | ||
| 19 | + | ||
| 20 | + | ||
| 21 | +namespace fe { | ||
| 22 | + | ||
| 23 | +class PlatFormInfosDefUTest : public testing::Test { | ||
| 24 | +protected: | ||
| 25 | + void SetUp() { | ||
| 26 | + platform_infos = new PlatFormInfos(); | ||
| 27 | + } | ||
| 28 | + | ||
| 29 | + void TearDown() { | ||
| 30 | + delete platform_infos; | ||
| 31 | + platform_infos = nullptr; | ||
| 32 | + GlobalMockObject::verify(); | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + PlatFormInfos *platform_infos; | ||
| 36 | +}; | ||
| 37 | + | ||
| 38 | +TEST_F(PlatFormInfosDefUTest, Init_NullptrCheck_Success) { | ||
| 39 | + EXPECT_TRUE(platform_infos->Init()); | ||
| 40 | + EXPECT_NE(platform_infos->platform_infos_impl_, nullptr); | ||
| 41 | +} | ||
| 42 | + | ||
| 43 | +TEST_F(PlatFormInfosDefUTest, GetAICoreIntrinsicDtype_NotInit_ReturnsEmpty) { | ||
| 44 | + PlatFormInfos uninitialized; | ||
| 45 | + auto result = uninitialized.GetAICoreIntrinsicDtype(); | ||
| 46 | + EXPECT_TRUE(result.empty()); | ||
| 47 | +} | ||
| 48 | + | ||
| 49 | +TEST_F(PlatFormInfosDefUTest, GetVectorCoreIntrinsicDtype_NotInit_ReturnsEmpty) { | ||
| 50 | + PlatFormInfos uninitialized; | ||
| 51 | + auto result = uninitialized.GetVectorCoreIntrinsicDtype(); | ||
| 52 | + EXPECT_TRUE(result.empty()); | ||
| 53 | +} | ||
| 54 | + | ||
| 55 | +TEST_F(PlatFormInfosDefUTest, GetPlatformRes_StringString_NotInit_ReturnsTrue) { | ||
| 56 | + PlatFormInfos uninitialized; | ||
| 57 | + std::string val; | ||
| 58 | + EXPECT_TRUE(uninitialized.GetPlatformRes("label", "key", val)); | ||
| 59 | +} | ||
| 60 | + | ||
| 61 | +TEST_F(PlatFormInfosDefUTest, GetPlatformResWithLock_StringString_NotInit_ReturnsTrue) { | ||
| 62 | + PlatFormInfos uninitialized; | ||
| 63 | + std::string val; | ||
| 64 | + EXPECT_TRUE(uninitialized.GetPlatformResWithLock("label", "key", val)); | ||
| 65 | +} | ||
| 66 | + | ||
| 67 | +TEST_F(PlatFormInfosDefUTest, GetPlatformRes_LabelMap_NotInit_ReturnsTrue) { | ||
| 68 | + PlatFormInfos uninitialized; | ||
| 69 | + std::map<std::string, std::string> res; | ||
| 70 | + EXPECT_TRUE(uninitialized.GetPlatformRes("label", res)); | ||
| 71 | +} | ||
| 72 | + | ||
| 73 | +TEST_F(PlatFormInfosDefUTest, GetPlatformResWithLock_LabelMap_NotInit_ReturnsTrue) { | ||
| 74 | + PlatFormInfos uninitialized; | ||
| 75 | + std::map<std::string, std::string> res; | ||
| 76 | + EXPECT_TRUE(uninitialized.GetPlatformResWithLock("label", res)); | ||
| 77 | +} | ||
| 78 | + | ||
| 79 | +TEST_F(PlatFormInfosDefUTest, GetPlatformResWithLock_MapMap_NotInit_ReturnsTrue) { | ||
| 80 | + PlatFormInfos uninitialized; | ||
| 81 | + std::map<std::string, std::map<std::string, std::string>> res; | ||
| 82 | + EXPECT_TRUE(uninitialized.GetPlatformResWithLock(res)); | ||
| 83 | +} | ||
| 84 | + | ||
| 85 | +TEST_F(PlatFormInfosDefUTest, SetAICoreIntrinsicDtype_NotInit_NoEffect) { | ||
| 86 | + PlatFormInfos uninitialized; | ||
| 87 | + std::map<std::string, std::vector<std::string>> dtypes; | ||
| 88 | + dtypes["test"] = {"float", "int"}; | ||
| 89 | + uninitialized.SetAICoreIntrinsicDtype(dtypes); | ||
| 90 | +} | ||
| 91 | + | ||
| 92 | +TEST_F(PlatFormInfosDefUTest, SetVectorCoreIntrinsicDtype_NotInit_NoEffect) { | ||
| 93 | + PlatFormInfos uninitialized; | ||
| 94 | + std::map<std::string, std::vector<std::string>> dtypes; | ||
| 95 | + dtypes["test"] = {"float", "int"}; | ||
| 96 | + uninitialized.SetVectorCoreIntrinsicDtype(dtypes); | ||
| 97 | +} | ||
| 98 | + | ||
| 99 | +TEST_F(PlatFormInfosDefUTest, SetFixPipeDtypeMap_NotInit_NoEffect) { | ||
| 100 | + PlatFormInfos uninitialized; | ||
| 101 | + std::map<std::string, std::vector<std::string>> dtype_map; | ||
| 102 | + dtype_map["pipe1"] = {"fp16"}; | ||
| 103 | + uninitialized.SetFixPipeDtypeMap(dtype_map); | ||
| 104 | +} | ||
| 105 | + | ||
| 106 | +TEST_F(PlatFormInfosDefUTest, SetCoreNum_Success) { | ||
| 107 | + EXPECT_TRUE(platform_infos->Init()); | ||
| 108 | + uint32_t core_num = 8; | ||
| 109 | + platform_infos->SetCoreNum(core_num); | ||
| 110 | + EXPECT_EQ(platform_infos->core_num_, core_num); | ||
| 111 | +} | ||
| 112 | + | ||
| 113 | +TEST_F(PlatFormInfosDefUTest, GetCoreNum_Success) { | ||
| 114 | + EXPECT_TRUE(platform_infos->Init()); | ||
| 115 | + platform_infos->core_num_ = 8; | ||
| 116 | + EXPECT_EQ(platform_infos->GetCoreNum(), 8); | ||
| 117 | +} | ||
| 118 | + | ||
| 119 | +TEST_F(PlatFormInfosDefUTest, GetCoreNumWithLock_Success) { | ||
| 120 | + EXPECT_TRUE(platform_infos->Init()); | ||
| 121 | + platform_infos->core_num_ = 16; | ||
| 122 | + EXPECT_EQ(platform_infos->GetCoreNumWithLock(), 16); | ||
| 123 | +} | ||
| 124 | + | ||
| 125 | +TEST_F(PlatFormInfosDefUTest, GetCoreNumByType_VectorCore_ReturnsCorrect) { | ||
| 126 | + EXPECT_TRUE(platform_infos->Init()); | ||
| 127 | + std::map<std::string, std::string> soc_info; | ||
| 128 | + soc_info["vector_core_cnt"] = "16"; | ||
| 129 | + platform_infos->SetPlatformRes("SoCInfo", soc_info); | ||
| 130 | + EXPECT_EQ(platform_infos->GetCoreNumByType("VectorCore"), 16); | ||
| 131 | +} | ||
| 132 | + | ||
| 133 | +TEST_F(PlatFormInfosDefUTest, GetCoreNumByType_AIV_ReturnsCorrect) { | ||
| 134 | + EXPECT_TRUE(platform_infos->Init()); | ||
| 135 | + std::map<std::string, std::string> soc_info; | ||
| 136 | + soc_info["vector_core_cnt"] = "16"; | ||
| 137 | + platform_infos->SetPlatformRes("SoCInfo", soc_info); | ||
| 138 | + EXPECT_EQ(platform_infos->GetCoreNumByType("AIV"), 16); | ||
| 139 | +} | ||
| 140 | + | ||
| 141 | +TEST_F(PlatFormInfosDefUTest, GetCoreNumByType_AICore_ReturnsCorrect) { | ||
| 142 | + EXPECT_TRUE(platform_infos->Init()); | ||
| 143 | + std::map<std::string, std::string> soc_info; | ||
| 144 | + soc_info["ai_core_cnt"] = "32"; | ||
| 145 | + platform_infos->SetPlatformRes("SoCInfo", soc_info); | ||
| 146 | + EXPECT_EQ(platform_infos->GetCoreNumByType("AICore"), 32); | ||
| 147 | +} | ||
| 148 | + | ||
| 149 | +TEST_F(PlatFormInfosDefUTest, GetCoreNumByType_OtherType_ReturnsAICoreCnt) { | ||
| 150 | + EXPECT_TRUE(platform_infos->Init()); | ||
| 151 | + std::map<std::string, std::string> soc_info; | ||
| 152 | + soc_info["ai_core_cnt"] = "32"; | ||
| 153 | + platform_infos->SetPlatformRes("SoCInfo", soc_info); | ||
| 154 | + EXPECT_EQ(platform_infos->GetCoreNumByType("MIX_AIV"), 32); | ||
| 155 | +} | ||
| 156 | + | ||
| 157 | +TEST_F(PlatFormInfosDefUTest, GetLocalMemSize_L0_A_Success) { | ||
| 158 | + EXPECT_TRUE(platform_infos->Init()); | ||
| 159 | + std::map<std::string, std::string> core_spec; | ||
| 160 | + core_spec["l0_a_size"] = "65536"; | ||
| 161 | + platform_infos->SetPlatformRes("AICoreSpec", core_spec); | ||
| 162 | + uint64_t size = 0; | ||
| 163 | + platform_infos->GetLocalMemSize(LocalMemType::L0_A, size); | ||
| 164 | +} | ||
| 165 | + | ||
| 166 | +TEST_F(PlatFormInfosDefUTest, GetLocalMemSize_L1_Success) { | ||
| 167 | + EXPECT_TRUE(platform_infos->Init()); | ||
| 168 | + std::map<std::string, std::string> core_spec; | ||
| 169 | + core_spec["l1_size"] = "131072"; | ||
| 170 | + platform_infos->SetPlatformRes("AICoreSpec", core_spec); | ||
| 171 | + uint64_t size = 0; | ||
| 172 | + platform_infos->GetLocalMemSize(LocalMemType::L1, size); | ||
| 173 | +} | ||
| 174 | + | ||
| 175 | +TEST_F(PlatFormInfosDefUTest, GetLocalMemSize_L2_Success) { | ||
| 176 | + EXPECT_TRUE(platform_infos->Init()); | ||
| 177 | + std::map<std::string, std::string> core_spec; | ||
| 178 | + core_spec["l2_size"] = "1048576"; | ||
| 179 | + platform_infos->SetPlatformRes("AICoreSpec", core_spec); | ||
| 180 | + uint64_t size = 0; | ||
| 181 | + platform_infos->GetLocalMemSize(LocalMemType::L2, size); | ||
| 182 | +} | ||
| 183 | + | ||
| 184 | +TEST_F(PlatFormInfosDefUTest, GetLocalMemSize_UB_Success) { | ||
| 185 | + EXPECT_TRUE(platform_infos->Init()); | ||
| 186 | + std::map<std::string, std::string> core_spec; | ||
| 187 | + core_spec["ub_size"] = "65536"; | ||
| 188 | + platform_infos->SetPlatformRes("AICoreSpec", core_spec); | ||
| 189 | + uint64_t size = 0; | ||
| 190 | + platform_infos->GetLocalMemSize(LocalMemType::UB, size); | ||
| 191 | +} | ||
| 192 | + | ||
| 193 | +TEST_F(PlatFormInfosDefUTest, GetLocalMemBw_L0_A_Success) { | ||
| 194 | + EXPECT_TRUE(platform_infos->Init()); | ||
| 195 | + std::map<std::string, std::string> memory_rates; | ||
| 196 | + memory_rates["l0_a_rate"] = "2.5"; | ||
| 197 | + platform_infos->SetPlatformRes("AICoreMemoryRates", memory_rates); | ||
| 198 | + uint64_t bw = 0; | ||
| 199 | + platform_infos->GetLocalMemBw(LocalMemType::L0_A, bw); | ||
| 200 | +} | ||
| 201 | + | ||
| 202 | +TEST_F(PlatFormInfosDefUTest, GetLocalMemBw_L1_Success) { | ||
| 203 | + EXPECT_TRUE(platform_infos->Init()); | ||
| 204 | + std::map<std::string, std::string> memory_rates; | ||
| 205 | + memory_rates["l1_to_l0_a_rate"] = "1.5"; | ||
| 206 | + platform_infos->SetPlatformRes("AICoreMemoryRates", memory_rates); | ||
| 207 | + uint64_t bw = 0; | ||
| 208 | + platform_infos->GetLocalMemBw(LocalMemType::L1, bw); | ||
| 209 | +} | ||
| 210 | + | ||
| 211 | +TEST_F(PlatFormInfosDefUTest, GetLocalMemBw_L2_Success) { | ||
| 212 | + EXPECT_TRUE(platform_infos->Init()); | ||
| 213 | + std::map<std::string, std::string> memory_rates; | ||
| 214 | + memory_rates["l2_rate"] = "2.0"; | ||
| 215 | + platform_infos->SetPlatformRes("AICoreMemoryRates", memory_rates); | ||
| 216 | + uint64_t bw = 0; | ||
| 217 | + platform_infos->GetLocalMemBw(LocalMemType::L2, bw); | ||
| 218 | +} | ||
| 219 | + | ||
| 220 | +TEST_F(PlatFormInfosDefUTest, GetLocalMemBw_HBM_Success) { | ||
| 221 | + EXPECT_TRUE(platform_infos->Init()); | ||
| 222 | + std::map<std::string, std::string> memory_rates; | ||
| 223 | + memory_rates["ddr_rate"] = "3.0"; | ||
| 224 | + platform_infos->SetPlatformRes("AICoreMemoryRates", memory_rates); | ||
| 225 | + uint64_t bw = 0; | ||
| 226 | + platform_infos->GetLocalMemBw(LocalMemType::HBM, bw); | ||
| 227 | +} | ||
| 228 | + | ||
| 229 | +TEST_F(PlatFormInfosDefUTest, GetFixPipeDtypeMap_Success) { | ||
| 230 | + EXPECT_TRUE(platform_infos->Init()); | ||
| 231 | + std::map<std::string, std::vector<std::string>> dtype_map; | ||
| 232 | + dtype_map["pipe1"] = {"fp16", "int32"}; | ||
| 233 | + platform_infos->SetFixPipeDtypeMap(dtype_map); | ||
| 234 | + auto result = platform_infos->GetFixPipeDtypeMap(); | ||
| 235 | + EXPECT_EQ(result.size(), 1); | ||
| 236 | + EXPECT_EQ(result["pipe1"].size(), 2); | ||
| 237 | +} | ||
| 238 | + | ||
| 239 | +TEST_F(PlatFormInfosDefUTest, SetPlatformRes_Success) { | ||
| 240 | + EXPECT_TRUE(platform_infos->Init()); | ||
| 241 | + std::map<std::string, std::string> res; | ||
| 242 | + res["key1"] = "value1"; | ||
| 243 | + res["key2"] = "value2"; | ||
| 244 | + platform_infos->SetPlatformRes("label", res); | ||
| 245 | +} | ||
| 246 | + | ||
| 247 | +TEST_F(PlatFormInfosDefUTest, SetPlatformResWithLock_Success) { | ||
| 248 | + EXPECT_TRUE(platform_infos->Init()); | ||
| 249 | + std::map<std::string, std::string> res; | ||
| 250 | + res["key1"] = "value1"; | ||
| 251 | + platform_infos->SetPlatformResWithLock("label", res); | ||
| 252 | +} | ||
| 253 | + | ||
| 254 | +TEST_F(PlatFormInfosDefUTest, InitByInstance_Success) { | ||
| 255 | + EXPECT_TRUE(platform_infos->Init()); | ||
| 256 | + PlatFormInfos other; | ||
| 257 | + EXPECT_TRUE(other.Init()); | ||
| 258 | + other.core_num_ = 8; | ||
| 259 | + platform_infos->InitByInstance(); | ||
| 260 | +} | ||
| 261 | + | ||
| 262 | +TEST_F(PlatFormInfosDefUTest, SaveToBuffer_Success) { | ||
| 263 | + EXPECT_TRUE(platform_infos->Init()); | ||
| 264 | + platform_infos->core_num_ = 8; | ||
| 265 | + std::map<std::string, std::vector<std::string>> dtypes; | ||
| 266 | + dtypes["test"] = {"float"}; | ||
| 267 | + platform_infos->SetAICoreIntrinsicDtype(dtypes); | ||
| 268 | + | ||
| 269 | + | ||
| 270 | + std::string buffer = platform_infos->SaveToBuffer(); | ||
| 271 | + EXPECT_TRUE(buffer.empty()); | ||
| 272 | + | ||
| 273 | + std::string buffer = platform_infos->SaveToBuffer(); | ||
| 274 | + EXPECT_FALSE(buffer.empty()); | ||
| 275 | + | ||
| 276 | +} | ||
| 277 | + | ||
| 278 | +TEST_F(PlatFormInfosDefUTest, LoadFromBuffer_Success) { | ||
| 279 | + PlatFormInfos loaded; | ||
| 280 | + EXPECT_TRUE(loaded.Init()); | ||
| 281 | + | ||
| 282 | + | ||
| 283 | + EXPECT_TRUE(loaded.LoadFromBuffer(nullptr, 0)); | ||
| 284 | + | ||
| 285 | + std::string buffer = "valid_buffer_data"; | ||
| 286 | + EXPECT_TRUE(loaded.LoadFromBuffer(buffer.c_str(), buffer.length())); | ||
| 287 | + | ||
| 288 | +} | ||
| 289 | + | ||
| 290 | +TEST_F(PlatFormInfosDefUTest, SetCoreNumByCoreType_AICore_Success) { | ||
| 291 | + EXPECT_TRUE(platform_infos->Init()); | ||
| 292 | + std::map<std::string, std::string> soc_info; | ||
| 293 | + soc_info["ai_core_cnt"] = "32"; | ||
| 294 | + platform_infos->SetPlatformRes("SoCInfo", soc_info); | ||
| 295 | + platform_infos->SetCoreNumByCoreType("AICore"); | ||
| 296 | + EXPECT_EQ(platform_infos->core_num_, 32); | ||
| 297 | +} | ||
| 298 | + | ||
| 299 | +TEST_F(PlatFormInfosDefUTest, SetCoreNumByCoreType_VectorCore_Success) { | ||
| 300 | + EXPECT_TRUE(platform_infos->Init()); | ||
| 301 | + std::map<std::string, std::string> soc_info; | ||
| 302 | + soc_info["vector_core_cnt"] = "16"; | ||
| 303 | + platform_infos->SetPlatformRes("SoCInfo", soc_info); | ||
| 304 | + platform_infos->SetCoreNumByCoreType("VectorCore"); | ||
| 305 | + EXPECT_EQ(platform_infos->core_num_, 16); | ||
| 306 | +} | ||
| 307 | + | ||
| 308 | +TEST_F(PlatFormInfosDefUTest, SetCoreNumByCoreType_MixAIC_AIV_Success) { | ||
| 309 | + EXPECT_TRUE(platform_infos->Init()); | ||
| 310 | + std::map<std::string, std::string> soc_info; | ||
| 311 | + soc_info["ai_core_cnt"] = "32"; | ||
| 312 | + soc_info["mix_vector_core_cnt"] = "8"; | ||
| 313 | + platform_infos->SetPlatformRes("SoCInfo", soc_info); | ||
| 314 | + platform_infos->SetCoreNumByCoreType("MIX_AIC_AIV"); | ||
| 315 | + EXPECT_EQ(platform_infos->core_num_, 8); | ||
| 316 | +} | ||
| 317 | + | ||
| 318 | +TEST_F(PlatFormInfosDefUTest, SetCoreNumByCoreType_EmptyCoreNumStr_Success) { | ||
| 319 | + EXPECT_TRUE(platform_infos->Init()); | ||
| 320 | + std::map<std::string, std::string> soc_info; | ||
| 321 | + soc_info["ai_core_cnt"] = ""; | ||
| 322 | + platform_infos->SetPlatformRes("SoCInfo", soc_info); | ||
| 323 | + platform_infos->SetCoreNumByCoreType("AICore"); | ||
| 324 | + EXPECT_EQ(platform_infos->core_num_, 0); | ||
| 325 | +} | ||
| 326 | + | ||
| 327 | +class OptionalInfosDefUTest : public testing::Test { | ||
| 328 | +protected: | ||
| 329 | + void SetUp() { | ||
| 330 | + optional_infos = new OptionalInfos(); | ||
| 331 | + } | ||
| 332 | + | ||
| 333 | + void TearDown() { | ||
| 334 | + delete optional_infos; | ||
| 335 | + optional_infos = nullptr; | ||
| 336 | + GlobalMockObject::verify(); | ||
| 337 | + } | ||
| 338 | + | ||
| 339 | + OptionalInfos *optional_infos; | ||
| 340 | +}; | ||
| 341 | + | ||
| 342 | +TEST_F(OptionalInfosDefUTest, Init_Success) { | ||
| 343 | + EXPECT_TRUE(optional_infos->Init()); | ||
| 344 | +} | ||
| 345 | + | ||
| 346 | +TEST_F(OptionalInfosDefUTest, GetSocVersion_Success) { | ||
| 347 | + EXPECT_TRUE(optional_infos->Init()); | ||
| 348 | + optional_infos->SetSocVersion("Ascend910"); | ||
| 349 | + EXPECT_EQ(optional_infos->GetSocVersion(), "Ascend910"); | ||
| 350 | +} | ||
| 351 | + | ||
| 352 | +TEST_F(OptionalInfosDefUTest, GetCoreType_Success) { | ||
| 353 | + EXPECT_TRUE(optional_infos->Init()); | ||
| 354 | + optional_infos->SetCoreType("AICore"); | ||
| 355 | + EXPECT_EQ(optional_infos->GetCoreType(), "AICore"); | ||
| 356 | +} | ||
| 357 | + | ||
| 358 | +TEST_F(OptionalInfosDefUTest, GetAICoreNum_Success) { | ||
| 359 | + EXPECT_TRUE(optional_infos->Init()); | ||
| 360 | + optional_infos->SetAICoreNum(32); | ||
| 361 | + EXPECT_EQ(optional_infos->GetAICoreNum(), 32); | ||
| 362 | +} | ||
| 363 | + | ||
| 364 | +TEST_F(OptionalInfosDefUTest, GetL1FusionFlag_Success) { | ||
| 365 | + EXPECT_TRUE(optional_infos->Init()); | ||
| 366 | + optional_infos->SetL1FusionFlag("enable"); | ||
| 367 | + EXPECT_EQ(optional_infos->GetL1FusionFlag(), "enable"); | ||
| 368 | +} | ||
| 369 | + | ||
| 370 | +TEST_F(OptionalInfosDefUTest, SetFixPipeDtypeMap_Success) { | ||
| 371 | + EXPECT_TRUE(optional_infos->Init()); | ||
| 372 | + std::map<std::string, std::vector<std::string>> dtype_map; | ||
| 373 | + dtype_map["pipe1"] = {"fp16"}; | ||
| 374 | + optional_infos->SetFixPipeDtypeMap(dtype_map); | ||
| 375 | +} | ||
| 376 | + | ||
| 377 | +TEST_F(OptionalInfosDefUTest, GetFixPipeDtypeMap_Success) { | ||
| 378 | + EXPECT_TRUE(optional_infos->Init()); | ||
| 379 | + std::map<std::string, std::vector<std::string>> dtype_map; | ||
| 380 | + dtype_map["pipe1"] = {"fp16"}; | ||
| 381 | + optional_infos->SetFixPipeDtypeMap(dtype_map); | ||
| 382 | + auto result = optional_infos->GetFixPipeDtypeMap(); | ||
| 383 | + EXPECT_EQ(result.size(), 1); | ||
| 384 | +} | ||
| 385 | + | ||
| 386 | +TEST_F(OptionalInfosDefUTest, SetSocVersion_Success) { | ||
| 387 | + EXPECT_TRUE(optional_infos->Init()); | ||
| 388 | + optional_infos->SetSocVersion("Ascend910B"); | ||
| 389 | +} | ||
| 390 | + | ||
| 391 | +TEST_F(OptionalInfosDefUTest, SetSocVersionWithLock_Success) { | ||
| 392 | + EXPECT_TRUE(optional_infos->Init()); | ||
| 393 | + optional_infos->SetSocVersionWithLock("Ascend910B2"); | ||
| 394 | +} | ||
| 395 | + | ||
| 396 | +TEST_F(OptionalInfosDefUTest, SetCoreType_Success) { | ||
| 397 | + EXPECT_TRUE(optional_infos->Init()); | ||
| 398 | + optional_infos->SetCoreType("VectorCore"); | ||
| 399 | +} | ||
| 400 | + | ||
| 401 | +TEST_F(OptionalInfosDefUTest, SetAICoreNum_Success) { | ||
| 402 | + EXPECT_TRUE(optional_infos->Init()); | ||
| 403 | + optional_infos->SetAICoreNum(16); | ||
| 404 | +} | ||
| 405 | + | ||
| 406 | +TEST_F(OptionalInfosDefUTest, SetL1FusionFlag_Success) { | ||
| 407 | + EXPECT_TRUE(optional_infos->Init()); | ||
| 408 | + optional_infos->SetL1FusionFlag("disable"); | ||
| 409 | +} | ||
| 410 | + | ||
| 411 | +// 新增测试:覆盖 ParseBufferOfAICoreMemoryRates catch 分支 | ||
| 412 | +TEST_F(PlatFormInfosDefUTest, ParseBufferOfAICoreMemoryRates_InvalidInput) { | ||
| 413 | + EXPECT_TRUE(platform_infos->Init()); | ||
| 414 | + std::map<std::string, std::string> memory_rates; | ||
| 415 | + memory_rates["invalid_key"] = "invalid_value"; | ||
| 416 | + platform_infos->SetPlatformRes("AICoreMemoryRates", memory_rates); | ||
| 417 | +} | ||
| 418 | + | ||
| 419 | +// 新增测试:覆盖 ParseUBOfAICoreMemoryRates catch 分支 | ||
| 420 | +TEST_F(PlatFormInfosDefUTest, ParseUBOfAICoreMemoryRates_ExceptionHandling) { | ||
| 421 | + EXPECT_TRUE(platform_infos->Init()); | ||
| 422 | + std::map<std::string, std::string> memory_rates; | ||
| 423 | + memory_rates["ub_to_l1_rate"] = "exception_value"; | ||
| 424 | + platform_infos->SetPlatformRes("AICoreMemoryRates", memory_rates); | ||
| 425 | +} | ||
| 426 | + | ||
| 427 | +// 新增测试:覆盖所有 Parse 函数的异常分支 | ||
| 428 | +TEST_F(PlatFormInfosDefUTest, ParseUnzipOfAICoreSpec_OutOfRangeValues) { | ||
| 429 | + EXPECT_TRUE(platform_infos->Init()); | ||
| 430 | + std::map<std::string, std::string> core_spec; | ||
| 431 | + core_spec["unzip_max_ratios"] = "4294967296"; | ||
| 432 | + core_spec["unzip_channels"] = "4294967296"; | ||
| 433 | + core_spec["unzip_is_tight"] = "256"; | ||
| 434 | + platform_infos->SetPlatformRes("AICoreSpec", core_spec); | ||
| 435 | +} | ||
| 436 | + | ||
| 437 | +// 新增测试:覆盖 CalculateFraction 所有异常分支 | ||
| 438 | +TEST_F(PlatFormInfosDefUTest, CalculateFraction_AllExceptionBranches) { | ||
| 439 | + EXPECT_TRUE(platform_infos->Init()); | ||
| 440 | + std::map<std::string, std::string> memory_rates; | ||
| 441 | + | ||
| 442 | + // 测试std::exception catch分支 | ||
| 443 | + memory_rates["ub_to_l2_rate"] = "invalid_format"; | ||
| 444 | + platform_infos->SetPlatformRes("AICoreMemoryRates", memory_rates); | ||
| 445 | + | ||
| 446 | + // 测试unknown exception分支 | ||
| 447 | + memory_rates["ub_to_l2_rate"] = ""; | ||
| 448 | + platform_infos->SetPlatformRes("AICoreMemoryRates", memory_rates); | ||
| 449 | +} | ||
| 450 | + | ||
| 451 | +// 新增测试:覆盖 PlatFormInfosImpl::operator= | ||
| 452 | +TEST_F(PlatFormInfosDefUTest, PlatFormInfosImpl_CopyAssignment_Success) { | ||
| 453 | + EXPECT_TRUE(platform_infos->Init()); | ||
| 454 | + | ||
| 455 | + std::map<std::string, std::vector<std::string>> dtypes; | ||
| 456 | + dtypes["test"] = {"float", "int"}; | ||
| 457 | + platform_infos->SetAICoreIntrinsicDtype(dtypes); | ||
| 458 | + | ||
| 459 | + std::map<std::string, std::string> res; | ||
| 460 | + res["key1"] = "value1"; | ||
| 461 | + platform_infos->SetPlatformRes("TestSection", res); | ||
| 462 | + | ||
| 463 | + PlatFormInfos other; | ||
| 464 | + EXPECT_TRUE(other.Init()); | ||
| 465 | + | ||
| 466 | + // 测试赋值运算符(通过 InitByInstance 触发) | ||
| 467 | + other.InitByInstance(); | ||
| 468 | +} | ||
| 469 | + | ||
| 470 | +// 新增测试:覆盖 PlatFormInfosImpl 拷贝构造 | ||
| 471 | +TEST_F(PlatFormInfosDefUTest, PlatFormInfosImpl_CopyConstructor_Success) { | ||
| 472 | + EXPECT_TRUE(platform_infos->Init()); | ||
| 473 | + | ||
| 474 | + std::map<std::string, std::vector<std::string>> dtypes; | ||
| 475 | + dtypes["vec"] = {"fp16"}; | ||
| 476 | + platform_infos->SetVectorCoreIntrinsicDtype(dtypes); | ||
| 477 | + | ||
| 478 | + std::map<std::string, std::string> res; | ||
| 479 | + res["config"] = "value"; | ||
| 480 | + platform_infos->SetPlatformRes("Config", res); | ||
| 481 | + | ||
| 482 | + PlatFormInfos copy; | ||
| 483 | + EXPECT_TRUE(copy.Init()); | ||
| 484 | + copy.InitByInstance(); | ||
| 485 | +} | ||
| 486 | + | ||
| 487 | +// 新增测试:覆盖 platform_infos_impl.h 模板函数分支 | ||
| 488 | +TEST_F(PlatFormInfosDefUTest, GetPlatformResWithLock_MapMap_AllBranches) { | ||
| 489 | + EXPECT_TRUE(platform_infos->Init()); | ||
| 490 | + | ||
| 491 | + std::map<std::string, std::map<std::string, std::string>> res_map; | ||
| 492 | + std::map<std::string, std::string> inner_map; | ||
| 493 | + inner_map["inner_key"] = "inner_value"; | ||
| 494 | + res_map["outer_key"] = inner_map; | ||
| 495 | + | ||
| 496 | + std::map<std::string, std::map<std::string, std::string>> result; | ||
| 497 | + EXPECT_TRUE(platform_infos->GetPlatformResWithLock(result)); | ||
| 498 | +} | ||
| 499 | + | ||
| 500 | +// 新增测试:覆盖 SetCoreNumByCoreType 所有分支 | ||
| 501 | +TEST_F(PlatFormInfosDefUTest, SetCoreNumByCoreType_AllCoreTypes) { | ||
| 502 | + EXPECT_TRUE(platform_infos->Init()); | ||
| 503 | + | ||
| 504 | + std::map<std::string, std::string> soc_info; | ||
| 505 | + soc_info["ai_core_cnt"] = "32"; | ||
| 506 | + soc_info["vector_core_cnt"] = "16"; | ||
| 507 | + soc_info["mix_vector_core_cnt"] = "8"; | ||
| 508 | + platform_infos->SetPlatformRes("SoCInfo", soc_info); | ||
| 509 | + | ||
| 510 | + // 测试 AICore 类型 | ||
| 511 | + platform_infos->SetCoreNumByCoreType("AICore"); | ||
| 512 | + EXPECT_EQ(platform_infos->core_num_, 32); | ||
| 513 | + | ||
| 514 | + // 测试 VectorCore 类型 | ||
| 515 | + platform_infos->SetCoreNumByCoreType("VectorCore"); | ||
| 516 | + EXPECT_EQ(platform_infos->core_num_, 16); | ||
| 517 | + | ||
| 518 | + // 测试 AIV 类型 | ||
| 519 | + platform_infos->SetCoreNumByCoreType("AIV"); | ||
| 520 | + EXPECT_EQ(platform_infos->core_num_, 16); | ||
| 521 | + | ||
| 522 | + // 测试 MIX_AIV 类型 | ||
| 523 | + platform_infos->SetCoreNumByCoreType("MIX_AIV"); | ||
| 524 | + EXPECT_EQ(platform_infos->core_num_, 8); | ||
| 525 | + | ||
| 526 | + // 测试 MIX_VECTOR_CORE 类型 | ||
| 527 | + platform_infos->SetCoreNumByCoreType("MIX_VECTOR_CORE"); | ||
| 528 | + EXPECT_EQ(platform_infos->core_num_, 8); | ||
| 529 | +} | ||
| 530 | + | ||
| 531 | +// 新增测试:覆盖 GetLocalMemSize 所有类型 | ||
| 532 | +TEST_F(PlatFormInfosDefUTest, GetLocalMemSize_AllTypes) { | ||
| 533 | + EXPECT_TRUE(platform_infos->Init()); | ||
| 534 | + | ||
| 535 | + std::map<std::string, std::string> core_spec; | ||
| 536 | + core_spec["l0_a_size"] = "1024"; | ||
| 537 | + core_spec["l0_b_size"] = "2048"; | ||
| 538 | + core_spec["l0_c_size"] = "3072"; | ||
| 539 | + core_spec["l1_size"] = "4096"; | ||
| 540 | + core_spec["ub_size"] = "5120"; | ||
| 541 | + platform_infos->SetPlatformRes("AICoreSpec", core_spec); | ||
| 542 | + | ||
| 543 | + std::map<std::string, std::string> soc_info; | ||
| 544 | + soc_info["l2_size"] = "6144"; | ||
| 545 | + soc_info["memory_size"] = "7168"; | ||
| 546 | + platform_infos->SetPlatformRes("SoCInfo", soc_info); | ||
| 547 | + | ||
| 548 | + uint64_t size = 0; | ||
| 549 | + | ||
| 550 | + platform_infos->GetLocalMemSize(LocalMemType::L0_A, size); | ||
| 551 | + platform_infos->GetLocalMemSize(LocalMemType::L0_B, size); | ||
| 552 | + platform_infos->GetLocalMemSize(LocalMemType::L0_C, size); | ||
| 553 | + platform_infos->GetLocalMemSize(LocalMemType::L1, size); | ||
| 554 | + platform_infos->GetLocalMemSize(LocalMemType::L2, size); | ||
| 555 | + platform_infos->GetLocalMemSize(LocalMemType::UB, size); | ||
| 556 | + platform_infos->GetLocalMemSize(LocalMemType::HBM, size); | ||
| 557 | +} | ||
| 558 | + | ||
| 559 | +// 新增测试:覆盖 GetLocalMemBw 所有类型 | ||
| 560 | +TEST_F(PlatFormInfosDefUTest, GetLocalMemBw_AllTypes) { | ||
| 561 | + EXPECT_TRUE(platform_infos->Init()); | ||
| 562 | + | ||
| 563 | + std::map<std::string, std::string> memory_rates; | ||
| 564 | + memory_rates["l2_rate"] = "2.0"; | ||
| 565 | + memory_rates["ddr_rate"] = "3.0"; | ||
| 566 | + platform_infos->SetPlatformRes("AICoreMemoryRates", memory_rates); | ||
| 567 | + | ||
| 568 | + uint64_t bw = 0; | ||
| 569 | + | ||
| 570 | + platform_infos->GetLocalMemBw(LocalMemType::L2, bw); | ||
| 571 | + platform_infos->GetLocalMemBw(LocalMemType::HBM, bw); | ||
| 572 | +} | ||
| 573 | + | ||
| 574 | +} | ||
| @@ -0,0 +1,208 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2025 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | + | ||
| 12 | + | ||
| 13 | + | ||
| 14 | + | ||
| 15 | + | ||
| 16 | + | ||
| 17 | + | ||
| 18 | + | ||
| 19 | + | ||
| 20 | + | ||
| 21 | +namespace fe { | ||
| 22 | + | ||
| 23 | +class PlatformInfosUtilsUTest : public testing::Test { | ||
| 24 | +protected: | ||
| 25 | + void SetUp() {} | ||
| 26 | + | ||
| 27 | + void TearDown() { | ||
| 28 | + GlobalMockObject::verify(); | ||
| 29 | + } | ||
| 30 | +}; | ||
| 31 | + | ||
| 32 | +TEST_F(PlatformInfosUtilsUTest, Trim_EmptyString_NoChange) { | ||
| 33 | + std::string str = ""; | ||
| 34 | + PlatformInfosUtils::Trim(str); | ||
| 35 | + EXPECT_EQ(str, ""); | ||
| 36 | +} | ||
| 37 | + | ||
| 38 | +TEST_F(PlatformInfosUtilsUTest, Trim_OnlySpaces_ReturnsEmpty) { | ||
| 39 | + std::string str = " \t\t \t "; | ||
| 40 | + PlatformInfosUtils::Trim(str); | ||
| 41 | + EXPECT_EQ(str, ""); | ||
| 42 | +} | ||
| 43 | + | ||
| 44 | +TEST_F(PlatformInfosUtilsUTest, Trim_LeadingAndTrailingSpaces_Success) { | ||
| 45 | + std::string str = " test123 "; | ||
| 46 | + PlatformInfosUtils::Trim(str); | ||
| 47 | + EXPECT_EQ(str, "test123"); | ||
| 48 | +} | ||
| 49 | + | ||
| 50 | +TEST_F(PlatformInfosUtilsUTest, Trim_LeadingTabs_Success) { | ||
| 51 | + std::string str = "\t\tvalue\t"; | ||
| 52 | + PlatformInfosUtils::Trim(str); | ||
| 53 | + EXPECT_EQ(str, "value"); | ||
| 54 | +} | ||
| 55 | + | ||
| 56 | +TEST_F(PlatformInfosUtilsUTest, Trim_NoSpaces_NoChange) { | ||
| 57 | + std::string str = "no_spaces"; | ||
| 58 | + PlatformInfosUtils::Trim(str); | ||
| 59 | + EXPECT_EQ(str, "no_spaces"); | ||
| 60 | +} | ||
| 61 | + | ||
| 62 | +TEST_F(PlatformInfosUtilsUTest, Split_EmptyString_ReturnsEmptyVector) { | ||
| 63 | + std::string str = ""; | ||
| 64 | + std::vector<std::string> result; | ||
| 65 | + PlatformInfosUtils::Split(str, ',', result); | ||
| 66 | + EXPECT_TRUE(result.empty()); | ||
| 67 | +} | ||
| 68 | + | ||
| 69 | +TEST_F(PlatformInfosUtilsUTest, Split_SingleDelimiter_ReturnsTwoEmptyStrings) { | ||
| 70 | + std::string str = ","; | ||
| 71 | + std::vector<std::string> result; | ||
| 72 | + PlatformInfosUtils::Split(str, ',', result); | ||
| 73 | + EXPECT_EQ(result.size(), 2); | ||
| 74 | + EXPECT_EQ(result[0], ""); | ||
| 75 | + EXPECT_EQ(result[1], ""); | ||
| 76 | +} | ||
| 77 | + | ||
| 78 | +TEST_F(PlatformInfosUtilsUTest, Split_MultipleValues_Success) { | ||
| 79 | + std::string str = "a,b,c,d"; | ||
| 80 | + std::vector<std::string> result; | ||
| 81 | + PlatformInfosUtils::Split(str, ',', result); | ||
| 82 | + EXPECT_EQ(result.size(), 4); | ||
| 83 | + EXPECT_EQ(result[0], "a"); | ||
| 84 | + EXPECT_EQ(result[1], "b"); | ||
| 85 | + EXPECT_EQ(result[2], "c"); | ||
| 86 | + EXPECT_EQ(result[3], "d"); | ||
| 87 | +} | ||
| 88 | + | ||
| 89 | +TEST_F(PlatformInfosUtilsUTest, Split_WithSpaces_Success) { | ||
| 90 | + std::string str = "item1, item2 , item3"; | ||
| 91 | + std::vector<std::string> result; | ||
| 92 | + PlatformInfosUtils::Split(str, ',', result); | ||
| 93 | + EXPECT_EQ(result.size(), 3); | ||
| 94 | + EXPECT_EQ(result[0], "item1"); | ||
| 95 | + EXPECT_EQ(result[1], " item2 "); | ||
| 96 | + EXPECT_EQ(result[2], " item3"); | ||
| 97 | +} | ||
| 98 | + | ||
| 99 | +TEST_F(PlatformInfosUtilsUTest, Split_TabDelimiter_Success) { | ||
| 100 | + std::string str = "val1\tval2\tval3"; | ||
| 101 | + std::vector<std::string> result; | ||
| 102 | + PlatformInfosUtils::Split(str, '\t', result); | ||
| 103 | + EXPECT_EQ(result.size(), 3); | ||
| 104 | + EXPECT_EQ(result[0], "val1"); | ||
| 105 | + EXPECT_EQ(result[1], "val2"); | ||
| 106 | + EXPECT_EQ(result[2], "val3"); | ||
| 107 | +} | ||
| 108 | + | ||
| 109 | +TEST_F(PlatformInfosUtilsUTest, Split_NoDelimiter_ReturnsSingleElement) { | ||
| 110 | + std::string str = "single_value"; | ||
| 111 | + std::vector<std::string> result; | ||
| 112 | + PlatformInfosUtils::Split(str, ',', result); | ||
| 113 | + EXPECT_EQ(result.size(), 1); | ||
| 114 | + EXPECT_EQ(result[0], "single_value"); | ||
| 115 | +} | ||
| 116 | + | ||
| 117 | +TEST_F(PlatformInfosUtilsUTest, Split_ConsecutiveDelimiters_HandlesEmptyStrings) { | ||
| 118 | + std::string str = "a,,b,,c"; | ||
| 119 | + std::vector<std::string> result; | ||
| 120 | + PlatformInfosUtils::Split(str, ',', result); | ||
| 121 | + EXPECT_EQ(result.size(), 5); | ||
| 122 | + EXPECT_EQ(result[0], "a"); | ||
| 123 | + EXPECT_EQ(result[1], ""); | ||
| 124 | + EXPECT_EQ(result[2], "b"); | ||
| 125 | + EXPECT_EQ(result[3], ""); | ||
| 126 | + EXPECT_EQ(result[4], "c"); | ||
| 127 | +} | ||
| 128 | + | ||
| 129 | +TEST_F(PlatformInfosUtilsUTest, RealSoFilePath_EmptyPath_ReturnsEmpty) { | ||
| 130 | + std::string path = ""; | ||
| 131 | + std::string result = RealSoFilePath(path); | ||
| 132 | + EXPECT_EQ(result, ""); | ||
| 133 | +} | ||
| 134 | + | ||
| 135 | +TEST_F(PlatformInfosUtilsUTest, RealSoFilePath_InvalidPath_ReturnsEmpty) { | ||
| 136 | + std::string path = "nonexistent_path"; | ||
| 137 | + std::string result = RealSoFilePath(path); | ||
| 138 | + EXPECT_EQ(result, ""); | ||
| 139 | +} | ||
| 140 | + | ||
| 141 | +TEST_F(PlatformInfosUtilsUTest, Clone_Success) { | ||
| 142 | + PlatFormInfos src; | ||
| 143 | + EXPECT_TRUE(src.Init()); | ||
| 144 | + src.core_num_ = 8; | ||
| 145 | + | ||
| 146 | + std::map<std::string, std::vector<std::string>> dtypes; | ||
| 147 | + dtypes["test"] = {"float"}; | ||
| 148 | + src.SetAICoreIntrinsicDtype(dtypes); | ||
| 149 | + | ||
| 150 | + PlatFormInfos dest; | ||
| 151 | + EXPECT_TRUE(dest.Init()); | ||
| 152 | + | ||
| 153 | + PlatformInfosUtils::GetInstance().Clone(dest, src); | ||
| 154 | +} | ||
| 155 | + | ||
| 156 | +TEST_F(PlatformInfosUtilsUTest, GetInstance_Success) { | ||
| 157 | + auto &instance = PlatformInfosUtils::GetInstance(); | ||
| 158 | + EXPECT_NE(&instance, nullptr); | ||
| 159 | +} | ||
| 160 | + | ||
| 161 | +TEST_F(PlatformInfosUtilsUTest, GetSoFilePath_Success) { | ||
| 162 | + auto path = GetSoFilePath<PlatformManagerV2>(); | ||
| 163 | +} | ||
| 164 | + | ||
| 165 | +TEST_F(PlatformInfosUtilsUTest, GetConfigFilePath_Success) { | ||
| 166 | + auto path = GetConfigFilePath<PlatformManagerV2>(); | ||
| 167 | +} | ||
| 168 | + | ||
| 169 | +// 新增测试:覆盖模板函数的所有分支 | ||
| 170 | +TEST_F(PlatformInfosUtilsUTest, GetSoFilePath_WithValidPath) { | ||
| 171 | + auto path = GetSoFilePath<PlatformManagerV2>(); | ||
| 172 | + // 模板函数内部逻辑会尝试获取 SO 文件路径 | ||
| 173 | + // 在 UT 环境可能返回空字符串 | ||
| 174 | +} | ||
| 175 | + | ||
| 176 | +TEST_F(PlatformInfosUtilsUTest, GetConfigFilePath_EdgeCases) { | ||
| 177 | + // 测试配置文件路径获取的边界情况 | ||
| 178 | + auto path = GetConfigFilePath<PlatformManagerV2>(); | ||
| 179 | + // 模板函数会处理空路径、路径查找等逻辑 | ||
| 180 | +} | ||
| 181 | + | ||
| 182 | +TEST_F(PlatformInfosUtilsUTest, Clone_WithAllData) { | ||
| 183 | + PlatFormInfos src; | ||
| 184 | + EXPECT_TRUE(src.Init()); | ||
| 185 | + | ||
| 186 | + std::map<std::string, std::vector<std::string>> ai_dtypes; | ||
| 187 | + ai_dtypes["ai_op"] = {"fp16", "int32"}; | ||
| 188 | + src.SetAICoreIntrinsicDtype(ai_dtypes); | ||
| 189 | + | ||
| 190 | + std::map<std::string, std::vector<std::string>> vec_dtypes; | ||
| 191 | + vec_dtypes["vec_op"] = {"fp32"}; | ||
| 192 | + src.SetVectorCoreIntrinsicDtype(vec_dtypes); | ||
| 193 | + | ||
| 194 | + std::map<std::string, std::string> res; | ||
| 195 | + res["key"] = "value"; | ||
| 196 | + src.SetPlatformRes("Section", res); | ||
| 197 | + | ||
| 198 | + std::map<std::string, std::vector<std::string>> fixpipe; | ||
| 199 | + fixpipe["pipe"] = {"dtype"}; | ||
| 200 | + src.SetFixPipeDtypeMap(fixpipe); | ||
| 201 | + | ||
| 202 | + PlatFormInfos dest; | ||
| 203 | + EXPECT_TRUE(dest.Init()); | ||
| 204 | + | ||
| 205 | + PlatformInfosUtils::GetInstance().Clone(dest, src); | ||
| 206 | +} | ||
| 207 | + | ||
| 208 | +} | ||