📄 test_tools_test.cpp
字号:
// (C) Copyright Gennadiy Rozental 2001-2003.
// 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)
// See http://www.boost.org/libs/test for the library home page.
//
// File : $RCSfile: test_tools_test.cpp,v $
//
// Version : $Revision: 1.28 $
//
// Description : tests all Test Tools but output_test_stream
// ***************************************************************************
// Boost.Test
#include <boost/test/unit_test.hpp>
#include <boost/test/unit_test_result.hpp>
using namespace boost::unit_test_framework;
using boost::test_toolbox::extended_predicate_value;
// BOOST
#include <boost/bind.hpp>
// STL
#include <iostream>
#include <iomanip>
#include <list>
#include <typeinfo>
#include <cassert>
//____________________________________________________________________________//
#define CHECK_TOOL_USAGE( tool_usage, check ) \
{ \
boost::test_toolbox::output_test_stream output; \
\
unit_test_log::instance().set_log_stream( output ); \
{ unit_test_result_saver saver; \
tool_usage; \
} \
unit_test_log::instance().set_log_stream( std::cout ); \
BOOST_CHECK( check ); \
}
//____________________________________________________________________________//
#define CHECK_CRITICAL_TOOL_USAGE( tool_usage, nothrow_check, throw_check ) \
{ \
boost::test_toolbox::output_test_stream output; \
\
unit_test_log::instance().set_log_stream( output ); \
try { \
{ unit_test_result_saver saver; \
tool_usage; \
} \
unit_test_log::instance().set_log_stream( std::cout ); \
BOOST_CHECK( nothrow_check ); \
} catch( boost::test_toolbox::detail::test_tool_failed const&) { \
unit_test_log::instance().set_log_stream( std::cout ); \
BOOST_CHECK( throw_check ); \
} \
}
//____________________________________________________________________________//
char
set_unix_slash( char in )
{
return in == '\\' ? '/' : in;
}
static std::string const&
normalize_file_name( char const* f )
{
static std::string buffer;
buffer = f;
std::transform( buffer.begin(), buffer.end(), buffer.begin(), &set_unix_slash );
return buffer;
}
#if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x570)) || BOOST_WORKAROUND(__IBMCPP__, BOOST_TESTED_AT(600))
#define CHECK_PATTERN( msg, shift ) \
(boost::wrap_stringstream().ref() << normalize_file_name( __FILE__ ) << "(" << (__LINE__-shift) << "): " << msg).str()
#else
#define CHECK_PATTERN( msg, shift ) \
(boost::wrap_stringstream().ref() << normalize_file_name( __FILE__ ) << "(" << __LINE__ << "): " << msg).str()
#endif
//____________________________________________________________________________//
class bool_convertible
{
struct Tester {};
public:
operator Tester*() { return static_cast<Tester*>( 0 ) + 1; }
};
void
test_BOOST_CHECK()
{
#define TEST_CASE_NAME << '\"' << "test_BOOST_CHECK" << '\"' <<
unit_test_log::instance().set_log_threshold_level( log_all_errors );
CHECK_TOOL_USAGE(
BOOST_CHECK( true ),
output.is_empty()
);
bool_convertible bc;
CHECK_TOOL_USAGE(
BOOST_CHECK( bc ),
output.is_empty()
);
CHECK_TOOL_USAGE(
BOOST_CHECK( false ),
output.is_equal( CHECK_PATTERN( "error in " TEST_CASE_NAME ": test false failed\n", 2 ) )
);
CHECK_TOOL_USAGE(
BOOST_CHECK( 1==2 ),
output.is_equal( CHECK_PATTERN( "error in " TEST_CASE_NAME ": test 1==2 failed\n", 2 ) )
);
int i=2;
CHECK_TOOL_USAGE(
BOOST_CHECK( i==1 ),
output.is_equal( CHECK_PATTERN( "error in " TEST_CASE_NAME ": test i==1 failed\n", 2 ) )
);
unit_test_log::instance().set_log_threshold_level( log_successful_tests );
CHECK_TOOL_USAGE(
BOOST_CHECK( i==2 ),
output.is_equal( CHECK_PATTERN( "info: test i==2 passed\n", 2 ) )
);
unit_test_log::instance().set_log_threshold_level( log_all_errors );
}
//____________________________________________________________________________//
void
test_BOOST_REQUIRE()
{
#undef TEST_CASE_NAME
#define TEST_CASE_NAME << '\"' << "test_BOOST_REQUIRE" << '\"' <<
unit_test_log::instance().set_log_threshold_level( log_all_errors );
CHECK_CRITICAL_TOOL_USAGE(
BOOST_REQUIRE( true ),
true, false
);
CHECK_CRITICAL_TOOL_USAGE(
BOOST_REQUIRE( false ),
false, true
);
int j = 3;
CHECK_CRITICAL_TOOL_USAGE(
BOOST_REQUIRE( j > 5 ),
false, output.is_equal( CHECK_PATTERN( "fatal error in " TEST_CASE_NAME ": test j > 5 failed\n", 2 ) )
);
unit_test_log::instance().set_log_threshold_level( log_successful_tests );
CHECK_CRITICAL_TOOL_USAGE(
BOOST_REQUIRE( j < 5 ),
output.is_equal( CHECK_PATTERN( "info: test j < 5 passed\n", 1 ) ) , false
);
unit_test_log::instance().set_log_threshold_level( log_all_errors );
}
//____________________________________________________________________________//
struct A {
friend std::ostream& operator<<( std::ostream& str, A const& a ) { str << "struct A"; return str;}
};
void
test_BOOST_MESSAGE()
{
#undef TEST_CASE_NAME
#define TEST_CASE_NAME << '\"' << "test_BOOST_REQUIRE" << '\"' <<
unit_test_log::instance().set_log_threshold_level( log_messages );
CHECK_TOOL_USAGE(
BOOST_MESSAGE( "still testing" ),
output.is_equal( "still testing\n" )
);
CHECK_TOOL_USAGE(
BOOST_MESSAGE( "1+1=" << 2 ),
output.is_equal( "1+1=2\n" )
);
int i = 2;
CHECK_TOOL_USAGE(
BOOST_MESSAGE( i << "+" << i << "=" << (i+i) ),
output.is_equal( "2+2=4\n" )
);
A a = A();
CHECK_TOOL_USAGE(
BOOST_MESSAGE( a ),
output.is_equal( "struct A\n" )
);
#ifndef BOOST_NO_STD_LOCALE
CHECK_TOOL_USAGE(
BOOST_MESSAGE( std::hex << std::showbase << 20 ),
output.is_equal( "0x14\n" )
);
#else
CHECK_TOOL_USAGE(
BOOST_MESSAGE( std::hex << "0x" << 20 ),
output.is_equal( "0x14\n" )
);
#endif
CHECK_TOOL_USAGE(
BOOST_MESSAGE( std::setw( 4 ) << 20 ),
output.is_equal( " 20\n" )
);
}
//____________________________________________________________________________//
void
test_BOOST_WARN()
{
#undef TEST_CASE_NAME
#define TEST_CASE_NAME << '\"' << "test_BOOST_WARN" << '\"' <<
unit_test_log::instance().set_log_threshold_level( log_warnings );
CHECK_TOOL_USAGE(
BOOST_WARN( sizeof(int) == sizeof(short) ),
output.is_equal( CHECK_PATTERN( "warning in " TEST_CASE_NAME
": condition sizeof(int) == sizeof(short) is not satisfied\n", 3 ) )
);
}
//____________________________________________________________________________//
class bad_func_container : public test_case
{
public:
bad_func_container() : test_case( "test_BOOST_CHECKPOINT", true, 1 ) {}
void do_run() {
throw "some error";
}
} bad;
void
test_BOOST_CHECKPOINT()
{
#undef TEST_CASE_NAME
#define TEST_CASE_NAME << '\"' << "test_BOOST_CHECKPOINT" << '\"' <<
unit_test_log::instance().set_log_threshold_level( log_all_errors );
BOOST_CHECKPOINT( "Going to do a silly things" );
CHECK_TOOL_USAGE(
bad.run(),
output.is_equal(
(boost::wrap_stringstream().ref()
<< "Exception in " TEST_CASE_NAME ": C string: some error\n"
<< normalize_file_name( __FILE__ ) << "(" << 277 << "): "
<< "last checkpoint: Going to do a silly things\n").str()
)
);
}
//____________________________________________________________________________//
void
test_BOOST_WARN_MESSAGE()
{
#undef TEST_CASE_NAME
#define TEST_CASE_NAME << '\"' << "test_BOOST_WARN_MESSAGE" << '\"' <<
unit_test_log::instance().set_log_threshold_level( log_warnings );
CHECK_TOOL_USAGE(
BOOST_WARN_MESSAGE( sizeof(int) == sizeof(short), "memory won't be used efficiently" ),
output.is_equal( CHECK_PATTERN( "warning in " TEST_CASE_NAME ": memory won't be used efficiently\n", 2 ) )
);
int obj_size = 33;
CHECK_TOOL_USAGE(
BOOST_WARN_MESSAGE( obj_size <= 8, "object size " << obj_size << " too big to be efficiently passed by value" ),
output.is_equal( CHECK_PATTERN( "warning in " TEST_CASE_NAME
": object size 33 too big to be efficiently passed by value\n", 3 ) )
);
}
//____________________________________________________________________________//
void
test_BOOST_CHECK_MESSAGE()
{
#undef TEST_CASE_NAME
#define TEST_CASE_NAME << '\"' << "test_BOOST_CHECK_MESSAGE" << '\"' <<
unit_test_log::instance().set_log_threshold_level( log_all_errors );
CHECK_TOOL_USAGE(
BOOST_CHECK_MESSAGE( 2+2 == 5, "Well, may be that what I belive in" ),
output.is_equal( CHECK_PATTERN( "error in " TEST_CASE_NAME ": Well, may be that what I belive in\n", 2 ) )
);
}
//____________________________________________________________________________//
void
test_BOOST_REQUIRE_MESSAGE()
{
#undef TEST_CASE_NAME
#define TEST_CASE_NAME << '\"' << "test_BOOST_REQUIRE_MESSAGE" << '\"' <<
unit_test_log::instance().set_log_threshold_level( log_all_errors );
CHECK_CRITICAL_TOOL_USAGE(
BOOST_REQUIRE_MESSAGE( false, "Here we should stop" ),
false, output.is_equal(
CHECK_PATTERN( "fatal error in " TEST_CASE_NAME ": Here we should stop" << "\n", 3 ) )
);
}
//____________________________________________________________________________//
struct B {
B( int i ) : m_i( i ) {}
friend bool operator==( B const& b1, B const& b2 ) { return b1.m_i == b2.m_i; }
friend std::ostream& operator<<( std::ostream& str, B const& b ) { str << "B(" << b.m_i << ")"; return str; }
int m_i;
};
void
test_BOOST_CHECK_EQUAL()
{
#undef TEST_CASE_NAME
#define TEST_CASE_NAME << '\"' << "test_BOOST_CHECK_EQUAL" << '\"' <<
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -