test_helpers3.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright (C) 2003, Fernando Luis Cacciola Carballal.
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. //
  8. // NOTE: This file is intended to be used ONLY by the test files
  9. // from the Numeric Conversions Library
  10. //
  11. // The conversion test is performed using a class whose instances encapsulate
  12. // a particular specific conversion defnied explicitely.
  13. // A ConversionInstance object includes the source type, the target type,
  14. // the source value and the expected result, including possible exceptions.
  15. //
  16. enum PostCondition { c_converted, c_overflow, c_neg_overflow, c_pos_overflow } ;
  17. template<class Converter>
  18. struct ConversionInstance
  19. {
  20. typedef Converter converter ;
  21. typedef typename Converter::argument_type argument_type ;
  22. typedef typename Converter::result_type result_type ;
  23. typedef typename Converter::traits traits ;
  24. typedef typename traits::target_type target_type ;
  25. typedef typename traits::source_type source_type ;
  26. ConversionInstance ( result_type a_result, argument_type a_source, PostCondition a_post)
  27. :
  28. source(a_source),
  29. result(a_result),
  30. post(a_post)
  31. {}
  32. std::string to_string() const
  33. {
  34. return std::string("converter<")
  35. + typeid(target_type).name()
  36. + std::string(",")
  37. + typeid(source_type).name()
  38. + std::string(">::convert(") ;
  39. }
  40. argument_type source ;
  41. result_type result ;
  42. PostCondition post ;
  43. } ;
  44. //
  45. // Main conversion test point.
  46. // Exercises a specific conversion described by 'conv'.
  47. //
  48. template<class Instance>
  49. void test_conv_base( Instance const& conv )
  50. {
  51. typedef typename Instance::argument_type argument_type ;
  52. typedef typename Instance::result_type result_type ;
  53. typedef typename Instance::converter converter ;
  54. argument_type source = conv.source ;
  55. try
  56. {
  57. result_type result = converter::convert(source);
  58. if ( conv.post == c_converted )
  59. {
  60. BOOST_CHECK_MESSAGE( result == conv.result,
  61. conv.to_string() << printable(source) << ")= " << printable(result) << ". Expected:" << printable(conv.result)
  62. ) ;
  63. }
  64. else
  65. {
  66. BOOST_ERROR( conv.to_string() << printable(source) << ") = " << printable(result)
  67. << ". Expected:" << ( conv.post == c_neg_overflow ? " negative_overflow" : "positive_overflow" )
  68. ) ;
  69. }
  70. }
  71. catch ( boost::numeric::negative_overflow const& )
  72. {
  73. if ( conv.post == c_neg_overflow )
  74. {
  75. BOOST_CHECK_MESSAGE( true, conv.to_string() << printable(source) << ") = negative_overflow, as expected" ) ;
  76. }
  77. else
  78. {
  79. BOOST_ERROR( conv.to_string() << printable(source) << ") = negative_overflow. Expected:" << printable(conv.result) ) ;
  80. }
  81. }
  82. catch ( boost::numeric::positive_overflow const& )
  83. {
  84. if ( conv.post == c_pos_overflow )
  85. {
  86. BOOST_CHECK_MESSAGE( true, conv.to_string() << printable(source) << ") = positive_overflow, as expected" ) ;
  87. }
  88. else
  89. {
  90. BOOST_ERROR( conv.to_string() << printable(source) << ") = positive_overflow. Expected:" << printable(conv.result) ) ;
  91. }
  92. }
  93. catch ( boost::numeric::bad_numeric_cast const& )
  94. {
  95. if ( conv.post == c_overflow )
  96. {
  97. BOOST_CHECK_MESSAGE( true, conv.to_string() << printable(source) << ") = bad_numeric_cast, as expected" ) ;
  98. }
  99. else
  100. {
  101. BOOST_ERROR( conv.to_string() << printable(source) << ") = bad_numeric_cast. Expected:" << printable(conv.result) ) ;
  102. }
  103. }
  104. }
  105. #define TEST_SUCCEEDING_CONVERSION(Conv,typeT,typeS,valueT,valueS) \
  106. test_conv_base( ConversionInstance< Conv >(valueT, valueS, c_converted ) )
  107. #define TEST_POS_OVERFLOW_CONVERSION(Conv,typeT,typeS,valueS) \
  108. test_conv_base( ConversionInstance< Conv >( static_cast< typeT >(0), valueS, c_pos_overflow ) )
  109. #define TEST_NEG_OVERFLOW_CONVERSION(Conv,typeT,typeS,valueS) \
  110. test_conv_base( ConversionInstance< Conv >( static_cast< typeT >(0), valueS, c_neg_overflow ) )
  111. #define DEF_CONVERTER(T,S) boost::numeric::converter< T , S >
  112. #define TEST_SUCCEEDING_CONVERSION_DEF(typeT,typeS,valueT,valueS) \
  113. TEST_SUCCEEDING_CONVERSION( DEF_CONVERTER(typeT,typeS), typeT, typeS, valueT, valueS )
  114. #define TEST_POS_OVERFLOW_CONVERSION_DEF(typeT,typeS,valueS) \
  115. TEST_POS_OVERFLOW_CONVERSION( DEF_CONVERTER(typeT,typeS), typeT, typeS, valueS )
  116. #define TEST_NEG_OVERFLOW_CONVERSION_DEF(typeT,typeS,valueS) \
  117. TEST_NEG_OVERFLOW_CONVERSION( DEF_CONVERTER(typeT,typeS), typeT, typeS, valueS )
  118. //
  119. ///////////////////////////////////////////////////////////////////////////////////////////////