File: math_abs.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (82 lines) | stat: -rw-r--r-- 2,219 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Boost.Geometry (aka GGL, Generic Geometry Library)
// Unit Test

// Copyright (c) 2015, Oracle and/or its affiliates.

// Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle

// Licensed under the Boost Software License version 1.0.
// http://www.boost.org/users/license.html

#ifndef BOOST_TEST_MODULE
#define BOOST_TEST_MODULE test_math_abs
#endif

#include <cmath>
#include <iostream>

#include <boost/test/included/unit_test.hpp>

#include <boost/config.hpp>

#include "number_types.hpp"

// important: the include above must precede the include below,
// otherwise the test will fail for the custom number type:
// custom_with_global_sqrt

#include <boost/geometry/util/math.hpp>
#include <boost/geometry/algorithms/not_implemented.hpp>

namespace bg = boost::geometry;
namespace bgm = boost::geometry::math;

template <typename T>
bool eq(T const& l, T const& r)
{
    return !(l < r || r < l);
}

BOOST_AUTO_TEST_CASE( test_math_abs )
{
    {
        float p1 = bgm::pi<float>();
        double p2 = bgm::pi<double>();
        long double p3 = bgm::pi<long double>();

        BOOST_CHECK(bgm::abs(p1) == p1);
        BOOST_CHECK(bgm::abs(p2) == p2);
        BOOST_CHECK(bgm::abs(p3) == p3);

        float n1 = -p1;
        double n2 = -p2;
        long double n3 = -p3;

        BOOST_CHECK(bgm::abs(n1) == p1);
        BOOST_CHECK(bgm::abs(n2) == p2);
        BOOST_CHECK(bgm::abs(n3) == p3);
    }

    {
        number_types::custom<double> p1(bgm::pi<double>());
        number_types::custom_with_global_sqrt<double> p2(bgm::pi<double>());
        custom_global<double> p3(bgm::pi<double>());
        custom_raw<double> p4(bgm::pi<double>());

        BOOST_CHECK(eq(bgm::abs(p1), p1));
        BOOST_CHECK(eq(bgm::abs(p2), p2));
        BOOST_CHECK(eq(bgm::abs(p3), p3));
        BOOST_CHECK(eq(bgm::abs(p4), p4));

        number_types::custom<double> n1 = -p1;
        number_types::custom_with_global_sqrt<double> n2 = -p2;
        custom_global<double> n3 = -p3;
        custom_raw<double> n4 = -p4;

        BOOST_CHECK(eq(bgm::abs(n1), p1));
        BOOST_CHECK(eq(bgm::abs(n2), p2));
        BOOST_CHECK(eq(bgm::abs(n3), p3));
        BOOST_CHECK(eq(bgm::abs(n4), p4));
    }
}