File: diagnostic_information_test.cpp

package info (click to toggle)
boost1.42 1.42.0-4
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 277,864 kB
  • ctags: 401,076
  • sloc: cpp: 1,235,659; xml: 74,142; ansic: 41,313; python: 26,756; sh: 11,840; cs: 2,118; makefile: 655; perl: 494; yacc: 456; asm: 353; csh: 6
file content (238 lines) | stat: -rw-r--r-- 6,237 bytes parent folder | download
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//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 <boost/exception/diagnostic_information.hpp>
#include <boost/exception/info.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <boost/detail/workaround.hpp>

#if BOOST_WORKAROUND(__CODEGEARC__, BOOST_TESTED_AT(0x610))
struct test_tag1 {};
struct test_tag2 {};
#endif

typedef boost::error_info<struct test_tag1,int> tagged_int1;
typedef boost::error_info<struct test_tag2,int> tagged_int2;

std::string
to_string( tagged_int2 const & x )
    {
    if( x.value()==42 )
        return "fourty-two";
    else
        return "bad value";
    }

struct
error1:
    std::exception,
    boost::exception
    {
    char const *
    what() const throw()
        {
        return "error1";
        }
    };

struct
error2:
    boost::exception
    {
    };

struct
error3:
    std::exception
    {
    char const *
    what() const throw()
        {
        return "error3";
        }
    };

struct
error4:
    std::exception,
    boost::exception
    {
    char const *
    what() const throw()
        {
        return diagnostic_information_what(*this);
        }
    };

void
test1( std::string const & di1, std::string const & di2 )
    {
    BOOST_TEST( di1!=di2 );
#ifndef BOOST_NO_RTTI
    BOOST_TEST(di1.find("type:")!=std::string::npos);
    BOOST_TEST(di1.find("error1")!=std::string::npos);
#endif
    BOOST_TEST(di1.find("test_tag1")!=std::string::npos);
    BOOST_TEST(di1.find("test_tag2")!=std::string::npos);
    BOOST_TEST(di1.find("fourty-two")!=std::string::npos);
#ifndef BOOST_NO_RTTI
    BOOST_TEST(di2.find("type:")!=std::string::npos);
    BOOST_TEST(di2.find("error1")!=std::string::npos);
#endif
    BOOST_TEST(di2.find("test_tag1")!=std::string::npos);
    BOOST_TEST(di2.find("test_tag2")!=std::string::npos);
    BOOST_TEST(di2.find("bad value")!=std::string::npos);
    }

void
test2( std::string const & di1, std::string const & di2 )
    {
    BOOST_TEST( di1!=di2 );
    BOOST_TEST(di1.find("test_tag1")!=std::string::npos);
    BOOST_TEST(di1.find("test_tag2")!=std::string::npos);
    BOOST_TEST(di1.find("fourty-two")!=std::string::npos);
    BOOST_TEST(di2.find("test_tag1")!=std::string::npos);
    BOOST_TEST(di2.find("test_tag2")!=std::string::npos);
    BOOST_TEST(di2.find("bad value")!=std::string::npos);
    }

void
test3( std::string const & di )
    {
#ifndef BOOST_NO_RTTI
    BOOST_TEST(di.find("type:")!=std::string::npos);
#endif
    BOOST_TEST(di.find("error3")!=std::string::npos);
    }

void
test4( std::string const & di1, std::string const & di2 )
    {
    BOOST_TEST( di1!=di2 );
#ifndef BOOST_NO_RTTI
    BOOST_TEST(di1.find("type:")!=std::string::npos);
#endif
    BOOST_TEST(di1.find("test_tag1")!=std::string::npos);
    BOOST_TEST(di1.find("test_tag2")!=std::string::npos);
    BOOST_TEST(di1.find("fourty-two")!=std::string::npos);
#ifndef BOOST_NO_RTTI
    BOOST_TEST(di2.find("type:")!=std::string::npos);
#endif
    BOOST_TEST(di2.find("test_tag1")!=std::string::npos);
    BOOST_TEST(di2.find("test_tag2")!=std::string::npos);
    BOOST_TEST(di2.find("bad value")!=std::string::npos);
    }

int
main()
    {
    using namespace boost;
    try
        {
        error1 x; x << tagged_int1(42) << tagged_int2(42);
        BOOST_TEST(x.what()==std::string("error1"));
        throw x;
        }
    catch(
    error1 & x )
        {
        std::string di1=boost::diagnostic_information(x);
        x << tagged_int1(2) << tagged_int2(2);
        std::string di2 = diagnostic_information(x);
        test1(di1,di2);
        }
    try
        {
        error1 x; x << tagged_int1(42) << tagged_int2(42);
        BOOST_TEST(x.what()==std::string("error1"));
        throw x;
        }
    catch(
    error1 & x )
        {
        std::string di1=boost::current_exception_diagnostic_information();
        x << tagged_int1(2) << tagged_int2(2);
        std::string di2 = current_exception_diagnostic_information();
        test1(di1,di2);
        }
    try
        {
        error2 x;
        x << tagged_int1(42) << tagged_int2(42);
        throw x;
        }
    catch(
    error2 & x )
        {
        std::string di1 = diagnostic_information(x);
        x << tagged_int1(2) << tagged_int2(2);
        std::string di2 = diagnostic_information(x);
        test2(di1,di2);
        }
    try
        {
        error2 x;
        x << tagged_int1(42) << tagged_int2(42);
        throw x;
        }
    catch(
    error2 & x )
        {
        std::string di1 = current_exception_diagnostic_information();
        BOOST_TEST(di1==boost::diagnostic_information_what(x));
        x << tagged_int1(2) << tagged_int2(2);
        std::string di2 = current_exception_diagnostic_information();
        BOOST_TEST(di2==boost::diagnostic_information_what(x));
        test2(di1,di2);
        }
    try
        {
        error3 x;
        std::string di=diagnostic_information(x);
        test3(di);
        throw x;
        }
    catch(
    ... )
        {
        std::string di=current_exception_diagnostic_information();
        test3(di);
        }
    try
        {
        error4 x; x << tagged_int1(42) << tagged_int2(42);
        throw x;
        }
    catch(
    error4 & x )
        {
        std::string di1=boost::diagnostic_information(x);
        std::string wh1=x.what();
        BOOST_TEST(wh1==di1);
        x << tagged_int1(2) << tagged_int2(2);
        std::string di2 = diagnostic_information(x);
        std::string wh2=x.what();
        BOOST_TEST(wh2==di2);
        test4(di1,di2);
        }
    try
        {
        error4 x; x << tagged_int1(42) << tagged_int2(42);
        throw x;
        }
    catch(
    error4 & x )
        {
        std::string di1=boost::current_exception_diagnostic_information();
        std::string wh1=x.what();
        BOOST_TEST(wh1==di1);
        x << tagged_int1(2) << tagged_int2(2);
        std::string di2 = current_exception_diagnostic_information();
        std::string wh2=x.what();
        BOOST_TEST(wh2==di2);
        test4(di1,di2);
        }
    return boost::report_errors();
    }