Debian Codegraph @

src/external/catch2/tests/SelfTest/IntrospectiveTests/FloatingPoint.tests.cpp

raw
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 #include <catch2/internal/catch_random_floating_point_helpers.hpp>
13
14 #include <limits>
15
16 TEST_CASE("convertToBits", "[floating-point][conversion]") {
17 using Catch::Detail::convertToBits;
18
19 CHECK( convertToBits( 0.f ) == 0 );
20 CHECK( convertToBits( -0.f ) == ( 1ULL << 31 ) );
21 CHECK( convertToBits( 0. ) == 0 );
22 CHECK( convertToBits( -0. ) == ( 1ULL << 63 ) );
23 CHECK( convertToBits( std::numeric_limits<float>::denorm_min() ) == 1 );
24 CHECK( convertToBits( std::numeric_limits<double>::denorm_min() ) == 1 );
25 }
26
27 TEMPLATE_TEST_CASE("type-shared ulpDistance tests", "[floating-point][ulp][approvals]", float, double) {
28 using FP = TestType;
29 using Catch::ulpDistance;
30
31 // Distance between zeros is zero
32 CHECK( ulpDistance( FP{}, FP{} ) == 0 );
33 CHECK( ulpDistance( FP{}, -FP{} ) == 0 );
34 CHECK( ulpDistance( -FP{}, -FP{} ) == 0 );
35
36 // Distance between same-sign infinities is zero
37 static constexpr FP infinity = std::numeric_limits<FP>::infinity();
38 CHECK( ulpDistance( infinity, infinity ) == 0 );
39 CHECK( ulpDistance( -infinity, -infinity ) == 0 );
40
41 // Distance between max-finite-val and same sign infinity is 1
42 static constexpr FP max_finite = std::numeric_limits<FP>::max();
43 CHECK( ulpDistance( max_finite, infinity ) == 1 );
44 CHECK( ulpDistance( -max_finite, -infinity ) == 1 );
45
46 // Distance between X and 0 is half of distance between X and -X
47 CHECK( ulpDistance( -infinity, infinity ) ==
48 2 * ulpDistance( infinity, FP{} ) );
49 CHECK( 2 * ulpDistance( FP{ -2. }, FP{} ) ==
50 ulpDistance( FP{ -2. }, FP{ 2. } ) );
51 CHECK( 2 * ulpDistance( FP{ 2. }, FP{} ) ==
52 ulpDistance( FP{ -2. }, FP{ 2. } ) );
53
54 // Denorms are supported
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(), FP{} ) == 1 );
58 CHECK( ulpDistance( -std::numeric_limits<FP>::denorm_min(), -FP{} ) == 1 );
59 CHECK( ulpDistance( std::numeric_limits<FP>::denorm_min(),
60 -std::numeric_limits<FP>::denorm_min() ) == 2 );
61
62 // Machine epsilon
63 CHECK( ulpDistance( FP{ 1. },
64 FP{ 1. } + std::numeric_limits<FP>::epsilon() ) == 1 );
65 CHECK( ulpDistance( -FP{ 1. },
66 -FP{ 1. } - std::numeric_limits<FP>::epsilon() ) == 1 );
67 }
68
69 TEST_CASE("UlpDistance", "[floating-point][ulp][approvals]") {
70 using Catch::ulpDistance;
71
72 CHECK( ulpDistance( 1., 2. ) == 0x10'00'00'00'00'00'00 );
73 CHECK( ulpDistance( -2., 2. ) == 0x80'00'00'00'00'00'00'00 );
74 CHECK( ulpDistance( 1.f, 2.f ) == 0x80'00'00 );
75 CHECK( ulpDistance( -2.f, 2.f ) == 0x80'00'00'00 );
76 }
77
78
79
80 TEMPLATE_TEST_CASE("gamma", "[approvals][floating-point][ulp][gamma]", float, double) {
81 using Catch::Detail::gamma;
82 using Catch::Detail::directCompare;
83
84 // We need to butcher the equal tests with the directCompare helper,
85 // because the Wfloat-equal triggers in decomposer rather than here,
86 // so we cannot locally disable it. Goddamn GCC.
87 CHECK( directCompare( gamma( TestType( -1. ), TestType( 1. ) ),
88 gamma( TestType( 0.2332 ), TestType( 1.0 ) ) ) );
89 CHECK( directCompare( gamma( TestType( -2. ), TestType( 0 ) ),
90 gamma( TestType( 1. ), TestType( 1.5 ) ) ) );
91 CHECK( gamma( TestType( 0. ), TestType( 1.0 ) ) <
92 gamma( TestType( 1.0 ), TestType( 1.5 ) ) );
93 CHECK( gamma( TestType( 0 ), TestType( 1. ) ) <
94 std::numeric_limits<TestType>::epsilon() );
95 CHECK( gamma( TestType( -1. ), TestType( -0. ) ) <
96 std::numeric_limits<TestType>::epsilon() );
97 CHECK( directCompare( gamma( TestType( 1. ), TestType( 2. ) ),
98 std::numeric_limits<TestType>::epsilon() ) );
99 CHECK( directCompare( gamma( TestType( -2. ), TestType( -1. ) ),
100 std::numeric_limits<TestType>::epsilon() ) );
101 }
102
103 TEMPLATE_TEST_CASE("count_equidistant_floats",
104 "[approvals][floating-point][distance]",
105 float,
106 double) {
107 using Catch::Detail::count_equidistant_floats;
108 auto count_steps = []( TestType a, TestType b ) {
109 return count_equidistant_floats( a, b, Catch::Detail::gamma( a, b ) );
110 };
111
112 CHECK( count_steps( TestType( -1. ), TestType( 1. ) ) ==
113 2 * count_steps( TestType( 0. ), TestType( 1. ) ) );
114 }
115
116 TEST_CASE( "count_equidistant_floats",
117 "[approvals][floating-point][distance]" ) {
118 using Catch::Detail::count_equidistant_floats;
119 auto count_floats_with_scaled_ulp = []( auto a, auto b ) {
120 return count_equidistant_floats( a, b, Catch::Detail::gamma( a, b ) );
121 };
122
123 CHECK( count_floats_with_scaled_ulp( 1., 1.5 ) == 1ull << 51 );
124 CHECK( count_floats_with_scaled_ulp( 1.25, 1.5 ) == 1ull << 50 );
125 CHECK( count_floats_with_scaled_ulp( 1.f, 1.5f ) == 1 << 22 );
126 CHECK( count_floats_with_scaled_ulp( -std::numeric_limits<float>::max(),
127 std::numeric_limits<float>::max() ) ==
128 33554430 ); // (1 << 25) - 2 due to not including infinities
129 CHECK( count_floats_with_scaled_ulp( -std::numeric_limits<double>::max(),
130 std::numeric_limits<double>::max() ) ==
131 18014398509481982 ); // (1 << 54) - 2 due to not including infinities
132
133 STATIC_REQUIRE( std::is_same<std::uint64_t,
134 decltype( count_floats_with_scaled_ulp(
135 0., 1. ) )>::value );
136 STATIC_REQUIRE( std::is_same<std::uint32_t,
137 decltype( count_floats_with_scaled_ulp(
138 0.f, 1.f ) )>::value );
139 }