forked from boostorg/system
Bring into compliance with N2066, TR2 Diagnostics Enhancements. Tests passing on Win32, Linux, on most modern compilers.
[SVN r35823]
This commit is contained in:
commit
f3c8c74656
11 files changed
+1445
No files matched your search
@@ -0,0 +1,115 @@
|
||||
// error_code_test.cpp -----------------------------------------------------//
|
||||
|
||||
// Copyright Beman Dawes 2006
|
||||
|
||||
// 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 be 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
|
||||
// functions in question. So turn off the warnings.
|
||||
#define _CRT_SECURE_NO_DEPRECATE
|
||||
#define _SCL_SECURE_NO_DEPRECATE
|
||||
|
||||
#include <boost/test/minimal.hpp>
|
||||
#include <boost/system/error_code.hpp>
|
||||
|
||||
// Although using directives are not the best programming practice, testing
|
||||
// with a boost::system using directive increases use scenario coverage.
|
||||
using namespace boost::system;
|
||||
|
||||
# if defined( BOOST_WINDOWS_API )
|
||||
# include "winerror.h"
|
||||
# include <boost/cerrno.hpp>
|
||||
# endif
|
||||
|
||||
// test_main ---------------------------------------------------------------//
|
||||
|
||||
// TODO: supply a build jam file
|
||||
// TODO: supply a test jam file
|
||||
// TODO: same for bjam v2
|
||||
// TODO: add message decoder 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 );
|
||||
|
||||
BOOST_CHECK( !ec );
|
||||
BOOST_CHECK( ec.value() == 0 );
|
||||
BOOST_CHECK( ec.to_errno() == 0 );
|
||||
|
||||
BOOST_CHECK( !ec_0_native );
|
||||
BOOST_CHECK( ec_0_native.value() == 0 );
|
||||
BOOST_CHECK( ec_0_native.to_errno() == 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_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 );
|
||||
|
||||
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 );
|
||||
|
||||
#ifdef BOOST_WINDOWS_API
|
||||
BOOST_CHECK( ec != ec_0_native );
|
||||
|
||||
// these tests probe the Windows to_errno decoder
|
||||
// test the first entry in the decoder table:
|
||||
ec = error_code( ERROR_FILE_NOT_FOUND, native_ecat );
|
||||
BOOST_CHECK( ec.value() == ERROR_FILE_NOT_FOUND );
|
||||
BOOST_CHECK( ec.to_errno() == ENOENT );
|
||||
|
||||
// test the second entry in the decoder table:
|
||||
ec = error_code( ERROR_PATH_NOT_FOUND, native_ecat );
|
||||
BOOST_CHECK( ec.value() == ERROR_PATH_NOT_FOUND );
|
||||
BOOST_CHECK( ec.to_errno() == ENOENT );
|
||||
|
||||
// test the third entry in the decoder table:
|
||||
ec = error_code( ERROR_ACCESS_DENIED, native_ecat );
|
||||
BOOST_CHECK( ec.value() == ERROR_ACCESS_DENIED );
|
||||
BOOST_CHECK( ec.to_errno() == EACCES );
|
||||
|
||||
// test the last regular entry in the decoder table:
|
||||
ec = error_code( ERROR_WRITE_PROTECT, native_ecat );
|
||||
BOOST_CHECK( ec.value() == ERROR_WRITE_PROTECT );
|
||||
BOOST_CHECK( ec.to_errno() == EROFS );
|
||||
|
||||
// test not-in-table condition:
|
||||
ec = error_code( 1234567890, native_ecat );
|
||||
BOOST_CHECK( ec.value() == 1234567890 );
|
||||
BOOST_CHECK( ec.to_errno() == EOTHER );
|
||||
|
||||
#else
|
||||
BOOST_CHECK( ec == ec_0_native );
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
// error_code_user_test.cpp ------------------------------------------------//
|
||||
|
||||
// Copyright Beman Dawes 2006
|
||||
|
||||
// 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
|
||||
|
||||
// ------------------------------------------------------------------------ //
|
||||
// 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.
|
||||
|
||||
#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 <boost/test/minimal.hpp>
|
||||
|
||||
int test_main( int, char *[] )
|
||||
{
|
||||
boost::system::error_code ec;
|
||||
boost::asio::boo_boo( ec );
|
||||
|
||||
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 );
|
||||
|
||||
// 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 );
|
||||
|
||||
// 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,84 @@
|
||||
// system_error_test.cpp ---------------------------------------------------//
|
||||
|
||||
// Copyright Beman Dawes 2006
|
||||
|
||||
// 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 be 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
|
||||
// functions in question. So turn off the warnings.
|
||||
#define _CRT_SECURE_NO_DEPRECATE
|
||||
#define _SCL_SECURE_NO_DEPRECATE
|
||||
|
||||
#include <boost/test/minimal.hpp>
|
||||
#include <boost/system/system_error.hpp>
|
||||
#include <iostream>
|
||||
|
||||
#include <winerror.h>
|
||||
|
||||
using boost::system::system_error;
|
||||
using boost::system::error_code;
|
||||
using boost::system::errno_ecat;
|
||||
using boost::system::no_message;
|
||||
|
||||
#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 )
|
||||
{
|
||||
std::cout << "test " << desc << "\n what() returns \"" << ex.what() << "\"\n";
|
||||
BOOST_CHECK( ex.code().value() == v );
|
||||
BOOST_CHECK( ex.code().category() == errno_ecat );
|
||||
# ifdef BOOST_WINDOWS_API
|
||||
BOOST_CHECK( std::string( ex.what() ) == str );
|
||||
if ( std::string( ex.what() ) != str )
|
||||
std::cout << "expected \"" << str << "\", but what() returned \""
|
||||
<< ex.what() << "\"\n";
|
||||
# endif
|
||||
}
|
||||
|
||||
const boost::uint_least32_t uvalue = 1u;
|
||||
}
|
||||
|
||||
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" );
|
||||
|
||||
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_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" );
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user