123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- //Copyright (c) 2006-2009 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/diagnostic_information.hpp>
- #include <boost/exception/info.hpp>
- #include <boost/detail/lightweight_test.hpp>
- #include <boost/detail/workaround.hpp>
- #if BOOST_WORKAROUND(__CODEGEARC__, BOOST_TESTED_AT(0x610))
- struct test_tag1 {};
- struct test_tag2 {};
- #endif
- typedef boost::error_info<struct test_tag1,int> tagged_int1;
- typedef boost::error_info<struct test_tag2,int> tagged_int2;
- std::string
- to_string( tagged_int2 const & x )
- {
- return '[' +boost::error_info_name(x) + "] = " + (x.value()==42 ? "fourty-two" : "bad value");
- }
- struct
- error1:
- std::exception,
- boost::exception
- {
- char const *
- what() const BOOST_NOEXCEPT_OR_NOTHROW
- {
- return "error1";
- }
- };
- struct
- error2:
- boost::exception
- {
- };
- struct
- error3:
- std::exception
- {
- char const *
- what() const BOOST_NOEXCEPT_OR_NOTHROW
- {
- return "error3";
- }
- };
- struct
- error4:
- std::exception,
- boost::exception
- {
- char const *
- what() const BOOST_NOEXCEPT_OR_NOTHROW
- {
- return diagnostic_information_what(*this);
- }
- };
- void
- test1( std::string const & di1, std::string const & di2 )
- {
- BOOST_TEST( di1!=di2 );
- #ifndef BOOST_NO_RTTI
- BOOST_TEST(di1.find("type:")!=std::string::npos);
- BOOST_TEST(di1.find("error1")!=std::string::npos);
- #endif
- BOOST_TEST(di1.find("test_tag1")!=std::string::npos);
- BOOST_TEST(di1.find("test_tag2")!=std::string::npos);
- BOOST_TEST(di1.find("fourty-two")!=std::string::npos);
- #ifndef BOOST_NO_RTTI
- BOOST_TEST(di2.find("type:")!=std::string::npos);
- BOOST_TEST(di2.find("error1")!=std::string::npos);
- #endif
- BOOST_TEST(di2.find("test_tag1")!=std::string::npos);
- BOOST_TEST(di2.find("test_tag2")!=std::string::npos);
- BOOST_TEST(di2.find("bad value")!=std::string::npos);
- }
- void
- test2( std::string const & di1, std::string const & di2 )
- {
- BOOST_TEST( di1!=di2 );
- BOOST_TEST(di1.find("test_tag1")!=std::string::npos);
- BOOST_TEST(di1.find("test_tag2")!=std::string::npos);
- BOOST_TEST(di1.find("fourty-two")!=std::string::npos);
- BOOST_TEST(di2.find("test_tag1")!=std::string::npos);
- BOOST_TEST(di2.find("test_tag2")!=std::string::npos);
- BOOST_TEST(di2.find("bad value")!=std::string::npos);
- }
- void
- test3( std::string const & di )
- {
- #ifndef BOOST_NO_RTTI
- BOOST_TEST(di.find("type:")!=std::string::npos);
- #endif
- BOOST_TEST(di.find("error3")!=std::string::npos);
- }
- void
- test4( std::string const & di1, std::string const & di2 )
- {
- BOOST_TEST( di1!=di2 );
- #ifndef BOOST_NO_RTTI
- BOOST_TEST(di1.find("type:")!=std::string::npos);
- #endif
- BOOST_TEST(di1.find("test_tag1")!=std::string::npos);
- BOOST_TEST(di1.find("test_tag2")!=std::string::npos);
- BOOST_TEST(di1.find("fourty-two")!=std::string::npos);
- #ifndef BOOST_NO_RTTI
- BOOST_TEST(di2.find("type:")!=std::string::npos);
- #endif
- BOOST_TEST(di2.find("test_tag1")!=std::string::npos);
- BOOST_TEST(di2.find("test_tag2")!=std::string::npos);
- BOOST_TEST(di2.find("bad value")!=std::string::npos);
- }
- int
- main()
- {
- using namespace boost;
- try
- {
- error1 x; x << tagged_int1(42) << tagged_int2(42);
- BOOST_TEST(x.what()==std::string("error1"));
- throw x;
- }
- catch(
- error1 & x )
- {
- std::string di1=boost::diagnostic_information(x);
- x << tagged_int1(2) << tagged_int2(2);
- std::string di2 = diagnostic_information(x);
- test1(di1,di2);
- }
- try
- {
- error1 x; x << tagged_int1(42) << tagged_int2(42);
- BOOST_TEST(x.what()==std::string("error1"));
- throw x;
- }
- catch(
- error1 & x )
- {
- std::string di1=boost::current_exception_diagnostic_information();
- x << tagged_int1(2) << tagged_int2(2);
- std::string di2 = current_exception_diagnostic_information();
- test1(di1,di2);
- }
- try
- {
- error2 x;
- x << tagged_int1(42) << tagged_int2(42);
- throw x;
- }
- catch(
- error2 & x )
- {
- std::string di1 = diagnostic_information(x);
- x << tagged_int1(2) << tagged_int2(2);
- std::string di2 = diagnostic_information(x);
- test2(di1,di2);
- }
- try
- {
- error2 x;
- x << tagged_int1(42) << tagged_int2(42);
- throw x;
- }
- catch(
- error2 & x )
- {
- std::string di1 = current_exception_diagnostic_information();
- BOOST_TEST(di1==boost::diagnostic_information_what(x));
- x << tagged_int1(2) << tagged_int2(2);
- std::string di2 = current_exception_diagnostic_information();
- BOOST_TEST(di2==boost::diagnostic_information_what(x));
- test2(di1,di2);
- }
- try
- {
- error3 x;
- std::string di=diagnostic_information(x);
- test3(di);
- throw x;
- }
- catch(
- ... )
- {
- std::string di=current_exception_diagnostic_information();
- test3(di);
- }
- try
- {
- throw error4();
- }
- catch(
- error4 & x )
- {
- std::string di1=boost::diagnostic_information(x);
- std::string wh1=x.what();
- BOOST_TEST(wh1==di1);
- }
- try
- {
- error4 x; x << tagged_int1(42) << tagged_int2(42);
- throw x;
- }
- catch(
- error4 & x )
- {
- std::string di1=boost::diagnostic_information(x);
- std::string wh1=x.what();
- BOOST_TEST(wh1==di1);
- x << tagged_int1(2) << tagged_int2(2);
- std::string di2 = diagnostic_information(x);
- std::string wh2=x.what();
- BOOST_TEST(wh2==di2);
- test4(di1,di2);
- }
- try
- {
- error4 x; x << tagged_int1(42) << tagged_int2(42);
- throw x;
- }
- catch(
- error4 & x )
- {
- std::string di1=boost::current_exception_diagnostic_information();
- std::string wh1=x.what();
- BOOST_TEST(wh1==di1);
- x << tagged_int1(2) << tagged_int2(2);
- std::string di2 = current_exception_diagnostic_information();
- std::string wh2=x.what();
- BOOST_TEST(wh2==di2);
- test4(di1,di2);
- }
- return boost::report_errors();
- }
|