system_error_test.cpp 3.4 KB

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