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