• 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 <cstring>
17 #include <cstdio>
18 
19 //
20 
21 #if defined(BOOST_WINDOWS_API) && defined(BOOST_SYSTEM_USE_UTF8)
22 
23 #include <boost/config/pragma_message.hpp>
24 
25 BOOST_PRAGMA_MESSAGE( "Skipping test due to BOOST_WINDOWS_API && BOOST_SYSTEM_USE_UTF8" )
26 
main()27 int main() {}
28 
29 #else
30 
31 #if defined(BOOST_WINDOWS_API)
32 
33 #include <windows.h>
34 
sys_strerror(int ev)35 std::string sys_strerror( int ev )
36 {
37     void * lpMsgBuf = 0;
38 
39     DWORD retval = FormatMessageA(
40         FORMAT_MESSAGE_ALLOCATE_BUFFER |
41         FORMAT_MESSAGE_FROM_SYSTEM |
42         FORMAT_MESSAGE_IGNORE_INSERTS,
43         NULL,
44         ev,
45         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
46         (LPSTR) &lpMsgBuf,
47         0,
48         NULL
49     );
50 
51     struct local_free
52     {
53         void * p_;
54 
55         ~local_free()
56         {
57             LocalFree( p_ );
58         }
59     };
60 
61     local_free lf_ = { lpMsgBuf };
62 
63     if( retval == 0 )
64     {
65         char buffer[ 38 ];
66 
67         std::sprintf( buffer, "Unknown error (%d)", ev );
68         return buffer;
69     }
70 
71     std::string str( static_cast<char const*>( lpMsgBuf ) );
72 
73     while( !str.empty() && (str[str.size()-1] == '\n' || str[str.size()-1] == '\r') )
74     {
75         str.erase( str.size()-1 );
76     }
77 
78     if( !str.empty() && str[str.size()-1] == '.' )
79     {
80         str.erase( str.size()-1 );
81     }
82 
83     return str;
84 }
85 
86 #else
87 
sys_strerror(int ev)88 std::string sys_strerror( int ev )
89 {
90     return std::strerror( ev );
91 }
92 
93 #endif
94 
95 //
96 
97 namespace sys = boost::system;
98 
test_message(sys::error_category const & cat,int ev)99 static void test_message( sys::error_category const & cat, int ev )
100 {
101     BOOST_TEST_EQ( cat.message( ev ), sys_strerror( ev ) );
102 
103     char buffer[ 4096 ]; // yes, really
104     BOOST_TEST_CSTR_EQ( cat.message( ev, buffer, sizeof( buffer ) ), sys_strerror( ev ).c_str() );
105 }
106 
main()107 int main()
108 {
109     sys::error_category const & cat = sys::system_category();
110 
111     // message
112 
113     for( int i = -2; i < 16000; ++i )
114     {
115         test_message( cat, i );
116     }
117 
118     return boost::report_errors();
119 }
120 
121 #endif // #if defined(BOOST_WINDOWS_API) && defined(BOOST_SYSTEM_USE_UTF8)
122