util_exception_handler.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. * Copyright Andrey Semashev 2007 - 2015.
  3. * Distributed under the Boost Software License, Version 1.0.
  4. * (See accompanying file LICENSE_1_0.txt or copy at
  5. * http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. /*!
  8. * \file util_exception_handler.cpp
  9. * \author Andrey Semashev
  10. * \date 13.07.2009
  11. *
  12. * \brief This header contains tests for the exception handler functional objects.
  13. */
  14. #define BOOST_TEST_MODULE util_exception_handler
  15. #include <string>
  16. #include <typeinfo>
  17. #include <stdexcept>
  18. #include <boost/mpl/vector.hpp>
  19. #include <boost/smart_ptr/scoped_ptr.hpp>
  20. #include <boost/test/unit_test.hpp>
  21. #include <boost/log/utility/exception_handler.hpp>
  22. namespace logging = boost::log;
  23. namespace {
  24. struct my_handler1
  25. {
  26. typedef void result_type;
  27. std::type_info const*& m_pExceptionType;
  28. my_handler1(std::type_info const*& p) : m_pExceptionType(p) {}
  29. void operator() (std::exception&) const
  30. {
  31. m_pExceptionType = &typeid(std::exception);
  32. }
  33. void operator() (std::runtime_error&) const
  34. {
  35. m_pExceptionType = &typeid(std::runtime_error);
  36. }
  37. };
  38. struct my_handler2
  39. {
  40. typedef void result_type;
  41. typedef boost::mpl::vector< std::runtime_error, std::exception >::type exception_types;
  42. std::type_info const*& m_pExceptionType;
  43. explicit my_handler2(std::type_info const*& p) : m_pExceptionType(p) {}
  44. void operator() (std::exception&) const
  45. {
  46. m_pExceptionType = &typeid(std::exception);
  47. }
  48. void operator() (std::runtime_error&) const
  49. {
  50. m_pExceptionType = &typeid(std::runtime_error);
  51. }
  52. };
  53. struct my_handler1_nothrow
  54. {
  55. typedef void result_type;
  56. std::type_info const*& m_pExceptionType;
  57. my_handler1_nothrow(std::type_info const*& p) : m_pExceptionType(p) {}
  58. void operator() (std::exception&) const
  59. {
  60. m_pExceptionType = &typeid(std::exception);
  61. }
  62. void operator() (std::runtime_error&) const
  63. {
  64. m_pExceptionType = &typeid(std::runtime_error);
  65. }
  66. void operator() () const
  67. {
  68. m_pExceptionType = &typeid(void);
  69. }
  70. };
  71. struct my_handler2_nothrow
  72. {
  73. typedef void result_type;
  74. typedef boost::mpl::vector< std::runtime_error, std::exception >::type exception_types;
  75. std::type_info const*& m_pExceptionType;
  76. explicit my_handler2_nothrow(std::type_info const*& p) : m_pExceptionType(p) {}
  77. void operator() (std::exception&) const
  78. {
  79. m_pExceptionType = &typeid(std::exception);
  80. }
  81. void operator() (std::runtime_error&) const
  82. {
  83. m_pExceptionType = &typeid(std::runtime_error);
  84. }
  85. void operator() () const
  86. {
  87. m_pExceptionType = &typeid(void);
  88. }
  89. };
  90. struct my_exception {};
  91. struct my_function0
  92. {
  93. struct impl_base
  94. {
  95. virtual ~impl_base() {}
  96. virtual void invoke() = 0;
  97. };
  98. template< typename T >
  99. struct impl : public impl_base
  100. {
  101. T m_Fun;
  102. explicit impl(T const& fun) : m_Fun(fun) {}
  103. void invoke() { m_Fun(); }
  104. };
  105. private:
  106. boost::scoped_ptr< impl_base > m_pImpl;
  107. public:
  108. template< typename T >
  109. my_function0& operator= (T const& fun)
  110. {
  111. m_pImpl.reset(new impl< T >(fun));
  112. return *this;
  113. }
  114. void operator() () const
  115. {
  116. m_pImpl->invoke();
  117. }
  118. };
  119. } // namespace
  120. // Tests for handler with explicit exception types specification
  121. BOOST_AUTO_TEST_CASE(explicit_exception_types)
  122. {
  123. std::type_info const* pExceptionType = 0;
  124. my_function0 handler;
  125. handler = logging::make_exception_handler<
  126. std::runtime_error,
  127. std::exception
  128. >(my_handler1(pExceptionType));
  129. try
  130. {
  131. throw std::runtime_error("error");
  132. }
  133. catch (...)
  134. {
  135. handler();
  136. }
  137. BOOST_REQUIRE(pExceptionType != 0);
  138. BOOST_CHECK(*pExceptionType == typeid(std::runtime_error));
  139. pExceptionType = 0;
  140. try
  141. {
  142. throw std::logic_error("error");
  143. }
  144. catch (...)
  145. {
  146. handler();
  147. }
  148. BOOST_REQUIRE(pExceptionType != 0);
  149. BOOST_CHECK(*pExceptionType == typeid(std::exception));
  150. pExceptionType = 0;
  151. try
  152. {
  153. throw my_exception();
  154. }
  155. catch (...)
  156. {
  157. BOOST_CHECK_THROW(handler(), my_exception);
  158. }
  159. BOOST_REQUIRE(pExceptionType == 0);
  160. // Verify that exception types are checked in the specified order
  161. handler = logging::make_exception_handler<
  162. std::exception,
  163. std::runtime_error
  164. >(my_handler1(pExceptionType));
  165. try
  166. {
  167. throw std::runtime_error("error");
  168. }
  169. catch (...)
  170. {
  171. handler();
  172. }
  173. BOOST_REQUIRE(pExceptionType != 0);
  174. BOOST_CHECK(*pExceptionType == typeid(std::exception));
  175. pExceptionType = 0;
  176. }
  177. // Tests for handler with explicit exception types specification (no-throw version)
  178. BOOST_AUTO_TEST_CASE(explicit_exception_types_nothrow)
  179. {
  180. std::type_info const* pExceptionType = 0;
  181. my_function0 handler;
  182. handler = logging::make_exception_handler<
  183. std::runtime_error,
  184. std::exception
  185. >(my_handler1_nothrow(pExceptionType), std::nothrow);
  186. try
  187. {
  188. throw std::runtime_error("error");
  189. }
  190. catch (...)
  191. {
  192. handler();
  193. }
  194. BOOST_REQUIRE(pExceptionType != 0);
  195. BOOST_CHECK(*pExceptionType == typeid(std::runtime_error));
  196. pExceptionType = 0;
  197. try
  198. {
  199. throw std::logic_error("error");
  200. }
  201. catch (...)
  202. {
  203. handler();
  204. }
  205. BOOST_REQUIRE(pExceptionType != 0);
  206. BOOST_CHECK(*pExceptionType == typeid(std::exception));
  207. pExceptionType = 0;
  208. try
  209. {
  210. throw my_exception();
  211. }
  212. catch (...)
  213. {
  214. BOOST_CHECK_NO_THROW(handler());
  215. }
  216. BOOST_REQUIRE(pExceptionType != 0);
  217. BOOST_CHECK(*pExceptionType == typeid(void));
  218. pExceptionType = 0;
  219. // Verify that exception types are checked in the specified order
  220. handler = logging::make_exception_handler<
  221. std::exception,
  222. std::runtime_error
  223. >(my_handler1_nothrow(pExceptionType), std::nothrow);
  224. try
  225. {
  226. throw std::runtime_error("error");
  227. }
  228. catch (...)
  229. {
  230. handler();
  231. }
  232. BOOST_REQUIRE(pExceptionType != 0);
  233. BOOST_CHECK(*pExceptionType == typeid(std::exception));
  234. pExceptionType = 0;
  235. }
  236. // Tests for handler with self-contained exception types
  237. BOOST_AUTO_TEST_CASE(self_contained_exception_types)
  238. {
  239. std::type_info const* pExceptionType = 0;
  240. my_function0 handler;
  241. handler = logging::make_exception_handler(my_handler2(pExceptionType));
  242. try
  243. {
  244. throw std::runtime_error("error");
  245. }
  246. catch (...)
  247. {
  248. handler();
  249. }
  250. BOOST_REQUIRE(pExceptionType != 0);
  251. BOOST_CHECK(*pExceptionType == typeid(std::runtime_error));
  252. pExceptionType = 0;
  253. try
  254. {
  255. throw std::logic_error("error");
  256. }
  257. catch (...)
  258. {
  259. handler();
  260. }
  261. BOOST_REQUIRE(pExceptionType != 0);
  262. BOOST_CHECK(*pExceptionType == typeid(std::exception));
  263. pExceptionType = 0;
  264. try
  265. {
  266. throw my_exception();
  267. }
  268. catch (...)
  269. {
  270. BOOST_CHECK_THROW(handler(), my_exception);
  271. }
  272. BOOST_REQUIRE(pExceptionType == 0);
  273. }
  274. // Tests for handler with self-contained exception types (no-throw version)
  275. BOOST_AUTO_TEST_CASE(self_contained_exception_types_nothrow)
  276. {
  277. std::type_info const* pExceptionType = 0;
  278. my_function0 handler;
  279. handler = logging::make_exception_handler(my_handler2_nothrow(pExceptionType), std::nothrow);
  280. try
  281. {
  282. throw std::runtime_error("error");
  283. }
  284. catch (...)
  285. {
  286. handler();
  287. }
  288. BOOST_REQUIRE(pExceptionType != 0);
  289. BOOST_CHECK(*pExceptionType == typeid(std::runtime_error));
  290. pExceptionType = 0;
  291. try
  292. {
  293. throw std::logic_error("error");
  294. }
  295. catch (...)
  296. {
  297. handler();
  298. }
  299. BOOST_REQUIRE(pExceptionType != 0);
  300. BOOST_CHECK(*pExceptionType == typeid(std::exception));
  301. pExceptionType = 0;
  302. try
  303. {
  304. throw my_exception();
  305. }
  306. catch (...)
  307. {
  308. BOOST_CHECK_NO_THROW(handler());
  309. }
  310. BOOST_REQUIRE(pExceptionType != 0);
  311. BOOST_CHECK(*pExceptionType == typeid(void));
  312. pExceptionType = 0;
  313. }