From 679471f74880f1348db3e0d17bbde1b0bce29597 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Mon, 22 Dec 2025 10:23:27 +0100 Subject: [PATCH] Improved unit tests --- Angle.h | 6 -- Direction.h | 6 -- Vector3.cpp | 10 +-- Vector3.h | 2 +- test/Angle_test.cc | 165 ++++++++++++++++++----------------- test/Direction_test.cc | 57 +++++++----- test/DiscreteAngle_test.cc | 82 ------------------ test/Matrix_test.cc | 173 +++++++++++++++++++------------------ test/Vector2_test.cc | 6 +- 9 files changed, 217 insertions(+), 290 deletions(-) delete mode 100644 test/DiscreteAngle_test.cc diff --git a/Angle.h b/Angle.h index e82b3af..0abba4c 100644 --- a/Angle.h +++ b/Angle.h @@ -216,12 +216,6 @@ using AngleSingle = AngleOf; using Angle16 = AngleOf; using Angle8 = AngleOf; -#if defined(ARDUINO) -using Angle = Angle16; -#else -using Angle = AngleSingle; -#endif - } // namespace LinearAlgebra #endif \ No newline at end of file diff --git a/Direction.h b/Direction.h index 8c58b6c..c9472de 100644 --- a/Direction.h +++ b/Direction.h @@ -91,12 +91,6 @@ class DirectionOf { using DirectionSingle = DirectionOf; using Direction16 = DirectionOf; -#if defined(ARDUINO) -using Direction = Direction16; -#else -using Direction = DirectionSingle; -#endif - } // namespace LinearAlgebra #endif \ No newline at end of file diff --git a/Vector3.cpp b/Vector3.cpp index ab2abe1..35bde51 100644 --- a/Vector3.cpp +++ b/Vector3.cpp @@ -236,10 +236,10 @@ Vector3Of::Vector3Of(Vector2Of v) : horizontal(v.horizontal), vertical(v.v template Vector3Of::Vector3Of(SphericalOf v) { - float cosVertical = Angle::Cos(v.direction.vertical); - float sinVertical = Angle::Sin(v.direction.vertical); - float cosHorizontal = Angle::Cos(v.direction.horizontal); - float sinHorizontal = Angle::Sin(v.direction.horizontal); + float cosVertical = AngleOf::Cos(v.direction.vertical); + float sinVertical = AngleOf::Sin(v.direction.vertical); + float cosHorizontal = AngleOf::Cos(v.direction.horizontal); + float sinHorizontal = AngleOf::Sin(v.direction.horizontal); horizontal = v.distance * sinVertical * sinHorizontal; vertical = v.distance * cosVertical; @@ -365,7 +365,7 @@ float Vector3Of::Dot(const Vector3Of& v1, const Vector3Of& v2) { } template -AngleOf Vector3Of::Angle(const Vector3Of& v1, const Vector3Of& v2) { +AngleOf Vector3Of::UnsignedAngle(const Vector3Of& v1, const Vector3Of& v2) { float denominator = sqrtf(v1.SqrMagnitude() * v2.SqrMagnitude()); if (denominator < epsilon) return AngleOf(); diff --git a/Vector3.h b/Vector3.h index 4105931..6621933 100644 --- a/Vector3.h +++ b/Vector3.h @@ -345,7 +345,7 @@ class Vector3Of { /// @remark This reterns an unsigned angle which is the shortest distance /// between the two vectors. Use Vector3::SignedAngle if a signed angle is /// needed. - static AngleOf Angle(const Vector3Of& v1, const Vector3Of& v2); + static AngleOf UnsignedAngle(const Vector3Of& v1, const Vector3Of& v2); }; using Vector3Int = Vector3Of; diff --git a/test/Angle_test.cc b/test/Angle_test.cc index 52cbff6..e1ac3a6 100644 --- a/test/Angle_test.cc +++ b/test/Angle_test.cc @@ -10,14 +10,15 @@ using namespace LinearAlgebra; #define FLOAT_INFINITY std::numeric_limits::infinity() -using AngleTypes = ::testing::Types, AngleOf, AngleOf>; +//using AngleTypes = ::testing::Types, AngleOf, AngleOf>; +using BaseTypes = ::testing::Types; template -class AngleTest : public ::testing::Test {}; -TYPED_TEST_SUITE(AngleTest, AngleTypes); +class AngleTests : public ::testing::Test {}; +TYPED_TEST_SUITE(AngleTests, BaseTypes); -TYPED_TEST(AngleTest, Construct) { - using Angle = TypeParam; +TYPED_TEST(AngleTests, Construct) { + using Angle = AngleOf; float angle = 0.0F; Angle a = Angle::Degrees(angle); EXPECT_FLOAT_EQ(a.InDegrees(), angle); @@ -31,8 +32,8 @@ TYPED_TEST(AngleTest, Construct) { EXPECT_FLOAT_EQ(a.InDegrees(), -90); } -TYPED_TEST(AngleTest, Negate) { - using Angle = TypeParam; +TYPED_TEST(AngleTests, Negate) { + using Angle = AngleOf; float angle = 0; Angle a = Angle::Degrees(angle); a = -a; @@ -44,205 +45,211 @@ TYPED_TEST(AngleTest, Negate) { EXPECT_FLOAT_EQ(a.InDegrees(), -angle); } -TYPED_TEST(AngleTest, Add) { - using Angle = TypeParam; +TYPED_TEST(AngleTests, Add) { + using Angle = AngleOf; Angle a = Angle::Degrees(-45); Angle b = Angle::Degrees(45.0F); Angle r = a + b; EXPECT_FLOAT_EQ(r.InDegrees(), 0); } -TYPED_TEST(AngleTest, Subtract) { - Angle8 a = Angle8::Degrees(0); - Angle8 b = Angle8::Degrees(45.0F); - Angle8 r = a - b; +TYPED_TEST(AngleTests, Subtract) { + using Angle = AngleOf; + Angle a = Angle::Degrees(0); + Angle b = Angle::Degrees(45.0F); + Angle r = a - b; EXPECT_FLOAT_EQ(r.InDegrees(), -45); } -TYPED_TEST(AngleTest, Compare) { - Angle8 a = Angle8::Degrees(45); +TYPED_TEST(AngleTests, Compare) { + using Angle = AngleOf; + Angle a = Angle::Degrees(45); bool r = false; - r = a > Angle8::Degrees(0); + r = a > Angle::Degrees(0); EXPECT_TRUE(r) << "45 > 0"; - r = a > Angle8::Degrees(90); + r = a > Angle::Degrees(90); EXPECT_FALSE(r) << "45 > 90"; - r = a > Angle8::Degrees(-90); + r = a > Angle::Degrees(-90); EXPECT_TRUE(r) << "45 > -90"; } -TYPED_TEST(AngleTest, Normalize) { - Angle8 r = Angle8(); +TYPED_TEST(AngleTests, Normalize) { + using Angle = AngleOf; + Angle r = Angle(); - r = Angle8::Normalize(Angle8::Degrees(90.0f)); + r = Angle::Normalize(Angle::Degrees(90.0f)); EXPECT_FLOAT_EQ(r.InDegrees(), 90) << "Normalize 90"; - r = Angle8::Normalize(Angle8::Degrees(-90)); + r = Angle::Normalize(Angle::Degrees(-90)); EXPECT_FLOAT_EQ(r.InDegrees(), -90) << "Normalize -90"; - r = Angle8::Normalize(Angle8::Degrees(270)); + r = Angle::Normalize(Angle::Degrees(270)); EXPECT_FLOAT_EQ(r.InDegrees(), -90) << "Normalize 270"; - r = Angle8::Normalize(Angle8::Degrees(270 + 360)); + r = Angle::Normalize(Angle::Degrees(270 + 360)); EXPECT_FLOAT_EQ(r.InDegrees(), -90) << "Normalize 270+360"; - r = Angle8::Normalize(Angle8::Degrees(-270)); + r = Angle::Normalize(Angle::Degrees(-270)); EXPECT_FLOAT_EQ(r.InDegrees(), 90) << "Normalize -270"; - r = Angle8::Normalize(Angle8::Degrees(-270 - 360)); + r = Angle::Normalize(Angle::Degrees(-270 - 360)); EXPECT_FLOAT_EQ(r.InDegrees(), 90) << "Normalize -270-360"; - r = Angle8::Normalize(Angle8::Degrees(0)); + r = Angle::Normalize(Angle::Degrees(0)); EXPECT_FLOAT_EQ(r.InDegrees(), 0) << "Normalize 0"; if (false) { // std::numeric_limits::is_iec559) { // Infinites are not supported - r = Angle8::Normalize(Angle8::Degrees(FLOAT_INFINITY)); + r = Angle::Normalize(Angle::Degrees(FLOAT_INFINITY)); EXPECT_FLOAT_EQ(r.InDegrees(), FLOAT_INFINITY) << "Normalize INFINITY"; - r = Angle8::Normalize(Angle8::Degrees(-FLOAT_INFINITY)); + r = Angle::Normalize(Angle::Degrees(-FLOAT_INFINITY)); EXPECT_FLOAT_EQ(r.InDegrees(), -FLOAT_INFINITY) << "Normalize INFINITY"; } } -TYPED_TEST(AngleTest, Clamp) { - Angle8 r = Angle8(); +TYPED_TEST(AngleTests, Clamp) { + using Angle = AngleOf; + Angle r = Angle(); - // Clamp(1, 0, 2) will fail because Angle8 does not have enough resolution for + // Clamp(1, 0, 2) will fail because Angle does not have enough resolution for // this. Instead we use Clamp(10, 0, 20) etc. - r = Angle8::Clamp(Angle8::Degrees(10), Angle8::Degrees(0), - Angle8::Degrees(20)); + r = Angle::Clamp(Angle::Degrees(10), Angle::Degrees(0), + Angle::Degrees(20)); EXPECT_NEAR(r.InDegrees(), 10, 1.0e-0) << "Clamp 10 0 20"; - r = Angle8::Clamp(Angle8::Degrees(-10), Angle8::Degrees(0), - Angle8::Degrees(20)); + r = Angle::Clamp(Angle::Degrees(-10), Angle::Degrees(0), + Angle::Degrees(20)); EXPECT_FLOAT_EQ(r.InDegrees(), 0) << "Clamp -10 0 20"; - r = Angle8::Clamp(Angle8::Degrees(30), Angle8::Degrees(0), - Angle8::Degrees(20)); + r = Angle::Clamp(Angle::Degrees(30), Angle::Degrees(0), + Angle::Degrees(20)); EXPECT_NEAR(r.InDegrees(), 20, 1.0e-0) << "Clamp 30 0 20"; - r = Angle8::Clamp(Angle8::Degrees(10), Angle8::Degrees(0), - Angle8::Degrees(0)); + r = Angle::Clamp(Angle::Degrees(10), Angle::Degrees(0), + Angle::Degrees(0)); EXPECT_FLOAT_EQ(r.InDegrees(), 0) << "Clamp 10 0 0"; - r = Angle8::Clamp(Angle8::Degrees(0), Angle8::Degrees(0), Angle8::Degrees(0)); + r = Angle::Clamp(Angle::Degrees(0), Angle::Degrees(0), Angle::Degrees(0)); EXPECT_FLOAT_EQ(r.InDegrees(), 0) << "Clamp 0 0 0"; - r = Angle8::Clamp(Angle8::Degrees(0), Angle8::Degrees(10), - Angle8::Degrees(-10)); + r = Angle::Clamp(Angle::Degrees(0), Angle::Degrees(10), + Angle::Degrees(-10)); EXPECT_FLOAT_EQ(r.InDegrees(), 0) << "Clamp 0 10 -10"; if (false) { // std::numeric_limits::is_iec559) { // Infinites are not supported - r = Angle8::Clamp(Angle8::Degrees(10), Angle8::Degrees(0), - Angle8::Degrees(FLOAT_INFINITY)); + r = Angle::Clamp(Angle::Degrees(10), Angle::Degrees(0), + Angle::Degrees(FLOAT_INFINITY)); EXPECT_NEAR(r.InDegrees(), 10, 1.0e-0) << "Clamp 1 0 INFINITY"; - r = Angle8::Clamp(Angle8::Degrees(10), Angle8::Degrees(-FLOAT_INFINITY), - Angle8::Degrees(10)); + r = Angle::Clamp(Angle::Degrees(10), Angle::Degrees(-FLOAT_INFINITY), + Angle::Degrees(10)); EXPECT_NEAR(r.InDegrees(), 10, 1.0e-0) << "Clamp 1 -INFINITY 1"; } } -// TEST(Angle8, Difference) { -// Angle8 r = 0; +// TEST(Angle, Difference) { +// using Angle = AngleOf; +// Angle r = 0; -// r = Angle8::Difference(0, 90); +// r = Angle::Difference(0, 90); // EXPECT_FLOAT_EQ(r.InDegrees(), 90) << "Difference 0 90"; -// r = Angle8::Difference(0, -90); +// r = Angle::Difference(0, -90); // EXPECT_FLOAT_EQ(r.InDegrees(), -90) << "Difference 0 -90"; -// r = Angle8::Difference(0, 270); +// r = Angle::Difference(0, 270); // EXPECT_FLOAT_EQ(r.InDegrees(), -90) << "Difference 0 270"; -// r = Angle8::Difference(0, -270); +// r = Angle::Difference(0, -270); // EXPECT_FLOAT_EQ(r.InDegrees(), 90) << "Difference 0 -270"; -// r = Angle8::Difference(90, 0); +// r = Angle::Difference(90, 0); // EXPECT_FLOAT_EQ(r.InDegrees(), -90) << "Difference 90 0"; -// r = Angle8::Difference(-90, 0); +// r = Angle::Difference(-90, 0); // EXPECT_FLOAT_EQ(r.InDegrees(), 90) << "Difference -90 0"; -// r = Angle8::Difference(0, 0); +// r = Angle::Difference(0, 0); // EXPECT_FLOAT_EQ(r.InDegrees(), 0) << "Difference 0 0"; -// r = Angle8::Difference(90, 90); +// r = Angle::Difference(90, 90); // EXPECT_FLOAT_EQ(r.InDegrees(), 0) << "Difference 90 90"; // if (std::numeric_limits::is_iec559) { -// r = Angle8::Difference(0, INFINITY); +// r = Angle::Difference(0, INFINITY); // EXPECT_FLOAT_EQ(r.InDegrees(), INFINITY) << "Difference 0 INFINITY"; -// r = Angle8::Difference(0, -INFINITY); +// r = Angle::Difference(0, -INFINITY); // EXPECT_FLOAT_EQ(r.InDegrees(), -INFINITY) << "Difference 0 -INFINITY"; -// r = Angle8::Difference(-INFINITY, INFINITY); +// r = Angle::Difference(-INFINITY, INFINITY); // EXPECT_FLOAT_EQ(r.InDegrees(), INFINITY) << "Difference -INFINITY // INFINITY"; // } // } -TYPED_TEST(AngleTest, MoveTowards) { - Angle8 r = Angle8(); +TYPED_TEST(AngleTests, MoveTowards) { + using Angle = AngleOf; + Angle r = Angle(); - r = Angle8::MoveTowards(Angle8::Degrees(0), Angle8::Degrees(90), 30); + r = Angle::MoveTowards(Angle::Degrees(0), Angle::Degrees(90), 30); EXPECT_NEAR(r.InDegrees(), 30, 1.0e-0) << "MoveTowards 0 90 30"; - r = Angle8::MoveTowards(Angle8::Degrees(0), Angle8::Degrees(90), 90); + r = Angle::MoveTowards(Angle::Degrees(0), Angle::Degrees(90), 90); EXPECT_FLOAT_EQ(r.InDegrees(), 90) << "MoveTowards 0 90 90"; - r = Angle8::MoveTowards(Angle8::Degrees(0), Angle8::Degrees(-90), 180); + r = Angle::MoveTowards(Angle::Degrees(0), Angle::Degrees(-90), 180); EXPECT_FLOAT_EQ(r.InDegrees(), 90) << "MoveTowards 0 -90 -180"; - r = Angle8::MoveTowards(Angle8::Degrees(0), Angle8::Degrees(90), 270); + r = Angle::MoveTowards(Angle::Degrees(0), Angle::Degrees(90), 270); EXPECT_FLOAT_EQ(r.InDegrees(), 90) << "MoveTowards 0 90 270"; - r = Angle8::MoveTowards(Angle8::Degrees(0), Angle8::Degrees(90), -30); + r = Angle::MoveTowards(Angle::Degrees(0), Angle::Degrees(90), -30); EXPECT_FLOAT_EQ(r.InDegrees(), 0) << "MoveTowards 0 90 -30"; - r = Angle8::MoveTowards(Angle8::Degrees(0), Angle8::Degrees(-90), -30); + r = Angle::MoveTowards(Angle::Degrees(0), Angle::Degrees(-90), -30); EXPECT_FLOAT_EQ(r.InDegrees(), 0) << "MoveTowards 0 -90 -30"; - r = Angle8::MoveTowards(Angle8::Degrees(0), Angle8::Degrees(-90), -90); + r = Angle::MoveTowards(Angle::Degrees(0), Angle::Degrees(-90), -90); EXPECT_FLOAT_EQ(r.InDegrees(), 0) << "MoveTowards 0 -90 -90"; - r = Angle8::MoveTowards(Angle8::Degrees(0), Angle8::Degrees(-90), -180); + r = Angle::MoveTowards(Angle::Degrees(0), Angle::Degrees(-90), -180); EXPECT_FLOAT_EQ(r.InDegrees(), 0) << "MoveTowards 0 -90 -180"; - r = Angle8::MoveTowards(Angle8::Degrees(0), Angle8::Degrees(-90), -270); + r = Angle::MoveTowards(Angle::Degrees(0), Angle::Degrees(-90), -270); EXPECT_FLOAT_EQ(r.InDegrees(), 0) << "MoveTowards 0 -90 -270"; - r = Angle8::MoveTowards(Angle8::Degrees(0), Angle8::Degrees(90), 0); + r = Angle::MoveTowards(Angle::Degrees(0), Angle::Degrees(90), 0); EXPECT_FLOAT_EQ(r.InDegrees(), 0) << "MoveTowards 0 90 0"; - r = Angle8::MoveTowards(Angle8::Degrees(0), Angle8::Degrees(0), 0); + r = Angle::MoveTowards(Angle::Degrees(0), Angle::Degrees(0), 0); EXPECT_FLOAT_EQ(r.InDegrees(), 0) << "MoveTowards 0 0 0"; - r = Angle8::MoveTowards(Angle8::Degrees(0), Angle8::Degrees(0), 30); + r = Angle::MoveTowards(Angle::Degrees(0), Angle::Degrees(0), 30); EXPECT_FLOAT_EQ(r.InDegrees(), 0) << "MoveTowards 0 0 30"; if (false) { // std::numeric_limits::is_iec559) { // infinites are not supported - r = Angle8::MoveTowards(Angle8::Degrees(0), Angle8::Degrees(90), + r = Angle::MoveTowards(Angle::Degrees(0), Angle::Degrees(90), FLOAT_INFINITY); EXPECT_FLOAT_EQ(r.InDegrees(), 90) << "MoveTowards 0 90 FLOAT_INFINITY"; - r = Angle8::MoveTowards(Angle8::Degrees(0), Angle8::Degrees(FLOAT_INFINITY), + r = Angle::MoveTowards(Angle::Degrees(0), Angle::Degrees(FLOAT_INFINITY), 30); EXPECT_FLOAT_EQ(r.InDegrees(), 30) << "MoveTowards 0 FLOAT_INFINITY 30"; - r = Angle8::MoveTowards(Angle8::Degrees(0), Angle8::Degrees(-90), + r = Angle::MoveTowards(Angle::Degrees(0), Angle::Degrees(-90), -FLOAT_INFINITY); EXPECT_FLOAT_EQ(r.InDegrees(), FLOAT_INFINITY) << "MoveTowards 0 -90 -FLOAT_INFINITY"; - r = Angle8::MoveTowards(Angle8::Degrees(0), - Angle8::Degrees(-FLOAT_INFINITY), -30); + r = Angle::MoveTowards(Angle::Degrees(0), + Angle::Degrees(-FLOAT_INFINITY), -30); EXPECT_FLOAT_EQ(r.InDegrees(), 30) << "MoveTowards 0 -FLOAT_INFINITY -30"; } } diff --git a/test/Direction_test.cc b/test/Direction_test.cc index 6489caa..6310ca4 100644 --- a/test/Direction_test.cc +++ b/test/Direction_test.cc @@ -10,49 +10,58 @@ using namespace LinearAlgebra; #define FLOAT_INFINITY std::numeric_limits::infinity() -TEST(Direction16, Compare) { - Direction16 d = Direction16::Degrees(45, 135); +using BaseTypes = ::testing::Types; + +template +class DirectionTests : public ::testing::Test {}; +TYPED_TEST_SUITE(DirectionTests, BaseTypes); + +TYPED_TEST(DirectionTests, Compare) { + using Direction = DirectionOf; + using Angle = AngleOf; + Direction d = Direction::Degrees(45, 135); bool r; - r = (d == Direction16(Angle16::Degrees(45), Angle16::Degrees(135))); + r = (d == Direction(Angle::Degrees(45), Angle::Degrees(135))); EXPECT_TRUE(r) << "45,135 == 45, 135"; - r = (d == - Direction16(Angle16::Degrees(45 + 360), Angle16::Degrees(135 - 360))); + r = (d == Direction(Angle::Degrees(45 + 360), Angle::Degrees(135 - 360))); EXPECT_TRUE(r) << "45+360, 135-360 == 45, 135"; } -TEST(Direction16, Inverse) { - Direction16 d; - Direction16 r; +TYPED_TEST(DirectionTests, Inverse) { + using Direction = DirectionOf; + Direction d; + Direction r; - d = Direction16::Degrees(45, 135); + d = Direction::Degrees(45, 135); r = -d; - EXPECT_EQ(r, Direction16::Degrees(-135, -135)) << "-(45, 135)"; + EXPECT_EQ(r, Direction::Degrees(-135, -135)) << "-(45, 135)"; - d = Direction16::Degrees(-45, -135); + d = Direction::Degrees(-45, -135); r = -d; - EXPECT_EQ(r, Direction16::Degrees(135, 135)) << "-(-45, -135)"; + EXPECT_EQ(r, Direction::Degrees(135, 135)) << "-(-45, -135)"; - d = Direction16::Degrees(0, 0); + d = Direction::Degrees(0, 0); r = -d; - EXPECT_EQ(r, Direction16::Degrees(180, 0)) << "-(0, 0)"; + EXPECT_EQ(r, Direction::Degrees(180, 0)) << "-(0, 0)"; - d = Direction16::Degrees(0, 45); + d = Direction::Degrees(0, 45); r = -d; - EXPECT_EQ(r, Direction16::Degrees(180, -45)) << "-(0, 45)"; + EXPECT_EQ(r, Direction::Degrees(180, -45)) << "-(0, 45)"; } -TEST(Direction16, Equality) { - Direction16 d; - d = Direction16::Degrees(135, 45); - EXPECT_EQ(d, Direction16::Degrees(135, 45)) << "(135, 45) == (135, 45)"; - EXPECT_EQ(d, Direction16::Degrees(135 + 360, 45)) +TYPED_TEST(DirectionTests, Equality) { + using Direction = DirectionOf; + Direction d; + d = Direction::Degrees(135, 45); + EXPECT_EQ(d, Direction::Degrees(135, 45)) << "(135, 45) == (135, 45)"; + EXPECT_EQ(d, Direction::Degrees(135 + 360, 45)) << "(135, 45) == (135 + 360, 45) "; - EXPECT_EQ(d, Direction16::Degrees(135 - 360, 45)) + EXPECT_EQ(d, Direction::Degrees(135 - 360, 45)) << "(135, 135) == (135 - 360, 45) "; - d = Direction16::Degrees(0, 45 + 180); - EXPECT_EQ(d, Direction16::Degrees(180, -45)) << "(0, 45+180) == (180, -45)"; + d = Direction::Degrees(0, 45 + 180); + EXPECT_EQ(d, Direction::Degrees(180, -45)) << "(0, 45+180) == (180, -45)"; } #endif \ No newline at end of file diff --git a/test/DiscreteAngle_test.cc b/test/DiscreteAngle_test.cc deleted file mode 100644 index 91a7cc8..0000000 --- a/test/DiscreteAngle_test.cc +++ /dev/null @@ -1,82 +0,0 @@ -/* -#if GTEST -#include - -#include -#include - -#include "Angle.h" -// #include "Angle16.h" -// #include "Angle8.h" - -#define FLOAT_INFINITY std::numeric_limits::infinity() - -TEST(Angle8, Construct) { - float angle = 0.0F; - Angle8 a = Angle8::Degrees(angle); - EXPECT_FLOAT_EQ(a.InDegrees(), angle); - - angle = -180.0F; - a = Angle8::Degrees(angle); - EXPECT_FLOAT_EQ(a.InDegrees(), angle); -} - -TEST(Angle8, Negate) { - float angle = 0; - Angle8 a = Angle8::Degrees(angle); - a = -a; - EXPECT_FLOAT_EQ(a.InDegrees(), angle); - - angle = 90.0F; - a = Angle8::Degrees(angle); - a = -a; - EXPECT_FLOAT_EQ(a.InDegrees(), -angle); -} - -TEST(Angle8, Add) { - Angle8 a = Angle8::Degrees(-45); - Angle8 b = Angle8::Degrees(45.0F); - Angle8 r = a + b; - EXPECT_FLOAT_EQ(r.InDegrees(), 0); -} - -TEST(Angle8, Subtract) { - Angle8 a = Angle8::Degrees(0); - Angle8 b = Angle8::Degrees(45.0F); - Angle8 r = a - b; - EXPECT_FLOAT_EQ(r.InDegrees(), -45); -} - -TEST(Angle16, Construct) { - Angle16 a = Angle16::Degrees(0.0F); - EXPECT_FLOAT_EQ(a.InDegrees(), 0); -} - -TEST(Angle16, Negate) { - float angle = 0; - Angle16 a = Angle16::Degrees(angle); - a = -a; - EXPECT_FLOAT_EQ(a.InDegrees(), angle); - - angle = 90.0F; - a = Angle16::Degrees(angle); - a = -a; - EXPECT_FLOAT_EQ(a.InDegrees(), -angle); -} - -TEST(Angle16, Subtract) { - Angle16 a = Angle16::Degrees(0); - Angle16 b = Angle16::Degrees(45.0F); - Angle16 r = a - b; - EXPECT_FLOAT_EQ(r.InDegrees(), -45); -} - -TEST(Angle16, Add) { - Angle16 a = Angle16::Degrees(-45); - Angle16 b = Angle16::Degrees(45.0F); - Angle16 r = a + b; - EXPECT_FLOAT_EQ(r.InDegrees(), 0); -} - -#endif -*/ \ No newline at end of file diff --git a/test/Matrix_test.cc b/test/Matrix_test.cc index 7f167eb..d32ba67 100644 --- a/test/Matrix_test.cc +++ b/test/Matrix_test.cc @@ -11,17 +11,18 @@ bool isDefaultValue(const T& value) { if constexpr (std::is_arithmetic::value) { return value == T(); // Default constructed value for T (0 or 0.0) } - //return false; // Improve this if you handle non-arithmetic types + // return false; // Improve this if you handle non-arithmetic types } -using MatrixTypes = ::testing::Types, Matrix2Of>; +// using MatrixTypes = ::testing::Types, Matrix2Of>; +using BaseTypes = ::testing::Types; template -class Matrix2Test : public ::testing::Test {}; -TYPED_TEST_SUITE(Matrix2Test, MatrixTypes); +class Matrix2Tests : public ::testing::Test {}; +TYPED_TEST_SUITE(Matrix2Tests, BaseTypes); -TYPED_TEST(Matrix2Test, Zero) { - using Matrix2 = TypeParam; +TYPED_TEST(Matrix2Tests, Zero) { + using Matrix2 = Matrix2Of; // Test case 1: 2x2 zero matrix Matrix2 zeroMatrix = Matrix2::Zero(2, 2); EXPECT_TRUE(zeroMatrix.RowCount() == 2); @@ -56,60 +57,81 @@ TYPED_TEST(Matrix2Test, Zero) { std::cout << "Test case 4 passed: 0x0 matrix\n"; } -// TYPED_TEST(Matrix2Test, Multiplication) { -// using T = typename TestFixture::TypeParam; -// using Matrix2 = TypeParam; -// // Test 1: Multiplying two 2x2 matrices -// T dataA[] = {1, 2, 3, 4}; -// T dataB[] = {5, 6, 7, 8}; -// Matrix2 A(dataA, 2, 2); -// Matrix2 B(dataB, 2, 2); +TYPED_TEST(Matrix2Tests, Multiplication) { + using T = TypeParam; + using Matrix2 = Matrix2Of; -// Matrix2 result = A * B; + // Test 1: Multiplying two 2x2 matrices + T dataA[] = {1, 2, 3, 4}; + T dataB[] = {5, 6, 7, 8}; + Matrix2 A(2, 2, dataA); + Matrix2 B(2, 2, dataB); -// float expectedData[] = {19, 22, 43, 50}; -// for (int i = 0; i < 4; ++i) -// EXPECT_TRUE(result.data[i] == expectedData[i]); -// std::cout << "Test 1 passed: 2x2 matrix multiplication.\n"; + Matrix2 result = A * B; -// // Test 2: Multiplying a 3x2 matrix with a 2x3 matrix -// float dataC[] = {1, 2, 3, 4, 5, 6}; -// float dataD[] = {7, 8, 9, 10, 11, 12}; -// Matrix2 C(dataC, 3, 2); -// Matrix2 D(dataD, 2, 3); + float expectedData[] = {19, 22, 43, 50}; + // for (int i = 0; i < 4; ++i) + for (unsigned int rowIx = 0; rowIx < 2; ++rowIx) { + for (unsigned int colIx = 0; colIx < 2; ++colIx) { + EXPECT_TRUE(result(rowIx, colIx) == expectedData[rowIx * 2 + colIx]); + } + } + // EXPECT_TRUE(result.data[i] == expectedData[i]); + std::cout << "Test 1 passed: 2x2 matrix multiplication.\n"; -// Matrix2 result2 = C * D; + // Test 2: Multiplying a 3x2 matrix with a 2x3 matrix + T dataC[] = {1, 2, 3, 4, 5, 6}; + T dataD[] = {7, 8, 9, 10, 11, 12}; + Matrix2 C(3, 2, dataC); + Matrix2 D(2, 3, dataD); -// float expectedData2[] = {27, 30, 33, 61, 68, 75, 95, 106, 117}; -// for (int i = 0; i < 9; ++i) -// EXPECT_TRUE(result2.data[i] == expectedData2[i]); -// std::cout << "Test 2 passed: 3x2 * 2x3 matrix multiplication.\n"; + Matrix2 result2 = C * D; -// // Test 3: Multiplying with a zero matrix -// Matrix2 zeroMatrix = Matrix2::Zero(2, 2); -// Matrix2 result3 = A * zeroMatrix; + float expectedData2[] = {27, 30, 33, 61, 68, 75, 95, 106, 117}; + for (unsigned int rowIx = 0; rowIx < 3; ++rowIx) { + for (unsigned int colIx = 0; colIx < 3; ++colIx) { + EXPECT_TRUE(result2(rowIx, colIx) == expectedData2[rowIx * 3 + colIx]); + } + } + // for (int i = 0; i < 9; ++i) + // EXPECT_TRUE(result2.data[i] == expectedData2[i]); + std::cout << "Test 2 passed: 3x2 * 2x3 matrix multiplication.\n"; -// for (int i = 0; i < 4; ++i) -// EXPECT_TRUE(result3.data[i] == 0); -// std::cout << "Test 3 passed: Multiplication with zero matrix.\n"; + // Test 3: Multiplying with a zero matrix + Matrix2 zeroMatrix = Matrix2::Zero(2, 2); + Matrix2 result3 = A * zeroMatrix; -// // Test 4: Multiplying with an identity matrix -// Matrix2 identityMatrix = Matrix2::Identity(2); -// Matrix2 result4 = A * identityMatrix; + for (unsigned int rowIx = 0; rowIx < 2; ++rowIx) { + for (unsigned int colIx = 0; colIx < 2; ++colIx) { + EXPECT_TRUE(result3(rowIx, colIx) == 0); + } + } + // for (int i = 0; i < 4; ++i) + // EXPECT_TRUE(result3.data[i] == 0); + std::cout << "Test 3 passed: Multiplication with zero matrix.\n"; -// for (int i = 0; i < 4; ++i) -// EXPECT_TRUE(result4.data[i] == A.data[i]); -// std::cout << "Test 4 passed: Multiplication with identity matrix.\n"; -// } + // Test 4: Multiplying with an identity matrix + Matrix2 identityMatrix = Matrix2::Identity(2); + Matrix2 result4 = A * identityMatrix; + + for (unsigned int rowIx = 0; rowIx < 2; ++rowIx) { + for (unsigned int colIx = 0; colIx < 2; ++colIx) { + EXPECT_TRUE(result4(rowIx, colIx) == A(rowIx, colIx)); + } + } + // for (int i = 0; i < 4; ++i) + // EXPECT_TRUE(result4.data[i] == A.data[i]); + std::cout << "Test 4 passed: Multiplication with identity matrix.\n"; +} template class Matrix1Test : public ::testing::Test { protected: }; -TYPED_TEST_SUITE(Matrix1Test, MatrixTypes); +TYPED_TEST_SUITE(Matrix1Test, BaseTypes); -TYPED_TEST(Matrix2Test, Init) { - using Matrix2 = TypeParam; +TYPED_TEST(Matrix2Tests, Init) { + using Matrix2 = Matrix2Of; // zero Matrix2 m0 = Matrix2(0, 0); @@ -129,8 +151,8 @@ TYPED_TEST(Matrix2Test, Init) { // parameters are unsigned } -TYPED_TEST(Matrix2Test, Transpose) { - using Matrix2 = TypeParam; +TYPED_TEST(Matrix2Tests, Transpose) { + using Matrix2 = Matrix2Of; Matrix2 m = Matrix2(1, 1); m(0, 0) = 1; @@ -170,14 +192,14 @@ TYPED_TEST(Matrix2Test, Transpose) { EXPECT_EQ(m12.RowCount(), 1); EXPECT_EQ(m12.ColCount(), 2); - //float data21[] = {0.0F, 0.0F}; + // float data21[] = {0.0F, 0.0F}; Matrix2 r21 = Matrix2(2, 1); - r21(0,0) = 0; - r21(1,0) = 0; + r21(0, 0) = 0; + r21(1, 0) = 0; EXPECT_EQ(r21.RowCount(), 2); EXPECT_EQ(r21.ColCount(), 1); - //m12.Transpose(&r21); + // m12.Transpose(&r21); r21 = m12.Transposed(); EXPECT_EQ(r21.RowCount(), 2); EXPECT_EQ(r21.ColCount(), 1); @@ -189,7 +211,7 @@ TYPED_TEST(Matrix2Test, Transpose) { EXPECT_EQ(r12.RowCount(), 1); EXPECT_EQ(r12.ColCount(), 2); - //m12.Transpose(&r12); + // m12.Transpose(&r12); r12 = m12.Transposed(); EXPECT_EQ(r12.RowCount(), 2); EXPECT_EQ(r12.ColCount(), 1); @@ -197,42 +219,42 @@ TYPED_TEST(Matrix2Test, Transpose) { EXPECT_EQ(r12(0, 1), 2); } -TYPED_TEST(Matrix2Test, Multiply) { - using Matrix1 = TypeParam; - Matrix1 m12 = Matrix1(1, 2); - m12(0,0) = 1; - m12(0,1) = 2; +TYPED_TEST(Matrix2Tests, Multiply) { + using Matrix2 = Matrix2Of; + Matrix2 m12 = Matrix2(1, 2); + m12(0, 0) = 1; + m12(0, 1) = 2; EXPECT_EQ(m12.RowCount(), 1); EXPECT_EQ(m12.ColCount(), 2); EXPECT_EQ(m12(0, 0), 1); EXPECT_EQ(m12(0, 1), 2); - //float m21data[] = {3.0F, 4.0F}; - Matrix1 m21 = Matrix1(2, 1); //, m21data); - m21(0,0) = 3; - m21(1,0) = 4; + // float m21data[] = {3.0F, 4.0F}; + Matrix2 m21 = Matrix2(2, 1); //, m21data); + m21(0, 0) = 3; + m21(1, 0) = 4; EXPECT_EQ(m21.RowCount(), 2); EXPECT_EQ(m21.ColCount(), 1); EXPECT_EQ(m21(0, 0), 3); EXPECT_EQ(m21(1, 0), 4); - //float r11data[] = {0.0F}; - Matrix1 r11 = Matrix1::Zero(1, 1); //, r11data); + // float r11data[] = {0.0F}; + Matrix2 r11 = Matrix2::Zero(1, 1); //, r11data); EXPECT_EQ(r11.RowCount(), 1); EXPECT_EQ(r11.ColCount(), 1); - r11 = m12 * m21; //Matrix1::Multiply(&m12, &m21, &r11); + r11 = m12 * m21; // Matrix1::Multiply(&m12, &m21, &r11); EXPECT_EQ(r11.RowCount(), 1); EXPECT_EQ(r11.ColCount(), 1); EXPECT_EQ(r11(0, 0), 11); - //float r22data[] = {0.0F, 0.0F, 0.0F, 0.0F}; - Matrix1 r22 = Matrix1::Zero(2, 2); //, r22data); + // float r22data[] = {0.0F, 0.0F, 0.0F, 0.0F}; + Matrix2 r22 = Matrix2::Zero(2, 2); //, r22data); - //Matrix1::Multiply(&m21, &m12, &r22); + // Matrix1::Multiply(&m21, &m12, &r22); r22 = m21 * m12; EXPECT_EQ(r22.RowCount(), 2); EXPECT_EQ(r22.ColCount(), 2); @@ -242,21 +264,4 @@ TYPED_TEST(Matrix2Test, Multiply) { EXPECT_EQ(r22(1, 1), 8); } -// TYPED_TEST(Matrix2Test, Multiply_Vector3) { -// using Matrix2 = TypeParam; -// auto v = Vector3(1.0, 2.0, 3.0); -// Matrix1Of r = Matrix1Of::zero; - -// // float m13data[] = {3.0, 4.0, 5.0}; -// // Matrix1 m13 = Matrix1(1, 3, m13data); -// // Vector3 r = Matrix1::Multiply(&m13, v); - -// //float m33data[] = {1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0}; -// Matrix2 m33 = Matrix2::Identity(3, 3); //, m33data); - -// //r = Matrix1::Multiply(&m33, v); -// r = m33 * v; -// EXPECT_FLOAT_EQ(Vector3::Distance(r, Vector3(1.0f, 2.0f, 3.0f)), 0); -// } - #endif \ No newline at end of file diff --git a/test/Vector2_test.cc b/test/Vector2_test.cc index 0eece89..8b5b127 100644 --- a/test/Vector2_test.cc +++ b/test/Vector2_test.cc @@ -391,10 +391,10 @@ TEST(Vector2, Distance) { } } -TEST(Vector2, Angle) { +TEST(Vector2, Angle) { Vector2 v1 = Vector2(4, 5); Vector2 v2 = Vector2(1, 2); - Angle f = Angle::zero; + AngleSingle f = AngleSingle::zero; bool r = false; f = Vector2::UnsignedAngle(v1, v2); @@ -426,7 +426,7 @@ TEST(Vector2, Angle) { TEST(Vector2, SignedAngle) { Vector2 v1 = Vector2(4, 5); Vector2 v2 = Vector2(1, 2); - Angle f = Angle::zero; + AngleSingle f = AngleSingle::zero; bool r = false; f = Vector2::SignedAngle(v1, v2);