check_integral_constant.hpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // (C) Copyright John Maddock 2000.
  2. // Use, modification and distribution are subject to the
  3. // Boost 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. #ifndef BOOST_CHECK_INTEGRAL_CONSTANT_HPP
  6. #define BOOST_CHECK_INTEGRAL_CONSTANT_HPP
  7. #include "test.hpp"
  8. namespace boost{
  9. namespace detail{
  10. /*
  11. macro:
  12. BOOST_CHECK_INTEGRAL_CONSTANT(expression, expected_value)
  13. expression: an integral constant expression to check.
  14. expected_value: the value expected.
  15. */
  16. template <long test_value>
  17. struct integral_constant
  18. {
  19. static long value() { return test_value; }
  20. };
  21. template <class T, class U>
  22. bool tt_compare(T found, U expected)
  23. { return static_cast<U>(found) == expected; }
  24. #define BOOST_CHECK_INTEGRAL_CONSTANT(expression, expected_value)\
  25. if(!::boost::detail::tt_compare(::boost::detail::integral_constant<(int)(expression)>::value(), (int)expression)){\
  26. BOOST_CHECK_MESSAGE(false, "The expression: \"" << BOOST_STRINGIZE(expression) << "\" had differing values depending upon whether it was used as an integral constant expression or not");\
  27. }else{\
  28. BOOST_CHECK_MESSAGE(true, "Validating Integral Constant Expression: \"" << BOOST_STRINGIZE(expression) << "\"");\
  29. }\
  30. if(!::boost::detail::tt_compare((int)expression, expected_value))\
  31. BOOST_CHECK_MESSAGE(false, "The expression: \"" << BOOST_STRINGIZE(expression) << "\" had an invalid value (found " << ::boost::detail::integral_constant<(int)(expression)>::value() << ", expected " << expected_value << ")" )
  32. /*
  33. macro:
  34. BOOST_CHECK_SOFT_INTEGRAL_CONSTANT(expression, expected_value, alternate_value)
  35. expression: an integral constant expression to check.
  36. expected_value: the value expected.
  37. alternate_value: an alternative value that results is a warning not a failure if found.
  38. */
  39. #define BOOST_CHECK_SOFT_INTEGRAL_CONSTANT(expression, expected_value, alternate_value)\
  40. if(!::boost::detail::tt_compare(::boost::detail::integral_constant<(int)(expression)>::value(), (int)expression)){\
  41. BOOST_CHECK_MESSAGE(false, "The expression: \"" << BOOST_STRINGIZE(expression) << "\" had differing values depending upon whether it was used as an integral constant expression or not");\
  42. }else{\
  43. BOOST_CHECK_MESSAGE(true, "Validating Integral Constant Expression: \"" << BOOST_STRINGIZE(expression) << "\"");\
  44. }\
  45. if(!::boost::detail::tt_compare((int)expression, expected_value))\
  46. {\
  47. if(!::boost::detail::tt_compare((int)expression, alternate_value))\
  48. {\
  49. BOOST_CHECK_MESSAGE(false, "The expression: \"" << BOOST_STRINGIZE(expression) << "\" had an invalid value (found " << ::boost::detail::integral_constant<(int)(expression)>::value() << ", expected " << expected_value << ")" );\
  50. }\
  51. else\
  52. {\
  53. BOOST_WARN_MESSAGE(false, "<note>The expression: \"" << BOOST_STRINGIZE(expression) << "\" did not have the value we wish it to have (found " << ::boost::detail::integral_constant<(int)(expression)>::value() << ", expected " << expected_value << ")</note>" );\
  54. }\
  55. }
  56. }//detail
  57. }//boost
  58. #endif