4 // Copyright (c) 2016-2017 Oracle and/or its affiliates.
6 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
8 // Use, modification and distribution is subject to the Boost Software License,
9 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
10 // http://www.boost.org/LICENSE_1_0.txt)
12 #ifndef BOOST_GEOMETRY_TEST_FORMULA_HPP
13 #define BOOST_GEOMETRY_TEST_FORMULA_HPP
15 #include <geometry_test_common.hpp>
17 #include <boost/geometry/util/math.hpp>
19 void normalize_deg(double & deg)
28 #define BOOST_GEOMETRY_CHECK_CLOSE( L, R, T, M ) BOOST_TEST_TOOL_IMPL( 0, \
29 ::boost::test_tools::check_is_close_t(), M, CHECK, CHECK_MSG, (L)(R)(::boost::math::fpc::percent_tolerance(T)) )
32 void check_one(std::string const& name,
33 double result, double expected, double reference, double reference_error,
34 bool normalize = false, bool check_reference_only = false)
36 std::string id = name.empty() ? "" : (name + " : ");
40 normalize_deg(result);
41 normalize_deg(expected);
42 normalize_deg(reference);
45 if (! check_reference_only)
47 double eps = std::numeric_limits<double>::epsilon();
48 double abs_result = bg::math::abs(result);
49 double abs_expected = bg::math::abs(expected);
50 double res_max = (std::max)(abs_result, abs_expected);
51 double res_min = (std::min)(abs_result, abs_expected);
52 if (res_min <= eps) // including 0
54 bool is_close = abs_result <= 30 * eps && abs_expected <= 30 * eps;
55 BOOST_CHECK_MESSAGE((is_close),
56 id << std::setprecision(20) << "result {" << result << "} different than expected {" << expected << "}.");
58 else if (res_max > 100 * eps)
60 BOOST_GEOMETRY_CHECK_CLOSE(result, expected, 0.1,
61 id << std::setprecision(20) << "result {" << result << "} different than expected {" << expected << "}.");
63 else if (res_max > 10 * eps)
65 BOOST_GEOMETRY_CHECK_CLOSE(result, expected, 10,
66 id << std::setprecision(20) << "result {" << result << "} different than expected {" << expected << "}.");
68 else if (res_max > eps)
70 BOOST_GEOMETRY_CHECK_CLOSE(result, expected, 1000,
71 id << std::setprecision(20) << "result {" << result << "} different than expected {" << expected << "}.");
75 // NOTE: in some cases it probably will be necessary to normalize
76 // the differences between the result and expected result
77 double ref_diff = bg::math::abs(result - reference);
78 double ref_max = (std::max)(bg::math::abs(result), bg::math::abs(reference));
79 bool is_ref_close = ref_diff <= reference_error || ref_diff <= reference_error * ref_max;
80 BOOST_CHECK_MESSAGE((is_ref_close),
81 id << std::setprecision(20) << "result {" << result << "} and reference {" << reference << "} not close enough.");
84 void check_one(double result, double expected, double reference, double reference_error,
85 bool normalize = false, bool check_reference_only = false)
87 check_one("", result, expected, reference, reference_error, normalize, check_reference_only);
90 #endif // BOOST_GEOMETRY_TEST_FORMULA_HPP