|
| 1 | +// The MIT License (MIT) |
| 2 | +// |
| 3 | +// Copyright (c) 2018 Mateusz Pusz |
| 4 | +// |
| 5 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +// of this software and associated documentation files (the "Software"), to deal |
| 7 | +// in the Software without restriction, including without limitation the rights |
| 8 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +// copies of the Software, and to permit persons to whom the Software is |
| 10 | +// furnished to do so, subject to the following conditions: |
| 11 | +// |
| 12 | +// The above copyright notice and this permission notice shall be included in all |
| 13 | +// copies or substantial portions of the Software. |
| 14 | +// |
| 15 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +// SOFTWARE. |
| 22 | + |
| 23 | +#include "almost_equals.h" |
| 24 | +#include <catch2/catch_template_test_macros.hpp> |
| 25 | +#include <catch2/catch_test_macros.hpp> |
| 26 | +#include <catch2/matchers/catch_matchers.hpp> |
| 27 | +#include <limits> |
| 28 | +#include <numbers> |
| 29 | +#ifdef MP_UNITS_MODULES |
| 30 | +import mp_units; |
| 31 | +#else |
| 32 | +#include <mp-units/ostream.h> // IWYU pragma: keep |
| 33 | +#include <mp-units/systems/angular.h> |
| 34 | +#endif |
| 35 | + |
| 36 | +using namespace mp_units; |
| 37 | +using namespace mp_units::angular; |
| 38 | +using namespace mp_units::angular::unit_symbols; |
| 39 | + |
| 40 | +inline constexpr struct half_revolution : named_unit<"hrev", mag_pi * radian> { |
| 41 | +} half_revolution; |
| 42 | +inline constexpr auto hrev = half_revolution; |
| 43 | + |
| 44 | +// constexpr auto revb6 = mag_ratio<1,3> * mag_pi * rad; |
| 45 | + |
| 46 | +TEST_CASE("value_cast should not truncate for valid inputs", "[value_cast]") |
| 47 | +{ |
| 48 | + SECTION("num > den > 1, irr = 1") |
| 49 | + { |
| 50 | + REQUIRE_THAT(value_cast<grad>(9 * deg), AlmostEquals(10 * grad)); |
| 51 | + REQUIRE_THAT(value_cast<grad>(360 * deg), AlmostEquals(400 * grad)); |
| 52 | + } |
| 53 | + |
| 54 | + SECTION("1 < num < den, irr = 1") |
| 55 | + { |
| 56 | + REQUIRE_THAT(value_cast<deg>(10 * grad), AlmostEquals(9 * deg)); |
| 57 | + REQUIRE_THAT(value_cast<deg>(400 * grad), AlmostEquals(360 * deg)); |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + SECTION("num > den = 1, irr > 1") |
| 62 | + { |
| 63 | + REQUIRE_THAT(value_cast<rad>(1 * rev), AlmostEquals(6 * rad)); |
| 64 | + REQUIRE_THAT(value_cast<rad>(5 * rev), AlmostEquals(31 * rad)); |
| 65 | + REQUIRE_THAT(value_cast<rad>(10 * rev), AlmostEquals(63 * rad)); |
| 66 | + REQUIRE_THAT(value_cast<rad>(20 * rev), AlmostEquals(126 * rad)); |
| 67 | + } |
| 68 | + |
| 69 | + SECTION("1 = num < den, irr < 1") |
| 70 | + { |
| 71 | + REQUIRE_THAT(value_cast<rev>(6 * rad), AlmostEquals(1 * rev)); |
| 72 | + REQUIRE_THAT(value_cast<rev>(31 * rad), AlmostEquals(5 * rev)); |
| 73 | + REQUIRE_THAT(value_cast<rev>(63 * rad), AlmostEquals(10 * rev)); |
| 74 | + REQUIRE_THAT(value_cast<rev>(126 * rad), AlmostEquals(20 * rev)); |
| 75 | + } |
| 76 | + |
| 77 | + SECTION("rational = 1, irrational > 1") |
| 78 | + { |
| 79 | + REQUIRE_THAT(value_cast<rad>(1 * hrev), AlmostEquals(3 * rad)); |
| 80 | + REQUIRE_THAT(value_cast<rad>(10 * hrev), AlmostEquals(31 * rad)); |
| 81 | + REQUIRE_THAT(value_cast<rad>(20 * hrev), AlmostEquals(63 * rad)); |
| 82 | + REQUIRE_THAT(value_cast<rad>(40 * hrev), AlmostEquals(126 * rad)); |
| 83 | + } |
| 84 | + |
| 85 | + SECTION("rational = 1, irrational < 1") |
| 86 | + { |
| 87 | + REQUIRE_THAT(value_cast<hrev>(3 * rad), AlmostEquals(1 * hrev)); |
| 88 | + REQUIRE_THAT(value_cast<hrev>(31 * rad), AlmostEquals(10 * hrev)); |
| 89 | + REQUIRE_THAT(value_cast<hrev>(63 * rad), AlmostEquals(20 * hrev)); |
| 90 | + REQUIRE_THAT(value_cast<hrev>(126 * rad), AlmostEquals(40 * hrev)); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | + |
| 95 | +TEMPLATE_TEST_CASE("value_cast should not overflow internally for valid inputs", "[value_cast]", std::int8_t, |
| 96 | + std::uint8_t, std::int16_t, std::uint16_t, std::int32_t, std::uint32_t) |
| 97 | +{ |
| 98 | + // max()/20: small enough so that none of the tested factors are likely to cause overflow, but still be nonzero; |
| 99 | + // the "easy" test to verify the test itself is good. |
| 100 | + std::vector<TestType> test_values = {std::numeric_limits<TestType>::max() / 20, |
| 101 | + std::numeric_limits<TestType>::max() - 1}; |
| 102 | + if (std::is_signed_v<TestType>) { |
| 103 | + test_values.push_back(std::numeric_limits<TestType>::min() + 1); |
| 104 | + } |
| 105 | + |
| 106 | + for (TestType tv : test_values) { |
| 107 | + SECTION("grad <-> deg") |
| 108 | + { |
| 109 | + auto deg_number = static_cast<TestType>(std::trunc(0.9 * tv)); |
| 110 | + auto grad_number = static_cast<TestType>(std::round(deg_number / 0.9)); |
| 111 | + INFO(MP_UNITS_STD_FMT::format("{} deg ~ {} grad", deg_number, grad_number)); |
| 112 | + REQUIRE_THAT(value_cast<grad>(deg_number * deg), AlmostEquals(grad_number * grad)); |
| 113 | + REQUIRE_THAT(value_cast<deg>(grad_number * grad), AlmostEquals(deg_number * deg)); |
| 114 | + } |
| 115 | + |
| 116 | + |
| 117 | + SECTION("rad <-> rev") |
| 118 | + { |
| 119 | + auto rev_number = static_cast<TestType>(std::trunc(0.5 * std::numbers::inv_pi * tv)); |
| 120 | + auto rad_number = static_cast<TestType>(std::round(2 * std::numbers::pi * rev_number)); |
| 121 | + INFO(MP_UNITS_STD_FMT::format("{} rev ~ {} rad", rev_number, rad_number)); |
| 122 | + REQUIRE_THAT(value_cast<rad>(rev_number * rev), AlmostEquals(rad_number * rad)); |
| 123 | + REQUIRE_THAT(value_cast<rev>(rad_number * rad), AlmostEquals(rev_number * rev)); |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments