From 3a6fac98589882e4c3413749659d0712389cdfd2 Mon Sep 17 00:00:00 2001 From: Chip Hogg Date: Wed, 24 Jul 2024 20:03:47 -0400 Subject: [PATCH] Move tests to runtime --- test/runtime/CMakeLists.txt | 10 ++++- test/runtime/quantity_test.cpp | 74 ++++++++++++++++++++++++++++++++++ test/static/quantity_test.cpp | 29 ------------- 3 files changed, 82 insertions(+), 31 deletions(-) create mode 100644 test/runtime/quantity_test.cpp diff --git a/test/runtime/CMakeLists.txt b/test/runtime/CMakeLists.txt index 0be597b38..d79ed54b3 100644 --- a/test/runtime/CMakeLists.txt +++ b/test/runtime/CMakeLists.txt @@ -23,8 +23,14 @@ find_package(Catch2 3 REQUIRED) add_executable( - unit_tests_runtime distribution_test.cpp fixed_string_test.cpp fmt_test.cpp math_test.cpp atomic_test.cpp - truncation_test.cpp + unit_tests_runtime + distribution_test.cpp + fixed_string_test.cpp + fmt_test.cpp + math_test.cpp + atomic_test.cpp + truncation_test.cpp + quantity_test.cpp ) if(${projectPrefix}BUILD_CXX_MODULES) target_compile_definitions(unit_tests_runtime PUBLIC ${projectPrefix}MODULES) diff --git a/test/runtime/quantity_test.cpp b/test/runtime/quantity_test.cpp new file mode 100644 index 000000000..ba1e472fe --- /dev/null +++ b/test/runtime/quantity_test.cpp @@ -0,0 +1,74 @@ +// The MIT License (MIT) +// +// Copyright (c) 2024 Chip Hogg +// +// 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 +#ifdef MP_UNITS_IMPORT_STD +import std; +#else +#include +#endif +#ifdef MP_UNITS_MODULES +import mp_units; +#else +#include +#include +#endif + +using namespace mp_units; +using namespace mp_units::si::unit_symbols; + +namespace { + +template +constexpr bool within_4_ulps(T a, T b) +{ + static_assert(std::is_floating_point_v); + auto walk_ulps = [](T x, int n) { + while (n > 0) { + x = std::nextafter(x, std::numeric_limits::infinity()); + --n; + } + while (n < 0) { + x = std::nextafter(x, -std::numeric_limits::infinity()); + ++n; + } + return x; + }; + + return (walk_ulps(a, -4) <= b) && (b <= walk_ulps(a, 4)); +} + +} // namespace + +// conversion requiring radical magnitudes +TEST_CASE("unit conversions support radical magnitudes", "[conversion][radical]") +{ + REQUIRE(within_4_ulps(sqrt((1.0 * m) * (1.0 * km)).numerical_value_in(m), sqrt(1000.0))); +} + +// Reproducing issue #474 exactly: +TEST_CASE("Issue 474 is fixed", "[conversion][radical]") +{ + constexpr auto val_issue_474 = 8.0 * si::si2019::boltzmann_constant * 1000.0 * K / (std::numbers::pi * 10 * Da); + REQUIRE(within_4_ulps(sqrt(val_issue_474).numerical_value_in(m / s), + sqrt(val_issue_474.numerical_value_in(m * m / s / s)))); +} diff --git a/test/static/quantity_test.cpp b/test/static/quantity_test.cpp index 6fad23d9a..d8ddfe609 100644 --- a/test/static/quantity_test.cpp +++ b/test/static/quantity_test.cpp @@ -24,7 +24,6 @@ #include #include #include -#include #include #include #include @@ -53,25 +52,6 @@ using namespace mp_units::si::unit_symbols; // quantity class invariants ////////////////////////////// -template -constexpr bool within_4_ulps(T a, T b) -{ - static_assert(std::is_floating_point_v); - auto walk_ulps = [](T x, int n) { - while (n > 0) { - x = std::nextafter(x, std::numeric_limits::infinity()); - --n; - } - while (n < 0) { - x = std::nextafter(x, -std::numeric_limits::infinity()); - ++n; - } - return x; - }; - - return (walk_ulps(a, -4) <= b) && (b <= walk_ulps(a, 4)); -} - static_assert(sizeof(quantity) == sizeof(double)); static_assert(sizeof(quantity) == sizeof(double)); static_assert(sizeof(quantity) == sizeof(short)); @@ -219,15 +199,6 @@ static_assert(std::convertible_to, quantity, quantity>); static_assert(std::convertible_to, quantity>); -// conversion requiring radical magnitudes -static_assert(within_4_ulps(sqrt((1.0 * m) * (1.0 * km)).numerical_value_in(m), sqrt(1000.0))); - -// Reproducing issue #494 exactly: -constexpr auto val_issue_494 = 8.0 * si::si2019::boltzmann_constant * 1000.0 * K / (std::numbers::pi * 10 * Da); -static_assert(within_4_ulps(sqrt(val_issue_494).numerical_value_in(m / s), - sqrt(val_issue_494.numerical_value_in(m* m / s / s)))); - - /////////////////////// // obtaining a number ///////////////////////