128 lines
3.5 KiB
C++
128 lines
3.5 KiB
C++
#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;
|
||
|
||
// 使用毫秒级时间戳
|
||
uint64_t now = 1000000;
|
||
|
||
// 第一个位置
|
||
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};
|
||
double distance = obj.calculateDistance(pos1, pos2);
|
||
printf("Distance: %.2f meters\n", distance);
|
||
|
||
obj.updateMotion(pos2, now + 1000); // 1000毫秒后
|
||
printf("Speed: %.2f m/s\n", obj.speed);
|
||
|
||
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";
|
||
|
||
// 使用毫秒级时间戳
|
||
uint64_t now = 1000000;
|
||
|
||
// 测试位置更新
|
||
GeoPosition pos1{36.0, 120.0};
|
||
aircraft.updateMotion(pos1, now);
|
||
|
||
GeoPosition pos2{36.0, 120.001}; // 约90米
|
||
aircraft.updateMotion(pos2, now + 2000); // 2000毫秒后
|
||
|
||
// 速度应该约为45米/秒
|
||
EXPECT_NEAR(aircraft.speed, 45.0, 2.0);
|
||
}
|
||
|
||
// 测试 Vehicle
|
||
TEST_F(BasicTypesTest, VehicleValidation) {
|
||
Vehicle vehicle;
|
||
vehicle.vehicleNo = "VEH001";
|
||
|
||
// 使用毫秒级时间戳
|
||
uint64_t now = 1000000;
|
||
|
||
// 测试位置更新
|
||
GeoPosition pos1{36.0, 120.0};
|
||
vehicle.updateMotion(pos1, now);
|
||
|
||
// 使用约9米的位置变化(小于10米的跳变限制)
|
||
GeoPosition pos2{36.00008, 120.0}; // 约9米
|
||
double distance = vehicle.calculateDistance(pos1, pos2);
|
||
printf("Vehicle distance: %.2f meters\n", distance);
|
||
|
||
vehicle.updateMotion(pos2, now + 1000); // 1000毫秒后
|
||
printf("Vehicle speed: %.2f m/s\n", vehicle.speed);
|
||
|
||
// 速度应该约为9米/秒(小于20米/秒的限制)
|
||
EXPECT_NEAR(vehicle.speed, 9.0, 1.0);
|
||
}
|
||
|
||
// 添加边界条件测试
|
||
TEST_F(BasicTypesTest, VectorZeroMagnitude) {
|
||
Vector2D v(0.0, 0.0);
|
||
EXPECT_DOUBLE_EQ(v.magnitude(), 0.0);
|
||
}
|
||
|
||
// 测试距离计算
|
||
TEST_F(BasicTypesTest, DistanceCalculation) {
|
||
TestMovingObject obj;
|
||
|
||
// 测试经度方向的距离(约90米)
|
||
GeoPosition pos1{36.0, 120.0};
|
||
GeoPosition pos2{36.0, 120.001};
|
||
double distance1 = obj.calculateDistance(pos1, pos2);
|
||
EXPECT_NEAR(distance1, 90.0, 1.0);
|
||
|
||
// 测试纬度方向的距离(约22米)
|
||
GeoPosition pos3{36.0, 120.0};
|
||
GeoPosition pos4{36.0002, 120.0};
|
||
double distance2 = obj.calculateDistance(pos3, pos4);
|
||
EXPECT_NEAR(distance2, 22.0, 1.0);
|
||
}
|