CollisionAvoidance/tests/BasicTypesTest.cpp

102 lines
2.6 KiB
C++
Raw 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 <gtest/gtest.h>
#include "types/BasicTypes.h"
#include <ctime>
class BasicTypesTest : public ::testing::Test {
protected:
void SetUp() override {
// 测试设置
}
};
// 测试 Vector2D
TEST_F(BasicTypesTest, Vector2DMagnitude) {
Vector2D v(3.0, 4.0);
EXPECT_DOUBLE_EQ(v.magnitude(), 5.0);
}
TEST_F(BasicTypesTest, Vector2DDirection) {
Vector2D v(1.0, 1.0); // 45度角
EXPECT_NEAR(v.direction(), 45.0, 0.001);
}
// 测试 MovingObject 的速度计算
class TestMovingObject : public MovingObject {
protected:
bool isValidPosition(const GeoPosition& newPos) const override {
return true;
}
bool isValidSpeed(double speed) const override {
return speed <= 100.0;
}
double getSpeedSmoothingFactor() const override {
return 0.3;
}
};
TEST_F(BasicTypesTest, MovingObjectSpeedCalculation) {
TestMovingObject obj;
// 获取当前时间戳
time_t now = time(nullptr);
// 第一个位置
GeoPosition pos1{36.0, 120.0};
obj.updateMotion(pos1, now);
EXPECT_DOUBLE_EQ(obj.speed, 0.0); // 第一个点速度应该为0
// 第二个位置向东移动约90米/秒)
GeoPosition pos2{36.0, 120.001};
obj.updateMotion(pos2, now + 1); // 1秒后
EXPECT_NEAR(obj.speed, 90.0, 2.0); // 使用 Haversine 公式计算的实际距离
// 检查航向角
EXPECT_NEAR(obj.heading, 90.0, 0.1); // 应该朝向正东
}
// 测试 Aircraft
TEST_F(BasicTypesTest, AircraftValidation) {
Aircraft aircraft;
aircraft.flightNo = "CES2501";
// 获取当前时间戳
time_t now = time(nullptr);
// 测试位置更新
GeoPosition pos1{36.0, 120.0};
aircraft.updateMotion(pos1, now);
GeoPosition pos2{36.0, 120.001}; // 约90米
aircraft.updateMotion(pos2, now + 2); // 2秒后
// 速度应该约为45米/秒
EXPECT_NEAR(aircraft.speed, 45.0, 2.0);
}
// 测试 Vehicle
TEST_F(BasicTypesTest, VehicleValidation) {
Vehicle vehicle;
vehicle.vehicleNo = "VEH001";
// 获取当前时间戳
time_t now = time(nullptr);
// 测试位置更新
GeoPosition pos1{36.0, 120.0};
vehicle.updateMotion(pos1, now);
// 使用约22米的位置变化小于50米的跳变限制
GeoPosition pos2{36.0002, 120.0}; // 约22米
vehicle.updateMotion(pos2, now + 1.0); // 1秒后
// 速度应该约为22米/秒
EXPECT_NEAR(vehicle.speed, 22.0, 2.0);
}
// 添加边界条件测试
TEST_F(BasicTypesTest, VectorZeroMagnitude) {
Vector2D v(0.0, 0.0);
EXPECT_DOUBLE_EQ(v.magnitude(), 0.0);
}