| 1 | |
| 2 | // Copyright Catch2 Authors |
| 3 | // Distributed under the Boost Software License, Version 1.0. |
| 4 | // (See accompanying file LICENSE.txt or copy at |
| 5 | // https://www.boost.org/LICENSE_1_0.txt) |
| 6 | |
| 7 | // SPDX-License-Identifier: BSL-1.0 |
| 8 | |
| 9 | #include <catch2/catch_test_macros.hpp> |
| 10 | #include <catch2/catch_template_test_macros.hpp> |
| 11 | #include <catch2/internal/catch_floating_point_helpers.hpp> |
| 12 | |
| 13 | |
| 14 | TEST_CASE("convertToBits", "[floating-point][conversion]") { |
| 15 | using Catch::Detail::convertToBits; |
| 16 | |
| 17 | CHECK( convertToBits( 0.f ) == 0 ); |
| 18 | CHECK( convertToBits( -0.f ) == ( 1ULL << 31 ) ); |
| 19 | CHECK( convertToBits( 0. ) == 0 ); |
| 20 | CHECK( convertToBits( -0. ) == ( 1ULL << 63 ) ); |
| 21 | CHECK( convertToBits( std::numeric_limits<float>::denorm_min() ) == 1 ); |
| 22 | CHECK( convertToBits( std::numeric_limits<double>::denorm_min() ) == 1 ); |
| 23 | } |
| 24 | |
| 25 | TEMPLATE_TEST_CASE("type-shared ulpDistance tests", "[floating-point][ulp][approvals]", float, double) { |
| 26 | using FP = TestType; |
| 27 | using Catch::ulpDistance; |
| 28 | |
| 29 | // Distance between zeros is zero |
| 30 | CHECK( ulpDistance( FP{}, FP{} ) == 0 ); |
| 31 | CHECK( ulpDistance( FP{}, -FP{} ) == 0 ); |
| 32 | CHECK( ulpDistance( -FP{}, -FP{} ) == 0 ); |
| 33 | |
| 34 | // Distance between same-sign infinities is zero |
| 35 | static constexpr FP infinity = std::numeric_limits<FP>::infinity(); |
| 36 | CHECK( ulpDistance( infinity, infinity ) == 0 ); |
| 37 | CHECK( ulpDistance( -infinity, -infinity ) == 0 ); |
| 38 | |
| 39 | // Distance between max-finite-val and same sign infinity is 1 |
| 40 | static constexpr FP max_finite = std::numeric_limits<FP>::max(); |
| 41 | CHECK( ulpDistance( max_finite, infinity ) == 1 ); |
| 42 | CHECK( ulpDistance( -max_finite, -infinity ) == 1 ); |
| 43 | |
| 44 | // Distance between X and 0 is half of distance between X and -X |
| 45 | CHECK( ulpDistance( -infinity, infinity ) == |
| 46 | 2 * ulpDistance( infinity, FP{} ) ); |
| 47 | CHECK( 2 * ulpDistance( FP{ -2. }, FP{} ) == |
| 48 | ulpDistance( FP{ -2. }, FP{ 2. } ) ); |
| 49 | CHECK( 2 * ulpDistance( FP{ 2. }, FP{} ) == |
| 50 | ulpDistance( FP{ -2. }, FP{ 2. } ) ); |
| 51 | |
| 52 | // Denorms are supported |
| 53 | CHECK( ulpDistance( std::numeric_limits<FP>::denorm_min(), FP{} ) == 1 ); |
| 54 | CHECK( ulpDistance( std::numeric_limits<FP>::denorm_min(), -FP{} ) == 1 ); |
| 55 | CHECK( ulpDistance( -std::numeric_limits<FP>::denorm_min(), FP{} ) == 1 ); |
| 56 | CHECK( ulpDistance( -std::numeric_limits<FP>::denorm_min(), -FP{} ) == 1 ); |
| 57 | CHECK( ulpDistance( std::numeric_limits<FP>::denorm_min(), |
| 58 | -std::numeric_limits<FP>::denorm_min() ) == 2 ); |
| 59 | |
| 60 | // Machine epsilon |
| 61 | CHECK( ulpDistance( FP{ 1. }, |
| 62 | FP{ 1. } + std::numeric_limits<FP>::epsilon() ) == 1 ); |
| 63 | CHECK( ulpDistance( -FP{ 1. }, |
| 64 | -FP{ 1. } - std::numeric_limits<FP>::epsilon() ) == 1 ); |
| 65 | } |
| 66 | |
| 67 | TEST_CASE("UlpDistance", "[floating-point][ulp][approvals]") { |
| 68 | using Catch::ulpDistance; |
| 69 | |
| 70 | CHECK( ulpDistance( 1., 2. ) == 0x10'00'00'00'00'00'00 ); |
| 71 | CHECK( ulpDistance( -2., 2. ) == 0x80'00'00'00'00'00'00'00 ); |
| 72 | CHECK( ulpDistance( 1.f, 2.f ) == 0x80'00'00 ); |
| 73 | CHECK( ulpDistance( -2.f, 2.f ) == 0x80'00'00'00 ); |
| 74 | } |