diff --git a/test/unit_test/runtime/almost_equals.h b/test/unit_test/runtime/almost_equals.h new file mode 100644 index 00000000..f8362143 --- /dev/null +++ b/test/unit_test/runtime/almost_equals.h @@ -0,0 +1,56 @@ +// The MIT License (MIT) +// +// Copyright (c) 2018 Mateusz Pusz +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#include +#include +#include + +namespace units { + +template +struct AlmostEqualsMatcher : Catch::Matchers::MatcherGenericBase { + AlmostEqualsMatcher(const T& target) : target_{target} {} + + template + requires std::three_way_comparable_with && std::same_as && + treat_as_floating_point bool + match(const U& other) const + { + using common = std::common_type_t; + auto maxTUOne = + std::max({typename T::rep(1), std::abs(common(target_).number()), std::abs(common(other).number())}); + return abs(target_ - other).number() <= std::numeric_limits::epsilon() * maxTUOne; + } + + std::string describe() const override { return "almost equals: " + STD_FMT::format("{}", target_); } + +private: + const T& target_; +}; + +template +AlmostEqualsMatcher AlmostEquals(const T& target) +{ + return {target}; +} + +} // namespace units diff --git a/test/unit_test/runtime/math_test.cpp b/test/unit_test/runtime/math_test.cpp index dc73da88..91b8ef82 100644 --- a/test/unit_test/runtime/math_test.cpp +++ b/test/unit_test/runtime/math_test.cpp @@ -20,6 +20,7 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. +#include "almost_equals.h" #include #include #include @@ -386,3 +387,73 @@ TEST_CASE("hypot functions", "[hypot]") REQUIRE(hypot(km<>(2.), cgs::length::cm<>(300'000.), km<>(6.)) == km<>(7.)); } } + +TEST_CASE("trigonometric functions", "[trig]") +{ + using namespace units::aliases; + + SECTION("sin") + { + REQUIRE_THAT(sin(deg<>(0.)), AlmostEquals(quantity{0.})); + REQUIRE_THAT(sin(deg<>(90.)), AlmostEquals(quantity{1.})); + REQUIRE_THAT(sin(deg<>(180.)), AlmostEquals(quantity{0.})); + REQUIRE_THAT(sin(deg<>(270.)), AlmostEquals(quantity{-1.})); + + REQUIRE_THAT(sin(grad<>(0.)), AlmostEquals(quantity{0.})); + REQUIRE_THAT(sin(grad<>(100.)), AlmostEquals(quantity{1.})); + REQUIRE_THAT(sin(grad<>(200.)), AlmostEquals(quantity{0.})); + REQUIRE_THAT(sin(grad<>(300.)), AlmostEquals(quantity{-1.})); + } + + SECTION("cos") + { + REQUIRE_THAT(cos(deg<>(0.)), AlmostEquals(quantity{1.})); + REQUIRE_THAT(cos(deg<>(90.)), AlmostEquals(quantity{0.})); + REQUIRE_THAT(cos(deg<>(180.)), AlmostEquals(quantity{-1.})); + REQUIRE_THAT(cos(deg<>(270.)), AlmostEquals(quantity{0.})); + + REQUIRE_THAT(cos(grad<>(0.)), AlmostEquals(quantity{1.})); + REQUIRE_THAT(cos(grad<>(100.)), AlmostEquals(quantity{0.})); + REQUIRE_THAT(cos(grad<>(200.)), AlmostEquals(quantity{-1.})); + REQUIRE_THAT(cos(grad<>(300.)), AlmostEquals(quantity{0.})); + } + + SECTION("tan") + { + REQUIRE_THAT(tan(deg<>(0.)), AlmostEquals(quantity{0.})); + REQUIRE_THAT(tan(deg<>(45.)), AlmostEquals(quantity{1.})); + REQUIRE_THAT(tan(deg<>(135.)), AlmostEquals(quantity{-1.})); + REQUIRE_THAT(tan(deg<>(180.)), AlmostEquals(quantity{0.})); + + REQUIRE_THAT(tan(grad<>(0.)), AlmostEquals(quantity{0.})); + REQUIRE_THAT(tan(grad<>(50.)), AlmostEquals(quantity{1.})); + REQUIRE_THAT(tan(grad<>(150.)), AlmostEquals(quantity{-1.})); + REQUIRE_THAT(tan(grad<>(200.)), AlmostEquals(quantity{0.})); + } +} + +TEST_CASE("inverse trigonometric functions", "[inv trig]") +{ + using namespace units::aliases; + + SECTION("asin") + { + REQUIRE_THAT(asin(quantity{-1.}), AlmostEquals(deg<>(-90.))); + REQUIRE_THAT(asin(quantity{0.}), AlmostEquals(deg<>(0.))); + REQUIRE_THAT(asin(quantity{1.}), AlmostEquals(deg<>(90.))); + } + + SECTION("acos") + { + REQUIRE_THAT(asin(quantity{-1.}), AlmostEquals(deg<>(-90.))); + REQUIRE_THAT(asin(quantity{0.}), AlmostEquals(deg<>(0.))); + REQUIRE_THAT(asin(quantity{1.}), AlmostEquals(deg<>(90.))); + } + + SECTION("atan") + { + REQUIRE_THAT(atan(quantity{-1.}), AlmostEquals(deg<>(-45.))); + REQUIRE_THAT(atan(quantity{0.}), AlmostEquals(deg<>(0.))); + REQUIRE_THAT(atan(quantity{1.}), AlmostEquals(deg<>(45.))); + } +}