1 | // |
2 | // assert_test.cpp - a test for boost/assert.hpp |
3 | // |
4 | // Copyright (c) 2002 Peter Dimov and Multi Media Ltd. |
5 | // Copyright (c) Beman Dawes 2011 |
6 | // |
7 | // Distributed under the Boost Software License, Version 1.0. (See |
8 | // accompanying file LICENSE_1_0.txt or copy at |
9 | // http://www.boost.org/LICENSE_1_0.txt) |
10 | // |
11 | |
12 | #include <boost/core/lightweight_test.hpp> |
13 | |
14 | #if defined(__GNUC__) |
15 | # pragma GCC diagnostic ignored "-Waddress" |
16 | #endif |
17 | |
18 | #include <boost/assert.hpp> |
19 | |
20 | void test_default() |
21 | { |
22 | int x = 1; (void)x; |
23 | |
24 | BOOST_ASSERT(1); |
25 | BOOST_ASSERT(x); |
26 | BOOST_ASSERT(x == 1); |
27 | BOOST_ASSERT(&x); |
28 | |
29 | BOOST_ASSERT_MSG(1, "msg" ); |
30 | BOOST_ASSERT_MSG(x, "msg" ); |
31 | BOOST_ASSERT_MSG(x == 1, "msg" ); |
32 | BOOST_ASSERT_MSG(&x, "msg" ); |
33 | } |
34 | |
35 | #define BOOST_DISABLE_ASSERTS |
36 | #include <boost/assert.hpp> |
37 | |
38 | void test_disabled() |
39 | { |
40 | int x = 1; |
41 | |
42 | BOOST_ASSERT(1); |
43 | BOOST_ASSERT(x); |
44 | BOOST_ASSERT(x == 1); |
45 | BOOST_ASSERT(&x); |
46 | |
47 | BOOST_ASSERT_MSG(1, "msg" ); |
48 | BOOST_ASSERT_MSG(x, "msg" ); |
49 | BOOST_ASSERT_MSG(x == 1, "msg" ); |
50 | BOOST_ASSERT_MSG(&x, "msg" ); |
51 | |
52 | BOOST_ASSERT(0); |
53 | BOOST_ASSERT(!x); |
54 | BOOST_ASSERT(x == 0); |
55 | |
56 | BOOST_ASSERT_MSG(0, "msg" ); |
57 | BOOST_ASSERT_MSG(!x, "msg" ); |
58 | BOOST_ASSERT_MSG(x == 0, "msg" ); |
59 | |
60 | void * p = 0; |
61 | |
62 | BOOST_ASSERT(p); |
63 | BOOST_ASSERT_MSG(p, "msg" ); |
64 | |
65 | // suppress warnings |
66 | p = &x; |
67 | p = &p; |
68 | } |
69 | |
70 | #undef BOOST_DISABLE_ASSERTS |
71 | |
72 | #define BOOST_ENABLE_ASSERT_HANDLER |
73 | #include <boost/assert.hpp> |
74 | #include <boost/config.hpp> |
75 | #include <cstdio> |
76 | |
77 | int handler_invoked = 0; |
78 | int msg_handler_invoked = 0; |
79 | |
80 | void boost::assertion_failed(char const * expr, char const * function, char const * file, long line) |
81 | { |
82 | #if !defined(BOOST_NO_STDC_NAMESPACE) |
83 | using std::printf; |
84 | #endif |
85 | |
86 | printf(format: "Expression: %s\nFunction: %s\nFile: %s\nLine: %ld\n\n" , expr, function, file, line); |
87 | ++handler_invoked; |
88 | } |
89 | |
90 | void boost::assertion_failed_msg(char const * expr, char const * msg, char const * function, |
91 | char const * file, long line) |
92 | { |
93 | #if !defined(BOOST_NO_STDC_NAMESPACE) |
94 | using std::printf; |
95 | #endif |
96 | |
97 | printf(format: "Expression: %s Message: %s\nFunction: %s\nFile: %s\nLine: %ld\n\n" , |
98 | expr, msg, function, file, line); |
99 | ++msg_handler_invoked; |
100 | } |
101 | |
102 | struct X |
103 | { |
104 | static void f() |
105 | { |
106 | BOOST_ASSERT(0); |
107 | BOOST_ASSERT_MSG(0, "msg f()" ); |
108 | } |
109 | }; |
110 | |
111 | void test_handler() |
112 | { |
113 | int x = 1; |
114 | |
115 | BOOST_ASSERT(1); |
116 | BOOST_ASSERT(x); |
117 | BOOST_ASSERT(x == 1); |
118 | BOOST_ASSERT(&x); |
119 | |
120 | BOOST_ASSERT_MSG(1, "msg2" ); |
121 | BOOST_ASSERT_MSG(x, "msg3" ); |
122 | BOOST_ASSERT_MSG(x == 1, "msg4" ); |
123 | BOOST_ASSERT_MSG(&x, "msg5" ); |
124 | |
125 | BOOST_ASSERT(0); |
126 | BOOST_ASSERT(!x); |
127 | BOOST_ASSERT(x == 0); |
128 | |
129 | BOOST_ASSERT_MSG(0,"msg 0" ); |
130 | BOOST_ASSERT_MSG(!x, "msg !x" ); |
131 | BOOST_ASSERT_MSG(x == 0, "msg x == 0" ); |
132 | |
133 | void * p = 0; |
134 | |
135 | BOOST_ASSERT(p); |
136 | BOOST_ASSERT_MSG(p, "msg p" ); |
137 | |
138 | X::f(); |
139 | |
140 | BOOST_ASSERT(handler_invoked == 5); |
141 | BOOST_TEST(handler_invoked == 5); |
142 | |
143 | BOOST_ASSERT_MSG(msg_handler_invoked == 5, "msg_handler_invoked count is wrong" ); |
144 | BOOST_TEST(msg_handler_invoked == 5); |
145 | } |
146 | |
147 | #undef BOOST_ENABLE_ASSERT_HANDLER |
148 | #undef BOOST_ENABLE_ASSERT_MSG_HANDLER |
149 | |
150 | int main() |
151 | { |
152 | test_default(); |
153 | test_disabled(); |
154 | test_handler(); |
155 | |
156 | return boost::report_errors(); |
157 | } |
158 | |