2 // Copyright 2018 Peter Dimov.
3 // Distributed under the Boost Software License, Version 1.0.
5 // Avoid spurious VC++ warnings
6 #define _CRT_SECURE_NO_WARNINGS
8 #include <boost/system/error_code.hpp>
9 #include <boost/core/lightweight_test.hpp>
12 using namespace boost::system
;
14 struct http_category_impl
: public error_category
16 // clang++ 3.8 and below: initialization of const object
17 // requires a user-provided default constructor
18 BOOST_SYSTEM_CONSTEXPR
http_category_impl() BOOST_NOEXCEPT
22 char const * name() const BOOST_NOEXCEPT
27 std::string
message( int ev
) const
31 std::sprintf( buffer
, "HTTP/1.0 %d", ev
);
35 bool failed( int ev
) const BOOST_NOEXCEPT
37 return !( ev
>= 200 && ev
< 300 );
41 error_category
const & http_category()
43 static const http_category_impl instance
;
47 #define TEST_NOT_FAILED(ec) BOOST_TEST( !ec.failed() ); BOOST_TEST( ec? false: true ); BOOST_TEST( !ec );
48 #define TEST_FAILED(ec) BOOST_TEST( ec.failed() ); BOOST_TEST( ec ); BOOST_TEST( !!ec );
50 template<class Ec
> void test()
54 TEST_NOT_FAILED( ec
);
56 ec
.assign( 1, generic_category() );
60 TEST_NOT_FAILED( ec
);
62 ec
= Ec( 1, generic_category() );
66 TEST_NOT_FAILED( ec
);
71 TEST_NOT_FAILED( ec
);
73 ec
.assign( 1, system_category() );
77 TEST_NOT_FAILED( ec
);
79 ec
= Ec( 1, system_category() );
83 TEST_NOT_FAILED( ec
);
87 Ec
ec( 0, generic_category() );
88 TEST_NOT_FAILED( ec
);
90 ec
.assign( 1, system_category() );
93 ec
= Ec( 0, system_category() );
94 TEST_NOT_FAILED( ec
);
98 Ec
ec( 1, generic_category() );
101 ec
.assign( 0, system_category() );
102 TEST_NOT_FAILED( ec
);
106 Ec
ec( 0, system_category() );
107 TEST_NOT_FAILED( ec
);
109 ec
.assign( 1, generic_category() );
112 ec
= Ec( 0, generic_category() );
113 TEST_NOT_FAILED( ec
);
117 Ec
ec( 1, system_category() );
120 ec
.assign( 0, generic_category() );
121 TEST_NOT_FAILED( ec
);
125 Ec
ec( 0, http_category() );
128 ec
.assign( 200, http_category() );
129 TEST_NOT_FAILED( ec
);
131 ec
= Ec( 404, http_category() );
139 BOOST_TEST( !generic_category().failed( 0 ) );
140 BOOST_TEST( generic_category().failed( 7 ) );
142 BOOST_TEST( !system_category().failed( 0 ) );
143 BOOST_TEST( system_category().failed( 7 ) );
145 BOOST_TEST( http_category().failed( 0 ) );
146 BOOST_TEST( !http_category().failed( 200 ) );
147 BOOST_TEST( http_category().failed( 404 ) );
150 test
<error_condition
>();
153 error_condition
ec( errc::success
);
154 TEST_NOT_FAILED( ec
);
156 ec
= errc::address_family_not_supported
;
161 error_condition
ec( errc::address_family_not_supported
);
165 TEST_NOT_FAILED( ec
);
169 error_code
ec( make_error_code( errc::success
) );
170 TEST_NOT_FAILED( ec
);
172 ec
= make_error_code( errc::address_family_not_supported
);
177 error_code
ec( make_error_code( errc::address_family_not_supported
) );
180 ec
= make_error_code( errc::success
);
181 TEST_NOT_FAILED( ec
);
184 return boost::report_errors();