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
11#include <boost/detail/lightweight_test.hpp>
12#include <stdio.h>
13
14// default case, !NDEBUG
15// BOOST_ASSERT_MSG(x) -> assert(x)
16
17#undef NDEBUG
18#include <boost/assert.hpp>
19
20void test_default()
21{
22 int x = 1;
23
24 BOOST_ASSERT_MSG( 1, "msg" );
25 BOOST_ASSERT_MSG( x, "msg" );
26 BOOST_ASSERT_MSG( x == 1, "msg" );
27}
28
29// default case, NDEBUG
30// BOOST_ASSERT_MSG(x) -> assert(x)
31
32#define NDEBUG
33#include <boost/assert.hpp>
34
35void test_default_ndebug()
36{
37 int x = 1;
38
39 BOOST_ASSERT_MSG( 1, "msg" );
40 BOOST_ASSERT_MSG( x, "msg" );
41 BOOST_ASSERT_MSG( x == 1, "msg" );
42
43 BOOST_ASSERT_MSG( 0, "msg" );
44 BOOST_ASSERT_MSG( !x, "msg" );
45 BOOST_ASSERT_MSG( x == 0, "msg" );
46
47 (void)x;
48}
49
50// BOOST_ENABLE_ASSERT_DEBUG_HANDLER, !NDEBUG
51// same as BOOST_ENABLE_ASSERT_HANDLER
52
53#define BOOST_ENABLE_ASSERT_DEBUG_HANDLER
54
55#undef NDEBUG
56#include <boost/assert.hpp>
57
58int handler_invoked = 0;
59
60void boost::assertion_failed_msg( char const * expr, char const * msg, char const * function, char const * file, long line )
61{
62 printf( format: "Expression: %s\nMessage: %s\nFunction: %s\nFile: %s\nLine: %ld\n\n", expr, msg, function, file, line );
63 ++handler_invoked;
64}
65
66void test_debug_handler()
67{
68 handler_invoked = 0;
69
70 int x = 1;
71
72 BOOST_ASSERT_MSG( 1, "msg" );
73 BOOST_ASSERT_MSG( x, "msg" );
74 BOOST_ASSERT_MSG( x == 1, "msg" );
75
76 BOOST_ASSERT_MSG( 0, "msg" );
77 BOOST_ASSERT_MSG( !x, "msg" );
78 BOOST_ASSERT_MSG( x == 0, "msg" );
79
80 BOOST_TEST( handler_invoked == 3 );
81}
82
83// BOOST_ENABLE_ASSERT_DEBUG_HANDLER, NDEBUG
84// BOOST_ASSERT_MSG(x) -> ((void)0)
85
86#define NDEBUG
87#include <boost/assert.hpp>
88
89void test_debug_handler_ndebug()
90{
91 handler_invoked = 0;
92
93 int x = 1;
94
95 BOOST_ASSERT_MSG( 1, "msg" );
96 BOOST_ASSERT_MSG( x, "msg" );
97 BOOST_ASSERT_MSG( x == 1, "msg" );
98
99 BOOST_ASSERT_MSG( 0, "msg" );
100 BOOST_ASSERT_MSG( !x, "msg" );
101 BOOST_ASSERT_MSG( x == 0, "msg" );
102
103 BOOST_TEST( handler_invoked == 0 );
104
105 (void)x;
106}
107
108#undef BOOST_ENABLE_ASSERT_DEBUG_HANDLER
109
110int main()
111{
112 test_default();
113 test_default_ndebug();
114 test_debug_handler();
115 test_debug_handler_ndebug();
116
117 return boost::report_errors();
118}
119

source code of boost/libs/assert/test/assert_msg_test2.cpp