]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Boost.Geometry (aka GGL, Generic Geometry Library) |
2 | // Unit Test | |
3 | ||
4 | // Copyright (c) 2014, Oracle and/or its affiliates. | |
5 | ||
6 | // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle | |
7 | ||
8 | // Licensed under the Boost Software License version 1.0. | |
9 | // http://www.boost.org/users/license.html | |
10 | ||
11 | #ifndef BOOST_TEST_MODULE | |
12 | #define BOOST_TEST_MODULE test_math_sqrt | |
13 | #endif | |
14 | ||
15 | #include <cmath> | |
16 | #include <iostream> | |
17 | ||
18 | #include <boost/test/included/unit_test.hpp> | |
19 | ||
20 | #include <boost/config.hpp> | |
21 | #include <boost/type_traits/is_fundamental.hpp> | |
22 | ||
23 | #include "number_types.hpp" | |
24 | ||
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 | |
28 | ||
29 | #include <boost/geometry/util/math.hpp> | |
30 | #include <boost/geometry/algorithms/not_implemented.hpp> | |
31 | ||
7c673cae FG |
32 | namespace bg = boost::geometry; |
33 | ||
34 | ||
35 | ||
36 | ||
37 | // call BOOST_CHECK | |
38 | template <typename Argument, bool IsFundamental /* true */> | |
39 | struct check | |
40 | { | |
41 | template <typename Result> | |
42 | static inline void apply(Argument const& arg, Result const& result) | |
43 | { | |
44 | BOOST_CHECK_CLOSE(static_cast<double>(bg::math::sqrt(arg)), | |
45 | static_cast<double>(result), | |
46 | 0.00001); | |
47 | } | |
48 | }; | |
49 | ||
50 | ||
51 | template <typename Argument> | |
52 | struct check<Argument, false> | |
53 | { | |
54 | template <typename Result> | |
55 | static inline void apply(Argument const& arg, Result const& result) | |
56 | { | |
57 | Result const tol(0.00001); | |
58 | BOOST_CHECK( bg::math::abs(bg::math::sqrt(arg) - result) < tol ); | |
59 | } | |
60 | }; | |
61 | ||
62 | ||
63 | ||
64 | ||
65 | ||
66 | ||
67 | // test sqrt return type and value | |
68 | template | |
69 | < | |
70 | typename Argument, | |
71 | typename ExpectedResult, | |
72 | typename Result = typename bg::math::detail::square_root | |
73 | < | |
74 | Argument | |
75 | >::return_type, | |
76 | bool IsFundamental = boost::is_fundamental<Argument>::value | |
77 | > | |
78 | struct check_sqrt | |
79 | : bg::not_implemented<Argument, Result, ExpectedResult> | |
80 | {}; | |
81 | ||
82 | ||
83 | template <typename Argument, typename Result, bool IsFundamental> | |
84 | struct check_sqrt<Argument, Result, Result, IsFundamental> | |
85 | { | |
86 | static inline void apply(Argument const& arg, Result const& result) | |
87 | { | |
88 | #ifdef BOOST_GEOMETRY_TEST_DEBUG | |
89 | std::cout << "testing: " << typeid(Result).name() | |
90 | << " sqrt(" << typeid(Argument).name() | |
91 | << ")" << std::endl; | |
92 | #endif | |
93 | check<Argument, IsFundamental>::apply(arg, result); | |
94 | } | |
95 | }; | |
96 | ||
97 | ||
98 | ||
99 | ||
100 | ||
101 | ||
102 | // test cases | |
103 | BOOST_AUTO_TEST_CASE( test_math_sqrt_fundamental ) | |
104 | { | |
105 | static const double sqrt2 = std::sqrt(2.0); | |
106 | static const long double sqrt2L = std::sqrt(2.0L); | |
107 | static const float sqrt2F = std::sqrt(2.0F); | |
108 | ||
109 | check_sqrt<float, float>::apply(2.0F, sqrt2F); | |
110 | check_sqrt<double, double>::apply(2.0, sqrt2); | |
111 | check_sqrt<long double, long double>::apply(2.0L, sqrt2L); | |
112 | ||
113 | check_sqrt<char, double>::apply(2, sqrt2); | |
114 | check_sqrt<signed char, double>::apply(2, sqrt2); | |
115 | check_sqrt<short, double>::apply(2, sqrt2); | |
116 | check_sqrt<int, double>::apply(2, sqrt2); | |
117 | check_sqrt<long, double>::apply(2L, sqrt2); | |
118 | #if !defined(BOOST_NO_LONG_LONG) | |
119 | check_sqrt<long long, double>::apply(2LL, sqrt2); | |
120 | #endif | |
121 | #ifdef BOOST_HAS_LONG_LONG | |
122 | check_sqrt | |
123 | < | |
124 | boost::long_long_type, double | |
125 | >::apply(boost::long_long_type(2), sqrt2); | |
126 | #endif | |
127 | } | |
128 | ||
129 | ||
130 | BOOST_AUTO_TEST_CASE( test_math_sqrt_custom ) | |
131 | { | |
132 | typedef number_types::custom<double> custom1; | |
133 | typedef custom_global<double> custom2; | |
134 | typedef number_types::custom_with_global_sqrt<double> custom3; | |
135 | ||
136 | static const double sqrt2 = std::sqrt(2.0); | |
137 | ||
138 | check_sqrt<custom1, custom1>::apply(custom1(2.0), custom1(sqrt2)); | |
139 | check_sqrt<custom2, custom2>::apply(custom2(2.0), custom2(sqrt2)); | |
140 | check_sqrt<custom3, custom3>::apply(custom3(2.0), custom3(sqrt2)); | |
7c673cae | 141 | } |