// (C) Copyright John Maddock 2005. // Use, modification and distribution are subject to 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) #ifdef TEST_STD_HEADERS #include #else #include #endif #include #include #include #include #include #include #include #include #include #ifndef VERBOSE #undef BOOST_MESSAGE #define BOOST_MESSAGE(x) #endif // // This test verifies that the complex-algorithms that are // overloaded for scalar types produce the same result as casting // the argument to a complex type, and calling the complex version // of the algorithm. Relative errors must be within 2e in order for // the tests to pass. // template void check(const T& t, const U& u) { static const T two = 2; static const T factor = std::pow(two, 1-std::numeric_limits::digits) * 200; BOOST_STATIC_ASSERT((::boost::is_same::value)); BOOST_CHECK_CLOSE(t, u, factor); } template void check(const std::complex& t, const std::complex& u) { BOOST_STATIC_ASSERT((::boost::is_same::value)); check(t.real(), u.real()); check(t.imag(), u.imag()); } template void check_val(const T& val) { typedef typename boost::mpl::if_< boost::is_floating_point, T, double>::type real_type; typedef std::complex complex_type; real_type rval = static_cast(val); complex_type cval = rval; if(val) { std::cout << " Testing std::arg.\n"; check(std::arg(cval), std::arg(rval)); check(std::arg(cval), std::arg(val)); } std::cout << " Testing std::norm.\n"; check(std::norm(cval), std::norm(rval)); check(std::norm(cval), std::norm(val)); std::cout << " Testing std::conj.\n"; check(std::conj(cval), std::conj(rval)); check(std::conj(cval), std::conj(val)); std::cout << " Testing std::polar.\n"; check(std::polar(val), std::polar(rval)); check(std::polar(val, 0), std::polar(rval, 0)); check(std::polar(val, val), std::polar(rval, rval)); check(std::polar(val, rval), std::polar(rval, val)); std::cout << " Testing std::real.\n"; check(std::real(cval), std::real(rval)); check(std::real(cval), std::real(val)); std::cout << " Testing std::imaj.\n"; check(std::imag(cval), std::imag(rval)); check(std::imag(cval), std::imag(val)); if(val && !boost::is_floating_point::value) { // // Note that these tests are not run for floating point // types as that would only test the std lib vendor's // implementation of pow, not our additional overloads. // Note that some std lib's do fail these tests, gcc on // Darwin is a particularly bad example ! // std::cout << " Testing std::pow.\n"; check(std::pow(cval, cval), std::pow(cval, val)); check(std::pow(cval, cval), std::pow(cval, rval)); check(std::pow(cval, cval), std::pow(val, cval)); check(std::pow(cval, cval), std::pow(rval, cval)); } } void check(double i) { std::cout << "Checking type double with value " << i << std::endl; check_val(i); std::cout << "Checking type float with value " << i << std::endl; check_val(static_cast(i)); std::cout << "Checking type long double with value " << i << std::endl; check_val(static_cast(i)); } void check(int i) { std::cout << "Checking type char with value " << i << std::endl; check_val(static_cast(i)); std::cout << "Checking type unsigned char with value " << i << std::endl; check_val(static_cast(i)); std::cout << "Checking type signed char with value " << i << std::endl; check_val(static_cast(i)); std::cout << "Checking type short with value " << i << std::endl; check_val(static_cast(i)); std::cout << "Checking type unsigned short with value " << i << std::endl; check_val(static_cast(i)); std::cout << "Checking type int with value " << i << std::endl; check_val(static_cast(i)); std::cout << "Checking type unsigned int with value " << i << std::endl; check_val(static_cast(i)); std::cout << "Checking type long with value " << i << std::endl; check_val(static_cast(i)); std::cout << "Checking type unsigned long with value " << i << std::endl; check_val(static_cast(i)); #ifdef BOOST_HAS_LONG_LONG std::cout << "Checking type long long with value " << i << std::endl; check_val(static_cast(i)); std::cout << "Checking type unsigned long long with value " << i << std::endl; check_val(static_cast(i)); #elif defined(BOOST_HAS_MS_INT64) std::cout << "Checking type __int64 with value " << i << std::endl; check_val(static_cast<__int64>(i)); std::cout << "Checking type unsigned __int64 with value " << i << std::endl; check_val(static_cast(i)); #endif check(static_cast(i)); } int test_main(int, char*[]) { check(0); check(0.0); check(1); check(1.5); check(0.5); return 0; }