]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/assert/test/assert_msg_test2.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / assert / test / assert_msg_test2.cpp
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
20 void 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
35 void 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
48 // BOOST_ENABLE_ASSERT_DEBUG_HANDLER, !NDEBUG
49 // same as BOOST_ENABLE_ASSERT_HANDLER
50
51 #define BOOST_ENABLE_ASSERT_DEBUG_HANDLER
52
53 #undef NDEBUG
54 #include <boost/assert.hpp>
55
56 int handler_invoked = 0;
57
58 void boost::assertion_failed_msg( char const * expr, char const * msg, char const * function, char const * file, long line )
59 {
60 printf( "Expression: %s\nMessage: %s\nFunction: %s\nFile: %s\nLine: %ld\n\n", expr, msg, function, file, line );
61 ++handler_invoked;
62 }
63
64 void test_debug_handler()
65 {
66 handler_invoked = 0;
67
68 int x = 1;
69
70 BOOST_ASSERT_MSG( 1, "msg" );
71 BOOST_ASSERT_MSG( x, "msg" );
72 BOOST_ASSERT_MSG( x == 1, "msg" );
73
74 BOOST_ASSERT_MSG( 0, "msg" );
75 BOOST_ASSERT_MSG( !x, "msg" );
76 BOOST_ASSERT_MSG( x == 0, "msg" );
77
78 BOOST_TEST( handler_invoked == 3 );
79 }
80
81 // BOOST_ENABLE_ASSERT_DEBUG_HANDLER, NDEBUG
82 // BOOST_ASSERT_MSG(x) -> ((void)0)
83
84 #define NDEBUG
85 #include <boost/assert.hpp>
86
87 void test_debug_handler_ndebug()
88 {
89 handler_invoked = 0;
90
91 int x = 1;
92
93 BOOST_ASSERT_MSG( 1, "msg" );
94 BOOST_ASSERT_MSG( x, "msg" );
95 BOOST_ASSERT_MSG( x == 1, "msg" );
96
97 BOOST_ASSERT_MSG( 0, "msg" );
98 BOOST_ASSERT_MSG( !x, "msg" );
99 BOOST_ASSERT_MSG( x == 0, "msg" );
100
101 BOOST_TEST( handler_invoked == 0 );
102 }
103
104 #undef BOOST_ENABLE_ASSERT_DEBUG_HANDLER
105
106 int main()
107 {
108 test_default();
109 test_default_ndebug();
110 test_debug_handler();
111 test_debug_handler_ndebug();
112
113 return boost::report_errors();
114 }