boost exception

[SVN r43485]
This commit is contained in:
Emil Dotchevski
2008-03-04 01:41:17 +00:00
parent ae714358ad
commit bf515d83ca
76 changed files with 4933 additions and 0 deletions

20
test/Jamfile Normal file
View File

@ -0,0 +1,20 @@
# Boost Exception Library test Jamfile
#
# Copyright (c) 2006-2008 Emil Dotchevski and Reverge Studios, Inc.
#
# 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)
import testing ;
run cloning_test.cpp ;
run unknown_exception_test.cpp ;
run to_string_test.cpp ;
run exception_test.cpp ;
run boost_error_info_test.cpp ;
run enable_error_info_test.cpp helper1.cpp ;
run throw_exception_test.cpp helper2.cpp ;
run errno_test.cpp ;
run error_info_test.cpp ;
run what_test.cpp ;
compile-fail exception_fail.cpp ;

View File

@ -0,0 +1,40 @@
//Copyright (c) 2006-2008 Emil Dotchevski and Reverge Studios, Inc.
//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)
#include <boost/exception/info.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <stdexcept>
namespace
test
{
class my_exception: public boost::exception { };
typedef boost::error_info<struct tag_my_info,int> my_info;
void
test_boost_error_info()
{
try
{
throw my_exception() << BOOST_ERROR_INFO << my_info(1);
}
catch(
my_exception & x )
{
BOOST_TEST(1==*boost::get_error_info<my_info>(x));
BOOST_TEST(boost::get_error_info<boost::throw_function>(x));
BOOST_TEST(boost::get_error_info<boost::throw_file>(x));
BOOST_TEST(boost::get_error_info<boost::throw_line>(x));
}
}
}
int
main()
{
test::test_boost_error_info();
return boost::report_errors();
}

42
test/cloning_test.cpp Normal file
View File

@ -0,0 +1,42 @@
//Copyright (c) 2006-2008 Emil Dotchevski and Reverge Studios, Inc.
//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)
#include <boost/exception/cloning.hpp>
#include <boost/detail/lightweight_test.hpp>
struct
test_exception:
std::exception
{
};
int
main()
{
try
{
throw boost::enable_exception_cloning(test_exception());
}
catch(
std::exception & x )
{
boost::exception_ptr p = boost::clone_exception(x);
try
{
rethrow_exception(p);
BOOST_TEST(false);
}
catch(
test_exception & )
{
}
catch(
... )
{
BOOST_TEST(false);
}
}
return boost::report_errors();
}

View File

@ -0,0 +1,49 @@
//Copyright (c) 2006-2008 Emil Dotchevski and Reverge Studios, Inc.
//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)
#include "helper1.hpp"
#include <boost/exception/info.hpp>
#include <boost/detail/lightweight_test.hpp>
namespace
{
typedef boost::error_info<struct tag_test_int,int> test_int;
void
throw_wrapper()
{
try
{
boost::exception_test::throw_length_error();
}
catch(
boost::exception & x )
{
x << test_int(42);
throw;
}
}
}
int
main()
{
try
{
throw_wrapper();
BOOST_TEST(false);
}
catch(
std::exception & x )
{
BOOST_TEST( 42==*boost::get_error_info<test_int>(x) );
}
catch(
... )
{
BOOST_TEST(false);
}
return boost::report_errors();
}

33
test/errno_test.cpp Normal file
View File

@ -0,0 +1,33 @@
//Copyright (c) 2006-2008 Emil Dotchevski and Reverge Studios, Inc.
//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)
#include <boost/exception/info.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <errno.h>
typedef boost::error_info<struct tag_errno,int> info_errno;
class
my_exception:
public boost::exception
{
};
int
main()
{
try
{
errno=1;
throw my_exception() << info_errno(errno);
BOOST_TEST(false);
}
catch(
my_exception & x )
{
BOOST_TEST(1==*boost::get_error_info<info_errno>(x));
}
return boost::report_errors();
}

217
test/error_info_test.cpp Normal file
View File

@ -0,0 +1,217 @@
//Copyright (c) 2006-2008 Emil Dotchevski and Reverge Studios, Inc.
//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)
#include <boost/exception/info_tuple.hpp>
#include <boost/detail/lightweight_test.hpp>
struct throws_on_copy;
struct non_printable { };
typedef boost::error_info<struct tag_test_1,int> test_1;
typedef boost::error_info<struct tag_test_2,unsigned int> test_2;
typedef boost::error_info<struct tag_test_3,float> test_3;
typedef boost::error_info<struct tag_test_4,throws_on_copy> test_4;
typedef boost::error_info<struct tag_test_5,std::string> test_5;
typedef boost::error_info<struct tag_test_6,non_printable> test_6;
struct
test_exception:
public boost::exception
{
};
struct
throws_on_copy
{
throws_on_copy()
{
}
throws_on_copy( throws_on_copy const & )
{
throw test_exception();
}
};
void
basic_test()
{
test_exception x;
x << test_1(1) << test_2(2u) << test_3(3.14159f);
BOOST_TEST(*boost::get_error_info<test_1>(x)==1);
BOOST_TEST(*boost::get_error_info<test_2>(x)==2u);
BOOST_TEST(*boost::get_error_info<test_3>(x)==3.14159f);
BOOST_TEST(!boost::get_error_info<test_4>(x));
}
void
exception_safety_test()
{
test_exception x;
try
{
x << test_4(throws_on_copy());
BOOST_TEST(false);
}
catch(
test_exception & )
{
}
BOOST_TEST(!boost::get_error_info<test_4>(x));
}
void
throw_empty()
{
throw test_exception();
}
void
throw_test_1( char const * value )
{
throw test_exception() << test_5(std::string(value));
}
void
throw_test_2()
{
throw test_exception() << test_6(non_printable());
}
void
throw_catch_add_file_name( char const * name )
{
try
{
throw_empty();
BOOST_TEST(false);
}
catch(
boost::exception & x )
{
x << test_5(std::string(name));
throw;
}
}
void
test_empty()
{
try
{
throw_empty();
BOOST_TEST(false);
}
catch(
boost::exception & x )
{
BOOST_TEST( dynamic_cast<test_exception *>(&x) );
BOOST_TEST( !boost::get_error_info<test_1>(x) );
}
try
{
throw_empty();
BOOST_TEST(false);
}
catch(
test_exception & x )
{
BOOST_TEST( dynamic_cast<boost::exception *>(&x) );
}
}
void
test_basic_throw_catch()
{
try
{
throw_test_1("test");
BOOST_ASSERT(false);
}
catch(
boost::exception & x )
{
BOOST_TEST(*boost::get_error_info<test_5>(x)==std::string("test"));
}
try
{
throw_test_2();
BOOST_ASSERT(false);
}
catch(
boost::exception & x )
{
BOOST_TEST(boost::get_error_info<test_6>(x));
}
}
void
test_catch_add_info()
{
try
{
throw_catch_add_file_name("test");
BOOST_TEST(false);
}
catch(
boost::exception & x )
{
BOOST_TEST(*boost::get_error_info<test_5>(x)==std::string("test"));
}
}
void
test_add_tuple()
{
typedef boost::tuple<test_1,test_2> test_12;
typedef boost::tuple<test_1,test_2,test_3> test_123;
typedef boost::tuple<test_1,test_2,test_3,test_5> test_1235;
try
{
throw test_exception() << test_12(42,42u);
}
catch(
test_exception & x )
{
BOOST_TEST( *boost::get_error_info<test_1>(x)==42 );
BOOST_TEST( *boost::get_error_info<test_2>(x)==42u );
}
try
{
throw test_exception() << test_123(42,42u,42.0f);
}
catch(
test_exception & x )
{
BOOST_TEST( *boost::get_error_info<test_1>(x)==42 );
BOOST_TEST( *boost::get_error_info<test_2>(x)==42u );
BOOST_TEST( *boost::get_error_info<test_3>(x)==42.0f );
}
try
{
throw test_exception() << test_1235(42,42u,42.0f,std::string("42"));
}
catch(
test_exception & x )
{
BOOST_TEST( *boost::get_error_info<test_1>(x)==42 );
BOOST_TEST( *boost::get_error_info<test_2>(x)==42u );
BOOST_TEST( *boost::get_error_info<test_3>(x)==42.0f );
BOOST_TEST( *boost::get_error_info<test_5>(x)=="42" );
}
}
int
main()
{
basic_test();
exception_safety_test();
test_empty();
test_basic_throw_catch();
test_catch_add_info();
test_add_tuple();
return boost::report_errors();
}

12
test/exception_fail.cpp Normal file
View File

@ -0,0 +1,12 @@
//Copyright (c) 2006-2008 Emil Dotchevski and Reverge Studios, Inc.
//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)
#include <boost/exception/exception.hpp>
void
tester( boost::exception & x )
{
throw x; //must not compile.
}

35
test/exception_test.cpp Normal file
View File

@ -0,0 +1,35 @@
//Copyright (c) 2006-2008 Emil Dotchevski and Reverge Studios, Inc.
//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)
#include <boost/exception/exception.hpp>
#include <boost/detail/lightweight_test.hpp>
class
test_exception:
public boost::exception
{
};
void
test_throw()
{
throw test_exception();
}
int
main()
{
try
{
test_throw();
BOOST_TEST(false);
}
catch(
test_exception & )
{
BOOST_TEST(true);
}
return boost::report_errors();
}

21
test/helper1.cpp Normal file
View File

@ -0,0 +1,21 @@
//Copyright (c) 2006-2008 Emil Dotchevski and Reverge Studios, Inc.
//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)
#include <boost/exception/enable_error_info.hpp>
#include <stdexcept>
namespace
boost
{
namespace
exception_test
{
void
throw_length_error()
{
throw enable_error_info( std::length_error("my length error") );
}
}
}

19
test/helper1.hpp Normal file
View File

@ -0,0 +1,19 @@
//Copyright (c) 2006-2008 Emil Dotchevski and Reverge Studios, Inc.
//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)
#ifndef UUID_4AEB7924CA2E11DC888A8C9355D89593
#define UUID_4AEB7924CA2E11DC888A8C9355D89593
namespace
boost
{
namespace
exception_test
{
void throw_length_error();
}
}
#endif

53
test/helper2.cpp Normal file
View File

@ -0,0 +1,53 @@
//Copyright (c) 2006-2008 Emil Dotchevski and Reverge Studios, Inc.
//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)
#include "helper2.hpp"
#include <boost/throw_exception.hpp>
namespace
boost
{
namespace
exception_test
{
inline
some_boost_exception::
some_boost_exception( int x ):
x_(x)
{
}
some_boost_exception::
~some_boost_exception() throw()
{
}
inline
some_std_exception::
some_std_exception( int x ):
x_(x)
{
}
some_std_exception::
~some_std_exception() throw()
{
}
template <>
void
throw_test_exception<some_boost_exception>( int x )
{
boost::throw_exception( some_boost_exception(x) );
}
template <>
void
throw_test_exception<some_std_exception>( int x )
{
boost::throw_exception( some_std_exception(x) );
}
}
}

47
test/helper2.hpp Normal file
View File

@ -0,0 +1,47 @@
//Copyright (c) 2006-2008 Emil Dotchevski and Reverge Studios, Inc.
//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)
#ifndef UUID_BC765EB4CA2A11DCBDC5828355D89593
#define UUID_BC765EB4CA2A11DCBDC5828355D89593
#include <boost/exception/exception.hpp>
namespace
boost
{
namespace
exception_test
{
struct
some_boost_exception:
public boost::exception,
public std::exception
{
explicit some_boost_exception( int x );
virtual ~some_boost_exception() throw();
int x_;
};
struct
some_std_exception:
public std::exception
{
explicit some_std_exception( int x );
virtual ~some_std_exception() throw();
int x_;
};
template <class>
void throw_test_exception( int );
template <>
void throw_test_exception<some_boost_exception>( int );
template <>
void throw_test_exception<some_std_exception>( int );
}
}
#endif

View File

@ -0,0 +1,71 @@
//Copyright (c) 2006-2008 Emil Dotchevski and Reverge Studios, Inc.
//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)
#include "helper2.hpp"
#include <boost/exception/info.hpp>
#include <boost/exception/cloning.hpp>
#include <boost/detail/lightweight_test.hpp>
typedef boost::error_info<struct tag_test_int,int> test_int;
void
throw_fwd( void (*thrower)(int) )
{
try
{
thrower(42);
}
catch(
boost::exception & x )
{
x << test_int(42);
throw;
}
}
template <class T>
void
tester()
{
try
{
throw_fwd( &boost::exception_test::throw_test_exception<T> );
BOOST_ASSERT(false);
}
catch(
std::exception & x )
{
boost::exception_ptr p = boost::clone_exception(x);
try
{
rethrow_exception(p);
BOOST_TEST(false);
}
catch(
T & y )
{
BOOST_TEST(*boost::get_error_info<test_int>(y)==42);
BOOST_TEST(y.x_==42);
}
catch(
... )
{
BOOST_TEST(false);
}
}
catch(
... )
{
BOOST_TEST(false);
}
}
int
main()
{
tester<boost::exception_test::some_boost_exception>();
tester<boost::exception_test::some_std_exception>();
return boost::report_errors();
}

97
test/to_string_test.cpp Normal file
View File

@ -0,0 +1,97 @@
//Copyright (c) 2006-2008 Emil Dotchevski and Reverge Studios, Inc.
//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)
#include <boost/exception/to_string_stub.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <iostream>
namespace
n1
{
class
c1
{
};
std::string
to_string( c1 const & )
{
return "c1";
}
}
namespace
n2
{
class
c2
{
};
std::ostream &
operator<<( std::ostream & s, c2 const & )
{
s << "c2";
return s;
}
}
namespace
n3
{
class
c3
{
};
std::ostream &
operator<<( std::ostream & s, c3 const & )
{
s << "bad";
return s;
}
std::string
to_string( c3 const & )
{
return "c3";
}
}
namespace
boost
{
class
to_string_tester
{
};
}
template <class T>
struct
my_stub
{
std::string
operator()( T const & )
{
return "stub";
}
};
int
main()
{
using namespace boost;
BOOST_TEST( to_string(5)=="5" );
BOOST_TEST( to_string(n1::c1())=="c1" );
BOOST_TEST( to_string(n2::c2())=="c2" );
BOOST_TEST( to_string(n3::c3())=="c3" );
BOOST_TEST( to_string_stub(5)=="5" );
BOOST_TEST( to_string_stub(n1::c1())=="c1" );
BOOST_TEST( to_string_stub(n2::c2())=="c2" );
BOOST_TEST( to_string_stub(n3::c3())=="c3" );
BOOST_TEST( to_string_stub(to_string_tester(),my_stub<to_string_tester>())=="stub" );
return boost::report_errors();
}

View File

@ -0,0 +1,84 @@
//Copyright (c) 2006-2008 Emil Dotchevski and Reverge Studios, Inc.
//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)
#include <boost/exception/cloning.hpp>
#include <boost/exception/info.hpp>
#include <boost/detail/lightweight_test.hpp>
typedef boost::error_info<struct tag_test,int> test;
struct
test_boost_exception:
boost::exception
{
};
void
throw_boost_exception()
{
throw test_boost_exception() << test(42);
}
void
throw_unknown_exception()
{
struct
test_exception:
std::exception
{
};
throw test_exception();
}
int
main()
{
try
{
throw_boost_exception();
}
catch(
boost::exception & x )
{
boost::exception_ptr ep=boost::clone_exception(x);
try
{
rethrow_exception(ep);
}
catch(
boost::unknown_exception & x )
{
BOOST_TEST( 42==*boost::get_error_info<test>(x) );
}
catch(
... )
{
BOOST_TEST(false);
}
}
try
{
throw_unknown_exception();
}
catch(
std::exception & x )
{
boost::exception_ptr ep=boost::clone_exception(x);
try
{
rethrow_exception(ep);
}
catch(
boost::unknown_exception & )
{
}
catch(
... )
{
BOOST_TEST(false);
}
}
return boost::report_errors();
}

27
test/what_test.cpp Normal file
View File

@ -0,0 +1,27 @@
//Copyright (c) 2006-2008 Emil Dotchevski and Reverge Studios, Inc.
//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)
#include <boost/exception/info.hpp>
#include <boost/detail/lightweight_test.hpp>
typedef boost::error_info<struct tag_test,int> test;
class
my_exception:
public boost::exception
{
};
int
main()
{
my_exception x;
x << test(1);
std::string w1 = x.what();
x << test(2);
std::string w2 = x.what();
BOOST_TEST( w1!=w2 );
return boost::report_errors();
}