|
| 1 | +// Copyright 2026 Matt Borland |
| 2 | +// Distributed under the Boost Software License, Version 1.0. |
| 3 | +// https://www.boost.org/LICENSE_1_0.txt |
| 4 | + |
| 5 | +#include <boost/core/lightweight_test.hpp> |
| 6 | + |
| 7 | +#ifdef BOOST_SAFE_NUMBERS_BUILD_MODULE |
| 8 | + |
| 9 | +import boost.safe_numbers; |
| 10 | + |
| 11 | +#else |
| 12 | + |
| 13 | +#include <boost/safe_numbers/cmath.hpp> |
| 14 | + |
| 15 | +#endif |
| 16 | + |
| 17 | +#include <bit> |
| 18 | +#include <cmath> |
| 19 | +#include <cstdint> |
| 20 | +#include <limits> |
| 21 | +#include <type_traits> |
| 22 | + |
| 23 | +using namespace boost::safe_numbers; |
| 24 | + |
| 25 | +// The classification predicates never throw and must agree with the standard |
| 26 | +// library for every category of value, including the special ones. |
| 27 | +template <typename T> |
| 28 | +void test_predicates() |
| 29 | +{ |
| 30 | + using basis_type = typename T::basis_type; |
| 31 | + |
| 32 | + const T zero {static_cast<basis_type>(0.0)}; |
| 33 | + const T neg_zero {static_cast<basis_type>(-0.0)}; |
| 34 | + const T one {static_cast<basis_type>(1.0)}; |
| 35 | + const T neg_one {static_cast<basis_type>(-1.0)}; |
| 36 | + const T pos_inf {std::numeric_limits<basis_type>::infinity()}; |
| 37 | + const T neg_inf {-std::numeric_limits<basis_type>::infinity()}; |
| 38 | + const T qnan {std::numeric_limits<basis_type>::quiet_NaN()}; |
| 39 | + const T tiny {std::numeric_limits<basis_type>::denorm_min()}; |
| 40 | + |
| 41 | + // isnan |
| 42 | + BOOST_TEST(isnan(qnan)); |
| 43 | + BOOST_TEST(!isnan(one)); |
| 44 | + BOOST_TEST(!isnan(pos_inf)); |
| 45 | + |
| 46 | + // isinf |
| 47 | + BOOST_TEST(isinf(pos_inf)); |
| 48 | + BOOST_TEST(isinf(neg_inf)); |
| 49 | + BOOST_TEST(!isinf(one)); |
| 50 | + BOOST_TEST(!isinf(qnan)); |
| 51 | + |
| 52 | + // isfinite |
| 53 | + BOOST_TEST(isfinite(one)); |
| 54 | + BOOST_TEST(isfinite(zero)); |
| 55 | + BOOST_TEST(!isfinite(pos_inf)); |
| 56 | + BOOST_TEST(!isfinite(qnan)); |
| 57 | + |
| 58 | + // isnormal |
| 59 | + BOOST_TEST(isnormal(one)); |
| 60 | + BOOST_TEST(!isnormal(zero)); |
| 61 | + BOOST_TEST(!isnormal(pos_inf)); |
| 62 | + BOOST_TEST(!isnormal(qnan)); |
| 63 | + BOOST_TEST(!isnormal(tiny)); |
| 64 | + |
| 65 | + // signbit |
| 66 | + BOOST_TEST(signbit(neg_zero)); |
| 67 | + BOOST_TEST(!signbit(zero)); |
| 68 | + BOOST_TEST(signbit(neg_one)); |
| 69 | + BOOST_TEST(!signbit(one)); |
| 70 | + BOOST_TEST(signbit(neg_inf)); |
| 71 | + BOOST_TEST(!signbit(pos_inf)); |
| 72 | + |
| 73 | + // fpclassify |
| 74 | + BOOST_TEST(fpclassify(zero) == FP_ZERO); |
| 75 | + BOOST_TEST(fpclassify(neg_zero) == FP_ZERO); |
| 76 | + BOOST_TEST(fpclassify(one) == FP_NORMAL); |
| 77 | + BOOST_TEST(fpclassify(pos_inf) == FP_INFINITE); |
| 78 | + BOOST_TEST(fpclassify(qnan) == FP_NAN); |
| 79 | + BOOST_TEST(fpclassify(tiny) == FP_SUBNORMAL); |
| 80 | +} |
| 81 | + |
| 82 | +// Signaling-NaN bit pattern built via bit_cast, so it reaches the predicate |
| 83 | +// without being quieted by a floating-point operation first. |
| 84 | +template <typename T> |
| 85 | +void test_signaling_nan() |
| 86 | +{ |
| 87 | + using basis_type = typename T::basis_type; |
| 88 | + |
| 89 | + if constexpr (std::numeric_limits<basis_type>::has_signaling_NaN) |
| 90 | + { |
| 91 | + using bit_type = std::conditional_t<std::is_same_v<basis_type, float>, std::uint32_t, std::uint64_t>; |
| 92 | + |
| 93 | + constexpr auto snan_bits {std::bit_cast<bit_type>(std::numeric_limits<basis_type>::signaling_NaN())}; |
| 94 | + const T snan {std::bit_cast<basis_type>(snan_bits)}; |
| 95 | + |
| 96 | + BOOST_TEST(isnan(snan)); |
| 97 | + BOOST_TEST(!isinf(snan)); |
| 98 | + BOOST_TEST(!isfinite(snan)); |
| 99 | + BOOST_TEST(!isnormal(snan)); |
| 100 | + BOOST_TEST(fpclassify(snan) == FP_NAN); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +// Non-signaling ordered comparisons: false (not a throw) when an operand is NAN. |
| 105 | +template <typename T> |
| 106 | +void test_comparisons() |
| 107 | +{ |
| 108 | + using basis_type = typename T::basis_type; |
| 109 | + |
| 110 | + const T one {static_cast<basis_type>(1.0)}; |
| 111 | + const T two {static_cast<basis_type>(2.0)}; |
| 112 | + const T qnan {std::numeric_limits<basis_type>::quiet_NaN()}; |
| 113 | + |
| 114 | + BOOST_TEST(isgreater(two, one)); |
| 115 | + BOOST_TEST(!isgreater(one, two)); |
| 116 | + BOOST_TEST(!isgreater(qnan, one)); |
| 117 | + BOOST_TEST(!isgreater(one, qnan)); |
| 118 | + |
| 119 | + BOOST_TEST(isgreaterequal(two, one)); |
| 120 | + BOOST_TEST(isgreaterequal(one, one)); |
| 121 | + BOOST_TEST(!isgreaterequal(qnan, one)); |
| 122 | + |
| 123 | + BOOST_TEST(isless(one, two)); |
| 124 | + BOOST_TEST(!isless(two, one)); |
| 125 | + BOOST_TEST(!isless(qnan, one)); |
| 126 | + |
| 127 | + BOOST_TEST(islessequal(one, two)); |
| 128 | + BOOST_TEST(islessequal(one, one)); |
| 129 | + BOOST_TEST(!islessequal(qnan, one)); |
| 130 | + |
| 131 | + BOOST_TEST(islessgreater(one, two)); |
| 132 | + BOOST_TEST(!islessgreater(one, one)); |
| 133 | + BOOST_TEST(!islessgreater(qnan, one)); |
| 134 | + |
| 135 | + BOOST_TEST(isunordered(qnan, one)); |
| 136 | + BOOST_TEST(isunordered(one, qnan)); |
| 137 | + BOOST_TEST(!isunordered(one, two)); |
| 138 | +} |
| 139 | + |
| 140 | +int main() |
| 141 | +{ |
| 142 | + test_predicates<f32>(); |
| 143 | + test_predicates<f64>(); |
| 144 | + |
| 145 | + test_signaling_nan<f32>(); |
| 146 | + test_signaling_nan<f64>(); |
| 147 | + |
| 148 | + test_comparisons<f32>(); |
| 149 | + test_comparisons<f64>(); |
| 150 | + |
| 151 | + return boost::report_errors(); |
| 152 | +} |
0 commit comments