test_binomial_coeff.hpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright John Maddock 2006.
  2. // Copyright Paul A. Bristow 2007, 2009
  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/math/concepts/real_concept.hpp>
  7. #define BOOST_TEST_MAIN
  8. #include <boost/test/unit_test.hpp>
  9. #include <boost/test/tools/floating_point_comparison.hpp>
  10. #include <boost/math/special_functions/binomial.hpp>
  11. #include <boost/math/special_functions/trunc.hpp>
  12. #include <boost/math/tools/test.hpp>
  13. #include "functor.hpp"
  14. #include <boost/array.hpp>
  15. #include <iostream>
  16. #include <iomanip>
  17. #include "handle_test_result.hpp"
  18. #include "table_type.hpp"
  19. #ifndef SC_
  20. #define SC_(x) static_cast<typename table_type<T>::type>(BOOST_JOIN(x, L))
  21. #endif
  22. template <class T>
  23. T binomial_wrapper(T n, T k)
  24. {
  25. #ifdef BINOMIAL_FUNCTION_TO_TEST
  26. return BINOMIAL_FUNCTION_TO_TEST(
  27. boost::math::itrunc(n),
  28. boost::math::itrunc(k));
  29. #else
  30. return boost::math::binomial_coefficient<T>(
  31. boost::math::itrunc(n),
  32. boost::math::itrunc(k));
  33. #endif
  34. }
  35. template <class T>
  36. void test_binomial(T, const char* type_name)
  37. {
  38. #if !(defined(ERROR_REPORTING_MODE) && !defined(BINOMIAL_FUNCTION_TO_TEST))
  39. using namespace std;
  40. typedef T (*func_t)(T, T);
  41. #if defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
  42. func_t f = &binomial_wrapper<T>;
  43. #else
  44. func_t f = &binomial_wrapper;
  45. #endif
  46. #include "binomial_data.ipp"
  47. boost::math::tools::test_result<T> result = boost::math::tools::test_hetero<T>(
  48. binomial_data,
  49. bind_func<T>(f, 0, 1),
  50. extract_result<T>(2));
  51. std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
  52. "Test results for small arguments and type " << type_name << std::endl << std::endl;
  53. std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
  54. handle_test_result(result, binomial_data[result.worst()], result.worst(), type_name, "binomial_coefficient", "Binomials: small arguments");
  55. std::cout << std::endl;
  56. #include "binomial_large_data.ipp"
  57. result = boost::math::tools::test_hetero<T>(
  58. binomial_large_data,
  59. bind_func<T>(f, 0, 1),
  60. extract_result<T>(2));
  61. std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
  62. "Test results for large arguments and type " << type_name << std::endl << std::endl;
  63. std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
  64. handle_test_result(result, binomial_large_data[result.worst()], result.worst(), type_name, "binomial_coefficient", "Binomials: large arguments");
  65. std::cout << std::endl;
  66. #endif
  67. }