forked from boostorg/system
Merge system and filesystem branches, bringing them in sync with N2415. Several filesystem bugs fixed, and current_path setter added.
[SVN r39173]
This commit is contained in:
1 parent
1656e087bb
commit
04e774f337
12 files changed
+1466
-551
No files matched your search
@@ -38,4 +38,7 @@ project
|
||||
: : : <define>BOOST_SYSTEM_DYN_LINK
|
||||
: system_error_test_dll
|
||||
]
|
||||
[ run header_only_test.cpp
|
||||
: : : <runtime-link>static <link>static
|
||||
]
|
||||
;
|
||||
+144
-50
@@ -10,7 +10,7 @@
|
||||
//----------------------------------------------------------------------------//
|
||||
|
||||
// VC++ 8.0 warns on usage of certain Standard Library and API functions that
|
||||
// can be cause buffer overruns or other possible security issues if misused.
|
||||
// can cause buffer overruns or other possible security issues if misused.
|
||||
// See http://msdn.microsoft.com/msdnmag/issues/05/05/SafeCandC/default.aspx
|
||||
// But the wording of the warning is misleading and unsettling, there are no
|
||||
// portable alternative functions, and VC++ 8.0's own libraries use the
|
||||
@@ -20,6 +20,11 @@
|
||||
|
||||
#include <boost/test/minimal.hpp>
|
||||
#include <boost/system/error_code.hpp>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
#include <boost/cerrno.hpp>
|
||||
|
||||
// Although using directives are not the best programming practice, testing
|
||||
// with a boost::system using directive increases use scenario coverage.
|
||||
@@ -27,86 +32,175 @@ using namespace boost::system;
|
||||
|
||||
# if defined( BOOST_WINDOWS_API )
|
||||
# include "winerror.h"
|
||||
# include <boost/cerrno.hpp>
|
||||
# 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
|
||||
{
|
||||
void check_ostream( error_code ec, const char * expected )
|
||||
{
|
||||
std::stringstream ss;
|
||||
std::string s;
|
||||
|
||||
ss << ec;
|
||||
ss >> s;
|
||||
BOOST_CHECK( s == expected );
|
||||
}
|
||||
}
|
||||
|
||||
// test_main ---------------------------------------------------------------//
|
||||
|
||||
// TODO: supply a build jam file
|
||||
// TODO: supply a test jam file
|
||||
// TODO: same for bjam v2
|
||||
// TODO: add message decoder tests
|
||||
// TODO: add hash_value tests
|
||||
|
||||
int test_main( int, char ** )
|
||||
{
|
||||
error_code ec;
|
||||
error_code ec_0_native( 0, native_ecat );
|
||||
error_code ec_0_errno( 0, errno_ecat );
|
||||
error_code ec_1_native( 1, native_ecat );
|
||||
error_code ec_1_errno( 1, errno_ecat );
|
||||
|
||||
std::cout << "General tests...\n";
|
||||
// unit tests:
|
||||
|
||||
BOOST_CHECK( posix_category == posix_category );
|
||||
BOOST_CHECK( system_category == system_category );
|
||||
BOOST_CHECK( posix_category != system_category );
|
||||
BOOST_CHECK( system_category != posix_category );
|
||||
BOOST_CHECK( posix_category < system_category );
|
||||
BOOST_CHECK( !(system_category < posix_category) );
|
||||
|
||||
error_code ec;
|
||||
error_condition dec;
|
||||
BOOST_CHECK( !ec );
|
||||
BOOST_CHECK( ec.value() == 0 );
|
||||
BOOST_CHECK( ec.to_errno() == 0 );
|
||||
dec = ec.default_error_condition();
|
||||
BOOST_CHECK( dec.value() == 0 );
|
||||
BOOST_CHECK( dec.category() == posix_category );
|
||||
BOOST_CHECK( ec == posix::success );
|
||||
BOOST_CHECK( ec.category() == system_category );
|
||||
BOOST_CHECK( std::strcmp( ec.category().name(), "system") == 0 );
|
||||
BOOST_CHECK( !(ec < error_code( 0, system_category )) );
|
||||
BOOST_CHECK( !(error_code( 0, system_category ) < ec) );
|
||||
BOOST_CHECK( ec < error_code( 1, system_category ) );
|
||||
BOOST_CHECK( !(error_code( 1, system_category ) < ec) );
|
||||
|
||||
BOOST_CHECK( !ec_0_native );
|
||||
BOOST_CHECK( ec_0_native.value() == 0 );
|
||||
BOOST_CHECK( ec_0_native.to_errno() == 0 );
|
||||
error_code ec_0_system( 0, system_category );
|
||||
BOOST_CHECK( !ec_0_system );
|
||||
BOOST_CHECK( ec_0_system.value() == 0 );
|
||||
dec = ec_0_system.default_error_condition();
|
||||
BOOST_CHECK( dec.value() == 0 );
|
||||
BOOST_CHECK( dec.category() == posix_category );
|
||||
BOOST_CHECK( ec_0_system == posix::success );
|
||||
BOOST_CHECK( ec_0_system.category() == system_category );
|
||||
BOOST_CHECK( std::strcmp( ec_0_system.category().name(), "system") == 0 );
|
||||
check_ostream( ec_0_system, "system:0" );
|
||||
|
||||
BOOST_CHECK( !ec_0_errno );
|
||||
BOOST_CHECK( ec_0_errno.value() == 0 );
|
||||
BOOST_CHECK( ec_0_errno.to_errno() == 0 );
|
||||
BOOST_CHECK( ec == ec_0_errno );
|
||||
BOOST_CHECK( native_ecat != errno_ecat || ec_0_native == ec_0_errno );
|
||||
BOOST_CHECK( native_ecat == errno_ecat || ec_0_native != ec_0_errno );
|
||||
BOOST_CHECK( ec_0_system == ec );
|
||||
|
||||
BOOST_CHECK( ec_1_native );
|
||||
BOOST_CHECK( ec_1_native.value() == 1 );
|
||||
BOOST_CHECK( ec_1_native.value() != 0 );
|
||||
BOOST_CHECK( ec_1_native.to_errno() != 0 );
|
||||
BOOST_CHECK( ec != ec_1_native );
|
||||
BOOST_CHECK( ec_0_native != ec_1_native );
|
||||
BOOST_CHECK( ec_0_errno != ec_1_native );
|
||||
error_code ec_1_system( 1, system_category );
|
||||
BOOST_CHECK( ec_1_system );
|
||||
BOOST_CHECK( ec_1_system.value() == 1 );
|
||||
BOOST_CHECK( ec_1_system.value() != 0 );
|
||||
BOOST_CHECK( ec != ec_1_system );
|
||||
BOOST_CHECK( ec_0_system != ec_1_system );
|
||||
check_ostream( ec_1_system, "system:1" );
|
||||
|
||||
BOOST_CHECK( ec_1_errno );
|
||||
BOOST_CHECK( ec_1_errno.value() == 1 );
|
||||
BOOST_CHECK( ec_1_errno.to_errno() == 1 );
|
||||
BOOST_CHECK( ec_1_errno.to_errno() != 0 );
|
||||
BOOST_CHECK( ec != ec_1_errno );
|
||||
BOOST_CHECK( ec_0_native != ec_1_errno );
|
||||
BOOST_CHECK( ec_0_errno != ec_1_errno );
|
||||
ec = error_code( BOOST_ACCESS_ERROR_MACRO, system_category );
|
||||
BOOST_CHECK( ec );
|
||||
BOOST_CHECK( ec.value() == BOOST_ACCESS_ERROR_MACRO );
|
||||
dec = ec.default_error_condition();
|
||||
BOOST_CHECK( dec.value() == static_cast<int>(posix::permission_denied) );
|
||||
BOOST_CHECK( dec.category() == posix_category );
|
||||
BOOST_CHECK( dec == error_condition( posix::permission_denied, posix_category ) );
|
||||
BOOST_CHECK( dec == posix::permission_denied );
|
||||
BOOST_CHECK( posix::permission_denied == dec );
|
||||
BOOST_CHECK( ec == posix::permission_denied );
|
||||
BOOST_CHECK( ec.category() == system_category );
|
||||
BOOST_CHECK( std::strcmp( ec.category().name(), "system") == 0 );
|
||||
|
||||
// test the explicit make_error_code conversion for posix
|
||||
ec = make_error_code( posix::bad_message );
|
||||
BOOST_CHECK( ec );
|
||||
BOOST_CHECK( ec == posix::bad_message );
|
||||
BOOST_CHECK( posix::bad_message == ec );
|
||||
BOOST_CHECK( ec != posix::permission_denied );
|
||||
BOOST_CHECK( posix::permission_denied != ec );
|
||||
BOOST_CHECK( ec.category() == posix_category );
|
||||
|
||||
// test the deprecated predefined error_category synonyms
|
||||
BOOST_CHECK( &system_category == &native_ecat );
|
||||
BOOST_CHECK( &posix_category == &errno_ecat );
|
||||
BOOST_CHECK( system_category == native_ecat );
|
||||
BOOST_CHECK( posix_category == errno_ecat );
|
||||
|
||||
#ifdef BOOST_WINDOWS_API
|
||||
BOOST_CHECK( ec != ec_0_native );
|
||||
|
||||
// these tests probe the Windows to_errno decoder
|
||||
std::cout << "Windows tests...\n";
|
||||
// these tests probe the Windows posix decoder
|
||||
// test the first entry in the decoder table:
|
||||
ec = error_code( ERROR_FILE_NOT_FOUND, native_ecat );
|
||||
ec = error_code( ERROR_FILE_NOT_FOUND, system_category );
|
||||
BOOST_CHECK( ec.value() == ERROR_FILE_NOT_FOUND );
|
||||
BOOST_CHECK( ec.to_errno() == ENOENT );
|
||||
BOOST_CHECK( ec == posix::no_such_file_or_directory );
|
||||
BOOST_CHECK( ec.default_error_condition().value() == posix::no_such_file_or_directory );
|
||||
BOOST_CHECK( ec.default_error_condition().category() == posix_category );
|
||||
|
||||
// test the second entry in the decoder table:
|
||||
ec = error_code( ERROR_PATH_NOT_FOUND, native_ecat );
|
||||
ec = error_code( ERROR_PATH_NOT_FOUND, system_category );
|
||||
BOOST_CHECK( ec.value() == ERROR_PATH_NOT_FOUND );
|
||||
BOOST_CHECK( ec.to_errno() == ENOENT );
|
||||
BOOST_CHECK( ec == posix::no_such_file_or_directory );
|
||||
BOOST_CHECK( ec.default_error_condition().value() == posix::no_such_file_or_directory );
|
||||
BOOST_CHECK( ec.default_error_condition().category() == posix_category );
|
||||
|
||||
// test the third entry in the decoder table:
|
||||
ec = error_code( ERROR_ACCESS_DENIED, native_ecat );
|
||||
ec = error_code( ERROR_ACCESS_DENIED, system_category );
|
||||
BOOST_CHECK( ec.value() == ERROR_ACCESS_DENIED );
|
||||
BOOST_CHECK( ec.to_errno() == EACCES );
|
||||
BOOST_CHECK( ec == posix::permission_denied );
|
||||
BOOST_CHECK( ec.default_error_condition().value() == posix::permission_denied );
|
||||
BOOST_CHECK( ec.default_error_condition().category() == posix_category );
|
||||
|
||||
// test the last regular entry in the decoder table:
|
||||
ec = error_code( ERROR_WRITE_PROTECT, native_ecat );
|
||||
ec = error_code( ERROR_WRITE_PROTECT, system_category );
|
||||
BOOST_CHECK( ec.value() == ERROR_WRITE_PROTECT );
|
||||
BOOST_CHECK( ec.to_errno() == EROFS );
|
||||
BOOST_CHECK( ec == posix::permission_denied );
|
||||
BOOST_CHECK( ec.default_error_condition().value() == posix::permission_denied );
|
||||
BOOST_CHECK( ec.default_error_condition().category() == posix_category );
|
||||
|
||||
// test not-in-table condition:
|
||||
ec = error_code( 1234567890, native_ecat );
|
||||
ec = error_code( 1234567890, system_category );
|
||||
BOOST_CHECK( ec.value() == 1234567890 );
|
||||
BOOST_CHECK( ec.to_errno() == EOTHER );
|
||||
BOOST_CHECK( ec.default_error_condition().value() == 1234567890 );
|
||||
BOOST_CHECK( ec.default_error_condition().category() == system_category );
|
||||
|
||||
#else // POSIX
|
||||
|
||||
std::cout << "POSIX tests...\n";
|
||||
ec = error_code( EACCES, system_category );
|
||||
BOOST_CHECK( ec == error_code( posix::permission_denied, system_category ) );
|
||||
BOOST_CHECK( error_code( posix::permission_denied, system_category ) == ec );
|
||||
BOOST_CHECK( ec == posix::permission_denied );
|
||||
BOOST_CHECK( posix::permission_denied == ec );
|
||||
BOOST_CHECK( ec.default_error_condition().value() == posix::permission_denied );
|
||||
BOOST_CHECK( ec.default_error_condition().category() == posix_category );
|
||||
|
||||
# ifdef __CYGWIN__
|
||||
|
||||
std::cout << "Cygwin tests...\n";
|
||||
ec = cygwin::no_package;
|
||||
BOOST_CHECK( ec == cygwin::no_package );
|
||||
BOOST_CHECK( ec == error_code( ENOPKG, system_category ) );
|
||||
BOOST_CHECK( ec == error_code( cygwin::no_package, system_category ) );
|
||||
BOOST_CHECK( ec.default_error_condition().category() == system_category );
|
||||
|
||||
# elif defined(linux) || defined(__linux) || defined(__linux__)
|
||||
|
||||
std::cout << "Linux tests...\n";
|
||||
ec = Linux::dot_dot_error;
|
||||
BOOST_CHECK( ec == Linux::dot_dot_error );
|
||||
BOOST_CHECK( ec == error_code( EDOTDOT, system_category ) );
|
||||
BOOST_CHECK( ec == error_code( Linux::dot_dot_error, system_category ) );
|
||||
BOOST_CHECK( ec.default_error_condition().category() == posix_category );
|
||||
|
||||
# endif
|
||||
|
||||
#else
|
||||
BOOST_CHECK( ec == ec_0_native );
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
|
||||
+366
-67
@@ -8,85 +8,384 @@
|
||||
// See library home page at http://www.boost.org/libs/system
|
||||
|
||||
// ------------------------------------------------------------------------ //
|
||||
// This code demonstrates creation and use of a new category of error codes.
|
||||
|
||||
// The motivation was a Boost posting by Christopher Kohlhoff on June 28, 2006.
|
||||
// This program demonstrates creation and use of new categories of error
|
||||
// codes. Several scenarios are demonstrated and tested.
|
||||
|
||||
// Motivation was a Boost posting by Christopher Kohlhoff on June 28, 2006.
|
||||
|
||||
#include <boost/system/error_code.hpp>
|
||||
#include <boost/cerrno.hpp>
|
||||
|
||||
// ------------------------------------------------------------------------ //
|
||||
|
||||
// header asio.hpp
|
||||
|
||||
#define BOO_BOO 12345 // this could also be a constant; a macro is used for
|
||||
// illustration because many older API's define errors
|
||||
// via macro.
|
||||
namespace boost
|
||||
{
|
||||
namespace asio
|
||||
{
|
||||
// asio declares have its own error_category:
|
||||
extern system::error_category asio_error;
|
||||
|
||||
namespace error
|
||||
{
|
||||
extern boost::system::error_code boo_boo;
|
||||
}
|
||||
|
||||
void boo_boo( boost::system::error_code & ec );
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------ //
|
||||
|
||||
// implementation file asio.cpp:
|
||||
|
||||
namespace boost
|
||||
{
|
||||
namespace asio
|
||||
{
|
||||
|
||||
system::error_category asio_error = system::error_code::new_category();
|
||||
|
||||
namespace error
|
||||
{
|
||||
boost::system::error_code boo_boo( BOO_BOO, asio_error );
|
||||
}
|
||||
|
||||
// function sets ec arg to boo_boo
|
||||
void boo_boo( boost::system::error_code & ec )
|
||||
{
|
||||
ec = error::boo_boo;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------ //
|
||||
|
||||
// a user program:
|
||||
|
||||
|
||||
// #include <asio.hpp>
|
||||
#include <string>
|
||||
#include <cstdio>
|
||||
#include <boost/test/minimal.hpp>
|
||||
|
||||
#ifdef BOOST_POSIX_API
|
||||
# include <sys/stat.h>
|
||||
#else
|
||||
# include <windows.h>
|
||||
#endif
|
||||
|
||||
// ------------------------------------------------------------------------ //
|
||||
|
||||
// Library 1: User function passes through an error code from the
|
||||
// operating system.
|
||||
|
||||
|
||||
boost::system::error_code my_mkdir( const std::string & path )
|
||||
{
|
||||
return boost::system::error_code(
|
||||
# ifdef BOOST_POSIX_API
|
||||
::mkdir( path.c_str(), S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH ) == 0 ? 0 : errno,
|
||||
# else
|
||||
::CreateDirectoryA( path.c_str(), 0 ) != 0 ? 0 : ::GetLastError(),
|
||||
# endif
|
||||
boost::system::system_category );
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------ //
|
||||
|
||||
// Library 2: User function passes through errno from the C-runtime.
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
boost::system::error_code my_remove( const std::string & path )
|
||||
{
|
||||
return boost::system::error_code(
|
||||
::remove( path.c_str() ) == 0 ? 0 : errno,
|
||||
boost::system::posix_category ); // OK for both Windows and POSIX
|
||||
// Alternatively, could use posix_category
|
||||
// on Windows and system_category on
|
||||
// POSIX-based systems.
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------ //
|
||||
|
||||
// Library 3: Library uses enum to identify library specific errors.
|
||||
|
||||
// This particular example is for a library within the parent namespace. For
|
||||
// an example of a library not within the parent namespace, see library 4.
|
||||
|
||||
// header lib3.hpp:
|
||||
|
||||
namespace boost
|
||||
{
|
||||
namespace lib3
|
||||
{
|
||||
// lib3 has its own error_category:
|
||||
extern const boost::system::error_category & lib3_error_category;
|
||||
|
||||
enum error
|
||||
{
|
||||
boo_boo=123,
|
||||
big_boo_boo
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
namespace system
|
||||
{
|
||||
template<> struct is_error_code_enum<boost::lib3::error>
|
||||
{ static const bool value = true; };
|
||||
|
||||
inline error_code make_error_code(boost::lib3::error e)
|
||||
{ return error_code(e,boost::lib3::lib3_error_category); }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// implementation file lib3.cpp:
|
||||
|
||||
// #include <lib3.hpp>
|
||||
|
||||
namespace boost
|
||||
{
|
||||
namespace lib3
|
||||
{
|
||||
class lib3_error_category_imp : public boost::system::error_category
|
||||
{
|
||||
public:
|
||||
const char * name() const
|
||||
{
|
||||
return "lib3";
|
||||
}
|
||||
|
||||
boost::system::error_condition default_error_condition( int ev ) const
|
||||
{
|
||||
return ev == boo_boo
|
||||
? boost::system::error_condition( boost::system::posix::io_error,
|
||||
boost::system::posix_category )
|
||||
: boost::system::error_condition( ev,
|
||||
boost::lib3::lib3_error_category );
|
||||
}
|
||||
|
||||
std::string message( int ev ) const
|
||||
{
|
||||
if ( ev == boo_boo ) return std::string("boo boo");
|
||||
if ( ev == big_boo_boo ) return std::string("big boo boo");
|
||||
return std::string("unknown error");
|
||||
}
|
||||
};
|
||||
|
||||
const lib3_error_category_imp lib3_error_category_const;
|
||||
|
||||
const boost::system::error_category & lib3_error_category
|
||||
= lib3_error_category_const;
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------ //
|
||||
|
||||
// Library 4: Library uses const error_code's to identify library specific
|
||||
// errors.
|
||||
|
||||
// This particular example is for a library not within the parent namespace.
|
||||
// For an example of a library within the parent namespace, see library 3.
|
||||
|
||||
// header lib4.hpp:
|
||||
|
||||
namespace lib4
|
||||
{
|
||||
// lib4 has its own error_category:
|
||||
extern const boost::system::error_category & lib4_error_category;
|
||||
|
||||
extern const boost::system::error_code boo_boo;
|
||||
extern const boost::system::error_code big_boo_boo;
|
||||
}
|
||||
|
||||
// implementation file lib4.cpp:
|
||||
|
||||
// #include <lib4.hpp>
|
||||
|
||||
namespace lib4
|
||||
{
|
||||
class lib4_error_category_imp : public boost::system::error_category
|
||||
{
|
||||
public:
|
||||
const char * name() const
|
||||
{
|
||||
return "lib4";
|
||||
}
|
||||
|
||||
boost::system::error_condition default_error_condition( int ev ) const
|
||||
{
|
||||
return ev == boo_boo.value()
|
||||
? boost::system::error_condition( boost::system::posix::io_error,
|
||||
boost::system::posix_category )
|
||||
: boost::system::error_condition( ev, lib4::lib4_error_category );
|
||||
}
|
||||
|
||||
std::string message( int ev ) const
|
||||
{
|
||||
if ( ev == boo_boo.value() ) return std::string("boo boo");
|
||||
if ( ev == big_boo_boo.value() ) return std::string("big boo boo");
|
||||
return std::string("unknown error");
|
||||
}
|
||||
};
|
||||
|
||||
const lib4_error_category_imp lib4_error_category_const;
|
||||
|
||||
const boost::system::error_category & lib4_error_category
|
||||
= lib4_error_category_const;
|
||||
|
||||
const boost::system::error_code boo_boo( 456, lib4_error_category );
|
||||
const boost::system::error_code big_boo_boo( 789, lib4_error_category );
|
||||
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------ //
|
||||
|
||||
// Chris Kolhoff's Test3, modified to work with error_code.hpp
|
||||
|
||||
// Test3
|
||||
// =====
|
||||
// Define error classes to check for success, permission_denied and
|
||||
// out_of_memory, but add additional mappings for a user-defined error category.
|
||||
//
|
||||
|
||||
//namespace test3 {
|
||||
|
||||
// enum user_err
|
||||
// {
|
||||
// user_success = 0,
|
||||
// user_permission_denied,
|
||||
// user_out_of_memory
|
||||
// };
|
||||
//
|
||||
// class user_error_category_imp : public boost::system::error_category
|
||||
// {
|
||||
// public:
|
||||
// const std::string & name() const
|
||||
// {
|
||||
// static std::string s( "test3" );
|
||||
// return s;
|
||||
// }
|
||||
//
|
||||
// boost::system::error_code portable_error_code( int ev ) const
|
||||
// {
|
||||
// switch (ev)
|
||||
// {
|
||||
// case user_success:
|
||||
// return boost::system::error_code(boost::system::posix::success, boost::system::posix_category);
|
||||
// case user_permission_denied:
|
||||
// return boost::system::error_code(boost::system::posix::permission_denied, boost::system::posix_category);
|
||||
// case user_out_of_memory:
|
||||
// return boost::system::error_code(boost::system::posix::not_enough_memory, boost::system::posix_category);
|
||||
// default:
|
||||
// break;
|
||||
// }
|
||||
// return boost::system::error_code(boost::system::posix::no_posix_equivalent, boost::system::posix_category);
|
||||
// }
|
||||
//
|
||||
// };
|
||||
//
|
||||
// const user_error_category_imp user_error_category_const;
|
||||
//
|
||||
// const boost::system::error_category & user_error_category
|
||||
// = user_error_category_const;
|
||||
//
|
||||
// inline boost::system::error_code make_error_code(user_err e)
|
||||
// {
|
||||
// return boost::system::error_code(e, user_error_category);
|
||||
// }
|
||||
//
|
||||
// // test code
|
||||
//
|
||||
// void check_success(const boost::system::error_code& ec, bool expect)
|
||||
// {
|
||||
// BOOST_CHECK( (ec == boost::system::posix::success) == expect );
|
||||
// if (ec == boost::system::posix::success)
|
||||
// std::cout << "yes... " << (expect ? "ok" : "fail") << '\n';
|
||||
// else
|
||||
// std::cout << "no... " << (expect ? "fail" : "ok") << '\n';
|
||||
// }
|
||||
//
|
||||
// void check_permission_denied(const boost::system::error_code& ec, bool expect)
|
||||
// {
|
||||
// BOOST_CHECK( (ec == boost::system::posix::permission_denied) == expect );
|
||||
// if (ec == boost::system::posix::permission_denied)
|
||||
// std::cout << "yes... " << (expect ? "ok" : "fail") << '\n';
|
||||
// else
|
||||
// std::cout << "no... " << (expect ? "fail" : "ok") << '\n';
|
||||
// }
|
||||
//
|
||||
// void check_out_of_memory(const boost::system::error_code& ec, bool expect)
|
||||
// {
|
||||
// BOOST_CHECK( (ec == boost::system::posix::not_enough_memory) == expect );
|
||||
// if (ec == boost::system::posix::not_enough_memory)
|
||||
// std::cout << "yes... " << (expect ? "ok" : "fail") << '\n';
|
||||
// else
|
||||
// std::cout << "no... " << (expect ? "fail" : "ok") << '\n';
|
||||
// }
|
||||
//
|
||||
// void run()
|
||||
// {
|
||||
// printf("Test3\n");
|
||||
// printf("=====\n");
|
||||
// boost::system::error_code ec;
|
||||
// check_success(ec, true);
|
||||
// check_success(boost::system::posix::success, true);
|
||||
// check_success(boost::system::posix::permission_denied, false);
|
||||
// check_success(boost::system::posix::not_enough_memory, false);
|
||||
// check_success(user_success, true);
|
||||
// check_success(user_permission_denied, false);
|
||||
// check_success(user_out_of_memory, false);
|
||||
// check_permission_denied(ec, false);
|
||||
// check_permission_denied(boost::system::posix::success, false);
|
||||
// check_permission_denied(boost::system::posix::permission_denied, true);
|
||||
// check_permission_denied(boost::system::posix::not_enough_memory, false);
|
||||
// check_permission_denied(user_success, false);
|
||||
// check_permission_denied(user_permission_denied, true);
|
||||
// check_permission_denied(user_out_of_memory, false);
|
||||
// check_out_of_memory(ec, false);
|
||||
// check_out_of_memory(boost::system::posix::success, false);
|
||||
// check_out_of_memory(boost::system::posix::permission_denied, false);
|
||||
// check_out_of_memory(boost::system::posix::not_enough_memory, true);
|
||||
// check_out_of_memory(user_success, false);
|
||||
// check_out_of_memory(user_permission_denied, false);
|
||||
// check_out_of_memory(user_out_of_memory, true);
|
||||
//
|
||||
//# ifdef BOOST_WINDOWS_API
|
||||
// check_success(boost::system::windows::success, true);
|
||||
// check_success(boost::system::windows::access_denied, false);
|
||||
// check_success(boost::system::windows::not_enough_memory, false);
|
||||
// check_permission_denied(boost::system::windows::success, false);
|
||||
// check_permission_denied(boost::system::windows::access_denied, true);
|
||||
// check_permission_denied(boost::system::windows::not_enough_memory, false);
|
||||
// check_out_of_memory(boost::system::windows::success, false);
|
||||
// check_out_of_memory(boost::system::windows::access_denied, false);
|
||||
// check_out_of_memory(boost::system::windows::not_enough_memory, true);
|
||||
//# endif
|
||||
//
|
||||
// printf("\n");
|
||||
// }
|
||||
//
|
||||
//} // namespace test3
|
||||
|
||||
|
||||
|
||||
// ------------------------------------------------------------------------ //
|
||||
|
||||
int test_main( int, char *[] )
|
||||
{
|
||||
boost::system::error_code ec;
|
||||
boost::asio::boo_boo( ec );
|
||||
|
||||
// Library 1 tests:
|
||||
|
||||
ec = my_mkdir( "/no-such-file-or-directory/will-not-succeed" );
|
||||
std::cout << "ec.value() is " << ec.value() << '\n';
|
||||
|
||||
BOOST_CHECK( ec );
|
||||
BOOST_CHECK( ec == boost::asio::error::boo_boo );
|
||||
BOOST_CHECK( ec.value() == BOO_BOO );
|
||||
BOOST_CHECK( ec.category() == boost::asio::asio_error );
|
||||
BOOST_CHECK( ec == boost::system::posix::no_such_file_or_directory );
|
||||
BOOST_CHECK( ec.category() == boost::system::system_category );
|
||||
|
||||
// a real user can't rely on the value of an error_category object's value,
|
||||
// but in this test program that value is known, so test for it.
|
||||
BOOST_CHECK( ec.category().value() == boost::system::native_ecat.value()+1 );
|
||||
// Library 2 tests:
|
||||
|
||||
ec = my_remove( "/no-such-file-or-directory" );
|
||||
std::cout << "ec.value() is " << ec.value() << '\n';
|
||||
|
||||
BOOST_CHECK( ec );
|
||||
BOOST_CHECK( ec == boost::system::posix::no_such_file_or_directory );
|
||||
BOOST_CHECK( ec.category() == boost::system::posix_category );
|
||||
|
||||
// Library 3 tests:
|
||||
|
||||
ec = boost::lib3::boo_boo;
|
||||
std::cout << "ec.value() is " << ec.value() << '\n';
|
||||
|
||||
BOOST_CHECK( ec );
|
||||
BOOST_CHECK( ec == boost::lib3::boo_boo );
|
||||
BOOST_CHECK( ec.value() == boost::lib3::boo_boo );
|
||||
BOOST_CHECK( ec.category() == boost::lib3::lib3_error_category );
|
||||
|
||||
BOOST_CHECK( ec == boost::system::posix::io_error );
|
||||
|
||||
boost::system::error_code ec3( boost::lib3::boo_boo+100,
|
||||
boost::lib3::lib3_error_category );
|
||||
BOOST_CHECK( ec3.category() == boost::lib3::lib3_error_category );
|
||||
BOOST_CHECK( ec3.default_error_condition().category()
|
||||
== boost::lib3::lib3_error_category );
|
||||
|
||||
// Library 4 tests:
|
||||
|
||||
ec = lib4::boo_boo;
|
||||
std::cout << "ec.value() is " << ec.value() << '\n';
|
||||
|
||||
BOOST_CHECK( ec );
|
||||
BOOST_CHECK( ec == lib4::boo_boo );
|
||||
BOOST_CHECK( ec.value() == lib4::boo_boo.value() );
|
||||
BOOST_CHECK( ec.category() == lib4::lib4_error_category );
|
||||
|
||||
BOOST_CHECK( ec == boost::system::posix::io_error );
|
||||
|
||||
boost::system::error_code ec4( lib4::boo_boo.value()+100,
|
||||
lib4::lib4_error_category );
|
||||
BOOST_CHECK( ec4.default_error_condition().category()
|
||||
== lib4::lib4_error_category );
|
||||
|
||||
// Test 3
|
||||
|
||||
//test3::run();
|
||||
|
||||
// asio did not supply decoders, so test the defaults
|
||||
BOOST_CHECK( ec.to_errno() == EOTHER );
|
||||
BOOST_CHECK( ec.message() == "API error" );
|
||||
BOOST_CHECK( ec.wmessage() == L"API error" );
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
// error_code_test.cpp -----------------------------------------------------//
|
||||
|
||||
// Copyright Beman Dawes 2007
|
||||
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
// See library home page at http://www.boost.org/libs/system
|
||||
|
||||
//----------------------------------------------------------------------------//
|
||||
|
||||
// VC++ 8.0 warns on usage of certain Standard Library and API functions that
|
||||
// can cause buffer overruns or other possible security issues if misused.
|
||||
// See http://msdn.microsoft.com/msdnmag/issues/05/05/SafeCandC/default.aspx
|
||||
// But the wording of the warning is misleading and unsettling, there are no
|
||||
// portable altersystem functions, and VC++ 8.0's own libraries use the
|
||||
// functions in question. So turn off the warnings.
|
||||
#define _CRT_SECURE_NO_DEPRECATE
|
||||
#define _SCL_SECURE_NO_DEPRECATE
|
||||
|
||||
#define BOOST_ERROR_CODE_HEADER_ONLY
|
||||
|
||||
#include <boost/test/minimal.hpp>
|
||||
|
||||
#include <boost/system/error_code.hpp>
|
||||
|
||||
int test_main( int, char*[] )
|
||||
{
|
||||
boost::system::error_code ec( 0, boost::system::system_category );
|
||||
return 0;
|
||||
}
|
||||
+18
-26
@@ -10,7 +10,7 @@
|
||||
//----------------------------------------------------------------------------//
|
||||
|
||||
// VC++ 8.0 warns on usage of certain Standard Library and API functions that
|
||||
// can be cause buffer overruns or other possible security issues if misused.
|
||||
// can cause buffer overruns or other possible security issues if misused.
|
||||
// See http://msdn.microsoft.com/msdnmag/issues/05/05/SafeCandC/default.aspx
|
||||
// But the wording of the warning is misleading and unsettling, there are no
|
||||
// portable alternative functions, and VC++ 8.0's own libraries use the
|
||||
@@ -23,24 +23,23 @@
|
||||
#include <iostream>
|
||||
|
||||
#ifdef BOOST_WINDOWS_API
|
||||
# include <winerror.h>
|
||||
#include <winerror.h>
|
||||
#endif
|
||||
|
||||
using boost::system::system_error;
|
||||
using boost::system::error_code;
|
||||
using boost::system::errno_ecat;
|
||||
using boost::system::no_message;
|
||||
using boost::system::system_category;
|
||||
|
||||
#define TEST(x,v,w) test(#x,x,v,w)
|
||||
|
||||
namespace
|
||||
{
|
||||
void test( const char * desc, const system_error & ex,
|
||||
error_code::value_type v, const char * str )
|
||||
int v, const char * str )
|
||||
{
|
||||
std::cout << "test " << desc << "\n what() returns \"" << ex.what() << "\"\n";
|
||||
BOOST_CHECK( ex.code().value() == v );
|
||||
BOOST_CHECK( ex.code().category() == errno_ecat );
|
||||
BOOST_CHECK( ex.code().category() == system_category );
|
||||
# ifdef BOOST_WINDOWS_API
|
||||
BOOST_CHECK( std::string( ex.what() ) == str );
|
||||
if ( std::string( ex.what() ) != str )
|
||||
@@ -56,29 +55,22 @@ int test_main( int, char *[] )
|
||||
{
|
||||
// all combinations of constructors:
|
||||
|
||||
system_error se_0( error_code(0, errno_ecat) );
|
||||
system_error se_1( 1, errno_ecat );
|
||||
system_error se_0_m( error_code(0, errno_ecat), "se_0_m" );
|
||||
system_error se_1_m( 1, errno_ecat, "se_1_m" );
|
||||
system_error se_0_nm( error_code(0, errno_ecat), "" );
|
||||
system_error se_1_nm( 1, errno_ecat, "" );
|
||||
system_error se_0_m_im( error_code(0, errno_ecat), "se_0_m_im", no_message );
|
||||
system_error se_1_m_im( 1, errno_ecat, "se_1_m_im", no_message );
|
||||
system_error se_0_nm_im( error_code(0, errno_ecat), "", no_message );
|
||||
system_error se_1_nm_im( 1, errno_ecat, "", no_message );
|
||||
system_error se_1u_m( uvalue, errno_ecat, "se_1u_m" );
|
||||
system_error se_0_m( error_code(0, system_category), "se_0_m" );
|
||||
system_error se_1_m( 1, system_category, "se_1_m" );
|
||||
system_error se_0_nm( error_code(0, system_category), "" );
|
||||
system_error se_1_nm( 1, system_category, "" );
|
||||
system_error se_0_nmx( error_code(0, system_category), "" );
|
||||
system_error se_1_nmx( 1, system_category, "" );
|
||||
system_error se_1u_m( uvalue, system_category, "se_1u_m" );
|
||||
|
||||
TEST( se_0, 0, "" );
|
||||
TEST( se_1, 1, "Operation not permitted" );
|
||||
TEST( se_0_m, 0, "se_0_m" );
|
||||
TEST( se_1_m, 1, "se_1_m: Operation not permitted" );
|
||||
TEST( se_1_m, 1, "se_1_m: Incorrect function" );
|
||||
TEST( se_0_nm, 0, "" );
|
||||
TEST( se_1_nm, 1, "Operation not permitted" );
|
||||
TEST( se_0_m_im, 0, "se_0_m_im" );
|
||||
TEST( se_1_m_im, 1, "se_1_m_im" );
|
||||
TEST( se_0_nm_im, 0, "" );
|
||||
TEST( se_1_nm_im, 1, "" );
|
||||
TEST( se_1u_m, 1, "se_1u_m: Operation not permitted" );
|
||||
TEST( se_1_nm, 1, "Incorrect function" );
|
||||
TEST( se_0_nmx, 0, "" );
|
||||
TEST( se_1_nmx, 1, "Incorrect function" );
|
||||
TEST( se_1u_m, 1, "se_1u_m: Incorrect function" );
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user