1 | // Boost.Geometry (aka GGL, Generic Geometry Library) |
2 | // Unit Test |
3 | |
4 | // Copyright (c) 2015, Oracle and/or its affiliates. |
5 | |
6 | // Contributed and/or modified by Adam Wulkiewicz, 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_abs |
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 | |
22 | #include "number_types.hpp" |
23 | |
24 | // important: the include above must precede the include below, |
25 | // otherwise the test will fail for the custom number type: |
26 | // custom_with_global_sqrt |
27 | |
28 | #include <boost/geometry/util/math.hpp> |
29 | #include <boost/geometry/algorithms/not_implemented.hpp> |
30 | |
31 | namespace bg = boost::geometry; |
32 | namespace bgm = boost::geometry::math; |
33 | |
34 | template <typename T> |
35 | bool eq(T const& l, T const& r) |
36 | { |
37 | return !(l < r || r < l); |
38 | } |
39 | |
40 | BOOST_AUTO_TEST_CASE( test_math_abs ) |
41 | { |
42 | { |
43 | float p1 = bgm::pi<float>(); |
44 | double p2 = bgm::pi<double>(); |
45 | long double p3 = bgm::pi<long double>(); |
46 | |
47 | BOOST_CHECK(bgm::abs(p1) == p1); |
48 | BOOST_CHECK(bgm::abs(p2) == p2); |
49 | BOOST_CHECK(bgm::abs(p3) == p3); |
50 | |
51 | float n1 = -p1; |
52 | double n2 = -p2; |
53 | long double n3 = -p3; |
54 | |
55 | BOOST_CHECK(bgm::abs(n1) == p1); |
56 | BOOST_CHECK(bgm::abs(n2) == p2); |
57 | BOOST_CHECK(bgm::abs(n3) == p3); |
58 | } |
59 | |
60 | { |
61 | number_types::custom<double> p1(bgm::pi<double>()); |
62 | number_types::custom_with_global_sqrt<double> p2(bgm::pi<double>()); |
63 | custom_global<double> p3(bgm::pi<double>()); |
64 | custom_raw<double> p4(bgm::pi<double>()); |
65 | |
66 | BOOST_CHECK(eq(bgm::abs(p1), p1)); |
67 | BOOST_CHECK(eq(bgm::abs(p2), p2)); |
68 | BOOST_CHECK(eq(bgm::abs(p3), p3)); |
69 | BOOST_CHECK(eq(bgm::abs(p4), p4)); |
70 | |
71 | number_types::custom<double> n1 = -p1; |
72 | number_types::custom_with_global_sqrt<double> n2 = -p2; |
73 | custom_global<double> n3 = -p3; |
74 | custom_raw<double> n4 = -p4; |
75 | |
76 | BOOST_CHECK(eq(bgm::abs(n1), p1)); |
77 | BOOST_CHECK(eq(bgm::abs(n2), p2)); |
78 | BOOST_CHECK(eq(bgm::abs(n3), p3)); |
79 | BOOST_CHECK(eq(bgm::abs(n4), p4)); |
80 | } |
81 | } |
82 | |
83 | |