1 | // (C) Copyright Gennadiy Rozental 2005. |
---|
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) |
---|
5 | |
---|
6 | // See http://www.boost.org/libs/test for the library home page. |
---|
7 | // |
---|
8 | // File : $RCSfile: logged_expectations.ipp,v $ |
---|
9 | // |
---|
10 | // ELOG_VER : $Revision: 1.6 $ |
---|
11 | // |
---|
12 | // Description : Facilities to perform interaction based testng of logged expectations |
---|
13 | // *************************************************************************** |
---|
14 | |
---|
15 | #ifndef BOOST_TEST_LOGGED_EXPECTATIONS_IPP_120905GER |
---|
16 | #define BOOST_TEST_LOGGED_EXPECTATIONS_IPP_120905GER |
---|
17 | |
---|
18 | // Boost.Test |
---|
19 | #include <boost/test/detail/config.hpp> |
---|
20 | |
---|
21 | #if !BOOST_WORKAROUND(__GNUC__, < 3) && \ |
---|
22 | !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564)) && \ |
---|
23 | !BOOST_WORKAROUND(BOOST_MSVC, <1310) && \ |
---|
24 | !BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x530)) |
---|
25 | |
---|
26 | #include <boost/test/detail/global_typedef.hpp> |
---|
27 | |
---|
28 | #include <boost/test/utils/callback.hpp> |
---|
29 | #include <boost/test/utils/iterator/token_iterator.hpp> |
---|
30 | |
---|
31 | #include <boost/test/interaction_based.hpp> |
---|
32 | #include <boost/test/test_tools.hpp> |
---|
33 | |
---|
34 | #include <boost/test/detail/suppress_warnings.hpp> |
---|
35 | |
---|
36 | // Boost |
---|
37 | #include <boost/lexical_cast.hpp> |
---|
38 | |
---|
39 | // STL |
---|
40 | #include <fstream> |
---|
41 | |
---|
42 | //____________________________________________________________________________// |
---|
43 | |
---|
44 | namespace boost { |
---|
45 | |
---|
46 | using namespace ::boost::unit_test; |
---|
47 | |
---|
48 | namespace itest { |
---|
49 | |
---|
50 | // ************************************************************************** // |
---|
51 | // ************** logged expectation test implementation ************** // |
---|
52 | // ************************************************************************** // |
---|
53 | |
---|
54 | struct expectations_logger : itest::manager { |
---|
55 | // Constructor |
---|
56 | expectations_logger( const_string log_file_name, bool test_or_log ); |
---|
57 | |
---|
58 | virtual bool decision_point( const_string, std::size_t ); |
---|
59 | virtual unsigned enter_scope( const_string, std::size_t, const_string scope_name ); |
---|
60 | virtual void allocated( const_string, std::size_t, void*, std::size_t s ); |
---|
61 | virtual void data_flow( const_string d ); |
---|
62 | virtual std::string return_value( const_string default_value ); |
---|
63 | |
---|
64 | private: |
---|
65 | // Data members |
---|
66 | bool m_test_or_log; |
---|
67 | std::fstream m_log_file; |
---|
68 | }; |
---|
69 | |
---|
70 | literal_string ELOG_VER = "1.0"; |
---|
71 | literal_string CLMN_SEP = "|"; |
---|
72 | static const char LINE_SEP = '\n'; |
---|
73 | |
---|
74 | literal_string FILE_SIG = "ELOG"; |
---|
75 | literal_string SCOPE_SIG = "SCOPE"; |
---|
76 | literal_string ALLOC_SIG = "ALLOC"; |
---|
77 | literal_string DP_SIG = "SWITCH"; |
---|
78 | literal_string DATA_SIG = "DATA"; |
---|
79 | literal_string RETURN_SIG = "RETURN"; |
---|
80 | |
---|
81 | //____________________________________________________________________________// |
---|
82 | |
---|
83 | expectations_logger::expectations_logger( const_string log_file_name, bool test_or_log ) |
---|
84 | : m_test_or_log( test_or_log ) |
---|
85 | { |
---|
86 | BOOST_REQUIRE_MESSAGE( !log_file_name.is_empty(), "Empty expectations log file name" ); |
---|
87 | |
---|
88 | m_log_file.open( log_file_name.begin(), test_or_log ? std::ios::in : std::ios::out ); |
---|
89 | |
---|
90 | BOOST_REQUIRE_MESSAGE( m_log_file.is_open(), |
---|
91 | "Couldn't open expectations log file " << log_file_name |
---|
92 | << " for " << ( m_test_or_log ? "reading" : "writing") ); |
---|
93 | |
---|
94 | if( m_test_or_log ) { |
---|
95 | std::string line; |
---|
96 | |
---|
97 | std::getline( m_log_file, line, LINE_SEP ); |
---|
98 | |
---|
99 | const_string cline( line ); |
---|
100 | string_token_iterator tit( cline, (dropped_delimeters = CLMN_SEP, kept_delimeters = dt_none)); |
---|
101 | |
---|
102 | BOOST_CHECK_EQUAL( *tit, FILE_SIG ); |
---|
103 | ++tit; |
---|
104 | BOOST_CHECK_EQUAL( *tit, ELOG_VER ); |
---|
105 | } |
---|
106 | else { |
---|
107 | m_log_file << FILE_SIG << CLMN_SEP << ELOG_VER << LINE_SEP; |
---|
108 | } |
---|
109 | } |
---|
110 | |
---|
111 | //____________________________________________________________________________// |
---|
112 | |
---|
113 | bool |
---|
114 | expectations_logger::decision_point( const_string, std::size_t ) |
---|
115 | { |
---|
116 | if( m_test_or_log ) { |
---|
117 | std::string line; |
---|
118 | |
---|
119 | std::getline( m_log_file, line, LINE_SEP ); |
---|
120 | |
---|
121 | const_string cline( line ); |
---|
122 | string_token_iterator tit( cline, (dropped_delimeters = CLMN_SEP, kept_delimeters = dt_none)); |
---|
123 | |
---|
124 | BOOST_CHECK_EQUAL( *tit, DP_SIG ); ++tit; |
---|
125 | return lexical_cast<bool>( *tit ); |
---|
126 | } |
---|
127 | else { |
---|
128 | m_log_file << DP_SIG << CLMN_SEP << std::boolalpha << true << LINE_SEP; |
---|
129 | |
---|
130 | return true; |
---|
131 | } |
---|
132 | } |
---|
133 | |
---|
134 | //____________________________________________________________________________// |
---|
135 | |
---|
136 | unsigned |
---|
137 | expectations_logger::enter_scope( const_string, std::size_t, const_string scope_name ) |
---|
138 | { |
---|
139 | if( m_test_or_log ) { |
---|
140 | std::string line; |
---|
141 | |
---|
142 | std::getline( m_log_file, line, LINE_SEP ); |
---|
143 | |
---|
144 | const_string cline( line ); |
---|
145 | string_token_iterator tit( cline, (dropped_delimeters = CLMN_SEP, kept_delimeters = dt_none)); |
---|
146 | |
---|
147 | BOOST_CHECK_EQUAL( *tit, SCOPE_SIG ); ++tit; |
---|
148 | BOOST_CHECK_EQUAL( *tit, scope_name ); |
---|
149 | } |
---|
150 | else { |
---|
151 | m_log_file << SCOPE_SIG << CLMN_SEP << scope_name << LINE_SEP; |
---|
152 | } |
---|
153 | |
---|
154 | return 0; |
---|
155 | } |
---|
156 | |
---|
157 | //____________________________________________________________________________// |
---|
158 | |
---|
159 | void |
---|
160 | expectations_logger::allocated( const_string, std::size_t, void*, std::size_t s ) |
---|
161 | { |
---|
162 | if( m_test_or_log ) { |
---|
163 | std::string line; |
---|
164 | |
---|
165 | std::getline( m_log_file, line, LINE_SEP ); |
---|
166 | |
---|
167 | const_string cline( line ); |
---|
168 | string_token_iterator tit( cline, (dropped_delimeters = CLMN_SEP, kept_delimeters = dt_none)); |
---|
169 | |
---|
170 | BOOST_CHECK_EQUAL( *tit, ALLOC_SIG ); ++tit; |
---|
171 | BOOST_CHECK_EQUAL( lexical_cast<std::size_t>( *tit ), s ); |
---|
172 | } |
---|
173 | else { |
---|
174 | m_log_file << ALLOC_SIG << CLMN_SEP << s << LINE_SEP; |
---|
175 | } |
---|
176 | } |
---|
177 | |
---|
178 | //____________________________________________________________________________// |
---|
179 | |
---|
180 | void |
---|
181 | expectations_logger::data_flow( const_string d ) |
---|
182 | { |
---|
183 | if( m_test_or_log ) { |
---|
184 | std::string line; |
---|
185 | |
---|
186 | std::getline( m_log_file, line, LINE_SEP ); |
---|
187 | |
---|
188 | const_string cline( line ); |
---|
189 | string_token_iterator tit( cline, (dropped_delimeters = CLMN_SEP, kept_delimeters = dt_none)); |
---|
190 | |
---|
191 | BOOST_CHECK_EQUAL( *tit, DATA_SIG ); ++tit; |
---|
192 | BOOST_CHECK_EQUAL( *tit, d ); |
---|
193 | } |
---|
194 | else { |
---|
195 | m_log_file << DATA_SIG << CLMN_SEP << d << LINE_SEP; |
---|
196 | } |
---|
197 | } |
---|
198 | |
---|
199 | //____________________________________________________________________________// |
---|
200 | |
---|
201 | std::string |
---|
202 | expectations_logger::return_value( const_string default_value ) |
---|
203 | { |
---|
204 | if( m_test_or_log ) { |
---|
205 | std::string line; |
---|
206 | |
---|
207 | std::getline( m_log_file, line, LINE_SEP ); |
---|
208 | |
---|
209 | const_string cline( line ); |
---|
210 | string_token_iterator tit( cline, (dropped_delimeters = CLMN_SEP, kept_delimeters = dt_none)); |
---|
211 | |
---|
212 | BOOST_CHECK_EQUAL( *tit, RETURN_SIG ); ++tit; |
---|
213 | |
---|
214 | return std::string( tit->begin(), tit->size() ); |
---|
215 | } |
---|
216 | else { |
---|
217 | m_log_file << RETURN_SIG << CLMN_SEP << default_value << LINE_SEP; |
---|
218 | |
---|
219 | return std::string(); |
---|
220 | } |
---|
221 | } |
---|
222 | |
---|
223 | //____________________________________________________________________________// |
---|
224 | |
---|
225 | // ************************************************************************** // |
---|
226 | // ************** logged expectations test ************** // |
---|
227 | // ************************************************************************** // |
---|
228 | |
---|
229 | void BOOST_TEST_DECL |
---|
230 | logged_expectations( callback0<> const& F, const_string log_file_name, bool test_or_log ) |
---|
231 | { |
---|
232 | expectations_logger el( log_file_name, test_or_log ); |
---|
233 | |
---|
234 | F(); |
---|
235 | } |
---|
236 | |
---|
237 | //____________________________________________________________________________// |
---|
238 | |
---|
239 | } // namespace itest |
---|
240 | |
---|
241 | } // namespace boost |
---|
242 | |
---|
243 | //____________________________________________________________________________// |
---|
244 | |
---|
245 | #include <boost/test/detail/enable_warnings.hpp> |
---|
246 | |
---|
247 | #endif // not ancient compiler |
---|
248 | |
---|
249 | // *************************************************************************** |
---|
250 | // Revision History : |
---|
251 | // |
---|
252 | // $Log: logged_expectations.ipp,v $ |
---|
253 | // Revision 1.6 2006/02/23 15:10:00 rogeeff |
---|
254 | // vc70 out |
---|
255 | // |
---|
256 | // Revision 1.5 2006/02/06 10:06:56 rogeeff |
---|
257 | // MSVC restored for now |
---|
258 | // |
---|
259 | // Revision 1.4 2006/01/28 08:55:31 rogeeff |
---|
260 | // VC6.0 workaround removed |
---|
261 | // |
---|
262 | // Revision 1.3 2005/12/22 15:49:32 rogeeff |
---|
263 | // sunpro port |
---|
264 | // made operator new conformant |
---|
265 | // |
---|
266 | // Revision 1.2 2005/12/19 00:08:34 rogeeff |
---|
267 | // gcc port |
---|
268 | // |
---|
269 | // Revision 1.1 2005/12/14 05:56:56 rogeeff |
---|
270 | // Interraction based / logged expectation testing is introduced |
---|
271 | // |
---|
272 | // *************************************************************************** |
---|
273 | |
---|
274 | #endif // BOOST_TEST_LOGGED_EXPECTATIONS_IPP_120905GER |
---|