]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/test/test/execution_monitor-ts/errors-handling-test.cpp
1 // (C) Copyright Gennadiy Rozental 2001-2015.
2 // (C) Copyright Beman Dawes 2001.
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)
8 // See http://www.boost.org/libs/test for the library home page.
12 // Version : $Revision$
14 // Description : tests an ability of Unit Test Framework to catch all kinds
15 // of test errors in a user code and properly report it.
16 // ***************************************************************************
19 #define BOOST_TEST_MAIN
20 #include <boost/test/unit_test.hpp>
21 #include <boost/test/tools/output_test_stream.hpp>
22 #include <boost/test/unit_test_log.hpp>
23 #include <boost/test/unit_test_suite.hpp>
24 #include <boost/test/framework.hpp>
25 #include <boost/test/tree/test_unit.hpp>
26 #include <boost/test/unit_test_parameters.hpp>
27 #include <boost/test/output/compiler_log_formatter.hpp>
28 #include <boost/test/results_reporter.hpp>
29 #include "../framework-ts/logger-for-tests.hpp"
34 using namespace boost::unit_test
;
35 using namespace boost::test_tools
;
37 #if defined(__GNUC__) || defined(__SUNPRO_CC) || defined(__DECCXX_VER) || defined(BOOST_EMBTC)
42 struct this_test_log_formatter
: public boost::unit_test::output::compiler_log_formatter
44 void print_prefix( std::ostream
& output
, boost::unit_test::const_string
, std::size_t line
)
46 output
<< line
<< ": ";
49 void test_unit_finish( std::ostream
& output
, test_unit
const& tu
, unsigned long )
51 output
<< "Leaving test " << tu
.p_type_name
<< " \"" << tu
.p_name
<< "\"" << std::endl
;
56 //____________________________________________________________________________//
58 char const* log_level_name
[] = {
59 "log_successful_tests",
64 "log_cpp_exception_errors",
70 enum error_type_enum
{
87 char const* error_type_name
[] = {
88 "no error", "user message", "user warning",
89 "user non-fatal error", "cpp exception", " system error",
90 "user fatal error", "system fatal error"
93 int divide_by_zero
= 0;
95 void error_on_demand()
97 switch( error_type
) {
99 BOOST_CHECK_MESSAGE( divide_by_zero
== 0, "no error" );
103 BOOST_TEST_MESSAGE( "message" );
107 BOOST_WARN_MESSAGE( divide_by_zero
!= 0, "warning" );
111 BOOST_ERROR( "non-fatal error" );
115 BOOST_FAIL( "fatal error" );
117 BOOST_ERROR( "Should never reach this code!" );
120 case et_cpp_exception
:
121 BOOST_TEST_CHECKPOINT( "error_on_demand() throw runtime_error" );
122 throw std::runtime_error( "test std::runtime error what() message" );
126 BOOST_TEST_CHECKPOINT( "error_on_demand() divide by zero" );
127 divide_by_zero
= 1 / divide_by_zero
;
130 case et_fatal_system
:
131 BOOST_TEST_CHECKPOINT( "write to an invalid address" );
136 BOOST_ERROR( "Should never reach this code!" );
141 BOOST_ERROR( "Should never reach this code!" );
148 //____________________________________________________________________________//
150 BOOST_AUTO_TEST_CASE( test_errors_handling
)
152 #define PATTERN_FILE_NAME "errors-handling-test.pattern"
153 std::string
pattern_file_name(
154 framework::master_test_suite().argc
<= 1
155 ? (runtime_config::save_pattern() ? PATTERN_FILE_NAME
: "./baseline-outputs/" PATTERN_FILE_NAME
)
156 : framework::master_test_suite().argv
[1] );
159 pattern_file_name
+= "2";
162 output_test_stream
test_output( pattern_file_name
, !runtime_config::save_pattern() );
164 test_case
* test
= BOOST_TEST_CASE( &error_on_demand
);
165 test
->p_default_status
.value
= test_unit::RS_ENABLED
;
166 framework::finalize_setup_phase( test
->p_id
);
168 // for each log level
169 for( log_level level
= log_successful_tests
;
170 level
<= log_nothing
;
171 level
= static_cast<log_level
>(level
+1) )
173 // for each error type
174 for( error_type
= et_begin
;
175 error_type
!= et_end
;
176 error_type
= static_cast<error_type_enum
>(error_type
+1) )
178 test_output
<< "\n===========================\n"
179 << "log level: " << log_level_name
[level
] << ';'
180 << " error type: " << error_type_name
[error_type
] << ";\n" << std::endl
;
182 unit_test_log
.set_formatter( new this_test_log_formatter
);
183 log_setup_teardown
holder(test_output
, OF_CUSTOM_LOGGER
, level
);
185 framework::run( test
);
188 BOOST_CHECK( test_output
.match_pattern() );
193 //____________________________________________________________________________//