assert_msg_test2.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //
  2. // assert_msg_test2.cpp - a test for BOOST_ASSERT_MSG and NDEBUG
  3. //
  4. // Copyright (c) 2014 Peter Dimov
  5. //
  6. // Distributed under the Boost Software License, Version 1.0.
  7. // See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt
  9. //
  10. #include <boost/detail/lightweight_test.hpp>
  11. #include <stdio.h>
  12. // default case, !NDEBUG
  13. // BOOST_ASSERT_MSG(x) -> assert(x)
  14. #undef NDEBUG
  15. #include <boost/assert.hpp>
  16. void test_default()
  17. {
  18. int x = 1;
  19. BOOST_ASSERT_MSG( 1, "msg" );
  20. BOOST_ASSERT_MSG( x, "msg" );
  21. BOOST_ASSERT_MSG( x == 1, "msg" );
  22. }
  23. // default case, NDEBUG
  24. // BOOST_ASSERT_MSG(x) -> assert(x)
  25. #define NDEBUG
  26. #include <boost/assert.hpp>
  27. void test_default_ndebug()
  28. {
  29. int x = 1;
  30. BOOST_ASSERT_MSG( 1, "msg" );
  31. BOOST_ASSERT_MSG( x, "msg" );
  32. BOOST_ASSERT_MSG( x == 1, "msg" );
  33. BOOST_ASSERT_MSG( 0, "msg" );
  34. BOOST_ASSERT_MSG( !x, "msg" );
  35. BOOST_ASSERT_MSG( x == 0, "msg" );
  36. }
  37. // BOOST_ENABLE_ASSERT_DEBUG_HANDLER, !NDEBUG
  38. // same as BOOST_ENABLE_ASSERT_HANDLER
  39. #define BOOST_ENABLE_ASSERT_DEBUG_HANDLER
  40. #undef NDEBUG
  41. #include <boost/assert.hpp>
  42. int handler_invoked = 0;
  43. void boost::assertion_failed_msg( char const * expr, char const * msg, char const * function, char const * file, long line )
  44. {
  45. printf( "Expression: %s\nMessage: %s\nFunction: %s\nFile: %s\nLine: %ld\n\n", expr, msg, function, file, line );
  46. ++handler_invoked;
  47. }
  48. void test_debug_handler()
  49. {
  50. handler_invoked = 0;
  51. int x = 1;
  52. BOOST_ASSERT_MSG( 1, "msg" );
  53. BOOST_ASSERT_MSG( x, "msg" );
  54. BOOST_ASSERT_MSG( x == 1, "msg" );
  55. BOOST_ASSERT_MSG( 0, "msg" );
  56. BOOST_ASSERT_MSG( !x, "msg" );
  57. BOOST_ASSERT_MSG( x == 0, "msg" );
  58. BOOST_TEST( handler_invoked == 3 );
  59. }
  60. // BOOST_ENABLE_ASSERT_DEBUG_HANDLER, NDEBUG
  61. // BOOST_ASSERT_MSG(x) -> ((void)0)
  62. #define NDEBUG
  63. #include <boost/assert.hpp>
  64. void test_debug_handler_ndebug()
  65. {
  66. handler_invoked = 0;
  67. int x = 1;
  68. BOOST_ASSERT_MSG( 1, "msg" );
  69. BOOST_ASSERT_MSG( x, "msg" );
  70. BOOST_ASSERT_MSG( x == 1, "msg" );
  71. BOOST_ASSERT_MSG( 0, "msg" );
  72. BOOST_ASSERT_MSG( !x, "msg" );
  73. BOOST_ASSERT_MSG( x == 0, "msg" );
  74. BOOST_TEST( handler_invoked == 0 );
  75. }
  76. #undef BOOST_ENABLE_ASSERT_DEBUG_HANDLER
  77. int main()
  78. {
  79. test_default();
  80. test_default_ndebug();
  81. test_debug_handler();
  82. test_debug_handler_ndebug();
  83. return boost::report_errors();
  84. }