test_tools.ipp
来自「Boost provides free peer-reviewed portab」· IPP 代码 · 共 631 行 · 第 1/2 页
IPP
631 行
// (C) Copyright Gennadiy Rozental 2005-2008.// Distributed under 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)// See http://www.boost.org/libs/test for the library home page.//// File : $RCSfile$//// Version : $Revision: 49312 $//// Description : supplies offline implementation for the Test Tools// ***************************************************************************#ifndef BOOST_TEST_TEST_TOOLS_IPP_012205GER#define BOOST_TEST_TEST_TOOLS_IPP_012205GER// Boost.Test#include <boost/test/test_tools.hpp>#include <boost/test/unit_test_log.hpp>#include <boost/test/output_test_stream.hpp>#include <boost/test/framework.hpp>#include <boost/test/execution_monitor.hpp> // execution_aborted#include <boost/test/unit_test_suite_impl.hpp>// Boost#include <boost/config.hpp>// STL#include <fstream>#include <string>#include <cstring>#include <cctype>#include <cwchar>#include <stdexcept>#include <ios>// !! should we use #include <cstdarg>#include <stdarg.h>#include <boost/test/detail/suppress_warnings.hpp>//____________________________________________________________________________//# ifdef BOOST_NO_STDC_NAMESPACEnamespace std { using ::strcmp; using ::strlen; using ::isprint; }#if !defined( BOOST_NO_CWCHAR )namespace std { using ::wcscmp; }#endif# endifnamespace boost {namespace test_tools {// ************************************************************************** //// ************** print_log_value ************** //// ************************************************************************** //voidprint_log_value<char>::operator()( std::ostream& ostr, char t ){ if( (std::isprint)( (unsigned char)t ) ) ostr << '\'' << t << '\''; else ostr << std::hex#if BOOST_TEST_USE_STD_LOCALE << std::showbase#else << "0x"#endif << (int)t;}//____________________________________________________________________________//voidprint_log_value<unsigned char>::operator()( std::ostream& ostr, unsigned char t ){ ostr << std::hex // showbase is only available for new style streams:#if BOOST_TEST_USE_STD_LOCALE << std::showbase#else << "0x"#endif << (int)t;}//____________________________________________________________________________//voidprint_log_value<char const*>::operator()( std::ostream& ostr, char const* t ){ ostr << ( t ? t : "null string" );}//____________________________________________________________________________//voidprint_log_value<wchar_t const*>::operator()( std::ostream& ostr, wchar_t const* t ){ ostr << ( t ? t : L"null string" );}//____________________________________________________________________________//namespace tt_detail {// ************************************************************************** //// ************** TOOL BOX Implementation ************** //// ************************************************************************** //using ::boost::unit_test::lazy_ostream;boolcheck_impl( predicate_result const& pr, lazy_ostream const& check_descr, const_string file_name, std::size_t line_num, tool_level tl, check_type ct, std::size_t num_of_args, ... ){ using namespace unit_test; if( !framework::is_initialized() ) throw std::runtime_error( "can't use testing tools before framework is initialized" ); if( !!pr ) tl = PASS; log_level ll; char const* prefix; char const* suffix; switch( tl ) { case PASS: ll = log_successful_tests; prefix = "check "; suffix = " passed"; break; case WARN: ll = log_warnings; prefix = "condition "; suffix = " is not satisfied"; break; case CHECK: ll = log_all_errors; prefix = "check "; suffix = " failed"; break; case REQUIRE: ll = log_fatal_errors; prefix = "critical check "; suffix = " failed"; break; default: return true; } switch( ct ) { case CHECK_PRED: unit_test_log << unit_test::log::begin( file_name, line_num ) << ll << prefix << check_descr << suffix; if( !pr.has_empty_message() ) unit_test_log << ". " << pr.message(); unit_test_log << unit_test::log::end(); break; case CHECK_MSG: unit_test_log << unit_test::log::begin( file_name, line_num ) << ll; if( tl == PASS ) unit_test_log << prefix << "'" << check_descr << "'" << suffix; else unit_test_log << check_descr; if( !pr.has_empty_message() ) unit_test_log << ". " << pr.message(); unit_test_log << unit_test::log::end(); break; case CHECK_EQUAL: case CHECK_NE: case CHECK_LT: case CHECK_LE: case CHECK_GT: case CHECK_GE: { static char const* check_str [] = { " == ", " != ", " < " , " <= ", " > " , " >= " }; static char const* rever_str [] = { " != ", " == ", " >= ", " > " , " <= ", " < " }; va_list args; va_start( args, num_of_args ); char const* arg1_descr = va_arg( args, char const* ); lazy_ostream const* arg1_val = va_arg( args, lazy_ostream const* ); char const* arg2_descr = va_arg( args, char const* ); lazy_ostream const* arg2_val = va_arg( args, lazy_ostream const* ); unit_test_log << unit_test::log::begin( file_name, line_num ) << ll << prefix << arg1_descr << check_str[ct-CHECK_EQUAL] << arg2_descr << suffix; if( tl != PASS ) unit_test_log << " [" << *arg1_val << rever_str[ct-CHECK_EQUAL] << *arg2_val << "]" ; va_end( args ); if( !pr.has_empty_message() ) unit_test_log << ". " << pr.message(); unit_test_log << unit_test::log::end(); break; } case CHECK_CLOSE: case CHECK_CLOSE_FRACTION: { va_list args; va_start( args, num_of_args ); char const* arg1_descr = va_arg( args, char const* ); lazy_ostream const* arg1_val = va_arg( args, lazy_ostream const* ); char const* arg2_descr = va_arg( args, char const* ); lazy_ostream const* arg2_val = va_arg( args, lazy_ostream const* ); /* toler_descr = */ va_arg( args, char const* ); lazy_ostream const* toler_val = va_arg( args, lazy_ostream const* ); unit_test_log << unit_test::log::begin( file_name, line_num ) << ll; unit_test_log << "difference between " << arg1_descr << "{" << *arg1_val << "}" << " and " << arg2_descr << "{" << *arg2_val << "}" << ( tl == PASS ? " doesn't exceed " : " exceeds " ) << *toler_val; if( ct == CHECK_CLOSE ) unit_test_log << "%"; va_end( args ); if( !pr.has_empty_message() ) unit_test_log << ". " << pr.message(); unit_test_log << unit_test::log::end(); break; } case CHECK_SMALL: { va_list args; va_start( args, num_of_args ); char const* arg1_descr = va_arg( args, char const* ); lazy_ostream const* arg1_val = va_arg( args, lazy_ostream const* ); /* toler_descr = */ va_arg( args, char const* ); lazy_ostream const* toler_val = va_arg( args, lazy_ostream const* ); unit_test_log << unit_test::log::begin( file_name, line_num ) << ll; unit_test_log << "absolute value of " << arg1_descr << "{" << *arg1_val << "}" << ( tl == PASS ? " doesn't exceed " : " exceeds " ) << *toler_val; va_end( args ); if( !pr.has_empty_message() ) unit_test_log << ". " << pr.message(); unit_test_log << unit_test::log::end(); break; } case CHECK_PRED_WITH_ARGS: { unit_test_log << unit_test::log::begin( file_name, line_num ) << ll << prefix << check_descr; // print predicate call description { va_list args; va_start( args, num_of_args ); unit_test_log << "( "; for( std::size_t i = 0; i < num_of_args; ++i ) { unit_test_log << va_arg( args, char const* ); va_arg( args, lazy_ostream const* ); // skip argument value; if( i != num_of_args-1 ) unit_test_log << ", "; } unit_test_log << " )" << suffix; va_end( args ); } if( tl != PASS ) { va_list args; va_start( args, num_of_args ); unit_test_log << " for ( "; for( std::size_t i = 0; i < num_of_args; ++i ) { va_arg( args, char const* ); // skip argument description; unit_test_log << *va_arg( args, lazy_ostream const* ); if( i != num_of_args-1 ) unit_test_log << ", "; } unit_test_log << " )"; va_end( args ); } if( !pr.has_empty_message() ) unit_test_log << ". " << pr.message(); unit_test_log << unit_test::log::end(); break; } case CHECK_EQUAL_COLL: { va_list args; va_start( args, num_of_args );
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?