New tests !

Former-commit-id: 1cb5949ce7b9fde74189a37c75f0b3380fda95c7
This commit is contained in:
Gawaboumga 2015-12-30 15:38:52 +01:00
parent 139c495966
commit 15e4fe1453
15 changed files with 399 additions and 33 deletions

View File

@ -37,6 +37,19 @@ TEST_CASE("Clamp", "[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]") TEST_CASE("DegreeToRadian", "[ALGORITHM]")
{ {
SECTION("Convert 45.f degree to radian") SECTION("Convert 45.f degree to radian")
@ -47,6 +60,11 @@ 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") SECTION("Nearest power of two of 16 = 16")
{ {
REQUIRE(Nz::GetNearestPowerOfTwo(16) == 16); REQUIRE(Nz::GetNearestPowerOfTwo(16) == 16);
@ -115,6 +133,47 @@ TEST_CASE("GetNumberLength", "[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]") TEST_CASE("IntegralPow", "[ALGORITHM]")
{ {
SECTION("2 to power 4") SECTION("2 to power 4")

View File

@ -99,5 +99,20 @@ SCENARIO("BoundingVolume", "[MATH][BOUNDINGVOLUME]")
REQUIRE(firstCenterAndUnit.aabb != secondCenterAndUnit.aabb); 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);
}
}
} }
} }

View File

@ -64,6 +64,18 @@ SCENARIO("Box", "[MATH][BOX]")
REQUIRE(tmp == firstCenterAndUnit); 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") GIVEN("Two wrong box (negative width, height and depth")

View File

@ -15,8 +15,8 @@ SCENARIO("EulerAngles", "[MATH][EULERANGLES]")
WHEN("We do some operations") WHEN("We do some operations")
{ {
Nz::EulerAnglesf euler90(90.f, 90.f, 90.f); Nz::EulerAnglesf euler90(Nz::FromDegrees(90.f), Nz::FromDegrees(90.f), Nz::FromDegrees(90.f));
Nz::EulerAnglesf euler270(270.f, 270.f, 270.f); Nz::EulerAnglesf euler270(Nz::FromDegrees(270.f), Nz::FromDegrees(270.f), Nz::FromDegrees(270.f));
Nz::EulerAnglesf euler360 = euler90 + euler270; Nz::EulerAnglesf euler360 = euler90 + euler270;
euler360.Normalize(); 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") GIVEN("Euler angles with rotation 45 on each axis")
{ {
WHEN("We convert to quaternion") WHEN("We convert to quaternion")

View File

@ -78,5 +78,18 @@ SCENARIO("Frustum", "[MATH][FRUSTUM]")
CHECK(frustum.Contains(&tmp, 1)); 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);
}
}
} }
} }

View File

@ -37,6 +37,16 @@ SCENARIO("Matrix4", "[MATH][Matrix4]")
REQUIRE(firstIdentity.Inverse() == secondIdentity.InverseAffine()); 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") GIVEN("Two different matrix")

View File

@ -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);
}
}
} }
} }

View File

@ -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)); 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)") GIVEN("The plane XZ, distance 1 with 3 points (0, 1, 0), (1, 1, 1), (-1, 1, 0)")

View File

@ -32,6 +32,22 @@ SCENARIO("Quaternion", "[MATH][QUATERNION]")
REQUIRE((firstQuaternion * Nz::Vector3f::UnitZ()) == -Nz::Vector3f::UnitZ()); 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") GIVEN("The four unit quaternions")
@ -154,5 +170,18 @@ SCENARIO("Quaternion", "[MATH][QUATERNION]")
REQUIRE(quaternionC.z == Approx(unitZ225.z)); 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);
}*/
}
} }
} }

View File

@ -86,10 +86,37 @@ SCENARIO("Ray", "[MATH][RAY]")
Nz::BoundingVolumef nullVolume(Nz::Extend_Null); Nz::BoundingVolumef nullVolume(Nz::Extend_Null);
CHECK(!ray.Intersect(nullVolume)); CHECK(!ray.Intersect(nullVolume));
float tmpClosest = -1.f;
float tmpFurthest = -1.f;
Nz::BoundingVolumef infiniteVolume(Nz::Extend_Infinite); 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<float>::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))));
}
} }
} }
} }

View File

@ -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);
}
}
} }
} }

View File

@ -59,5 +59,45 @@ SCENARIO("Sphere", "[MATH][SPHERE]")
REQUIRE(centerUnitBox.GetSquaredBoundingSphere() == Nz::Spheref(Nz::Vector3f::Zero(), 0.75f)); 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);
}
}
} }
} }

View File

@ -15,6 +15,7 @@ SCENARIO("Vector2", "[MATH][VECTOR2]")
THEN("They are the same") THEN("They are the same")
{ {
REQUIRE(firstUnit == secondUnit); REQUIRE(firstUnit == secondUnit);
REQUIRE(firstUnit <= secondUnit);
} }
} }
@ -22,11 +23,13 @@ SCENARIO("Vector2", "[MATH][VECTOR2]")
{ {
Nz::Vector2f tmp(-1.f, 1.f); 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.AbsDotProduct(tmp) == Approx(2.f));
REQUIRE(firstUnit.DotProduct(tmp) == Approx(0.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))); 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));
}
}
} }
} }

View File

@ -26,7 +26,8 @@ SCENARIO("Vector3", "[MATH][VECTOR3]")
{ {
REQUIRE(firstUnit.AbsDotProduct(tmp) == Approx(2.f)); REQUIRE(firstUnit.AbsDotProduct(tmp) == Approx(2.f));
REQUIRE(firstUnit.DotProduct(tmp) == Approx(0.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))); 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));
}
}
} }
} }

View File

@ -39,5 +39,15 @@ SCENARIO("Vector4", "[MATH][VECTOR4]")
REQUIRE(tmp.Normalize() == Nz::Vector4f(Nz::Vector3f::Unit() * (1.f / 3.f), 1.f)); 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));
}
}
} }
} }