// Boost pow_test.cpp test file // Tests the pow function // (C) Copyright Bruno Lalande 2008. // 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) #include #include #include #include #include #include #include #include #include using namespace std; using namespace boost; using namespace boost::math; template void test_pow(T base) { if (!base && N < 0) { BOOST_CHECK_THROW(pow(base), std::overflow_error); } else { BOOST_CHECK_CLOSE((double)pow(base), ::pow(base, static_cast(N)), 0.001); } } template void test_with_big_bases() { for (T base = T(); base < T(1000); ++base) test_pow(base); } template void test_with_small_bases() { double base = 0.9; for (int i = 0; i < 10; ++i) { base += base/50; test_pow(base); } } template void test_with_small_exponents() { test_with_big_bases<0, T>(); test_with_big_bases(); test_with_big_bases(); test_with_big_bases(); test_with_big_bases(); test_with_big_bases(); test_with_big_bases(); test_with_big_bases(); test_with_big_bases(); test_with_big_bases(); test_with_big_bases(); test_with_big_bases(); } template void test_with_big_exponents() { test_with_small_bases(); test_with_small_bases(); test_with_small_bases(); test_with_small_bases(); test_with_small_bases(); test_with_small_bases(); test_with_small_bases(); test_with_small_bases(); test_with_small_bases(); test_with_small_bases(); } void test_return_types() { BOOST_STATIC_ASSERT((is_same('\1')), double>::value)); BOOST_STATIC_ASSERT((is_same(L'\2')), double>::value)); BOOST_STATIC_ASSERT((is_same(3)), double>::value)); BOOST_STATIC_ASSERT((is_same(4u)), double>::value)); BOOST_STATIC_ASSERT((is_same(5ul)), double>::value)); BOOST_STATIC_ASSERT((is_same(6.0f)), float>::value)); BOOST_STATIC_ASSERT((is_same(7.0)), double>::value)); BOOST_STATIC_ASSERT((is_same(7.0l)), long double>::value)); }; namespace boost { namespace math { namespace policies { template T user_overflow_error(const char*, const char*, const T&) { return 123.456; } }}} void test_error_policy() { using namespace policies; BOOST_CHECK(pow<-2>( 0.0, policy< ::boost::math::policies::overflow_error >() ) == 123.456); } int test_main(int, char* []) { cout << "Testing with integral bases and positive small exponents" << endl; test_with_small_exponents(); cout << "Testing with integral bases and negative small exponents" << endl; test_with_small_exponents(); cout << "Testing with double precision bases and positive small exponents" << endl; test_with_small_exponents(); cout << "Testing with double precision bases and negative small exponents" << endl; test_with_small_exponents(); cout << "Testing with positive big exponents" << endl; test_with_big_exponents<1>(); cout << "Testing with negative big exponents" << endl; test_with_big_exponents<-1>(); test_return_types(); test_error_policy(); return 0; }