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
http_category_implhttp_category_impl18 BOOST_SYSTEM_CONSTEXPR http_category_impl() BOOST_NOEXCEPT
19 {
20 }
21
namehttp_category_impl22 char const * name() const BOOST_NOEXCEPT
23 {
24 return "http";
25 }
26
messagehttp_category_impl27 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
failedhttp_category_impl35 bool failed( int ev ) const BOOST_NOEXCEPT
36 {
37 return !( ev >= 200 && ev < 300 );
38 }
39 };
40
http_category()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
test()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 }
123 }
124
test2()125 template<class Ec> void test2()
126 {
127 Ec ec( 0, http_category() );
128 TEST_FAILED( ec );
129
130 ec.assign( 200, http_category() );
131 TEST_NOT_FAILED( ec );
132
133 ec = Ec( 404, http_category() );
134 TEST_FAILED( ec );
135 }
136
test3()137 template<class Ec> void test3()
138 {
139 Ec ec( 0, http_category() );
140 BOOST_TEST( ec.failed() );
141
142 ec.assign( 200, http_category() );
143 BOOST_TEST( !ec.failed() );
144
145 ec = Ec( 404, http_category() );
146 BOOST_TEST( ec.failed() );
147 }
148
main()149 int main()
150 {
151 BOOST_TEST( !generic_category().failed( 0 ) );
152 BOOST_TEST( generic_category().failed( 7 ) );
153
154 BOOST_TEST( !system_category().failed( 0 ) );
155 BOOST_TEST( system_category().failed( 7 ) );
156
157 BOOST_TEST( http_category().failed( 0 ) );
158 BOOST_TEST( !http_category().failed( 200 ) );
159 BOOST_TEST( http_category().failed( 404 ) );
160
161 test<error_code>();
162 test2<error_code>();
163 test<error_condition>();
164 test3<error_condition>();
165
166 {
167 error_condition ec( errc::success );
168 TEST_NOT_FAILED( ec );
169
170 ec = errc::address_family_not_supported;
171 TEST_FAILED( ec );
172 }
173
174 {
175 error_condition ec( errc::address_family_not_supported );
176 TEST_FAILED( ec );
177
178 ec = errc::success;
179 TEST_NOT_FAILED( ec );
180 }
181
182 {
183 error_code ec( make_error_code( errc::success ) );
184 TEST_NOT_FAILED( ec );
185
186 ec = make_error_code( errc::address_family_not_supported );
187 TEST_FAILED( ec );
188 }
189
190 {
191 error_code ec( make_error_code( errc::address_family_not_supported ) );
192 TEST_FAILED( ec );
193
194 ec = make_error_code( errc::success );
195 TEST_NOT_FAILED( ec );
196 }
197
198 return boost::report_errors();
199 }
200