• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // Avoid spurious VC++ warnings
12 # define _CRT_SECURE_NO_WARNINGS
13 
14 #include <boost/system/error_code.hpp>
15 #include <boost/core/lightweight_test.hpp>
16 #include <cstdio>
17 
18 //
19 
20 namespace sys = boost::system;
21 
22 class user_category: public sys::error_category
23 {
24 public:
25 
name() const26     virtual const char * name() const BOOST_NOEXCEPT
27     {
28         return "user";
29     }
30 
message(int ev) const31     virtual std::string message( int ev ) const
32     {
33         char buffer[ 256 ];
34         std::sprintf( buffer, "user message %d", ev );
35 
36         return buffer;
37     }
38 
39     using sys::error_category::message;
40 };
41 
42 static user_category s_cat_1;
43 static user_category s_cat_2;
44 
main()45 int main()
46 {
47     // default_error_condition
48 
49     BOOST_TEST( s_cat_1.default_error_condition( 1 ) == sys::error_condition( 1, s_cat_1 ) );
50     BOOST_TEST( s_cat_2.default_error_condition( 2 ) == sys::error_condition( 2, s_cat_2 ) );
51 
52     // equivalent
53 
54     BOOST_TEST( s_cat_1.equivalent( 1, sys::error_condition( 1, s_cat_1 ) ) );
55     BOOST_TEST( !s_cat_1.equivalent( 1, sys::error_condition( 2, s_cat_1 ) ) );
56     BOOST_TEST( !s_cat_1.equivalent( 1, sys::error_condition( 2, s_cat_2 ) ) );
57 
58     // the other equivalent
59 
60     BOOST_TEST( s_cat_1.equivalent( sys::error_code( 1, s_cat_1 ), 1 ) );
61     BOOST_TEST( !s_cat_1.equivalent( sys::error_code( 1, s_cat_1 ), 2 ) );
62     BOOST_TEST( !s_cat_1.equivalent( sys::error_code( 1, s_cat_2 ), 1 ) );
63 
64     // message
65 
66     {
67         char buffer[ 256 ];
68         BOOST_TEST_CSTR_EQ( s_cat_1.message( 1, buffer, sizeof( buffer ) ), s_cat_1.message( 1 ).c_str() );
69     }
70 
71     {
72         char buffer[ 4 ];
73         BOOST_TEST_CSTR_EQ( s_cat_1.message( 1, buffer, sizeof( buffer ) ), "use" );
74     }
75 
76     // ==
77 
78     BOOST_TEST_NOT( s_cat_1 == s_cat_2 );
79     BOOST_TEST( s_cat_1 != s_cat_2 );
80 
81     return boost::report_errors();
82 }
83