1
2// Copyright 2018 Peter Dimov.
3//
4// Distributed under the Boost Software License, Version 1.0.
5//
6// See accompanying file LICENSE_1_0.txt or copy at
7// http://www.boost.org/LICENSE_1_0.txt
8
9// See library home page at http://www.boost.org/libs/system
10
11#include <boost/system/error_code.hpp>
12#include <boost/core/lightweight_test.hpp>
13#include <boost/core/snprintf.hpp>
14#include <cstdio>
15
16//
17
18namespace sys = boost::system;
19
20class user_category: public sys::error_category
21{
22public:
23
24 virtual const char * name() const noexcept
25 {
26 return "user";
27 }
28
29 virtual std::string message( int ev ) const
30 {
31 char buffer[ 256 ];
32 boost::core::snprintf( s: buffer, maxlen: sizeof( buffer ), format: "user message %d", ev );
33
34 return buffer;
35 }
36
37 using sys::error_category::message;
38};
39
40static user_category s_cat_1;
41static user_category s_cat_2;
42
43int main()
44{
45 // default_error_condition
46
47 BOOST_TEST( s_cat_1.default_error_condition( 1 ) == sys::error_condition( 1, s_cat_1 ) );
48 BOOST_TEST( s_cat_2.default_error_condition( 2 ) == sys::error_condition( 2, s_cat_2 ) );
49
50 // equivalent
51
52 BOOST_TEST( s_cat_1.equivalent( 1, sys::error_condition( 1, s_cat_1 ) ) );
53 BOOST_TEST( !s_cat_1.equivalent( 1, sys::error_condition( 2, s_cat_1 ) ) );
54 BOOST_TEST( !s_cat_1.equivalent( 1, sys::error_condition( 2, s_cat_2 ) ) );
55
56 // the other equivalent
57
58 BOOST_TEST( s_cat_1.equivalent( sys::error_code( 1, s_cat_1 ), 1 ) );
59 BOOST_TEST( !s_cat_1.equivalent( sys::error_code( 1, s_cat_1 ), 2 ) );
60 BOOST_TEST( !s_cat_1.equivalent( sys::error_code( 1, s_cat_2 ), 1 ) );
61
62 // message
63
64 {
65 char buffer[ 256 ];
66 BOOST_TEST_CSTR_EQ( s_cat_1.message( 1, buffer, sizeof( buffer ) ), s_cat_1.message( 1 ).c_str() );
67 }
68
69 {
70 char buffer[ 4 ];
71 BOOST_TEST_CSTR_EQ( s_cat_1.message( 1, buffer, sizeof( buffer ) ), "use" );
72 }
73
74 // ==
75
76 BOOST_TEST_NOT( s_cat_1 == s_cat_2 );
77 BOOST_TEST( s_cat_1 != s_cat_2 );
78
79 return boost::report_errors();
80}
81

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