1 // (C) Copyright Gennadiy Rozental 2001.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
6 // See http://www.boost.org/libs/test for the library home page.
10 // Version : $Revision$
12 // Description : supplies offline implementation for the Test Tools
13 // ***************************************************************************
15 #ifndef BOOST_TEST_TEST_TOOLS_IPP_012205GER
16 #define BOOST_TEST_TEST_TOOLS_IPP_012205GER
19 #include <boost/test/test_tools.hpp>
20 #include <boost/test/unit_test_log.hpp>
21 #include <boost/test/tools/context.hpp>
22 #include <boost/test/tools/output_test_stream.hpp>
24 #include <boost/test/tools/detail/fwd.hpp>
25 #include <boost/test/tools/detail/print_helper.hpp>
27 #include <boost/test/framework.hpp>
28 #include <boost/test/tree/test_unit.hpp>
29 #include <boost/test/execution_monitor.hpp> // execution_aborted
31 #include <boost/test/detail/throw_exception.hpp>
33 #include <boost/test/utils/algorithm.hpp>
36 #include <boost/config.hpp>
49 // !! should we use #include <cstdarg>
52 #include <boost/test/detail/suppress_warnings.hpp>
54 //____________________________________________________________________________//
56 # ifdef BOOST_NO_STDC_NAMESPACE
57 namespace std { using ::strcmp; using ::strlen; using ::isprint; }
58 #if !defined( BOOST_NO_CWCHAR )
59 namespace std { using ::wcscmp; }
66 // local static variable, needed here for visibility reasons
67 lazy_ostream lazy_ostream::inst = lazy_ostream();
71 namespace test_tools {
74 // ************************************************************************** //
75 // ************** print_log_value ************** //
76 // ************************************************************************** //
79 print_log_value<bool>::operator()( std::ostream& ostr, bool t )
81 ostr << std::boolalpha << t;
85 print_log_value<char>::operator()( std::ostream& ostr, char t )
87 if( (std::isprint)( static_cast<unsigned char>(t) ) )
88 ostr << '\'' << t << '\'';
91 #if BOOST_TEST_USE_STD_LOCALE
96 << static_cast<int>(t);
99 //____________________________________________________________________________//
102 print_log_value<unsigned char>::operator()( std::ostream& ostr, unsigned char t )
105 // showbase is only available for new style streams:
106 #if BOOST_TEST_USE_STD_LOCALE
111 << static_cast<int>(t);
114 //____________________________________________________________________________//
117 print_log_value<char const*>::operator()( std::ostream& ostr, char const* t )
119 ostr << ( t ? t : "null string" );
122 //____________________________________________________________________________//
125 print_log_value<wchar_t const*>::operator()( std::ostream& ostr, wchar_t const* t )
128 ostr << static_cast<const void*>(t);
131 ostr << "null w-string";
135 //____________________________________________________________________________//
137 // ************************************************************************** //
138 // ************** TOOL BOX Implementation ************** //
139 // ************************************************************************** //
141 using ::boost::unit_test::lazy_ostream;
143 static char const* check_str [] = { " == ", " != ", " < " , " <= ", " > " , " >= " };
144 static char const* rever_str [] = { " != ", " == ", " >= ", " > " , " <= ", " < " };
146 template<typename OutStream>
148 format_report( OutStream& os, assertion_result const& pr, unit_test::lazy_ostream const& assertion_descr,
149 tool_level tl, check_type ct,
150 std::size_t num_args, va_list args,
151 char const* prefix, char const* suffix )
153 using namespace unit_test;
157 os << prefix << assertion_descr << suffix;
159 if( !pr.has_empty_message() )
160 os << ". " << pr.message();
163 case CHECK_BUILT_ASSERTION: {
164 os << prefix << assertion_descr << suffix;
167 const_string details_message = pr.message();
169 if( !details_message.is_empty() ) {
170 os << details_message;
178 os << prefix << "'" << assertion_descr << "'" << suffix;
180 os << assertion_descr;
182 if( !pr.has_empty_message() )
183 os << ". " << pr.message();
192 char const* arg1_descr = va_arg( args, char const* );
193 lazy_ostream const* arg1_val = va_arg( args, lazy_ostream const* );
194 char const* arg2_descr = va_arg( args, char const* );
195 lazy_ostream const* arg2_val = va_arg( args, lazy_ostream const* );
197 os << prefix << arg1_descr << check_str[ct-CHECK_EQUAL] << arg2_descr << suffix;
200 os << " [" << *arg1_val << rever_str[ct-CHECK_EQUAL] << *arg2_val << "]" ;
202 if( !pr.has_empty_message() )
203 os << ". " << pr.message();
208 case CHECK_CLOSE_FRACTION: {
209 char const* arg1_descr = va_arg( args, char const* );
210 lazy_ostream const* arg1_val = va_arg( args, lazy_ostream const* );
211 char const* arg2_descr = va_arg( args, char const* );
212 lazy_ostream const* arg2_val = va_arg( args, lazy_ostream const* );
213 /* toler_descr = */ va_arg( args, char const* );
214 lazy_ostream const* toler_val = va_arg( args, lazy_ostream const* );
216 os << "difference{" << pr.message()
217 << "} between " << arg1_descr << "{" << *arg1_val
218 << "} and " << arg2_descr << "{" << *arg2_val
219 << ( tl == PASS ? "} doesn't exceed " : "} exceeds " )
221 if( ct == CHECK_CLOSE )
226 char const* arg1_descr = va_arg( args, char const* );
227 lazy_ostream const* arg1_val = va_arg( args, lazy_ostream const* );
228 /* toler_descr = */ va_arg( args, char const* );
229 lazy_ostream const* toler_val = va_arg( args, lazy_ostream const* );
231 os << "absolute value of " << arg1_descr << "{" << *arg1_val << "}"
232 << ( tl == PASS ? " doesn't exceed " : " exceeds " )
235 if( !pr.has_empty_message() )
236 os << ". " << pr.message();
240 case CHECK_PRED_WITH_ARGS: {
241 std::vector< std::pair<char const*, lazy_ostream const*> > args_copy;
242 args_copy.reserve( num_args );
243 for( std::size_t i = 0; i < num_args; ++i ) {
244 char const* desc = va_arg( args, char const* );
245 lazy_ostream const* value = va_arg( args, lazy_ostream const* );
246 args_copy.push_back( std::make_pair( desc, value ) );
249 os << prefix << assertion_descr;
251 // print predicate call description
253 for( std::size_t i = 0; i < num_args; ++i ) {
254 os << args_copy[i].first;
256 if( i != num_args-1 )
259 os << " )" << suffix;
263 for( std::size_t i = 0; i < num_args; ++i ) {
264 os << *args_copy[i].second;
266 if( i != num_args-1 )
272 if( !pr.has_empty_message() )
273 os << ". " << pr.message();
277 case CHECK_EQUAL_COLL: {
278 char const* left_begin_descr = va_arg( args, char const* );
279 char const* left_end_descr = va_arg( args, char const* );
280 char const* right_begin_descr = va_arg( args, char const* );
281 char const* right_end_descr = va_arg( args, char const* );
283 os << prefix << "{ " << left_begin_descr << ", " << left_end_descr << " } == { "
284 << right_begin_descr << ", " << right_end_descr << " }"
287 if( !pr.has_empty_message() )
288 os << ". " << pr.message();
292 case CHECK_BITWISE_EQUAL: {
293 char const* left_descr = va_arg( args, char const* );
294 char const* right_descr = va_arg( args, char const* );
296 os << prefix << left_descr << " =.= " << right_descr << suffix;
298 if( !pr.has_empty_message() )
299 os << ". " << pr.message();
305 //____________________________________________________________________________//
308 report_assertion( assertion_result const& ar,
309 lazy_ostream const& assertion_descr,
310 const_string file_name,
311 std::size_t line_num,
314 std::size_t num_args, ... )
316 using namespace unit_test;
318 if( !framework::test_in_progress() ) {
319 // in case no test is in progress, we do not throw anything:
320 // raising an exception here may result in raising an exception in a destructor of a global fixture
321 // which will abort the process
322 // We flag this as aborted instead
324 //BOOST_TEST_I_ASSRT( framework::current_test_case_id() != INV_TEST_UNIT_ID,
325 // std::runtime_error( "Can't use testing tools outside of test case implementation." ) );
327 framework::test_aborted();
341 ll = log_successful_tests;
343 suffix = " has passed";
347 prefix = "condition ";
348 suffix = " is not satisfied";
353 suffix = " has failed";
356 ll = log_fatal_errors;
357 prefix = "critical check ";
358 suffix = " has failed";
364 unit_test_log << unit_test::log::begin( file_name, line_num ) << ll;
366 va_start( args, num_args );
368 format_report( unit_test_log, ar, assertion_descr, tl, ct, num_args, args, prefix, suffix );
371 unit_test_log << unit_test::log::end();
375 framework::assertion_result( AR_PASSED );
379 framework::assertion_result( AR_TRIGGERED );
383 framework::assertion_result( AR_FAILED );
387 framework::assertion_result( AR_FAILED );
388 framework::test_unit_aborted( framework::current_test_unit() );
389 BOOST_TEST_I_THROW( execution_aborted() );
390 // the previous line either throws or aborts and the return below is not reached
392 BOOST_TEST_UNREACHABLE_RETURN(false);
398 //____________________________________________________________________________//
401 format_assertion_result( const_string expr_val, const_string details )
403 assertion_result res(false);
405 bool starts_new_line = first_char( expr_val ) == '\n';
407 if( !starts_new_line && !expr_val.is_empty() )
408 res.message().stream() << " [" << expr_val << "]";
410 if( !details.is_empty() ) {
411 if( first_char(details) != '[' )
412 res.message().stream() << ": ";
414 res.message().stream() << " ";
416 res.message().stream() << details;
419 if( starts_new_line )
420 res.message().stream() << "." << expr_val;
425 //____________________________________________________________________________//
427 BOOST_TEST_DECL std::string
428 prod_report_format( assertion_result const& ar, unit_test::lazy_ostream const& assertion_descr, check_type ct, std::size_t num_args, ... )
430 std::ostringstream msg_buff;
433 va_start( args, num_args );
435 format_report( msg_buff, ar, assertion_descr, CHECK, ct, num_args, args, "assertion ", " failed" );
439 return msg_buff.str();
442 //____________________________________________________________________________//
445 equal_impl( char const* left, char const* right )
447 return (left && right) ? std::strcmp( left, right ) == 0 : (left == right);
450 //____________________________________________________________________________//
452 #if !defined( BOOST_NO_CWCHAR )
455 equal_impl( wchar_t const* left, wchar_t const* right )
457 return (left && right) ? std::wcscmp( left, right ) == 0 : (left == right);
460 #endif // !defined( BOOST_NO_CWCHAR )
462 //____________________________________________________________________________//
465 is_defined_impl( const_string symbol_name, const_string symbol_value )
467 symbol_value.trim_left( 2 );
468 return symbol_name != symbol_value;
471 //____________________________________________________________________________//
473 // ************************************************************************** //
474 // ************** context_frame ************** //
475 // ************************************************************************** //
477 context_frame::context_frame( ::boost::unit_test::lazy_ostream const& context_descr )
478 : m_frame_id( unit_test::framework::add_context( context_descr, true ) )
482 //____________________________________________________________________________//
484 context_frame::~context_frame()
486 unit_test::framework::clear_context( m_frame_id );
489 //____________________________________________________________________________//
491 context_frame::operator bool()
496 //____________________________________________________________________________//
498 } // namespace tt_detail
500 // ************************************************************************** //
501 // ************** output_test_stream ************** //
502 // ************************************************************************** //
504 struct output_test_stream::Impl
506 std::fstream m_pattern;
507 bool m_match_or_save;
508 bool m_text_or_binary;
509 std::string m_synced_string;
515 m_pattern.get( res );
516 } while( m_text_or_binary && res == '\r' && !m_pattern.fail() && !m_pattern.eof() );
521 void check_and_fill( assertion_result& res )
523 if( !res.p_predicate_value )
524 res.message() << "Output content: \"" << m_synced_string << '\"';
528 //____________________________________________________________________________//
530 output_test_stream::output_test_stream( const_string pattern_file_name, bool match_or_save, bool text_or_binary )
531 : m_pimpl( new Impl )
533 if( !pattern_file_name.is_empty() ) {
534 std::ios::openmode m = match_or_save ? std::ios::in : std::ios::out;
535 if( !text_or_binary )
536 m |= std::ios::binary;
538 m_pimpl->m_pattern.open( pattern_file_name.begin(), m );
540 if( !m_pimpl->m_pattern.is_open() )
541 BOOST_TEST_FRAMEWORK_MESSAGE( "Can't open pattern file " << pattern_file_name << " for " << (match_or_save ? "reading" : "writing") );
544 m_pimpl->m_match_or_save = match_or_save;
545 m_pimpl->m_text_or_binary = text_or_binary;
548 //____________________________________________________________________________//
550 output_test_stream::~output_test_stream()
555 //____________________________________________________________________________//
558 output_test_stream::is_empty( bool flush_stream )
562 assertion_result res( m_pimpl->m_synced_string.empty() );
564 m_pimpl->check_and_fill( res );
572 //____________________________________________________________________________//
575 output_test_stream::check_length( std::size_t length_, bool flush_stream )
579 assertion_result res( m_pimpl->m_synced_string.length() == length_ );
581 m_pimpl->check_and_fill( res );
589 //____________________________________________________________________________//
592 output_test_stream::is_equal( const_string arg, bool flush_stream )
596 assertion_result res( const_string( m_pimpl->m_synced_string ) == arg );
598 m_pimpl->check_and_fill( res );
606 //____________________________________________________________________________//
608 std::string pretty_print_log(std::string str) {
610 static const std::string to_replace[] = { "\r", "\n" };
611 static const std::string replacement[] = { "\\r", "\\n" };
613 return unit_test::utils::replace_all_occurrences_of(
615 to_replace, to_replace + sizeof(to_replace)/sizeof(to_replace[0]),
616 replacement, replacement + sizeof(replacement)/sizeof(replacement[0]));
620 output_test_stream::match_pattern( bool flush_stream )
622 const std::string::size_type n_chars_presuffix = 10;
625 assertion_result result( true );
627 const std::string stream_string_repr = get_stream_string_representation();
629 if( !m_pimpl->m_pattern.is_open() ) {
631 result.message() << "Pattern file can't be opened!";
634 if( m_pimpl->m_match_or_save ) {
637 std::vector<char> last_elements;
638 for ( std::string::size_type i = 0; static_cast<int>(i + offset) < static_cast<int>(stream_string_repr.length()); ++i ) {
640 char c = m_pimpl->get_char();
642 if( last_elements.size() <= n_chars_presuffix ) {
643 last_elements.push_back( c );
646 last_elements[ i % last_elements.size() ] = c;
649 bool is_same = !m_pimpl->m_pattern.fail() &&
650 !m_pimpl->m_pattern.eof() &&
651 (stream_string_repr[i+offset] == c);
657 std::string::size_type prefix_size = (std::min)( i + offset, n_chars_presuffix );
659 std::string::size_type suffix_size = (std::min)( stream_string_repr.length() - i - offset,
662 // try to log area around the mismatch
663 std::string substr = stream_string_repr.substr(0, i+offset);
664 std::size_t line = std::count(substr.begin(), substr.end(), '\n');
665 std::size_t column = i + offset - substr.rfind('\n');
668 << "Mismatch at position " << i
670 << ", column " << column
671 << "): '" << pretty_print_log(std::string(1, stream_string_repr[i+offset])) << "' != '" << pretty_print_log(std::string(1, c)) << "' :\n";
673 // we already escape this substring because we need its actual size for the pretty print
674 // of the difference location.
675 std::string sub_str_prefix(pretty_print_log(stream_string_repr.substr( i + offset - prefix_size, prefix_size )));
677 // we need this substring as is because we compute the best matching substrings on it.
678 std::string sub_str_suffix(stream_string_repr.substr( i + offset, suffix_size));
679 result.message() << "... " << sub_str_prefix + pretty_print_log(sub_str_suffix) << " ..." << '\n';
681 result.message() << "... ";
682 for( std::size_t j = 0; j < last_elements.size() ; j++ )
683 result.message() << pretty_print_log(std::string(1, last_elements[(i + j + 1) % last_elements.size()]));
685 std::vector<char> last_elements_ordered;
686 last_elements_ordered.push_back(c);
687 for( std::string::size_type counter = 0; counter < suffix_size - 1 ; counter++ ) {
688 char c2 = m_pimpl->get_char();
690 if( m_pimpl->m_pattern.fail() || m_pimpl->m_pattern.eof() )
693 result.message() << pretty_print_log(std::string(1, c2));
695 last_elements_ordered.push_back(c2);
698 // tries to find the best substring matching in the remainder of the
700 std::size_t max_nb_char_in_common = 0;
701 std::size_t best_pattern_start_index = 0;
702 std::size_t best_stream_start_index = 0;
703 for( std::size_t pattern_start_index = best_pattern_start_index;
704 pattern_start_index < last_elements_ordered.size();
705 pattern_start_index++ ) {
706 for( std::size_t stream_start_index = best_stream_start_index;
707 stream_start_index < sub_str_suffix.size();
708 stream_start_index++ ) {
710 std::size_t max_size = (std::min)( last_elements_ordered.size() - pattern_start_index, sub_str_suffix.size() - stream_start_index );
711 if( max_nb_char_in_common > max_size )
712 break; // safely break to go to the outer loop
714 std::size_t nb_char_in_common = 0;
715 for( std::size_t k = 0; k < max_size; k++) {
716 if( last_elements_ordered[pattern_start_index + k] == sub_str_suffix[stream_start_index + k] )
717 nb_char_in_common ++;
719 break; // we take fully matching substring only
722 if( nb_char_in_common > max_nb_char_in_common ) {
723 max_nb_char_in_common = nb_char_in_common;
724 best_pattern_start_index = pattern_start_index;
725 best_stream_start_index = stream_start_index;
730 // indicates with more precision the location of the mismatchs in "ascii arts" ...
731 result.message() << " ...\n... ";
732 for( std::string::size_type j = 0; j < sub_str_prefix.size(); j++) {
733 result.message() << ' ';
736 result.message() << '~'; // places the first tilde at the current char that mismatches
738 for( std::size_t k = 1; k < (std::max)(best_pattern_start_index, best_stream_start_index); k++ ) { // 1 is for the current char c
739 std::string s1(pretty_print_log(std::string(1, last_elements_ordered[(std::min)(k, best_pattern_start_index)])));
740 std::string s2(pretty_print_log(std::string(1, sub_str_suffix[(std::min)(k, best_stream_start_index)])));
741 for( int h = static_cast<int>((std::max)(s1.size(), s2.size())); h > 0; h--)
742 result.message() << "~";
745 if( m_pimpl->m_pattern.eof() ) {
746 result.message() << " (reference string shorter than current stream)";
749 result.message() << "\n";
751 // no need to continue if the EOF is reached
752 if( m_pimpl->m_pattern.eof() ) {
756 // first char is a replicat of c, so we do not copy it.
757 for(std::string::size_type counter = 0; counter < last_elements_ordered.size() - 1 ; counter++)
758 last_elements[ (i + 1 + counter) % last_elements.size() ] = last_elements_ordered[counter + 1];
760 i += last_elements_ordered.size()-1;
761 offset += best_stream_start_index - best_pattern_start_index;
767 // not needed anymore
769 if(offset > 0 && false) {
770 m_pimpl->m_pattern.ignore(
771 static_cast<std::streamsize>( offset ));
776 m_pimpl->m_pattern.write( stream_string_repr.c_str(),
777 static_cast<std::streamsize>( stream_string_repr.length() ) );
778 m_pimpl->m_pattern.flush();
788 //____________________________________________________________________________//
791 output_test_stream::flush()
793 m_pimpl->m_synced_string.erase();
795 #ifndef BOOST_NO_STRINGSTREAM
796 str( std::string() );
798 seekp( 0, std::ios::beg );
804 output_test_stream::get_stream_string_representation() const {
805 return m_pimpl->m_synced_string;
808 //____________________________________________________________________________//
811 output_test_stream::length()
815 return m_pimpl->m_synced_string.length();
818 //____________________________________________________________________________//
821 output_test_stream::sync()
823 #ifdef BOOST_NO_STRINGSTREAM
824 m_pimpl->m_synced_string.assign( str(), pcount() );
827 m_pimpl->m_synced_string = str();
831 //____________________________________________________________________________//
833 } // namespace test_tools
836 #include <boost/test/detail/enable_warnings.hpp>
838 #endif // BOOST_TEST_TEST_TOOLS_IPP_012205GER