From 15e4fe14534bf99bb22c4720d482333db588f577 Mon Sep 17 00:00:00 2001 From: Gawaboumga Date: Wed, 30 Dec 2015 15:38:52 +0100 Subject: [PATCH] New tests ! Former-commit-id: 1cb5949ce7b9fde74189a37c75f0b3380fda95c7 --- tests/Engine/Math/Algorithm.cpp | 83 ++++++++++++++++++++++++---- tests/Engine/Math/BoundingVolume.cpp | 15 +++++ tests/Engine/Math/Box.cpp | 12 ++++ tests/Engine/Math/EulerAngles.cpp | 25 ++++++++- tests/Engine/Math/Frustum.cpp | 13 +++++ tests/Engine/Math/Matrix4.cpp | 40 +++++++++----- tests/Engine/Math/OrientedBox.cpp | 15 +++++ tests/Engine/Math/Plane.cpp | 12 ++++ tests/Engine/Math/Quaternion.cpp | 29 ++++++++++ tests/Engine/Math/Ray.cpp | 29 +++++++++- tests/Engine/Math/Rect.cpp | 12 ++++ tests/Engine/Math/Sphere.cpp | 40 ++++++++++++++ tests/Engine/Math/Vector2.cpp | 51 ++++++++++++++++- tests/Engine/Math/Vector3.cpp | 46 ++++++++++++++- tests/Engine/Math/Vector4.cpp | 10 ++++ 15 files changed, 399 insertions(+), 33 deletions(-) diff --git a/tests/Engine/Math/Algorithm.cpp b/tests/Engine/Math/Algorithm.cpp index 38080c576..c42b90d11 100644 --- a/tests/Engine/Math/Algorithm.cpp +++ b/tests/Engine/Math/Algorithm.cpp @@ -1,7 +1,7 @@ #include #include -TEST_CASE("Approach", "[MATH][ALGORITHM]" ) +TEST_CASE("Approach", "[MATH][ALGORITHM]") { SECTION("Approach 8 with 5 by 2") { @@ -19,7 +19,7 @@ TEST_CASE("Approach", "[MATH][ALGORITHM]" ) } } -TEST_CASE("Clamp", "[ALGORITHM]" ) +TEST_CASE("Clamp", "[ALGORITHM]") { SECTION("Clamp 8 between 5 and 10") { @@ -37,7 +37,20 @@ TEST_CASE("Clamp", "[ALGORITHM]" ) } } -TEST_CASE("DegreeToRadian", "[ALGORITHM]" ) +TEST_CASE("CountBits", "[ALGORITHM]") +{ + SECTION("Number 10 has 2 bits set to 1") + { + REQUIRE(Nz::CountBits(10) == 2); + } + + SECTION("Number 0 has 0 bit set to 1") + { + REQUIRE(Nz::CountBits(0) == 0); + } +} + +TEST_CASE("DegreeToRadian", "[ALGORITHM]") { SECTION("Convert 45.f degree to radian") { @@ -45,8 +58,13 @@ TEST_CASE("DegreeToRadian", "[ALGORITHM]" ) } } -TEST_CASE("GetNearestPowerOfTwo", "[ALGORITHM]" ) +TEST_CASE("GetNearestPowerOfTwo", "[ALGORITHM]") { + SECTION("Nearest power of two of 0 = 1") + { + REQUIRE(Nz::GetNearestPowerOfTwo(0) == 1); + } + SECTION("Nearest power of two of 16 = 16") { REQUIRE(Nz::GetNearestPowerOfTwo(16) == 16); @@ -58,7 +76,7 @@ TEST_CASE("GetNearestPowerOfTwo", "[ALGORITHM]" ) } } -TEST_CASE("GetNumberLength", "[ALGORITHM]" ) +TEST_CASE("GetNumberLength", "[ALGORITHM]") { SECTION("GetNumberLength of -127 signed char") { @@ -115,7 +133,48 @@ TEST_CASE("GetNumberLength", "[ALGORITHM]" ) } } -TEST_CASE("IntegralPow", "[ALGORITHM]" ) +TEST_CASE("IntegralLog2", "[ALGORITHM]") +{ + SECTION("According to implementation, log in base 2 of 0 = 0") + { + REQUIRE(Nz::IntegralLog2(0) == 0); + } + + SECTION("Log in base 2 of 1 = 0") + { + REQUIRE(Nz::IntegralLog2(1) == 0); + } + + SECTION("Log in base 2 of 4 = 2") + { + REQUIRE(Nz::IntegralLog2(4) == 2); + } + + SECTION("Log in base 2 of 5 = 2") + { + REQUIRE(Nz::IntegralLog2(5) == 2); + } +} + +TEST_CASE("IntegralLog2Pot", "[ALGORITHM]") +{ + SECTION("According to implementation, log in base 2 of 0 = 0") + { + REQUIRE(Nz::IntegralLog2Pot(0) == 0); + } + + SECTION("Log in base 2 of 1 = 0") + { + REQUIRE(Nz::IntegralLog2Pot(1) == 0); + } + + SECTION("Log in base 2 of 4 = 2") + { + REQUIRE(Nz::IntegralLog2Pot(4) == 2); + } +} + +TEST_CASE("IntegralPow", "[ALGORITHM]") { SECTION("2 to power 4") { @@ -123,7 +182,7 @@ TEST_CASE("IntegralPow", "[ALGORITHM]" ) } } -TEST_CASE("Lerp", "[ALGORITHM]" ) +TEST_CASE("Lerp", "[ALGORITHM]") { SECTION("Lerp 2 to 6 with 0.5") { @@ -131,7 +190,7 @@ TEST_CASE("Lerp", "[ALGORITHM]" ) } } -TEST_CASE("MultiplyAdd", "[ALGORITHM]" ) +TEST_CASE("MultiplyAdd", "[ALGORITHM]") { SECTION("2 * 3 + 1") { @@ -139,7 +198,7 @@ TEST_CASE("MultiplyAdd", "[ALGORITHM]" ) } } -TEST_CASE("NumberEquals", "[ALGORITHM]" ) +TEST_CASE("NumberEquals", "[ALGORITHM]") { SECTION("2.35 and 2.351 should be the same at 0.01") { @@ -152,7 +211,7 @@ TEST_CASE("NumberEquals", "[ALGORITHM]" ) } } -TEST_CASE("NumberToString", "[ALGORITHM]" ) +TEST_CASE("NumberToString", "[ALGORITHM]") { SECTION("235 to string") { @@ -170,7 +229,7 @@ TEST_CASE("NumberToString", "[ALGORITHM]" ) } } -TEST_CASE("RadianToDegree", "[ALGORITHM]" ) +TEST_CASE("RadianToDegree", "[ALGORITHM]") { SECTION("PI / 4 to degree") { @@ -178,7 +237,7 @@ TEST_CASE("RadianToDegree", "[ALGORITHM]" ) } } -TEST_CASE("StringToNumber", "[ALGORITHM]" ) +TEST_CASE("StringToNumber", "[ALGORITHM]") { SECTION("235 in string") { diff --git a/tests/Engine/Math/BoundingVolume.cpp b/tests/Engine/Math/BoundingVolume.cpp index 05af375bc..9fd552863 100644 --- a/tests/Engine/Math/BoundingVolume.cpp +++ b/tests/Engine/Math/BoundingVolume.cpp @@ -99,5 +99,20 @@ SCENARIO("BoundingVolume", "[MATH][BOUNDINGVOLUME]") REQUIRE(firstCenterAndUnit.aabb != secondCenterAndUnit.aabb); } } + + WHEN("We try to lerp") + { + THEN("Compilation should be fine") + { + Nz::BoundingVolumef nullBoundingVolume = Nz::BoundingVolumef(Nz::Vector3f::Zero(), Nz::Vector3f::Zero()); + Nz::BoundingVolumef centerAndUnit = firstCenterAndUnit; + nullBoundingVolume.Update(Nz::Matrix4f::Identity()); + centerAndUnit.Update(Nz::Matrix4f::Identity()); + Nz::BoundingVolumef result(Nz::Vector3f::Zero(), Nz::Vector3f::Unit() * 0.5f); + result.Update(Nz::Matrix4f::Identity()); + + REQUIRE(Nz::BoundingVolumef::Lerp(nullBoundingVolume, centerAndUnit, 0.5f) == result); + } + } } } diff --git a/tests/Engine/Math/Box.cpp b/tests/Engine/Math/Box.cpp index e39e0654c..7d9b42a51 100644 --- a/tests/Engine/Math/Box.cpp +++ b/tests/Engine/Math/Box.cpp @@ -64,6 +64,18 @@ SCENARIO("Box", "[MATH][BOX]") REQUIRE(tmp == firstCenterAndUnit); } } + + WHEN("We try to lerp") + { + THEN("Compilation should be fine") + { + Nz::Boxf nullBox = Nz::Boxf::Zero(); + Nz::Boxf centerAndUnit = firstCenterAndUnit; + Nz::Boxf result(Nz::Vector3f::Zero(), Nz::Vector3f::Unit() * 0.5f); + + REQUIRE(Nz::Boxf::Lerp(nullBox, centerAndUnit, 0.5f) == result); + } + } } GIVEN("Two wrong box (negative width, height and depth") diff --git a/tests/Engine/Math/EulerAngles.cpp b/tests/Engine/Math/EulerAngles.cpp index a44efb361..6a6f7f77e 100644 --- a/tests/Engine/Math/EulerAngles.cpp +++ b/tests/Engine/Math/EulerAngles.cpp @@ -15,8 +15,8 @@ SCENARIO("EulerAngles", "[MATH][EULERANGLES]") WHEN("We do some operations") { - Nz::EulerAnglesf euler90(90.f, 90.f, 90.f); - Nz::EulerAnglesf euler270(270.f, 270.f, 270.f); + Nz::EulerAnglesf euler90(Nz::FromDegrees(90.f), Nz::FromDegrees(90.f), Nz::FromDegrees(90.f)); + Nz::EulerAnglesf euler270(Nz::FromDegrees(270.f), Nz::FromDegrees(270.f), Nz::FromDegrees(270.f)); Nz::EulerAnglesf euler360 = euler90 + euler270; euler360.Normalize(); @@ -42,6 +42,27 @@ SCENARIO("EulerAngles", "[MATH][EULERANGLES]") } } + GIVEN("Three rotation of 90 on each axis") + { + Nz::EulerAnglesf euler90P(Nz::FromDegrees(90.f), 0.f, 0.f); + Nz::EulerAnglesf euler90Y(0.f, Nz::FromDegrees(90.f), 0.f); + Nz::EulerAnglesf euler90R(0.f, 0.f, Nz::FromDegrees(90.f)); + + WHEN("We transform the axis") + { + THEN("This is supposed to be left-handed") + { + Nz::Vector3f rotation90P = euler90P.ToQuaternion() * Nz::Vector3f::UnitY(); + Nz::Vector3f rotation90Y = euler90Y.ToQuaternion() * Nz::Vector3f::UnitZ(); + Nz::Vector3f rotation90R = euler90R.ToQuaternion() * Nz::Vector3f::UnitX(); + + REQUIRE(rotation90P == Nz::Vector3f::UnitZ()); + REQUIRE(rotation90Y == Nz::Vector3f::UnitX()); + REQUIRE(rotation90R == Nz::Vector3f::UnitY()); + } + } + } + GIVEN("Euler angles with rotation 45 on each axis") { WHEN("We convert to quaternion") diff --git a/tests/Engine/Math/Frustum.cpp b/tests/Engine/Math/Frustum.cpp index 98715940d..d9fedebdd 100644 --- a/tests/Engine/Math/Frustum.cpp +++ b/tests/Engine/Math/Frustum.cpp @@ -78,5 +78,18 @@ SCENARIO("Frustum", "[MATH][FRUSTUM]") CHECK(frustum.Contains(&tmp, 1)); } } + + WHEN("We test for edge cases") + { + THEN("Implementation defined these") + { + Nz::BoundingVolumef nullVolume = Nz::BoundingVolumef::Null(); + CHECK(!frustum.Contains(nullVolume)); + Nz::BoundingVolumef infiniteVolume = Nz::BoundingVolumef::Infinite(); + CHECK(frustum.Contains(infiniteVolume)); + REQUIRE(frustum.Intersect(nullVolume) == Nz::IntersectionSide_Outside); + REQUIRE(frustum.Intersect(infiniteVolume) == Nz::IntersectionSide_Intersecting); + } + } } } diff --git a/tests/Engine/Math/Matrix4.cpp b/tests/Engine/Math/Matrix4.cpp index 24a56658b..eec10aacd 100644 --- a/tests/Engine/Math/Matrix4.cpp +++ b/tests/Engine/Math/Matrix4.cpp @@ -37,19 +37,29 @@ SCENARIO("Matrix4", "[MATH][Matrix4]") REQUIRE(firstIdentity.Inverse() == secondIdentity.InverseAffine()); } } + + WHEN("We transpose one of this matrix") + { + THEN("Identity transposed is the same than identity") + { + Nz::Matrix4f transposedIdentity; + firstIdentity.GetTransposed(&transposedIdentity); + REQUIRE(firstIdentity == transposedIdentity); + } + } } GIVEN("Two different matrix") { Nz::Matrix4f matrix1(1.0f, 0.0f, 0.0f, 0.0f, - 7.0f, 2.0f, 0.0f, 0.0f, - 1.0f, 5.0f, 3.0f, 0.0f, - 8.0f, 9.0f, 2.0f, 4.0f); + 7.0f, 2.0f, 0.0f, 0.0f, + 1.0f, 5.0f, 3.0f, 0.0f, + 8.0f, 9.0f, 2.0f, 4.0f); Nz::Matrix4f matrix2(1.0f, 1.0f, 2.0f, -1.0f, - -2.0f, -1.0f, -2.0f, 2.0f, - 4.0f, 2.0f, 5.0f, -4.0f, - 5.0f, -3.0f, -7.0f, -6.0f); + -2.0f, -1.0f, -2.0f, 2.0f, + 4.0f, 2.0f, 5.0f, -4.0f, + 5.0f, -3.0f, -7.0f, -6.0f); WHEN("We ask for determinant") { @@ -92,9 +102,9 @@ SCENARIO("Matrix4", "[MATH][Matrix4]") { transformedMatrix.MakeTransform(Nz::Vector3f::Zero(), Nz::EulerAnglesf(Nz::FromDegrees(45.f), 0.f, 0.f).ToQuaternion()); Nz::Matrix4f rotation45X(1.f, 0.f, 0.f, 0.f, - 0.f, std::sqrt(2.f) / 2.f, std::sqrt(2.f) / 2.f, 0.f, - 0.f, -std::sqrt(2.f) / 2.f, std::sqrt(2.f) / 2.f, 0.f, - 0.f, 0.f, 0.f, 1.f); + 0.f, std::sqrt(2.f) / 2.f, std::sqrt(2.f) / 2.f, 0.f, + 0.f, -std::sqrt(2.f) / 2.f, std::sqrt(2.f) / 2.f, 0.f, + 0.f, 0.f, 0.f, 1.f); REQUIRE(transformedMatrix == rotation45X); transformedMatrix.MakeTransform(Nz::Vector3f::Unit(), Nz::EulerAnglesf(Nz::FromDegrees(45.f), 0.f, 0.f).ToQuaternion()); @@ -106,9 +116,9 @@ SCENARIO("Matrix4", "[MATH][Matrix4]") { transformedMatrix.MakeTransform(Nz::Vector3f::Zero(), Nz::EulerAnglesf(0.f, Nz::FromDegrees(45.f), 0.f).ToQuaternion()); Nz::Matrix4f rotation45Y(std::sqrt(2.f) / 2.f, 0.f, -std::sqrt(2.f) / 2.f, 0.f, - 0.f, 1.f, 0.f, 0.f, - std::sqrt(2.f) / 2.f, 0.f, std::sqrt(2.f) / 2.f, 0.f, - 0.f, 0.f, 0.f, 1.f); + 0.f, 1.f, 0.f, 0.f, + std::sqrt(2.f) / 2.f, 0.f, std::sqrt(2.f) / 2.f, 0.f, + 0.f, 0.f, 0.f, 1.f); REQUIRE(transformedMatrix == rotation45Y); transformedMatrix.MakeTransform(Nz::Vector3f::Unit(), Nz::EulerAnglesf(0.f, Nz::FromDegrees(45.f), 0.f).ToQuaternion()); @@ -120,9 +130,9 @@ SCENARIO("Matrix4", "[MATH][Matrix4]") { transformedMatrix.MakeTransform(Nz::Vector3f::Zero(), Nz::EulerAnglesf(0.f, 0.f, Nz::FromDegrees(45.f)).ToQuaternion()); Nz::Matrix4f rotation45Z( std::sqrt(2.f) / 2.f, std::sqrt(2.f) / 2.f, 0.f, 0.f, - -std::sqrt(2.f) / 2.f, std::sqrt(2.f) / 2.f, 0.f, 0.f, - 0.f, 0.f, 1.f, 0.f, - 0.f, 0.f, 0.f, 1.f); + -std::sqrt(2.f) / 2.f, std::sqrt(2.f) / 2.f, 0.f, 0.f, + 0.f, 0.f, 1.f, 0.f, + 0.f, 0.f, 0.f, 1.f); REQUIRE(transformedMatrix == rotation45Z); transformedMatrix.MakeTransform(Nz::Vector3f::Unit(), Nz::EulerAnglesf(Nz::EulerAnglesf(0.f, 0.f, Nz::FromDegrees(45.f)).ToQuaternion())); diff --git a/tests/Engine/Math/OrientedBox.cpp b/tests/Engine/Math/OrientedBox.cpp index bd11f58ef..7a1035ee2 100644 --- a/tests/Engine/Math/OrientedBox.cpp +++ b/tests/Engine/Math/OrientedBox.cpp @@ -43,5 +43,20 @@ SCENARIO("OrientedBox", "[MATH][ORIENTEDBOX]") } } } + + WHEN("We try to lerp") + { + THEN("Compilation should be fine") + { + Nz::OrientedBoxf nullOrientedBox = Nz::OrientedBoxf::Zero(); + Nz::OrientedBoxf centerAndUnit = firstCenterAndUnit; + nullOrientedBox.Update(Nz::Matrix4f::Identity()); + centerAndUnit.Update(Nz::Matrix4f::Identity()); + Nz::OrientedBoxf result(Nz::Vector3f::Zero(), Nz::Vector3f::Unit() * 0.5f); + result.Update(Nz::Matrix4f::Identity()); + + REQUIRE(Nz::OrientedBoxf::Lerp(nullOrientedBox, centerAndUnit, 0.5f) == result); + } + } } } diff --git a/tests/Engine/Math/Plane.cpp b/tests/Engine/Math/Plane.cpp index 1cab6f385..44377bf67 100644 --- a/tests/Engine/Math/Plane.cpp +++ b/tests/Engine/Math/Plane.cpp @@ -49,6 +49,18 @@ SCENARIO("Plane", "[MATH][PLANE]") REQUIRE(Nz::Planef(-Nz::Vector3f::UnitY(), -1000.f).Distance(Nz::Vector3f::UnitY() * 1500.f) == Approx(-500.f)); } } + + WHEN("We try to lerp") + { + THEN("Compilation should be fine") + { + Nz::Planef planeXY = Nz::Planef::XY(); + Nz::Planef planeXZ = Nz::Planef::XZ(); + Nz::Vector3f result = Nz::Vector3f(0.f, 1.f, 1.f) * 0.5f; + result.Normalize(); + REQUIRE(Nz::Planef::Lerp(planeXY, planeXZ, 0.5f) == Nz::Planef(result, 0.f)); + } + } } GIVEN("The plane XZ, distance 1 with 3 points (0, 1, 0), (1, 1, 1), (-1, 1, 0)") diff --git a/tests/Engine/Math/Quaternion.cpp b/tests/Engine/Math/Quaternion.cpp index 65ab201bf..7ebf6ede5 100644 --- a/tests/Engine/Math/Quaternion.cpp +++ b/tests/Engine/Math/Quaternion.cpp @@ -32,6 +32,22 @@ SCENARIO("Quaternion", "[MATH][QUATERNION]") REQUIRE((firstQuaternion * Nz::Vector3f::UnitZ()) == -Nz::Vector3f::UnitZ()); } } + + WHEN("We invert or normalize Zero quaternion") + { + Nz::Quaternionf zero = Nz::Quaternionf::Zero(); + + THEN("It's meant not to be changed") + { + Nz::Quaternionf inverted = zero.GetInverse(); + float tmp = -1.f; + Nz::Quaternionf normalized = zero.GetNormal(&tmp); + + REQUIRE(inverted == zero); + REQUIRE(normalized == zero); + REQUIRE(tmp == Approx(0.f)); + } + } } GIVEN("The four unit quaternions") @@ -154,5 +170,18 @@ SCENARIO("Quaternion", "[MATH][QUATERNION]") REQUIRE(quaternionC.z == Approx(unitZ225.z)); } } + + WHEN("We get the rotation between two vectors") + { + /*TODO + * Nz::Quaternionf rotationBetweenXY = Nz::Quaternionf::RotationBetween(Nz::Vector3f::UnitX(), Nz::Vector3f::UnitY()); + + THEN("The rotation in left-handed is 270 degree on z") + { + Nz::Quaternionf rotation270Z(Nz::FromDegrees(270.f), Nz::Vector3f::UnitZ()); + Nz::Quaternionf rotation90Z(Nz::FromDegrees(90.f), Nz::Vector3f::UnitZ()); + REQUIRE(rotation90Z == rotationBetweenXY); + }*/ + } } } diff --git a/tests/Engine/Math/Ray.cpp b/tests/Engine/Math/Ray.cpp index df77a34ac..286a358c2 100644 --- a/tests/Engine/Math/Ray.cpp +++ b/tests/Engine/Math/Ray.cpp @@ -86,10 +86,37 @@ SCENARIO("Ray", "[MATH][RAY]") Nz::BoundingVolumef nullVolume(Nz::Extend_Null); CHECK(!ray.Intersect(nullVolume)); + float tmpClosest = -1.f; + float tmpFurthest = -1.f; Nz::BoundingVolumef infiniteVolume(Nz::Extend_Infinite); - CHECK(ray.Intersect(infiniteVolume)); + CHECK(ray.Intersect(infiniteVolume, &tmpClosest, &tmpFurthest)); + CHECK(tmpClosest == Approx(0.f)); + CHECK(tmpFurthest == std::numeric_limits::infinity()); } + THEN("For the triangle collision's") + { + Nz::Vector3f firstPoint(0.f, 1.f, 1.f); + Nz::Vector3f secondPoint(-1.f, 1.f, -1.f); + Nz::Vector3f thidPoint(1.f, 1.f, -1.f); + float tmpHit = -1.f; + + CHECK(ray.Intersect(firstPoint, secondPoint, thidPoint, &tmpHit)); + REQUIRE(ray.GetPoint(tmpHit) == Nz::Vector3f::UnitY()); + + Nz::Vector3f offset = Nz::Vector3f(10.f, 0.f, 10.f); + CHECK(!ray.Intersect(firstPoint + offset, secondPoint + offset, thidPoint + offset, &tmpHit)); + } + } + + WHEN("We try to lerp") + { + THEN("Compilation should be fine") + { + Nz::Rayf AxisX = Nz::Rayf::AxisX(); + Nz::Rayf AxisY = Nz::Rayf::AxisY(); + REQUIRE(Nz::Rayf::Lerp(AxisX, AxisY, 0.5f) == (Nz::Rayf(Nz::Vector3f::Zero(), Nz::Vector3f(0.5f, 0.5f, 0.f)))); + } } } } diff --git a/tests/Engine/Math/Rect.cpp b/tests/Engine/Math/Rect.cpp index 7a1035f79..b119d76c9 100644 --- a/tests/Engine/Math/Rect.cpp +++ b/tests/Engine/Math/Rect.cpp @@ -52,5 +52,17 @@ SCENARIO("Rect", "[MATH][RECT]") } } + + WHEN("We try to lerp") + { + THEN("Compilation should be fine") + { + Nz::Rectf nullRect = Nz::Rectf::Zero(); + Nz::Rectf centerAndUnit = firstCenterAndUnit; + Nz::Rectf result(Nz::Vector2f::Zero(), Nz::Vector2f::Unit() * 0.5f); + + REQUIRE(Nz::Rectf::Lerp(nullRect, centerAndUnit, 0.5f) == result); + } + } } } diff --git a/tests/Engine/Math/Sphere.cpp b/tests/Engine/Math/Sphere.cpp index 0f0e509ca..1820acd35 100644 --- a/tests/Engine/Math/Sphere.cpp +++ b/tests/Engine/Math/Sphere.cpp @@ -59,5 +59,45 @@ SCENARIO("Sphere", "[MATH][SPHERE]") REQUIRE(centerUnitBox.GetSquaredBoundingSphere() == Nz::Spheref(Nz::Vector3f::Zero(), 0.75f)); } } + + WHEN("We ask for positive and negative vertex") + { + Nz::Vector3f positiveVector = Nz::Vector3f::UnitY(); + + THEN("Positive vertex should be the same with centered and unit sphere") + { + REQUIRE(positiveVector == firstCenterAndUnit.GetPositiveVertex(positiveVector)); + } + + AND_THEN("Negative vertex should be the opposite") + { + REQUIRE(-positiveVector == firstCenterAndUnit.GetNegativeVertex(positiveVector)); + } + } + + WHEN("We extend the unit sphere to one point") + { + Nz::Vector3f point = Nz::Vector3f::UnitY() * 2.f; + + firstCenterAndUnit.ExtendTo(point); + + THEN("Sphere must contain it and distance should be good") + { + CHECK(firstCenterAndUnit.Contains(point)); + REQUIRE(firstCenterAndUnit.Distance(point) == 2.f); + } + } + + WHEN("We try to lerp") + { + THEN("Compilation should be fine") + { + Nz::Spheref nullRect = Nz::Spheref::Zero(); + Nz::Spheref centerAndUnit = firstCenterAndUnit; + Nz::Spheref result(Nz::Vector3f::Zero(), 0.5f); + + REQUIRE(Nz::Spheref::Lerp(nullRect, centerAndUnit, 0.5f) == result); + } + } } } diff --git a/tests/Engine/Math/Vector2.cpp b/tests/Engine/Math/Vector2.cpp index 17ae62e72..45ca1468c 100644 --- a/tests/Engine/Math/Vector2.cpp +++ b/tests/Engine/Math/Vector2.cpp @@ -15,6 +15,7 @@ SCENARIO("Vector2", "[MATH][VECTOR2]") THEN("They are the same") { REQUIRE(firstUnit == secondUnit); + REQUIRE(firstUnit <= secondUnit); } } @@ -22,11 +23,13 @@ SCENARIO("Vector2", "[MATH][VECTOR2]") { Nz::Vector2f tmp(-1.f, 1.f); - THEN("These results are expected") + THEN("These are perpendicular") { REQUIRE(firstUnit.AbsDotProduct(tmp) == Approx(2.f)); REQUIRE(firstUnit.DotProduct(tmp) == Approx(0.f)); - REQUIRE(firstUnit.AngleBetween(tmp) == Approx(90.f)); + REQUIRE(firstUnit.AngleBetween(tmp) == Approx(Nz::FromDegrees(90.f))); + Nz::Vector2f negativeUnitX = -Nz::Vector2f::UnitX(); + REQUIRE(negativeUnitX.AngleBetween(negativeUnitX + Nz::Vector2f(0, 0.0000001f)) == Approx(Nz::FromDegrees(360.f))); } } @@ -45,5 +48,49 @@ SCENARIO("Vector2", "[MATH][VECTOR2]") REQUIRE(firstUnit.GetLength() == Approx(std::sqrt(2.f))); } } + + WHEN("We nomalize the vectors") + { + float ratio = 0.f; + THEN("For normal cases should be normal") + { + Nz::Vector2f normalized = firstUnit.GetNormal(&ratio); + REQUIRE(normalized == (Nz::Vector2f::Unit() / std::sqrt(2.f))); + REQUIRE(ratio == Approx(std::sqrt(2.f))); + } + + THEN("For null vector") + { + Nz::Vector2f zero = Nz::Vector2f::Zero(); + REQUIRE(zero.GetNormal(&ratio) == Nz::Vector2f::Zero()); + REQUIRE(ratio == Approx(0.f)); + } + } + + WHEN("We try to maximize and minimize") + { + Nz::Vector2f maximize(2.f, 1.f); + Nz::Vector2f minimize(1.f, 2.f); + + THEN("The minimised and maximised should be (1, 1) and (2, 2)") + { + Nz::Vector2f maximized = maximize; + Nz::Vector2f minimized = minimize; + REQUIRE(minimized.Minimize(maximized) == Nz::Vector2f::Unit()); + REQUIRE(maximize.Maximize(minimize) == (2.f * Nz::Vector2f::Unit())); + } + + } + + WHEN("We try to lerp") + { + THEN("Compilation should be fine") + { + Nz::Vector2f zero = Nz::Vector2f::Zero(); + Nz::Vector2f unit = Nz::Vector2f::Unit(); + REQUIRE(Nz::Vector2f::Lerp(zero, unit, 0.5f) == (Nz::Vector2f::Unit() * 0.5f)); + } + } + } } diff --git a/tests/Engine/Math/Vector3.cpp b/tests/Engine/Math/Vector3.cpp index a61016e82..84c6a01ae 100644 --- a/tests/Engine/Math/Vector3.cpp +++ b/tests/Engine/Math/Vector3.cpp @@ -26,7 +26,8 @@ SCENARIO("Vector3", "[MATH][VECTOR3]") { REQUIRE(firstUnit.AbsDotProduct(tmp) == Approx(2.f)); REQUIRE(firstUnit.DotProduct(tmp) == Approx(0.f)); - REQUIRE(firstUnit.AngleBetween(tmp) == Approx(90.f)); + REQUIRE(firstUnit.AngleBetween(tmp) == Approx(Nz::FromDegrees(90.f))); + REQUIRE(firstUnit.AngleBetween(-firstUnit) == Approx(Nz::FromDegrees(180.f))); } } @@ -52,5 +53,48 @@ SCENARIO("Vector3", "[MATH][VECTOR3]") REQUIRE(firstUnit.GetLength() == Approx(std::sqrt(3.f))); } } + + WHEN("We nomalize the vectors") + { + float ratio = 0.f; + THEN("For normal cases should be normal") + { + Nz::Vector3f normalized = firstUnit.GetNormal(&ratio); + REQUIRE(normalized == (Nz::Vector3f::Unit() / std::sqrt(3.f))); + REQUIRE(ratio == Approx(std::sqrt(3.f))); + } + + THEN("For null vector") + { + Nz::Vector3f zero = Nz::Vector3f::Zero(); + REQUIRE(zero.GetNormal(&ratio) == Nz::Vector3f::Zero()); + REQUIRE(ratio == Approx(0.f)); + } + } + + WHEN("We try to maximize and minimize") + { + Nz::Vector3f maximize(2.f, 1.f, 2.f); + Nz::Vector3f minimize(1.f, 2.f, 1.f); + + THEN("The minimised and maximised should be (1, 1, 1) and (2, 2, 2)") + { + Nz::Vector3f maximized = maximize; + Nz::Vector3f minimized = minimize; + REQUIRE(minimized.Minimize(maximized) == Nz::Vector3f::Unit()); + REQUIRE(maximize.Maximize(minimize) == (2.f * Nz::Vector3f::Unit())); + } + + } + + WHEN("We try to lerp") + { + THEN("Compilation should be fine") + { + Nz::Vector3f zero = Nz::Vector3f::Zero(); + Nz::Vector3f unit = Nz::Vector3f::Unit(); + REQUIRE(Nz::Vector3f::Lerp(zero, unit, 0.5f) == (Nz::Vector3f::Unit() * 0.5f)); + } + } } } diff --git a/tests/Engine/Math/Vector4.cpp b/tests/Engine/Math/Vector4.cpp index cfe91eab7..85c321ac1 100644 --- a/tests/Engine/Math/Vector4.cpp +++ b/tests/Engine/Math/Vector4.cpp @@ -39,5 +39,15 @@ SCENARIO("Vector4", "[MATH][VECTOR4]") REQUIRE(tmp.Normalize() == Nz::Vector4f(Nz::Vector3f::Unit() * (1.f / 3.f), 1.f)); } } + + WHEN("We try to lerp") + { + THEN("Compilation should be fine") + { + Nz::Vector4f zero = Nz::Vector4f::Zero(); + Nz::Vector4f unitX = Nz::Vector4f::UnitX(); + REQUIRE(Nz::Vector4f::Lerp(zero, unitX, 0.5f) == Nz::Vector4f(Nz::Vector3f::UnitX() * 0.5f, 1.f)); + } + } } }