blob: 33884dcc9f9f44ffdb5fdf1be6528d0435ff8698 [file] [log] [blame]
Davide Pesaventofd674012019-02-06 02:00:12 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2013-2019 Regents of the University of California.
4 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#include "ndn-cxx/util/exception.hpp"
23
24#include "tests/boost-test.hpp"
25
26#include <boost/exception/diagnostic_information.hpp>
27#include <boost/exception/get_error_info.hpp>
28
29namespace ndn {
30namespace exception {
31namespace test {
32
33BOOST_AUTO_TEST_SUITE(Util)
34BOOST_AUTO_TEST_SUITE(TestException)
35
36BOOST_AUTO_TEST_CASE(Throw)
37{
38 auto f = [] { NDN_THROW(std::invalid_argument("test")); };
39
40 BOOST_CHECK_THROW(f(), boost::exception);
41 BOOST_CHECK_THROW(f(), std::exception);
42 BOOST_CHECK_THROW(f(), std::invalid_argument);
43
44 try {
45 f();
46 }
47 catch (const boost::exception& ex) {
48 BOOST_CHECK(boost::get_error_info<boost::throw_file>(ex) != nullptr);
49 BOOST_CHECK(boost::get_error_info<boost::throw_line>(ex) != nullptr);
50 BOOST_CHECK(boost::get_error_info<boost::throw_function>(ex) != nullptr);
51
52#ifdef NDN_CXX_HAVE_STACKTRACE
53 auto stack = boost::get_error_info<errinfo_stacktrace>(ex);
54 BOOST_REQUIRE(stack != nullptr);
55 auto info = boost::diagnostic_information(ex);
56 BOOST_TEST_MESSAGE(info);
57 BOOST_CHECK(stack->empty() || info.find("===== Stacktrace =====") != std::string::npos);
58#endif
59 }
60}
61
62BOOST_AUTO_TEST_CASE(ThrowErrno)
63{
64 auto f = [] {
65 errno = ERANGE;
66 NDN_THROW_ERRNO(std::out_of_range("test"));
67 };
68
69 BOOST_CHECK_THROW(f(), boost::exception);
70 BOOST_CHECK_THROW(f(), std::exception);
71 BOOST_CHECK_THROW(f(), std::out_of_range);
72
73 try {
74 f();
75 }
76 catch (const boost::exception& ex) {
77 auto errPtr = boost::get_error_info<boost::errinfo_errno>(ex);
78 BOOST_REQUIRE(errPtr != nullptr);
79 BOOST_CHECK_EQUAL(*errPtr, ERANGE);
80 }
81}
82
83BOOST_AUTO_TEST_CASE(ThrowNested)
84{
85 auto f = [] {
86 try {
87 NDN_THROW(std::overflow_error("inner"));
88 }
89 catch (...) {
90 NDN_THROW_NESTED(std::domain_error("outer"));
91 }
92 };
93
94 BOOST_CHECK_THROW(f(), boost::exception);
95 BOOST_CHECK_THROW(f(), std::exception);
96 BOOST_CHECK_THROW(f(), std::domain_error);
97
98 try {
99 f();
100 }
101 catch (const boost::exception& ex) {
102 BOOST_CHECK(boost::get_error_info<boost::errinfo_nested_exception>(ex) != nullptr);
103 }
104}
105
106BOOST_AUTO_TEST_SUITE_END() // TestException
107BOOST_AUTO_TEST_SUITE_END() // Util
108
109} // namespace test
110} // namespace exception
111} // namespace ndn