blob: d8184c59a26138fff5d790e75732fbfbd98e0b5f [file] [log] [blame]
Davide Pesaventofd674012019-02-06 02:00:12 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento47ce2ee2023-05-09 01:33:33 -04003 * Copyright (c) 2013-2023 Regents of the University of California.
Davide Pesaventofd674012019-02-06 02:00:12 -05004 *
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
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040029namespace ndn::tests {
Davide Pesaventofd674012019-02-06 02:00:12 -050030
31BOOST_AUTO_TEST_SUITE(Util)
32BOOST_AUTO_TEST_SUITE(TestException)
33
34BOOST_AUTO_TEST_CASE(Throw)
35{
36 auto f = [] { NDN_THROW(std::invalid_argument("test")); };
37
38 BOOST_CHECK_THROW(f(), boost::exception);
39 BOOST_CHECK_THROW(f(), std::exception);
40 BOOST_CHECK_THROW(f(), std::invalid_argument);
41
42 try {
43 f();
44 }
45 catch (const boost::exception& ex) {
46 BOOST_CHECK(boost::get_error_info<boost::throw_file>(ex) != nullptr);
47 BOOST_CHECK(boost::get_error_info<boost::throw_line>(ex) != nullptr);
48 BOOST_CHECK(boost::get_error_info<boost::throw_function>(ex) != nullptr);
49
50#ifdef NDN_CXX_HAVE_STACKTRACE
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040051 auto stack = boost::get_error_info<ndn::exception::errinfo_stacktrace>(ex);
Davide Pesaventofd674012019-02-06 02:00:12 -050052 BOOST_REQUIRE(stack != nullptr);
53 auto info = boost::diagnostic_information(ex);
54 BOOST_TEST_MESSAGE(info);
55 BOOST_CHECK(stack->empty() || info.find("===== Stacktrace =====") != std::string::npos);
56#endif
57 }
58}
59
60BOOST_AUTO_TEST_CASE(ThrowErrno)
61{
62 auto f = [] {
63 errno = ERANGE;
64 NDN_THROW_ERRNO(std::out_of_range("test"));
65 };
66
67 BOOST_CHECK_THROW(f(), boost::exception);
68 BOOST_CHECK_THROW(f(), std::exception);
69 BOOST_CHECK_THROW(f(), std::out_of_range);
70
71 try {
72 f();
73 }
74 catch (const boost::exception& ex) {
75 auto errPtr = boost::get_error_info<boost::errinfo_errno>(ex);
76 BOOST_REQUIRE(errPtr != nullptr);
77 BOOST_CHECK_EQUAL(*errPtr, ERANGE);
78 }
79}
80
81BOOST_AUTO_TEST_CASE(ThrowNested)
82{
83 auto f = [] {
84 try {
85 NDN_THROW(std::overflow_error("inner"));
86 }
87 catch (...) {
88 NDN_THROW_NESTED(std::domain_error("outer"));
89 }
90 };
91
92 BOOST_CHECK_THROW(f(), boost::exception);
93 BOOST_CHECK_THROW(f(), std::exception);
94 BOOST_CHECK_THROW(f(), std::domain_error);
95
96 try {
97 f();
98 }
99 catch (const boost::exception& ex) {
100 BOOST_CHECK(boost::get_error_info<boost::errinfo_nested_exception>(ex) != nullptr);
101 }
102}
103
104BOOST_AUTO_TEST_SUITE_END() // TestException
105BOOST_AUTO_TEST_SUITE_END() // Util
106
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400107} // namespace ndn::tests