summaryrefslogtreecommitdiff
path: root/libs/exception/test/throw_exception_test.cpp
blob: 37ecef9daf3a0a6e3d0bcad9769ecc30b76a3df9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//Copyright (c) 2006-2009 Emil Dotchevski and Reverge Studios, Inc.

//Distributed under the Boost Software License, Version 1.0. (See accompanying
//file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include "helper2.hpp"
#include <boost/exception/get_error_info.hpp>
#include <boost/exception/info.hpp>
#include <boost/exception_ptr.hpp>
#include <boost/detail/lightweight_test.hpp>

typedef boost::error_info<struct tag_test_int,int> test_data;

struct
exception1:
    std::exception
    {
    };

struct
exception2:
    std::exception,
    boost::exception
    {
    };

void
boost_throw_exception_test()
    {
    try
        {
        BOOST_THROW_EXCEPTION(exception1());
        BOOST_ERROR("BOOST_THROW_EXCEPTION failed to throw.");
        }
    catch(
    boost::exception & x )
        {
        char const * const * file=boost::get_error_info<boost::throw_function>(x);
        char const * const * function=boost::get_error_info<boost::throw_file>(x);
        int const * line=boost::get_error_info<boost::throw_line>(x);
        BOOST_TEST( file && *file );
        BOOST_TEST( function && *function );
        BOOST_TEST( line && *line==32 );
        }
    catch(
    ... )
        {
        BOOST_TEST(false);
        }
    try
        {
        BOOST_THROW_EXCEPTION(exception2() << test_data(42));
        BOOST_ERROR("BOOST_THROW_EXCEPTION failed to throw.");
        }
    catch(
    boost::exception & x )
        {
        char const * const * file=boost::get_error_info<boost::throw_function>(x);
        char const * const * function=boost::get_error_info<boost::throw_file>(x);
        int const * line=boost::get_error_info<boost::throw_line>(x);
        int const * data=boost::get_error_info<test_data>(x);
        BOOST_TEST( file && *file );
        BOOST_TEST( function && *function );
        BOOST_TEST( line && *line==52 );
        BOOST_TEST( data && *data==42 );
        }
    catch(
    ... )
        {
        BOOST_TEST(false);
        }
    }

void
throw_fwd( void (*thrower)(int) )
    {
    try
        {
        thrower(42);
        }
    catch(
    boost::exception & x )
        {
        x << test_data(42);
        throw;
        }
    }

template <class T>
void
tester()
    {
    try
        {
        throw_fwd( &boost::exception_test::throw_test_exception<T> );
        BOOST_ASSERT(false);
        }
    catch(
    ... )
        {
        boost::exception_ptr p = boost::current_exception();
        try
            {
            rethrow_exception(p);
            BOOST_TEST(false);
            }
        catch(
        T & y )
            {
#ifdef BOOST_NO_RTTI
            try
                {
                throw;
                }
            catch(
            boost::exception & y )
                {
#endif
                BOOST_TEST(boost::get_error_info<test_data>(y));
                if( int const * d=boost::get_error_info<test_data>(y) )
                    BOOST_TEST(*d==42);
#ifdef BOOST_NO_RTTI
                }
            catch(
            ... )
                {
                BOOST_TEST(false);
                }
#endif
            BOOST_TEST(y.x_==42);
            }
        catch(
        ... )
            {
            BOOST_TEST(false);
            }
        }
    }

int
main()
    {
    boost_throw_exception_test();
    tester<boost::exception_test::derives_boost_exception>();
    tester<boost::exception_test::derives_boost_exception_virtually>();
    tester<boost::exception_test::derives_std_exception>();
    return boost::report_errors();
    }