LinearAlgebra-cpp/test/Direction_test.cc
Pascal Serrarens 679471f748
All checks were successful
Build and Run C++ Unit Tests / build-and-test (push) Successful in 2m1s
Improved unit tests
2025-12-22 10:23:27 +01:00

67 lines
1.8 KiB
C++

#if GTEST
#include <gtest/gtest.h>
#include <math.h>
#include <limits>
#include "Direction.h"
using namespace LinearAlgebra;
#define FLOAT_INFINITY std::numeric_limits<float>::infinity()
using BaseTypes = ::testing::Types<float, signed short>;
template <typename T>
class DirectionTests : public ::testing::Test {};
TYPED_TEST_SUITE(DirectionTests, BaseTypes);
TYPED_TEST(DirectionTests, Compare) {
using Direction = DirectionOf<TypeParam>;
using Angle = AngleOf<TypeParam>;
Direction d = Direction::Degrees(45, 135);
bool r;
r = (d == Direction(Angle::Degrees(45), Angle::Degrees(135)));
EXPECT_TRUE(r) << "45,135 == 45, 135";
r = (d == Direction(Angle::Degrees(45 + 360), Angle::Degrees(135 - 360)));
EXPECT_TRUE(r) << "45+360, 135-360 == 45, 135";
}
TYPED_TEST(DirectionTests, Inverse) {
using Direction = DirectionOf<TypeParam>;
Direction d;
Direction r;
d = Direction::Degrees(45, 135);
r = -d;
EXPECT_EQ(r, Direction::Degrees(-135, -135)) << "-(45, 135)";
d = Direction::Degrees(-45, -135);
r = -d;
EXPECT_EQ(r, Direction::Degrees(135, 135)) << "-(-45, -135)";
d = Direction::Degrees(0, 0);
r = -d;
EXPECT_EQ(r, Direction::Degrees(180, 0)) << "-(0, 0)";
d = Direction::Degrees(0, 45);
r = -d;
EXPECT_EQ(r, Direction::Degrees(180, -45)) << "-(0, 45)";
}
TYPED_TEST(DirectionTests, Equality) {
using Direction = DirectionOf<TypeParam>;
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, Direction::Degrees(135 - 360, 45))
<< "(135, 135) == (135 - 360, 45) ";
d = Direction::Degrees(0, 45 + 180);
EXPECT_EQ(d, Direction::Degrees(180, -45)) << "(0, 45+180) == (180, -45)";
}
#endif