wpilibc: Add unit-safety to C++ geometry classes (#1811)

This commit is contained in:
Prateek Machiraju
2019-08-17 01:00:33 -04:00
committed by Peter Johnson
parent c07ac23532
commit 8f386f6bb3
12 changed files with 108 additions and 117 deletions

View File

@@ -17,39 +17,39 @@ using namespace frc;
static constexpr double kEpsilon = 1E-9;
TEST(Rotation2dTest, RadiansToDegrees) {
const Rotation2d one{wpi::math::pi / 3};
const Rotation2d two{wpi::math::pi / 4};
const Rotation2d one{units::radian_t(wpi::math::pi / 3)};
const Rotation2d two{units::radian_t(wpi::math::pi / 4)};
EXPECT_NEAR(one.Degrees(), 60.0, kEpsilon);
EXPECT_NEAR(two.Degrees(), 45.0, kEpsilon);
EXPECT_NEAR(one.Degrees().to<double>(), 60.0, kEpsilon);
EXPECT_NEAR(two.Degrees().to<double>(), 45.0, kEpsilon);
}
TEST(Rotation2dTest, DegreesToRadians) {
const auto one = Rotation2d::FromDegrees(45.0);
const auto two = Rotation2d::FromDegrees(30.0);
const auto one = Rotation2d(45.0_deg);
const auto two = Rotation2d(30.0_deg);
EXPECT_NEAR(one.Radians(), wpi::math::pi / 4.0, kEpsilon);
EXPECT_NEAR(two.Radians(), wpi::math::pi / 6.0, kEpsilon);
EXPECT_NEAR(one.Radians().to<double>(), wpi::math::pi / 4.0, kEpsilon);
EXPECT_NEAR(two.Radians().to<double>(), wpi::math::pi / 6.0, kEpsilon);
}
TEST(Rotation2dTest, RotateByFromZero) {
const Rotation2d zero;
auto sum = zero + Rotation2d::FromDegrees(90.0);
auto sum = zero + Rotation2d(90.0_deg);
EXPECT_NEAR(sum.Radians(), wpi::math::pi / 2.0, kEpsilon);
EXPECT_NEAR(sum.Degrees(), 90.0, kEpsilon);
EXPECT_NEAR(sum.Radians().to<double>(), wpi::math::pi / 2.0, kEpsilon);
EXPECT_NEAR(sum.Degrees().to<double>(), 90.0, kEpsilon);
}
TEST(Rotation2dTest, RotateByNonZero) {
auto rot = Rotation2d::FromDegrees(90.0);
rot += Rotation2d::FromDegrees(30.0);
auto rot = Rotation2d(90.0_deg);
rot += Rotation2d(30.0_deg);
EXPECT_NEAR(rot.Degrees(), 120.0, kEpsilon);
EXPECT_NEAR(rot.Degrees().to<double>(), 120.0, kEpsilon);
}
TEST(Rotation2dTest, Minus) {
const auto one = Rotation2d::FromDegrees(70.0);
const auto two = Rotation2d::FromDegrees(30.0);
const auto one = Rotation2d(70.0_deg);
const auto two = Rotation2d(30.0_deg);
EXPECT_NEAR((one - two).Degrees(), 40.0, kEpsilon);
EXPECT_NEAR((one - two).Degrees().to<double>(), 40.0, kEpsilon);
}