1 // Boost.Geometry (aka GGL, Generic Geometry Library)
4 // Copyright (c) 2014, Oracle and/or its affiliates.
6 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
8 // Licensed under the Boost Software License version 1.0.
9 // http://www.boost.org/users/license.html
11 #ifndef BOOST_TEST_MODULE
12 #define BOOST_TEST_MODULE test_math_sqrt
18 #include <boost/test/included/unit_test.hpp>
20 #include <boost/config.hpp>
21 #include <boost/type_traits/is_fundamental.hpp>
23 #include "number_types.hpp"
25 // important: the include above must precede the include below,
26 // otherwise the test will fail for the custom number type:
27 // custom_with_global_sqrt
29 #include <boost/geometry/util/math.hpp>
30 #include <boost/geometry/algorithms/not_implemented.hpp>
33 # include <boost/geometry/extensions/contrib/ttmath_stub.hpp>
36 namespace bg
= boost::geometry
;
42 template <typename Argument
, bool IsFundamental
/* true */>
45 template <typename Result
>
46 static inline void apply(Argument
const& arg
, Result
const& result
)
48 BOOST_CHECK_CLOSE(static_cast<double>(bg::math::sqrt(arg
)),
49 static_cast<double>(result
),
55 template <typename Argument
>
56 struct check
<Argument
, false>
58 template <typename Result
>
59 static inline void apply(Argument
const& arg
, Result
const& result
)
61 Result
const tol(0.00001);
62 BOOST_CHECK( bg::math::abs(bg::math::sqrt(arg
) - result
) < tol
);
71 // test sqrt return type and value
75 typename ExpectedResult
,
76 typename Result
= typename
bg::math::detail::square_root
80 bool IsFundamental
= boost::is_fundamental
<Argument
>::value
83 : bg::not_implemented
<Argument
, Result
, ExpectedResult
>
87 template <typename Argument
, typename Result
, bool IsFundamental
>
88 struct check_sqrt
<Argument
, Result
, Result
, IsFundamental
>
90 static inline void apply(Argument
const& arg
, Result
const& result
)
92 #ifdef BOOST_GEOMETRY_TEST_DEBUG
93 std::cout
<< "testing: " << typeid(Result
).name()
94 << " sqrt(" << typeid(Argument
).name()
97 check
<Argument
, IsFundamental
>::apply(arg
, result
);
107 BOOST_AUTO_TEST_CASE( test_math_sqrt_fundamental
)
109 static const double sqrt2
= std::sqrt(2.0);
110 static const long double sqrt2L
= std::sqrt(2.0L);
111 static const float sqrt2F
= std::sqrt(2.0F
);
113 check_sqrt
<float, float>::apply(2.0F
, sqrt2F
);
114 check_sqrt
<double, double>::apply(2.0, sqrt2
);
115 check_sqrt
<long double, long double>::apply(2.0L, sqrt2L
);
117 check_sqrt
<char, double>::apply(2, sqrt2
);
118 check_sqrt
<signed char, double>::apply(2, sqrt2
);
119 check_sqrt
<short, double>::apply(2, sqrt2
);
120 check_sqrt
<int, double>::apply(2, sqrt2
);
121 check_sqrt
<long, double>::apply(2L, sqrt2
);
122 #if !defined(BOOST_NO_LONG_LONG)
123 check_sqrt
<long long, double>::apply(2LL, sqrt2
);
125 #ifdef BOOST_HAS_LONG_LONG
128 boost::long_long_type
, double
129 >::apply(boost::long_long_type(2), sqrt2
);
134 BOOST_AUTO_TEST_CASE( test_math_sqrt_custom
)
136 typedef number_types::custom
<double> custom1
;
137 typedef custom_global
<double> custom2
;
138 typedef number_types::custom_with_global_sqrt
<double> custom3
;
140 static const double sqrt2
= std::sqrt(2.0);
142 check_sqrt
<custom1
, custom1
>::apply(custom1(2.0), custom1(sqrt2
));
143 check_sqrt
<custom2
, custom2
>::apply(custom2(2.0), custom2(sqrt2
));
144 check_sqrt
<custom3
, custom3
>::apply(custom3(2.0), custom3(sqrt2
));
147 typedef ttmath_big custom4
;
148 typedef ttmath::Big
<1, 4> custom5
;
150 check_sqrt
<custom4
, custom4
>::apply(custom4(2.0), custom4(sqrt2
));
151 check_sqrt
<custom5
, custom5
>::apply(custom5(2.0), custom5(sqrt2
));