log1p_expm1_test.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright John Maddock 2005.
  2. // Copyright Paul A. Bristow 2010
  3. // Use, modification and distribution are subject to the
  4. // Boost Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #include <boost/array.hpp>
  7. #include "functor.hpp"
  8. #include "handle_test_result.hpp"
  9. #include "table_type.hpp"
  10. template <class Real, class T>
  11. void do_test(const T& data, const char* type_name, const char* test_name)
  12. {
  13. typedef Real value_type;
  14. typedef value_type (*pg)(value_type);
  15. #ifdef LOG1P_FUNCTION_TO_TEST
  16. pg funcp = LOG1P_FUNCTION_TO_TEST;
  17. #elif defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
  18. pg funcp = &boost::math::log1p<value_type>;
  19. #else
  20. pg funcp = &boost::math::log1p;
  21. #endif
  22. boost::math::tools::test_result<value_type> result;
  23. std::cout << "Testing " << test_name << " with type " << type_name
  24. << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
  25. //
  26. // test log1p against data:
  27. //
  28. #if !(defined(ERROR_REPORTING_MODE) && !defined(LOG1P_FUNCTION_TO_TEST))
  29. result = boost::math::tools::test_hetero<Real>(
  30. data,
  31. bind_func<Real>(funcp, 0),
  32. extract_result<Real>(1));
  33. handle_test_result(result, data[result.worst()], result.worst(), type_name, "log1p", "Random test data");
  34. std::cout << std::endl;
  35. #endif
  36. #if !(defined(ERROR_REPORTING_MODE) && !defined(EXPM1_FUNCTION_TO_TEST))
  37. //
  38. // test expm1 against data:
  39. //
  40. #ifdef EXPM1_FUNCTION_TO_TEST
  41. funcp = EXPM1_FUNCTION_TO_TEST;
  42. #elif defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
  43. funcp = boost::math::expm1<value_type>;
  44. #else
  45. funcp = boost::math::expm1;
  46. #endif
  47. result = boost::math::tools::test_hetero<Real>(
  48. data,
  49. bind_func<Real>(funcp, 0),
  50. extract_result<Real>(2));
  51. handle_test_result(result, data[result.worst()], result.worst(), type_name, "expm1", "Random test data");
  52. std::cout << std::endl;
  53. #endif
  54. }
  55. template <class T>
  56. void test(T, const char* type_name)
  57. {
  58. # include "log1p_expm1_data.ipp"
  59. do_test<T>(log1p_expm1_data, type_name, "expm1 and log1p");
  60. //
  61. // C99 Appendix F special cases:
  62. static const T zero = 0;
  63. static const T m_one = -1;
  64. BOOST_CHECK_EQUAL(boost::math::log1p(zero), zero);
  65. BOOST_CHECK_EQUAL(boost::math::log1p(T(-zero)), zero);
  66. BOOST_CHECK_EQUAL(boost::math::expm1(zero), zero);
  67. if(std::numeric_limits<T>::has_infinity)
  68. {
  69. BOOST_CHECK_EQUAL(boost::math::log1p(m_one), -std::numeric_limits<T>::infinity());
  70. BOOST_CHECK_EQUAL(boost::math::expm1(T(-std::numeric_limits<T>::infinity())), m_one);
  71. BOOST_CHECK_EQUAL(boost::math::expm1(std::numeric_limits<T>::infinity()), std::numeric_limits<T>::infinity());
  72. #ifndef __BORLANDC__
  73. #ifndef BOOST_NO_EXCEPTIONS
  74. // When building with Borland's compiler, simply the *presence*
  75. // of these tests cause other unrelated tests to fail!!! :-(
  76. using namespace boost::math::policies;
  77. typedef policy<overflow_error<throw_on_error> > pol;
  78. BOOST_MATH_CHECK_THROW(boost::math::log1p(m_one, pol()), std::overflow_error);
  79. BOOST_MATH_CHECK_THROW(boost::math::expm1(std::numeric_limits<T>::infinity(), pol()), std::overflow_error);
  80. #endif
  81. #endif
  82. }
  83. }