]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | //Copyright (c) 2006-2009 Emil Dotchevski and Reverge Studios, Inc. |
2 | ||
3 | //Distributed under the Boost Software License, Version 1.0. (See accompanying | |
4 | //file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
5 | ||
6 | #include <boost/exception/diagnostic_information.hpp> | |
7 | #include <boost/exception/info.hpp> | |
8 | #include <boost/detail/lightweight_test.hpp> | |
9 | #include <boost/detail/workaround.hpp> | |
10 | ||
11 | #if BOOST_WORKAROUND(__CODEGEARC__, BOOST_TESTED_AT(0x610)) | |
12 | struct test_tag1 {}; | |
13 | struct test_tag2 {}; | |
14 | #endif | |
15 | ||
16 | typedef boost::error_info<struct test_tag1,int> tagged_int1; | |
17 | typedef boost::error_info<struct test_tag2,int> tagged_int2; | |
18 | ||
19 | std::string | |
20 | to_string( tagged_int2 const & x ) | |
21 | { | |
22 | return '[' +boost::error_info_name(x) + "] = " + (x.value()==42 ? "fourty-two" : "bad value"); | |
23 | } | |
24 | ||
25 | struct | |
26 | error1: | |
27 | std::exception, | |
28 | boost::exception | |
29 | { | |
30 | char const * | |
92f5a8d4 | 31 | what() const BOOST_NOEXCEPT_OR_NOTHROW |
7c673cae FG |
32 | { |
33 | return "error1"; | |
34 | } | |
35 | }; | |
36 | ||
37 | struct | |
38 | error2: | |
39 | boost::exception | |
40 | { | |
41 | }; | |
42 | ||
43 | struct | |
44 | error3: | |
45 | std::exception | |
46 | { | |
47 | char const * | |
92f5a8d4 | 48 | what() const BOOST_NOEXCEPT_OR_NOTHROW |
7c673cae FG |
49 | { |
50 | return "error3"; | |
51 | } | |
52 | }; | |
53 | ||
54 | struct | |
55 | error4: | |
56 | std::exception, | |
57 | boost::exception | |
58 | { | |
59 | char const * | |
92f5a8d4 | 60 | what() const BOOST_NOEXCEPT_OR_NOTHROW |
7c673cae FG |
61 | { |
62 | return diagnostic_information_what(*this); | |
63 | } | |
64 | }; | |
65 | ||
66 | void | |
67 | test1( std::string const & di1, std::string const & di2 ) | |
68 | { | |
69 | BOOST_TEST( di1!=di2 ); | |
70 | #ifndef BOOST_NO_RTTI | |
71 | BOOST_TEST(di1.find("type:")!=std::string::npos); | |
72 | BOOST_TEST(di1.find("error1")!=std::string::npos); | |
73 | #endif | |
74 | BOOST_TEST(di1.find("test_tag1")!=std::string::npos); | |
75 | BOOST_TEST(di1.find("test_tag2")!=std::string::npos); | |
76 | BOOST_TEST(di1.find("fourty-two")!=std::string::npos); | |
77 | #ifndef BOOST_NO_RTTI | |
78 | BOOST_TEST(di2.find("type:")!=std::string::npos); | |
79 | BOOST_TEST(di2.find("error1")!=std::string::npos); | |
80 | #endif | |
81 | BOOST_TEST(di2.find("test_tag1")!=std::string::npos); | |
82 | BOOST_TEST(di2.find("test_tag2")!=std::string::npos); | |
83 | BOOST_TEST(di2.find("bad value")!=std::string::npos); | |
84 | } | |
85 | ||
86 | void | |
87 | test2( std::string const & di1, std::string const & di2 ) | |
88 | { | |
89 | BOOST_TEST( di1!=di2 ); | |
90 | BOOST_TEST(di1.find("test_tag1")!=std::string::npos); | |
91 | BOOST_TEST(di1.find("test_tag2")!=std::string::npos); | |
92 | BOOST_TEST(di1.find("fourty-two")!=std::string::npos); | |
93 | BOOST_TEST(di2.find("test_tag1")!=std::string::npos); | |
94 | BOOST_TEST(di2.find("test_tag2")!=std::string::npos); | |
95 | BOOST_TEST(di2.find("bad value")!=std::string::npos); | |
96 | } | |
97 | ||
98 | void | |
99 | test3( std::string const & di ) | |
100 | { | |
101 | #ifndef BOOST_NO_RTTI | |
102 | BOOST_TEST(di.find("type:")!=std::string::npos); | |
103 | #endif | |
104 | BOOST_TEST(di.find("error3")!=std::string::npos); | |
105 | } | |
106 | ||
107 | void | |
108 | test4( std::string const & di1, std::string const & di2 ) | |
109 | { | |
110 | BOOST_TEST( di1!=di2 ); | |
111 | #ifndef BOOST_NO_RTTI | |
112 | BOOST_TEST(di1.find("type:")!=std::string::npos); | |
113 | #endif | |
114 | BOOST_TEST(di1.find("test_tag1")!=std::string::npos); | |
115 | BOOST_TEST(di1.find("test_tag2")!=std::string::npos); | |
116 | BOOST_TEST(di1.find("fourty-two")!=std::string::npos); | |
117 | #ifndef BOOST_NO_RTTI | |
118 | BOOST_TEST(di2.find("type:")!=std::string::npos); | |
119 | #endif | |
120 | BOOST_TEST(di2.find("test_tag1")!=std::string::npos); | |
121 | BOOST_TEST(di2.find("test_tag2")!=std::string::npos); | |
122 | BOOST_TEST(di2.find("bad value")!=std::string::npos); | |
123 | } | |
124 | ||
125 | int | |
126 | main() | |
127 | { | |
128 | using namespace boost; | |
129 | try | |
130 | { | |
131 | error1 x; x << tagged_int1(42) << tagged_int2(42); | |
132 | BOOST_TEST(x.what()==std::string("error1")); | |
133 | throw x; | |
134 | } | |
135 | catch( | |
136 | error1 & x ) | |
137 | { | |
138 | std::string di1=boost::diagnostic_information(x); | |
139 | x << tagged_int1(2) << tagged_int2(2); | |
140 | std::string di2 = diagnostic_information(x); | |
141 | test1(di1,di2); | |
142 | } | |
143 | try | |
144 | { | |
145 | error1 x; x << tagged_int1(42) << tagged_int2(42); | |
146 | BOOST_TEST(x.what()==std::string("error1")); | |
147 | throw x; | |
148 | } | |
149 | catch( | |
150 | error1 & x ) | |
151 | { | |
152 | std::string di1=boost::current_exception_diagnostic_information(); | |
153 | x << tagged_int1(2) << tagged_int2(2); | |
154 | std::string di2 = current_exception_diagnostic_information(); | |
155 | test1(di1,di2); | |
156 | } | |
157 | try | |
158 | { | |
159 | error2 x; | |
160 | x << tagged_int1(42) << tagged_int2(42); | |
161 | throw x; | |
162 | } | |
163 | catch( | |
164 | error2 & x ) | |
165 | { | |
166 | std::string di1 = diagnostic_information(x); | |
167 | x << tagged_int1(2) << tagged_int2(2); | |
168 | std::string di2 = diagnostic_information(x); | |
169 | test2(di1,di2); | |
170 | } | |
171 | try | |
172 | { | |
173 | error2 x; | |
174 | x << tagged_int1(42) << tagged_int2(42); | |
175 | throw x; | |
176 | } | |
177 | catch( | |
178 | error2 & x ) | |
179 | { | |
180 | std::string di1 = current_exception_diagnostic_information(); | |
181 | BOOST_TEST(di1==boost::diagnostic_information_what(x)); | |
182 | x << tagged_int1(2) << tagged_int2(2); | |
183 | std::string di2 = current_exception_diagnostic_information(); | |
184 | BOOST_TEST(di2==boost::diagnostic_information_what(x)); | |
185 | test2(di1,di2); | |
186 | } | |
187 | try | |
188 | { | |
189 | error3 x; | |
190 | std::string di=diagnostic_information(x); | |
191 | test3(di); | |
192 | throw x; | |
193 | } | |
194 | catch( | |
195 | ... ) | |
196 | { | |
197 | std::string di=current_exception_diagnostic_information(); | |
198 | test3(di); | |
199 | } | |
200 | try | |
201 | { | |
202 | throw error4(); | |
203 | } | |
204 | catch( | |
205 | error4 & x ) | |
206 | { | |
207 | std::string di1=boost::diagnostic_information(x); | |
208 | std::string wh1=x.what(); | |
209 | BOOST_TEST(wh1==di1); | |
210 | } | |
211 | try | |
212 | { | |
213 | error4 x; x << tagged_int1(42) << tagged_int2(42); | |
214 | throw x; | |
215 | } | |
216 | catch( | |
217 | error4 & x ) | |
218 | { | |
219 | std::string di1=boost::diagnostic_information(x); | |
220 | std::string wh1=x.what(); | |
221 | BOOST_TEST(wh1==di1); | |
222 | x << tagged_int1(2) << tagged_int2(2); | |
223 | std::string di2 = diagnostic_information(x); | |
224 | std::string wh2=x.what(); | |
225 | BOOST_TEST(wh2==di2); | |
226 | test4(di1,di2); | |
227 | } | |
228 | try | |
229 | { | |
230 | error4 x; x << tagged_int1(42) << tagged_int2(42); | |
231 | throw x; | |
232 | } | |
233 | catch( | |
234 | error4 & x ) | |
235 | { | |
236 | std::string di1=boost::current_exception_diagnostic_information(); | |
237 | std::string wh1=x.what(); | |
238 | BOOST_TEST(wh1==di1); | |
239 | x << tagged_int1(2) << tagged_int2(2); | |
240 | std::string di2 = current_exception_diagnostic_information(); | |
241 | std::string wh2=x.what(); | |
242 | BOOST_TEST(wh2==di2); | |
243 | test4(di1,di2); | |
244 | } | |
245 | return boost::report_errors(); | |
246 | } |