• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //  verify_test.cpp - a test for BOOST_VERIFY
3 //
4 //  Copyright (c) 2002 Peter Dimov and Multi Media Ltd.
5 //  Copyright (c) 2007 Peter Dimov
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 
f(int & x)20 int f( int & x )
21 {
22     return ++x;
23 }
24 
test_default()25 void test_default()
26 {
27     int x = 1;
28 
29     BOOST_VERIFY( 1 );
30     BOOST_VERIFY( x == 1 );
31     BOOST_VERIFY( ++x );
32     BOOST_VERIFY( f(x) );
33     BOOST_VERIFY( &x );
34 
35     BOOST_TEST( x == 3 );
36 }
37 
38 #define BOOST_DISABLE_ASSERTS
39 #include <boost/assert.hpp>
40 
test_disabled()41 void test_disabled()
42 {
43     int x = 1;
44 
45     BOOST_VERIFY( 1 );
46     BOOST_VERIFY( x == 1 );
47     BOOST_VERIFY( ++x );
48     BOOST_VERIFY( f(x) );
49     BOOST_VERIFY( &x );
50 
51     BOOST_TEST( x == 3 );
52 
53     BOOST_VERIFY( 0 );
54     BOOST_VERIFY( !x );
55     BOOST_VERIFY( x == 0 );
56     BOOST_VERIFY( !++x );
57     BOOST_VERIFY( !f(x) );
58 
59     BOOST_TEST( x == 5 );
60 
61     void * p = 0;
62     BOOST_VERIFY( p );
63 }
64 
65 #undef BOOST_DISABLE_ASSERTS
66 
67 #define BOOST_ENABLE_ASSERT_HANDLER
68 #include <boost/assert.hpp>
69 #include <boost/config.hpp>
70 #include <cstdio>
71 
72 int handler_invoked = 0;
73 
assertion_failed(char const * expr,char const * function,char const * file,long line)74 void boost::assertion_failed(char const * expr, char const * function, char const * file, long line)
75 {
76 #if !defined(BOOST_NO_STDC_NAMESPACE)
77     using std::printf;
78 #endif
79 
80     printf("Expression: %s\nFunction: %s\nFile: %s\nLine: %ld\n\n", expr, function, file, line);
81     ++handler_invoked;
82 }
83 
84 struct X
85 {
fX86     static bool f()
87     {
88         BOOST_VERIFY( 0 );
89         return false;
90     }
91 };
92 
test_handler()93 void test_handler()
94 {
95     int x = 1;
96 
97     BOOST_VERIFY( 1 );
98     BOOST_VERIFY( x == 1 );
99     BOOST_VERIFY( ++x );
100     BOOST_VERIFY( f(x) );
101     BOOST_VERIFY( &x );
102 
103     BOOST_TEST( x == 3 );
104 
105     BOOST_VERIFY( 0 );
106     BOOST_VERIFY( !x );
107     BOOST_VERIFY( x == 0 );
108     BOOST_VERIFY( !++x );
109     BOOST_VERIFY( !f(x) );
110 
111     BOOST_TEST( x == 5 );
112 
113     void * p = 0;
114     BOOST_VERIFY( p );
115 
116     BOOST_VERIFY( X::f() );
117 
118     BOOST_TEST( handler_invoked == 8 );
119 }
120 
121 #undef BOOST_ENABLE_ASSERT_HANDLER
122 
main()123 int main()
124 {
125     test_default();
126     test_disabled();
127     test_handler();
128 
129     return boost::report_errors();
130 }
131