1 | // Copyright 2021 Peter Dimov. |
2 | // Distributed under the Boost Software License, Version 1.0. |
3 | // https://www.boost.org/LICENSE_1_0.txt |
4 | |
5 | #include <boost/system.hpp> |
6 | #include <boost/core/lightweight_test.hpp> |
7 | #include <cerrno> |
8 | |
9 | namespace sys = boost::system; |
10 | |
11 | enum E |
12 | { |
13 | none = 0, |
14 | einval = EINVAL |
15 | }; |
16 | |
17 | namespace boost |
18 | { |
19 | namespace system |
20 | { |
21 | |
22 | template<> struct is_error_code_enum< ::E > |
23 | { |
24 | static const bool value = true; |
25 | }; |
26 | |
27 | } // namespace system |
28 | } // namespace boost |
29 | |
30 | sys::error_code make_error_code( E e ) |
31 | { |
32 | return e == 0? sys::error_code(): sys::error_code( e, sys::generic_category() ); |
33 | } |
34 | |
35 | enum E2 |
36 | { |
37 | e2inval = EINVAL |
38 | }; |
39 | |
40 | namespace boost |
41 | { |
42 | namespace system |
43 | { |
44 | |
45 | template<> struct is_error_code_enum< ::E2 > |
46 | { |
47 | static const bool value = true; |
48 | }; |
49 | |
50 | } // namespace system |
51 | } // namespace boost |
52 | |
53 | std::error_code make_error_code( E2 e ) |
54 | { |
55 | return std::error_code( e, std::generic_category() ); |
56 | } |
57 | |
58 | int main() |
59 | { |
60 | { |
61 | sys::error_code ec( einval ); |
62 | |
63 | BOOST_TEST_EQ( ec.value(), EINVAL ); |
64 | BOOST_TEST_EQ( &ec.category(), &sys::generic_category() ); |
65 | |
66 | BOOST_TEST( ec.failed() ); |
67 | |
68 | BOOST_TEST( !ec.has_location() ); |
69 | BOOST_TEST_EQ( ec.location().line(), 0 ); |
70 | } |
71 | |
72 | { |
73 | BOOST_STATIC_CONSTEXPR boost::source_location loc = BOOST_CURRENT_LOCATION; |
74 | |
75 | sys::error_code ec( einval, &loc ); |
76 | |
77 | BOOST_TEST_EQ( ec.value(), EINVAL ); |
78 | BOOST_TEST_EQ( &ec.category(), &sys::generic_category() ); |
79 | |
80 | BOOST_TEST( ec.failed() ); |
81 | |
82 | BOOST_TEST( ec.has_location() ); |
83 | BOOST_TEST_EQ( ec.location().line(), 73 ); |
84 | } |
85 | |
86 | { |
87 | sys::error_code ec( none ); |
88 | |
89 | BOOST_TEST_EQ( ec.value(), 0 ); |
90 | BOOST_TEST_EQ( &ec.category(), &sys::system_category() ); |
91 | |
92 | BOOST_TEST( !ec.failed() ); |
93 | |
94 | BOOST_TEST( !ec.has_location() ); |
95 | BOOST_TEST_EQ( ec.location().line(), 0 ); |
96 | } |
97 | |
98 | { |
99 | BOOST_STATIC_CONSTEXPR boost::source_location loc = BOOST_CURRENT_LOCATION; |
100 | |
101 | sys::error_code ec( none, &loc ); |
102 | |
103 | BOOST_TEST_EQ( ec.value(), 0 ); |
104 | BOOST_TEST_EQ( &ec.category(), &sys::system_category() ); |
105 | |
106 | BOOST_TEST( !ec.failed() ); |
107 | |
108 | BOOST_TEST( !ec.has_location() ); |
109 | BOOST_TEST_EQ( ec.location().line(), 0 ); |
110 | } |
111 | |
112 | { |
113 | sys::error_code ec; |
114 | |
115 | BOOST_TEST_EQ( ec.value(), 0 ); |
116 | BOOST_TEST_EQ( &ec.category(), &sys::system_category() ); |
117 | |
118 | BOOST_TEST( !ec.failed() ); |
119 | |
120 | BOOST_TEST( !ec.has_location() ); |
121 | BOOST_TEST_EQ( ec.location().line(), 0 ); |
122 | |
123 | BOOST_STATIC_CONSTEXPR boost::source_location loc = BOOST_CURRENT_LOCATION; |
124 | |
125 | ec = sys::error_code( einval, &loc ); |
126 | |
127 | BOOST_TEST_EQ( ec.value(), EINVAL ); |
128 | BOOST_TEST_EQ( &ec.category(), &sys::generic_category() ); |
129 | |
130 | BOOST_TEST( ec.failed() ); |
131 | |
132 | BOOST_TEST( ec.has_location() ); |
133 | BOOST_TEST_EQ( ec.location().line(), 123 ); |
134 | } |
135 | |
136 | { |
137 | sys::error_code ec; |
138 | |
139 | BOOST_TEST_EQ( ec.value(), 0 ); |
140 | BOOST_TEST_EQ( &ec.category(), &sys::system_category() ); |
141 | |
142 | BOOST_TEST( !ec.failed() ); |
143 | |
144 | BOOST_TEST( !ec.has_location() ); |
145 | BOOST_TEST_EQ( ec.location().line(), 0 ); |
146 | |
147 | BOOST_STATIC_CONSTEXPR boost::source_location loc = BOOST_CURRENT_LOCATION; |
148 | |
149 | ec = sys::error_code( none, &loc ); |
150 | |
151 | BOOST_TEST_EQ( ec.value(), 0 ); |
152 | BOOST_TEST_EQ( &ec.category(), &sys::system_category() ); |
153 | |
154 | BOOST_TEST( !ec.failed() ); |
155 | |
156 | BOOST_TEST( !ec.has_location() ); |
157 | BOOST_TEST_EQ( ec.location().line(), 0 ); |
158 | } |
159 | |
160 | { |
161 | sys::error_code ec; |
162 | |
163 | BOOST_TEST_EQ( ec.value(), 0 ); |
164 | BOOST_TEST_EQ( &ec.category(), &sys::system_category() ); |
165 | |
166 | BOOST_TEST( !ec.failed() ); |
167 | |
168 | BOOST_TEST( !ec.has_location() ); |
169 | BOOST_TEST_EQ( ec.location().line(), 0 ); |
170 | |
171 | BOOST_STATIC_CONSTEXPR boost::source_location loc = BOOST_CURRENT_LOCATION; |
172 | |
173 | ec.assign( ec: einval, loc: &loc ); |
174 | |
175 | BOOST_TEST_EQ( ec.value(), EINVAL ); |
176 | BOOST_TEST_EQ( &ec.category(), &sys::generic_category() ); |
177 | |
178 | BOOST_TEST( ec.failed() ); |
179 | |
180 | BOOST_TEST( ec.has_location() ); |
181 | BOOST_TEST_EQ( ec.location().line(), 171 ); |
182 | } |
183 | |
184 | { |
185 | sys::error_code ec; |
186 | |
187 | BOOST_TEST_EQ( ec.value(), 0 ); |
188 | BOOST_TEST_EQ( &ec.category(), &sys::system_category() ); |
189 | |
190 | BOOST_TEST( !ec.failed() ); |
191 | |
192 | BOOST_TEST( !ec.has_location() ); |
193 | BOOST_TEST_EQ( ec.location().line(), 0 ); |
194 | |
195 | BOOST_STATIC_CONSTEXPR boost::source_location loc = BOOST_CURRENT_LOCATION; |
196 | |
197 | ec.assign( ec: none, loc: &loc ); |
198 | |
199 | BOOST_TEST_EQ( ec.value(), 0 ); |
200 | BOOST_TEST_EQ( &ec.category(), &sys::system_category() ); |
201 | |
202 | BOOST_TEST( !ec.failed() ); |
203 | |
204 | BOOST_TEST( !ec.has_location() ); |
205 | BOOST_TEST_EQ( ec.location().line(), 0 ); |
206 | } |
207 | |
208 | { |
209 | sys::error_code ec( e2inval ); |
210 | |
211 | BOOST_TEST_EQ( ec, std::error_code( EINVAL, std::generic_category() ) ); |
212 | |
213 | BOOST_TEST( ec.failed() ); |
214 | |
215 | BOOST_TEST( !ec.has_location() ); |
216 | BOOST_TEST_EQ( ec.location().line(), 0 ); |
217 | } |
218 | |
219 | { |
220 | BOOST_STATIC_CONSTEXPR boost::source_location loc = BOOST_CURRENT_LOCATION; |
221 | |
222 | sys::error_code ec( e2inval, &loc ); |
223 | |
224 | BOOST_TEST_EQ( ec, std::error_code( EINVAL, std::generic_category() ) ); |
225 | |
226 | BOOST_TEST( ec.failed() ); |
227 | |
228 | BOOST_TEST( !ec.has_location() ); |
229 | BOOST_TEST_EQ( ec.location().line(), 0 ); |
230 | } |
231 | |
232 | { |
233 | sys::error_code ec; |
234 | |
235 | BOOST_TEST_EQ( ec.value(), 0 ); |
236 | BOOST_TEST_EQ( &ec.category(), &sys::system_category() ); |
237 | |
238 | BOOST_TEST( !ec.failed() ); |
239 | |
240 | BOOST_TEST( !ec.has_location() ); |
241 | BOOST_TEST_EQ( ec.location().line(), 0 ); |
242 | |
243 | BOOST_STATIC_CONSTEXPR boost::source_location loc = BOOST_CURRENT_LOCATION; |
244 | |
245 | ec = sys::error_code( e2inval, &loc ); |
246 | |
247 | BOOST_TEST_EQ( ec, std::error_code( EINVAL, std::generic_category() ) ); |
248 | |
249 | BOOST_TEST( ec.failed() ); |
250 | |
251 | BOOST_TEST( !ec.has_location() ); |
252 | BOOST_TEST_EQ( ec.location().line(), 0 ); |
253 | } |
254 | |
255 | { |
256 | sys::error_code ec; |
257 | |
258 | BOOST_TEST_EQ( ec.value(), 0 ); |
259 | BOOST_TEST_EQ( &ec.category(), &sys::system_category() ); |
260 | |
261 | BOOST_TEST( !ec.failed() ); |
262 | |
263 | BOOST_TEST( !ec.has_location() ); |
264 | BOOST_TEST_EQ( ec.location().line(), 0 ); |
265 | |
266 | BOOST_STATIC_CONSTEXPR boost::source_location loc = BOOST_CURRENT_LOCATION; |
267 | |
268 | ec.assign( ec: e2inval, loc: &loc ); |
269 | |
270 | BOOST_TEST_EQ( ec, std::error_code( EINVAL, std::generic_category() ) ); |
271 | |
272 | BOOST_TEST( ec.failed() ); |
273 | |
274 | BOOST_TEST( !ec.has_location() ); |
275 | BOOST_TEST_EQ( ec.location().line(), 0 ); |
276 | } |
277 | |
278 | return boost::report_errors(); |
279 | } |
280 | |