1 //
2 // assert_test2.cpp - a test for BOOST_ASSERT 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(x) -> assert(x)
16
17 #undef NDEBUG
18 #include <boost/assert.hpp>
19
test_default()20 void test_default()
21 {
22 int x = 1;
23
24 BOOST_ASSERT( 1 );
25 BOOST_ASSERT( x );
26 BOOST_ASSERT( x == 1 );
27 }
28
29 // default case, NDEBUG
30 // BOOST_ASSERT(x) -> assert(x)
31
32 #define NDEBUG
33 #include <boost/assert.hpp>
34
test_default_ndebug()35 void test_default_ndebug()
36 {
37 int x = 1;
38
39 BOOST_ASSERT( 1 );
40 BOOST_ASSERT( x );
41 BOOST_ASSERT( x == 1 );
42
43 BOOST_ASSERT( 0 );
44 BOOST_ASSERT( !x );
45 BOOST_ASSERT( x == 0 );
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
58 int handler_invoked = 0;
59
assertion_failed(char const * expr,char const * function,char const * file,long line)60 void boost::assertion_failed( char const * expr, char const * function, char const * file, long line )
61 {
62 printf( "Expression: %s\nFunction: %s\nFile: %s\nLine: %ld\n\n", expr, function, file, line );
63 ++handler_invoked;
64 }
65
test_debug_handler()66 void test_debug_handler()
67 {
68 handler_invoked = 0;
69
70 int x = 1;
71
72 BOOST_ASSERT( 1 );
73 BOOST_ASSERT( x );
74 BOOST_ASSERT( x == 1 );
75
76 BOOST_ASSERT( 0 );
77 BOOST_ASSERT( !x );
78 BOOST_ASSERT( x == 0 );
79
80 BOOST_TEST( handler_invoked == 3 );
81 }
82
83 // BOOST_ENABLE_ASSERT_DEBUG_HANDLER, NDEBUG
84 // BOOST_ASSERT(x) -> ((void)0)
85
86 #define NDEBUG
87 #include <boost/assert.hpp>
88
test_debug_handler_ndebug()89 void test_debug_handler_ndebug()
90 {
91 handler_invoked = 0;
92
93 int x = 1;
94
95 BOOST_ASSERT( 1 );
96 BOOST_ASSERT( x );
97 BOOST_ASSERT( x == 1 );
98
99 BOOST_ASSERT( 0 );
100 BOOST_ASSERT( !x );
101 BOOST_ASSERT( x == 0 );
102
103 BOOST_TEST( handler_invoked == 0 );
104
105 (void)x;
106 }
107
108 #undef BOOST_ENABLE_ASSERT_DEBUG_HANDLER
109
main()110 int 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