• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifdef HAVE_TTMATH
32 #  include <boost/geometry/extensions/contrib/ttmath_stub.hpp>
33 #endif
34 
35 namespace bg = boost::geometry;
36 namespace bgm = boost::geometry::math;
37 
38 template <typename T>
eq(T const & l,T const & r)39 bool eq(T const& l, T const& r)
40 {
41     return !(l < r || r < l);
42 }
43 
BOOST_AUTO_TEST_CASE(test_math_abs)44 BOOST_AUTO_TEST_CASE( test_math_abs )
45 {
46     {
47         float p1 = bgm::pi<float>();
48         double p2 = bgm::pi<double>();
49         long double p3 = bgm::pi<long double>();
50 
51         BOOST_CHECK(bgm::abs(p1) == p1);
52         BOOST_CHECK(bgm::abs(p2) == p2);
53         BOOST_CHECK(bgm::abs(p3) == p3);
54 
55         float n1 = -p1;
56         double n2 = -p2;
57         long double n3 = -p3;
58 
59         BOOST_CHECK(bgm::abs(n1) == p1);
60         BOOST_CHECK(bgm::abs(n2) == p2);
61         BOOST_CHECK(bgm::abs(n3) == p3);
62     }
63 
64     {
65         number_types::custom<double> p1(bgm::pi<double>());
66         number_types::custom_with_global_sqrt<double> p2(bgm::pi<double>());
67         custom_global<double> p3(bgm::pi<double>());
68         custom_raw<double> p4(bgm::pi<double>());
69 
70         BOOST_CHECK(eq(bgm::abs(p1), p1));
71         BOOST_CHECK(eq(bgm::abs(p2), p2));
72         BOOST_CHECK(eq(bgm::abs(p3), p3));
73         BOOST_CHECK(eq(bgm::abs(p4), p4));
74 
75         number_types::custom<double> n1 = -p1;
76         number_types::custom_with_global_sqrt<double> n2 = -p2;
77         custom_global<double> n3 = -p3;
78         custom_raw<double> n4 = -p4;
79 
80         BOOST_CHECK(eq(bgm::abs(n1), p1));
81         BOOST_CHECK(eq(bgm::abs(n2), p2));
82         BOOST_CHECK(eq(bgm::abs(n3), p3));
83         BOOST_CHECK(eq(bgm::abs(n4), p4));
84     }
85 
86 #ifdef HAVE_TTMATH
87     {
88         ttmath_big p1 = bgm::pi<ttmath_big>();
89         ttmath::Big<1, 4> p1 = bgm::pi<ttmath::Big<1, 4> >();
90 
91         BOOST_CHECK(bgm::abs(p1) == p1);
92         BOOST_CHECK(bgm::abs(p2) == p2);
93 
94         ttmath_big n1 = -p1;
95         ttmath::Big<1, 4> n2 = -p2;
96 
97         BOOST_CHECK(bgm::abs(n1) == p1);
98         BOOST_CHECK(bgm::abs(n2) == p2);
99     }
100 #endif
101 }
102 
103