1 //Copyright (c) 2006-2015 Emil Dotchevski and Reverge Studios, Inc.
3 //Distributed under the Boost Software License, Version 1.0. (See accompanying
4 //file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 #include <boost/exception/get_error_info.hpp>
7 #include <boost/exception/info_tuple.hpp>
8 #include <boost/exception_ptr.hpp>
9 #include <boost/detail/lightweight_test.hpp>
10 #include <boost/detail/workaround.hpp>
12 struct throws_on_copy;
13 struct non_printable { };
21 user_data( int & count ):
27 user_data( user_data const & x ):
39 typedef boost::error_info<struct tag_test_1,int> test_1;
40 typedef boost::error_info<struct tag_test_2,unsigned int> test_2;
41 typedef boost::error_info<struct tag_test_3,float> test_3;
42 typedef boost::error_info<struct tag_test_4,throws_on_copy> test_4;
43 typedef boost::error_info<struct tag_test_5,std::string> test_5;
44 typedef boost::error_info<struct tag_test_6,non_printable> test_6;
45 typedef boost::error_info<struct tag_user_data,user_data> test_7;
60 throws_on_copy( throws_on_copy const & )
62 throw test_exception();
72 add_info(add_info(add_info(x,test_1(1)),test_2(2u)),test_3(3.14159f));
78 ++*boost::get_error_info<test_1>(x);
79 ++*boost::get_error_info<test_2>(x);
80 ++*boost::get_error_info<test_3>(x);
81 BOOST_TEST(*boost::get_error_info<test_1>(x)==2);
82 BOOST_TEST(*boost::get_error_info<test_2>(x)==3u);
83 BOOST_TEST(*boost::get_error_info<test_3>(x)==4.14159f);
84 BOOST_TEST(!boost::get_error_info<test_4>(x));
89 add_info(add_info(add_info(x,test_1(1)),test_2(2u)),test_3(3.14159f));
93 test_exception const & x )
95 BOOST_TEST(*boost::get_error_info<test_1>(x)==1);
96 BOOST_TEST(*boost::get_error_info<test_2>(x)==2u);
97 BOOST_TEST(*boost::get_error_info<test_3>(x)==3.14159f);
98 BOOST_TEST(!boost::get_error_info<test_4>(x));
103 exception_safety_test()
108 add_info(x,test_4(throws_on_copy()));
114 BOOST_TEST(!boost::get_error_info<test_4>(x));
121 throw test_exception();
125 throw_test_1( char const * value )
127 throw add_info(test_exception(),test_5(std::string(value)));
133 throw add_info(test_exception(),test_6(non_printable()));
137 throw_catch_add_file_name( char const * name )
145 boost::exception & x )
147 add_info(x,test_5(std::string(name)));
161 boost::exception & x )
163 #ifndef BOOST_NO_RTTI
164 BOOST_TEST( dynamic_cast<test_exception *>(&x) );
166 BOOST_TEST( !boost::get_error_info<test_1>(x) );
182 #ifndef BOOST_NO_RTTI
183 BOOST_TEST( dynamic_cast<boost::exception const *>(&x)!=0 );
194 test_basic_throw_catch()
198 throw_test_1("test");
202 boost::exception & x )
204 BOOST_TEST(*boost::get_error_info<test_5>(x)==std::string("test"));
218 boost::exception & x )
220 BOOST_TEST(boost::get_error_info<test_6>(x));
230 test_catch_add_info()
234 throw_catch_add_file_name("test");
238 boost::exception & x )
240 BOOST_TEST(*boost::get_error_info<test_5>(x)==std::string("test"));
252 typedef boost::tuple<> tuple_test_;
253 typedef boost::tuple<test_1> tuple_test_1;
254 typedef boost::tuple<test_1,test_2> tuple_test_12;
255 typedef boost::tuple<test_1,test_2,test_3> tuple_test_123;
256 typedef boost::tuple<test_1,test_2,test_3,test_5> tuple_test_1235;
259 throw add_info(test_exception(),tuple_test_());
272 throw add_info(test_exception(),tuple_test_1(42));
277 BOOST_TEST( *boost::get_error_info<test_1>(x)==42 );
286 throw add_info(test_exception(),tuple_test_12(42,42u));
291 BOOST_TEST( *boost::get_error_info<test_1>(x)==42 );
292 BOOST_TEST( *boost::get_error_info<test_2>(x)==42u );
301 throw add_info(test_exception(),tuple_test_123(42,42u,42.0f));
306 BOOST_TEST( *boost::get_error_info<test_1>(x)==42 );
307 BOOST_TEST( *boost::get_error_info<test_2>(x)==42u );
308 BOOST_TEST( *boost::get_error_info<test_3>(x)==42.0f );
317 throw add_info(test_exception(),tuple_test_1235(42,42u,42.0f,std::string("42")));
322 BOOST_TEST( *boost::get_error_info<test_1>(x)==42 );
323 BOOST_TEST( *boost::get_error_info<test_2>(x)==42u );
324 BOOST_TEST( *boost::get_error_info<test_3>(x)==42.0f );
325 BOOST_TEST( *boost::get_error_info<test_5>(x)=="42" );
340 throw add_info(test_exception(),test_7(user_data(count)));
343 boost::exception & x )
345 BOOST_TEST(count==1);
346 BOOST_TEST( boost::get_error_info<test_7>(x) );
361 boost::exception_ptr ep;
362 test_exception e; add_info(e,test_7(user_data(count)));
363 ep=boost::copy_exception(e);
370 is_const( int const * )
385 boost::exception const & c(e);
386 boost::exception & m(e);
387 BOOST_TEST(is_const(boost::get_error_info<test_1>(c)));
388 BOOST_TEST(!is_const(boost::get_error_info<test_1>(m)));
395 exception_safety_test();
397 test_basic_throw_catch();
398 test_catch_add_info();
403 return boost::report_errors();