UnitTests: Add build script and move current tests to "tests/Engine" directory (from "tests/Nazara")

Former-commit-id: 5639305bbdbb69ad6f6f282df6c6de930220b57f
This commit is contained in:
Lynix
2015-09-19 01:14:19 +02:00
parent a09f859144
commit a61f968d05
24 changed files with 27 additions and 0 deletions

View File

@@ -0,0 +1,197 @@
#include <Nazara/Math/Algorithm.hpp>
#include <Catch/catch.hpp>
TEST_CASE("Approach", "[MATH][ALGORITHM]" )
{
SECTION("Approach 8 with 5 by 2")
{
REQUIRE(NzApproach(5, 8, 2) == 7);
}
SECTION("Approach 5 with 8 by 2")
{
REQUIRE(NzApproach(8, 5, 2) == 6);
}
SECTION("Approach 8 with 8 by 2")
{
REQUIRE(NzApproach(8, 8, 2) == 8);
}
}
TEST_CASE("Clamp", "[ALGORITHM]" )
{
SECTION("Clamp 8 between 5 and 10")
{
REQUIRE(NzClamp(8, 5, 10) == 8);
}
SECTION("Clamp 4 between 5 and 10")
{
REQUIRE(NzClamp(4, 5, 10) == 5);
}
SECTION("Clamp 12 between 5 and 10")
{
REQUIRE(NzClamp(12, 5, 10) == 10);
}
}
TEST_CASE("DegreeToRadian", "[ALGORITHM]" )
{
SECTION("Convert 45.f degree to radian")
{
REQUIRE(NzDegreeToRadian(45.f) == Approx(M_PI / 4));
}
}
TEST_CASE("GetNearestPowerOfTwo", "[ALGORITHM]" )
{
SECTION("Nearest power of two of 16 = 16")
{
REQUIRE(NzGetNearestPowerOfTwo(16) == 16);
}
SECTION("Nearest power of two of 17 = 32")
{
REQUIRE(NzGetNearestPowerOfTwo(17) == 32);
}
}
TEST_CASE("GetNumberLength", "[ALGORITHM]" )
{
SECTION("GetNumberLength of -127 signed char")
{
signed char minus127 = -127;
REQUIRE(NzGetNumberLength(minus127) == 4);
}
SECTION("GetNumberLength of 255 unsigned char")
{
unsigned char plus255 = 255;
REQUIRE(NzGetNumberLength(plus255) == 3);
}
SECTION("GetNumberLength of -1270 signed int")
{
signed int minus1270 = -1270;
REQUIRE(NzGetNumberLength(minus1270) == 5);
}
SECTION("GetNumberLength of 2550 unsigned int")
{
unsigned int plus2550 = 2550;
REQUIRE(NzGetNumberLength(plus2550) == 4);
}
SECTION("GetNumberLength of -1270 signed long long")
{
signed long long minus12700 = -12700;
REQUIRE(NzGetNumberLength(minus12700) == 6);
}
SECTION("GetNumberLength of 2550 unsigned long long")
{
unsigned long long plus25500 = 25500;
REQUIRE(NzGetNumberLength(plus25500) == 5);
}
SECTION("GetNumberLength of -2.456f float")
{
float minus2P456 = -2.456f;
REQUIRE(NzGetNumberLength(minus2P456, 3) == 6);
}
SECTION("GetNumberLength of -2.456 double")
{
double minus2P456 = -2.456;
REQUIRE(NzGetNumberLength(minus2P456, 3) == 6);
}
SECTION("GetNumberLength of -2.456 long double")
{
long double minus2P456 = -2.456L;
REQUIRE(NzGetNumberLength(minus2P456, 3) == 6);
}
}
TEST_CASE("IntegralPow", "[ALGORITHM]" )
{
SECTION("2 to power 4")
{
REQUIRE(NzIntegralPow(2, 4) == 16);
}
}
TEST_CASE("Lerp", "[ALGORITHM]" )
{
SECTION("Lerp 2 to 6 with 0.5")
{
REQUIRE(NzLerp(2, 6, 0.5) == 4);
}
}
TEST_CASE("MultiplyAdd", "[ALGORITHM]" )
{
SECTION("2 * 3 + 1")
{
REQUIRE(NzMultiplyAdd(2, 3, 1) == 7);
}
}
TEST_CASE("NumberEquals", "[ALGORITHM]" )
{
SECTION("2.35 and 2.351 should be the same at 0.01")
{
CHECK(NzNumberEquals(2.35, 2.35, 0.01));
}
SECTION("3 and 4 unsigned should be the same at 1")
{
CHECK(NzNumberEquals(3U, 4U, 1U));
}
}
TEST_CASE("NumberToString", "[ALGORITHM]" )
{
SECTION("235 to string")
{
REQUIRE(NzNumberToString(235) == "235");
}
SECTION("-235 to string")
{
REQUIRE(NzNumberToString(-235) == "-235");
}
SECTION("16 in base 16 to string")
{
REQUIRE(NzNumberToString(16, 16) == "10");
}
}
TEST_CASE("RadianToDegree", "[ALGORITHM]" )
{
SECTION("PI / 4 to degree")
{
REQUIRE(NzRadianToDegree(M_PI / 4) == Approx(45.f));
}
}
TEST_CASE("StringToNumber", "[ALGORITHM]" )
{
SECTION("235 in string")
{
REQUIRE(NzStringToNumber("235") == 235);
}
SECTION("-235 in string")
{
REQUIRE(NzStringToNumber("-235") == -235);
}
SECTION("16 in base 16 in string")
{
REQUIRE(NzStringToNumber("10", 16) == 16);
}
}

View File

@@ -0,0 +1,103 @@
#include <Nazara/Math/BoundingVolume.hpp>
#include <Catch/catch.hpp>
SCENARIO("BoundingVolume", "[MATH][BOUNDINGVOLUME]")
{
GIVEN("With a null bounding volume and an infinite")
{
NzBoundingVolumef nullVolume = NzBoundingVolumef::Null();
NzBoundingVolumef infiniteVolume = NzBoundingVolumef::Infinite();
WHEN("We compare them")
{
THEN("They should be different")
{
REQUIRE(nullVolume != infiniteVolume);
}
}
WHEN("We ask for the characteristic")
{
THEN("They should be respectively null and infinite")
{
CHECK(nullVolume.IsNull());
CHECK(infiniteVolume.IsInfinite());
}
}
WHEN("If we multiply them")
{
THEN("They should still be different")
{
nullVolume *= 5.f;
infiniteVolume = infiniteVolume * 0.5f;
REQUIRE(nullVolume != infiniteVolume);
AND_WHEN("We ask for the characteristic (infinite and null)")
{
THEN("They should still be respectively null and infinite")
{
CHECK(nullVolume.IsNull());
CHECK(infiniteVolume.IsInfinite());
}
}
}
}
WHEN("We compare two null or two infinite")
{
THEN("Everything should be ok")
{
REQUIRE(NzBoundingVolumef::Null() == NzBoundingVolumef::Null());
REQUIRE(NzBoundingVolumef::Infinite() == NzBoundingVolumef::Infinite());
}
}
}
GIVEN("Two same bounding volume with different constructor")
{
NzBoundingVolumef firstCenterAndUnit(0.f, 0.f, 0.f, 1.f, 1.f, 1.f);
NzBoundingVolumef secondCenterAndUnit(NzVector3f::Zero(), NzVector3f::Unit());
firstCenterAndUnit.Update(NzMatrix4f::Identity());
secondCenterAndUnit.Update(NzMatrix4f::Identity());
WHEN("We compare them")
{
THEN("Then the should be equal")
{
REQUIRE(firstCenterAndUnit == secondCenterAndUnit);
}
}
WHEN("We ask for the characteristic")
{
THEN("They should be finite")
{
CHECK(firstCenterAndUnit.IsFinite());
CHECK(secondCenterAndUnit.IsFinite());
}
}
WHEN("We use a constructor of conversion")
{
THEN("There's no problem")
{
NzBoundingVolume<int> intVolumeCenterAndUnit(NzBoxi(NzVector3i::Zero(), NzVector3i::Unit()));
NzBoundingVolumef thirdCenterAndUnit(intVolumeCenterAndUnit);
REQUIRE(thirdCenterAndUnit == secondCenterAndUnit);
}
}
WHEN("We make one twice bigger with a matrix")
{
firstCenterAndUnit.Update(NzMatrix4f::Scale(NzVector3f::Unit() * 2.f));
THEN("The local box should be the same but the aabb different")
{
REQUIRE(firstCenterAndUnit.obb.localBox == secondCenterAndUnit.obb.localBox);
REQUIRE(firstCenterAndUnit.aabb != secondCenterAndUnit.aabb);
}
}
}
}

113
tests/Engine/Math/Box.cpp Normal file
View File

@@ -0,0 +1,113 @@
#include <Nazara/Math/Box.hpp>
#include <Catch/catch.hpp>
SCENARIO("Box", "[MATH][BOX]")
{
GIVEN("Two zero boxes")
{
NzBoxf firstZero(NzBoxf::Zero());
NzBoxf secondZero(NzVector3f::Zero(), NzVector3f::Zero());
WHEN("We multiply them")
{
firstZero = firstZero * 1.f;
secondZero = secondZero * NzVector3f::Unit() * 3.f;
THEN("They should stay the same")
{
REQUIRE(firstZero == secondZero);
CHECK(!firstZero.IsValid());
CHECK(!secondZero.IsValid());
}
}
}
GIVEN("Two unit and center boxes")
{
NzBoxf firstCenterAndUnit(NzRectf(NzVector2f::Zero(), NzVector2f::Unit()));
NzBoxf secondCenterAndUnit(1.f, 1.f, 1.f);
WHEN("We ask for some informations")
{
THEN("These results are expected")
{
REQUIRE(firstCenterAndUnit.GetBoundingSphere() == NzSpheref(NzVector3f::Unit() * 0.5f, std::sqrt(3.f * 0.5f * 0.5f)));
REQUIRE(firstCenterAndUnit.GetCenter() == (NzVector3f::Unit() * 0.5f));
REQUIRE(firstCenterAndUnit.GetCorner(nzBoxCorner_FarLeftTop) == NzVector3f::UnitY());
REQUIRE(firstCenterAndUnit.GetLengths() == NzVector3f::Unit());
REQUIRE(firstCenterAndUnit.GetMaximum() == NzVector3f::Unit());
REQUIRE(firstCenterAndUnit.GetMinimum() == NzVector3f::Zero());
REQUIRE(firstCenterAndUnit.GetNegativeVertex(NzVector3f::Unit()) == NzVector3f::Zero());
REQUIRE(firstCenterAndUnit.GetPosition() == NzVector3f::Zero());
REQUIRE(firstCenterAndUnit.GetPositiveVertex(NzVector3f::Unit()) == NzVector3f::Unit());
REQUIRE(firstCenterAndUnit.GetRadius() == Approx(std::sqrt(3.f * 0.5f * 0.5f)));
REQUIRE(firstCenterAndUnit.GetSquaredBoundingSphere() == NzSpheref(NzVector3f::Unit() * 0.5f, 3.f * 0.5f * 0.5f));
REQUIRE(firstCenterAndUnit.GetSquaredRadius() == Approx(3.f * 0.5f * 0.5f));
}
}
WHEN("We ask for the intersection between the two")
{
THEN("We should have a center and unit")
{
NzBoxf thirdCenterAndUnit;
CHECK(firstCenterAndUnit.Intersect(secondCenterAndUnit, &thirdCenterAndUnit));
REQUIRE(firstCenterAndUnit == secondCenterAndUnit);
}
}
WHEN("We use the constructor of conversion")
{
THEN("Shouldn't be a problem")
{
NzBoxf tmp(NzBoxi(0, 0, 0, 1, 1, 1));
REQUIRE(tmp == firstCenterAndUnit);
}
}
}
GIVEN("Two wrong box (negative width, height and depth")
{
NzBoxf firstWrongBox(-NzVector3f::Unit());
NzBoxf secondWrongBox(-NzVector3f::Unit());
WHEN("We check if valid")
{
THEN("Result if false")
{
CHECK(!firstWrongBox.IsValid());
CHECK(!secondWrongBox.IsValid());
}
}
WHEN("We correct them")
{
firstWrongBox.ExtendTo(NzVector3f::Unit());
secondWrongBox.Transform(NzMatrix4f::Scale(-NzVector3f::Unit()));
THEN("They should be valid")
{
CHECK(firstWrongBox.IsValid());
CHECK(secondWrongBox.IsValid());
}
AND_WHEN("We ask if they contain boxes")
{
THEN("These results are expected")
{
CHECK(firstWrongBox.Contains(0.f, 0.f, 0.f));
CHECK(secondWrongBox.Contains(0.f, 0.f, 0.f));
secondWrongBox = secondWrongBox.Lerp(NzBoxf::Zero(), secondWrongBox, 0.f); // Zeroed
secondWrongBox.ExtendTo(NzBoxf(NzVector3f(0.1f, 0.1f, 0.1f), NzVector3f(0.9f, 0.9f, 0.9f)));
secondWrongBox.Translate(NzVector3f(0.05f, 0.05f, 0.05f)); // Box 0.15 to 0.95
CHECK(firstWrongBox.Contains(secondWrongBox));
NzBoxf test(1.f, -500.f, -500.f, 1000.f, 1000.f, 1000.f);
CHECK(test.Contains(NzBoxf(500.f, -0.5f, -0.5f, 1.f, 1.f, 1.f)));
CHECK(test.Contains(500.f, 0.f, 0.f));
}
}
}
}
}

View File

@@ -0,0 +1,83 @@
#include <Nazara/Math/EulerAngles.hpp>
#include <Catch/catch.hpp>
SCENARIO("EulerAngles", "[MATH][EULERANGLES]")
{
GIVEN("Two zero euler angles")
{
NzEulerAnglesf firstZero(0.f, 0.f, 0.f);
NzEulerAnglesf secondZero(NzEulerAngles<int>::Zero());
THEN("They should be equal")
{
REQUIRE(firstZero == secondZero);
}
WHEN("We do some operations")
{
NzEulerAnglesf euler90(90.f, 90.f, 90.f);
NzEulerAnglesf euler270(270.f, 270.f, 270.f);
NzEulerAnglesf euler360 = euler90 + euler270;
euler360.Normalize();
NzEulerAnglesf euler0 = euler270 - euler90;
euler0 -= euler90;
euler0 -= euler90;
THEN("They should still be equal")
{
REQUIRE(euler360 == firstZero);
REQUIRE(euler0 == secondZero);
}
}
WHEN("We ask for conversion to quaternion")
{
THEN("They are the same")
{
REQUIRE(firstZero.ToQuaternion() == secondZero.ToQuaternion());
REQUIRE(firstZero.ToQuaternion() == NzEulerAnglesf(NzQuaternionf(1.f, 0.f, 0.f, 0.f)));
REQUIRE(secondZero.ToQuaternion() == NzEulerAnglesf(NzQuaternionf(1.f, 0.f, 0.f, 0.f)));
}
}
}
GIVEN("Euler angles with rotation 45 on each axis")
{
WHEN("We convert to quaternion")
{
THEN("These results are expected")
{
REQUIRE(NzEulerAnglesf(NzFromDegrees(45.f), 0.f, 0.f) == NzQuaternionf(0.923879504204f, 0.382683455944f, 0.f, 0.f).ToEulerAngles());
REQUIRE(NzEulerAnglesf(0.f, NzFromDegrees(45.f), 0.f) == NzQuaternionf(0.923879504204f, 0.f, 0.382683455944f, 0.f).ToEulerAngles());
//REQUIRE(NzEulerAnglesf(0.f, 0.f, NzFromDegrees(45.f)) == NzQuaternionf(0.923879504204f, 0.f, 0.f, 0.382683455944f).ToEulerAngles());
}
}
}
GIVEN("Three euler angles: (0, 22.5, 22.5), (90, 90, 0) and (30, 0, 30)")
{
NzEulerAnglesf euler45(NzFromDegrees(0.f), NzFromDegrees(22.5f), NzFromDegrees(22.5f));
NzEulerAnglesf euler90(NzFromDegrees(90.f), NzFromDegrees(90.f), NzFromDegrees(0.f));
NzEulerAnglesf euler30(NzFromDegrees(30.f), NzFromDegrees(0.f), NzFromDegrees(30.f));
WHEN("We convert them to quaternion")
{
THEN("And then convert to euler angles, we have identity")
{
NzEulerAnglesf tmp = NzQuaternionf(euler45.ToQuaternion()).ToEulerAngles();
REQUIRE(tmp.pitch == Approx(0.f));
REQUIRE(tmp.yaw == Approx(22.5f));
REQUIRE(tmp.roll == Approx(22.5f));
tmp = NzQuaternionf(euler90.ToQuaternion()).ToEulerAngles();
REQUIRE(tmp.pitch == Approx(90.f));
REQUIRE(tmp.yaw == Approx(90.f));
REQUIRE(tmp.roll == Approx(0.f));
tmp = NzQuaternionf(euler30.ToQuaternion()).ToEulerAngles();
REQUIRE(tmp.pitch == Approx(30.f));
REQUIRE(tmp.yaw == Approx(0.f));
REQUIRE(tmp.roll == Approx(30.f));
}
}
}
}

View File

@@ -0,0 +1,82 @@
#include <Nazara/Math/Frustum.hpp>
#include <Catch/catch.hpp>
SCENARIO("Frustum", "[MATH][FRUSTUM]")
{
GIVEN("One frustum (90, 1, 1, 1000, (0, 0, 0), (1, 0, 0))")
{
NzFrustumf frustum;
frustum.Build(NzFromDegrees(90.f), 1.f, 1.f, 1000.f, NzVector3f::Zero(), NzVector3f::UnitX());
WHEN("We ask for intersection with objects outside the frustum")
{
THEN("These results are expected")
{
NzBoundingVolumef bv(NzVector3f::Zero(), NzVector3f::Unit());
bv.Update(NzMatrix4f::Identity());
REQUIRE(nzIntersectionSide_Outside == frustum.Intersect(bv));
REQUIRE(nzIntersectionSide_Outside == frustum.Intersect(NzBoxf(NzVector3f::Zero(), NzVector3f::Unit() * 0.9f)));
NzOrientedBoxf obb(NzVector3f::Zero(), NzVector3f::Unit() * 0.9f);
obb.Update(NzMatrix4f::Identity());
REQUIRE(nzIntersectionSide_Outside == frustum.Intersect(obb));
REQUIRE(nzIntersectionSide_Outside == frustum.Intersect(NzSpheref(NzVector3f::Zero(), 0.5f)));
NzVector3f tmp = NzVector3f::Zero();
REQUIRE(nzIntersectionSide_Outside == frustum.Intersect(&tmp, 1));
tmp = NzVector3f::UnitX() * -10.f;
REQUIRE(nzIntersectionSide_Outside == frustum.Intersect(&tmp, 1));
}
}
WHEN("We ask for intersection with objects inside the frustum")
{
THEN("These results are expected")
{
NzBoundingVolumef bv(500.f, -0.5f, -0.5f, 1.f, 1.f, 1.f);
bv.Update(NzMatrix4f::Identity());
REQUIRE(nzIntersectionSide_Inside == frustum.Intersect(bv));
REQUIRE(nzIntersectionSide_Inside == frustum.Intersect(NzBoxf(NzVector3f::UnitX() * 500.f, NzVector3f::Unit())));
NzOrientedBoxf obb(NzVector3f::UnitX() * 100.f, NzVector3f::Unit());
obb.Update(NzMatrix4f::Identity());
REQUIRE(nzIntersectionSide_Inside == frustum.Intersect(obb));
REQUIRE(nzIntersectionSide_Inside == frustum.Intersect(NzSpheref(NzVector3f::UnitX() * 100.f, 0.5f)));
NzVector3f tmp = NzVector3f::UnitX() * 100.f;
REQUIRE(nzIntersectionSide_Inside == frustum.Intersect(&tmp, 1));
}
}
WHEN("We ask for contains with objects outside the frustum")
{
THEN("These results are expected")
{
NzBoundingVolumef bv(0.f, -0.25f, -0.25f, 0.5f, 0.5f, 0.5f);
bv.Update(NzMatrix4f::Identity());
CHECK(!frustum.Contains(bv));
CHECK(!frustum.Contains(NzBoxf(0.f, -0.25f, -0.25f, 0.5f, 0.5f, 0.5f)));
NzOrientedBoxf obb(0.f, -0.25f, -0.25f, 0.5f, 0.5f, 0.5f);
obb.Update(NzMatrix4f::Identity());
CHECK(!frustum.Contains(obb));
CHECK(!frustum.Contains(NzSpheref(NzVector3f::Zero(), 0.5f)));
NzVector3f tmp = NzVector3f::Zero();
CHECK(!frustum.Contains(&tmp, 1));
}
}
WHEN("We ask for contains with objects inside the frustum")
{
THEN("These results are expected")
{
NzBoundingVolumef bv(500.f, -0.5f, -0.5f, 1.f, 1.f, 1.f);
bv.Update(NzMatrix4f::Identity());
CHECK(frustum.Contains(bv));
CHECK(frustum.Contains(NzBoxf(500.f, -0.5f, -0.5f, 1.f, 1.f, 1.f)));
NzOrientedBoxf obb(500.f, -0.5f, -0.5f, 1.f, 1.f, 1.f);
obb.Update(NzMatrix4f::Identity());
CHECK(frustum.Contains(obb));
CHECK(frustum.Contains(NzSpheref(NzVector3f::UnitX() * 500.f, 1.f)));
NzVector3f tmp = NzVector3f::UnitX() * 500.f;
CHECK(frustum.Contains(&tmp, 1));
}
}
}
}

View File

@@ -0,0 +1,134 @@
#include <Nazara/Math/Matrix4.hpp>
#include <Catch/catch.hpp>
SCENARIO("Matrix4", "[MATH][MATRIX4]")
{
GIVEN("Two identity matrix")
{
NzMatrix4f firstIdentity(NzMatrix4<int>::Identity());
NzMatrix4f secondIdentity(1.f, 0.f, 0.f, 0.f, 0.f, 1.f, 0.f, 0.f, 0.f, 0.f, 1.f, 0.f, 0.f, 0.f, 0.f, 1.f);
WHEN("We compare them")
{
THEN("They are equal")
{
REQUIRE(firstIdentity == secondIdentity);
}
}
WHEN("We multiply the first with a vector")
{
THEN("Vector stay the same")
{
REQUIRE(firstIdentity.Transform(NzVector2f::Unit()) == NzVector2f::Unit());
REQUIRE(firstIdentity.Transform(NzVector3f::Unit()) == NzVector3f::Unit());
REQUIRE(firstIdentity.Transform(NzVector4f(1.f, 1.f, 1.f, 1.f)) == NzVector4f(1.f, 1.f, 1.f, 1.f));
}
}
WHEN("We multiply them")
{
THEN("It keeps being a identity")
{
REQUIRE(firstIdentity.Concatenate(secondIdentity) == firstIdentity);
REQUIRE(firstIdentity.ConcatenateAffine(secondIdentity) == firstIdentity);
REQUIRE((firstIdentity * secondIdentity) == firstIdentity);
REQUIRE((1.f * firstIdentity) == firstIdentity);
REQUIRE(firstIdentity.Inverse() == secondIdentity.InverseAffine());
}
}
}
GIVEN("Two different matrix")
{
NzMatrix4f 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);
NzMatrix4f 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);
WHEN("We ask for determinant")
{
THEN("These results are expected")
{
REQUIRE(matrix1.GetDeterminant() == Approx(24.f));
REQUIRE(matrix2.GetDeterminant() == Approx(-1.f));
}
}
WHEN("We multiply the matrix and its inverse")
{
NzMatrix4f invMatrix1;
matrix1.GetInverse(&invMatrix1);
NzMatrix4f invMatrix2;
matrix2.GetInverse(&invMatrix2);
THEN("We get the identity")
{
NzMatrix4f tmp = matrix1 * invMatrix1;
REQUIRE(tmp.m32 == Approx(0.f));
REQUIRE(tmp.m42 == Approx(0.f));
tmp.m32 = 0.f;
tmp.m42 = 0.f;
REQUIRE(tmp == NzMatrix4f::Identity());
REQUIRE((matrix2 * invMatrix2) == NzMatrix4f::Identity());
}
}
}
GIVEN("One transformed matrix from rotation 45 and translation 0")
{
NzMatrix4f transformedMatrix = NzMatrix4f::Transform(NzVector3f::Zero(), NzQuaternionf::Identity());
REQUIRE(transformedMatrix == NzMatrix4f::Identity());
WHEN("We compare with the right matrix")
{
THEN("Rotation around X")
{
transformedMatrix.MakeTransform(NzVector3f::Zero(), NzEulerAnglesf(NzFromDegrees(45.f), 0.f, 0.f).ToQuaternion());
NzMatrix4f 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);
REQUIRE(transformedMatrix == rotation45X);
transformedMatrix.MakeTransform(NzVector3f::Unit(), NzEulerAnglesf(NzFromDegrees(45.f), 0.f, 0.f).ToQuaternion());
rotation45X.ApplyTranslation(NzVector3f::Unit());
REQUIRE(transformedMatrix == rotation45X);
}
THEN("Rotation around Y")
{
transformedMatrix.MakeTransform(NzVector3f::Zero(), NzEulerAnglesf(0.f, NzFromDegrees(45.f), 0.f).ToQuaternion());
NzMatrix4f 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);
REQUIRE(transformedMatrix == rotation45Y);
transformedMatrix.MakeTransform(NzVector3f::Unit(), NzEulerAnglesf(0.f, NzFromDegrees(45.f), 0.f).ToQuaternion());
rotation45Y.ApplyTranslation(NzVector3f::Unit());
REQUIRE(transformedMatrix == rotation45Y);
}
THEN("Rotation around Z")
{
transformedMatrix.MakeTransform(NzVector3f::Zero(), NzEulerAnglesf(0.f, 0.f, NzFromDegrees(45.f)).ToQuaternion());
NzMatrix4f 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);
REQUIRE(transformedMatrix == rotation45Z);
transformedMatrix.MakeTransform(NzVector3f::Unit(), NzEulerAnglesf(NzEulerAnglesf(0.f, 0.f, NzFromDegrees(45.f)).ToQuaternion()));
rotation45Z.ApplyTranslation(NzVector3f::Unit());
REQUIRE(transformedMatrix == rotation45Z);
}
}
}
}

View File

@@ -0,0 +1,46 @@
#include <Nazara/Math/OrientedBox.hpp>
#include <Catch/catch.hpp>
SCENARIO("OrientedBox", "[MATH][ORIENTEDBOX]")
{
GIVEN("Two center and unit oriented boxes")
{
NzOrientedBoxf firstCenterAndUnit(0.f, 0.f, 0.f, 1.f, 1.f, 1.f);
NzOrientedBoxf secondCenterAndUnit(NzOrientedBox<int>(NzVector3i::Zero(), NzVector3i::Unit()));
firstCenterAndUnit.Update(NzMatrix4f::Identity());
WHEN("We compare them")
{
THEN("They are the same")
{
REQUIRE(firstCenterAndUnit == secondCenterAndUnit);
}
}
WHEN("We ask if they are valid")
{
THEN("They are valid")
{
CHECK(firstCenterAndUnit.IsValid());
CHECK(secondCenterAndUnit.IsValid());
}
}
WHEN("We multiply them")
{
THEN("Results are different between operator * and update(ScaleMatrix) but corners are the same")
{
firstCenterAndUnit *= 2.f;
firstCenterAndUnit.Update(NzMatrix4f::Identity());
secondCenterAndUnit.Update(NzMatrix4f::Scale(NzVector3f::Unit() * 2.f));
REQUIRE(firstCenterAndUnit != secondCenterAndUnit);
for (unsigned int i = 0; i <= nzBoxCorner_Max; ++i)
{
REQUIRE(firstCenterAndUnit(i) == secondCenterAndUnit(i));
}
}
}
}
}

View File

@@ -0,0 +1,75 @@
#include <Nazara/Math/Plane.hpp>
#include <Catch/catch.hpp>
SCENARIO("Plane", "[MATH][PLANE]")
{
GIVEN("Two planes normal(1, 1, 1), distance 1")
{
NzPlanef firstPlane(NzVector3f::Unit().Normalize(), 1.f);
NzPlanef secondPlane(NzPlaned(NzVector3d::Unit().Normalize(), 1.0));
WHEN("We compare them")
{
THEN("They are equal")
{
REQUIRE(firstPlane == secondPlane);
}
AND_THEN("We compare with normal(-1, -1, -1), distance -1")
{
REQUIRE(firstPlane == NzPlanef(-NzVector3f::Unit().Normalize(), -1.f));
}
AND_THEN("They have the same distance from the same point")
{
NzVector3f point(-2.f, 3.f, 1.f);
REQUIRE(firstPlane.Distance(point) == Approx(secondPlane.Distance(point)));
REQUIRE(firstPlane.Distance(-2.f, 3.f, 1.f) == Approx(0.1547f));
}
AND_THEN("Distance between Plane (0, 1, 0), distance 1 and point (0, 2, 0) should be 1")
{
REQUIRE(NzPlanef(NzVector3f::UnitY(), 1.f).Distance(NzVector3f::UnitY() * 2.f) == Approx(1.f));
}
AND_THEN("Distance between Plane (0, 1, 0), distance 5 and point (0, 2, 0) should be -3")
{
REQUIRE(NzPlanef(NzVector3f::UnitY(), 5.f).Distance(NzVector3f::UnitY() * 2.f) == Approx(-3.f));
}
AND_THEN("Distance between Plane (0, 1, 0), distance 1000 and point (0, 500, 0) and (0, 1500, 0)")
{
REQUIRE(NzPlanef(NzVector3f::UnitY(), 1000.f).Distance(NzVector3f::UnitY() * 500.f) == Approx(-500.f));
REQUIRE(NzPlanef(NzVector3f::UnitY(), 1000.f).Distance(NzVector3f::UnitY() * 1500.f) == Approx(500.f));
}
AND_THEN("Distance between Plane (0, -1, 0), distance -1000 and point (0, 500, 0) and (0, 1500, 0)")
{
REQUIRE(NzPlanef(-NzVector3f::UnitY(), -1000.f).Distance(NzVector3f::UnitY() * 500.f) == Approx(500.f));
REQUIRE(NzPlanef(-NzVector3f::UnitY(), -1000.f).Distance(NzVector3f::UnitY() * 1500.f) == Approx(-500.f));
}
}
}
GIVEN("The plane XZ, distance 1 with 3 points (0, 1, 0), (1, 1, 1), (-1, 1, 0)")
{
WHEN("We do a positive plane")
{
NzPlanef xy(NzVector3f(2.f, 1.f, 0.f), NzVector3f(-1.f, 1.f, -1.f), NzVector3f(-1.f, 1.f, 0.f));
THEN("It must be equal to XZ distance 1")
{
REQUIRE(xy == NzPlanef(NzVector3f::UnitY(), 1.f));
}
}
WHEN("We do a negative plane")
{
NzPlanef xy(NzVector3f(0.f, 1.f, 0.f), NzVector3f(1.f, 1.f, 1.f), NzVector3f(-1.f, 1.f, 0.f));
THEN("It must be equal to XZ distance 1")
{
REQUIRE(xy == NzPlanef(-NzVector3f::UnitY(), -1.f));
}
}
}
}

View File

@@ -0,0 +1,154 @@
#include <Nazara/Math/Quaternion.hpp>
#include <Catch/catch.hpp>
SCENARIO("Quaternion", "[MATH][QUATERNION]")
{
GIVEN("Two quaternions (0, 1, 0, 0)")
{
NzQuaternionf firstQuaternion(NzFromDegrees(180.f), NzVector3f::UnitX());
NzQuaternionf secondQuaternion(0.f, 1.f, 0.f, 0.f);
WHEN("We compare them")
{
THEN("They are the same and the proprieties of quaternions are respected")
{
REQUIRE(firstQuaternion == secondQuaternion);
REQUIRE(firstQuaternion.ComputeW() == secondQuaternion.Normalize());
REQUIRE(firstQuaternion.Conjugate() == secondQuaternion.Inverse());
REQUIRE(firstQuaternion.DotProduct(secondQuaternion) == Approx(1.f));
}
}
WHEN("We do some operations")
{
THEN("Multiply with a vectorX is identity")
{
REQUIRE((firstQuaternion * NzVector3f::UnitX()) == NzVector3f::UnitX());
}
AND_THEN("Multiply with a vectorY or Z is opposite")
{
REQUIRE((firstQuaternion * NzVector3f::UnitY()) == -NzVector3f::UnitY());
REQUIRE((firstQuaternion * NzVector3f::UnitZ()) == -NzVector3f::UnitZ());
}
}
}
GIVEN("The four unit quaternions")
{
NzQuaternionf w(1.f, 0.f, 0.f, 0.f);
NzQuaternionf x(0.f, 1.f, 0.f, 0.f);
NzQuaternionf y(0.f, 0.f, 1.f, 0.f);
NzQuaternionf z(0.f, 0.f, 0.f, 1.f);
NzQuaternionf xyzw = x * y * z * w;
WHEN("We ask for the norm")
{
THEN("They are all equal to 1")
{
REQUIRE(w.Magnitude() == Approx(1.f));
REQUIRE(x.Magnitude() == Approx(1.f));
REQUIRE(y.Magnitude() == Approx(1.f));
REQUIRE(z.Magnitude() == Approx(1.f));
REQUIRE(xyzw.Magnitude() == Approx(1.f));
}
}
WHEN("We multiply them")
{
THEN("Results shoud follow")
{
NzQuaternionf oppositeOfW(-1.f, 0.f, 0.f, 0.f);
NzQuaternionf oppositeOfX = x.GetConjugate();
NzQuaternionf oppositeOfY = y.GetConjugate();
NzQuaternionf oppositeOfZ = z.GetConjugate();
REQUIRE((x * x) == oppositeOfW);
REQUIRE((y * y) == oppositeOfW);
REQUIRE((z * z) == oppositeOfW);
REQUIRE((x * y * z) == oppositeOfW);
REQUIRE((x * y) == z);
REQUIRE((y * x) == oppositeOfZ);
REQUIRE((y * z) == x);
REQUIRE((z * y) == oppositeOfX);
REQUIRE((z * x) == y);
REQUIRE((x * z) == oppositeOfY);
}
}
}
GIVEN("Two different quaternions (10, (1, 0, 0) and (20, (1, 0, 0))")
{
NzQuaternionf x10 = NzQuaternionf(NzFromDegrees(10.f), NzVector3f::UnitX());
NzQuaternionf x20 = x10 * x10;
NzQuaternionf x30a = x10 * x20;
NzQuaternionf x30b = x20 * x10;
WHEN("We multiply them")
{
THEN("These results are expected")
{
REQUIRE(x20 == NzQuaternionf(NzFromDegrees(20.f), NzVector3f::UnitX()));
REQUIRE(x30a == x30b);
}
}
WHEN("Convert euler to quaternion")
{
NzQuaternionf X45(NzEulerAnglesf(NzFromDegrees(45.f), 0.f, 0.f));
NzQuaternionf Y45(NzEulerAnglesf(0.f, NzFromDegrees(45.f), 0.f));
NzQuaternionf Z45(NzEulerAnglesf(0.f, 0.f, NzFromDegrees(45.f)));
THEN("They must be equal")
{
REQUIRE(X45 == NzQuaternionf(0.9238795f, 0.38268346f, 0.f, 0.f));
REQUIRE(Y45 == NzQuaternionf(0.9238795f, 0.f, 0.38268346f, 0.f));
REQUIRE(Z45 == NzQuaternionf(0.9238795f, 0.f, 0.f, 0.38268346f));
}
}
WHEN("We convert to euler angles and then to quaternions")
{
THEN("These results are expected")
{
REQUIRE(x30a.ToEulerAngles() == x30b.ToEulerAngles());
REQUIRE(x30a.ToEulerAngles().ToQuaternion() == x30b.ToEulerAngles().ToQuaternion());
NzQuaternionf tmp(1.f, 1.f, 0.f, 0.f);
tmp.Normalize();
REQUIRE(tmp == tmp.ToEulerAngles().ToQuaternion());
}
}
WHEN("We slerp")
{
THEN("The half of 10 and 30 is 20")
{
NzQuaternionf slerpx10x30a = NzQuaternionf::Slerp(x10, x30a, 0.5f);
REQUIRE(slerpx10x30a.w == Approx(x20.w));
REQUIRE(slerpx10x30a.x == Approx(x20.x));
REQUIRE(slerpx10x30a.y == Approx(x20.y));
REQUIRE(slerpx10x30a.z == Approx(x20.z));
NzQuaternionf slerpx10x30b = NzQuaternionf::Slerp(x10, x30b, 0.5f);
REQUIRE(slerpx10x30b.w == Approx(x20.w));
REQUIRE(slerpx10x30b.x == Approx(x20.x));
REQUIRE(slerpx10x30b.y == Approx(x20.y));
REQUIRE(slerpx10x30b.z == Approx(x20.z));
REQUIRE(NzQuaternionf::Slerp(x10, x30a, 0.f) == x10);
REQUIRE(NzQuaternionf::Slerp(x10, x30a, 1.f) == x30a);
}
AND_THEN("The half of 45 is 22.5")
{
NzQuaternionf quaterionA(NzFromDegrees(0.f), NzVector3f::UnitZ());
NzQuaternionf quaterionB(NzFromDegrees(45.f), NzVector3f::UnitZ());
NzQuaternionf quaternionC = NzQuaternionf::Slerp(quaterionA, quaterionB, 0.5f);
REQUIRE(quaternionC == NzQuaternionf(NzFromDegrees(22.5f), NzVector3f::UnitZ()));
}
}
}
}

95
tests/Engine/Math/Ray.cpp Normal file
View File

@@ -0,0 +1,95 @@
#include <Nazara/Math/Ray.hpp>
#include <Catch/catch.hpp>
SCENARIO("Ray", "[RAY]")
{
GIVEN("Two same rays (0, 0, 0) -> (0, 1, 0)")
{
NzRayf firstRay(NzRay<int>(NzPlane<int>::XY(), NzPlane<int>::YZ()));
NzRayf secondRay(0.f, 0.f, 0.f, 0.f, 1.f, 0.f);
WHEN("We compare them")
{
THEN("They are the same and Y axis")
{
REQUIRE(firstRay == secondRay);
REQUIRE(firstRay == NzRayf::AxisY());
}
}
WHEN("We ask for the closest point")
{
THEN("The point that is multiple on the ray, is at multiple")
{
REQUIRE(firstRay.ClosestPoint(secondRay.GetPoint(1.f)) == Approx(1.f));
}
}
WHEN("We ask for intersection")
{
THEN("For the Box collision's")
{
float tmpClosest;
float tmpFurthest;
CHECK(firstRay.Intersect(NzBoxf(-0.5f, 1.f, -0.5f, 1.f, 1.f, 1.f), &tmpClosest, &tmpFurthest));
REQUIRE(firstRay.GetPoint(tmpClosest) == NzVector3f::UnitY());
REQUIRE(firstRay.GetPoint(tmpFurthest) == (NzVector3f::UnitY() * 2.f));
CHECK(!firstRay.Intersect(NzBoxf(-10.f, 1.f, -10.f, 1.f, 1.f, 1.f), &tmpClosest, &tmpFurthest));
}
THEN("For the Plane collision's")
{
float tmpHit;
CHECK(firstRay.Intersect(NzPlanef(NzVector3f::UnitY(), 1.f), &tmpHit));
REQUIRE(firstRay.GetPoint(tmpHit) == NzVector3f::UnitY());
CHECK(firstRay.Intersect(NzPlanef::XZ(), &tmpHit));
REQUIRE(firstRay.GetPoint(tmpHit) == NzVector3f::Zero());
CHECK(firstRay.Intersect(NzPlanef(NzVector3f::UnitY(), 2.f), &tmpHit));
REQUIRE(firstRay.GetPoint(tmpHit) == 2.f * NzVector3f::UnitY());
CHECK(!firstRay.Intersect(NzPlanef(NzVector3f::UnitX(), 1.f)));
}
THEN("For the Sphere collision's")
{
float tmpClosest;
float tmpFurthest;
CHECK(firstRay.Intersect(NzSpheref(NzVector3f::UnitY(), 0.1f), &tmpClosest, &tmpFurthest));
REQUIRE(firstRay.GetPoint(tmpClosest) == NzVector3f::UnitY() * 0.9f);
REQUIRE(firstRay.GetPoint(tmpFurthest) == (NzVector3f::UnitY() * 1.1f));
CHECK(!firstRay.Intersect(NzSpheref(NzVector3f::UnitX(), 0.9f)));
}
THEN("For the OBB collision's")
{
float tmpClosest;
float tmpFurthest;
NzOrientedBoxf obb(-0.5f, 1.f, -0.5f, 1.f, 1.f, 1.f);
obb.Update(NzMatrix4f::Rotate(NzEulerAnglesf(0.f, 90.f, 0.f).ToQuaternion()));
CHECK(firstRay.Intersect(obb, &tmpClosest, &tmpFurthest));
REQUIRE(firstRay.GetPoint(tmpClosest) == NzVector3f::UnitY());
REQUIRE(firstRay.GetPoint(tmpFurthest) == (NzVector3f::UnitY() * 2.f));
obb = NzOrientedBoxf(-10.f, 1.f, -10.f, 1.f, 1.f, 1.f);
obb.Update(NzMatrix4f::Rotate(NzEulerAnglesf(0.f, 0.f, 90.f).ToQuaternion()));
CHECK(!firstRay.Intersect(obb, &tmpClosest, &tmpFurthest));
}
THEN("For the bounding volume collision's")
{
NzBoundingVolumef nullVolume(nzExtend_Null);
CHECK(!firstRay.Intersect(nullVolume));
NzBoundingVolumef infiniteVolume(nzExtend_Infinite);
CHECK(firstRay.Intersect(infiniteVolume));
}
}
}
}

View File

@@ -0,0 +1,56 @@
#include <Nazara/Math/Rect.hpp>
#include <Catch/catch.hpp>
SCENARIO("Rect", "[MATH][RECT]")
{
GIVEN("Two same rectangles center and unit lengths")
{
NzRectf firstCenterAndUnit(0.f, 0.f, 1.f, 1.f);
NzRectf secondCenterAndUnit(NzRecti(NzVector2i::Unit(), NzVector2i::Zero()));
WHEN("We ask if they are the same")
{
THEN("They should be")
{
REQUIRE(firstCenterAndUnit == secondCenterAndUnit);
REQUIRE(firstCenterAndUnit.GetCenter() == secondCenterAndUnit.GetCenter());
REQUIRE(firstCenterAndUnit.GetCorner(nzRectCorner_LeftBottom) == secondCenterAndUnit.GetCorner(nzRectCorner_LeftBottom));
CHECK(firstCenterAndUnit.IsValid());
}
}
WHEN("We move one from (0.5, 0.5)")
{
firstCenterAndUnit.Translate(NzVector2f(0.5f, 0.5f));
THEN("The collision should be (0.5, 0.5) -> (0.5, 0.5)")
{
NzRectf tmp;
CHECK(firstCenterAndUnit.Intersect(secondCenterAndUnit, &tmp));
REQUIRE(tmp == NzRectf(0.5f, 0.5f, 0.5f, 0.5f));
}
}
WHEN("We make an empty")
{
THEN("It's not valid")
{
CHECK(!(firstCenterAndUnit * 0.f).IsValid());
}
}
WHEN("We ask for infos")
{
THEN("These results are expected")
{
REQUIRE(firstCenterAndUnit.GetLengths() == NzVector2f::Unit());
REQUIRE(firstCenterAndUnit.GetMaximum() == NzVector2f::Unit());
REQUIRE(firstCenterAndUnit.GetMinimum() == NzVector2f::Zero());
REQUIRE(firstCenterAndUnit.GetNegativeVertex(NzVector2f::Unit()) == NzVector2f::Zero());
REQUIRE(firstCenterAndUnit.GetPosition() == NzVector2f::Zero());
REQUIRE(firstCenterAndUnit.GetPositiveVertex(NzVector2f::Unit()) == NzVector2f::Unit());
}
}
}
}

View File

@@ -0,0 +1,63 @@
#include <Nazara/Math/Sphere.hpp>
#include <Catch/catch.hpp>
SCENARIO("Sphere", "[MATH][SPHERE]")
{
GIVEN("Two same sphere center and unit")
{
NzSpheref firstCenterAndUnit(0.f, 0.f, 0.f, 1.f);
NzSpheref secondCenterAndUnit(NzSphere<int>(NzVector3i::Zero(), 1));
WHEN("We compare them")
{
THEN("They are the same")
{
REQUIRE(firstCenterAndUnit == secondCenterAndUnit);
}
}
WHEN("We ask if they intersect or contain")
{
THEN("These results are expected for Contains")
{
CHECK(firstCenterAndUnit.Contains(0.5f, 0.5f, 0.5f));
CHECK(firstCenterAndUnit.Contains(NzBoxf(NzVector3f::Zero(), NzVector3f::Unit() * 0.5f)));
CHECK(!firstCenterAndUnit.Contains(NzBoxf(NzVector3f::Zero(), NzVector3f::Unit() * 5.f)));
}
THEN("There are for Intersect")
{
CHECK(firstCenterAndUnit.Intersect(NzBoxf(NzVector3f::Zero(), NzVector3f::Unit() * 0.5f)));
CHECK(firstCenterAndUnit.Intersect(NzBoxf(NzVector3f::Zero(), NzVector3f::Unit() * 5.f)));
CHECK(!firstCenterAndUnit.Intersect(NzBoxf(NzVector3f::Unit() * 5.f, NzVector3f::Unit())));
CHECK(firstCenterAndUnit.Intersect(NzSpheref(NzVector3f::Zero(), 0.5f)));
CHECK(firstCenterAndUnit.Intersect(NzSpheref(NzVector3f::Zero(), 5.f)));
CHECK(!firstCenterAndUnit.Intersect(NzSpheref(NzVector3f::Unit() * 5.f, 1.f)));
}
}
WHEN("We ask for distance")
{
THEN("These results are expected")
{
REQUIRE(firstCenterAndUnit.Distance(NzVector3f::UnitX()) == Approx(1.f));
REQUIRE(firstCenterAndUnit.SquaredDistance(NzVector3f::UnitX()) == Approx(1.f));
NzSpheref tmp(NzVector3f::UnitX(), 1.f);
REQUIRE(tmp.Distance(NzVector3f::UnitX() * 4.f) == Approx(3.f));
REQUIRE(tmp.SquaredDistance(NzVector3f::UnitX() * 4.f) == Approx(9.f));
}
}
WHEN("We get sphere from box unit and center")
{
NzBoxf centerUnitBox(NzVector3f::Unit() * -0.5f, NzVector3f::Unit() * 0.5f);
THEN("This is equal to sphere center and radius 0.75")
{
REQUIRE(centerUnitBox.GetSquaredBoundingSphere() == NzSpheref(NzVector3f::Zero(), 0.75f));
}
}
}
}

View File

@@ -0,0 +1,49 @@
#include <Nazara/Math/Vector2.hpp>
#include <Catch/catch.hpp>
#include <Nazara/Math/Vector4.hpp>
SCENARIO("Vector2", "[MATH][VECTOR2]")
{
GIVEN("Two same vectors (1, 1)")
{
NzVector2f firstUnit(1.f);
NzVector2f secondUnit(NzVector2i(NzVector4i(1, 1, 3, 5)));
WHEN("We compare them")
{
THEN("They are the same")
{
REQUIRE(firstUnit == secondUnit);
}
}
WHEN("We test the dot product")
{
NzVector2f tmp(-1.f, 1.f);
THEN("These results are expected")
{
REQUIRE(firstUnit.AbsDotProduct(tmp) == Approx(2.f));
REQUIRE(firstUnit.DotProduct(tmp) == Approx(0.f));
REQUIRE(firstUnit.AngleBetween(tmp) == Approx(90.f));
}
}
WHEN("We ask for distance from (-2, -3)")
{
NzVector2f tmp(-2.f, -3.f);
NzVector2f tmp2(-1.f, -1.f);
THEN("These are expected")
{
REQUIRE(firstUnit.Distance(tmp2) == Approx(2.f * std::sqrt(2.f)));
REQUIRE(firstUnit.Distance(tmp) == Approx(5.f));
REQUIRE(firstUnit.SquaredDistance(tmp) == Approx(25.f));
REQUIRE(firstUnit.GetSquaredLength() == Approx(2.f));
REQUIRE(firstUnit.GetLength() == Approx(std::sqrt(2.f)));
}
}
}
}

View File

@@ -0,0 +1,56 @@
#include <Nazara/Math/Vector3.hpp>
#include <Catch/catch.hpp>
#include <Nazara/Math/Vector4.hpp>
SCENARIO("Vector3", "[MATH][VECTOR3]")
{
GIVEN("Two same unit vector")
{
NzVector3f firstUnit(1.f, 1.f, 1.f);
NzVector3f secondUnit(NzVector3i(NzVector4i(1, 1, 1, 5)));
WHEN("We compare them")
{
THEN("They are the same")
{
REQUIRE(firstUnit == secondUnit);
}
}
WHEN("We test the dot product")
{
NzVector3f tmp(-1.f, 0.f, 1.f);
THEN("These results are expected")
{
REQUIRE(firstUnit.AbsDotProduct(tmp) == Approx(2.f));
REQUIRE(firstUnit.DotProduct(tmp) == Approx(0.f));
REQUIRE(firstUnit.AngleBetween(tmp) == Approx(90.f));
}
}
WHEN("We test the cross product")
{
THEN("These results are expected")
{
REQUIRE(NzVector3f::CrossProduct(NzVector3f::UnitX(), NzVector3f::UnitY()) == NzVector3f::UnitZ());
REQUIRE(NzVector3f::CrossProduct(NzVector3f(1.f, 2.f, 3.f), NzVector3f(3.f, 2.f, 1.f)) == NzVector3f(-4.f, 8.f, -4.f));
}
}
WHEN("We ask for distance")
{
NzVector3f tmp(-1.f, -5.f, -8.f);
THEN("These are expected")
{
REQUIRE(firstUnit.Distance(tmp) == Approx(11.f));
REQUIRE(firstUnit.SquaredDistance(tmp) == Approx(121.f));
REQUIRE(firstUnit.GetSquaredLength() == Approx(3.f));
REQUIRE(firstUnit.GetLength() == Approx(std::sqrt(3.f)));
}
}
}
}

View File

@@ -0,0 +1,43 @@
#include <Nazara/Math/Vector4.hpp>
#include <Catch/catch.hpp>
#include <Nazara/Math/Vector3.hpp>
SCENARIO("Vector4", "[MATH][VECTOR4]")
{
GIVEN("Two same unit vector")
{
NzVector4f firstUnit(1.f, 1.f, 1.f);
NzVector4f secondUnit(NzVector4i(1, 1, 1, 1));
WHEN("We compare them")
{
THEN("They are the same")
{
REQUIRE(firstUnit == secondUnit);
}
}
WHEN("We test the dot product")
{
NzVector4f tmp(-1.f, 0.f, 1.f, 0.f);
THEN("These results are expected")
{
REQUIRE(firstUnit.AbsDotProduct(tmp) == Approx(2.f));
REQUIRE(firstUnit.DotProduct(tmp) == Approx(0.f));
}
}
WHEN("We normalize")
{
NzVector4f tmp(1.f, 1.f, 1.f, 3.f);
THEN("These results are expected")
{
REQUIRE(firstUnit.Normalize() == NzVector4f(1.f, NzVector3f::Unit()));
REQUIRE(tmp.Normalize() == NzVector4f(NzVector3f::Unit() * (1.f / 3.f), 1.f));
}
}
}
}