Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/utility/assert_test.cpp @ 14

Last change on this file since 14 was 12, checked in by landauf, 17 years ago

added boost

File size: 1.8 KB
Line 
1//
2//  assert_test.cpp - a test for boost/assert.hpp
3//
4//  Copyright (c) 2002 Peter Dimov and Multi Media Ltd.
5//
6// Distributed under the Boost Software License, Version 1.0. (See
7// 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
13#include <boost/assert.hpp>
14
15void test_default()
16{
17    int x = 1;
18
19    BOOST_ASSERT(1);
20    BOOST_ASSERT(x);
21    BOOST_ASSERT(x == 1);
22    BOOST_ASSERT(&x);
23}
24
25#define BOOST_DISABLE_ASSERTS
26#include <boost/assert.hpp>
27
28void test_disabled()
29{
30    int x = 1;
31
32    BOOST_ASSERT(1);
33    BOOST_ASSERT(x);
34    BOOST_ASSERT(x == 1);
35    BOOST_ASSERT(&x);
36
37    BOOST_ASSERT(0);
38    BOOST_ASSERT(!x);
39    BOOST_ASSERT(x == 0);
40
41    void * p = 0;
42
43    BOOST_ASSERT(p);
44
45    // supress warnings
46    p = &x;
47    p = &p;
48}
49
50#undef BOOST_DISABLE_ASSERTS
51
52#define BOOST_ENABLE_ASSERT_HANDLER
53#include <boost/assert.hpp>
54#include <boost/config.hpp>
55#include <cstdio>
56
57int handler_invoked = 0;
58
59void boost::assertion_failed(char const * expr, char const * function, char const * file, long line)
60{
61#if !defined(BOOST_NO_STDC_NAMESPACE)
62    using std::printf;
63#endif
64
65    printf("Expression: %s\nFunction: %s\nFile: %s\nLine: %ld\n\n", expr, function, file, line);
66    ++handler_invoked;
67}
68
69struct X
70{
71    static void f()
72    {
73        BOOST_ASSERT(0);
74    }
75};
76
77void test_handler()
78{
79    int x = 1;
80
81    BOOST_ASSERT(1);
82    BOOST_ASSERT(x);
83    BOOST_ASSERT(x == 1);
84    BOOST_ASSERT(&x);
85
86    BOOST_ASSERT(0);
87    BOOST_ASSERT(!x);
88    BOOST_ASSERT(x == 0);
89
90    void * p = 0;
91
92    BOOST_ASSERT(p);
93
94    X::f();
95
96    BOOST_ASSERT(handler_invoked == 5);
97    BOOST_TEST(handler_invoked == 5);
98}
99
100#undef BOOST_ENABLE_ASSERT_HANDLER
101
102int main()
103{
104    test_default();
105    test_disabled();
106    test_handler();
107
108    return boost::report_errors();
109}
Note: See TracBrowser for help on using the repository browser.