From 58950ac91d961b606b1403dcd19c36f93822f304 Mon Sep 17 00:00:00 2001 From: Adam Wulkiewicz Date: Wed, 22 Jul 2015 04:50:47 +0200 Subject: [PATCH] [test][math] Add test for math::abs(). --- test/util/Jamfile.v2 | 1 + test/util/math_abs.cpp | 102 +++++++++++++++++++++++++++++++++++++ test/util/number_types.hpp | 52 ++++++++++++++++++- 3 files changed, 153 insertions(+), 2 deletions(-) create mode 100644 test/util/math_abs.cpp diff --git a/test/util/Jamfile.v2 b/test/util/Jamfile.v2 index a2f5e055b..39ef14af2 100644 --- a/test/util/Jamfile.v2 +++ b/test/util/Jamfile.v2 @@ -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 ] diff --git a/test/util/math_abs.cpp b/test/util/math_abs.cpp new file mode 100644 index 000000000..c1cfbf51e --- /dev/null +++ b/test/util/math_abs.cpp @@ -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 +#include + +#include + +#include + +#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 +#include + +#ifdef HAVE_TTMATH +# include +#endif + +namespace bg = boost::geometry; +namespace bgm = boost::geometry::math; + +template +bool eq(T const& l, T const& r) +{ + return !(l < r || r < l); +} + +BOOST_AUTO_TEST_CASE( test_math_abs ) +{ + { + float p1 = bgm::pi(); + double p2 = bgm::pi(); + long double p3 = bgm::pi(); + + 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 p1(bgm::pi()); + number_types::custom_with_global_sqrt p2(bgm::pi()); + custom_global p3(bgm::pi()); + custom_raw p4(bgm::pi()); + + 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 n1 = -p1; + number_types::custom_with_global_sqrt n2 = -p2; + custom_global n3 = -p3; + custom_raw 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<1, 4> p1 = bgm::pi >(); + + 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 +} + diff --git a/test/util/number_types.hpp b/test/util/number_types.hpp index bec68bf93..815dc956d 100644 --- a/test/util/number_types.hpp +++ b/test/util/number_types.hpp @@ -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 sqrt(custom const& c) return custom(std::sqrt(c.m_value)); } +template +inline custom fabs(custom const& c) +{ + return custom(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 const& c) return number_types::custom_with_global_sqrt(std::sqrt(c.m_value)); } - +template +inline number_types::custom_with_global_sqrt +fabs(number_types::custom_with_global_sqrt const& c) +{ + return number_types::custom_with_global_sqrt + (c.m_value < T(0) ? c.m_value : -c.m_value); +} @@ -136,4 +149,39 @@ inline custom_global sqrt(custom_global const& c) return custom_global(std::sqrt(c.m_value)); } +template +inline custom_global fabs(custom_global const& c) +{ + return custom_global(c.m_value < T(0) ? c.m_value : -c.m_value); +} + + + +// custom number type without functions definition +template +struct custom_raw +{ + typedef custom_raw 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