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/unit_test_log.hpp>
20 #include <boost/test/tools/context.hpp>
21 #include <boost/test/tools/output_test_stream.hpp>
23 #include <boost/test/tools/detail/fwd.hpp>
24 #include <boost/test/tools/detail/print_helper.hpp>
26 #include <boost/test/framework.hpp>
27 #include <boost/test/tree/test_unit.hpp>
28 #include <boost/test/execution_monitor.hpp> // execution_aborted
30 #include <boost/test/detail/throw_exception.hpp>
32 #include <boost/test/utils/algorithm.hpp>
35 #include <boost/config.hpp>
48 // !! should we use #include <cstdarg>
51 #include <boost/test/detail/suppress_warnings.hpp>
53 //____________________________________________________________________________//
55 # ifdef BOOST_NO_STDC_NAMESPACE
56 namespace std { using ::strcmp; using ::strlen; using ::isprint; }
57 #if !defined( BOOST_NO_CWCHAR )
58 namespace std { using ::wcscmp; }
63 namespace test_tools {
66 // ************************************************************************** //
67 // ************** print_log_value ************** //
68 // ************************************************************************** //
71 print_log_value<bool>::operator()( std::ostream& ostr, bool t )
73 ostr << std::boolalpha << t;
77 print_log_value<char>::operator()( std::ostream& ostr, char t )
79 if( (std::isprint)( static_cast<unsigned char>(t) ) )
80 ostr << '\'' << t << '\'';
83 #if BOOST_TEST_USE_STD_LOCALE
88 << static_cast<int>(t);
91 //____________________________________________________________________________//
94 print_log_value<unsigned char>::operator()( std::ostream& ostr, unsigned char t )
97 // showbase is only available for new style streams:
98 #if BOOST_TEST_USE_STD_LOCALE
103 << static_cast<int>(t);
106 //____________________________________________________________________________//
109 print_log_value<char const*>::operator()( std::ostream& ostr, char const* t )
111 ostr << ( t ? t : "null string" );
114 //____________________________________________________________________________//
117 print_log_value<wchar_t const*>::operator()( std::ostream& ostr, wchar_t const* t )
119 ostr << ( t ? t : L"null string" );
122 #if !defined(BOOST_NO_CXX11_NULLPTR)
124 print_log_value<std::nullptr_t>::operator()( std::ostream& ostr, std::nullptr_t )
130 //____________________________________________________________________________//
132 // ************************************************************************** //
133 // ************** TOOL BOX Implementation ************** //
134 // ************************************************************************** //
136 using ::boost::unit_test::lazy_ostream;
138 static char const* check_str [] = { " == ", " != ", " < " , " <= ", " > " , " >= " };
139 static char const* rever_str [] = { " != ", " == ", " >= ", " > " , " <= ", " < " };
141 template<typename OutStream>
143 format_report( OutStream& os, assertion_result const& pr, unit_test::lazy_ostream const& assertion_descr,
144 tool_level tl, check_type ct,
145 std::size_t num_args, va_list args,
146 char const* prefix, char const* suffix )
148 using namespace unit_test;
152 os << prefix << assertion_descr << suffix;
154 if( !pr.has_empty_message() )
155 os << ". " << pr.message();
158 case CHECK_BUILT_ASSERTION: {
159 os << prefix << assertion_descr << suffix;
162 const_string details_message = pr.message();
164 if( !details_message.is_empty() ) {
165 os << details_message;
173 os << prefix << "'" << assertion_descr << "'" << suffix;
175 os << assertion_descr;
177 if( !pr.has_empty_message() )
178 os << ". " << pr.message();
187 char const* arg1_descr = va_arg( args, char const* );
188 lazy_ostream const* arg1_val = va_arg( args, lazy_ostream const* );
189 char const* arg2_descr = va_arg( args, char const* );
190 lazy_ostream const* arg2_val = va_arg( args, lazy_ostream const* );
192 os << prefix << arg1_descr << check_str[ct-CHECK_EQUAL] << arg2_descr << suffix;
195 os << " [" << *arg1_val << rever_str[ct-CHECK_EQUAL] << *arg2_val << "]" ;
197 if( !pr.has_empty_message() )
198 os << ". " << pr.message();
203 case CHECK_CLOSE_FRACTION: {
204 char const* arg1_descr = va_arg( args, char const* );
205 lazy_ostream const* arg1_val = va_arg( args, lazy_ostream const* );
206 char const* arg2_descr = va_arg( args, char const* );
207 lazy_ostream const* arg2_val = va_arg( args, lazy_ostream const* );
208 /* toler_descr = */ va_arg( args, char const* );
209 lazy_ostream const* toler_val = va_arg( args, lazy_ostream const* );
211 os << "difference{" << pr.message()
212 << "} between " << arg1_descr << "{" << *arg1_val
213 << "} and " << arg2_descr << "{" << *arg2_val
214 << ( tl == PASS ? "} doesn't exceed " : "} exceeds " )
216 if( ct == CHECK_CLOSE )
221 char const* arg1_descr = va_arg( args, char const* );
222 lazy_ostream const* arg1_val = va_arg( args, lazy_ostream const* );
223 /* toler_descr = */ va_arg( args, char const* );
224 lazy_ostream const* toler_val = va_arg( args, lazy_ostream const* );
226 os << "absolute value of " << arg1_descr << "{" << *arg1_val << "}"
227 << ( tl == PASS ? " doesn't exceed " : " exceeds " )
230 if( !pr.has_empty_message() )
231 os << ". " << pr.message();
235 case CHECK_PRED_WITH_ARGS: {
236 std::vector< std::pair<char const*, lazy_ostream const*> > args_copy;
237 args_copy.reserve( num_args );
238 for( std::size_t i = 0; i < num_args; ++i ) {
239 char const* desc = va_arg( args, char const* );
240 lazy_ostream const* value = va_arg( args, lazy_ostream const* );
241 args_copy.push_back( std::make_pair( desc, value ) );
244 os << prefix << assertion_descr;
246 // print predicate call description
248 for( std::size_t i = 0; i < num_args; ++i ) {
249 os << args_copy[i].first;
251 if( i != num_args-1 )
254 os << " )" << suffix;
258 for( std::size_t i = 0; i < num_args; ++i ) {
259 os << *args_copy[i].second;
261 if( i != num_args-1 )
267 if( !pr.has_empty_message() )
268 os << ". " << pr.message();
272 case CHECK_EQUAL_COLL: {
273 char const* left_begin_descr = va_arg( args, char const* );
274 char const* left_end_descr = va_arg( args, char const* );
275 char const* right_begin_descr = va_arg( args, char const* );
276 char const* right_end_descr = va_arg( args, char const* );
278 os << prefix << "{ " << left_begin_descr << ", " << left_end_descr << " } == { "
279 << right_begin_descr << ", " << right_end_descr << " }"
282 if( !pr.has_empty_message() )
283 os << ". " << pr.message();
287 case CHECK_BITWISE_EQUAL: {
288 char const* left_descr = va_arg( args, char const* );
289 char const* right_descr = va_arg( args, char const* );
291 os << prefix << left_descr << " =.= " << right_descr << suffix;
293 if( !pr.has_empty_message() )
294 os << ". " << pr.message();
300 //____________________________________________________________________________//
303 report_assertion( assertion_result const& ar,
304 lazy_ostream const& assertion_descr,
305 const_string file_name,
306 std::size_t line_num,
309 std::size_t num_args, ... )
311 using namespace unit_test;
313 if( !framework::test_in_progress() ) {
314 // in case no test is in progress, we do not throw anything:
315 // raising an exception here may result in raising an exception in a destructor of a global fixture
316 // which will abort the process
317 // We flag this as aborted instead
319 //BOOST_TEST_I_ASSRT( framework::current_test_case_id() != INV_TEST_UNIT_ID,
320 // std::runtime_error( "Can't use testing tools outside of test case implementation." ) );
322 framework::test_aborted();
336 ll = log_successful_tests;
338 suffix = " has passed";
342 prefix = "condition ";
343 suffix = " is not satisfied";
348 suffix = " has failed";
351 ll = log_fatal_errors;
352 prefix = "critical check ";
353 suffix = " has failed";
359 unit_test_log << unit_test::log::begin( file_name, line_num ) << ll;
361 va_start( args, num_args );
363 format_report( unit_test_log, ar, assertion_descr, tl, ct, num_args, args, prefix, suffix );
366 unit_test_log << unit_test::log::end();
370 framework::assertion_result( AR_PASSED );
374 framework::assertion_result( AR_TRIGGERED );
378 framework::assertion_result( AR_FAILED );
382 framework::assertion_result( AR_FAILED );
383 framework::test_unit_aborted( framework::current_test_unit() );
384 BOOST_TEST_I_THROW( execution_aborted() );
391 //____________________________________________________________________________//
394 format_assertion_result( const_string expr_val, const_string details )
396 assertion_result res(false);
398 bool starts_new_line = first_char( expr_val ) == '\n';
400 if( !starts_new_line && !expr_val.is_empty() )
401 res.message().stream() << " [" << expr_val << "]";
403 if( !details.is_empty() ) {
404 if( first_char(details) != '[' )
405 res.message().stream() << ". ";
407 res.message().stream() << " ";
409 res.message().stream() << details;
412 if( starts_new_line )
413 res.message().stream() << "." << expr_val;
418 //____________________________________________________________________________//
420 BOOST_TEST_DECL std::string
421 prod_report_format( assertion_result const& ar, unit_test::lazy_ostream const& assertion_descr, check_type ct, std::size_t num_args, ... )
423 std::ostringstream msg_buff;
426 va_start( args, num_args );
428 format_report( msg_buff, ar, assertion_descr, CHECK, ct, num_args, args, "assertion ", " failed" );
432 return msg_buff.str();
435 //____________________________________________________________________________//
438 equal_impl( char const* left, char const* right )
440 return (left && right) ? std::strcmp( left, right ) == 0 : (left == right);
443 //____________________________________________________________________________//
445 #if !defined( BOOST_NO_CWCHAR )
448 equal_impl( wchar_t const* left, wchar_t const* right )
450 return (left && right) ? std::wcscmp( left, right ) == 0 : (left == right);
453 #endif // !defined( BOOST_NO_CWCHAR )
455 //____________________________________________________________________________//
458 is_defined_impl( const_string symbol_name, const_string symbol_value )
460 symbol_value.trim_left( 2 );
461 return symbol_name != symbol_value;
464 //____________________________________________________________________________//
466 // ************************************************************************** //
467 // ************** context_frame ************** //
468 // ************************************************************************** //
470 context_frame::context_frame( ::boost::unit_test::lazy_ostream const& context_descr )
471 : m_frame_id( unit_test::framework::add_context( context_descr, true ) )
475 //____________________________________________________________________________//
477 context_frame::~context_frame()
479 unit_test::framework::clear_context( m_frame_id );
482 //____________________________________________________________________________//
484 context_frame::operator bool()
489 //____________________________________________________________________________//
491 } // namespace tt_detail
493 // ************************************************************************** //
494 // ************** output_test_stream ************** //
495 // ************************************************************************** //
497 struct output_test_stream::Impl
499 std::fstream m_pattern;
500 bool m_match_or_save;
501 bool m_text_or_binary;
502 std::string m_synced_string;
508 m_pattern.get( res );
509 } while( m_text_or_binary && res == '\r' && !m_pattern.fail() && !m_pattern.eof() );
514 void check_and_fill( assertion_result& res )
516 if( !res.p_predicate_value )
517 res.message() << "Output content: \"" << m_synced_string << '\"';
521 //____________________________________________________________________________//
523 output_test_stream::output_test_stream( const_string pattern_file_name, bool match_or_save, bool text_or_binary )
524 : m_pimpl( new Impl )
526 if( !pattern_file_name.is_empty() ) {
527 std::ios::openmode m = match_or_save ? std::ios::in : std::ios::out;
528 if( !text_or_binary )
529 m |= std::ios::binary;
531 m_pimpl->m_pattern.open( pattern_file_name.begin(), m );
533 if( !m_pimpl->m_pattern.is_open() )
534 BOOST_TEST_FRAMEWORK_MESSAGE( "Can't open pattern file " << pattern_file_name << " for " << (match_or_save ? "reading" : "writing") );
537 m_pimpl->m_match_or_save = match_or_save;
538 m_pimpl->m_text_or_binary = text_or_binary;
541 //____________________________________________________________________________//
543 output_test_stream::~output_test_stream()
548 //____________________________________________________________________________//
551 output_test_stream::is_empty( bool flush_stream )
555 assertion_result res( m_pimpl->m_synced_string.empty() );
557 m_pimpl->check_and_fill( res );
565 //____________________________________________________________________________//
568 output_test_stream::check_length( std::size_t length_, bool flush_stream )
572 assertion_result res( m_pimpl->m_synced_string.length() == length_ );
574 m_pimpl->check_and_fill( res );
582 //____________________________________________________________________________//
585 output_test_stream::is_equal( const_string arg, bool flush_stream )
589 assertion_result res( const_string( m_pimpl->m_synced_string ) == arg );
591 m_pimpl->check_and_fill( res );
599 //____________________________________________________________________________//
601 std::string pretty_print_log(std::string str) {
603 static const std::string to_replace[] = { "\r", "\n" };
604 static const std::string replacement[] = { "\\r", "\\n" };
606 return unit_test::utils::replace_all_occurrences_of(
608 to_replace, to_replace + sizeof(to_replace)/sizeof(to_replace[0]),
609 replacement, replacement + sizeof(replacement)/sizeof(replacement[0]));
613 output_test_stream::match_pattern( bool flush_stream )
615 const std::string::size_type n_chars_presuffix = 10;
618 assertion_result result( true );
620 const std::string stream_string_repr = get_stream_string_representation();
622 if( !m_pimpl->m_pattern.is_open() ) {
624 result.message() << "Pattern file can't be opened!";
627 if( m_pimpl->m_match_or_save ) {
630 std::vector<char> last_elements;
631 for ( std::string::size_type i = 0; static_cast<int>(i + offset) < static_cast<int>(stream_string_repr.length()); ++i ) {
633 char c = m_pimpl->get_char();
635 if( last_elements.size() <= n_chars_presuffix ) {
636 last_elements.push_back( c );
639 last_elements[ i % last_elements.size() ] = c;
642 bool is_same = !m_pimpl->m_pattern.fail() &&
643 !m_pimpl->m_pattern.eof() &&
644 (stream_string_repr[i+offset] == c);
650 std::string::size_type prefix_size = (std::min)( i + offset, n_chars_presuffix );
652 std::string::size_type suffix_size = (std::min)( stream_string_repr.length() - i - offset,
655 // try to log area around the mismatch
656 std::string substr = stream_string_repr.substr(0, i+offset);
657 std::size_t line = std::count(substr.begin(), substr.end(), '\n');
658 std::size_t column = i + offset - substr.rfind('\n');
661 << "Mismatch at position " << i
663 << ", column " << column
664 << "): '" << pretty_print_log(std::string(1, stream_string_repr[i+offset])) << "' != '" << pretty_print_log(std::string(1, c)) << "' :\n";
666 // we already escape this substring because we need its actual size for the pretty print
667 // of the difference location.
668 std::string sub_str_prefix(pretty_print_log(stream_string_repr.substr( i + offset - prefix_size, prefix_size )));
670 // we need this substring as is because we compute the best matching substrings on it.
671 std::string sub_str_suffix(stream_string_repr.substr( i + offset, suffix_size));
672 result.message() << "... " << sub_str_prefix + pretty_print_log(sub_str_suffix) << " ..." << '\n';
674 result.message() << "... ";
675 for( std::size_t j = 0; j < last_elements.size() ; j++ )
676 result.message() << pretty_print_log(std::string(1, last_elements[(i + j + 1) % last_elements.size()]));
678 std::vector<char> last_elements_ordered;
679 last_elements_ordered.push_back(c);
680 for( std::string::size_type counter = 0; counter < suffix_size - 1 ; counter++ ) {
681 char c2 = m_pimpl->get_char();
683 if( m_pimpl->m_pattern.fail() || m_pimpl->m_pattern.eof() )
686 result.message() << pretty_print_log(std::string(1, c2));
688 last_elements_ordered.push_back(c2);
691 // tries to find the best substring matching in the remainder of the
693 std::size_t max_nb_char_in_common = 0;
694 std::size_t best_pattern_start_index = 0;
695 std::size_t best_stream_start_index = 0;
696 for( std::size_t pattern_start_index = best_pattern_start_index;
697 pattern_start_index < last_elements_ordered.size();
698 pattern_start_index++ ) {
699 for( std::size_t stream_start_index = best_stream_start_index;
700 stream_start_index < sub_str_suffix.size();
701 stream_start_index++ ) {
703 std::size_t max_size = (std::min)( last_elements_ordered.size() - pattern_start_index, sub_str_suffix.size() - stream_start_index );
704 if( max_nb_char_in_common > max_size )
705 break; // safely break to go to the outer loop
707 std::size_t nb_char_in_common = 0;
708 for( std::size_t k = 0; k < max_size; k++) {
709 if( last_elements_ordered[pattern_start_index + k] == sub_str_suffix[stream_start_index + k] )
710 nb_char_in_common ++;
712 break; // we take fully matching substring only
715 if( nb_char_in_common > max_nb_char_in_common ) {
716 max_nb_char_in_common = nb_char_in_common;
717 best_pattern_start_index = pattern_start_index;
718 best_stream_start_index = stream_start_index;
723 // indicates with more precision the location of the mismatchs in "ascii arts" ...
724 result.message() << " ...\n... ";
725 for( std::string::size_type j = 0; j < sub_str_prefix.size(); j++) {
726 result.message() << ' ';
729 result.message() << '~'; // places the first tilde at the current char that mismatches
731 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
732 std::string s1(pretty_print_log(std::string(1, last_elements_ordered[(std::min)(k, best_pattern_start_index)])));
733 std::string s2(pretty_print_log(std::string(1, sub_str_suffix[(std::min)(k, best_stream_start_index)])));
734 for( int h = (std::max)(s1.size(), s2.size()); h > 0; h--)
735 result.message() << "~";
738 if( m_pimpl->m_pattern.eof() ) {
739 result.message() << " (reference string shorter than current stream)";
742 result.message() << "\n";
744 // no need to continue if the EOF is reached
745 if( m_pimpl->m_pattern.eof() ) {
749 // first char is a replicat of c, so we do not copy it.
750 for(std::string::size_type counter = 0; counter < last_elements_ordered.size() - 1 ; counter++)
751 last_elements[ (i + 1 + counter) % last_elements.size() ] = last_elements_ordered[counter + 1];
753 i += last_elements_ordered.size()-1;
754 offset += best_stream_start_index - best_pattern_start_index;
760 // not needed anymore
762 if(offset > 0 && false) {
763 m_pimpl->m_pattern.ignore(
764 static_cast<std::streamsize>( offset ));
769 m_pimpl->m_pattern.write( stream_string_repr.c_str(),
770 static_cast<std::streamsize>( stream_string_repr.length() ) );
771 m_pimpl->m_pattern.flush();
781 //____________________________________________________________________________//
784 output_test_stream::flush()
786 m_pimpl->m_synced_string.erase();
788 #ifndef BOOST_NO_STRINGSTREAM
789 str( std::string() );
791 seekp( 0, std::ios::beg );
797 output_test_stream::get_stream_string_representation() const {
798 return m_pimpl->m_synced_string;
801 //____________________________________________________________________________//
804 output_test_stream::length()
808 return m_pimpl->m_synced_string.length();
811 //____________________________________________________________________________//
814 output_test_stream::sync()
816 #ifdef BOOST_NO_STRINGSTREAM
817 m_pimpl->m_synced_string.assign( str(), pcount() );
820 m_pimpl->m_synced_string = str();
824 //____________________________________________________________________________//
826 } // namespace test_tools
829 #include <boost/test/detail/enable_warnings.hpp>
831 #endif // BOOST_TEST_TEST_TOOLS_IPP_012205GER