error_info_test.hpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. //Copyright (c) 2006-2015 Emil Dotchevski and Reverge Studios, Inc.
  2. //Distributed under the Boost Software License, Version 1.0. (See accompanying
  3. //file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. #include <boost/exception/get_error_info.hpp>
  5. #include <boost/exception/info_tuple.hpp>
  6. #include <boost/exception_ptr.hpp>
  7. #include <boost/detail/lightweight_test.hpp>
  8. #include <boost/detail/workaround.hpp>
  9. struct throws_on_copy;
  10. struct non_printable { };
  11. struct
  12. user_data
  13. {
  14. int & count;
  15. explicit
  16. user_data( int & count ):
  17. count(count)
  18. {
  19. ++count;
  20. }
  21. user_data( user_data const & x ):
  22. count(x.count)
  23. {
  24. ++count;
  25. }
  26. ~user_data()
  27. {
  28. --count;
  29. }
  30. };
  31. typedef boost::error_info<struct tag_test_1,int> test_1;
  32. typedef boost::error_info<struct tag_test_2,unsigned int> test_2;
  33. typedef boost::error_info<struct tag_test_3,float> test_3;
  34. typedef boost::error_info<struct tag_test_4,throws_on_copy> test_4;
  35. typedef boost::error_info<struct tag_test_5,std::string> test_5;
  36. typedef boost::error_info<struct tag_test_6,non_printable> test_6;
  37. typedef boost::error_info<struct tag_user_data,user_data> test_7;
  38. struct
  39. test_exception:
  40. boost::exception
  41. {
  42. };
  43. struct
  44. throws_on_copy
  45. {
  46. throws_on_copy()
  47. {
  48. }
  49. throws_on_copy( throws_on_copy const & )
  50. {
  51. throw test_exception();
  52. }
  53. };
  54. void
  55. basic_test()
  56. {
  57. try
  58. {
  59. test_exception x;
  60. add_info(add_info(add_info(x,test_1(1)),test_2(2u)),test_3(3.14159f));
  61. throw x;
  62. }
  63. catch(
  64. test_exception & x )
  65. {
  66. ++*boost::get_error_info<test_1>(x);
  67. ++*boost::get_error_info<test_2>(x);
  68. ++*boost::get_error_info<test_3>(x);
  69. BOOST_TEST(*boost::get_error_info<test_1>(x)==2);
  70. BOOST_TEST(*boost::get_error_info<test_2>(x)==3u);
  71. BOOST_TEST(*boost::get_error_info<test_3>(x)==4.14159f);
  72. BOOST_TEST(!boost::get_error_info<test_4>(x));
  73. }
  74. try
  75. {
  76. test_exception x;
  77. add_info(add_info(add_info(x,test_1(1)),test_2(2u)),test_3(3.14159f));
  78. throw x;
  79. }
  80. catch(
  81. test_exception const & x )
  82. {
  83. BOOST_TEST(*boost::get_error_info<test_1>(x)==1);
  84. BOOST_TEST(*boost::get_error_info<test_2>(x)==2u);
  85. BOOST_TEST(*boost::get_error_info<test_3>(x)==3.14159f);
  86. BOOST_TEST(!boost::get_error_info<test_4>(x));
  87. }
  88. }
  89. void
  90. exception_safety_test()
  91. {
  92. test_exception x;
  93. try
  94. {
  95. add_info(x,test_4(throws_on_copy()));
  96. BOOST_TEST(false);
  97. }
  98. catch(
  99. test_exception & )
  100. {
  101. BOOST_TEST(!boost::get_error_info<test_4>(x));
  102. }
  103. }
  104. void
  105. throw_empty()
  106. {
  107. throw test_exception();
  108. }
  109. void
  110. throw_test_1( char const * value )
  111. {
  112. throw add_info(test_exception(),test_5(std::string(value)));
  113. }
  114. void
  115. throw_test_2()
  116. {
  117. throw add_info(test_exception(),test_6(non_printable()));
  118. }
  119. void
  120. throw_catch_add_file_name( char const * name )
  121. {
  122. try
  123. {
  124. throw_empty();
  125. BOOST_TEST(false);
  126. }
  127. catch(
  128. boost::exception & x )
  129. {
  130. add_info(x,test_5(std::string(name)));
  131. throw;
  132. }
  133. }
  134. void
  135. test_empty()
  136. {
  137. try
  138. {
  139. throw_empty();
  140. BOOST_TEST(false);
  141. }
  142. catch(
  143. boost::exception & x )
  144. {
  145. #ifndef BOOST_NO_RTTI
  146. BOOST_TEST( dynamic_cast<test_exception *>(&x) );
  147. #endif
  148. BOOST_TEST( !boost::get_error_info<test_1>(x) );
  149. }
  150. catch(
  151. ... )
  152. {
  153. BOOST_TEST(false);
  154. }
  155. try
  156. {
  157. throw_empty();
  158. BOOST_TEST(false);
  159. }
  160. catch(
  161. test_exception & x )
  162. {
  163. #ifndef BOOST_NO_RTTI
  164. BOOST_TEST( dynamic_cast<boost::exception const *>(&x)!=0 );
  165. #endif
  166. }
  167. catch(
  168. ... )
  169. {
  170. BOOST_TEST(false);
  171. }
  172. }
  173. void
  174. test_basic_throw_catch()
  175. {
  176. try
  177. {
  178. throw_test_1("test");
  179. BOOST_ASSERT(false);
  180. }
  181. catch(
  182. boost::exception & x )
  183. {
  184. BOOST_TEST(*boost::get_error_info<test_5>(x)==std::string("test"));
  185. }
  186. catch(
  187. ... )
  188. {
  189. BOOST_TEST(false);
  190. }
  191. try
  192. {
  193. throw_test_2();
  194. BOOST_ASSERT(false);
  195. }
  196. catch(
  197. boost::exception & x )
  198. {
  199. BOOST_TEST(boost::get_error_info<test_6>(x));
  200. }
  201. catch(
  202. ... )
  203. {
  204. BOOST_TEST(false);
  205. }
  206. }
  207. void
  208. test_catch_add_info()
  209. {
  210. try
  211. {
  212. throw_catch_add_file_name("test");
  213. BOOST_TEST(false);
  214. }
  215. catch(
  216. boost::exception & x )
  217. {
  218. BOOST_TEST(*boost::get_error_info<test_5>(x)==std::string("test"));
  219. }
  220. catch(
  221. ... )
  222. {
  223. BOOST_TEST(false);
  224. }
  225. }
  226. void
  227. test_add_tuple()
  228. {
  229. typedef boost::tuple<> tuple_test_;
  230. typedef boost::tuple<test_1> tuple_test_1;
  231. typedef boost::tuple<test_1,test_2> tuple_test_12;
  232. typedef boost::tuple<test_1,test_2,test_3> tuple_test_123;
  233. typedef boost::tuple<test_1,test_2,test_3,test_5> tuple_test_1235;
  234. try
  235. {
  236. throw add_info(test_exception(),tuple_test_());
  237. }
  238. catch(
  239. test_exception & )
  240. {
  241. }
  242. catch(
  243. ... )
  244. {
  245. BOOST_TEST(false);
  246. }
  247. try
  248. {
  249. throw add_info(test_exception(),tuple_test_1(42));
  250. }
  251. catch(
  252. test_exception & x )
  253. {
  254. BOOST_TEST( *boost::get_error_info<test_1>(x)==42 );
  255. }
  256. catch(
  257. ... )
  258. {
  259. BOOST_TEST(false);
  260. }
  261. try
  262. {
  263. throw add_info(test_exception(),tuple_test_12(42,42u));
  264. }
  265. catch(
  266. test_exception & x )
  267. {
  268. BOOST_TEST( *boost::get_error_info<test_1>(x)==42 );
  269. BOOST_TEST( *boost::get_error_info<test_2>(x)==42u );
  270. }
  271. catch(
  272. ... )
  273. {
  274. BOOST_TEST(false);
  275. }
  276. try
  277. {
  278. throw add_info(test_exception(),tuple_test_123(42,42u,42.0f));
  279. }
  280. catch(
  281. test_exception & x )
  282. {
  283. BOOST_TEST( *boost::get_error_info<test_1>(x)==42 );
  284. BOOST_TEST( *boost::get_error_info<test_2>(x)==42u );
  285. BOOST_TEST( *boost::get_error_info<test_3>(x)==42.0f );
  286. }
  287. catch(
  288. ... )
  289. {
  290. BOOST_TEST(false);
  291. }
  292. try
  293. {
  294. throw add_info(test_exception(),tuple_test_1235(42,42u,42.0f,std::string("42")));
  295. }
  296. catch(
  297. test_exception & x )
  298. {
  299. BOOST_TEST( *boost::get_error_info<test_1>(x)==42 );
  300. BOOST_TEST( *boost::get_error_info<test_2>(x)==42u );
  301. BOOST_TEST( *boost::get_error_info<test_3>(x)==42.0f );
  302. BOOST_TEST( *boost::get_error_info<test_5>(x)=="42" );
  303. }
  304. catch(
  305. ... )
  306. {
  307. BOOST_TEST(false);
  308. }
  309. }
  310. void
  311. test_lifetime1()
  312. {
  313. int count=0;
  314. try
  315. {
  316. throw add_info(test_exception(),test_7(user_data(count)));
  317. }
  318. catch(
  319. boost::exception & x )
  320. {
  321. BOOST_TEST(count==1);
  322. BOOST_TEST( boost::get_error_info<test_7>(x) );
  323. }
  324. catch(
  325. ... )
  326. {
  327. BOOST_TEST(false);
  328. }
  329. BOOST_TEST(!count);
  330. }
  331. void
  332. test_lifetime2()
  333. {
  334. int count=0;
  335. {
  336. boost::exception_ptr ep;
  337. test_exception e; add_info(e,test_7(user_data(count)));
  338. ep=boost::copy_exception(e);
  339. BOOST_TEST(count>0);
  340. }
  341. BOOST_TEST(!count);
  342. }
  343. bool
  344. is_const( int const * )
  345. {
  346. return true;
  347. }
  348. bool
  349. is_const( int * )
  350. {
  351. return false;
  352. }
  353. void
  354. test_const()
  355. {
  356. test_exception e;
  357. boost::exception const & c(e);
  358. boost::exception & m(e);
  359. BOOST_TEST(is_const(boost::get_error_info<test_1>(c)));
  360. BOOST_TEST(!is_const(boost::get_error_info<test_1>(m)));
  361. }
  362. int
  363. main()
  364. {
  365. basic_test();
  366. exception_safety_test();
  367. test_empty();
  368. test_basic_throw_catch();
  369. test_catch_add_info();
  370. test_add_tuple();
  371. test_lifetime1();
  372. test_lifetime2();
  373. test_const();
  374. return boost::report_errors();
  375. }