[test][util] add unit test for math::sqrt

This commit is contained in:
Menelaos Karavelas 2014-06-10 09:19:58 +03:00
parent ef1a257242
commit ed442a15d2
3 changed files with 162 additions and 93 deletions

View File

@ -1,8 +1,13 @@
# Boost.Geometry (aka GGL, Generic Geometry Library) # Boost.Geometry (aka GGL, Generic Geometry Library)
# #
# Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands. # Copyright (c) 2007-2014 Barend Gehrels, Amsterdam, the Netherlands.
# Copyright (c) 2008-2012 Bruno Lalande, Paris, France. # Copyright (c) 2008-2014 Bruno Lalande, Paris, France.
# Copyright (c) 2009-2012 Mateusz Loskot, London, UK. # 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, # Use, modification and distribution is subject to the Boost Software License,
# Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at # 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 calculation_type.cpp ]
[ run for_each_coordinate.cpp ] [ run for_each_coordinate.cpp ]
[ run math_sqrt.cpp ]
[ run rational.cpp ] [ run rational.cpp ]
[ run select_most_precise.cpp ] [ run select_most_precise.cpp ]
[ run write_dsv.cpp ] [ run write_dsv.cpp ]

View File

@ -19,6 +19,12 @@
#include <boost/type_traits/is_fundamental.hpp> #include <boost/type_traits/is_fundamental.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/util/math.hpp>
#include <boost/geometry/algorithms/not_implemented.hpp> #include <boost/geometry/algorithms/not_implemented.hpp>
@ -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 // call BOOST_CHECK
template <typename Argument, bool IsFundamental /* true */> template <typename Argument, bool IsFundamental /* true */>
@ -205,19 +127,21 @@ BOOST_AUTO_TEST_CASE( test_math_sqrt_fundamental )
BOOST_AUTO_TEST_CASE( test_math_sqrt_custom ) BOOST_AUTO_TEST_CASE( test_math_sqrt_custom )
{ {
typedef number_types::custom custom1; typedef number_types::custom<double> custom1;
typedef custom_global custom2; typedef custom_global<double> custom2;
typedef number_types::custom_with_global_sqrt<double> custom3;
static const double sqrt2 = std::sqrt(2.0); static const double sqrt2 = std::sqrt(2.0);
check_sqrt<custom1, custom1>::apply(custom1(2.0), custom1(sqrt2)); check_sqrt<custom1, custom1>::apply(custom1(2.0), custom1(sqrt2));
check_sqrt<custom2, custom2>::apply(custom2(2.0), custom2(sqrt2)); check_sqrt<custom2, custom2>::apply(custom2(2.0), custom2(sqrt2));
check_sqrt<custom3, custom3>::apply(custom3(2.0), custom3(sqrt2));
#ifdef HAVE_TTMATH #ifdef HAVE_TTMATH
typedef ttmath_big custom3; typedef ttmath_big custom4;
typedef ttmath::Big<1, 4> custom4; typedef ttmath::Big<1, 4> custom5;
check_sqrt<custom3, custom3>::apply(custom3(2.0), custom3(sqrt2));
check_sqrt<custom4, custom4>::apply(custom4(2.0), custom4(sqrt2)); check_sqrt<custom4, custom4>::apply(custom4(2.0), custom4(sqrt2));
check_sqrt<custom5, custom5>::apply(custom5(2.0), custom5(sqrt2));
#endif #endif
} }

View File

@ -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 <cmath>
// define a custom number type and its sqrt in own namespace
namespace number_types
{
template <typename T>
struct custom
{
typedef custom<T> 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 <typename T>
inline custom<T> sqrt(custom<T> const& c)
{
return custom<T>(std::sqrt(c.m_value));
}
} // namespace number_types
// define a custom number type with sqrt in global namespace
namespace number_types
{
template <typename T>
struct custom_with_global_sqrt
{
typedef custom_with_global_sqrt<T> 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 <typename T>
inline number_types::custom_with_global_sqrt<T>
sqrt(number_types::custom_with_global_sqrt<T> const& c)
{
return number_types::custom_with_global_sqrt<T>(std::sqrt(c.m_value));
}
// define a custom number type and its sqrt in global namespace
template <typename T>
struct custom_global
{
typedef custom_global<T> 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 <typename T>
inline custom_global<T> sqrt(custom_global<T> const& c)
{
return custom_global<T>(std::sqrt(c.m_value));
}
#endif // BOOST_GEOMETRY_TEST_UTIL_NUMBER_TYPES_HPP