summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/system/test/failed_test.cpp
blob: 142d6ef8f436eb918c4d471500a225d42009b9e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright 2018 Peter Dimov.
// Distributed under the Boost Software License, Version 1.0.

// Avoid spurious VC++ warnings
#define _CRT_SECURE_NO_WARNINGS

#include <boost/system/error_code.hpp>
#include <boost/core/lightweight_test.hpp>
#include <cstdio>

using namespace boost::system;

struct http_category_impl: public error_category
{
    // clang++ 3.8 and below: initialization of const object
    // requires a user-provided default constructor
    BOOST_SYSTEM_CONSTEXPR http_category_impl() BOOST_NOEXCEPT
    {
    }

    char const * name() const BOOST_NOEXCEPT
    {
        return "http";
    }

    std::string message( int ev ) const
    {
        char buffer[ 32 ];

        std::sprintf( buffer, "HTTP/1.0 %d", ev );
        return buffer;
    }

    bool failed( int ev ) const BOOST_NOEXCEPT
    {
        return !( ev >= 200 && ev < 300 );
    }
};

error_category const & http_category()
{
    static const http_category_impl instance;
    return instance;
}

#define TEST_NOT_FAILED(ec) BOOST_TEST( !ec.failed() ); BOOST_TEST( ec? false: true ); BOOST_TEST( !ec );
#define TEST_FAILED(ec) BOOST_TEST( ec.failed() ); BOOST_TEST( ec ); BOOST_TEST( !!ec );

template<class Ec> void test()
{
    {
        Ec ec;
        TEST_NOT_FAILED( ec );

        ec.assign( 1, generic_category() );
        TEST_FAILED( ec );

        ec.clear();
        TEST_NOT_FAILED( ec );

        ec = Ec( 1, generic_category() );
        TEST_FAILED( ec );

        ec = Ec();
        TEST_NOT_FAILED( ec );
    }

    {
        Ec ec;
        TEST_NOT_FAILED( ec );

        ec.assign( 1, system_category() );
        TEST_FAILED( ec );

        ec.clear();
        TEST_NOT_FAILED( ec );

        ec = Ec( 1, system_category() );
        TEST_FAILED( ec );

        ec = Ec();
        TEST_NOT_FAILED( ec );
    }

    {
        Ec ec( 0, generic_category() );
        TEST_NOT_FAILED( ec );

        ec.assign( 1, system_category() );
        TEST_FAILED( ec );

        ec = Ec( 0, system_category() );
        TEST_NOT_FAILED( ec );
    }

    {
        Ec ec( 1, generic_category() );
        TEST_FAILED( ec );

        ec.assign( 0, system_category() );
        TEST_NOT_FAILED( ec );
    }

    {
        Ec ec( 0, system_category() );
        TEST_NOT_FAILED( ec );

        ec.assign( 1, generic_category() );
        TEST_FAILED( ec );

        ec = Ec( 0, generic_category() );
        TEST_NOT_FAILED( ec );
    }

    {
        Ec ec( 1, system_category() );
        TEST_FAILED( ec );

        ec.assign( 0, generic_category() );
        TEST_NOT_FAILED( ec );
    }

    {
        Ec ec( 0, http_category() );
        BOOST_TEST( ec.failed() );

        ec.assign( 200, http_category() );
        BOOST_TEST( !ec.failed() );

        ec = Ec( 404, http_category() );
        BOOST_TEST( ec.failed() );
    }

}

int main()
{
    BOOST_TEST( !generic_category().failed( 0 ) );
    BOOST_TEST( generic_category().failed( 7 ) );

    BOOST_TEST( !system_category().failed( 0 ) );
    BOOST_TEST( system_category().failed( 7 ) );

    BOOST_TEST( http_category().failed( 0 ) );
    BOOST_TEST( !http_category().failed( 200 ) );
    BOOST_TEST( http_category().failed( 404 ) );

    test<error_code>();
    test<error_condition>();

    {
        error_condition ec( errc::success );
        TEST_NOT_FAILED( ec );

        ec = errc::address_family_not_supported;
        TEST_FAILED( ec );
    }

    {
        error_condition ec( errc::address_family_not_supported );
        TEST_FAILED( ec );

        ec = errc::success;
        TEST_NOT_FAILED( ec );
    }

    {
        error_code ec( make_error_code( errc::success ) );
        TEST_NOT_FAILED( ec );

        ec = make_error_code( errc::address_family_not_supported );
        TEST_FAILED( ec );
    }

    {
        error_code ec( make_error_code( errc::address_family_not_supported ) );
        TEST_FAILED( ec );

        ec = make_error_code( errc::success );
        TEST_NOT_FAILED( ec );
    }

    return boost::report_errors();
}