]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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) | |
5 | ||
6 | // See http://www.boost.org/libs/test for the library home page. | |
7 | // | |
8 | // File : $RCSfile$ | |
9 | // | |
10 | // Version : $Revision$ | |
11 | // | |
12 | // Description : implements framework API - main driver for the test | |
13 | // *************************************************************************** | |
14 | ||
15 | #ifndef BOOST_TEST_FRAMEWORK_IPP_021005GER | |
16 | #define BOOST_TEST_FRAMEWORK_IPP_021005GER | |
17 | ||
18 | // Boost.Test | |
19 | #include <boost/test/framework.hpp> | |
20 | #include <boost/test/execution_monitor.hpp> | |
21 | #include <boost/test/debug.hpp> | |
22 | #include <boost/test/unit_test_parameters.hpp> | |
23 | ||
24 | #include <boost/test/unit_test_log.hpp> | |
25 | #include <boost/test/unit_test_log_formatter.hpp> | |
26 | #include <boost/test/unit_test_monitor.hpp> | |
27 | #include <boost/test/results_collector.hpp> | |
28 | #include <boost/test/progress_monitor.hpp> | |
29 | #include <boost/test/results_reporter.hpp> | |
30 | ||
31 | #include <boost/test/tree/observer.hpp> | |
32 | #include <boost/test/tree/test_unit.hpp> | |
33 | #include <boost/test/tree/visitor.hpp> | |
34 | #include <boost/test/tree/traverse.hpp> | |
35 | #include <boost/test/tree/test_case_counter.hpp> | |
36 | ||
37 | #if BOOST_TEST_SUPPORT_TOKEN_ITERATOR | |
38 | #include <boost/test/utils/iterator/token_iterator.hpp> | |
39 | #endif | |
40 | ||
41 | #include <boost/test/utils/foreach.hpp> | |
42 | #include <boost/test/utils/basic_cstring/io.hpp> | |
43 | #include <boost/test/utils/basic_cstring/compare.hpp> | |
44 | ||
45 | #include <boost/test/detail/global_typedef.hpp> | |
46 | #include <boost/test/detail/throw_exception.hpp> | |
47 | ||
48 | // Boost | |
49 | #include <boost/timer.hpp> | |
50 | #include <boost/bind.hpp> | |
51 | ||
52 | // STL | |
53 | #include <limits> | |
54 | #include <map> | |
55 | #include <set> | |
56 | #include <cstdlib> | |
57 | #include <ctime> | |
58 | #include <numeric> | |
59 | ||
60 | #ifdef BOOST_NO_STDC_NAMESPACE | |
61 | namespace std { using ::time; using ::srand; } | |
62 | #endif | |
63 | ||
64 | #include <boost/test/detail/suppress_warnings.hpp> | |
65 | ||
66 | //____________________________________________________________________________// | |
67 | ||
68 | namespace boost { | |
69 | namespace unit_test { | |
70 | namespace framework { | |
71 | namespace impl { | |
72 | ||
73 | // ************************************************************************** // | |
74 | // ************** order detection helpers ************** // | |
75 | // ************************************************************************** // | |
76 | ||
77 | struct order_info { | |
78 | order_info() : depth(-1) {} | |
79 | ||
80 | int depth; | |
81 | std::vector<test_unit_id> dependant_siblings; | |
82 | }; | |
83 | ||
84 | typedef std::set<test_unit_id> tu_id_set; | |
85 | typedef std::map<test_unit_id,order_info> order_info_per_tu; // !! ?? unordered map | |
86 | ||
87 | //____________________________________________________________________________// | |
88 | ||
89 | static test_unit_id | |
90 | get_tu_parent( test_unit_id tu_id ) | |
91 | { | |
92 | return framework::get( tu_id, TUT_ANY ).p_parent_id; | |
93 | } | |
94 | ||
95 | //____________________________________________________________________________// | |
96 | ||
97 | static int | |
98 | tu_depth( test_unit_id tu_id, test_unit_id master_tu_id, order_info_per_tu& tuoi ) | |
99 | { | |
100 | if( tu_id == master_tu_id ) | |
101 | return 0; | |
102 | ||
103 | order_info& info = tuoi[tu_id]; | |
104 | ||
105 | if( info.depth == -1 ) | |
106 | info.depth = tu_depth( get_tu_parent( tu_id ), master_tu_id, tuoi ) + 1; | |
107 | ||
108 | return info.depth; | |
109 | } | |
110 | ||
111 | //____________________________________________________________________________// | |
112 | ||
113 | static void | |
114 | collect_dependant_siblings( test_unit_id from, test_unit_id to, test_unit_id master_tu_id, order_info_per_tu& tuoi ) | |
115 | { | |
116 | int from_depth = tu_depth( from, master_tu_id, tuoi ); | |
117 | int to_depth = tu_depth( to, master_tu_id, tuoi ); | |
118 | ||
119 | while(from_depth > to_depth) { | |
120 | from = get_tu_parent( from ); | |
121 | --from_depth; | |
122 | } | |
123 | ||
124 | while(from_depth < to_depth) { | |
125 | to = get_tu_parent( to ); | |
126 | --to_depth; | |
127 | } | |
128 | ||
129 | while(true) { | |
130 | test_unit_id from_parent = get_tu_parent( from ); | |
131 | test_unit_id to_parent = get_tu_parent( to ); | |
132 | if( from_parent == to_parent ) | |
133 | break; | |
134 | from = from_parent; | |
135 | to = to_parent; | |
136 | } | |
137 | ||
138 | tuoi[from].dependant_siblings.push_back( to ); | |
139 | } | |
140 | ||
141 | //____________________________________________________________________________// | |
142 | ||
143 | static counter_t | |
144 | assign_sibling_rank( test_unit_id tu_id, order_info_per_tu& tuoi ) | |
145 | { | |
146 | test_unit& tu = framework::get( tu_id, TUT_ANY ); | |
147 | ||
148 | BOOST_TEST_SETUP_ASSERT( tu.p_sibling_rank != (std::numeric_limits<counter_t>::max)(), | |
149 | "Cyclic dependency detected involving test unit \"" + tu.full_name() + "\"" ); | |
150 | ||
151 | if( tu.p_sibling_rank != 0 ) | |
152 | return tu.p_sibling_rank; | |
153 | ||
154 | order_info const& info = tuoi[tu_id]; | |
155 | ||
156 | // indicate in progress | |
157 | tu.p_sibling_rank.value = (std::numeric_limits<counter_t>::max)(); | |
158 | ||
159 | counter_t new_rank = 1; | |
160 | BOOST_TEST_FOREACH( test_unit_id, sibling_id, info.dependant_siblings ) | |
161 | new_rank = (std::max)(new_rank, assign_sibling_rank( sibling_id, tuoi ) + 1); | |
162 | ||
163 | return tu.p_sibling_rank.value = new_rank; | |
164 | } | |
165 | ||
166 | //____________________________________________________________________________// | |
167 | ||
168 | // ************************************************************************** // | |
169 | // ************** test_init call wrapper ************** // | |
170 | // ************************************************************************** // | |
171 | ||
172 | static void | |
173 | invoke_init_func( init_unit_test_func init_func ) | |
174 | { | |
175 | #ifdef BOOST_TEST_ALTERNATIVE_INIT_API | |
176 | BOOST_TEST_I_ASSRT( (*init_func)(), std::runtime_error( "test module initialization failed" ) ); | |
177 | #else | |
178 | test_suite* manual_test_units = (*init_func)( framework::master_test_suite().argc, framework::master_test_suite().argv ); | |
179 | ||
180 | if( manual_test_units ) | |
181 | framework::master_test_suite().add( manual_test_units ); | |
182 | #endif | |
183 | } | |
184 | ||
185 | // ************************************************************************** // | |
186 | // ************** name_filter ************** // | |
187 | // ************************************************************************** // | |
188 | ||
189 | class name_filter : public test_tree_visitor { | |
190 | struct component { | |
191 | component( const_string name ) // has to be implicit | |
192 | { | |
193 | if( name == "*" ) | |
194 | m_kind = SFK_ALL; | |
195 | else if( first_char( name ) == '*' && last_char( name ) == '*' ) { | |
196 | m_kind = SFK_SUBSTR; | |
197 | m_name = name.substr( 1, name.size()-1 ); | |
198 | } | |
199 | else if( first_char( name ) == '*' ) { | |
200 | m_kind = SFK_TRAILING; | |
201 | m_name = name.substr( 1 ); | |
202 | } | |
203 | else if( last_char( name ) == '*' ) { | |
204 | m_kind = SFK_LEADING; | |
205 | m_name = name.substr( 0, name.size()-1 ); | |
206 | } | |
207 | else { | |
208 | m_kind = SFK_MATCH; | |
209 | m_name = name; | |
210 | } | |
211 | } | |
212 | ||
213 | bool pass( test_unit const& tu ) const | |
214 | { | |
215 | const_string name( tu.p_name ); | |
216 | ||
217 | switch( m_kind ) { | |
218 | default: | |
219 | case SFK_ALL: | |
220 | return true; | |
221 | case SFK_LEADING: | |
222 | return name.substr( 0, m_name.size() ) == m_name; | |
223 | case SFK_TRAILING: | |
224 | return name.size() >= m_name.size() && name.substr( name.size() - m_name.size() ) == m_name; | |
225 | case SFK_SUBSTR: | |
226 | return name.find( m_name ) != const_string::npos; | |
227 | case SFK_MATCH: | |
228 | return m_name == tu.p_name.get(); | |
229 | } | |
230 | } | |
231 | enum kind { SFK_ALL, SFK_LEADING, SFK_TRAILING, SFK_SUBSTR, SFK_MATCH }; | |
232 | ||
233 | kind m_kind; | |
234 | const_string m_name; | |
235 | }; | |
236 | ||
237 | public: | |
238 | // Constructor | |
239 | name_filter( test_unit_id_list& targ_list, const_string filter_expr ) : m_targ_list( targ_list ), m_depth( 0 ) | |
240 | { | |
241 | #ifdef BOOST_TEST_SUPPORT_TOKEN_ITERATOR | |
242 | utils::string_token_iterator tit( filter_expr, (utils::dropped_delimeters = "/", | |
243 | utils::kept_delimeters = utils::dt_none) ); | |
244 | ||
245 | while( tit != utils::string_token_iterator() ) { | |
246 | m_components.push_back( | |
247 | std::vector<component>( utils::string_token_iterator( *tit, (utils::dropped_delimeters = ",", | |
248 | utils::kept_delimeters = utils::dt_none) ), | |
249 | utils::string_token_iterator() ) ); | |
250 | ||
251 | ++tit; | |
252 | } | |
253 | #endif | |
254 | } | |
255 | ||
256 | private: | |
257 | bool filter_unit( test_unit const& tu ) | |
258 | { | |
259 | // skip master test suite | |
260 | if( m_depth == 0 ) | |
261 | return true; | |
262 | ||
263 | // corresponding name filters are at level m_depth-1 | |
264 | std::vector<component> const& filters = m_components[m_depth-1]; | |
265 | ||
266 | // look for match | |
267 | using namespace boost::placeholders; | |
268 | return std::find_if( filters.begin(), filters.end(), bind( &component::pass, _1, boost::ref(tu) ) ) != filters.end(); | |
269 | } | |
270 | ||
271 | // test_tree_visitor interface | |
272 | virtual void visit( test_case const& tc ) | |
273 | { | |
274 | // make sure we only accept test cases if we match last component of the filter | |
275 | if( m_depth == m_components.size() && filter_unit( tc ) ) | |
276 | m_targ_list.push_back( tc.p_id ); // found a test case | |
277 | } | |
278 | virtual bool test_suite_start( test_suite const& ts ) | |
279 | { | |
280 | if( !filter_unit( ts ) ) | |
281 | return false; | |
282 | ||
283 | if( m_depth < m_components.size() ) { | |
284 | ++m_depth; | |
285 | return true; | |
286 | } | |
287 | ||
288 | m_targ_list.push_back( ts.p_id ); // found a test suite | |
289 | ||
290 | return false; | |
291 | } | |
292 | virtual void test_suite_finish( test_suite const& /*ts*/ ) | |
293 | { | |
294 | --m_depth; | |
295 | } | |
296 | ||
297 | // Data members | |
298 | typedef std::vector<std::vector<component> > components_per_level; | |
299 | ||
300 | components_per_level m_components; | |
301 | test_unit_id_list& m_targ_list; | |
302 | unsigned m_depth; | |
303 | }; | |
304 | ||
305 | // ************************************************************************** // | |
306 | // ************** label_filter ************** // | |
307 | // ************************************************************************** // | |
308 | ||
309 | class label_filter : public test_tree_visitor { | |
310 | public: | |
311 | label_filter( test_unit_id_list& targ_list, const_string label ) | |
312 | : m_targ_list( targ_list ) | |
313 | , m_label( label ) | |
314 | {} | |
315 | ||
316 | private: | |
317 | // test_tree_visitor interface | |
318 | virtual bool visit( test_unit const& tu ) | |
319 | { | |
320 | if( tu.has_label( m_label ) ) { | |
321 | // found a test unit; add it to list of tu to enable with children and stop recursion in case of suites | |
322 | m_targ_list.push_back( tu.p_id ); | |
323 | return false; | |
324 | } | |
325 | ||
326 | return true; | |
327 | } | |
328 | ||
329 | // Data members | |
330 | test_unit_id_list& m_targ_list; | |
331 | const_string m_label; | |
332 | }; | |
333 | ||
334 | // ************************************************************************** // | |
335 | // ************** set_run_status ************** // | |
336 | // ************************************************************************** // | |
337 | ||
338 | class set_run_status : public test_tree_visitor { | |
339 | public: | |
340 | explicit set_run_status( test_unit::run_status rs, test_unit_id_list* dep_collector = 0 ) | |
341 | : m_new_status( rs ) | |
342 | , m_dep_collector( dep_collector ) | |
343 | {} | |
344 | ||
345 | private: | |
346 | // test_tree_visitor interface | |
347 | virtual bool visit( test_unit const& tu ) | |
348 | { | |
349 | const_cast<test_unit&>(tu).p_run_status.value = m_new_status == test_unit::RS_INVALID ? tu.p_default_status : m_new_status; | |
350 | ||
351 | if( m_dep_collector ) { | |
352 | BOOST_TEST_FOREACH( test_unit_id, dep_id, tu.p_dependencies.get() ) { | |
353 | test_unit const& dep = framework::get( dep_id, TUT_ANY ); | |
354 | ||
355 | if( dep.p_run_status == tu.p_run_status ) | |
356 | continue; | |
357 | ||
358 | BOOST_TEST_FRAMEWORK_MESSAGE( "Including test " << dep.p_type_name << ' ' << dep.full_name() << | |
359 | " as a dependency of test " << tu.p_type_name << ' ' << tu.full_name() ); | |
360 | ||
361 | m_dep_collector->push_back( dep_id ); | |
362 | } | |
363 | } | |
364 | return true; | |
365 | } | |
366 | ||
367 | // Data members | |
368 | test_unit::run_status m_new_status; | |
369 | test_unit_id_list* m_dep_collector; | |
370 | }; | |
371 | ||
372 | // ************************************************************************** // | |
373 | // ************** parse_filters ************** // | |
374 | // ************************************************************************** // | |
375 | ||
376 | static void | |
377 | add_filtered_test_units( test_unit_id master_tu_id, const_string filter, test_unit_id_list& targ ) | |
378 | { | |
379 | // Choose between two kinds of filters | |
380 | if( filter[0] == '@' ) { | |
381 | filter.trim_left( 1 ); | |
382 | label_filter lf( targ, filter ); | |
383 | traverse_test_tree( master_tu_id, lf, true ); | |
384 | } | |
385 | else { | |
386 | name_filter nf( targ, filter ); | |
387 | traverse_test_tree( master_tu_id, nf, true ); | |
388 | } | |
389 | } | |
390 | ||
391 | //____________________________________________________________________________// | |
392 | ||
393 | static bool | |
394 | parse_filters( test_unit_id master_tu_id, test_unit_id_list& tu_to_enable, test_unit_id_list& tu_to_disable ) | |
395 | { | |
396 | // 10. collect tu to enable and disable based on filters | |
397 | bool had_selector_filter = false; | |
398 | ||
399 | std::vector<std::string> const& filters = runtime_config::get<std::vector<std::string> >( runtime_config::RUN_FILTERS ); | |
400 | ||
401 | BOOST_TEST_FOREACH( const_string, filter, filters ) { | |
402 | BOOST_TEST_SETUP_ASSERT( !filter.is_empty(), "Invalid filter specification" ); | |
403 | ||
404 | // each --run_test command may also be separated by a ':' (environment variable) | |
405 | utils::string_token_iterator t_filter_it( filter, (utils::dropped_delimeters = ":", | |
406 | utils::kept_delimeters = utils::dt_none) ); | |
407 | ||
408 | while( t_filter_it != utils::string_token_iterator() ) { | |
409 | const_string filter_token = *t_filter_it; | |
410 | ||
411 | enum { SELECTOR, ENABLER, DISABLER } filter_type = SELECTOR; | |
412 | ||
413 | // 11. Deduce filter type | |
414 | if( filter_token[0] == '!' || filter_token[0] == '+' ) { | |
415 | filter_type = filter_token[0] == '+' ? ENABLER : DISABLER; | |
416 | filter_token.trim_left( 1 ); | |
417 | BOOST_TEST_SETUP_ASSERT( !filter_token.is_empty(), "Invalid filter specification" ); | |
418 | } | |
419 | ||
420 | had_selector_filter |= filter_type == SELECTOR; | |
421 | ||
422 | // 12. Add test units to corresponding list | |
423 | switch( filter_type ) { | |
424 | case SELECTOR: | |
425 | case ENABLER: add_filtered_test_units( master_tu_id, filter_token, tu_to_enable ); break; | |
426 | case DISABLER: add_filtered_test_units( master_tu_id, filter_token, tu_to_disable ); break; | |
427 | } | |
428 | ||
429 | ++t_filter_it; | |
430 | } | |
431 | } | |
432 | ||
433 | return had_selector_filter; | |
434 | } | |
435 | ||
436 | //____________________________________________________________________________// | |
437 | ||
438 | } // namespace impl | |
439 | ||
440 | // ************************************************************************** // | |
441 | // ************** framework::state ************** // | |
442 | // ************************************************************************** // | |
443 | ||
444 | unsigned const TIMEOUT_EXCEEDED = static_cast<unsigned>( -1 ); | |
445 | ||
446 | class state { | |
447 | public: | |
448 | state() | |
449 | : m_curr_test_case( INV_TEST_UNIT_ID ) | |
450 | , m_next_test_case_id( MIN_TEST_CASE_ID ) | |
451 | , m_next_test_suite_id( MIN_TEST_SUITE_ID ) | |
452 | , m_test_in_progress( false ) | |
453 | , m_context_idx( 0 ) | |
454 | , m_log_sinks( ) | |
455 | , m_report_sink( std::cerr ) | |
456 | { | |
457 | } | |
458 | ||
459 | ~state() { clear(); } | |
460 | ||
461 | void clear() | |
462 | { | |
463 | while( !m_test_units.empty() ) { | |
464 | test_unit_store::value_type const& tu = *m_test_units.begin(); | |
465 | test_unit const* tu_ptr = tu.second; | |
466 | ||
467 | // the delete will erase this element from map | |
468 | if( ut_detail::test_id_2_unit_type( tu.second->p_id ) == TUT_SUITE ) | |
469 | delete static_cast<test_suite const*>(tu_ptr); | |
470 | else | |
471 | delete static_cast<test_case const*>(tu_ptr); | |
472 | } | |
473 | } | |
474 | ||
475 | void set_tu_id( test_unit& tu, test_unit_id id ) { tu.p_id.value = id; } | |
476 | ||
477 | ////////////////////////////////////////////////////////////////// | |
478 | ||
479 | // Validates the dependency graph and deduces the sibling dependency rank for each child | |
480 | void deduce_siblings_order( test_unit_id tu_id, test_unit_id master_tu_id, impl::order_info_per_tu& tuoi ) | |
481 | { | |
482 | test_unit& tu = framework::get( tu_id, TUT_ANY ); | |
483 | ||
484 | // collect all sibling dependancy from tu own list | |
485 | BOOST_TEST_FOREACH( test_unit_id, dep_id, tu.p_dependencies.get() ) | |
486 | collect_dependant_siblings( tu_id, dep_id, master_tu_id, tuoi ); | |
487 | ||
488 | if( tu.p_type != TUT_SUITE ) | |
489 | return; | |
490 | ||
491 | test_suite& ts = static_cast<test_suite&>(tu); | |
492 | ||
493 | // recursive call to children first | |
494 | BOOST_TEST_FOREACH( test_unit_id, chld_id, ts.m_children ) | |
495 | deduce_siblings_order( chld_id, master_tu_id, tuoi ); | |
496 | ||
497 | ts.m_ranked_children.clear(); | |
498 | BOOST_TEST_FOREACH( test_unit_id, chld_id, ts.m_children ) { | |
499 | counter_t rank = assign_sibling_rank( chld_id, tuoi ); | |
500 | ts.m_ranked_children.insert( std::make_pair( rank, chld_id ) ); | |
501 | } | |
502 | } | |
503 | ||
504 | ////////////////////////////////////////////////////////////////// | |
505 | ||
506 | // Finalize default run status: | |
507 | // 1) inherit run status from parent where applicable | |
508 | // 2) if any of test units in test suite enabled enable it as well | |
509 | bool finalize_default_run_status( test_unit_id tu_id, test_unit::run_status parent_status ) | |
510 | { | |
511 | test_unit& tu = framework::get( tu_id, TUT_ANY ); | |
512 | ||
513 | if( tu.p_default_status == test_suite::RS_INHERIT ) | |
514 | tu.p_default_status.value = parent_status; | |
515 | ||
516 | // go through list of children | |
517 | if( tu.p_type == TUT_SUITE ) { | |
518 | bool has_enabled_child = false; | |
519 | BOOST_TEST_FOREACH( test_unit_id, chld_id, static_cast<test_suite const&>(tu).m_children ) | |
520 | has_enabled_child |= finalize_default_run_status( chld_id, tu.p_default_status ); | |
521 | ||
522 | tu.p_default_status.value = has_enabled_child ? test_suite::RS_ENABLED : test_suite::RS_DISABLED; | |
523 | } | |
524 | ||
525 | return tu.p_default_status == test_suite::RS_ENABLED; | |
526 | } | |
527 | ||
528 | ////////////////////////////////////////////////////////////////// | |
529 | ||
530 | bool finalize_run_status( test_unit_id tu_id ) | |
531 | { | |
532 | test_unit& tu = framework::get( tu_id, TUT_ANY ); | |
533 | ||
534 | // go through list of children | |
535 | if( tu.p_type == TUT_SUITE ) { | |
536 | bool has_enabled_child = false; | |
537 | BOOST_TEST_FOREACH( test_unit_id, chld_id, static_cast<test_suite const&>(tu).m_children) | |
538 | has_enabled_child |= finalize_run_status( chld_id ); | |
539 | ||
540 | tu.p_run_status.value = has_enabled_child ? test_suite::RS_ENABLED : test_suite::RS_DISABLED; | |
541 | } | |
542 | ||
543 | return tu.is_enabled(); | |
544 | } | |
545 | ||
546 | ////////////////////////////////////////////////////////////////// | |
547 | ||
548 | void deduce_run_status( test_unit_id master_tu_id ) | |
549 | { | |
550 | using namespace framework::impl; | |
551 | test_unit_id_list tu_to_enable; | |
552 | test_unit_id_list tu_to_disable; | |
553 | ||
554 | // 10. If there are any filters supplied, figure out lists of test units to enable/disable | |
555 | bool had_selector_filter = !runtime_config::get<std::vector<std::string> >( runtime_config::RUN_FILTERS ).empty() && | |
556 | parse_filters( master_tu_id, tu_to_enable, tu_to_disable ); | |
557 | ||
558 | // 20. Set the stage: either use default run status or disable all test units | |
559 | set_run_status initial_setter( had_selector_filter ? test_unit::RS_DISABLED : test_unit::RS_INVALID ); | |
560 | traverse_test_tree( master_tu_id, initial_setter, true ); | |
561 | ||
562 | // 30. Apply all selectors and enablers. | |
563 | while( !tu_to_enable.empty() ) { | |
564 | test_unit& tu = framework::get( tu_to_enable.back(), TUT_ANY ); | |
565 | ||
566 | tu_to_enable.pop_back(); | |
567 | ||
568 | // 35. Ignore test units which already enabled | |
569 | if( tu.is_enabled() ) | |
570 | continue; | |
571 | ||
572 | // set new status and add all dependencies into tu_to_enable | |
573 | set_run_status enabler( test_unit::RS_ENABLED, &tu_to_enable ); | |
574 | traverse_test_tree( tu.p_id, enabler, true ); | |
575 | } | |
576 | ||
577 | // 40. Apply all disablers | |
578 | while( !tu_to_disable.empty() ) { | |
579 | test_unit const& tu = framework::get( tu_to_disable.back(), TUT_ANY ); | |
580 | ||
581 | tu_to_disable.pop_back(); | |
582 | ||
583 | // 35. Ignore test units which already disabled | |
584 | if( !tu.is_enabled() ) | |
585 | continue; | |
586 | ||
587 | set_run_status disabler( test_unit::RS_DISABLED ); | |
588 | traverse_test_tree( tu.p_id, disabler, true ); | |
589 | } | |
590 | ||
591 | // 50. Make sure parents of enabled test units are also enabled | |
592 | finalize_run_status( master_tu_id ); | |
593 | } | |
594 | ||
595 | ////////////////////////////////////////////////////////////////// | |
596 | ||
597 | typedef unit_test_monitor_t::error_level execution_result; | |
598 | ||
599 | // Random generator using the std::rand function (seeded prior to the call) | |
600 | struct random_generator_helper { | |
601 | size_t operator()(size_t i) const { | |
602 | return std::rand() % i; | |
603 | } | |
604 | }; | |
605 | ||
606 | // Executed the test tree with the root at specified test unit | |
607 | execution_result execute_test_tree( test_unit_id tu_id, | |
608 | unsigned timeout = 0, | |
609 | random_generator_helper const * const p_random_generator = 0) | |
610 | { | |
611 | test_unit const& tu = framework::get( tu_id, TUT_ANY ); | |
612 | ||
613 | execution_result result = unit_test_monitor_t::test_ok; | |
614 | ||
615 | if( !tu.is_enabled() ) | |
616 | return result; | |
617 | ||
618 | // 10. Check preconditions, including zero time left for execution and | |
619 | // successful execution of all dependencies | |
620 | if( timeout == TIMEOUT_EXCEEDED ) { | |
621 | // notify all observers about skipped test unit | |
622 | BOOST_TEST_FOREACH( test_observer*, to, m_observers ) | |
623 | to->test_unit_skipped( tu, "timeout for the test unit is exceeded" ); | |
624 | ||
625 | return unit_test_monitor_t::os_timeout; | |
626 | } | |
627 | else if( timeout == 0 || timeout > tu.p_timeout ) // deduce timeout for this test unit | |
628 | timeout = tu.p_timeout; | |
629 | ||
630 | test_tools::assertion_result const precondition_res = tu.check_preconditions(); | |
631 | if( !precondition_res ) { | |
632 | // notify all observers about skipped test unit | |
633 | BOOST_TEST_FOREACH( test_observer*, to, m_observers ) | |
634 | to->test_unit_skipped( tu, precondition_res.message() ); | |
635 | ||
636 | return unit_test_monitor_t::precondition_failure; | |
637 | } | |
638 | ||
639 | // 20. Notify all observers about the start of the test unit | |
640 | BOOST_TEST_FOREACH( test_observer*, to, m_observers ) | |
641 | to->test_unit_start( tu ); | |
642 | ||
643 | // 30. Execute setup fixtures if any; any failure here leads to test unit abortion | |
644 | BOOST_TEST_FOREACH( test_unit_fixture_ptr, F, tu.p_fixtures.get() ) { | |
645 | result = unit_test_monitor.execute_and_translate( boost::bind( &test_unit_fixture::setup, F ) ); | |
646 | if( result != unit_test_monitor_t::test_ok ) | |
647 | break; | |
648 | } | |
649 | ||
650 | // This is the time we are going to spend executing the test unit | |
651 | unsigned long elapsed = 0; | |
652 | ||
653 | if( result == unit_test_monitor_t::test_ok ) { | |
654 | // 40. We are going to time the execution | |
655 | boost::timer tu_timer; | |
656 | ||
657 | if( tu.p_type == TUT_SUITE ) { | |
658 | test_suite const& ts = static_cast<test_suite const&>( tu ); | |
659 | ||
660 | if( runtime_config::get<unsigned>( runtime_config::RANDOM_SEED ) == 0 ) { | |
661 | typedef std::pair<counter_t,test_unit_id> value_type; | |
662 | ||
663 | BOOST_TEST_FOREACH( value_type, chld, ts.m_ranked_children ) { | |
664 | unsigned chld_timeout = child_timeout( timeout, tu_timer.elapsed() ); | |
665 | ||
666 | result = (std::min)( result, execute_test_tree( chld.second, chld_timeout ) ); | |
667 | ||
668 | if( unit_test_monitor.is_critical_error( result ) ) | |
669 | break; | |
670 | } | |
671 | } | |
672 | else { | |
673 | // Go through ranges of chldren with the same dependency rank and shuffle them | |
674 | // independently. Execute each subtree in this order | |
675 | test_unit_id_list children_with_the_same_rank; | |
676 | ||
677 | typedef test_suite::children_per_rank::const_iterator it_type; | |
678 | it_type it = ts.m_ranked_children.begin(); | |
679 | while( it != ts.m_ranked_children.end() ) { | |
680 | children_with_the_same_rank.clear(); | |
681 | ||
682 | std::pair<it_type,it_type> range = ts.m_ranked_children.equal_range( it->first ); | |
683 | it = range.first; | |
684 | while( it != range.second ) { | |
685 | children_with_the_same_rank.push_back( it->second ); | |
686 | it++; | |
687 | } | |
688 | ||
689 | const random_generator_helper& rand_gen = p_random_generator ? *p_random_generator : random_generator_helper(); | |
690 | ||
691 | std::random_shuffle( children_with_the_same_rank.begin(), children_with_the_same_rank.end(), rand_gen ); | |
692 | ||
693 | BOOST_TEST_FOREACH( test_unit_id, chld, children_with_the_same_rank ) { | |
694 | unsigned chld_timeout = child_timeout( timeout, tu_timer.elapsed() ); | |
695 | ||
696 | result = (std::min)( result, execute_test_tree( chld, chld_timeout, &rand_gen ) ); | |
697 | ||
698 | if( unit_test_monitor.is_critical_error( result ) ) | |
699 | break; | |
700 | } | |
701 | } | |
702 | } | |
703 | ||
704 | elapsed = static_cast<unsigned long>( tu_timer.elapsed() * 1e6 ); | |
705 | } | |
706 | else { // TUT_CASE | |
707 | test_case const& tc = static_cast<test_case const&>( tu ); | |
708 | ||
709 | // setup contexts | |
710 | m_context_idx = 0; | |
711 | ||
712 | // setup current test case | |
713 | test_unit_id bkup = m_curr_test_case; | |
714 | m_curr_test_case = tc.p_id; | |
715 | ||
716 | // execute the test case body | |
717 | result = unit_test_monitor.execute_and_translate( tc.p_test_func, timeout ); | |
718 | elapsed = static_cast<unsigned long>( tu_timer.elapsed() * 1e6 ); | |
719 | ||
720 | // cleanup leftover context | |
721 | m_context.clear(); | |
722 | ||
723 | // restore state and abort if necessary | |
724 | m_curr_test_case = bkup; | |
725 | } | |
726 | } | |
727 | ||
728 | // if run error is critical skip teardown, who knows what the state of the program at this point | |
729 | if( !unit_test_monitor.is_critical_error( result ) ) { | |
730 | // execute teardown fixtures if any in reverse order | |
731 | BOOST_TEST_REVERSE_FOREACH( test_unit_fixture_ptr, F, tu.p_fixtures.get() ) { | |
732 | result = (std::min)( result, unit_test_monitor.execute_and_translate( boost::bind( &test_unit_fixture::teardown, F ), 0 ) ); | |
733 | ||
734 | if( unit_test_monitor.is_critical_error( result ) ) | |
735 | break; | |
736 | } | |
737 | } | |
738 | ||
739 | // notify all observers about abortion | |
740 | if( unit_test_monitor.is_critical_error( result ) ) { | |
741 | BOOST_TEST_FOREACH( test_observer*, to, m_observers ) | |
742 | to->test_aborted(); | |
743 | } | |
744 | ||
745 | // notify all observers about completion | |
746 | BOOST_TEST_REVERSE_FOREACH( test_observer*, to, m_observers ) | |
747 | to->test_unit_finish( tu, elapsed ); | |
748 | ||
749 | return result; | |
750 | } | |
751 | ||
752 | ////////////////////////////////////////////////////////////////// | |
753 | ||
754 | unsigned child_timeout( unsigned tu_timeout, double elapsed ) | |
755 | { | |
756 | if( tu_timeout == 0U ) | |
757 | return 0U; | |
758 | ||
759 | unsigned elpsed_sec = static_cast<unsigned>(elapsed); // rounding to number of whole seconds | |
760 | ||
761 | return tu_timeout > elpsed_sec ? tu_timeout - elpsed_sec : TIMEOUT_EXCEEDED; | |
762 | } | |
763 | ||
764 | struct priority_order { | |
765 | bool operator()( test_observer* lhs, test_observer* rhs ) const | |
766 | { | |
767 | return (lhs->priority() < rhs->priority()) || ((lhs->priority() == rhs->priority()) && (lhs < rhs)); | |
768 | } | |
769 | }; | |
770 | ||
771 | // Data members | |
772 | typedef std::map<test_unit_id,test_unit*> test_unit_store; | |
773 | typedef std::set<test_observer*,priority_order> observer_store; | |
774 | struct context_frame { | |
775 | context_frame( std::string const& d, int id, bool sticky ) | |
776 | : descr( d ) | |
777 | , frame_id( id ) | |
778 | , is_sticky( sticky ) | |
779 | {} | |
780 | ||
781 | std::string descr; | |
782 | int frame_id; | |
783 | bool is_sticky; | |
784 | }; | |
785 | typedef std::vector<context_frame> context_data; | |
786 | ||
787 | master_test_suite_t* m_master_test_suite; | |
788 | std::vector<test_suite*> m_auto_test_suites; | |
789 | ||
790 | test_unit_id m_curr_test_case; | |
791 | test_unit_store m_test_units; | |
792 | ||
793 | test_unit_id m_next_test_case_id; | |
794 | test_unit_id m_next_test_suite_id; | |
795 | ||
796 | bool m_test_in_progress; | |
797 | ||
798 | observer_store m_observers; | |
799 | context_data m_context; | |
800 | int m_context_idx; | |
801 | ||
802 | boost::execution_monitor m_aux_em; | |
803 | ||
804 | std::map<output_format, runtime_config::stream_holder> m_log_sinks; | |
805 | runtime_config::stream_holder m_report_sink; | |
806 | }; | |
807 | ||
808 | //____________________________________________________________________________// | |
809 | ||
810 | namespace impl { | |
811 | namespace { | |
812 | ||
813 | #if defined(__CYGWIN__) | |
814 | framework::state& s_frk_state() { static framework::state* the_inst = 0; if(!the_inst) the_inst = new framework::state; return *the_inst; } | |
815 | #else | |
816 | framework::state& s_frk_state() { static framework::state the_inst; return the_inst; } | |
817 | #endif | |
818 | ||
819 | } // local namespace | |
820 | ||
821 | void | |
822 | setup_for_execution( test_unit const& tu ) | |
823 | { | |
824 | s_frk_state().deduce_run_status( tu.p_id ); | |
825 | } | |
826 | ||
827 | struct sum_to_first_only { | |
828 | sum_to_first_only() : is_first(true) {} | |
829 | template <class T, class U> | |
830 | T operator()(T const& l_, U const& r_) { | |
831 | if(is_first) { | |
832 | is_first = false; | |
833 | return l_ + r_.first; | |
834 | } | |
835 | return l_ + ", " + r_.first; | |
836 | } | |
837 | ||
838 | bool is_first; | |
839 | }; | |
840 | ||
841 | void | |
842 | setup_loggers() | |
843 | { | |
844 | ||
845 | BOOST_TEST_I_TRY { | |
846 | ||
847 | ||
848 | ||
849 | #ifdef BOOST_TEST_SUPPORT_TOKEN_ITERATOR | |
850 | bool has_combined_logger = runtime_config::has( runtime_config::COMBINED_LOGGER ) | |
851 | && !runtime_config::get< std::vector<std::string> >( runtime_config::COMBINED_LOGGER ).empty(); | |
852 | #else | |
853 | bool has_combined_logger = false; | |
854 | #endif | |
855 | ||
856 | if( !has_combined_logger ) { | |
857 | unit_test_log.set_threshold_level( runtime_config::get<log_level>( runtime_config::LOG_LEVEL ) ); | |
858 | const output_format format = runtime_config::get<output_format>( runtime_config::LOG_FORMAT ); | |
859 | unit_test_log.set_format( format ); | |
860 | ||
861 | runtime_config::stream_holder& stream_logger = s_frk_state().m_log_sinks[format]; | |
862 | if( runtime_config::has( runtime_config::LOG_SINK ) ) | |
863 | stream_logger.setup( runtime_config::get<const_string>( runtime_config::LOG_SINK ) ); | |
864 | unit_test_log.set_stream( stream_logger.ref() ); | |
865 | } | |
866 | else | |
867 | { | |
868 | ||
869 | const std::vector<std::string>& v_output_format = runtime_config::get< std::vector<std::string> >( runtime_config::COMBINED_LOGGER ) ; | |
870 | ||
871 | static const std::pair<const char*, log_level> all_log_levels[] = { | |
872 | std::make_pair( "all" , log_successful_tests ), | |
873 | std::make_pair( "success" , log_successful_tests ), | |
874 | std::make_pair( "test_suite" , log_test_units ), | |
875 | std::make_pair( "unit_scope" , log_test_units ), | |
876 | std::make_pair( "message" , log_messages ), | |
877 | std::make_pair( "warning" , log_warnings ), | |
878 | std::make_pair( "error" , log_all_errors ), | |
879 | std::make_pair( "cpp_exception" , log_cpp_exception_errors ), | |
880 | std::make_pair( "system_error" , log_system_errors ), | |
881 | std::make_pair( "fatal_error" , log_fatal_errors ), | |
882 | std::make_pair( "nothing" , log_nothing ) | |
883 | }; | |
884 | ||
885 | static const std::pair<const char*, output_format> all_formats[] = { | |
886 | std::make_pair( "HRF" , OF_CLF ), | |
887 | std::make_pair( "CLF" , OF_CLF ), | |
888 | std::make_pair( "XML" , OF_XML ), | |
889 | std::make_pair( "JUNIT", OF_JUNIT ) | |
890 | }; | |
891 | ||
892 | ||
893 | bool is_first = true; | |
894 | ||
895 | BOOST_TEST_FOREACH( const_string, current_multi_config, v_output_format ) { | |
896 | ||
897 | #ifdef BOOST_TEST_SUPPORT_TOKEN_ITERATOR | |
898 | utils::string_token_iterator current_config( current_multi_config, (utils::dropped_delimeters = ":", | |
899 | utils::kept_delimeters = utils::dt_none) ); | |
900 | ||
901 | for( ; current_config != utils::string_token_iterator() ; ++current_config) { | |
902 | ||
903 | utils::string_token_iterator current_format_specs( *current_config, (utils::keep_empty_tokens, | |
904 | utils::dropped_delimeters = ",", | |
905 | utils::kept_delimeters = utils::dt_none) ); | |
906 | ||
907 | output_format format = OF_INVALID ; // default | |
908 | if( current_format_specs != utils::string_token_iterator() && | |
909 | current_format_specs->size() ) { | |
910 | ||
911 | for(size_t elem=0; elem < sizeof(all_formats)/sizeof(all_formats[0]); elem++) { | |
912 | if(const_string(all_formats[elem].first) == *current_format_specs) { | |
913 | format = all_formats[elem].second; | |
914 | break; | |
915 | } | |
916 | } | |
917 | } | |
918 | ||
919 | BOOST_TEST_I_ASSRT( format != OF_INVALID, | |
920 | boost::runtime::access_to_missing_argument() | |
921 | << "Unable to determine the logger type from '" | |
922 | << *current_config | |
923 | << "'. Possible choices are: " | |
924 | << std::accumulate(all_formats, | |
925 | all_formats + sizeof(all_formats)/sizeof(all_formats[0]), | |
926 | std::string(""), | |
927 | sum_to_first_only()) | |
928 | ); | |
929 | ||
930 | // activates this format | |
931 | if( is_first ) { | |
932 | unit_test_log.set_format( format ); | |
933 | } | |
934 | else { | |
935 | unit_test_log.add_format( format ); | |
936 | } | |
937 | is_first = false; | |
938 | ||
939 | unit_test_log_formatter * const formatter = unit_test_log.get_formatter(format); | |
940 | BOOST_TEST_SETUP_ASSERT( formatter, "Logger setup error" ); | |
941 | ||
942 | log_level formatter_log_level = invalid_log_level; | |
943 | if( !current_format_specs->size() ) { | |
944 | formatter_log_level = formatter->get_log_level(); // default log level given by the formatter | |
945 | } | |
946 | else if( ++current_format_specs != utils::string_token_iterator() ) { | |
947 | ||
948 | for(size_t elem=0; elem < sizeof(all_log_levels)/sizeof(all_log_levels[0]); elem++) { | |
949 | if(const_string(all_log_levels[elem].first) == *current_format_specs) { | |
950 | formatter_log_level = all_log_levels[elem].second; | |
951 | break; | |
952 | } | |
953 | } | |
954 | } | |
955 | ||
956 | ||
957 | BOOST_TEST_I_ASSRT( formatter_log_level != invalid_log_level, | |
958 | boost::runtime::access_to_missing_argument() | |
959 | << "Unable to determine the log level from '" | |
960 | << *current_config | |
961 | << "'. Possible choices are: " | |
962 | << std::accumulate(all_log_levels, | |
963 | all_log_levels + sizeof(all_log_levels)/sizeof(all_log_levels[0]), | |
964 | std::string(""), | |
965 | sum_to_first_only()) | |
966 | ); | |
967 | ||
968 | unit_test_log.set_threshold_level( format, formatter_log_level ); | |
969 | ||
970 | runtime_config::stream_holder& stream_logger = s_frk_state().m_log_sinks[format]; | |
971 | if( ++current_format_specs != utils::string_token_iterator() && | |
972 | current_format_specs->size() ) { | |
973 | stream_logger.setup( *current_format_specs ); | |
974 | } | |
975 | else { | |
976 | stream_logger.setup( formatter->get_default_stream_description() ); | |
977 | } | |
978 | unit_test_log.set_stream( format, stream_logger.ref() ); | |
979 | ||
980 | } | |
981 | #endif | |
982 | } | |
983 | ||
984 | } | |
985 | } | |
986 | BOOST_TEST_I_CATCH( boost::runtime::init_error, ex ) { | |
987 | BOOST_TEST_SETUP_ASSERT( false, ex.msg ); | |
988 | } | |
989 | BOOST_TEST_I_CATCH( boost::runtime::input_error, ex ) { | |
990 | std::cerr << ex.msg << "\n\n"; | |
991 | ||
992 | BOOST_TEST_I_THROW( framework::nothing_to_test( boost::exit_exception_failure ) ); | |
993 | } | |
994 | ||
995 | ||
996 | } | |
997 | ||
998 | //____________________________________________________________________________// | |
999 | ||
1000 | } // namespace impl | |
1001 | ||
1002 | //____________________________________________________________________________// | |
1003 | ||
1004 | // ************************************************************************** // | |
1005 | // ************** framework::init ************** // | |
1006 | // ************************************************************************** // | |
1007 | ||
1008 | void | |
1009 | init( init_unit_test_func init_func, int argc, char* argv[] ) | |
1010 | { | |
1011 | using namespace impl; | |
1012 | ||
1013 | // 10. Set up runtime parameters | |
1014 | runtime_config::init( argc, argv ); | |
1015 | ||
1016 | // 20. Set the desired log level, format and sink | |
1017 | impl::setup_loggers(); | |
1018 | ||
1019 | // 30. Set the desired report level, format and sink | |
1020 | results_reporter::set_level( runtime_config::get<report_level>( runtime_config::REPORT_LEVEL ) ); | |
1021 | results_reporter::set_format( runtime_config::get<output_format>( runtime_config::REPORT_FORMAT ) ); | |
1022 | ||
1023 | if( runtime_config::has( runtime_config::REPORT_SINK ) ) | |
1024 | s_frk_state().m_report_sink.setup( runtime_config::get<const_string>( runtime_config::REPORT_SINK ) ); | |
1025 | results_reporter::set_stream( s_frk_state().m_report_sink.ref() ); | |
1026 | ||
1027 | // 40. Register default test observers | |
1028 | register_observer( results_collector ); | |
1029 | register_observer( unit_test_log ); | |
1030 | ||
1031 | if( runtime_config::get<bool>( runtime_config::SHOW_PROGRESS ) ) { | |
1032 | progress_monitor.set_stream( std::cout ); // defaults to stdout | |
1033 | register_observer( progress_monitor ); | |
1034 | } | |
1035 | ||
1036 | // 50. Set up memory leak detection | |
1037 | unsigned long detect_mem_leak = runtime_config::get<unsigned long>( runtime_config::DETECT_MEM_LEAKS ); | |
1038 | if( detect_mem_leak > 0 ) { | |
1039 | debug::detect_memory_leaks( true, runtime_config::get<std::string>( runtime_config::REPORT_MEM_LEAKS ) ); | |
1040 | debug::break_memory_alloc( (long)detect_mem_leak ); | |
1041 | } | |
1042 | ||
1043 | // 60. Initialize master unit test suite | |
1044 | master_test_suite().argc = argc; | |
1045 | master_test_suite().argv = argv; | |
1046 | ||
1047 | // 70. Invoke test module initialization routine | |
1048 | BOOST_TEST_I_TRY { | |
1049 | s_frk_state().m_aux_em.vexecute( boost::bind( &impl::invoke_init_func, init_func ) ); | |
1050 | } | |
1051 | BOOST_TEST_I_CATCH( execution_exception, ex ) { | |
1052 | BOOST_TEST_SETUP_ASSERT( false, ex.what() ); | |
1053 | } | |
1054 | } | |
1055 | ||
1056 | //____________________________________________________________________________// | |
1057 | ||
1058 | void | |
1059 | finalize_setup_phase( test_unit_id master_tu_id ) | |
1060 | { | |
1061 | if( master_tu_id == INV_TEST_UNIT_ID ) | |
1062 | master_tu_id = master_test_suite().p_id; | |
1063 | ||
1064 | // 10. Apply all decorators to the auto test units | |
1065 | class apply_decorators : public test_tree_visitor { | |
1066 | private: | |
1067 | // test_tree_visitor interface | |
1068 | virtual bool visit( test_unit const& tu ) | |
1069 | { | |
1070 | BOOST_TEST_FOREACH( decorator::base_ptr, d, tu.p_decorators.get() ) | |
1071 | d->apply( const_cast<test_unit&>(tu) ); | |
1072 | ||
1073 | return true; | |
1074 | } | |
1075 | } ad; | |
1076 | traverse_test_tree( master_tu_id, ad, true ); | |
1077 | ||
1078 | // 20. Finalize setup phase | |
1079 | impl::order_info_per_tu tuoi; | |
1080 | impl::s_frk_state().deduce_siblings_order( master_tu_id, master_tu_id, tuoi ); | |
1081 | impl::s_frk_state().finalize_default_run_status( master_tu_id, test_unit::RS_INVALID ); | |
1082 | } | |
1083 | ||
1084 | // ************************************************************************** // | |
1085 | // ************** test_in_progress ************** // | |
1086 | // ************************************************************************** // | |
1087 | ||
1088 | bool | |
1089 | test_in_progress() | |
1090 | { | |
1091 | return impl::s_frk_state().m_test_in_progress; | |
1092 | } | |
1093 | ||
1094 | //____________________________________________________________________________// | |
1095 | ||
1096 | // ************************************************************************** // | |
1097 | // ************** framework::shutdown ************** // | |
1098 | // ************************************************************************** // | |
1099 | ||
1100 | void | |
1101 | shutdown() | |
1102 | { | |
1103 | // eliminating some fake memory leak reports. See for more details: | |
1104 | // http://connect.microsoft.com/VisualStudio/feedback/details/106937/memory-leaks-reported-by-debug-crt-inside-typeinfo-name | |
1105 | ||
1106 | # if BOOST_WORKAROUND(BOOST_MSVC, <= 1600 ) && !defined(_DLL) && defined(_DEBUG) | |
1107 | # if BOOST_WORKAROUND(BOOST_MSVC, < 1600 ) | |
1108 | #define _Next next | |
1109 | #define _MemPtr memPtr | |
1110 | #endif | |
1111 | __type_info_node* pNode = __type_info_root_node._Next; | |
1112 | __type_info_node* tmpNode = &__type_info_root_node; | |
1113 | ||
1114 | for( ; pNode!=NULL; pNode = tmpNode ) { | |
1115 | tmpNode = pNode->_Next; | |
1116 | delete pNode->_MemPtr; | |
1117 | delete pNode; | |
1118 | } | |
1119 | # if BOOST_WORKAROUND(BOOST_MSVC, < 1600 ) | |
1120 | #undef _Next | |
1121 | #undef _MemPtr | |
1122 | #endif | |
1123 | # endif | |
1124 | } | |
1125 | ||
1126 | //____________________________________________________________________________// | |
1127 | ||
1128 | // ************************************************************************** // | |
1129 | // ************** register_test_unit ************** // | |
1130 | // ************************************************************************** // | |
1131 | ||
1132 | void | |
1133 | register_test_unit( test_case* tc ) | |
1134 | { | |
1135 | BOOST_TEST_SETUP_ASSERT( tc->p_id == INV_TEST_UNIT_ID, BOOST_TEST_L( "test case already registered" ) ); | |
1136 | ||
1137 | test_unit_id new_id = impl::s_frk_state().m_next_test_case_id; | |
1138 | ||
1139 | BOOST_TEST_SETUP_ASSERT( new_id != MAX_TEST_CASE_ID, BOOST_TEST_L( "too many test cases" ) ); | |
1140 | ||
1141 | typedef state::test_unit_store::value_type map_value_type; | |
1142 | ||
1143 | impl::s_frk_state().m_test_units.insert( map_value_type( new_id, tc ) ); | |
1144 | impl::s_frk_state().m_next_test_case_id++; | |
1145 | ||
1146 | impl::s_frk_state().set_tu_id( *tc, new_id ); | |
1147 | } | |
1148 | ||
1149 | //____________________________________________________________________________// | |
1150 | ||
1151 | // ************************************************************************** // | |
1152 | // ************** register_test_unit ************** // | |
1153 | // ************************************************************************** // | |
1154 | ||
1155 | void | |
1156 | register_test_unit( test_suite* ts ) | |
1157 | { | |
1158 | BOOST_TEST_SETUP_ASSERT( ts->p_id == INV_TEST_UNIT_ID, BOOST_TEST_L( "test suite already registered" ) ); | |
1159 | ||
1160 | test_unit_id new_id = impl::s_frk_state().m_next_test_suite_id; | |
1161 | ||
1162 | BOOST_TEST_SETUP_ASSERT( new_id != MAX_TEST_SUITE_ID, BOOST_TEST_L( "too many test suites" ) ); | |
1163 | ||
1164 | typedef state::test_unit_store::value_type map_value_type; | |
1165 | ||
1166 | impl::s_frk_state().m_test_units.insert( map_value_type( new_id, ts ) ); | |
1167 | impl::s_frk_state().m_next_test_suite_id++; | |
1168 | ||
1169 | impl::s_frk_state().set_tu_id( *ts, new_id ); | |
1170 | } | |
1171 | ||
1172 | //____________________________________________________________________________// | |
1173 | ||
1174 | // ************************************************************************** // | |
1175 | // ************** deregister_test_unit ************** // | |
1176 | // ************************************************************************** // | |
1177 | ||
1178 | void | |
1179 | deregister_test_unit( test_unit* tu ) | |
1180 | { | |
1181 | impl::s_frk_state().m_test_units.erase( tu->p_id ); | |
1182 | } | |
1183 | ||
1184 | //____________________________________________________________________________// | |
1185 | ||
1186 | // ************************************************************************** // | |
1187 | // ************** clear ************** // | |
1188 | // ************************************************************************** // | |
1189 | ||
1190 | void | |
1191 | clear() | |
1192 | { | |
1193 | impl::s_frk_state().clear(); | |
1194 | } | |
1195 | ||
1196 | //____________________________________________________________________________// | |
1197 | ||
1198 | // ************************************************************************** // | |
1199 | // ************** register_observer ************** // | |
1200 | // ************************************************************************** // | |
1201 | ||
1202 | void | |
1203 | register_observer( test_observer& to ) | |
1204 | { | |
1205 | impl::s_frk_state().m_observers.insert( &to ); | |
1206 | } | |
1207 | ||
1208 | //____________________________________________________________________________// | |
1209 | ||
1210 | // ************************************************************************** // | |
1211 | // ************** deregister_observer ************** // | |
1212 | // ************************************************************************** // | |
1213 | ||
1214 | void | |
1215 | deregister_observer( test_observer& to ) | |
1216 | { | |
1217 | impl::s_frk_state().m_observers.erase( &to ); | |
1218 | } | |
1219 | ||
1220 | //____________________________________________________________________________// | |
1221 | ||
1222 | // ************************************************************************** // | |
1223 | // ************** add_context ************** // | |
1224 | // ************************************************************************** // | |
1225 | ||
1226 | int | |
1227 | add_context( ::boost::unit_test::lazy_ostream const& context_descr, bool sticky ) | |
1228 | { | |
1229 | std::stringstream buffer; | |
1230 | context_descr( buffer ); | |
1231 | int res_idx = impl::s_frk_state().m_context_idx++; | |
1232 | ||
1233 | impl::s_frk_state().m_context.push_back( state::context_frame( buffer.str(), res_idx, sticky ) ); | |
1234 | ||
1235 | return res_idx; | |
1236 | } | |
1237 | ||
1238 | //____________________________________________________________________________// | |
1239 | ||
1240 | // ************************************************************************** // | |
1241 | // ************** clear_context ************** // | |
1242 | // ************************************************************************** // | |
1243 | ||
1244 | struct frame_with_id { | |
1245 | explicit frame_with_id( int id ) : m_id( id ) {} | |
1246 | ||
1247 | bool operator()( state::context_frame const& f ) | |
1248 | { | |
1249 | return f.frame_id == m_id; | |
1250 | } | |
1251 | int m_id; | |
1252 | }; | |
1253 | ||
1254 | //____________________________________________________________________________// | |
1255 | ||
1256 | void | |
1257 | clear_context( int frame_id ) | |
1258 | { | |
1259 | if( frame_id == -1 ) { // clear all non sticky frames | |
1260 | for( int i=static_cast<int>(impl::s_frk_state().m_context.size())-1; i>=0; i-- ) | |
1261 | if( !impl::s_frk_state().m_context[i].is_sticky ) | |
1262 | impl::s_frk_state().m_context.erase( impl::s_frk_state().m_context.begin()+i ); | |
1263 | } | |
1264 | ||
1265 | else { // clear specific frame | |
1266 | state::context_data::iterator it = | |
1267 | std::find_if( impl::s_frk_state().m_context.begin(), impl::s_frk_state().m_context.end(), frame_with_id( frame_id ) ); | |
1268 | ||
1269 | if( it != impl::s_frk_state().m_context.end() ) // really an internal error if this is not true | |
1270 | impl::s_frk_state().m_context.erase( it ); | |
1271 | } | |
1272 | } | |
1273 | ||
1274 | //____________________________________________________________________________// | |
1275 | ||
1276 | // ************************************************************************** // | |
1277 | // ************** get_context ************** // | |
1278 | // ************************************************************************** // | |
1279 | ||
1280 | context_generator | |
1281 | get_context() | |
1282 | { | |
1283 | return context_generator(); | |
1284 | } | |
1285 | ||
1286 | //____________________________________________________________________________// | |
1287 | ||
1288 | // ************************************************************************** // | |
1289 | // ************** context_generator ************** // | |
1290 | // ************************************************************************** // | |
1291 | ||
1292 | bool | |
1293 | context_generator::is_empty() const | |
1294 | { | |
1295 | return impl::s_frk_state().m_context.empty(); | |
1296 | } | |
1297 | ||
1298 | //____________________________________________________________________________// | |
1299 | ||
1300 | const_string | |
1301 | context_generator::next() const | |
1302 | { | |
1303 | return m_curr_frame < impl::s_frk_state().m_context.size() ? impl::s_frk_state().m_context[m_curr_frame++].descr : const_string(); | |
1304 | } | |
1305 | ||
1306 | //____________________________________________________________________________// | |
1307 | ||
1308 | // ************************************************************************** // | |
1309 | // ************** master_test_suite ************** // | |
1310 | // ************************************************************************** // | |
1311 | ||
1312 | master_test_suite_t& | |
1313 | master_test_suite() | |
1314 | { | |
1315 | if( !impl::s_frk_state().m_master_test_suite ) | |
1316 | impl::s_frk_state().m_master_test_suite = new master_test_suite_t; | |
1317 | ||
1318 | return *impl::s_frk_state().m_master_test_suite; | |
1319 | } | |
1320 | ||
1321 | //____________________________________________________________________________// | |
1322 | ||
1323 | // ************************************************************************** // | |
1324 | // ************** current_auto_test_suite ************** // | |
1325 | // ************************************************************************** // | |
1326 | ||
1327 | test_suite& | |
1328 | current_auto_test_suite( test_suite* ts, bool push_or_pop ) | |
1329 | { | |
1330 | if( impl::s_frk_state().m_auto_test_suites.empty() ) | |
1331 | impl::s_frk_state().m_auto_test_suites.push_back( &framework::master_test_suite() ); | |
1332 | ||
1333 | if( !push_or_pop ) | |
1334 | impl::s_frk_state().m_auto_test_suites.pop_back(); | |
1335 | else if( ts ) | |
1336 | impl::s_frk_state().m_auto_test_suites.push_back( ts ); | |
1337 | ||
1338 | return *impl::s_frk_state().m_auto_test_suites.back(); | |
1339 | } | |
1340 | ||
1341 | //____________________________________________________________________________// | |
1342 | ||
1343 | // ************************************************************************** // | |
1344 | // ************** current_test_case ************** // | |
1345 | // ************************************************************************** // | |
1346 | ||
1347 | test_case const& | |
1348 | current_test_case() | |
1349 | { | |
1350 | return get<test_case>( impl::s_frk_state().m_curr_test_case ); | |
1351 | } | |
1352 | ||
1353 | //____________________________________________________________________________// | |
1354 | ||
1355 | test_unit_id | |
1356 | current_test_case_id() | |
1357 | { | |
1358 | return impl::s_frk_state().m_curr_test_case; | |
1359 | } | |
1360 | ||
1361 | //____________________________________________________________________________// | |
1362 | ||
1363 | // ************************************************************************** // | |
1364 | // ************** framework::get ************** // | |
1365 | // ************************************************************************** // | |
1366 | ||
1367 | test_unit& | |
1368 | get( test_unit_id id, test_unit_type t ) | |
1369 | { | |
1370 | test_unit* res = impl::s_frk_state().m_test_units[id]; | |
1371 | ||
1372 | BOOST_TEST_I_ASSRT( (res->p_type & t) != 0, internal_error( "Invalid test unit type" ) ); | |
1373 | ||
1374 | return *res; | |
1375 | } | |
1376 | ||
1377 | //____________________________________________________________________________// | |
1378 | ||
1379 | // ************************************************************************** // | |
1380 | // ************** framework::run ************** // | |
1381 | // ************************************************************************** // | |
1382 | ||
1383 | void | |
1384 | run( test_unit_id id, bool continue_test ) | |
1385 | { | |
1386 | if( id == INV_TEST_UNIT_ID ) | |
1387 | id = master_test_suite().p_id; | |
1388 | ||
1389 | // Figure out run status for execution phase | |
1390 | impl::s_frk_state().deduce_run_status( id ); | |
1391 | ||
1392 | test_case_counter tcc; | |
1393 | traverse_test_tree( id, tcc ); | |
1394 | ||
1395 | BOOST_TEST_SETUP_ASSERT( tcc.p_count != 0 , runtime_config::get<std::vector<std::string> >( runtime_config::RUN_FILTERS ).empty() | |
1396 | ? BOOST_TEST_L( "test tree is empty" ) | |
1397 | : BOOST_TEST_L( "no test cases matching filter or all test cases were disabled" ) ); | |
1398 | ||
1399 | bool was_in_progress = framework::test_in_progress(); | |
1400 | bool call_start_finish = !continue_test || !was_in_progress; | |
1401 | ||
1402 | impl::s_frk_state().m_test_in_progress = true; | |
1403 | ||
1404 | if( call_start_finish ) { | |
1405 | BOOST_TEST_FOREACH( test_observer*, to, impl::s_frk_state().m_observers ) { | |
1406 | BOOST_TEST_I_TRY { | |
1407 | impl::s_frk_state().m_aux_em.vexecute( boost::bind( &test_observer::test_start, to, tcc.p_count ) ); | |
1408 | } | |
1409 | BOOST_TEST_I_CATCH( execution_exception, ex ) { | |
1410 | BOOST_TEST_SETUP_ASSERT( false, ex.what() ); | |
1411 | } | |
1412 | } | |
1413 | } | |
1414 | ||
1415 | unsigned seed = runtime_config::get<unsigned>( runtime_config::RANDOM_SEED ); | |
1416 | switch( seed ) { | |
1417 | case 0: | |
1418 | break; | |
1419 | case 1: | |
1420 | seed = static_cast<unsigned>( std::rand() ^ std::time( 0 ) ); // better init using std::rand() ^ ... | |
1421 | default: | |
1422 | BOOST_TEST_FRAMEWORK_MESSAGE( "Test cases order is shuffled using seed: " << seed ); | |
1423 | std::srand( seed ); | |
1424 | } | |
1425 | ||
1426 | impl::s_frk_state().execute_test_tree( id ); | |
1427 | ||
1428 | if( call_start_finish ) { | |
1429 | BOOST_TEST_REVERSE_FOREACH( test_observer*, to, impl::s_frk_state().m_observers ) | |
1430 | to->test_finish(); | |
1431 | } | |
1432 | ||
1433 | impl::s_frk_state().m_test_in_progress = was_in_progress; | |
1434 | } | |
1435 | ||
1436 | //____________________________________________________________________________// | |
1437 | ||
1438 | void | |
1439 | run( test_unit const* tu, bool continue_test ) | |
1440 | { | |
1441 | run( tu->p_id, continue_test ); | |
1442 | } | |
1443 | ||
1444 | //____________________________________________________________________________// | |
1445 | ||
1446 | // ************************************************************************** // | |
1447 | // ************** assertion_result ************** // | |
1448 | // ************************************************************************** // | |
1449 | ||
1450 | void | |
1451 | assertion_result( unit_test::assertion_result ar ) | |
1452 | { | |
1453 | BOOST_TEST_FOREACH( test_observer*, to, impl::s_frk_state().m_observers ) | |
1454 | to->assertion_result( ar ); | |
1455 | } | |
1456 | ||
1457 | //____________________________________________________________________________// | |
1458 | ||
1459 | // ************************************************************************** // | |
1460 | // ************** exception_caught ************** // | |
1461 | // ************************************************************************** // | |
1462 | ||
1463 | void | |
1464 | exception_caught( execution_exception const& ex ) | |
1465 | { | |
1466 | BOOST_TEST_FOREACH( test_observer*, to, impl::s_frk_state().m_observers ) | |
1467 | to->exception_caught( ex ); | |
1468 | } | |
1469 | ||
1470 | //____________________________________________________________________________// | |
1471 | ||
1472 | // ************************************************************************** // | |
1473 | // ************** test_unit_aborted ************** // | |
1474 | // ************************************************************************** // | |
1475 | ||
1476 | void | |
1477 | test_unit_aborted( test_unit const& tu ) | |
1478 | { | |
1479 | BOOST_TEST_FOREACH( test_observer*, to, impl::s_frk_state().m_observers ) | |
1480 | to->test_unit_aborted( tu ); | |
1481 | } | |
1482 | ||
1483 | //____________________________________________________________________________// | |
1484 | ||
1485 | } // namespace framework | |
1486 | } // namespace unit_test | |
1487 | } // namespace boost | |
1488 | ||
1489 | #include <boost/test/detail/enable_warnings.hpp> | |
1490 | ||
1491 | #endif // BOOST_TEST_FRAMEWORK_IPP_021005GER |