From ed442a15d28a0e7d65c14ebe2984ba7d80cf551a Mon Sep 17 00:00:00 2001 From: Menelaos Karavelas Date: Tue, 10 Jun 2014 09:19:58 +0300 Subject: [PATCH] [test][util] add unit test for math::sqrt --- test/util/Jamfile.v2 | 12 +++- test/util/math_sqrt.cpp | 104 ++++----------------------- test/util/number_types.hpp | 139 +++++++++++++++++++++++++++++++++++++ 3 files changed, 162 insertions(+), 93 deletions(-) diff --git a/test/util/Jamfile.v2 b/test/util/Jamfile.v2 index bf706df73..7fbf827d0 100644 --- a/test/util/Jamfile.v2 +++ b/test/util/Jamfile.v2 @@ -1,8 +1,13 @@ # Boost.Geometry (aka GGL, Generic Geometry Library) # -# Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands. -# Copyright (c) 2008-2012 Bruno Lalande, Paris, France. -# Copyright (c) 2009-2012 Mateusz Loskot, London, UK. +# Copyright (c) 2007-2014 Barend Gehrels, Amsterdam, the Netherlands. +# Copyright (c) 2008-2014 Bruno Lalande, Paris, France. +# Copyright (c) 2009-2014 Mateusz Loskot, London, UK. +# +# This file was modified by Oracle on 2014. +# Modifications copyright (c) 2014, Oracle and/or its affiliates. +# +# Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle # # Use, modification and distribution is subject to the Boost Software License, # Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at @@ -12,6 +17,7 @@ test-suite boost-geometry-util : [ run calculation_type.cpp ] [ run for_each_coordinate.cpp ] + [ run math_sqrt.cpp ] [ run rational.cpp ] [ run select_most_precise.cpp ] [ run write_dsv.cpp ] diff --git a/test/util/math_sqrt.cpp b/test/util/math_sqrt.cpp index 907062d5a..b76e1f5f1 100644 --- a/test/util/math_sqrt.cpp +++ b/test/util/math_sqrt.cpp @@ -19,6 +19,12 @@ #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 @@ -30,90 +36,6 @@ namespace bg = boost::geometry; -// define a custom number type and its sqrt in own namespace -namespace number_types -{ - -struct custom -{ - double m_value; - - custom() : m_value(0) {} - explicit custom(double value) : m_value(value) {} - - bool operator<(custom const& other) const - { - return m_value < other.m_value; - } - - custom operator-() const - { - return custom(-m_value); - } - - custom operator-(custom const& other) const - { - return custom(m_value - other.m_value); - } -}; - -inline custom sqrt(custom const& c) -{ - return custom(std::sqrt(c.m_value)); -} - -inline custom abs(custom const& c) -{ - return custom(std::abs(c.m_value)); -} - -} // namespace number_types - - - - - - - -// define a custom number type and its sqrt in global namespace -struct custom_global -{ - double m_value; - - custom_global() : m_value(0) {} - explicit custom_global(double value) : m_value(value) {} - - bool operator<(custom_global const& other) const - { - return m_value < other.m_value; - } - - custom_global operator-() const - { - return custom_global(-m_value); - } - - custom_global operator-(custom_global const& other) const - { - return custom_global(m_value - other.m_value); - } -}; - -inline custom_global sqrt(custom_global const& c) -{ - return custom_global(std::sqrt(c.m_value)); -} - -inline custom_global abs(custom_global const& c) -{ - return custom_global(std::abs(c.m_value)); -} - - - - - - // call BOOST_CHECK template @@ -205,19 +127,21 @@ BOOST_AUTO_TEST_CASE( test_math_sqrt_fundamental ) BOOST_AUTO_TEST_CASE( test_math_sqrt_custom ) { - typedef number_types::custom custom1; - typedef custom_global custom2; + typedef number_types::custom custom1; + typedef custom_global custom2; + typedef number_types::custom_with_global_sqrt custom3; - static const double sqrt2 = std::sqrt(2.0); + static const double sqrt2 = std::sqrt(2.0); check_sqrt::apply(custom1(2.0), custom1(sqrt2)); check_sqrt::apply(custom2(2.0), custom2(sqrt2)); + check_sqrt::apply(custom3(2.0), custom3(sqrt2)); #ifdef HAVE_TTMATH - typedef ttmath_big custom3; - typedef ttmath::Big<1, 4> custom4; + typedef ttmath_big custom4; + typedef ttmath::Big<1, 4> custom5; - check_sqrt::apply(custom3(2.0), custom3(sqrt2)); check_sqrt::apply(custom4(2.0), custom4(sqrt2)); + check_sqrt::apply(custom5(2.0), custom5(sqrt2)); #endif } diff --git a/test/util/number_types.hpp b/test/util/number_types.hpp index e69de29bb..bec68bf93 100644 --- a/test/util/number_types.hpp +++ b/test/util/number_types.hpp @@ -0,0 +1,139 @@ +// Boost.Geometry (aka GGL, Generic Geometry Library) +// Unit Test + +// Copyright (c) 2014, Oracle and/or its affiliates. + +// Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle + +// Licensed under the Boost Software License version 1.0. +// http://www.boost.org/users/license.html + +#ifndef BOOST_GEOMETRY_TEST_UTIL_NUMBER_TYPES_HPP +#define BOOST_GEOMETRY_TEST_UTIL_NUMBER_TYPES_HPP + +#include + + +// define a custom number type and its sqrt in own namespace +namespace number_types +{ + +template +struct custom +{ + typedef custom self; + + T m_value; + + custom() : m_value(0) {} + explicit custom(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); + } +}; + +template +inline custom sqrt(custom const& c) +{ + return custom(std::sqrt(c.m_value)); +} + +} // namespace number_types + + + + + + +// define a custom number type with sqrt in global namespace +namespace number_types +{ + +template +struct custom_with_global_sqrt +{ + typedef custom_with_global_sqrt self; + + T m_value; + + custom_with_global_sqrt() : m_value(0) {} + explicit custom_with_global_sqrt(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); + } +}; + +} // namespace number_types + +template +inline number_types::custom_with_global_sqrt +sqrt(number_types::custom_with_global_sqrt const& c) +{ + return number_types::custom_with_global_sqrt(std::sqrt(c.m_value)); +} + + + + + + + + +// define a custom number type and its sqrt in global namespace +template +struct custom_global +{ + typedef custom_global self; + + T m_value; + + custom_global() : m_value(0) {} + explicit custom_global(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); + } +}; + +template +inline custom_global sqrt(custom_global const& c) +{ + return custom_global(std::sqrt(c.m_value)); +} + +#endif // BOOST_GEOMETRY_TEST_UTIL_NUMBER_TYPES_HPP