]>
Commit | Line | Data |
---|---|---|
92f5a8d4 TL |
1 | |
2 | // Copyright 2018 Peter Dimov. | |
3 | // Distributed under the Boost Software License, Version 1.0. | |
4 | ||
5 | // Avoid spurious VC++ warnings | |
6 | #define _CRT_SECURE_NO_WARNINGS | |
7 | ||
8 | #include <boost/system/error_code.hpp> | |
9 | #include <boost/core/lightweight_test.hpp> | |
10 | #include <cstdio> | |
11 | ||
12 | using namespace boost::system; | |
13 | ||
14 | struct http_category_impl: public error_category | |
15 | { | |
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 | |
19 | { | |
20 | } | |
21 | ||
22 | char const * name() const BOOST_NOEXCEPT | |
23 | { | |
24 | return "http"; | |
25 | } | |
26 | ||
27 | std::string message( int ev ) const | |
28 | { | |
29 | char buffer[ 32 ]; | |
30 | ||
31 | std::sprintf( buffer, "HTTP/1.0 %d", ev ); | |
32 | return buffer; | |
33 | } | |
34 | ||
35 | bool failed( int ev ) const BOOST_NOEXCEPT | |
36 | { | |
37 | return !( ev >= 200 && ev < 300 ); | |
38 | } | |
39 | }; | |
40 | ||
41 | error_category const & http_category() | |
42 | { | |
43 | static const http_category_impl instance; | |
44 | return instance; | |
45 | } | |
46 | ||
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 ); | |
49 | ||
50 | template<class Ec> void test() | |
51 | { | |
52 | { | |
53 | Ec ec; | |
54 | TEST_NOT_FAILED( ec ); | |
55 | ||
56 | ec.assign( 1, generic_category() ); | |
57 | TEST_FAILED( ec ); | |
58 | ||
59 | ec.clear(); | |
60 | TEST_NOT_FAILED( ec ); | |
61 | ||
62 | ec = Ec( 1, generic_category() ); | |
63 | TEST_FAILED( ec ); | |
64 | ||
65 | ec = Ec(); | |
66 | TEST_NOT_FAILED( ec ); | |
67 | } | |
68 | ||
69 | { | |
70 | Ec ec; | |
71 | TEST_NOT_FAILED( ec ); | |
72 | ||
73 | ec.assign( 1, system_category() ); | |
74 | TEST_FAILED( ec ); | |
75 | ||
76 | ec.clear(); | |
77 | TEST_NOT_FAILED( ec ); | |
78 | ||
79 | ec = Ec( 1, system_category() ); | |
80 | TEST_FAILED( ec ); | |
81 | ||
82 | ec = Ec(); | |
83 | TEST_NOT_FAILED( ec ); | |
84 | } | |
85 | ||
86 | { | |
87 | Ec ec( 0, generic_category() ); | |
88 | TEST_NOT_FAILED( ec ); | |
89 | ||
90 | ec.assign( 1, system_category() ); | |
91 | TEST_FAILED( ec ); | |
92 | ||
93 | ec = Ec( 0, system_category() ); | |
94 | TEST_NOT_FAILED( ec ); | |
95 | } | |
96 | ||
97 | { | |
98 | Ec ec( 1, generic_category() ); | |
99 | TEST_FAILED( ec ); | |
100 | ||
101 | ec.assign( 0, system_category() ); | |
102 | TEST_NOT_FAILED( ec ); | |
103 | } | |
104 | ||
105 | { | |
106 | Ec ec( 0, system_category() ); | |
107 | TEST_NOT_FAILED( ec ); | |
108 | ||
109 | ec.assign( 1, generic_category() ); | |
110 | TEST_FAILED( ec ); | |
111 | ||
112 | ec = Ec( 0, generic_category() ); | |
113 | TEST_NOT_FAILED( ec ); | |
114 | } | |
115 | ||
116 | { | |
117 | Ec ec( 1, system_category() ); | |
118 | TEST_FAILED( ec ); | |
119 | ||
120 | ec.assign( 0, generic_category() ); | |
121 | TEST_NOT_FAILED( ec ); | |
122 | } | |
92f5a8d4 | 123 | |
1e59de90 TL |
124 | { |
125 | Ec ec( 0, http_category() ); | |
126 | TEST_FAILED( ec ); | |
20effc67 | 127 | |
1e59de90 TL |
128 | ec.assign( 200, http_category() ); |
129 | TEST_NOT_FAILED( ec ); | |
20effc67 | 130 | |
1e59de90 TL |
131 | ec = Ec( 404, http_category() ); |
132 | TEST_FAILED( ec ); | |
133 | } | |
92f5a8d4 TL |
134 | |
135 | } | |
136 | ||
137 | int main() | |
138 | { | |
139 | BOOST_TEST( !generic_category().failed( 0 ) ); | |
140 | BOOST_TEST( generic_category().failed( 7 ) ); | |
141 | ||
142 | BOOST_TEST( !system_category().failed( 0 ) ); | |
143 | BOOST_TEST( system_category().failed( 7 ) ); | |
144 | ||
145 | BOOST_TEST( http_category().failed( 0 ) ); | |
146 | BOOST_TEST( !http_category().failed( 200 ) ); | |
147 | BOOST_TEST( http_category().failed( 404 ) ); | |
148 | ||
149 | test<error_code>(); | |
150 | test<error_condition>(); | |
151 | ||
152 | { | |
153 | error_condition ec( errc::success ); | |
154 | TEST_NOT_FAILED( ec ); | |
155 | ||
156 | ec = errc::address_family_not_supported; | |
157 | TEST_FAILED( ec ); | |
158 | } | |
159 | ||
160 | { | |
161 | error_condition ec( errc::address_family_not_supported ); | |
162 | TEST_FAILED( ec ); | |
163 | ||
164 | ec = errc::success; | |
165 | TEST_NOT_FAILED( ec ); | |
166 | } | |
167 | ||
168 | { | |
169 | error_code ec( make_error_code( errc::success ) ); | |
170 | TEST_NOT_FAILED( ec ); | |
171 | ||
172 | ec = make_error_code( errc::address_family_not_supported ); | |
173 | TEST_FAILED( ec ); | |
174 | } | |
175 | ||
176 | { | |
177 | error_code ec( make_error_code( errc::address_family_not_supported ) ); | |
178 | TEST_FAILED( ec ); | |
179 | ||
180 | ec = make_error_code( errc::success ); | |
181 | TEST_NOT_FAILED( ec ); | |
182 | } | |
183 | ||
184 | return boost::report_errors(); | |
185 | } |