From ffff35a5970f43bd3c0239a21cd8ec23559ddf90 Mon Sep 17 00:00:00 2001 From: Matthias Grob Date: Thu, 26 Dec 2019 12:10:56 +0100 Subject: [PATCH] Unit tests: use EXPECT_FLOAT_EQ for float comparisons as precaution to prevent failing tests because of small potential rounding errors on certain platforms or compilares. --- .../CollisionPreventionTest.cpp | 8 ++-- .../Utility/ManualVelocitySmoothingXYTest.cpp | 6 +-- .../tasks/Utility/VelocitySmoothingTest.cpp | 8 ++-- src/lib/parameters/ParameterTest.cpp | 8 ++-- .../PositionControl/PositionControlTest.cpp | 46 +++++++++---------- .../mc_pos_control/Takeoff/TakeoffTest.cpp | 10 ++-- 6 files changed, 43 insertions(+), 43 deletions(-) diff --git a/src/lib/collision_prevention/CollisionPreventionTest.cpp b/src/lib/collision_prevention/CollisionPreventionTest.cpp index e92f3fdc38..fd68b0ea0d 100644 --- a/src/lib/collision_prevention/CollisionPreventionTest.cpp +++ b/src/lib/collision_prevention/CollisionPreventionTest.cpp @@ -313,7 +313,7 @@ TEST_F(CollisionPreventionTest, testPurgeOldData) if (i < 6) { // THEN: If the data is new enough, the velocity setpoint should stay the same as the input // Note: direction will change slightly due to guidance - EXPECT_EQ(original_setpoint.norm(), modified_setpoint.norm()); + EXPECT_FLOAT_EQ(original_setpoint.norm(), modified_setpoint.norm()); } else { // THEN: If the data is expired, the velocity setpoint should be cut down to zero because there is no data @@ -381,7 +381,7 @@ TEST_F(CollisionPreventionTest, testNoRangeData) if (i < 5) { // THEN: If the data is new enough, the velocity setpoint should stay the same as the input // Note: direction will change slightly due to guidance - EXPECT_EQ(original_setpoint.norm(), modified_setpoint.norm()); + EXPECT_FLOAT_EQ(original_setpoint.norm(), modified_setpoint.norm()); } else { // THEN: If the data is expired, the velocity setpoint should be cut down to zero because there is no data @@ -1008,8 +1008,8 @@ TEST_F(CollisionPreventionTest, adaptSetpointDirection_flat_minimum) //THEN: the setpoint direction should be modified correctly EXPECT_EQ(sp_index, 2); - EXPECT_FLOAT_EQ(setpoint_dir(0), 0.93969262); - EXPECT_FLOAT_EQ(setpoint_dir(1), 0.34202012); + EXPECT_FLOAT_EQ(setpoint_dir(0), 0.93969262f); + EXPECT_FLOAT_EQ(setpoint_dir(1), 0.34202012f); } TEST_F(CollisionPreventionTest, overlappingSensors) diff --git a/src/lib/flight_tasks/tasks/Utility/ManualVelocitySmoothingXYTest.cpp b/src/lib/flight_tasks/tasks/Utility/ManualVelocitySmoothingXYTest.cpp index b2eb242ff4..e66218377b 100644 --- a/src/lib/flight_tasks/tasks/Utility/ManualVelocitySmoothingXYTest.cpp +++ b/src/lib/flight_tasks/tasks/Utility/ManualVelocitySmoothingXYTest.cpp @@ -58,9 +58,9 @@ TEST_F(ManualVelocitySmoothingXYTest, setGet) _smoothing.setMaxVel(5.f); // THEN: We should be able to get them back - EXPECT_EQ(_smoothing.getMaxJerk(), 11.f); - EXPECT_EQ(_smoothing.getMaxAccel(), 7.f); - EXPECT_EQ(_smoothing.getMaxVel(), 5.f); + EXPECT_FLOAT_EQ(_smoothing.getMaxJerk(), 11.f); + EXPECT_FLOAT_EQ(_smoothing.getMaxAccel(), 7.f); + EXPECT_FLOAT_EQ(_smoothing.getMaxVel(), 5.f); } TEST_F(ManualVelocitySmoothingXYTest, getCurrentState) diff --git a/src/lib/flight_tasks/tasks/Utility/VelocitySmoothingTest.cpp b/src/lib/flight_tasks/tasks/Utility/VelocitySmoothingTest.cpp index a81028ff88..2bf2b0e983 100644 --- a/src/lib/flight_tasks/tasks/Utility/VelocitySmoothingTest.cpp +++ b/src/lib/flight_tasks/tasks/Utility/VelocitySmoothingTest.cpp @@ -172,9 +172,9 @@ TEST_F(VelocitySmoothingTest, testZeroSetpoint) // THEN: All the trajectories should still be zero for (int i = 0; i < 3; i++) { - EXPECT_EQ(_trajectories[i].getCurrentJerk(), 0.f); - EXPECT_EQ(_trajectories[i].getCurrentAcceleration(), 0.f); - EXPECT_EQ(_trajectories[i].getCurrentVelocity(), 0.f); - EXPECT_EQ(_trajectories[i].getCurrentPosition(), 0.f); + EXPECT_FLOAT_EQ(_trajectories[i].getCurrentJerk(), 0.f); + EXPECT_FLOAT_EQ(_trajectories[i].getCurrentAcceleration(), 0.f); + EXPECT_FLOAT_EQ(_trajectories[i].getCurrentVelocity(), 0.f); + EXPECT_FLOAT_EQ(_trajectories[i].getCurrentPosition(), 0.f); } } diff --git a/src/lib/parameters/ParameterTest.cpp b/src/lib/parameters/ParameterTest.cpp index dc1376239e..4e5ff3231e 100644 --- a/src/lib/parameters/ParameterTest.cpp +++ b/src/lib/parameters/ParameterTest.cpp @@ -59,7 +59,7 @@ TEST_F(ParameterTest, testParamReadWrite) // THEN it should be successful and have the default value EXPECT_EQ(0, status); - EXPECT_EQ(-1, value); + EXPECT_FLOAT_EQ(-1.f, value); // WHEN: we set the parameter value = 42.f; @@ -74,7 +74,7 @@ TEST_F(ParameterTest, testParamReadWrite) // THEN: it should be exactly the value we set EXPECT_EQ(0, status); - EXPECT_EQ(42.f, value2); + EXPECT_FLOAT_EQ(42.f, value2); } @@ -99,8 +99,8 @@ TEST_F(ParameterTest, testUorbSendReceive) // AND: the values we got should be the same EXPECT_EQ(message.timestamp, obstacle_distance.timestamp); - EXPECT_EQ(message.min_distance, obstacle_distance.min_distance); - EXPECT_EQ(message.max_distance, obstacle_distance.max_distance); + EXPECT_FLOAT_EQ(message.min_distance, obstacle_distance.min_distance); + EXPECT_FLOAT_EQ(message.max_distance, obstacle_distance.max_distance); // AND: all the bytes should be equal EXPECT_EQ(0, memcmp(&message, &obstacle_distance, sizeof(message))); diff --git a/src/modules/mc_pos_control/PositionControl/PositionControlTest.cpp b/src/modules/mc_pos_control/PositionControl/PositionControlTest.cpp index 10c3d228c1..1ba6372684 100644 --- a/src/modules/mc_pos_control/PositionControl/PositionControlTest.cpp +++ b/src/modules/mc_pos_control/PositionControl/PositionControlTest.cpp @@ -43,24 +43,24 @@ TEST(PositionControlTest, EmptySetpoint) vehicle_local_position_setpoint_s output_setpoint{}; position_control.getLocalPositionSetpoint(output_setpoint); - EXPECT_EQ(output_setpoint.x, 0.f); - EXPECT_EQ(output_setpoint.y, 0.f); - EXPECT_EQ(output_setpoint.z, 0.f); - EXPECT_EQ(output_setpoint.yaw, 0.f); - EXPECT_EQ(output_setpoint.yawspeed, 0.f); - EXPECT_EQ(output_setpoint.vx, 0.f); - EXPECT_EQ(output_setpoint.vy, 0.f); - EXPECT_EQ(output_setpoint.vz, 0.f); + EXPECT_FLOAT_EQ(output_setpoint.x, 0.f); + EXPECT_FLOAT_EQ(output_setpoint.y, 0.f); + EXPECT_FLOAT_EQ(output_setpoint.z, 0.f); + EXPECT_FLOAT_EQ(output_setpoint.yaw, 0.f); + EXPECT_FLOAT_EQ(output_setpoint.yawspeed, 0.f); + EXPECT_FLOAT_EQ(output_setpoint.vx, 0.f); + EXPECT_FLOAT_EQ(output_setpoint.vy, 0.f); + EXPECT_FLOAT_EQ(output_setpoint.vz, 0.f); EXPECT_EQ(Vector3f(output_setpoint.acceleration), Vector3f(0.f, 0.f, 0.f)); EXPECT_EQ(Vector3f(output_setpoint.jerk), Vector3f(0.f, 0.f, 0.f)); EXPECT_EQ(Vector3f(output_setpoint.thrust), Vector3f(0, 0, 0)); vehicle_attitude_setpoint_s attitude{}; position_control.getAttitudeSetpoint(attitude); - EXPECT_EQ(attitude.roll_body, 0.f); - EXPECT_EQ(attitude.pitch_body, 0.f); - EXPECT_EQ(attitude.yaw_body, 0.f); - EXPECT_EQ(attitude.yaw_sp_move_rate, 0.f); + EXPECT_FLOAT_EQ(attitude.roll_body, 0.f); + EXPECT_FLOAT_EQ(attitude.pitch_body, 0.f); + EXPECT_FLOAT_EQ(attitude.yaw_body, 0.f); + EXPECT_FLOAT_EQ(attitude.yaw_sp_move_rate, 0.f); EXPECT_EQ(Quatf(attitude.q_d), Quatf(1.f, 0.f, 0.f, 0.f)); //EXPECT_EQ(attitude.q_d_valid, false); // TODO should not be true when there was no control EXPECT_EQ(Vector3f(attitude.thrust_body), Vector3f(0.f, 0.f, 0.f)); @@ -68,7 +68,7 @@ TEST(PositionControlTest, EmptySetpoint) EXPECT_EQ(attitude.pitch_reset_integral, false); EXPECT_EQ(attitude.yaw_reset_integral, false); EXPECT_EQ(attitude.fw_control_yaw, false); - EXPECT_EQ(attitude.apply_flaps, 0.f);//vehicle_attitude_setpoint_s::FLAPS_OFF); // TODO why no reference? + EXPECT_FLOAT_EQ(attitude.apply_flaps, 0.f);//vehicle_attitude_setpoint_s::FLAPS_OFF); // TODO why no reference? } class PositionControlBasicTest : public ::testing::Test @@ -190,8 +190,8 @@ TEST_F(PositionControlBasicTest, ThrustLimit) _input_setpoint.z = -10.f; runController(); - EXPECT_EQ(_attitude.thrust_body[0], 0.f); - EXPECT_EQ(_attitude.thrust_body[1], 0.f); + EXPECT_FLOAT_EQ(_attitude.thrust_body[0], 0.f); + EXPECT_FLOAT_EQ(_attitude.thrust_body[1], 0.f); EXPECT_LT(_attitude.thrust_body[2], -.1f); EXPECT_GE(_attitude.thrust_body[2], -0.9f); } @@ -202,8 +202,8 @@ TEST_F(PositionControlBasicTest, FailsafeInput) _input_setpoint.acceleration[0] = _input_setpoint.acceleration[1] = 0.f; runController(); - EXPECT_EQ(_attitude.thrust_body[0], 0.f); - EXPECT_EQ(_attitude.thrust_body[1], 0.f); + EXPECT_FLOAT_EQ(_attitude.thrust_body[0], 0.f); + EXPECT_FLOAT_EQ(_attitude.thrust_body[1], 0.f); EXPECT_LT(_output_setpoint.thrust[2], -.1f); EXPECT_GT(_output_setpoint.thrust[2], -.5f); EXPECT_GT(_attitude.thrust_body[2], -.5f); @@ -217,9 +217,9 @@ TEST_F(PositionControlBasicTest, InputCombinationsPosition) _input_setpoint.z = .3f; runController(); - EXPECT_EQ(_output_setpoint.x, .1f); - EXPECT_EQ(_output_setpoint.y, .2f); - EXPECT_EQ(_output_setpoint.z, .3f); + EXPECT_FLOAT_EQ(_output_setpoint.x, .1f); + EXPECT_FLOAT_EQ(_output_setpoint.y, .2f); + EXPECT_FLOAT_EQ(_output_setpoint.z, .3f); EXPECT_FALSE(isnan(_output_setpoint.vx)); EXPECT_FALSE(isnan(_output_setpoint.vy)); EXPECT_FALSE(isnan(_output_setpoint.vz)); @@ -237,9 +237,9 @@ TEST_F(PositionControlBasicTest, InputCombinationsPositionVelocity) runController(); // EXPECT_TRUE(isnan(_output_setpoint.x)); // EXPECT_TRUE(isnan(_output_setpoint.y)); - EXPECT_EQ(_output_setpoint.z, .3f); - EXPECT_EQ(_output_setpoint.vx, .1f); - EXPECT_EQ(_output_setpoint.vy, .2f); + EXPECT_FLOAT_EQ(_output_setpoint.z, .3f); + EXPECT_FLOAT_EQ(_output_setpoint.vx, .1f); + EXPECT_FLOAT_EQ(_output_setpoint.vy, .2f); EXPECT_FALSE(isnan(_output_setpoint.vz)); EXPECT_FALSE(isnan(_output_setpoint.thrust[0])); EXPECT_FALSE(isnan(_output_setpoint.thrust[1])); diff --git a/src/modules/mc_pos_control/Takeoff/TakeoffTest.cpp b/src/modules/mc_pos_control/Takeoff/TakeoffTest.cpp index d8025825a5..6666ba7d88 100644 --- a/src/modules/mc_pos_control/Takeoff/TakeoffTest.cpp +++ b/src/modules/mc_pos_control/Takeoff/TakeoffTest.cpp @@ -66,11 +66,11 @@ TEST(TakeoffTest, RegularTakeoffRamp) // armed, not landed, want takeoff, ramping up takeoff.updateTakeoffState(true, false, true, 1.f, false, 4_s); - EXPECT_EQ(takeoff.updateRamp(.5f, 1.5f), 0.f); - EXPECT_EQ(takeoff.updateRamp(.5f, 1.5f), .5f); - EXPECT_EQ(takeoff.updateRamp(.5f, 1.5f), 1.f); - EXPECT_EQ(takeoff.updateRamp(.5f, 1.5f), 1.5f); - EXPECT_EQ(takeoff.updateRamp(.5f, 1.5f), 1.5f); + EXPECT_FLOAT_EQ(takeoff.updateRamp(.5f, 1.5f), 0.f); + EXPECT_FLOAT_EQ(takeoff.updateRamp(.5f, 1.5f), .5f); + EXPECT_FLOAT_EQ(takeoff.updateRamp(.5f, 1.5f), 1.f); + EXPECT_FLOAT_EQ(takeoff.updateRamp(.5f, 1.5f), 1.5f); + EXPECT_FLOAT_EQ(takeoff.updateRamp(.5f, 1.5f), 1.5f); // armed, not landed, want takeoff, rampup time passed takeoff.updateTakeoffState(true, false, true, 1.f, false, 6500_ms);