/** * \file libs/numeric/ublas/test/test_utils.hpp * * \brief Test suite for utils.hpp. * * Copyright (c) 2012, Marco Guazzone * * 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) * * \author Marco Guazzone (marco.guazzone@gmail.com) */ #include #include #include #include #include #include "utils.hpp" namespace ublas = boost::numeric::ublas; static const double tol(1e-6); static const double mul(tol*10); BOOST_UBLAS_TEST_DEF( check ) { BOOST_UBLAS_TEST_TRACE( "Test case: 'check'" ); BOOST_UBLAS_TEST_CHECK( true ); } BOOST_UBLAS_TEST_DEF( check_eq ) { BOOST_UBLAS_TEST_TRACE( "Test case: 'check_eq'" ); BOOST_UBLAS_DEBUG_TRACE( "-- Test against same types." ); BOOST_UBLAS_TEST_CHECK_EQ( short(1), short(1) ); BOOST_UBLAS_TEST_CHECK_EQ( int(1), int(1) ); BOOST_UBLAS_TEST_CHECK_EQ( long(1), long(1) ); BOOST_UBLAS_TEST_CHECK_EQ( unsigned(1), unsigned(1) ); BOOST_UBLAS_DEBUG_TRACE( "-- Test against different types." ); BOOST_UBLAS_TEST_CHECK_EQ( short(1), int(1) ); BOOST_UBLAS_TEST_CHECK_EQ( short(1), int(1) ); BOOST_UBLAS_TEST_CHECK_EQ( int(1), long(1) ); BOOST_UBLAS_TEST_CHECK_EQ( long(1), int(1) ); BOOST_UBLAS_DEBUG_TRACE( "-- Test aliases." ); BOOST_UBLAS_TEST_CHECK_EQUAL( int(1), int(1) ); } BOOST_UBLAS_TEST_DEF( check_close ) { BOOST_UBLAS_TEST_TRACE( "Test case: 'check_close'" ); const double c1(1*mul); const double c2(2*mul); // Check T vs. T BOOST_UBLAS_DEBUG_TRACE( "-- Test against same types." ); BOOST_UBLAS_TEST_CHECK_CLOSE( float(c1), float(c1), tol ); BOOST_UBLAS_TEST_CHECK_CLOSE( double(c1), double(c1), tol ); BOOST_UBLAS_TEST_CHECK_CLOSE( ::std::complex(c1,c2), ::std::complex(c1,c2), tol ); BOOST_UBLAS_TEST_CHECK_CLOSE( ::std::complex(c1,c2), ::std::complex(c1,c2), tol ); // Check T1 vs. T2 BOOST_UBLAS_DEBUG_TRACE( "-- Test against different types." ); BOOST_UBLAS_TEST_CHECK_CLOSE( float(c1), double(c1), tol ); BOOST_UBLAS_TEST_CHECK_CLOSE( double(c1), float(c1), tol ); BOOST_UBLAS_TEST_CHECK_CLOSE( ::std::complex(c1,c2), ::std::complex(c1,c2), tol ); BOOST_UBLAS_TEST_CHECK_CLOSE( ::std::complex(c1,c2), ::std::complex(c1,c2), tol ); // Check alias BOOST_UBLAS_DEBUG_TRACE( "-- Test aliases." ); BOOST_UBLAS_TEST_CHECK_PRECISION( float(c1), float(c1), tol ); } BOOST_UBLAS_TEST_DEF( check_rel_close ) { BOOST_UBLAS_TEST_TRACE( "Test case: 'check_rel_close'" ); const double c1(1*mul); const double c2(2*mul); // Check T vs. T BOOST_UBLAS_DEBUG_TRACE( "-- Test against same types." ); BOOST_UBLAS_TEST_CHECK_REL_CLOSE( float(c1), float(c1), tol ); BOOST_UBLAS_TEST_CHECK_REL_CLOSE( double(c1), double(c1), tol ); BOOST_UBLAS_TEST_CHECK_REL_CLOSE( ::std::complex(c1,c2), ::std::complex(c1,c2), tol ); BOOST_UBLAS_TEST_CHECK_REL_CLOSE( ::std::complex(c1,c2), ::std::complex(c1,c2), tol ); // Check T1 vs. T2 BOOST_UBLAS_DEBUG_TRACE( "-- Test against different types." ); BOOST_UBLAS_TEST_CHECK_REL_CLOSE( float(c1), double(c1), tol ); BOOST_UBLAS_TEST_CHECK_REL_CLOSE( double(c1), float(c1), tol ); BOOST_UBLAS_TEST_CHECK_REL_CLOSE( ::std::complex(c1,c2), ::std::complex(c1,c2), tol ); BOOST_UBLAS_TEST_CHECK_REL_CLOSE( ::std::complex(c1,c2), ::std::complex(c1,c2), tol ); // Check alias BOOST_UBLAS_DEBUG_TRACE( "-- Test aliases." ); BOOST_UBLAS_TEST_CHECK_REL_PRECISION( float(c1), float(c1), tol ); } BOOST_UBLAS_TEST_DEF( check_vector_eq ) { BOOST_UBLAS_TEST_TRACE( "Test case: 'check_vector_eq'" ); const ::std::size_t n(5); ublas::vector sv = ublas::scalar_vector(n, 1); ublas::vector iv = ublas::scalar_vector(n, 1); ublas::vector lv = ublas::scalar_vector(n, 1L); ublas::vector uv = ublas::scalar_vector(n, 1u); // Check T vs. T BOOST_UBLAS_DEBUG_TRACE( "-- Test against same types." ); BOOST_UBLAS_TEST_CHECK_VECTOR_EQ( sv, sv, n ); BOOST_UBLAS_TEST_CHECK_VECTOR_EQ( iv, iv, n ); BOOST_UBLAS_TEST_CHECK_VECTOR_EQ( lv, lv, n ); BOOST_UBLAS_TEST_CHECK_VECTOR_EQ( uv, uv, n ); // Check T1 vs. T2 BOOST_UBLAS_DEBUG_TRACE( "-- Test against different types." ); BOOST_UBLAS_TEST_CHECK_VECTOR_EQ( sv, iv, n ); BOOST_UBLAS_TEST_CHECK_VECTOR_EQ( iv, sv, n ); BOOST_UBLAS_TEST_CHECK_VECTOR_EQ( iv, lv, n ); BOOST_UBLAS_TEST_CHECK_VECTOR_EQ( lv, iv, n ); } BOOST_UBLAS_TEST_DEF( check_vector_close ) { BOOST_UBLAS_TEST_TRACE( "Test case: 'check_vector_close'" ); const ::std::size_t n(5); ublas::vector fv = ublas::scalar_vector(n, 1); ublas::vector dv = ublas::scalar_vector(n, 1); ublas::vector< ::std::complex > cfv = ublas::scalar_vector< ::std::complex >(n, ::std::complex(1,2)); ublas::vector< ::std::complex > cdv = ublas::scalar_vector< ::std::complex >(n, ::std::complex(1,2)); // Check T vs. T BOOST_UBLAS_DEBUG_TRACE( "-- Test against same types." ); BOOST_UBLAS_TEST_CHECK_VECTOR_CLOSE( fv, fv, n, tol ); BOOST_UBLAS_TEST_CHECK_VECTOR_CLOSE( dv, dv, n, tol ); BOOST_UBLAS_TEST_CHECK_VECTOR_CLOSE( cfv, cfv, n, tol ); BOOST_UBLAS_TEST_CHECK_VECTOR_CLOSE( cdv, cdv, n, tol ); // Check T1 vs. T2 BOOST_UBLAS_DEBUG_TRACE( "-- Test against different types." ); BOOST_UBLAS_TEST_CHECK_VECTOR_CLOSE( fv, dv, n, tol ); BOOST_UBLAS_TEST_CHECK_VECTOR_CLOSE( dv, fv, n, tol ); BOOST_UBLAS_TEST_CHECK_VECTOR_CLOSE( cfv, cdv, n, tol ); BOOST_UBLAS_TEST_CHECK_VECTOR_CLOSE( cdv, cfv, n, tol ); } BOOST_UBLAS_TEST_DEF( check_vector_rel_close ) { BOOST_UBLAS_TEST_TRACE( "Test case: 'check_vector_rel_close'" ); const ::std::size_t n(5); const double c1(1*mul); const double c2(2*mul); ublas::vector fv = ublas::scalar_vector(n, c1); ublas::vector dv = ublas::scalar_vector(n, c1); ublas::vector< ::std::complex > cfv = ublas::scalar_vector< ::std::complex >(n, ::std::complex(c1,c2)); ublas::vector< ::std::complex > cdv = ublas::scalar_vector< ::std::complex >(n, ::std::complex(c1,c2)); // Check T vs. T BOOST_UBLAS_DEBUG_TRACE( "-- Test against same types." ); BOOST_UBLAS_TEST_CHECK_VECTOR_REL_CLOSE( fv, fv, n, tol ); BOOST_UBLAS_TEST_CHECK_VECTOR_REL_CLOSE( dv, dv, n, tol ); BOOST_UBLAS_TEST_CHECK_VECTOR_REL_CLOSE( cfv, cfv, n, tol ); BOOST_UBLAS_TEST_CHECK_VECTOR_REL_CLOSE( cdv, cdv, n, tol ); // Check T1 vs. T2 BOOST_UBLAS_DEBUG_TRACE( "-- Test against different types." ); BOOST_UBLAS_TEST_CHECK_VECTOR_REL_CLOSE( fv, dv, n, tol ); BOOST_UBLAS_TEST_CHECK_VECTOR_REL_CLOSE( dv, fv, n, tol ); BOOST_UBLAS_TEST_CHECK_VECTOR_REL_CLOSE( cfv, cdv, n, tol ); BOOST_UBLAS_TEST_CHECK_VECTOR_REL_CLOSE( cdv, cfv, n, tol ); } BOOST_UBLAS_TEST_DEF( check_matrix_eq ) { BOOST_UBLAS_TEST_TRACE( "Test case: 'check_matrix_eq'" ); const ::std::size_t nr(3); const ::std::size_t nc(4); ublas::matrix sv = ublas::scalar_matrix(nr, nc, 1); ublas::matrix iv = ublas::scalar_matrix(nr, nc, 1); ublas::matrix lv = ublas::scalar_matrix(nr, nc, 1L); ublas::matrix uv = ublas::scalar_matrix(nr, nc, 1u); // Check T vs. T BOOST_UBLAS_DEBUG_TRACE( "-- Test against same types." ); BOOST_UBLAS_TEST_CHECK_MATRIX_EQ( sv, sv, nr, nc ); BOOST_UBLAS_TEST_CHECK_MATRIX_EQ( iv, iv, nr, nc ); BOOST_UBLAS_TEST_CHECK_MATRIX_EQ( lv, lv, nr, nc ); BOOST_UBLAS_TEST_CHECK_MATRIX_EQ( uv, uv, nr, nc ); // Check T1 vs. T2 BOOST_UBLAS_DEBUG_TRACE( "-- Test against different types." ); BOOST_UBLAS_TEST_CHECK_MATRIX_EQ( sv, iv, nr, nc ); BOOST_UBLAS_TEST_CHECK_MATRIX_EQ( iv, sv, nr, nc ); BOOST_UBLAS_TEST_CHECK_MATRIX_EQ( iv, lv, nr, nc ); BOOST_UBLAS_TEST_CHECK_MATRIX_EQ( lv, iv, nr, nc ); } BOOST_UBLAS_TEST_DEF( check_matrix_close ) { BOOST_UBLAS_TEST_TRACE( "Test case: 'check_matrix_close'" ); const ::std::size_t nr(3); const ::std::size_t nc(4); const double c1(1*mul); const double c2(2*mul); ublas::matrix fA = ublas::scalar_matrix(nr, nc, c1); ublas::matrix dA = ublas::scalar_matrix(nr, nc, c1); ublas::matrix< ::std::complex > cfA = ublas::scalar_matrix< ::std::complex >(nr, nc, ::std::complex(c1,c2)); ublas::matrix< ::std::complex > cdA = ublas::scalar_matrix< ::std::complex >(nr, nc, ::std::complex(c1,c2)); // Check T vs. T BOOST_UBLAS_DEBUG_TRACE( "-- Test against same types." ); BOOST_UBLAS_TEST_CHECK_MATRIX_CLOSE( fA, fA, nr, nc, tol ); BOOST_UBLAS_TEST_CHECK_MATRIX_CLOSE( dA, dA, nr, nc, tol ); BOOST_UBLAS_TEST_CHECK_MATRIX_CLOSE( cfA, cfA, nr, nc, tol ); BOOST_UBLAS_TEST_CHECK_MATRIX_CLOSE( cdA, cdA, nr, nc, tol ); // Check T1 vs. T2 BOOST_UBLAS_DEBUG_TRACE( "-- Test against different types." ); BOOST_UBLAS_TEST_CHECK_MATRIX_CLOSE( fA, dA, nr, nc, tol ); BOOST_UBLAS_TEST_CHECK_MATRIX_CLOSE( dA, fA, nr, nc, tol ); BOOST_UBLAS_TEST_CHECK_MATRIX_CLOSE( cfA, cdA, nr, nc, tol ); BOOST_UBLAS_TEST_CHECK_MATRIX_CLOSE( cdA, cfA, nr, nc, tol ); } BOOST_UBLAS_TEST_DEF( check_matrix_rel_close ) { BOOST_UBLAS_TEST_TRACE( "Test case: 'check_matrix_rel_close'" ); const ::std::size_t nr(3); const ::std::size_t nc(4); const double c1(1*mul); const double c2(2*mul); ublas::matrix fA = ublas::scalar_matrix(nr, nc, c1); ublas::matrix dA = ublas::scalar_matrix(nr, nc, c1); ublas::matrix< ::std::complex > cfA = ublas::scalar_matrix< ::std::complex >(nr, nc, ::std::complex(c1,c2)); ublas::matrix< ::std::complex > cdA = ublas::scalar_matrix< ::std::complex >(nr, nc, ::std::complex(c1,c2)); // Check T vs. T BOOST_UBLAS_DEBUG_TRACE( "-- Test against same types." ); BOOST_UBLAS_TEST_CHECK_MATRIX_REL_CLOSE( fA, fA, nr, nc, tol ); BOOST_UBLAS_TEST_CHECK_MATRIX_REL_CLOSE( dA, dA, nr, nc, tol ); BOOST_UBLAS_TEST_CHECK_MATRIX_REL_CLOSE( cfA, cfA, nr, nc, tol ); BOOST_UBLAS_TEST_CHECK_MATRIX_REL_CLOSE( cdA, cdA, nr, nc, tol ); // Check T1 vs. T2 BOOST_UBLAS_DEBUG_TRACE( "-- Test against different types." ); BOOST_UBLAS_TEST_CHECK_MATRIX_REL_CLOSE( fA, dA, nr, nc, tol ); BOOST_UBLAS_TEST_CHECK_MATRIX_REL_CLOSE( dA, fA, nr, nc, tol ); BOOST_UBLAS_TEST_CHECK_MATRIX_REL_CLOSE( cfA, cdA, nr, nc, tol ); BOOST_UBLAS_TEST_CHECK_MATRIX_REL_CLOSE( cdA, cfA, nr, nc, tol ); } int main() { BOOST_UBLAS_TEST_SUITE( "Test 'utils.hpp' functionalities" ); BOOST_UBLAS_TEST_BEGIN(); BOOST_UBLAS_TEST_DO( check ); BOOST_UBLAS_TEST_DO( check_eq ); BOOST_UBLAS_TEST_DO( check_close ); BOOST_UBLAS_TEST_DO( check_rel_close ); BOOST_UBLAS_TEST_DO( check_vector_eq ); BOOST_UBLAS_TEST_DO( check_vector_close ); BOOST_UBLAS_TEST_DO( check_vector_rel_close ); BOOST_UBLAS_TEST_DO( check_matrix_eq ); BOOST_UBLAS_TEST_DO( check_matrix_close ); BOOST_UBLAS_TEST_DO( check_matrix_rel_close ); BOOST_UBLAS_TEST_END(); }