From cd5fcd5c6cebbaa5df3934008a526ee8a9d608b4 Mon Sep 17 00:00:00 2001 From: Josiah VanderZee Date: Sat, 29 Mar 2025 08:03:05 -0500 Subject: [PATCH] Make changes requested by sfan5 * Use `float` overload of `std::fabs` to not necessitate promotion. * Word test descriptions to describe intent instead of method. --- src/unittest/test_collision.cpp | 82 ++++++++++++++------------------- 1 file changed, 35 insertions(+), 47 deletions(-) diff --git a/src/unittest/test_collision.cpp b/src/unittest/test_collision.cpp index 46f1bf7cb..c7dbb6b02 100644 --- a/src/unittest/test_collision.cpp +++ b/src/unittest/test_collision.cpp @@ -11,7 +11,7 @@ #include "irrlichttypes.h" -const double EPSILON = 0.001; +const float EPSILON = 0.001f; class TestCollision : public TestBase { public: @@ -78,19 +78,17 @@ TEST_CASE("Test axis aligned collision with unit cube.", "[collision]") // The following set of tests is for boxes translated in the -X direction // from the static cube, possibly with additional offsets. - SECTION("Given a unit cube translated by -2 units on the x-axis, " - "when it moves 1 unit per step in the +x direction for 1 step, " - "then it should collide on the X axis within epsilon of 1 step.") + SECTION("When the box moves towards the target on the X axis, " + "then they should collide on the X axis.") { aabb3f m{bx-2.0f, by, bz, bx-1.0f, by+1.0f, bz+1.0f}; f32 dtime = 1.0f; CHECK(0 == axisAlignedCollision(s, m, v3f{1.0f, 0.0f, 0.0f}, &dtime)); - CHECK(std::fabs(static_cast(dtime) - 1.0) < EPSILON); + CHECK(std::fabs(dtime - 1.0f) < EPSILON); } - SECTION("Given a unit cube translated by -2 units on the x-axis, " - "when it moves 1 unit per step in the -x direction for 1 step, " - "then it should never collide.") + SECTION("When the box moves away from the target on the X axis, " + "then they should never collide.") { aabb3f m{bx-2.0f, by, bz, bx-1.0f, by+1.0f, bz+1.0f}; f32 dtime = 1.0f; @@ -98,86 +96,76 @@ TEST_CASE("Test axis aligned collision with unit cube.", "[collision]") } - SECTION("Given a unit cube translated by -2 units on the x-axis " - "and 1.5 units on the y-axis, " - "when it moves 1 unit per step in the +x direction for 1 step, " - "then it should never collide.") + SECTION("Given the box and the target do not overlap on the X or Y axes, " + "when the box moves away on the X axis, " + "then they should never collide." { aabb3f m{bx-2.0f, by+1.5f, bz, bx-1.0f, by+2.5f, bz+1.0f}; f32 dtime = 1.0f; CHECK(-1 == axisAlignedCollision(s, m, v3f{1.0f, 0.0f, 0.0f}, &dtime)); } - SECTION("Given a 0.5x2x1 cube translated by -2 units on the x-axis " - "and -1.5 units on the y-axis, " - "when it moves 0.5 units per step in the +x direction " - "and 0.1 units per step in the +y direction for 3 steps, " - "then it should collide on the X axis within epsilon of 3 steps.") + SECTION("Given the box and the target do not overlap on the X or Y axes, " + "when the box moves at the right speeds on the X and Y axes, " + "then they should collide on the X axis.") { aabb3f m{bx-2.0f, by-1.5f, bz, bx-1.5f, by+0.5f, bz+1.0f}; f32 dtime = 3.0f; CHECK(0 == axisAlignedCollision(s, m, v3f{0.5f, 0.1f, 0}, &dtime)); - CHECK(std::fabs(static_cast(dtime) - 3.0) < EPSILON); + CHECK(std::fabs(dtime - 3.0f) < EPSILON); } // The following set of tests is for boxes translated in the +X direction // from the static cube, possibly with additional offsets. They are not // all mirror images of the tests for the -X direction. - SECTION("Given a unit cube translated by +2 units on the x-axis, " - "when it moves 1 unit per step in the -x direction for 1 step, " - "then it should collide on the X axis within epsilon of 1 step.") + SECTION("When the box moves towards the target on the X axis, " + "then they should collide on the X axis.") { aabb3f m{bx+2.0f, by, bz, bx+3.0f, by+1.0f, bz+1.0f}; f32 dtime = 1.0f; CHECK(0 == axisAlignedCollision(s, m, v3f{-1.0f, 0.0f, 0.0f}, &dtime)); - CHECK(std::fabs(static_cast(dtime) - 1.0) < EPSILON); + CHECK(std::fabs(dtime - 1.0f) < EPSILON); } - SECTION("Given a unit cube translated by +2 units on the x-axis, " - "when it moves 1 unit per step in the +x direction for 1 step, " - "then it should never collide.") + SECTION("When the box moves away from the target on the X axis, " + "then they should never collide.") { aabb3f m{bx+2.0f, by, bz, bx+3.0f, by+1.0f, bz+1.0f}; f32 dtime = 1.0f; CHECK(-1 == axisAlignedCollision(s, m, v3f{1.0f, 0.0f, 0.0f}, &dtime)); } - SECTION("Given a unit cube translated by +2 units on the x-axis " - "and 1.5 units on the z-axis, " - "when it moves 1 unit per step in the -x direction for 1 step, " - "then it should never collide.") + SECTION("Given the box and the target do not overlap on the X or Z axes, " + "when the box moves away on the X axis, " + "then they should never collide." { aabb3f m{bx+2.0f, by, bz+1.5f, bx+3.0f, by+1.0f, bz+3.5f}; f32 dtime = 1.0f; CHECK(-1 == axisAlignedCollision(s, m, v3f{-1.0f, 0.0f, 0.0f}, &dtime)); } - SECTION("Given a 0.5x1x1 cube translated by +2 units on the x-axis " - "and -1.5 units on the y-axis, " - "when it moves 0.5 units per step in the -x direction " - "and 0.2 units per step in the +y direction for 3 steps, " - "then it should collide on the Y axis within epsilon of 2.5 steps.") + SECTION("Given the box and the target do not overlap on the X or Y axes, " + "when the box moves at the right speeds on the X and Y axes, " + "then they should collide on the Y axis.") { // This test is interesting because the Y-faces are the first to collide. aabb3f m{bx+2.0f, by-1.5f, bz, bx+2.5f, by-0.5f, bz+1.0f}; f32 dtime = 2.51f; // y velocity is 0.200000003 precisely CHECK(1 == axisAlignedCollision(s, m, v3f{-0.5f, 0.2f, 0}, &dtime)); - CHECK(std::fabs(static_cast(dtime) - 2.5) < EPSILON); + CHECK(std::fabs(dtime - 2.5f) < EPSILON); } - SECTION("Given a 0.5x1x1 cube translated by +2 units on the x-axis " - "and -1.5 units on the y-axis, " - "when it moves 0.5 units per step in the -x direction " - "and 0.3 units per step in the +y direction for 3 steps, " - "then it should collide on the X axis within epsilon of 2.0 steps.") + SECTION("Given the box and the target do not overlap on the X or Y axes, " + "when the box moves at the right speeds on the X and Y axes, " + "then they should collide on the X axis.") { aabb3f m{bx+2.0f, by-1.5f, bz, bx+2.5f, by-0.5f, bz+1.0f}; f32 dtime = 2.1f; // y velocity is 0.300000012 precisely CHECK(0 == axisAlignedCollision(s, m, v3f{-0.5f, 0.3f, 0}, &dtime)); - CHECK(std::fabs(static_cast(dtime) - 2.0) < EPSILON); + CHECK(std::fabs(dtime - 2.0f) < EPSILON); } } @@ -201,7 +189,7 @@ TEST_CASE("Test axis aligned collision with 2x2x2 cube.", "[collision]") v3f v{-1.0f/3.0f, -1.0f/3.0f, -1.0/3.0f}; f32 dtime = 1.0f; CHECK(0 == axisAlignedCollision(s, m, v, &dtime)); - CHECK(std::fabs(static_cast(dtime) - 0.9) < EPSILON); + CHECK(std::fabs(dtime - 0.9f) < EPSILON); } SECTION("Collides on Y axis near (+X,+Y,+Z) corner.") @@ -210,7 +198,7 @@ TEST_CASE("Test axis aligned collision with 2x2x2 cube.", "[collision]") v3f v{-1.0f/3.0f, -1.0f/3.0f, -1.0/3.0f}; f32 dtime = 1.0f; CHECK(1 == axisAlignedCollision(s, m, v, &dtime)); - CHECK(std::fabs(static_cast(dtime) - 0.9) < EPSILON); + CHECK(std::fabs(dtime - 0.9f) < EPSILON); } SECTION("Collides on Z axis near (+X,+Y,+Z) corner.") @@ -219,7 +207,7 @@ TEST_CASE("Test axis aligned collision with 2x2x2 cube.", "[collision]") v3f v{-1.0f/3.0f, -1.0f/3.0f, -1.0/3.0f}; f32 dtime = 1.0f; CHECK(2 == axisAlignedCollision(s, m, v, &dtime)); - CHECK(std::fabs(static_cast(dtime) - 0.9) < EPSILON); + CHECK(std::fabs(dtime - 0.9f) < EPSILON); } SECTION("Collides on X axis near (-X,-Y,-Z) corner.") @@ -228,7 +216,7 @@ TEST_CASE("Test axis aligned collision with 2x2x2 cube.", "[collision]") v3f v{1.0f/7.0f, 1.0f/7.0f, 1.0/7.0f}; f32 dtime = 17.0f; CHECK(0 == axisAlignedCollision(s, m, v, &dtime)); - CHECK(std::fabs(static_cast(dtime) - 16.1) < EPSILON); + CHECK(std::fabs(dtime - 16.1f) < EPSILON); } SECTION("Collides on Y axis near (-X,-Y,-Z) corner.") @@ -237,7 +225,7 @@ TEST_CASE("Test axis aligned collision with 2x2x2 cube.", "[collision]") v3f v{1.0f/7.0f, 1.0f/7.0f, 1.0/7.0f}; f32 dtime = 17.0f; CHECK(1 == axisAlignedCollision(s, m, v, &dtime)); - CHECK(std::fabs(static_cast(dtime) - 16.1) < EPSILON); + CHECK(std::fabs(dtime - 16.1f) < EPSILON); } SECTION("Collides on Z axis near (-X,-Y,-Z) corner.") @@ -246,7 +234,7 @@ TEST_CASE("Test axis aligned collision with 2x2x2 cube.", "[collision]") v3f v{1.0f/7.0f, 1.0f/7.0f, 1.0/7.0f}; f32 dtime = 17.0f; CHECK(2 == axisAlignedCollision(s, m, v, &dtime)); - CHECK(std::fabs(static_cast(dtime) - 16.1) < EPSILON); + CHECK(std::fabs(dtime - 16.1f) < EPSILON); } }