test: unit tests for trigonometric functions added

This commit is contained in:
Mateusz Pusz
2022-09-01 16:25:33 +02:00
parent 9ae3403b18
commit a95cb53f73
2 changed files with 127 additions and 0 deletions

View File

@ -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 <catch2/matchers/catch_matchers_templated.hpp>
#include <units/format.h>
#include <units/quantity.h>
namespace units {
template<Quantity T>
struct AlmostEqualsMatcher : Catch::Matchers::MatcherGenericBase {
AlmostEqualsMatcher(const T& target) : target_{target} {}
template<Quantity U>
requires std::three_way_comparable_with<T, U> && std::same_as<typename T::rep, typename U::rep> &&
treat_as_floating_point<typename T::rep> bool
match(const U& other) const
{
using common = std::common_type_t<T, U>;
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<typename T::rep>::epsilon() * maxTUOne;
}
std::string describe() const override { return "almost equals: " + STD_FMT::format("{}", target_); }
private:
const T& target_;
};
template<Quantity T>
AlmostEqualsMatcher<T> AlmostEquals(const T& target)
{
return {target};
}
} // namespace units

View File

@ -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 <catch2/catch_all.hpp>
#include <units/isq/si/area.h>
#include <units/isq/si/cgs/length.h>
@ -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.)));
}
}