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

source code of boost/libs/system/test/failed_test.cpp