Skip to content

Commit

Permalink
Move tests to runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
chiphogg committed Jul 25, 2024
1 parent f6696e8 commit 3a6fac9
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 31 deletions.
10 changes: 8 additions & 2 deletions test/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
74 changes: 74 additions & 0 deletions test/runtime/quantity_test.cpp
Original file line number Diff line number Diff line change
@@ -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 <catch2/catch_test_macros.hpp>
#ifdef MP_UNITS_IMPORT_STD
import std;
#else
#include <atomic>
#endif
#ifdef MP_UNITS_MODULES
import mp_units;
#else
#include <mp-units/math.h>
#include <mp-units/systems/si.h>
#endif

using namespace mp_units;
using namespace mp_units::si::unit_symbols;

namespace {

template<typename T>
constexpr bool within_4_ulps(T a, T b)
{
static_assert(std::is_floating_point_v<T>);
auto walk_ulps = [](T x, int n) {
while (n > 0) {
x = std::nextafter(x, std::numeric_limits<T>::infinity());
--n;
}
while (n < 0) {
x = std::nextafter(x, -std::numeric_limits<T>::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))));
}
29 changes: 0 additions & 29 deletions test/static/quantity_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include <mp-units/bits/hacks.h>
#include <mp-units/ext/fixed_string.h>
#include <mp-units/ext/type_traits.h>
#include <mp-units/math.h>
#include <mp-units/systems/isq/mechanics.h>
#include <mp-units/systems/isq/space_and_time.h>
#include <mp-units/systems/si.h>
Expand Down Expand Up @@ -53,25 +52,6 @@ using namespace mp_units::si::unit_symbols;
// quantity class invariants
//////////////////////////////

template<typename T>
constexpr bool within_4_ulps(T a, T b)
{
static_assert(std::is_floating_point_v<T>);
auto walk_ulps = [](T x, int n) {
while (n > 0) {
x = std::nextafter(x, std::numeric_limits<T>::infinity());
--n;
}
while (n < 0) {
x = std::nextafter(x, -std::numeric_limits<T>::infinity());
++n;
}
return x;
};

return (walk_ulps(a, -4) <= b) && (b <= walk_ulps(a, 4));
}

static_assert(sizeof(quantity<si::metre>) == sizeof(double));
static_assert(sizeof(quantity<isq::length[m]>) == sizeof(double));
static_assert(sizeof(quantity<si::metre, short>) == sizeof(short));
Expand Down Expand Up @@ -219,15 +199,6 @@ static_assert(std::convertible_to<quantity<isq::length[km], int>, quantity<isq::
static_assert(std::constructible_from<quantity<isq::length[km]>, quantity<isq::length[m], int>>);
static_assert(std::convertible_to<quantity<isq::length[m], int>, quantity<isq::length[km]>>);

// 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
///////////////////////
Expand Down

0 comments on commit 3a6fac9

Please sign in to comment.