]>
Commit | Line | Data |
---|---|---|
b32b8144 FG |
1 | |
2 | // Copyright 2017 Peter Dimov. | |
3 | // | |
4 | // Distributed under the Boost Software License, Version 1.0. | |
5 | // | |
6 | // See accompanying file LICENSE_1_0.txt or copy at | |
7 | // http://www.boost.org/LICENSE_1_0.txt | |
8 | ||
9 | // See library home page at http://www.boost.org/libs/system | |
10 | ||
11 | // Avoid spurious VC++ warnings | |
12 | # define _CRT_SECURE_NO_WARNINGS | |
13 | ||
14 | #include <boost/config.hpp> | |
15 | #include <iostream> | |
16 | ||
17 | #if defined(BOOST_NO_CXX11_HDR_SYSTEM_ERROR) | |
18 | ||
19 | int main() | |
20 | { | |
21 | std::cout | |
22 | << "The version of the C++ standard library being used does not" | |
23 | " support header <system_error> so interoperation will not be tested.\n"; | |
24 | } | |
25 | ||
26 | #else | |
27 | ||
28 | #include <boost/system/error_code.hpp> | |
29 | #include <boost/core/lightweight_test.hpp> | |
30 | #include <system_error> | |
31 | #include <cerrno> | |
32 | #include <string> | |
33 | #include <cstdio> | |
34 | ||
35 | static void test_generic_category() | |
36 | { | |
37 | boost::system::error_category const & bt = boost::system::generic_category(); | |
38 | std::error_category const & st = bt; | |
39 | ||
40 | BOOST_TEST_CSTR_EQ( bt.name(), st.name() ); | |
41 | ||
42 | int ev = ENOENT; | |
43 | ||
44 | BOOST_TEST_EQ( bt.message( ev ), st.message( ev ) ); | |
45 | ||
46 | { | |
47 | boost::system::error_code bc( ev, bt ); | |
48 | ||
49 | BOOST_TEST_EQ( bc.value(), ev ); | |
50 | BOOST_TEST_EQ( &bc.category(), &bt ); | |
51 | ||
52 | std::error_code sc( bc ); | |
53 | ||
54 | BOOST_TEST_EQ( sc.value(), ev ); | |
55 | BOOST_TEST_EQ( &sc.category(), &st ); | |
56 | } | |
57 | ||
58 | { | |
59 | boost::system::error_condition bn = bt.default_error_condition( ev ); | |
60 | ||
61 | BOOST_TEST_EQ( bn.value(), ev ); | |
62 | BOOST_TEST_EQ( &bn.category(), &bt ); | |
63 | ||
64 | BOOST_TEST( bt.equivalent( ev, bn ) ); | |
65 | ||
66 | std::error_condition sn( bn ); | |
67 | ||
68 | BOOST_TEST_EQ( sn.value(), ev ); | |
69 | BOOST_TEST_EQ( &sn.category(), &st ); | |
70 | ||
71 | BOOST_TEST( st.equivalent( ev, sn ) ); | |
72 | } | |
73 | } | |
74 | ||
75 | static void test_system_category() | |
76 | { | |
77 | boost::system::error_category const & bt = boost::system::system_category(); | |
78 | std::error_category const & st = bt; | |
79 | ||
80 | BOOST_TEST_CSTR_EQ( bt.name(), st.name() ); | |
81 | ||
82 | { | |
83 | int ev = 5; | |
84 | BOOST_TEST_EQ( bt.message( ev ), st.message( ev ) ); | |
85 | ||
86 | { | |
87 | boost::system::error_code bc( ev, bt ); | |
88 | ||
89 | BOOST_TEST_EQ( bc.value(), ev ); | |
90 | BOOST_TEST_EQ( &bc.category(), &bt ); | |
91 | ||
92 | std::error_code sc( bc ); | |
93 | ||
94 | BOOST_TEST_EQ( sc.value(), ev ); | |
95 | BOOST_TEST_EQ( &sc.category(), &st ); | |
96 | } | |
97 | ||
98 | { | |
99 | boost::system::error_condition bn = bt.default_error_condition( ev ); | |
100 | BOOST_TEST( bt.equivalent( ev, bn ) ); | |
101 | ||
102 | std::error_condition sn( bn ); | |
103 | BOOST_TEST( st.equivalent( ev, sn ) ); | |
104 | } | |
105 | } | |
106 | ||
107 | { | |
108 | int ev = 4; | |
109 | BOOST_TEST_EQ( bt.message( ev ), st.message( ev ) ); | |
110 | ||
111 | { | |
112 | boost::system::error_code bc( ev, bt ); | |
113 | ||
114 | BOOST_TEST_EQ( bc.value(), ev ); | |
115 | BOOST_TEST_EQ( &bc.category(), &bt ); | |
116 | ||
117 | std::error_code sc( bc ); | |
118 | ||
119 | BOOST_TEST_EQ( sc.value(), ev ); | |
120 | BOOST_TEST_EQ( &sc.category(), &st ); | |
121 | } | |
122 | ||
123 | { | |
124 | boost::system::error_condition bn = bt.default_error_condition( ev ); | |
125 | BOOST_TEST( bt.equivalent( ev, bn ) ); | |
126 | ||
127 | std::error_condition sn( bn ); | |
128 | BOOST_TEST( st.equivalent( ev, sn ) ); | |
129 | } | |
130 | } | |
131 | } | |
132 | ||
133 | // test_user_category | |
134 | ||
135 | class user_category_impl: public boost::system::error_category | |
136 | { | |
137 | public: | |
138 | ||
139 | virtual const char * name() const BOOST_NOEXCEPT | |
140 | { | |
141 | return "user"; | |
142 | } | |
143 | ||
144 | virtual std::string message( int ev ) const | |
145 | { | |
146 | char buffer[ 256 ]; | |
147 | std::sprintf( buffer, "user message %d", ev ); | |
148 | ||
149 | return buffer; | |
150 | } | |
151 | ||
152 | virtual boost::system::error_condition default_error_condition( int ev ) const BOOST_NOEXCEPT | |
153 | { | |
154 | if( ev == 4 ) | |
155 | { | |
156 | return boost::system::error_condition( EMFILE, boost::system::generic_category() ); | |
157 | } | |
158 | else if( ev == 5 ) | |
159 | { | |
160 | return boost::system::error_condition( EACCES, boost::system::generic_category() ); | |
161 | } | |
162 | else | |
163 | { | |
164 | return boost::system::error_condition( ev, *this ); | |
165 | } | |
166 | } | |
167 | ||
168 | virtual bool equivalent( int code, const boost::system::error_condition & condition ) const BOOST_NOEXCEPT | |
169 | { | |
170 | if( code == 4 && condition == make_error_condition( boost::system::errc::too_many_files_open_in_system ) ) | |
171 | { | |
172 | return true; | |
173 | } | |
174 | ||
175 | if( code == 4 && condition == make_error_condition( boost::system::errc::too_many_files_open ) ) | |
176 | { | |
177 | return true; | |
178 | } | |
179 | ||
180 | return default_error_condition( code ) == condition; | |
181 | } | |
182 | ||
183 | // virtual bool equivalent( const error_code & code, int condition ) const BOOST_NOEXCEPT; | |
184 | }; | |
185 | ||
186 | boost::system::error_category const & user_category() | |
187 | { | |
188 | static user_category_impl cat_; | |
189 | return cat_; | |
190 | } | |
191 | ||
192 | static void test_user_category() | |
193 | { | |
194 | boost::system::error_category const & bt = user_category(); | |
195 | std::error_category const & st = bt; | |
196 | ||
197 | BOOST_TEST_CSTR_EQ( bt.name(), st.name() ); | |
198 | ||
199 | { | |
200 | int ev = 5; | |
201 | BOOST_TEST_EQ( bt.message( ev ), st.message( ev ) ); | |
202 | ||
203 | { | |
204 | boost::system::error_code bc( ev, bt ); | |
205 | ||
206 | BOOST_TEST_EQ( bc.value(), ev ); | |
207 | BOOST_TEST_EQ( &bc.category(), &bt ); | |
208 | ||
209 | std::error_code sc( bc ); | |
210 | ||
211 | BOOST_TEST_EQ( sc.value(), ev ); | |
212 | BOOST_TEST_EQ( &sc.category(), &st ); | |
213 | } | |
214 | ||
215 | { | |
216 | boost::system::error_condition bn = bt.default_error_condition( ev ); | |
217 | BOOST_TEST( bt.equivalent( ev, bn ) ); | |
218 | ||
219 | std::error_condition sn( bn ); | |
220 | BOOST_TEST( st.equivalent( ev, sn ) ); | |
221 | } | |
222 | } | |
223 | ||
224 | { | |
225 | int ev = 4; | |
226 | BOOST_TEST_EQ( bt.message( ev ), st.message( ev ) ); | |
227 | ||
228 | { | |
229 | boost::system::error_code bc( ev, bt ); | |
230 | ||
231 | BOOST_TEST_EQ( bc.value(), ev ); | |
232 | BOOST_TEST_EQ( &bc.category(), &bt ); | |
233 | ||
234 | std::error_code sc( bc ); | |
235 | ||
236 | BOOST_TEST_EQ( sc.value(), ev ); | |
237 | BOOST_TEST_EQ( &sc.category(), &st ); | |
238 | } | |
239 | ||
240 | { | |
241 | boost::system::error_condition bn = bt.default_error_condition( ev ); | |
242 | BOOST_TEST( bt.equivalent( ev, bn ) ); | |
243 | ||
244 | std::error_condition sn( bn ); | |
245 | BOOST_TEST( st.equivalent( ev, sn ) ); | |
246 | } | |
247 | ||
248 | { | |
249 | boost::system::error_condition bn = make_error_condition( boost::system::errc::too_many_files_open_in_system ); | |
250 | BOOST_TEST( bt.equivalent( ev, bn ) ); | |
251 | ||
252 | std::error_condition sn( bn ); | |
253 | BOOST_TEST( st.equivalent( ev, sn ) ); | |
254 | } | |
255 | ||
256 | { | |
257 | boost::system::error_condition bn = make_error_condition( boost::system::errc::too_many_files_open ); | |
258 | BOOST_TEST( bt.equivalent( ev, bn ) ); | |
259 | ||
260 | std::error_condition sn( bn ); | |
261 | BOOST_TEST( st.equivalent( ev, sn ) ); | |
262 | } | |
263 | } | |
264 | } | |
265 | ||
266 | // test_user2_category | |
267 | ||
268 | enum user2_errc | |
269 | { | |
270 | my_enoent = 1, | |
271 | my_einval, | |
272 | my_other | |
273 | }; | |
274 | ||
275 | class user2_category_impl: public boost::system::error_category | |
276 | { | |
277 | public: | |
278 | ||
279 | virtual const char * name() const BOOST_NOEXCEPT | |
280 | { | |
281 | return "user2"; | |
282 | } | |
283 | ||
284 | virtual std::string message( int ev ) const | |
285 | { | |
286 | char buffer[ 256 ]; | |
287 | std::sprintf( buffer, "user2 message %d", ev ); | |
288 | ||
289 | return buffer; | |
290 | } | |
291 | ||
292 | virtual boost::system::error_condition default_error_condition( int ev ) const BOOST_NOEXCEPT | |
293 | { | |
294 | return boost::system::error_condition( ev, *this ); | |
295 | } | |
296 | ||
297 | virtual bool equivalent( int code, const boost::system::error_condition & condition ) const BOOST_NOEXCEPT | |
298 | { | |
299 | return default_error_condition( code ) == condition; | |
300 | } | |
301 | ||
302 | virtual bool equivalent( const boost::system::error_code & code, int condition ) const BOOST_NOEXCEPT | |
303 | { | |
304 | if( code.category() == *this ) | |
305 | { | |
306 | return condition == code.value(); | |
307 | } | |
308 | else if( condition == my_enoent ) | |
309 | { | |
310 | return code == boost::system::errc::no_such_file_or_directory; | |
311 | } | |
312 | else if( condition == my_einval ) | |
313 | { | |
314 | return code == boost::system::errc::invalid_argument; | |
315 | } | |
316 | else | |
317 | { | |
318 | return false; | |
319 | } | |
320 | } | |
321 | }; | |
322 | ||
323 | boost::system::error_category const & user2_category() | |
324 | { | |
325 | static user2_category_impl cat_; | |
326 | return cat_; | |
327 | } | |
328 | ||
329 | static void test_user2_category() | |
330 | { | |
331 | boost::system::error_category const & bt = user2_category(); | |
332 | std::error_category const & st = bt; | |
333 | ||
334 | int ev = my_enoent; | |
335 | ||
336 | boost::system::error_condition bn( ev, bt ); | |
337 | ||
338 | BOOST_TEST_EQ( bn.value(), ev ); | |
339 | BOOST_TEST_EQ( &bn.category(), &bt ); | |
340 | ||
341 | boost::system::error_code bc = make_error_code( boost::system::errc::no_such_file_or_directory ); | |
342 | ||
343 | BOOST_TEST( bc == bn ); | |
344 | ||
345 | std::error_condition sn( bn ); | |
346 | ||
347 | BOOST_TEST_EQ( sn.value(), ev ); | |
348 | BOOST_TEST_EQ( &sn.category(), &st ); | |
349 | ||
350 | std::error_code sc( bc ); | |
351 | ||
352 | BOOST_TEST( sc == sn ); | |
353 | } | |
354 | ||
355 | // | |
356 | ||
357 | int main() | |
358 | { | |
359 | std::cout | |
360 | << "The version of the C++ standard library being used" | |
361 | " supports header <system_error> so interoperation will be tested.\n"; | |
362 | test_generic_category(); | |
363 | test_system_category(); | |
364 | test_user_category(); | |
365 | test_user2_category(); | |
366 | ||
367 | return boost::report_errors(); | |
368 | } | |
369 | ||
370 | #endif |