failed_test.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright 2018 Peter Dimov.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // Avoid spurious VC++ warnings
  4. #define _CRT_SECURE_NO_WARNINGS
  5. #include <boost/system/error_code.hpp>
  6. #include <boost/core/lightweight_test.hpp>
  7. #include <cstdio>
  8. using namespace boost::system;
  9. struct http_category_impl: public error_category
  10. {
  11. // clang++ 3.8 and below: initialization of const object
  12. // requires a user-provided default constructor
  13. BOOST_SYSTEM_CONSTEXPR http_category_impl() BOOST_NOEXCEPT
  14. {
  15. }
  16. char const * name() const BOOST_NOEXCEPT
  17. {
  18. return "http";
  19. }
  20. std::string message( int ev ) const
  21. {
  22. char buffer[ 32 ];
  23. std::sprintf( buffer, "HTTP/1.0 %d", ev );
  24. return buffer;
  25. }
  26. bool failed( int ev ) const BOOST_NOEXCEPT
  27. {
  28. return !( ev >= 200 && ev < 300 );
  29. }
  30. };
  31. error_category const & http_category()
  32. {
  33. static const http_category_impl instance;
  34. return instance;
  35. }
  36. #define TEST_NOT_FAILED(ec) BOOST_TEST( !ec.failed() ); BOOST_TEST( ec? false: true ); BOOST_TEST( !ec );
  37. #define TEST_FAILED(ec) BOOST_TEST( ec.failed() ); BOOST_TEST( ec ); BOOST_TEST( !!ec );
  38. template<class Ec> void test()
  39. {
  40. {
  41. Ec ec;
  42. TEST_NOT_FAILED( ec );
  43. ec.assign( 1, generic_category() );
  44. TEST_FAILED( ec );
  45. ec.clear();
  46. TEST_NOT_FAILED( ec );
  47. ec = Ec( 1, generic_category() );
  48. TEST_FAILED( ec );
  49. ec = Ec();
  50. TEST_NOT_FAILED( ec );
  51. }
  52. {
  53. Ec ec;
  54. TEST_NOT_FAILED( ec );
  55. ec.assign( 1, system_category() );
  56. TEST_FAILED( ec );
  57. ec.clear();
  58. TEST_NOT_FAILED( ec );
  59. ec = Ec( 1, system_category() );
  60. TEST_FAILED( ec );
  61. ec = Ec();
  62. TEST_NOT_FAILED( ec );
  63. }
  64. {
  65. Ec ec( 0, generic_category() );
  66. TEST_NOT_FAILED( ec );
  67. ec.assign( 1, system_category() );
  68. TEST_FAILED( ec );
  69. ec = Ec( 0, system_category() );
  70. TEST_NOT_FAILED( ec );
  71. }
  72. {
  73. Ec ec( 1, generic_category() );
  74. TEST_FAILED( ec );
  75. ec.assign( 0, system_category() );
  76. TEST_NOT_FAILED( ec );
  77. }
  78. {
  79. Ec ec( 0, system_category() );
  80. TEST_NOT_FAILED( ec );
  81. ec.assign( 1, generic_category() );
  82. TEST_FAILED( ec );
  83. ec = Ec( 0, generic_category() );
  84. TEST_NOT_FAILED( ec );
  85. }
  86. {
  87. Ec ec( 1, system_category() );
  88. TEST_FAILED( ec );
  89. ec.assign( 0, generic_category() );
  90. TEST_NOT_FAILED( ec );
  91. }
  92. {
  93. Ec ec( 0, http_category() );
  94. BOOST_TEST( ec.failed() );
  95. ec.assign( 200, http_category() );
  96. BOOST_TEST( !ec.failed() );
  97. ec = Ec( 404, http_category() );
  98. BOOST_TEST( ec.failed() );
  99. }
  100. }
  101. int main()
  102. {
  103. BOOST_TEST( !generic_category().failed( 0 ) );
  104. BOOST_TEST( generic_category().failed( 7 ) );
  105. BOOST_TEST( !system_category().failed( 0 ) );
  106. BOOST_TEST( system_category().failed( 7 ) );
  107. BOOST_TEST( http_category().failed( 0 ) );
  108. BOOST_TEST( !http_category().failed( 200 ) );
  109. BOOST_TEST( http_category().failed( 404 ) );
  110. test<error_code>();
  111. test<error_condition>();
  112. {
  113. error_condition ec( errc::success );
  114. TEST_NOT_FAILED( ec );
  115. ec = errc::address_family_not_supported;
  116. TEST_FAILED( ec );
  117. }
  118. {
  119. error_condition ec( errc::address_family_not_supported );
  120. TEST_FAILED( ec );
  121. ec = errc::success;
  122. TEST_NOT_FAILED( ec );
  123. }
  124. {
  125. error_code ec( make_error_code( errc::success ) );
  126. TEST_NOT_FAILED( ec );
  127. ec = make_error_code( errc::address_family_not_supported );
  128. TEST_FAILED( ec );
  129. }
  130. {
  131. error_code ec( make_error_code( errc::address_family_not_supported ) );
  132. TEST_FAILED( ec );
  133. ec = make_error_code( errc::success );
  134. TEST_NOT_FAILED( ec );
  135. }
  136. return boost::report_errors();
  137. }