141 lines
4.6 KiB
C++
141 lines
4.6 KiB
C++
#include <gtest/gtest.h>
|
|
#include <gmock/gmock.h>
|
|
#include <fstream>
|
|
#include <nlohmann/json.hpp>
|
|
#include "detector/TrafficLightDetector.h"
|
|
#include "config/IntersectionConfig.h"
|
|
#include "vehicle/ControllableVehicles.h"
|
|
#include "utils/Logger.h"
|
|
#include "types/VehicleCommand.h"
|
|
#include "types/TrafficLightTypes.h"
|
|
#include "core/System.h"
|
|
|
|
// Mock System 类
|
|
class MockSystem : public System {
|
|
public:
|
|
MOCK_METHOD(void, broadcastTrafficLightCommand, (const std::string&, const VehicleCommand&));
|
|
static MockSystem& getInstance() {
|
|
static MockSystem instance;
|
|
return instance;
|
|
}
|
|
private:
|
|
MockSystem() = default;
|
|
MockSystem(const MockSystem&) = delete;
|
|
MockSystem& operator=(const MockSystem&) = delete;
|
|
};
|
|
|
|
class TrafficLightDetectorTest : public ::testing::Test {
|
|
protected:
|
|
void SetUp() override {
|
|
// 创建测试用的交叉口配置
|
|
Intersection intersection;
|
|
intersection.id = "INT001";
|
|
intersection.name = "Test Intersection";
|
|
intersection.position.latitude = 36.36;
|
|
intersection.position.longitude = 120.08;
|
|
intersection.position.altitude = 9.5;
|
|
intersection.width = 50.0;
|
|
intersection.trafficLightId = "TL001";
|
|
|
|
// 创建临时配置文件
|
|
std::ofstream config_file("test_intersections.json");
|
|
nlohmann::json j;
|
|
j["intersections"] = nlohmann::json::array();
|
|
j["intersections"].push_back({
|
|
{"id", intersection.id},
|
|
{"name", intersection.name},
|
|
{"position", {
|
|
{"latitude", intersection.position.latitude},
|
|
{"longitude", intersection.position.longitude},
|
|
{"altitude", intersection.position.altitude}
|
|
}},
|
|
{"width", intersection.width},
|
|
{"trafficLightId", intersection.trafficLightId},
|
|
{"safetyZone", {
|
|
{"aircraftRadius", 50.0},
|
|
{"vehicleRadius", 50.0}
|
|
}}
|
|
});
|
|
config_file << j.dump(4);
|
|
config_file.close();
|
|
|
|
intersectionConfig = IntersectionConfig::load("test_intersections.json");
|
|
detector = std::make_unique<TrafficLightDetector>(
|
|
intersectionConfig,
|
|
ControllableVehicles::getInstance(),
|
|
MockSystem::getInstance()
|
|
);
|
|
}
|
|
|
|
void TearDown() override {
|
|
detector.reset();
|
|
std::remove("test_intersections.json");
|
|
}
|
|
|
|
// 创建测试用的红绿灯信号
|
|
TrafficLightSignal createTestSignal(const std::string& id, SignalStatus status) {
|
|
TrafficLightSignal signal;
|
|
signal.trafficLightId = id;
|
|
signal.ns_status = status;
|
|
signal.ew_status = status;
|
|
signal.timestamp = std::chrono::system_clock::now().time_since_epoch().count();
|
|
return signal;
|
|
}
|
|
|
|
// 创建测试用的车辆数据
|
|
Vehicle createTestVehicle(const std::string& id, double lat, double lon) {
|
|
Vehicle v;
|
|
v.id = id;
|
|
v.vehicleNo = id;
|
|
v.geo.latitude = lat;
|
|
v.geo.longitude = lon;
|
|
v.timestamp = std::chrono::system_clock::now().time_since_epoch().count();
|
|
return v;
|
|
}
|
|
|
|
std::unique_ptr<TrafficLightDetector> detector;
|
|
IntersectionConfig intersectionConfig;
|
|
};
|
|
|
|
// 测试红绿灯信号处理
|
|
TEST_F(TrafficLightDetectorTest, SignalProcessing) {
|
|
// 创建一个在交叉口范围内的车辆
|
|
Vehicle vehicle = createTestVehicle("V001", 36.36, 120.08);
|
|
std::vector<Vehicle> vehicles = {vehicle};
|
|
|
|
// 测试红灯
|
|
TrafficLightSignal redSignal = createTestSignal("TL001", SignalStatus::RED);
|
|
|
|
detector->processSignal(redSignal, vehicles);
|
|
|
|
// 测试绿灯
|
|
TrafficLightSignal greenSignal = createTestSignal("TL001", SignalStatus::GREEN);
|
|
|
|
detector->processSignal(greenSignal, vehicles);
|
|
}
|
|
|
|
// 测试无效的红绿灯信号
|
|
TEST_F(TrafficLightDetectorTest, InvalidSignal) {
|
|
Vehicle vehicle = createTestVehicle("V001", 36.36, 120.08);
|
|
std::vector<Vehicle> vehicles = {vehicle};
|
|
|
|
// 测试无效的红绿灯ID
|
|
TrafficLightSignal invalidSignal = createTestSignal("TL999", SignalStatus::RED);
|
|
|
|
detector->processSignal(invalidSignal, vehicles);
|
|
}
|
|
|
|
// 测试边界情况
|
|
TEST_F(TrafficLightDetectorTest, EdgeCases) {
|
|
// 测试空车辆列表
|
|
std::vector<Vehicle> emptyVehicles;
|
|
TrafficLightSignal signal = createTestSignal("TL001", SignalStatus::RED);
|
|
|
|
detector->processSignal(signal, emptyVehicles);
|
|
|
|
// 测试交叉口范围外的车辆
|
|
Vehicle farVehicle = createTestVehicle("V002", 36.40, 120.12);
|
|
std::vector<Vehicle> vehicles = {farVehicle};
|
|
|
|
detector->processSignal(signal, vehicles);
|
|
}
|