numeric_cast_test.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // boost utility cast test program -----------------------------------------//
  2. // (C) Copyright Beman Dawes, Dave Abrahams 1999. Distributed under the Boost
  3. // Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. // See http://www.boost.org for most recent version including documentation.
  6. // Revision History
  7. // 28 Set 04 taken from the old cast library (Fernando Cacciola)
  8. #include <iostream>
  9. #include <climits>
  10. #include <cfloat> // for DBL_MAX (Peter Schmid)
  11. #include <boost/numeric/conversion/cast.hpp>
  12. #include "boost/test/minimal.hpp"
  13. # if SCHAR_MAX == LONG_MAX
  14. # error "This test program doesn't work if SCHAR_MAX == LONG_MAX"
  15. # endif
  16. using namespace boost;
  17. using std::cout;
  18. int test_main( int argc, char * argv[] )
  19. {
  20. # ifdef NDEBUG
  21. cout << "NDEBUG is defined\n";
  22. # else
  23. cout << "NDEBUG is not defined\n";
  24. # endif
  25. cout << "\nBeginning tests...\n";
  26. // test implicit_cast and numeric_cast -------------------------------------//
  27. // tests which should succeed
  28. long small_value = 1;
  29. long small_negative_value = -1;
  30. long large_value = LONG_MAX;
  31. long large_negative_value = LONG_MIN;
  32. signed char c = 0;
  33. c = large_value; // see if compiler generates warning
  34. c = numeric_cast<signed char>( small_value );
  35. BOOST_CHECK( c == 1 );
  36. c = 0;
  37. c = numeric_cast<signed char>( small_value );
  38. BOOST_CHECK( c == 1 );
  39. c = 0;
  40. c = numeric_cast<signed char>( small_negative_value );
  41. BOOST_CHECK( c == -1 );
  42. // These tests courtesy of Joe R NWP Swatosh<joe.r.swatosh@usace.army.mil>
  43. BOOST_CHECK( 0.0f == numeric_cast<float>( 0.0 ) );
  44. BOOST_CHECK( 0.0 == numeric_cast<double>( 0.0 ) );
  45. // tests which should result in errors being detected
  46. bool caught_exception = false;
  47. try { c = numeric_cast<signed char>( large_value ); }
  48. catch ( numeric::bad_numeric_cast )
  49. { cout<<"caught bad_numeric_cast #1\n"; caught_exception = true; }
  50. BOOST_CHECK ( caught_exception );
  51. caught_exception = false;
  52. try { c = numeric_cast<signed char>( large_negative_value ); }
  53. catch ( numeric::bad_numeric_cast )
  54. { cout<<"caught bad_numeric_cast #2\n"; caught_exception = true; }
  55. BOOST_CHECK ( caught_exception );
  56. unsigned long ul;
  57. caught_exception = false;
  58. try { ul = numeric_cast<unsigned long>( large_negative_value ); }
  59. catch ( numeric::bad_numeric_cast )
  60. { cout<<"caught bad_numeric_cast #3\n"; caught_exception = true; }
  61. BOOST_CHECK ( caught_exception );
  62. caught_exception = false;
  63. try { ul = numeric_cast<unsigned long>( small_negative_value ); }
  64. catch ( numeric::bad_numeric_cast )
  65. { cout<<"caught bad_numeric_cast #4\n"; caught_exception = true; }
  66. BOOST_CHECK ( caught_exception );
  67. caught_exception = false;
  68. try { numeric_cast<int>( DBL_MAX ); }
  69. catch ( numeric::bad_numeric_cast )
  70. { cout<<"caught bad_numeric_cast #5\n"; caught_exception = true; }
  71. BOOST_CHECK ( caught_exception );
  72. return 0 ;
  73. } // main