[test][math] Add test for math::abs().

This commit is contained in:
Adam Wulkiewicz 2015-07-22 04:50:47 +02:00
parent 5a1e553c75
commit 58950ac91d
3 changed files with 153 additions and 2 deletions

View File

@ -18,6 +18,7 @@ test-suite boost-geometry-util
:
[ run calculation_type.cpp : : : : util_calculation_type ]
[ run for_each_coordinate.cpp : : : : util_for_each_coordinate ]
[ run math_abs.cpp : : : : util_math_abs ]
[ run math_equals.cpp : : : : util_math_equals ]
[ run math_sqrt.cpp : : : : util_math_sqrt ]
[ run promote_integral.cpp : : : : util_promote_integral ]

102
test/util/math_abs.cpp Normal file
View File

@ -0,0 +1,102 @@
// 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>
#ifdef HAVE_TTMATH
# include <boost/geometry/extensions/contrib/ttmath_stub.hpp>
#endif
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));
}
#ifdef HAVE_TTMATH
{
ttmath_big p1 = bgm::pi<ttmath_big>();
ttmath::Big<1, 4> p1 = bgm::pi<ttmath::Big<1, 4> >();
BOOST_CHECK(bgm::abs(p1) == p1);
BOOST_CHECK(bgm::abs(p2) == p2);
ttmath_big n1 = -p1;
ttmath::Big<1, 4> n2 = -p2;
BOOST_CHECK(bgm::abs(n1) == p1);
BOOST_CHECK(bgm::abs(n2) == p2);
}
#endif
}

View File

@ -1,9 +1,10 @@
// Boost.Geometry (aka GGL, Generic Geometry Library)
// Unit Test
// Copyright (c) 2014, Oracle and/or its affiliates.
// Copyright (c) 2014-2015 Oracle and/or its affiliates.
// Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
// 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
@ -50,6 +51,12 @@ inline custom<T> sqrt(custom<T> const& c)
return custom<T>(std::sqrt(c.m_value));
}
template <typename T>
inline custom<T> fabs(custom<T> const& c)
{
return custom<T>(c.m_value < T(0) ? c.m_value : -c.m_value);
}
} // namespace number_types
@ -96,7 +103,13 @@ sqrt(number_types::custom_with_global_sqrt<T> const& c)
return number_types::custom_with_global_sqrt<T>(std::sqrt(c.m_value));
}
template <typename T>
inline number_types::custom_with_global_sqrt<T>
fabs(number_types::custom_with_global_sqrt<T> const& c)
{
return number_types::custom_with_global_sqrt<T>
(c.m_value < T(0) ? c.m_value : -c.m_value);
}
@ -136,4 +149,39 @@ inline custom_global<T> sqrt(custom_global<T> const& c)
return custom_global<T>(std::sqrt(c.m_value));
}
template <typename T>
inline custom_global<T> fabs(custom_global<T> const& c)
{
return custom_global<T>(c.m_value < T(0) ? c.m_value : -c.m_value);
}
// custom number type without functions definition
template <typename T>
struct custom_raw
{
typedef custom_raw<T> self;
T m_value;
custom_raw() : m_value(0) {}
explicit custom_raw(T const& value) : m_value(value) {}
bool operator<(self const& other) const
{
return m_value < other.m_value;
}
self operator-() const
{
return self(-m_value);
}
self operator-(self const& other) const
{
return self(m_value - other.m_value);
}
};
#endif // BOOST_GEOMETRY_TEST_UTIL_NUMBER_TYPES_HPP