1 // 2 // Test for BOOST_TEST_WITH 3 // 4 // Copyright (c) 2020 Bjorn Reese 5 // 6 // Distributed under the Boost Software License, Version 1.0. 7 // See accompanying file LICENSE_1_0.txt or copy at 8 // http://www.boost.org/LICENSE_1_0.txt 9 // 10 11 #include <boost/core/lightweight_test.hpp> 12 #include <cmath> 13 14 template <typename T> 15 struct with_tolerance 16 { with_tolerancewith_tolerance17 with_tolerance(T tolerance) : tolerance(tolerance) {} operator ()with_tolerance18 bool operator()(T lhs, T rhs) const 19 { 20 return (std::abs(lhs - rhs) <= tolerance); 21 } 22 23 private: 24 T tolerance; 25 }; 26 test_tolerance_predicate()27void test_tolerance_predicate() 28 { 29 BOOST_TEST_WITH( 1.0, 1.0, with_tolerance<double>(1e-5) ); 30 BOOST_TEST_WITH( 1.0, 1.0 - 1e-6, with_tolerance<double>(1e-5) ); 31 BOOST_TEST_WITH( 1.0, 1.0 + 1e-6, with_tolerance<double>(1e-5) ); 32 } 33 main()34int main() 35 { 36 test_tolerance_predicate(); 37 38 return boost::report_errors(); 39 } 40