1
// (C) Copyright Gennadiy Rozental 2005-2008.
2
// Use, modification, and distribution are subject to the
3
// Boost Software License, ELOG_VER 1.0. (See accompanying file
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: 54633 $
12
// Description : Facilities to perform interaction based testng of logged expectations
13
// ***************************************************************************
15
#ifndef BOOST_TEST_LOGGED_EXPECTATIONS_IPP_120905GER
16
#define BOOST_TEST_LOGGED_EXPECTATIONS_IPP_120905GER
19
#include <boost/test/detail/config.hpp>
21
#if BOOST_TEST_SUPPORT_INTERACTION_TESTING
23
#include <boost/test/detail/global_typedef.hpp>
25
#include <boost/test/utils/callback.hpp>
26
#include <boost/test/utils/iterator/token_iterator.hpp>
28
#include <boost/test/interaction_based.hpp>
29
#include <boost/test/test_tools.hpp>
31
#include <boost/test/detail/suppress_warnings.hpp>
34
#include <boost/lexical_cast.hpp>
39
//____________________________________________________________________________//
43
using namespace ::boost::unit_test;
47
// ************************************************************************** //
48
// ************** logged expectation test implementation ************** //
49
// ************************************************************************** //
51
struct expectations_logger : itest::manager {
53
expectations_logger( const_string log_file_name, bool test_or_log );
55
virtual bool decision_point( const_string, std::size_t );
56
virtual unsigned enter_scope( const_string, std::size_t, const_string scope_name );
57
virtual void allocated( const_string, std::size_t, void*, std::size_t s );
58
virtual void data_flow( const_string d );
59
virtual std::string return_value( const_string default_value );
64
std::fstream m_log_file;
67
literal_string ELOG_VER = "1.0";
68
literal_string CLMN_SEP = "|";
69
static const char LINE_SEP = '\n';
71
literal_string FILE_SIG = "ELOG";
72
literal_string SCOPE_SIG = "SCOPE";
73
literal_string ALLOC_SIG = "ALLOC";
74
literal_string DP_SIG = "SWITCH";
75
literal_string DATA_SIG = "DATA";
76
literal_string RETURN_SIG = "RETURN";
78
//____________________________________________________________________________//
80
expectations_logger::expectations_logger( const_string log_file_name, bool test_or_log )
81
: m_test_or_log( test_or_log )
83
BOOST_REQUIRE_MESSAGE( !log_file_name.is_empty(), "Empty expectations log file name" );
85
m_log_file.open( log_file_name.begin(), test_or_log ? std::ios::in : std::ios::out );
87
BOOST_REQUIRE_MESSAGE( m_log_file.is_open(),
88
"Can't open expectations log file " << log_file_name
89
<< " for " << ( m_test_or_log ? "reading" : "writing") );
94
std::getline( m_log_file, line, LINE_SEP );
96
const_string cline( line );
97
string_token_iterator tit( cline, (dropped_delimeters = CLMN_SEP, kept_delimeters = dt_none));
99
BOOST_CHECK_EQUAL( *tit, FILE_SIG );
101
BOOST_CHECK_EQUAL( *tit, ELOG_VER );
104
m_log_file << FILE_SIG << CLMN_SEP << ELOG_VER << LINE_SEP;
108
//____________________________________________________________________________//
111
expectations_logger::decision_point( const_string, std::size_t )
113
if( m_test_or_log ) {
116
std::getline( m_log_file, line, LINE_SEP );
118
const_string cline( line );
119
string_token_iterator tit( cline, (dropped_delimeters = CLMN_SEP, kept_delimeters = dt_none));
121
BOOST_CHECK_EQUAL( *tit, DP_SIG ); ++tit;
122
return lexical_cast<bool>( *tit );
125
m_log_file << DP_SIG << CLMN_SEP << std::boolalpha << true << LINE_SEP;
131
//____________________________________________________________________________//
134
expectations_logger::enter_scope( const_string, std::size_t, const_string scope_name )
136
if( m_test_or_log ) {
139
std::getline( m_log_file, line, LINE_SEP );
141
const_string cline( line );
142
string_token_iterator tit( cline, (dropped_delimeters = CLMN_SEP, kept_delimeters = dt_none));
144
BOOST_CHECK_EQUAL( *tit, SCOPE_SIG ); ++tit;
145
BOOST_CHECK_EQUAL( *tit, scope_name );
148
m_log_file << SCOPE_SIG << CLMN_SEP << scope_name << LINE_SEP;
154
//____________________________________________________________________________//
157
expectations_logger::allocated( const_string, std::size_t, void*, std::size_t s )
159
if( m_test_or_log ) {
162
std::getline( m_log_file, line, LINE_SEP );
164
const_string cline( line );
165
string_token_iterator tit( cline, (dropped_delimeters = CLMN_SEP, kept_delimeters = dt_none));
167
BOOST_CHECK_EQUAL( *tit, ALLOC_SIG ); ++tit;
168
BOOST_CHECK_EQUAL( lexical_cast<std::size_t>( *tit ), s );
171
m_log_file << ALLOC_SIG << CLMN_SEP << s << LINE_SEP;
175
//____________________________________________________________________________//
178
expectations_logger::data_flow( const_string d )
180
if( m_test_or_log ) {
183
std::getline( m_log_file, line, LINE_SEP );
185
const_string cline( line );
186
string_token_iterator tit( cline, (dropped_delimeters = CLMN_SEP, kept_delimeters = dt_none));
188
BOOST_CHECK_EQUAL( *tit, DATA_SIG ); ++tit;
189
BOOST_CHECK_EQUAL( *tit, d );
192
m_log_file << DATA_SIG << CLMN_SEP << d << LINE_SEP;
196
//____________________________________________________________________________//
199
expectations_logger::return_value( const_string default_value )
201
if( m_test_or_log ) {
204
std::getline( m_log_file, line, LINE_SEP );
206
const_string cline( line );
207
string_token_iterator tit( cline, (dropped_delimeters = CLMN_SEP, kept_delimeters = dt_none));
209
BOOST_CHECK_EQUAL( *tit, RETURN_SIG ); ++tit;
211
return std::string( tit->begin(), tit->size() );
214
m_log_file << RETURN_SIG << CLMN_SEP << default_value << LINE_SEP;
216
return std::string();
220
//____________________________________________________________________________//
222
// ************************************************************************** //
223
// ************** logged expectations test ************** //
224
// ************************************************************************** //
227
logged_expectations( callback0<> const& F, const_string log_file_name, bool test_or_log )
229
expectations_logger el( log_file_name, test_or_log );
234
//____________________________________________________________________________//
240
//____________________________________________________________________________//
242
#include <boost/test/detail/enable_warnings.hpp>
244
#endif // not ancient compiler
246
#endif // BOOST_TEST_LOGGED_EXPECTATIONS_IPP_120905GER