1
2// Copyright 2018, 2020 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_category.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 ).value() == 1 );
48 BOOST_TEST( s_cat_1.default_error_condition( 1 ).category() == s_cat_1 );
49
50 BOOST_TEST( s_cat_2.default_error_condition( 2 ).value() == 2 );
51 BOOST_TEST( s_cat_2.default_error_condition( 2 ).category() == s_cat_2 );
52
53 // equivalent
54
55 BOOST_TEST( s_cat_1.equivalent( 1, s_cat_1.default_error_condition( 1 ) ) );
56 BOOST_TEST( !s_cat_1.equivalent( 1, s_cat_1.default_error_condition( 2 ) ) );
57 BOOST_TEST( !s_cat_1.equivalent( 1, s_cat_2.default_error_condition( 2 ) ) );
58
59 // message
60
61 {
62 char buffer[ 256 ];
63 BOOST_TEST_CSTR_EQ( s_cat_1.message( 1, buffer, sizeof( buffer ) ), s_cat_1.message( 1 ).c_str() );
64 }
65
66 {
67 char buffer[ 4 ];
68 BOOST_TEST_CSTR_EQ( s_cat_1.message( 1, buffer, sizeof( buffer ) ), "use" );
69 }
70
71 // ==
72
73 BOOST_TEST_NOT( s_cat_1 == s_cat_2 );
74 BOOST_TEST( s_cat_1 != s_cat_2 );
75
76 return boost::report_errors();
77}
78

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