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