Changed test to use Boost.Test's unit test system

[SVN r35815]
This commit is contained in:
Daryle Walker
2006-11-03 03:08:27 +00:00
committed by Peter Dimov
parent c041451a54
commit c8faa83d47

View File

@ -1,6 +1,6 @@
// Boost GCD & LCM common_factor.hpp test program --------------------------//
// (C) Copyright Daryle Walker 2001.
// (C) Copyright Daryle Walker 2001, 2006.
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
@ -8,81 +8,93 @@
// See http://www.boost.org for most recent version including documentation.
// Revision History
// 02 Nov 2006 Change to Boost.Test's unit test system
// 07 Nov 2001 Initial version (Daryle Walker)
#define BOOST_INCLUDE_MAIN
#define BOOST_TEST_MAIN "Boost.Math GCD & LCM unit tests"
#include <boost/config.hpp> // for BOOST_MSVC
#include <boost/cstdlib.hpp> // for boost::exit_success
#include <boost/math/common_factor.hpp> // for boost::math::gcd, etc.
#include <boost/test/test_tools.hpp> // for main, BOOST_CHECK
#include <iostream> // for std::cout (std::endl indirectly)
#include <boost/mpl/list.hpp> // for boost::mpl::list
#include <boost/test/unit_test.hpp>
#include <boost/test/test_case_template.hpp>
// Control to determine what kind of built-in integers are used
#ifndef CONTROL_INT_TYPE
#define CONTROL_INT_TYPE int
#endif
namespace {
// TODO: add new signed and unsigned types here, with and without numeric_limits
// specialization; add polynominal/non-real type; especially after any switch to
// the binary-GCD algorithm for built-in types
// Various types to test with each GCD/LCM
typedef ::boost::mpl::list<short, int, long> builtin_signed_test_types;
typedef ::boost::mpl::list<unsigned short, unsigned, unsigned long>
builtin_unsigned_test_types;
} // namespace
// Main testing function
int
test_main
(
int , // "argc" is unused
char * [] // "argv" is unused
)
{
using std::cout;
using std::endl;
// GCD tests
BOOST_AUTO_TEST_SUITE( gcd_test_suite )
// GCD on built-in signed integer types
BOOST_AUTO_TEST_CASE_TEMPLATE( gcd_int_test, T, builtin_signed_test_types )
{
#ifndef BOOST_MSVC
using boost::math::gcd;
using boost::math::static_gcd;
using boost::math::lcm;
using boost::math::static_lcm;
#else
using namespace boost::math;
#endif
typedef CONTROL_INT_TYPE int_type;
typedef unsigned CONTROL_INT_TYPE uint_type;
// Originally from Boost.Rational tests
BOOST_CHECK_EQUAL( gcd<T>( 1, -1), static_cast<T>( 1) );
BOOST_CHECK_EQUAL( gcd<T>( -1, 1), static_cast<T>( 1) );
BOOST_CHECK_EQUAL( gcd<T>( 1, 1), static_cast<T>( 1) );
BOOST_CHECK_EQUAL( gcd<T>( -1, -1), static_cast<T>( 1) );
BOOST_CHECK_EQUAL( gcd<T>( 0, 0), static_cast<T>( 0) );
BOOST_CHECK_EQUAL( gcd<T>( 7, 0), static_cast<T>( 7) );
BOOST_CHECK_EQUAL( gcd<T>( 0, 9), static_cast<T>( 9) );
BOOST_CHECK_EQUAL( gcd<T>( -7, 0), static_cast<T>( 7) );
BOOST_CHECK_EQUAL( gcd<T>( 0, -9), static_cast<T>( 9) );
BOOST_CHECK_EQUAL( gcd<T>( 42, 30), static_cast<T>( 6) );
BOOST_CHECK_EQUAL( gcd<T>( 6, -9), static_cast<T>( 3) );
BOOST_CHECK_EQUAL( gcd<T>(-10, -10), static_cast<T>(10) );
BOOST_CHECK_EQUAL( gcd<T>(-25, -10), static_cast<T>( 5) );
BOOST_CHECK_EQUAL( gcd<T>( 3, 7), static_cast<T>( 1) );
BOOST_CHECK_EQUAL( gcd<T>( 8, 9), static_cast<T>( 1) );
BOOST_CHECK_EQUAL( gcd<T>( 7, 49), static_cast<T>( 7) );
}
// GCD tests
cout << "Doing tests on gcd." << endl;
// GCD on built-in unsigned integer types
BOOST_AUTO_TEST_CASE_TEMPLATE(gcd_unsigned_test, T, builtin_unsigned_test_types)
{
#ifndef BOOST_MSVC
using boost::math::gcd;
#else
using namespace boost::math;
#endif
BOOST_CHECK( gcd<int_type>( 1, -1) == 1 );
BOOST_CHECK( gcd<int_type>( -1, 1) == 1 );
BOOST_CHECK( gcd<int_type>( 1, 1) == 1 );
BOOST_CHECK( gcd<int_type>( -1, -1) == 1 );
BOOST_CHECK( gcd<int_type>( 0, 0) == 0 );
BOOST_CHECK( gcd<int_type>( 7, 0) == 7 );
BOOST_CHECK( gcd<int_type>( 0, 9) == 9 );
BOOST_CHECK( gcd<int_type>( -7, 0) == 7 );
BOOST_CHECK( gcd<int_type>( 0, -9) == 9 );
BOOST_CHECK( gcd<int_type>( 42, 30) == 6 );
BOOST_CHECK( gcd<int_type>( 6, -9) == 3 );
BOOST_CHECK( gcd<int_type>(-10, -10) == 10 );
BOOST_CHECK( gcd<int_type>(-25, -10) == 5 );
BOOST_CHECK( gcd<int_type>( 3, 7) == 1 );
BOOST_CHECK( gcd<int_type>( 8, 9) == 1 );
BOOST_CHECK( gcd<int_type>( 7, 49) == 7 );
BOOST_CHECK_EQUAL( gcd<T>( 1u, 1u), static_cast<T>( 1u) );
BOOST_CHECK_EQUAL( gcd<T>( 0u, 0u), static_cast<T>( 0u) );
BOOST_CHECK_EQUAL( gcd<T>( 7u, 0u), static_cast<T>( 7u) );
BOOST_CHECK_EQUAL( gcd<T>( 0u, 9u), static_cast<T>( 9u) );
BOOST_CHECK_EQUAL( gcd<T>( 42u, 30u), static_cast<T>( 6u) );
BOOST_CHECK_EQUAL( gcd<T>( 3u, 7u), static_cast<T>( 1u) );
BOOST_CHECK_EQUAL( gcd<T>( 8u, 9u), static_cast<T>( 1u) );
BOOST_CHECK_EQUAL( gcd<T>( 7u, 49u), static_cast<T>( 7u) );
}
// GCD tests
cout << "Doing tests on unsigned-gcd." << endl;
BOOST_CHECK( gcd<uint_type>( 1u, 1u) == 1u );
BOOST_CHECK( gcd<uint_type>( 0u, 0u) == 0u );
BOOST_CHECK( gcd<uint_type>( 7u, 0u) == 7u );
BOOST_CHECK( gcd<uint_type>( 0u, 9u) == 9u );
BOOST_CHECK( gcd<uint_type>( 42u, 30u) == 6u );
BOOST_CHECK( gcd<uint_type>( 3u, 7u) == 1u );
BOOST_CHECK( gcd<uint_type>( 8u, 9u) == 1u );
BOOST_CHECK( gcd<uint_type>( 7u, 49u) == 7u );
cout << "Doing tests on static_gcd." << endl;
// GCD at compile-time
BOOST_AUTO_TEST_CASE( gcd_static_test )
{
#ifndef BOOST_MSVC
using boost::math::static_gcd;
#else
using namespace boost::math;
#endif
// Can't use "BOOST_CHECK_EQUAL", otherwise the "value" member will be
// disqualified as compile-time-only constant, needing explicit definition
BOOST_CHECK( (static_gcd< 1, 1>::value) == 1 );
BOOST_CHECK( (static_gcd< 0, 0>::value) == 0 );
BOOST_CHECK( (static_gcd< 7, 0>::value) == 7 );
@ -91,40 +103,76 @@ test_main
BOOST_CHECK( (static_gcd< 3, 7>::value) == 1 );
BOOST_CHECK( (static_gcd< 8, 9>::value) == 1 );
BOOST_CHECK( (static_gcd< 7, 49>::value) == 7 );
}
// LCM tests
cout << "Doing tests on lcm." << endl;
// TODO: non-built-in signed and unsigned integer tests, with and without
// numeric_limits specialization; polynominal tests; note any changes if
// built-ins switch to binary-GCD algorithm
BOOST_CHECK( lcm<int_type>( 1, -1) == 1 );
BOOST_CHECK( lcm<int_type>( -1, 1) == 1 );
BOOST_CHECK( lcm<int_type>( 1, 1) == 1 );
BOOST_CHECK( lcm<int_type>( -1, -1) == 1 );
BOOST_CHECK( lcm<int_type>( 0, 0) == 0 );
BOOST_CHECK( lcm<int_type>( 6, 0) == 0 );
BOOST_CHECK( lcm<int_type>( 0, 7) == 0 );
BOOST_CHECK( lcm<int_type>( -5, 0) == 0 );
BOOST_CHECK( lcm<int_type>( 0, -4) == 0 );
BOOST_CHECK( lcm<int_type>( 18, 30) == 90 );
BOOST_CHECK( lcm<int_type>( -6, 9) == 18 );
BOOST_CHECK( lcm<int_type>(-10, -10) == 10 );
BOOST_CHECK( lcm<int_type>( 25, -10) == 50 );
BOOST_CHECK( lcm<int_type>( 3, 7) == 21 );
BOOST_CHECK( lcm<int_type>( 8, 9) == 72 );
BOOST_CHECK( lcm<int_type>( 7, 49) == 49 );
BOOST_AUTO_TEST_SUITE_END()
cout << "Doing tests on unsigned-lcm." << endl;
BOOST_CHECK( lcm<uint_type>( 1u, 1u) == 1u );
BOOST_CHECK( lcm<uint_type>( 0u, 0u) == 0u );
BOOST_CHECK( lcm<uint_type>( 6u, 0u) == 0u );
BOOST_CHECK( lcm<uint_type>( 0u, 7u) == 0u );
BOOST_CHECK( lcm<uint_type>( 18u, 30u) == 90u );
BOOST_CHECK( lcm<uint_type>( 3u, 7u) == 21u );
BOOST_CHECK( lcm<uint_type>( 8u, 9u) == 72u );
BOOST_CHECK( lcm<uint_type>( 7u, 49u) == 49u );
// LCM tests
BOOST_AUTO_TEST_SUITE( lcm_test_suite )
cout << "Doing tests on static_lcm." << endl;
// LCM on built-in signed integer types
BOOST_AUTO_TEST_CASE_TEMPLATE( lcm_int_test, T, builtin_signed_test_types )
{
#ifndef BOOST_MSVC
using boost::math::lcm;
#else
using namespace boost::math;
#endif
// Originally from Boost.Rational tests
BOOST_CHECK_EQUAL( lcm<T>( 1, -1), static_cast<T>( 1) );
BOOST_CHECK_EQUAL( lcm<T>( -1, 1), static_cast<T>( 1) );
BOOST_CHECK_EQUAL( lcm<T>( 1, 1), static_cast<T>( 1) );
BOOST_CHECK_EQUAL( lcm<T>( -1, -1), static_cast<T>( 1) );
BOOST_CHECK_EQUAL( lcm<T>( 0, 0), static_cast<T>( 0) );
BOOST_CHECK_EQUAL( lcm<T>( 6, 0), static_cast<T>( 0) );
BOOST_CHECK_EQUAL( lcm<T>( 0, 7), static_cast<T>( 0) );
BOOST_CHECK_EQUAL( lcm<T>( -5, 0), static_cast<T>( 0) );
BOOST_CHECK_EQUAL( lcm<T>( 0, -4), static_cast<T>( 0) );
BOOST_CHECK_EQUAL( lcm<T>( 18, 30), static_cast<T>(90) );
BOOST_CHECK_EQUAL( lcm<T>( -6, 9), static_cast<T>(18) );
BOOST_CHECK_EQUAL( lcm<T>(-10, -10), static_cast<T>(10) );
BOOST_CHECK_EQUAL( lcm<T>( 25, -10), static_cast<T>(50) );
BOOST_CHECK_EQUAL( lcm<T>( 3, 7), static_cast<T>(21) );
BOOST_CHECK_EQUAL( lcm<T>( 8, 9), static_cast<T>(72) );
BOOST_CHECK_EQUAL( lcm<T>( 7, 49), static_cast<T>(49) );
}
// LCM on built-in unsigned integer types
BOOST_AUTO_TEST_CASE_TEMPLATE(lcm_unsigned_test, T, builtin_unsigned_test_types)
{
#ifndef BOOST_MSVC
using boost::math::lcm;
#else
using namespace boost::math;
#endif
BOOST_CHECK_EQUAL( lcm<T>( 1u, 1u), static_cast<T>( 1u) );
BOOST_CHECK_EQUAL( lcm<T>( 0u, 0u), static_cast<T>( 0u) );
BOOST_CHECK_EQUAL( lcm<T>( 6u, 0u), static_cast<T>( 0u) );
BOOST_CHECK_EQUAL( lcm<T>( 0u, 7u), static_cast<T>( 0u) );
BOOST_CHECK_EQUAL( lcm<T>( 18u, 30u), static_cast<T>(90u) );
BOOST_CHECK_EQUAL( lcm<T>( 3u, 7u), static_cast<T>(21u) );
BOOST_CHECK_EQUAL( lcm<T>( 8u, 9u), static_cast<T>(72u) );
BOOST_CHECK_EQUAL( lcm<T>( 7u, 49u), static_cast<T>(49u) );
}
// LCM at compile-time
BOOST_AUTO_TEST_CASE( lcm_static_test )
{
#ifndef BOOST_MSVC
using boost::math::static_lcm;
#else
using namespace boost::math;
#endif
// Can't use "BOOST_CHECK_EQUAL", otherwise the "value" member will be
// disqualified as compile-time-only constant, needing explicit definition
BOOST_CHECK( (static_lcm< 1, 1>::value) == 1 );
BOOST_CHECK( (static_lcm< 0, 0>::value) == 0 );
BOOST_CHECK( (static_lcm< 6, 0>::value) == 0 );
@ -133,6 +181,8 @@ test_main
BOOST_CHECK( (static_lcm< 3, 7>::value) == 21 );
BOOST_CHECK( (static_lcm< 8, 9>::value) == 72 );
BOOST_CHECK( (static_lcm< 7, 49>::value) == 49 );
return boost::exit_success;
}
// TODO: see GCD to-do
BOOST_AUTO_TEST_SUITE_END()