Skip to content

Commit b6df881

Browse files
committed
added more testing of conversion accuracy when using integral types
1 parent 2adc934 commit b6df881

File tree

3 files changed

+151
-5
lines changed

3 files changed

+151
-5
lines changed

test/runtime/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ find_package(Catch2 3 REQUIRED)
2626

2727
add_executable(
2828
unit_tests_runtime distribution_test.cpp fixed_string_test.cpp fmt_test.cpp math_test.cpp atomic_test.cpp
29+
truncation_test.cpp
2930
)
3031
if(${projectPrefix}BUILD_CXX_MODULES)
3132
target_compile_definitions(unit_tests_runtime PUBLIC ${projectPrefix}MODULES)

test/runtime/almost_equals.h

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,36 @@ struct AlmostEqualsMatcher : Catch::Matchers::MatcherGenericBase {
3737
explicit AlmostEqualsMatcher(const T& target) : target_{target} {}
3838

3939
template<std::convertible_to<T> U>
40-
requires std::same_as<typename T::rep, typename U::rep> && treat_as_floating_point<typename T::rep>
40+
requires std::same_as<typename T::rep, typename U::rep>
4141
bool match(const U& other) const
4242
{
4343
using std::abs;
44-
using common = std::common_type_t<T, U>;
44+
using rep = typename T::rep;
45+
using common = conditional<treat_as_floating_point<rep>, std::common_type_t<T, U>, T>;
4546
const auto x = common(target_).numerical_value_in(common::unit);
4647
const auto y = common(other).numerical_value_in(common::unit);
47-
const auto maxXYOne = std::max({typename T::rep{1}, abs(x), abs(y)});
48-
return abs(x - y) <= std::numeric_limits<typename T::rep>::epsilon() * maxXYOne;
48+
if constexpr (treat_as_floating_point<rep>) {
49+
const auto maxXYOne = std::max({rep{1}, abs(x), abs(y)});
50+
return abs(x - y) <= std::numeric_limits<rep>::epsilon() * maxXYOne;
51+
} else {
52+
if (x >= 0) {
53+
return x - 1 <= y && y - 1 <= x;
54+
} else {
55+
return x <= y + 1 && y <= x + 1;
56+
}
57+
}
4958
}
5059

51-
std::string describe() const override { return "almost equals: " + MP_UNITS_STD_FMT::format("{}", target_); }
60+
std::string describe() const override
61+
{
62+
if constexpr (treat_as_floating_point<typename T::rep>) {
63+
return "almost equals: " + MP_UNITS_STD_FMT::format("{}", target_);
64+
} else {
65+
return "almost equals: " + MP_UNITS_STD_FMT::format("[ {0} ({0:#x}) +/- 1 ] {1}",
66+
target_.numerical_value_is_an_implementation_detail_,
67+
target_.unit);
68+
}
69+
}
5270

5371
private:
5472
const T& target_;
@@ -60,4 +78,5 @@ AlmostEqualsMatcher<T> AlmostEquals(const T& target)
6078
return AlmostEqualsMatcher<T>{target};
6179
}
6280

81+
6382
} // namespace mp_units

test/runtime/truncation_test.cpp

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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

Comments
 (0)