bounds_test.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // (c) Copyright Fernando Luis Cacciola Carballal 2000-2004
  2. // Use, modification, and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // See library home page at http://www.boost.org/libs/numeric/conversion
  6. //
  7. // Contact the author at: fernando_cacciola@hotmail.com
  8. //
  9. #include<typeinfo>
  10. #include<iostream>
  11. #include<iomanip>
  12. #include "boost/numeric/conversion/bounds.hpp"
  13. #ifdef __BORLANDC__
  14. #pragma hdrstop
  15. #endif
  16. #include "test_helpers.cpp"
  17. using namespace std ;
  18. using namespace boost ;
  19. using namespace numeric ;
  20. // Test the fields of boost::numeric::bounds<> against the expected values.
  21. //
  22. template<class T>
  23. void test_bounds( T expected_lowest, T expected_highest, T expected_smallest )
  24. {
  25. T lowest = bounds<T>::lowest () ;
  26. T highest = bounds<T>::highest () ;
  27. T smallest = bounds<T>::smallest() ;
  28. BOOST_CHECK_MESSAGE ( lowest == expected_lowest,
  29. "bounds<" << typeid(T).name() << ">::lowest() = " << printable(lowest) << ". Expected " << printable(expected_lowest)
  30. ) ;
  31. BOOST_CHECK_MESSAGE ( highest == expected_highest,
  32. "bounds<" << typeid(T).name() << ">::highest() = " << printable(highest) << ". Expected " << printable(expected_highest)
  33. ) ;
  34. BOOST_CHECK_MESSAGE ( smallest == expected_smallest,
  35. "bounds<" << typeid(T).name() << ">::smallest() = " << printable(smallest) << ". Expected " << printable(expected_smallest)
  36. ) ;
  37. }
  38. template<class T>
  39. void test_bounds_integer( MATCH_FNTPL_ARG(T) )
  40. {
  41. test_bounds( numeric_limits<T>::min BOOST_PREVENT_MACRO_SUBSTITUTION()
  42. , numeric_limits<T>::max BOOST_PREVENT_MACRO_SUBSTITUTION()
  43. , static_cast<T>(1)
  44. ) ;
  45. }
  46. template<class T>
  47. void test_bounds_float( MATCH_FNTPL_ARG(T))
  48. {
  49. test_bounds( -numeric_limits<T>::max BOOST_PREVENT_MACRO_SUBSTITUTION ()
  50. , numeric_limits<T>::max BOOST_PREVENT_MACRO_SUBSTITUTION ()
  51. , numeric_limits<T>::min BOOST_PREVENT_MACRO_SUBSTITUTION ()
  52. ) ;
  53. }
  54. void test_bounds_integers()
  55. {
  56. test_bounds_integer( SET_FNTPL_ARG(unsigned char) ) ;
  57. test_bounds_integer( SET_FNTPL_ARG(signed char) ) ;
  58. test_bounds_integer( SET_FNTPL_ARG(char) ) ;
  59. test_bounds_integer( SET_FNTPL_ARG(unsigned short) ) ;
  60. test_bounds_integer( SET_FNTPL_ARG(short) ) ;
  61. test_bounds_integer( SET_FNTPL_ARG(unsigned int) ) ;
  62. test_bounds_integer( SET_FNTPL_ARG(int) ) ;
  63. test_bounds_integer( SET_FNTPL_ARG(unsigned long) ) ;
  64. test_bounds_integer( SET_FNTPL_ARG(long) ) ;
  65. }
  66. void test_bounds_floats()
  67. {
  68. test_bounds_float( SET_FNTPL_ARG(float) );
  69. test_bounds_float( SET_FNTPL_ARG(double) );
  70. test_bounds_float( SET_FNTPL_ARG(long double) );
  71. }
  72. void test_bounds()
  73. {
  74. test_bounds_integers() ;
  75. test_bounds_floats () ;
  76. }
  77. int test_main( int, char * [] )
  78. {
  79. cout << setprecision( std::numeric_limits<long double>::digits10 ) ;
  80. test_bounds();
  81. return 0;
  82. }