]> git.proxmox.com Git - ceph.git/blobdiff - ceph/src/boost/libs/system/test/error_code_test.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / system / test / error_code_test.cpp
index a9a928ef3bfa5a08ee14c5625267bf7865563c1d..7965541baafe0a10dcf8fae219286132301fc81b 100644 (file)
@@ -9,9 +9,6 @@
 
 //----------------------------------------------------------------------------//
 
-//  test without deprecated features
-#define BOOST_SYSTEM_NO_DEPRECATED
-
 #include <boost/config/warning_disable.hpp>
 
 #include <boost/detail/lightweight_test.hpp>
 //  with a boost::system using directive increases use scenario coverage.
 using namespace boost::system;
 
-# if defined( BOOST_WINDOWS_API )
-#   include "winerror.h"
-#   define BOOST_ACCESS_ERROR_MACRO ERROR_ACCESS_DENIED
-# elif defined( BOOST_POSIX_API )
-#   define BOOST_ACCESS_ERROR_MACRO EACCES
-# else
-#   error "Only supported for POSIX and Windows"
-# endif
+#if defined( BOOST_WINDOWS_API )
+// Neither MinGW or Cygwin versions of winerror.h work if used alone, so on
+// either of those platforms include the full windows.h
+#  if defined(__MINGW32__) || defined(__CYGWIN__)
+#    include <windows.h>
+#  else
+#    include <winerror.h>
+#  endif
+#  define BOOST_ACCESS_ERROR_MACRO ERROR_ACCESS_DENIED
+#elif defined( BOOST_POSIX_API )
+#  define BOOST_ACCESS_ERROR_MACRO EACCES
+#else
+#  error "Only supported for POSIX and Windows"
+#endif
 
 namespace
 {
@@ -50,6 +53,53 @@ namespace
     ss >> s;
     BOOST_TEST( s == expected );
   }
+
+  //  throws_function_test  ------------------------------------------------------------//
+
+  //  usage example
+
+  int divide(int dividend, int divisor, boost::system::error_code& ec = boost::throws())
+  {
+    if (divisor == 0)                            // is there an error?
+    {
+      if (&ec == &boost::throws())               // throw on error
+        throw "oops!";                           // whatever exception you prefer
+      ec = error_code(EDOM, generic_category()); // report error via error_code
+      return 0;
+    }
+    
+    if (&ec != &boost::throws())                 // error reporting via error_code
+      ec.clear();
+    return dividend / divisor;
+  }
+
+  //  test usage example
+
+  void test_throws_usage()
+  {
+    std::cout << "Test throws() example and usage...\n";
+    error_code ec;
+
+    // no error tests
+    BOOST_TEST_EQ((divide(10, 2)), 5);        // no error, report via exception
+    ec = make_error_code(errc::argument_out_of_domain);
+    BOOST_TEST_EQ((divide(10, 5, ec)), 2);    // no error, report via error_code
+    BOOST_TEST(!ec);
+
+    ec = make_error_code(errc::argument_out_of_domain);
+    BOOST_TEST_EQ((divide(10, 0, ec)), 0);    // error, report via error_code
+    BOOST_TEST(ec);
+
+    bool exception_thrown = false;            
+    try
+      { divide(10, 0); }                      // error, report via exception
+    catch (...)
+      { exception_thrown = true; }            
+    BOOST_TEST(exception_thrown);
+
+    //error_code should_fail(boost::throws());     // should fail at runtime
+    //boost::throws() = ec;                        // should fail at runtime
+  }
 }
 
 //  main  ------------------------------------------------------------------------------//
@@ -196,6 +246,8 @@ int main( int, char ** )
   BOOST_TEST( econd.message() != "" );
   BOOST_TEST( econd.message().substr( 0, 13) != "Unknown error" );
 
+  test_throws_usage();
+
 #ifdef BOOST_WINDOWS_API
   std::cout << "Windows tests...\n";
   // these tests probe the Windows errc decoder