summaryrefslogtreecommitdiff
path: root/boost/test/impl/logged_expectations.ipp
blob: ff30628b8e6f59abc54aadf9ac00d7124ef16f80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
//  (C) Copyright Gennadiy Rozental 2005-2008.
//  Use, modification, and distribution are subject to the
//  Boost Software License, ELOG_VER 1.0. (See accompanying file
//  http://www.boost.org/LICENSE_1_0.txt)

//  See http://www.boost.org/libs/test for the library home page.
//
//  File        : $RCSfile$
//
//  Version     : $Revision$
//
//  Description : Facilities to perform interaction based testng of logged expectations
// ***************************************************************************

#ifndef BOOST_TEST_LOGGED_EXPECTATIONS_IPP_120905GER
#define BOOST_TEST_LOGGED_EXPECTATIONS_IPP_120905GER

// Boost.Test
#include <boost/test/detail/config.hpp>

#if BOOST_TEST_SUPPORT_INTERACTION_TESTING

#include <boost/test/detail/global_typedef.hpp>

#include <boost/test/utils/callback.hpp>
#include <boost/test/utils/iterator/token_iterator.hpp>

#include <boost/test/interaction_based.hpp>
#include <boost/test/test_tools.hpp>

#include <boost/test/detail/suppress_warnings.hpp>

// Boost
#include <boost/lexical_cast.hpp>

// STL
#include <fstream>

//____________________________________________________________________________//

namespace boost {

using namespace ::boost::unit_test;

namespace itest {

// ************************************************************************** //
// **************    logged expectation test implementation    ************** //
// ************************************************************************** //

struct expectations_logger : itest::manager {
    // Constructor
    expectations_logger( const_string log_file_name, bool test_or_log );

    virtual bool        decision_point( const_string, std::size_t );
    virtual unsigned    enter_scope( const_string, std::size_t, const_string scope_name );
    virtual void        allocated( const_string, std::size_t, void*, std::size_t s );
    virtual void        data_flow( const_string d );
    virtual std::string return_value( const_string default_value );
    
private:
    // Data members
    bool            m_test_or_log;
    std::fstream    m_log_file;
};

literal_string ELOG_VER     = "1.0";
literal_string CLMN_SEP     = "|";
static const char LINE_SEP  = '\n';

literal_string FILE_SIG     = "ELOG";
literal_string SCOPE_SIG    = "SCOPE";
literal_string ALLOC_SIG    = "ALLOC";
literal_string DP_SIG       = "SWITCH";
literal_string DATA_SIG     = "DATA";
literal_string RETURN_SIG   = "RETURN";

//____________________________________________________________________________//

expectations_logger::expectations_logger( const_string log_file_name, bool test_or_log )
: m_test_or_log( test_or_log )
{
    BOOST_REQUIRE_MESSAGE( !log_file_name.is_empty(), "Empty expectations log file name" );

    m_log_file.open( log_file_name.begin(), test_or_log ? std::ios::in : std::ios::out );

    BOOST_REQUIRE_MESSAGE( m_log_file.is_open(),
                           "Can't open expectations log file " << log_file_name
                                << " for " << ( m_test_or_log ? "reading" : "writing") );

    if( m_test_or_log ) {
        std::string line;
        
        std::getline( m_log_file, line, LINE_SEP );
        
        const_string cline( line );
        string_token_iterator tit( cline, (dropped_delimeters = CLMN_SEP, kept_delimeters = dt_none));

        BOOST_CHECK_EQUAL( *tit, FILE_SIG ); 
        ++tit;
        BOOST_CHECK_EQUAL( *tit, ELOG_VER );
    }
    else {
        m_log_file << FILE_SIG << CLMN_SEP << ELOG_VER << LINE_SEP;
    }
}

//____________________________________________________________________________//

bool
expectations_logger::decision_point( const_string, std::size_t )
{
    if( m_test_or_log ) {
        std::string line;
        
        std::getline( m_log_file, line, LINE_SEP );
        
        const_string cline( line );
        string_token_iterator tit( cline, (dropped_delimeters = CLMN_SEP, kept_delimeters = dt_none));
        
        BOOST_CHECK_EQUAL( *tit, DP_SIG ); ++tit;
        return lexical_cast<bool>( *tit );
    }
    else {
        m_log_file << DP_SIG << CLMN_SEP << std::boolalpha << true << LINE_SEP;
        
        return true;
    }
}

//____________________________________________________________________________//
    
unsigned
expectations_logger::enter_scope( const_string, std::size_t, const_string scope_name )
{
    if( m_test_or_log ) {
        std::string line;
        
        std::getline( m_log_file, line, LINE_SEP );
        
        const_string cline( line );
        string_token_iterator tit( cline, (dropped_delimeters = CLMN_SEP, kept_delimeters = dt_none));
        
        BOOST_CHECK_EQUAL( *tit, SCOPE_SIG ); ++tit;
        BOOST_CHECK_EQUAL( *tit, scope_name );
    }
    else {
        m_log_file << SCOPE_SIG << CLMN_SEP << scope_name << LINE_SEP;
    }
    
    return 0;
}
    
//____________________________________________________________________________//

void
expectations_logger::allocated( const_string, std::size_t, void*, std::size_t s )
{
    if( m_test_or_log ) {
        std::string line;
        
        std::getline( m_log_file, line, LINE_SEP );
        
        const_string cline( line );
        string_token_iterator tit( cline, (dropped_delimeters = CLMN_SEP, kept_delimeters = dt_none));
        
        BOOST_CHECK_EQUAL( *tit, ALLOC_SIG ); ++tit;
        BOOST_CHECK_EQUAL( lexical_cast<std::size_t>( *tit ), s );
    }
    else {
        m_log_file << ALLOC_SIG << CLMN_SEP << s << LINE_SEP;
    }
}

//____________________________________________________________________________//

void
expectations_logger::data_flow( const_string d )
{
    if( m_test_or_log ) {
        std::string line;
        
        std::getline( m_log_file, line, LINE_SEP );
        
        const_string cline( line );
        string_token_iterator tit( cline, (dropped_delimeters = CLMN_SEP, kept_delimeters = dt_none));
        
        BOOST_CHECK_EQUAL( *tit, DATA_SIG ); ++tit;
        BOOST_CHECK_EQUAL( *tit, d );
    }
    else {
        m_log_file << DATA_SIG << CLMN_SEP << d << LINE_SEP;
    }
}

//____________________________________________________________________________//

std::string
expectations_logger::return_value( const_string default_value )
{
    if( m_test_or_log ) {
        std::string line;
        
        std::getline( m_log_file, line, LINE_SEP );
        
        const_string cline( line );
        string_token_iterator tit( cline, (dropped_delimeters = CLMN_SEP, kept_delimeters = dt_none));
        
        BOOST_CHECK_EQUAL( *tit, RETURN_SIG ); ++tit;
        
        return std::string( tit->begin(), tit->size() );
    }
    else {
        m_log_file << RETURN_SIG << CLMN_SEP << default_value << LINE_SEP;
                                 
        return std::string();
    }
}

//____________________________________________________________________________//
    
// ************************************************************************** //
// **************           logged expectations test           ************** //
// ************************************************************************** //

void BOOST_TEST_DECL
logged_expectations( callback0<> const& F, const_string log_file_name, bool test_or_log )
{
    expectations_logger el( log_file_name, test_or_log );

    F();
}

//____________________________________________________________________________//

}  // namespace itest

} // namespace boost

//____________________________________________________________________________//

#include <boost/test/detail/enable_warnings.hpp>

#endif // not ancient compiler

#endif // BOOST_TEST_LOGGED_EXPECTATIONS_IPP_120905GER