| 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_approx.hpp> |
| 11 | |
| 12 | #include <cmath> |
| 13 | |
| 14 | using Catch::Approx; |
| 15 | |
| 16 | namespace { |
| 17 | static double divide(double a, double b) { |
| 18 | return a / b; |
| 19 | } |
| 20 | |
| 21 | class StrongDoubleTypedef { |
| 22 | double d_ = 0.0; |
| 23 | |
| 24 | public: |
| 25 | explicit StrongDoubleTypedef(double d) : d_(d) {} |
| 26 | explicit operator double() const { return d_; } |
| 27 | }; |
| 28 | |
| 29 | static std::ostream& operator<<(std::ostream& os, StrongDoubleTypedef td) { |
| 30 | return os << "StrongDoubleTypedef(" << static_cast<double>(td) << ")"; |
| 31 | } |
| 32 | } // end unnamed namespace |
| 33 | |
| 34 | using namespace Catch::literals; |
| 35 | |
| 36 | /////////////////////////////////////////////////////////////////////////////// |
| 37 | TEST_CASE( "A comparison that uses literals instead of the normal constructor", "[Approx]" ) { |
| 38 | double d = 1.23; |
| 39 | |
| 40 | REQUIRE( d == 1.23_a ); |
| 41 | REQUIRE( d != 1.22_a ); |
| 42 | REQUIRE( -d == -1.23_a ); |
| 43 | |
| 44 | REQUIRE( d == 1.2_a .epsilon(.1) ); |
| 45 | REQUIRE( d != 1.2_a .epsilon(.001) ); |
| 46 | REQUIRE( d == 1_a .epsilon(.3) ); |
| 47 | } |
| 48 | |
| 49 | TEST_CASE( "Some simple comparisons between doubles", "[Approx]" ) { |
| 50 | double d = 1.23; |
| 51 | |
| 52 | REQUIRE( d == Approx( 1.23 ) ); |
| 53 | REQUIRE( d != Approx( 1.22 ) ); |
| 54 | REQUIRE( d != Approx( 1.24 ) ); |
| 55 | |
| 56 | REQUIRE( d == 1.23_a ); |
| 57 | REQUIRE( d != 1.22_a ); |
| 58 | |
| 59 | REQUIRE( Approx( d ) == 1.23 ); |
| 60 | REQUIRE( Approx( d ) != 1.22 ); |
| 61 | REQUIRE( Approx( d ) != 1.24 ); |
| 62 | } |
| 63 | |
| 64 | /////////////////////////////////////////////////////////////////////////////// |
| 65 | TEST_CASE( "Approximate comparisons with different epsilons", "[Approx]" ) { |
| 66 | double d = 1.23; |
| 67 | |
| 68 | REQUIRE( d != Approx( 1.231 ) ); |
| 69 | REQUIRE( d == Approx( 1.231 ).epsilon( 0.1 ) ); |
| 70 | } |
| 71 | |
| 72 | /////////////////////////////////////////////////////////////////////////////// |
| 73 | TEST_CASE( "Less-than inequalities with different epsilons", "[Approx]" ) { |
| 74 | double d = 1.23; |
| 75 | |
| 76 | REQUIRE( d <= Approx( 1.24 ) ); |
| 77 | REQUIRE( d <= Approx( 1.23 ) ); |
| 78 | REQUIRE_FALSE( d <= Approx( 1.22 ) ); |
| 79 | REQUIRE( d <= Approx( 1.22 ).epsilon(0.1) ); |
| 80 | } |
| 81 | |
| 82 | /////////////////////////////////////////////////////////////////////////////// |
| 83 | TEST_CASE( "Greater-than inequalities with different epsilons", "[Approx]" ) { |
| 84 | double d = 1.23; |
| 85 | |
| 86 | REQUIRE( d >= Approx( 1.22 ) ); |
| 87 | REQUIRE( d >= Approx( 1.23 ) ); |
| 88 | REQUIRE_FALSE( d >= Approx( 1.24 ) ); |
| 89 | REQUIRE( d >= Approx( 1.24 ).epsilon(0.1) ); |
| 90 | } |
| 91 | |
| 92 | /////////////////////////////////////////////////////////////////////////////// |
| 93 | TEST_CASE( "Approximate comparisons with floats", "[Approx]" ) { |
| 94 | REQUIRE( 1.23f == Approx( 1.23f ) ); |
| 95 | REQUIRE( 0.0f == Approx( 0.0f ) ); |
| 96 | } |
| 97 | |
| 98 | /////////////////////////////////////////////////////////////////////////////// |
| 99 | TEST_CASE( "Approximate comparisons with ints", "[Approx]" ) { |
| 100 | REQUIRE( 1 == Approx( 1 ) ); |
| 101 | REQUIRE( 0 == Approx( 0 ) ); |
| 102 | } |
| 103 | |
| 104 | /////////////////////////////////////////////////////////////////////////////// |
| 105 | TEST_CASE( "Approximate comparisons with mixed numeric types", "[Approx]" ) { |
| 106 | const double dZero = 0; |
| 107 | const double dSmall = 0.00001; |
| 108 | const double dMedium = 1.234; |
| 109 | |
| 110 | REQUIRE( 1.0f == Approx( 1 ) ); |
| 111 | REQUIRE( 0 == Approx( dZero) ); |
| 112 | REQUIRE( 0 == Approx( dSmall ).margin( 0.001 ) ); |
| 113 | REQUIRE( 1.234f == Approx( dMedium ) ); |
| 114 | REQUIRE( dMedium == Approx( 1.234f ) ); |
| 115 | } |
| 116 | |
| 117 | /////////////////////////////////////////////////////////////////////////////// |
| 118 | TEST_CASE( "Use a custom approx", "[Approx][custom]" ) { |
| 119 | double d = 1.23; |
| 120 | |
| 121 | Approx approx = Approx::custom().epsilon( 0.01 ); |
| 122 | |
| 123 | REQUIRE( d == approx( 1.23 ) ); |
| 124 | REQUIRE( d == approx( 1.22 ) ); |
| 125 | REQUIRE( d == approx( 1.24 ) ); |
| 126 | REQUIRE( d != approx( 1.25 ) ); |
| 127 | |
| 128 | REQUIRE( approx( d ) == 1.23 ); |
| 129 | REQUIRE( approx( d ) == 1.22 ); |
| 130 | REQUIRE( approx( d ) == 1.24 ); |
| 131 | REQUIRE( approx( d ) != 1.25 ); |
| 132 | } |
| 133 | |
| 134 | TEST_CASE( "Approximate PI", "[Approx][PI]" ) { |
| 135 | REQUIRE( divide( 22, 7 ) == Approx( 3.141 ).epsilon( 0.001 ) ); |
| 136 | REQUIRE( divide( 22, 7 ) != Approx( 3.141 ).epsilon( 0.0001 ) ); |
| 137 | } |
| 138 | |
| 139 | /////////////////////////////////////////////////////////////////////////////// |
| 140 | |
| 141 | TEST_CASE( "Absolute margin", "[Approx]" ) { |
| 142 | REQUIRE( 104.0 != Approx(100.0) ); |
| 143 | REQUIRE( 104.0 == Approx(100.0).margin(5) ); |
| 144 | REQUIRE( 104.0 == Approx(100.0).margin(4) ); |
| 145 | REQUIRE( 104.0 != Approx(100.0).margin(3) ); |
| 146 | REQUIRE( 100.3 != Approx(100.0) ); |
| 147 | REQUIRE( 100.3 == Approx(100.0).margin(0.5) ); |
| 148 | } |
| 149 | |
| 150 | TEST_CASE("Approx with exactly-representable margin", "[Approx]") { |
| 151 | CHECK( 0.25f == Approx(0.0f).margin(0.25f) ); |
| 152 | |
| 153 | CHECK( 0.0f == Approx(0.25f).margin(0.25f) ); |
| 154 | CHECK( 0.5f == Approx(0.25f).margin(0.25f) ); |
| 155 | |
| 156 | CHECK( 245.0f == Approx(245.25f).margin(0.25f) ); |
| 157 | CHECK( 245.5f == Approx(245.25f).margin(0.25f) ); |
| 158 | } |
| 159 | |
| 160 | TEST_CASE("Approx setters validate their arguments", "[Approx]") { |
| 161 | REQUIRE_NOTHROW(Approx(0).margin(0)); |
| 162 | REQUIRE_NOTHROW(Approx(0).margin(1234656)); |
| 163 | |
| 164 | REQUIRE_THROWS_AS(Approx(0).margin(-2), std::domain_error); |
| 165 | |
| 166 | REQUIRE_NOTHROW(Approx(0).epsilon(0)); |
| 167 | REQUIRE_NOTHROW(Approx(0).epsilon(1)); |
| 168 | |
| 169 | REQUIRE_THROWS_AS(Approx(0).epsilon(-0.001), std::domain_error); |
| 170 | REQUIRE_THROWS_AS(Approx(0).epsilon(1.0001), std::domain_error); |
| 171 | } |
| 172 | |
| 173 | TEST_CASE("Default scale is invisible to comparison", "[Approx]") { |
| 174 | REQUIRE(101.000001 != Approx(100).epsilon(0.01)); |
| 175 | REQUIRE(std::pow(10, -5) != Approx(std::pow(10, -7))); |
| 176 | } |
| 177 | |
| 178 | TEST_CASE("Epsilon only applies to Approx's value", "[Approx]") { |
| 179 | REQUIRE(101.01 != Approx(100).epsilon(0.01)); |
| 180 | } |
| 181 | |
| 182 | TEST_CASE("Assorted miscellaneous tests", "[Approx][approvals]") { |
| 183 | REQUIRE(INFINITY == Approx(INFINITY)); |
| 184 | REQUIRE(-INFINITY != Approx(INFINITY)); |
| 185 | REQUIRE(1 != Approx(INFINITY)); |
| 186 | REQUIRE(INFINITY != Approx(1)); |
| 187 | REQUIRE(NAN != Approx(NAN)); |
| 188 | REQUIRE_FALSE(NAN == Approx(NAN)); |
| 189 | } |
| 190 | |
| 191 | TEST_CASE( "Comparison with explicitly convertible types", "[Approx]" ) |
| 192 | { |
| 193 | StrongDoubleTypedef td(10.0); |
| 194 | |
| 195 | REQUIRE(td == Approx(10.0)); |
| 196 | REQUIRE(Approx(10.0) == td); |
| 197 | |
| 198 | REQUIRE(td != Approx(11.0)); |
| 199 | REQUIRE(Approx(11.0) != td); |
| 200 | |
| 201 | REQUIRE(td <= Approx(10.0)); |
| 202 | REQUIRE(td <= Approx(11.0)); |
| 203 | REQUIRE(Approx(10.0) <= td); |
| 204 | REQUIRE(Approx(9.0) <= td); |
| 205 | |
| 206 | REQUIRE(td >= Approx(9.0)); |
| 207 | REQUIRE(td >= Approx(td)); |
| 208 | REQUIRE(Approx(td) >= td); |
| 209 | REQUIRE(Approx(11.0) >= td); |
| 210 | |
| 211 | } |
| 212 | |
| 213 | TEST_CASE("Approx::operator() is const correct", "[Approx][.approvals]") { |
| 214 | const Approx ap = Approx(0.0).margin(0.01); |
| 215 | |
| 216 | // As long as this compiles, the test should be considered passing |
| 217 | REQUIRE(1.0 == ap(1.0)); |
| 218 | } |