1// system_error_test.cpp ---------------------------------------------------//
2
3// Copyright Beman Dawes 2006
4// Copyright (c) Microsoft Corporation 2014
5// Distributed under the Boost Software License, Version 1.0. (See accompanying
6// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7
8// See library home page at http://www.boost.org/libs/system
9
10//----------------------------------------------------------------------------//
11
12#include <boost/config/warning_disable.hpp>
13
14#include <boost/detail/lightweight_test.hpp>
15#include <boost/system/system_error.hpp>
16#include <iostream>
17#include <string>
18
19#ifdef BOOST_WINDOWS_API
20#include <windows.h>
21#endif
22
23using boost::system::system_error;
24using boost::system::error_code;
25using boost::system::system_category;
26using std::string;
27
28#define TEST(x,v,w) test(#x,x,v,w)
29
30namespace
31{
32 void test( const char * desc, const system_error & ex,
33 int v, const char * str )
34 {
35 std::cout << "test " << desc << "\n what() returns \"" << ex.what() << "\"\n";
36 BOOST_TEST( ex.code().value() == v );
37 BOOST_TEST( ex.code().category() == system_category() );
38# ifdef BOOST_WINDOWS_API
39 LANGID language_id;
40# if !BOOST_PLAT_WINDOWS_RUNTIME
41 language_id = ::GetUserDefaultUILanguage();
42# else
43 language_id = 0x0409; // Assume US English
44# endif
45 // std::cout << "GetUserDefaultUILanguage() returns " << language_id << '\n';
46 if ( language_id == 0x0409 ) // English (United States)
47 {
48 BOOST_TEST( std::string( ex.what() ) == str );
49 if ( std::string( ex.what() ) != str )
50 std::cout << "expected \"" << str << "\", but what() returned \""
51 << ex.what() << "\"\n";
52 }
53# endif
54 (void)str;
55 }
56
57 const boost::uint_least32_t uvalue = 2u;
58}
59
60int main( int, char *[] )
61{
62 // all constructors, in the same order as they appear in the header:
63
64 system_error c1_0( error_code(0, system_category()) );
65 system_error c1_1( error_code(1, system_category()) );
66 system_error c1_2u( error_code(uvalue, system_category()) );
67
68 system_error c2_0( error_code(0, system_category()), string("c2_0") );
69 system_error c2_1( error_code(1, system_category()), string("c2_1") );
70
71 system_error c3_0( error_code(0, system_category()), "c3_0" );
72 system_error c3_1( error_code(1, system_category()), "c3_1" );
73
74 system_error c4_0( 0, system_category() );
75 system_error c4_1( 1, system_category() );
76 system_error c4_2u( uvalue, system_category() );
77
78 system_error c5_0( 0, system_category(), string("c5_0") );
79 system_error c5_1( 1, system_category(), string("c5_1") );
80
81 system_error c6_0( 0, system_category(), "c6_0" );
82 system_error c6_1( 1, system_category(), "c6_1" );
83
84 TEST( c1_0, 0, "The operation completed successfully [system:0]" );
85 TEST( c1_1, 1, "Incorrect function [system:1]" );
86 TEST( c1_2u, 2, "The system cannot find the file specified [system:2]" );
87
88 TEST( c2_0, 0, "c2_0: The operation completed successfully [system:0]" );
89 TEST( c2_1, 1, "c2_1: Incorrect function [system:1]" );
90
91 TEST( c3_0, 0, "c3_0: The operation completed successfully [system:0]" );
92 TEST( c3_1, 1, "c3_1: Incorrect function [system:1]" );
93
94 TEST( c4_0, 0, "The operation completed successfully [system:0]" );
95 TEST( c4_1, 1, "Incorrect function [system:1]" );
96 TEST( c4_2u, 2, "The system cannot find the file specified [system:2]" );
97
98 TEST( c5_0, 0, "c5_0: The operation completed successfully [system:0]" );
99 TEST( c5_1, 1, "c5_1: Incorrect function [system:1]" );
100
101 TEST( c6_0, 0, "c6_0: The operation completed successfully [system:0]" );
102 TEST( c6_1, 1, "c6_1: Incorrect function [system:1]" );
103
104 return ::boost::report_errors();
105}
106

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