317 lines
8.5 KiB
C++
317 lines
8.5 KiB
C++
#include <iostream>
|
|
#include <string>
|
|
#include <cassert>
|
|
#include <cstdio>
|
|
#include "../../src/pPowerManger/sqlit3/SQLite.h"
|
|
|
|
// 简单的测试框架
|
|
#define TEST_CASE(name) void test_##name()
|
|
#define RUN_TEST(name) \
|
|
std::cout << "Running test: " << #name << std::endl; \
|
|
test_##name(); \
|
|
std::cout << "Test " << #name << " passed!" << std::endl
|
|
|
|
// 测试数据生成函数
|
|
ccuState createTestCcuState() {
|
|
ccuState state = {};
|
|
|
|
// 填充一些测试数据
|
|
state.fc_mode = 2; // 自动模式
|
|
state.fc_status = 6; // 运行状态
|
|
state.fc_fault_level = 0; // 无故障
|
|
|
|
state.output_power_limit = 15000;
|
|
state.total_generation_time = 100;
|
|
state.total_generation_power = 50000;
|
|
|
|
state.hydrogen_capacity = 80;
|
|
state.liquid_oxygen_capacity = 75;
|
|
|
|
state.palladium_temp = 450;
|
|
state.main_pipe_pressure = 1000;
|
|
state.aux_pipe_pressure = 800;
|
|
|
|
state.cabin_pressure1 = 1013;
|
|
state.cabin_pressure2 = 1012;
|
|
state.cabin_temp1 = 2500;
|
|
state.cabin_temp2 = 2490;
|
|
state.cabin_humidity1 = 6000;
|
|
state.cabin_humidity2 = 6100;
|
|
|
|
state.flame_detector1 = 0;
|
|
state.flame_detector2 = 0;
|
|
|
|
state.h2_concentration1 = 50;
|
|
state.h2_concentration2 = 48;
|
|
state.h2_concentration3 = 52;
|
|
state.o2_concentration1 = 2100;
|
|
state.o2_concentration2 = 2090;
|
|
|
|
state.emergency_battery1_voltage = 480;
|
|
state.emergency_battery1_current = 25;
|
|
state.emergency_battery1_max_temp = 35;
|
|
|
|
state.ins_soc = 85;
|
|
state.dyn_soc = 90;
|
|
|
|
state.heartbeat = 12345;
|
|
|
|
return state;
|
|
}
|
|
|
|
ccuColCmd createTestCcuColCmd() {
|
|
ccuColCmd cmd = {};
|
|
cmd.fcmode = 2;
|
|
cmd.fcuCmd = 2;
|
|
cmd.fcuPowerEfficiency = 80;
|
|
cmd.insChargeEnable = 1;
|
|
cmd.insBatCmd = 1;
|
|
cmd.powerBatCmd = 1;
|
|
cmd.powerBatEfficiency = 85;
|
|
cmd.heartbeat = 123; // 修复unsigned char溢出警告
|
|
cmd.masterState = 1;
|
|
return cmd;
|
|
}
|
|
|
|
disHighVolBusState createTestDisHighVolBusState() {
|
|
disHighVolBusState state = {};
|
|
state.fuelCellCircuitBreaker = 0x55;
|
|
state.powerLithiumBatteryCircuitBreaker = 0x55;
|
|
state.propulsionMotorCircuitBreaker = 0x55;
|
|
state.powerBusVoltage = 4800;
|
|
state.dcDc5ModuleCurrent = 100;
|
|
state.powerBusCurrent = 500;
|
|
state.busbarAVoltage = 4750;
|
|
return state;
|
|
}
|
|
|
|
disHighAVolBusState createTestDisHighAVolBusState() {
|
|
disHighAVolBusState state = {};
|
|
state.bowFTDevice123CircuitBreaker = 0x55;
|
|
state.actuatorCircuitBreaker = 0x55;
|
|
state.busbarBVoltage = 4800;
|
|
state.busbarBCurrent = 200;
|
|
return state;
|
|
}
|
|
|
|
disLowMainBusState createTestDisLowMainBusState() {
|
|
disLowMainBusState state = {};
|
|
state.lithiumBatteryGroupInstrumentCircuitBreaker = 0x55;
|
|
state.bowLowVoltageDistributionBoxCircuitBreaker = 0x55;
|
|
state.instrumentBusbarVoltage = 480;
|
|
state.bowLowVoltageDistributionBoxCurrent = 50;
|
|
return state;
|
|
}
|
|
|
|
disLowBusState createTestDisLowBusState() {
|
|
disLowBusState state = {};
|
|
state.unit1CircuitBreaker = 0x55;
|
|
state.unit2CircuitBreaker = 0x55;
|
|
state.instrumentBusbarVoltage = 480;
|
|
state.instrumentBusbarCurrent = 100;
|
|
return state;
|
|
}
|
|
|
|
disHighVolBusCmd createTestDisHighVolBusCmd() {
|
|
disHighVolBusCmd cmd = {};
|
|
cmd.fuelCellCircuitBreaker = 0x55;
|
|
cmd.powerLithiumBatteryCircuitBreaker = 0x55;
|
|
cmd.propulsionMotorCircuitBreaker = 0x55;
|
|
cmd.dcdc5Module = 1;
|
|
cmd.coolingSystem = 1;
|
|
return cmd;
|
|
}
|
|
|
|
disHighAVolBusCmd createTestDisHighAVolBusCmd() {
|
|
disHighAVolBusCmd cmd = {};
|
|
cmd.bowFTDevice123CircuitBreaker = 0x55;
|
|
cmd.actuatorCircuitBreaker = 0x55;
|
|
return cmd;
|
|
}
|
|
|
|
disLowMainBusCmd createTestDisLowMainBusCmd() {
|
|
disLowMainBusCmd cmd = {};
|
|
cmd.lithiumBatteryGroupInstrumentCircuitBreaker = 0x55;
|
|
cmd.bowLowVoltageDistributionBoxCircuitBreaker = 0x55;
|
|
return cmd;
|
|
}
|
|
|
|
disLowBusCmd createTestDisLowBusCmd() {
|
|
disLowBusCmd cmd = {};
|
|
cmd.unit1CircuitBreaker = 0x55;
|
|
cmd.unit2CircuitBreaker = 0x55;
|
|
return cmd;
|
|
}
|
|
|
|
// 测试用例
|
|
TEST_CASE(constructor_and_destructor) {
|
|
// 测试正常构造
|
|
SQLite db(":memory:");
|
|
assert(db.m_dbPath == ":memory:");
|
|
|
|
// 测试无效路径
|
|
SQLite invalidDb("/invalid/path/test.db");
|
|
// 这里应该检查数据库是否成功打开,但由于是内存数据库,我们主要测试构造函数不崩溃
|
|
}
|
|
|
|
TEST_CASE(create_table) {
|
|
SQLite db(":memory:");
|
|
|
|
// 测试创建默认表
|
|
bool result = db.createTable();
|
|
assert(result == true);
|
|
|
|
// 测试创建自定义表
|
|
result = db.createTable("test_table", "id INTEGER PRIMARY KEY, name TEXT");
|
|
assert(result == true);
|
|
}
|
|
|
|
TEST_CASE(insert_ccu_state) {
|
|
SQLite db(":memory:");
|
|
db.createTable();
|
|
|
|
ccuState state = createTestCcuState();
|
|
bool result = db.insertData(state);
|
|
assert(result == true);
|
|
}
|
|
|
|
TEST_CASE(insert_ccu_col_cmd) {
|
|
SQLite db(":memory:");
|
|
db.createTable();
|
|
|
|
ccuColCmd cmd = createTestCcuColCmd();
|
|
bool result = db.insertData(cmd);
|
|
assert(result == true);
|
|
}
|
|
|
|
TEST_CASE(insert_dis_high_vol_bus_state) {
|
|
SQLite db(":memory:");
|
|
db.createTable();
|
|
|
|
disHighVolBusState state = createTestDisHighVolBusState();
|
|
bool result = db.insertData(state);
|
|
assert(result == true);
|
|
}
|
|
|
|
TEST_CASE(insert_dis_high_a_vol_bus_state) {
|
|
SQLite db(":memory:");
|
|
db.createTable();
|
|
|
|
disHighAVolBusState state = createTestDisHighAVolBusState();
|
|
bool result = db.insertData(state);
|
|
assert(result == true);
|
|
}
|
|
|
|
TEST_CASE(insert_dis_low_main_bus_state) {
|
|
SQLite db(":memory:");
|
|
db.createTable();
|
|
|
|
disLowMainBusState state = createTestDisLowMainBusState();
|
|
bool result = db.insertData(state);
|
|
assert(result == true);
|
|
}
|
|
|
|
TEST_CASE(insert_dis_low_bus_state) {
|
|
SQLite db(":memory:");
|
|
db.createTable();
|
|
|
|
disLowBusState state = createTestDisLowBusState();
|
|
bool result = db.insertData(state);
|
|
assert(result == true);
|
|
}
|
|
|
|
TEST_CASE(insert_dis_high_vol_bus_cmd) {
|
|
SQLite db(":memory:");
|
|
db.createTable();
|
|
|
|
disHighVolBusCmd cmd = createTestDisHighVolBusCmd();
|
|
bool result = db.insertData(cmd);
|
|
assert(result == true);
|
|
}
|
|
|
|
TEST_CASE(insert_dis_high_a_vol_bus_cmd) {
|
|
SQLite db(":memory:");
|
|
db.createTable();
|
|
|
|
disHighAVolBusCmd cmd = createTestDisHighAVolBusCmd();
|
|
bool result = db.insertData(cmd);
|
|
assert(result == true);
|
|
}
|
|
|
|
TEST_CASE(insert_dis_low_main_bus_cmd) {
|
|
SQLite db(":memory:");
|
|
db.createTable();
|
|
|
|
disLowMainBusCmd cmd = createTestDisLowMainBusCmd();
|
|
bool result = db.insertData(cmd);
|
|
assert(result == true);
|
|
}
|
|
|
|
TEST_CASE(insert_dis_low_bus_cmd) {
|
|
SQLite db(":memory:");
|
|
db.createTable();
|
|
|
|
disLowBusCmd cmd = createTestDisLowBusCmd();
|
|
bool result = db.insertData(cmd);
|
|
assert(result == true);
|
|
}
|
|
|
|
TEST_CASE(multiple_inserts) {
|
|
SQLite db(":memory:");
|
|
db.createTable();
|
|
|
|
// 插入多种类型的数据
|
|
ccuState state = createTestCcuState();
|
|
assert(db.insertData(state) == true);
|
|
|
|
ccuColCmd cmd = createTestCcuColCmd();
|
|
assert(db.insertData(cmd) == true);
|
|
|
|
disHighVolBusState busState = createTestDisHighVolBusState();
|
|
assert(db.insertData(busState) == true);
|
|
}
|
|
|
|
TEST_CASE(invalid_database) {
|
|
// 测试无效数据库路径
|
|
SQLite db("/invalid/path/test.db");
|
|
|
|
// 尝试创建表应该失败
|
|
bool result = db.createTable();
|
|
assert(result == false);
|
|
|
|
// 尝试插入数据应该失败
|
|
ccuState state = createTestCcuState();
|
|
result = db.insertData(state);
|
|
assert(result == false);
|
|
}
|
|
|
|
int main() {
|
|
std::cout << "Starting SQLite database tests..." << std::endl;
|
|
|
|
try {
|
|
RUN_TEST(constructor_and_destructor);
|
|
RUN_TEST(create_table);
|
|
RUN_TEST(insert_ccu_state);
|
|
RUN_TEST(insert_ccu_col_cmd);
|
|
RUN_TEST(insert_dis_high_vol_bus_state);
|
|
RUN_TEST(insert_dis_high_a_vol_bus_state);
|
|
RUN_TEST(insert_dis_low_main_bus_state);
|
|
RUN_TEST(insert_dis_low_bus_state);
|
|
RUN_TEST(insert_dis_high_vol_bus_cmd);
|
|
RUN_TEST(insert_dis_high_a_vol_bus_cmd);
|
|
RUN_TEST(insert_dis_low_main_bus_cmd);
|
|
RUN_TEST(insert_dis_low_bus_cmd);
|
|
RUN_TEST(multiple_inserts);
|
|
RUN_TEST(invalid_database);
|
|
|
|
std::cout << "\nAll tests passed successfully!" << std::endl;
|
|
return 0;
|
|
} catch (const std::exception& e) {
|
|
std::cerr << "Test failed with exception: " << e.what() << std::endl;
|
|
return 1;
|
|
} catch (...) {
|
|
std::cerr << "Test failed with unknown exception" << std::endl;
|
|
return 1;
|
|
}
|
|
}
|