Brian Silverman | ea861c1 | 2018-08-04 17:43:02 -0700 | [diff] [blame^] | 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 | |
| 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 | |
| 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 | |
| 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( char const * expr, char const * function, char const * file, long line ) |
| 59 | { |
| 60 | printf( "Expression: %s\nFunction: %s\nFile: %s\nLine: %ld\n\n", expr, 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( 1 ); |
| 71 | BOOST_ASSERT( x ); |
| 72 | BOOST_ASSERT( x == 1 ); |
| 73 | |
| 74 | BOOST_ASSERT( 0 ); |
| 75 | BOOST_ASSERT( !x ); |
| 76 | BOOST_ASSERT( x == 0 ); |
| 77 | |
| 78 | BOOST_TEST( handler_invoked == 3 ); |
| 79 | } |
| 80 | |
| 81 | // BOOST_ENABLE_ASSERT_DEBUG_HANDLER, NDEBUG |
| 82 | // BOOST_ASSERT(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( 1 ); |
| 94 | BOOST_ASSERT( x ); |
| 95 | BOOST_ASSERT( x == 1 ); |
| 96 | |
| 97 | BOOST_ASSERT( 0 ); |
| 98 | BOOST_ASSERT( !x ); |
| 99 | BOOST_ASSERT( x == 0 ); |
| 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 | } |