27/tests/basic_test.cpp

163 lines
4.7 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "app.hpp"
#include <cassert>
#include <iostream>
#include <string>
/**
* @brief ODF Manager 单元测试 (使用标准库 assert)
*
* 覆盖:编码生成、兼容性校验、单元注册、配置查询、标签与排期。
*/
static void test_naming_code_generation() {
ODFManager mgr;
std::string code = mgr.generate_naming_code("DC01", "RF02", "SL05");
// 编码格式应为 "DC01-RF02-SL05"
assert(code == "DC01-RF02-SL05");
assert(code.size() <= 64);
std::cout << "[PASS] test_naming_code_generation\n";
}
static void test_naming_code_uniqueness() {
ODFManager mgr;
ODFUnitInfo u1;
u1.unit_id = "SITE-A-01";
u1.max_capacity_cores = 144;
mgr.register_unit(u1);
// 当编码与已注册 ID 冲突时,应追加后缀避免重复
std::string code = mgr.generate_naming_code("SITE", "A", "01");
// 由于冲突code 会包含时间戳后缀,不应等于 "SITE-A-01"
assert(code != "SITE-A-01");
assert(code.size() <= 64);
std::cout << "[PASS] test_naming_code_uniqueness\n";
}
static void test_adapter_compatibility() {
ODFManager mgr;
AdapterSpec lc_upc;
lc_upc.interface_type = ConnectorType::LC;
lc_upc.polish_type = PolishType::UPC;
AdapterSpec lc_apc;
lc_apc.interface_type = ConnectorType::LC;
lc_apc.polish_type = PolishType::APC;
AdapterSpec sc_upc;
sc_upc.interface_type = ConnectorType::SC;
sc_upc.polish_type = PolishType::UPC;
// 同类型兼容
assert(mgr.validate_adapter_compatibility(lc_upc, lc_upc) == true);
// LC ↔ LCUPC ↔ APC 物理兼容
assert(mgr.validate_adapter_compatibility(lc_upc, lc_apc) == true);
// LC ↔ SC 不兼容
assert(mgr.validate_adapter_compatibility(lc_upc, sc_upc) == false);
std::cout << "[PASS] test_adapter_compatibility\n";
}
static void test_unit_registration() {
ODFManager mgr;
ODFUnitInfo u;
u.unit_id = "TEST-001";
u.model_name = "ODF-TEST";
u.location = "机房A-01-10U";
u.manufacturer = "TestMfr";
u.production_date = "2025-01-01";
u.max_capacity_cores = 144;
mgr.register_unit(u);
const ODFUnitInfo* found = mgr.find_unit("TEST-001");
assert(found != nullptr);
assert(found->model_name == "ODF-TEST");
assert(found->max_capacity_cores == 144);
// 重复注册应抛出异常
bool threw = false;
try {
mgr.register_unit(u);
} catch (const std::invalid_argument&) {
threw = true;
}
assert(threw);
// 超容量约束
ODFUnitInfo over;
over.unit_id = "OVER-001";
over.max_capacity_cores = 300;
bool threw2 = false;
try {
mgr.register_unit(over);
} catch (const std::invalid_argument&) {
threw2 = true;
}
assert(threw2);
std::cout << "[PASS] test_unit_registration\n";
}
static void test_typical_config() {
ODFManager mgr;
TypicalConfig cfg;
cfg.scene_name = "数据中心LC单模预端接";
cfg.environment = "数据中心";
cfg.port_density_per_u = 48;
mgr.register_typical_config(cfg);
TypicalConfig found = mgr.get_typical_config("数据中心LC单模预端接");
assert(!found.scene_name.empty());
assert(found.port_density_per_u == 48);
// 不存在的场景应返回空
TypicalConfig not_found = mgr.get_typical_config("UNKNOWN_SCENE");
assert(not_found.scene_name.empty());
std::cout << "[PASS] test_typical_config\n";
}
static void test_label_and_milestone() {
ODFManager mgr;
// 标签:不存在的单元
std::string label = mgr.print_label("NONEXIST");
assert(label.find("ERROR") != std::string::npos);
// 里程碑排期:空
std::string empty_schedule = mgr.export_milestone_schedule();
assert(empty_schedule.find("No milestones") != std::string::npos);
// 添加里程碑
Milestone ms;
ms.phase_name = "测试阶段";
ms.start_date = "2025-03-01";
ms.end_date = "2025-03-15";
ms.owner = "Tester";
ms.deliverable = "测试报告";
ms.is_completed = false;
mgr.add_milestone(ms);
assert(mgr.milestones().size() == 1);
std::string schedule = mgr.export_milestone_schedule();
assert(schedule.find("测试阶段") != std::string::npos);
assert(schedule.find("N") != std::string::npos); // 未完成标记
std::cout << "[PASS] test_label_and_milestone\n";
}
int main() {
std::cout << "=== ODF Manager Unit Tests ===\n\n";
test_naming_code_generation();
test_naming_code_uniqueness();
test_adapter_compatibility();
test_unit_registration();
test_typical_config();
test_label_and_milestone();
std::cout << "\nAll " << 6 << " tests PASSED.\n";
return 0;
}