📄 test_error_handling.cpp
字号:
// Copyright Paul A. Bristow 2006-7.// Copyright John Maddock 2006-7.// Use, modification and distribution are subject to the// Boost Software License, Version 1.0.// (See accompanying file LICENSE_1_0.txt// or copy at http://www.boost.org/LICENSE_1_0.txt)// Test error handling mechanism produces the expected error messages.// for example Error in function boost::math::test_function<float>(float, float, float): Domain Error evaluating function at 0// Define some custom dummy error handlers that do nothing but throw,// in order to check that they are otherwise undefined.// The user MUST define them before they can be used.//struct user_defined_error{};namespace boost{ namespace math{ namespace policies{template <class T>T user_domain_error(const char* , const char* , const T& ){ throw user_defined_error();}template <class T>T user_pole_error(const char* , const char* , const T& ){ throw user_defined_error();}template <class T>T user_overflow_error(const char* , const char* , const T& ){ throw user_defined_error();}template <class T>T user_underflow_error(const char* , const char* , const T& ){ throw user_defined_error();}template <class T>T user_denorm_error(const char* , const char* , const T& ){ throw user_defined_error();}template <class T>T user_evaluation_error(const char* , const char* , const T& ){ throw user_defined_error();}template <class T>T user_indeterminate_result_error(const char* , const char* , const T& ){ throw user_defined_error();}}}} // namespaces#include <boost/math/concepts/real_concept.hpp>#include <boost/math/policies/policy.hpp>#include <boost/math/policies/error_handling.hpp>#include <boost/test/included/test_exec_monitor.hpp> // for test_main//// Define some policies://using namespace boost::math::policies;policy< domain_error<throw_on_error>, pole_error<throw_on_error>, overflow_error<throw_on_error>, underflow_error<throw_on_error>, denorm_error<throw_on_error>, evaluation_error<throw_on_error>, indeterminate_result_error<throw_on_error> > throw_policy;policy< domain_error<errno_on_error>, pole_error<errno_on_error>, overflow_error<errno_on_error>, underflow_error<errno_on_error>, denorm_error<errno_on_error>, evaluation_error<errno_on_error>, indeterminate_result_error<errno_on_error> > errno_policy;policy< domain_error<ignore_error>, pole_error<ignore_error>, overflow_error<ignore_error>, underflow_error<ignore_error>, denorm_error<ignore_error>, evaluation_error<ignore_error>, indeterminate_result_error<ignore_error> > ignore_policy;policy< domain_error<user_error>, pole_error<user_error>, overflow_error<user_error>, underflow_error<user_error>, denorm_error<user_error>, evaluation_error<user_error>, indeterminate_result_error<user_error> > user_policy;policy<> default_policy;#define TEST_EXCEPTION(expression, exception)\ BOOST_CHECK_THROW(expression, exception);\ try{ expression; }catch(const exception& e){ std::cout << e.what() << std::endl; }template <class T>void test_error(T){ const char* func = "boost::math::test_function<%1%>(%1%, %1%, %1%)"; const char* msg1 = "Error while handling value %1%"; const char* msg2 = "Error message goes here..."; // Check that exception is thrown, catch and show the message, for example: // Error in function boost::math::test_function<float>(float, float, float): Error while handling value 0 TEST_EXCEPTION(boost::math::policies::raise_domain_error(func, msg1, T(0.0), throw_policy), std::domain_error); TEST_EXCEPTION(boost::math::policies::raise_domain_error(func, 0, T(0.0), throw_policy), std::domain_error); TEST_EXCEPTION(boost::math::policies::raise_pole_error(func, msg1, T(0.0), throw_policy), std::domain_error); TEST_EXCEPTION(boost::math::policies::raise_pole_error(func, 0, T(0.0), throw_policy), std::domain_error);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -