2010-05-02 04:35:11 +00:00
|
|
|
//
|
2011-02-24 22:07:11 +00:00
|
|
|
// Copyright 2010-2011 Ettus Research LLC
|
2018-02-19 23:30:32 +00:00
|
|
|
// Copyright 2018 Ettus Research, a National Instruments Company
|
2010-05-02 04:35:11 +00:00
|
|
|
//
|
2018-02-19 23:30:32 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
2010-05-02 04:35:11 +00:00
|
|
|
//
|
|
|
|
|
|
2011-02-24 22:18:52 +00:00
|
|
|
#include <uhd/exception.hpp>
|
2011-02-24 22:54:24 +00:00
|
|
|
#include <uhd/utils/assert_has.hpp>
|
2019-01-14 18:35:25 +00:00
|
|
|
#include <boost/test/unit_test.hpp>
|
2010-05-02 04:35:11 +00:00
|
|
|
#include <iostream>
|
2019-01-14 18:35:25 +00:00
|
|
|
#include <vector>
|
2010-05-02 04:35:11 +00:00
|
|
|
|
2019-01-14 18:35:25 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(test_exception_methods)
|
|
|
|
|
{
|
|
|
|
|
try {
|
2011-02-24 22:07:11 +00:00
|
|
|
throw uhd::assertion_error("your assertion failed: 1 != 2");
|
2019-01-14 18:35:25 +00:00
|
|
|
} catch (const uhd::exception& e) {
|
2011-02-24 22:07:11 +00:00
|
|
|
std::cout << "what: " << e.what() << std::endl;
|
|
|
|
|
std::cout << "code: " << e.code() << std::endl;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-14 18:35:25 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(test_assert_has)
|
|
|
|
|
{
|
2010-05-02 04:35:11 +00:00
|
|
|
std::vector<int> vec;
|
|
|
|
|
vec.push_back(2);
|
|
|
|
|
vec.push_back(3);
|
|
|
|
|
vec.push_back(5);
|
|
|
|
|
|
2019-01-14 18:35:25 +00:00
|
|
|
// verify the uhd::has utility
|
2011-02-25 01:28:10 +00:00
|
|
|
BOOST_CHECK(uhd::has(vec, 2));
|
|
|
|
|
BOOST_CHECK(not uhd::has(vec, 1));
|
2010-05-02 04:35:11 +00:00
|
|
|
|
|
|
|
|
std::cout << "The output of the assert_has error:" << std::endl;
|
2019-01-14 18:35:25 +00:00
|
|
|
try {
|
2010-05-02 04:35:11 +00:00
|
|
|
uhd::assert_has(vec, 1, "prime");
|
2019-01-14 18:35:25 +00:00
|
|
|
} catch (const std::exception& e) {
|
2010-05-06 00:03:59 +00:00
|
|
|
std::cout << e.what() << std::endl;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-14 18:35:25 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(test_assert_throw)
|
|
|
|
|
{
|
2010-05-06 00:03:59 +00:00
|
|
|
std::cout << "The output of the assert throw error:" << std::endl;
|
2019-01-14 18:35:25 +00:00
|
|
|
try {
|
2010-05-06 00:03:59 +00:00
|
|
|
UHD_ASSERT_THROW(2 + 2 == 5);
|
2019-01-14 18:35:25 +00:00
|
|
|
} catch (const std::exception& e) {
|
2011-06-24 22:22:55 +00:00
|
|
|
std::cout << e.what() << std::endl;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-14 18:35:25 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(test_exception_dynamic)
|
|
|
|
|
{
|
|
|
|
|
uhd::exception* exception_clone;
|
2011-06-24 22:22:55 +00:00
|
|
|
|
2019-01-14 18:35:25 +00:00
|
|
|
// throw an exception and dynamically clone it
|
|
|
|
|
try {
|
2011-06-24 22:22:55 +00:00
|
|
|
throw uhd::runtime_error("noooooo");
|
2019-01-14 18:35:25 +00:00
|
|
|
} catch (const uhd::exception& e) {
|
2010-05-06 00:03:59 +00:00
|
|
|
std::cout << e.what() << std::endl;
|
2011-06-24 22:22:55 +00:00
|
|
|
exception_clone = e.dynamic_clone();
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-14 18:35:25 +00:00
|
|
|
// now we dynamically re-throw the exception
|
|
|
|
|
try {
|
2011-06-24 22:22:55 +00:00
|
|
|
exception_clone->dynamic_throw();
|
2019-01-14 18:35:25 +00:00
|
|
|
} catch (const uhd::assertion_error& e) {
|
2011-06-24 23:09:22 +00:00
|
|
|
std::cout << e.what() << std::endl;
|
2011-06-24 22:22:55 +00:00
|
|
|
BOOST_CHECK(false);
|
2019-01-14 18:35:25 +00:00
|
|
|
} catch (const uhd::runtime_error& e) {
|
2011-06-24 23:09:22 +00:00
|
|
|
std::cout << e.what() << std::endl;
|
2011-06-24 22:22:55 +00:00
|
|
|
BOOST_CHECK(true);
|
2019-01-14 18:35:25 +00:00
|
|
|
} catch (const uhd::exception& e) {
|
2011-06-24 23:09:22 +00:00
|
|
|
std::cout << e.what() << std::endl;
|
2011-06-24 22:22:55 +00:00
|
|
|
BOOST_CHECK(false);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-14 18:35:25 +00:00
|
|
|
delete exception_clone; // manual cleanup
|
2010-05-02 04:35:11 +00:00
|
|
|
}
|