]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/test/impl/framework.ipp
update sources to ceph Nautilus 14.2.1
[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 // test_tree_visitor interface
351 virtual bool visit( test_unit const& tu )
352 {
353 const_cast<test_unit&>(tu).p_run_status.value = m_new_status == test_unit::RS_INVALID ? tu.p_default_status : m_new_status;
354 if( m_dep_collector ) {
355 BOOST_TEST_FOREACH( test_unit_id, dep_id, tu.p_dependencies.get() ) {
356 test_unit const& dep = framework::get( dep_id, TUT_ANY );
357
358 if( dep.p_run_status == tu.p_run_status )
359 continue;
360
361 BOOST_TEST_FRAMEWORK_MESSAGE( "Including test " << dep.p_type_name << ' ' << dep.full_name() <<
362 " as a dependency of test " << tu.p_type_name << ' ' << tu.full_name() );
363
364 m_dep_collector->push_back( dep_id );
365 }
366 }
367 return true;
368 }
369
370 private:
371 // Data members
372 test_unit::run_status m_new_status;
373 test_unit_id_list* m_dep_collector;
374 };
375
376 // ************************************************************************** //
377 // ************** parse_filters ************** //
378 // ************************************************************************** //
379
380 static void
381 add_filtered_test_units( test_unit_id master_tu_id, const_string filter, test_unit_id_list& targ )
382 {
383 // Choose between two kinds of filters
384 if( filter[0] == '@' ) {
385 filter.trim_left( 1 );
386 label_filter lf( targ, filter );
387 traverse_test_tree( master_tu_id, lf, true );
388 }
389 else {
390 name_filter nf( targ, filter );
391 traverse_test_tree( master_tu_id, nf, true );
392 }
393 }
394
395 //____________________________________________________________________________//
396
397 static bool
398 parse_filters( test_unit_id master_tu_id, test_unit_id_list& tu_to_enable, test_unit_id_list& tu_to_disable )
399 {
400 // 10. collect tu to enable and disable based on filters
401 bool had_selector_filter = false;
402
403 std::vector<std::string> const& filters = runtime_config::get<std::vector<std::string> >( runtime_config::btrt_run_filters );
404
405 BOOST_TEST_FOREACH( const_string, filter, filters ) {
406 BOOST_TEST_SETUP_ASSERT( !filter.is_empty(), "Invalid filter specification" );
407
408 // each --run_test command may also be separated by a ':' (environment variable)
409 utils::string_token_iterator t_filter_it( filter, (utils::dropped_delimeters = ":",
410 utils::kept_delimeters = utils::dt_none) );
411
412 while( t_filter_it != utils::string_token_iterator() ) {
413 const_string filter_token = *t_filter_it;
414
415 enum { SELECTOR, ENABLER, DISABLER } filter_type = SELECTOR;
416
417 // 11. Deduce filter type
418 if( filter_token[0] == '!' || filter_token[0] == '+' ) {
419 filter_type = filter_token[0] == '+' ? ENABLER : DISABLER;
420 filter_token.trim_left( 1 );
421 BOOST_TEST_SETUP_ASSERT( !filter_token.is_empty(), "Invalid filter specification" );
422 }
423
424 had_selector_filter |= filter_type == SELECTOR;
425
426 // 12. Add test units to corresponding list
427 switch( filter_type ) {
428 case SELECTOR:
429 case ENABLER: add_filtered_test_units( master_tu_id, filter_token, tu_to_enable ); break;
430 case DISABLER: add_filtered_test_units( master_tu_id, filter_token, tu_to_disable ); break;
431 }
432
433 ++t_filter_it;
434 }
435 }
436
437 return had_selector_filter;
438 }
439
440 //____________________________________________________________________________//
441
442 #ifdef BOOST_NO_CXX98_RANDOM_SHUFFLE
443
444 // a poor man's implementation of random_shuffle
445 template< class RandomIt, class RandomFunc >
446 void random_shuffle( RandomIt first, RandomIt last, RandomFunc &r )
447 {
448 typedef typename std::iterator_traits<RandomIt>::difference_type difference_type;
449 difference_type n = last - first;
450 for (difference_type i = n-1; i > 0; --i) {
451 difference_type j = r(i+1);
452 if (j != i) {
453 using std::swap;
454 swap(first[i], first[j]);
455 }
456 }
457 }
458
459 #endif
460
461
462 // A simple handle for registering the global fixtures to the master test suite
463 // without deleting an existing static object (the global fixture itself) when the program
464 // terminates (shared_ptr).
465 class global_fixture_handle : public test_unit_fixture {
466 public:
467 global_fixture_handle(test_unit_fixture* fixture) : m_global_fixture(fixture) {}
468 ~global_fixture_handle() {}
469
470 virtual void setup() {
471 m_global_fixture->setup();
472 }
473 virtual void teardown() {
474 m_global_fixture->teardown();
475 }
476
477 private:
478 test_unit_fixture* m_global_fixture;
479 };
480
481
482 } // namespace impl
483
484 // ************************************************************************** //
485 // ************** framework::state ************** //
486 // ************************************************************************** //
487
488 unsigned const TIMEOUT_EXCEEDED = static_cast<unsigned>( -1 );
489
490 class state {
491 public:
492 state()
493 : m_master_test_suite( 0 )
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 are 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 // Add the dependencies of the parent suites, see trac #13149
622 test_unit_id parent_id = tu.p_parent_id;
623 while( parent_id != INV_TEST_UNIT_ID
624 && parent_id != master_tu_id )
625 {
626 // we do not use the traverse_test_tree as otherwise it would enable the sibblings and subtree
627 // of the test case we want to enable (we need to enable the parent suites and their dependencies only)
628 // the parent_id needs to be enabled in order to be properly parsed by finalize_run_status, the visit
629 // does the job
630 test_unit& tu_parent = framework::get( parent_id, TUT_ANY );
631 enabler.visit( tu_parent );
632 parent_id = tu_parent.p_parent_id;
633 }
634 }
635
636 // 40. Apply all disablers
637 while( !tu_to_disable.empty() ) {
638 test_unit const& tu = framework::get( tu_to_disable.back(), TUT_ANY );
639
640 tu_to_disable.pop_back();
641
642 // 35. Ignore test units which already disabled
643 if( !tu.is_enabled() )
644 continue;
645
646 set_run_status disabler( test_unit::RS_DISABLED );
647 traverse_test_tree( tu.p_id, disabler, true );
648 }
649
650 // 50. Make sure parents of enabled test units are also enabled
651 finalize_run_status( master_tu_id );
652 }
653
654 //////////////////////////////////////////////////////////////////
655
656 typedef unit_test_monitor_t::error_level execution_result;
657
658 // Random generator using the std::rand function (seeded prior to the call)
659 struct random_generator_helper {
660 size_t operator()(size_t i) const {
661 return std::rand() % i;
662 }
663 };
664
665 // Executes the test tree with the root at specified test unit
666 execution_result execute_test_tree( test_unit_id tu_id,
667 unsigned timeout = 0,
668 random_generator_helper const * const p_random_generator = 0)
669 {
670 test_unit const& tu = framework::get( tu_id, TUT_ANY );
671
672 execution_result result = unit_test_monitor_t::test_ok;
673
674 if( !tu.is_enabled() )
675 return result;
676
677 // 10. Check preconditions, including zero time left for execution and
678 // successful execution of all dependencies
679 if( timeout == TIMEOUT_EXCEEDED ) {
680 // notify all observers about skipped test unit
681 BOOST_TEST_FOREACH( test_observer*, to, m_observers )
682 to->test_unit_skipped( tu, "timeout for the test unit is exceeded" );
683
684 return unit_test_monitor_t::os_timeout;
685 }
686 else if( timeout == 0 || timeout > tu.p_timeout ) // deduce timeout for this test unit
687 timeout = tu.p_timeout;
688
689 test_tools::assertion_result const precondition_res = tu.check_preconditions();
690 if( !precondition_res ) {
691 // notify all observers about skipped test unit
692 BOOST_TEST_FOREACH( test_observer*, to, m_observers )
693 to->test_unit_skipped( tu, precondition_res.message() );
694
695 return unit_test_monitor_t::precondition_failure;
696 }
697
698 // 20. Notify all observers about the start of the test unit
699 BOOST_TEST_FOREACH( test_observer*, to, m_observers )
700 to->test_unit_start( tu );
701
702 // 30. Execute setup fixtures if any; any failure here leads to test unit abortion
703 BOOST_TEST_FOREACH( test_unit_fixture_ptr, F, tu.p_fixtures.get() ) {
704 ut_detail::test_unit_id_restore restore_current_test_unit(m_curr_test_unit, tu.p_id);
705 result = unit_test_monitor.execute_and_translate( boost::bind( &test_unit_fixture::setup, F ) );
706 if( result != unit_test_monitor_t::test_ok )
707 break;
708 test_results const& test_rslt = unit_test::results_collector.results( m_curr_test_unit );
709 if( test_rslt.aborted() ) {
710 result = unit_test_monitor_t::precondition_failure;
711 break;
712 }
713 }
714
715 // This is the time we are going to spend executing the test unit
716 unsigned long elapsed = 0;
717
718 if( result == unit_test_monitor_t::test_ok ) {
719 // 40. We are going to time the execution
720 boost::timer tu_timer;
721
722 // we pass the random generator
723 const random_generator_helper& rand_gen = p_random_generator ? *p_random_generator : random_generator_helper();
724
725 if( tu.p_type == TUT_SUITE ) {
726 test_suite const& ts = static_cast<test_suite const&>( tu );
727
728 if( runtime_config::get<unsigned>( runtime_config::btrt_random_seed ) == 0 ) {
729 typedef std::pair<counter_t,test_unit_id> value_type;
730
731 BOOST_TEST_FOREACH( value_type, chld, ts.m_ranked_children ) {
732 unsigned chld_timeout = child_timeout( timeout, tu_timer.elapsed() );
733
734 result = (std::min)( result, execute_test_tree( chld.second, chld_timeout, &rand_gen ) );
735
736 if( unit_test_monitor.is_critical_error( result ) )
737 break;
738 }
739 }
740 else {
741 // Go through ranges of children with the same dependency rank and shuffle them
742 // independently. Execute each subtree in this order
743 test_unit_id_list children_with_the_same_rank;
744
745 typedef test_suite::children_per_rank::const_iterator it_type;
746 it_type it = ts.m_ranked_children.begin();
747 while( it != ts.m_ranked_children.end() ) {
748 children_with_the_same_rank.clear();
749
750 std::pair<it_type,it_type> range = ts.m_ranked_children.equal_range( it->first );
751 it = range.first;
752 while( it != range.second ) {
753 children_with_the_same_rank.push_back( it->second );
754 it++;
755 }
756
757 #ifdef BOOST_NO_CXX98_RANDOM_SHUFFLE
758 impl::random_shuffle( children_with_the_same_rank.begin(), children_with_the_same_rank.end(), rand_gen );
759 #else
760 std::random_shuffle( children_with_the_same_rank.begin(), children_with_the_same_rank.end(), rand_gen );
761 #endif
762
763 BOOST_TEST_FOREACH( test_unit_id, chld, children_with_the_same_rank ) {
764 unsigned chld_timeout = child_timeout( timeout, tu_timer.elapsed() );
765
766 result = (std::min)( result, execute_test_tree( chld, chld_timeout, &rand_gen ) );
767
768 if( unit_test_monitor.is_critical_error( result ) )
769 break;
770 }
771 }
772 }
773
774 elapsed = static_cast<unsigned long>( tu_timer.elapsed() * 1e6 );
775 }
776 else { // TUT_CASE
777 test_case const& tc = static_cast<test_case const&>( tu );
778
779 // setup contexts
780 m_context_idx = 0;
781
782 // setup current test case
783 ut_detail::test_unit_id_restore restore_current_test_unit(m_curr_test_unit, tc.p_id);
784
785 // execute the test case body
786 result = unit_test_monitor.execute_and_translate( tc.p_test_func, timeout );
787 elapsed = static_cast<unsigned long>( tu_timer.elapsed() * 1e6 );
788
789 // cleanup leftover context
790 m_context.clear();
791
792 // restore state (scope exit) and abort if necessary
793 }
794 }
795
796 // if run error is critical skip teardown, who knows what the state of the program at this point
797 if( !unit_test_monitor.is_critical_error( result ) ) {
798 // execute teardown fixtures if any in reverse order
799 BOOST_TEST_REVERSE_FOREACH( test_unit_fixture_ptr, F, tu.p_fixtures.get() ) {
800 ut_detail::test_unit_id_restore restore_current_test_unit(m_curr_test_unit, tu.p_id);
801 result = (std::min)( result, unit_test_monitor.execute_and_translate( boost::bind( &test_unit_fixture::teardown, F ), 0 ) );
802
803 if( unit_test_monitor.is_critical_error( result ) )
804 break;
805 }
806 }
807
808 // notify all observers about abortion
809 if( unit_test_monitor.is_critical_error( result ) ) {
810 BOOST_TEST_FOREACH( test_observer*, to, m_observers )
811 to->test_aborted();
812 }
813
814 // notify all observers about completion
815 BOOST_TEST_REVERSE_FOREACH( test_observer*, to, m_observers )
816 to->test_unit_finish( tu, elapsed );
817
818 return result;
819 }
820
821 //////////////////////////////////////////////////////////////////
822
823 unsigned child_timeout( unsigned tu_timeout, double elapsed )
824 {
825 if( tu_timeout == 0U )
826 return 0U;
827
828 unsigned elpsed_sec = static_cast<unsigned>(elapsed); // rounding to number of whole seconds
829
830 return tu_timeout > elpsed_sec ? tu_timeout - elpsed_sec : TIMEOUT_EXCEEDED;
831 }
832
833 struct priority_order {
834 bool operator()( test_observer* lhs, test_observer* rhs ) const
835 {
836 return (lhs->priority() < rhs->priority()) || ((lhs->priority() == rhs->priority()) && (lhs < rhs));
837 }
838 };
839
840 // Data members
841 typedef std::map<test_unit_id,test_unit*> test_unit_store;
842 typedef std::set<test_observer*,priority_order> observer_store;
843 struct context_frame {
844 context_frame( std::string const& d, int id, bool sticky )
845 : descr( d )
846 , frame_id( id )
847 , is_sticky( sticky )
848 {}
849
850 std::string descr;
851 int frame_id;
852 bool is_sticky;
853 };
854 typedef std::vector<context_frame> context_data;
855
856 master_test_suite_t* m_master_test_suite;
857 std::vector<test_suite*> m_auto_test_suites;
858
859 test_unit_id m_curr_test_unit;
860 test_unit_store m_test_units;
861
862 test_unit_id m_next_test_case_id;
863 test_unit_id m_next_test_suite_id;
864
865 bool m_test_in_progress;
866
867 observer_store m_observers;
868 context_data m_context;
869 int m_context_idx;
870
871 std::set<test_unit_fixture*> m_global_fixtures;
872
873 boost::execution_monitor m_aux_em;
874
875 std::map<output_format, runtime_config::stream_holder> m_log_sinks;
876 runtime_config::stream_holder m_report_sink;
877 };
878
879 //____________________________________________________________________________//
880
881 namespace impl {
882 namespace {
883
884 #if defined(__CYGWIN__)
885 framework::state& s_frk_state() { static framework::state* the_inst = 0; if(!the_inst) the_inst = new framework::state; return *the_inst; }
886 #else
887 framework::state& s_frk_state() { static framework::state the_inst; return the_inst; }
888 #endif
889
890 } // local namespace
891
892 void
893 setup_for_execution( test_unit const& tu )
894 {
895 s_frk_state().deduce_run_status( tu.p_id );
896 }
897
898 struct sum_to_first_only {
899 sum_to_first_only() : is_first(true) {}
900 template <class T, class U>
901 T operator()(T const& l_, U const& r_) {
902 if(is_first) {
903 is_first = false;
904 return l_ + r_.first;
905 }
906 return l_ + ", " + r_.first;
907 }
908
909 bool is_first;
910 };
911
912 void
913 shutdown_loggers_and_reports()
914 {
915 s_frk_state().m_log_sinks.clear();
916 s_frk_state().m_report_sink.setup( "stderr" );
917 }
918
919 void
920 setup_loggers()
921 {
922
923 BOOST_TEST_I_TRY {
924
925 #ifdef BOOST_TEST_SUPPORT_TOKEN_ITERATOR
926 bool has_combined_logger = runtime_config::has( runtime_config::btrt_combined_logger )
927 && !runtime_config::get< std::vector<std::string> >( runtime_config::btrt_combined_logger ).empty();
928 #else
929 bool has_combined_logger = false;
930 #endif
931
932 if( !has_combined_logger ) {
933 unit_test_log.set_threshold_level( runtime_config::get<log_level>( runtime_config::btrt_log_level ) );
934 const output_format format = runtime_config::get<output_format>( runtime_config::btrt_log_format );
935 unit_test_log.set_format( format );
936
937 runtime_config::stream_holder& stream_logger = s_frk_state().m_log_sinks[format];
938 if( runtime_config::has( runtime_config::btrt_log_sink ) ) {
939 // we remove all streams in this case, so we do not specify the format
940 boost::function< void () > log_cleaner = boost::bind( &unit_test_log_t::set_stream,
941 &unit_test_log,
942 boost::ref(std::cout)
943 );
944 stream_logger.setup( runtime_config::get<std::string>( runtime_config::btrt_log_sink ),
945 log_cleaner );
946 }
947 unit_test_log.set_stream( stream_logger.ref() );
948 }
949 else
950 {
951
952 const std::vector<std::string>& v_output_format = runtime_config::get< std::vector<std::string> >( runtime_config::btrt_combined_logger ) ;
953
954 static const std::pair<const char*, log_level> all_log_levels[] = {
955 std::make_pair( "all" , log_successful_tests ),
956 std::make_pair( "success" , log_successful_tests ),
957 std::make_pair( "test_suite" , log_test_units ),
958 std::make_pair( "unit_scope" , log_test_units ),
959 std::make_pair( "message" , log_messages ),
960 std::make_pair( "warning" , log_warnings ),
961 std::make_pair( "error" , log_all_errors ),
962 std::make_pair( "cpp_exception" , log_cpp_exception_errors ),
963 std::make_pair( "system_error" , log_system_errors ),
964 std::make_pair( "fatal_error" , log_fatal_errors ),
965 std::make_pair( "nothing" , log_nothing )
966 };
967
968 static const std::pair<const char*, output_format> all_formats[] = {
969 std::make_pair( "HRF" , OF_CLF ),
970 std::make_pair( "CLF" , OF_CLF ),
971 std::make_pair( "XML" , OF_XML ),
972 std::make_pair( "JUNIT", OF_JUNIT )
973 };
974
975
976 bool is_first = true;
977
978 BOOST_TEST_FOREACH( const_string, current_multi_config, v_output_format ) {
979
980 #ifdef BOOST_TEST_SUPPORT_TOKEN_ITERATOR
981
982 // ':' may be used for file names: C:/tmp/mylogsink.xml
983 // we merge the tokens that start with / or \ with the previous one.
984 std::vector<std::string> v_processed_tokens;
985
986 {
987 utils::string_token_iterator current_config( current_multi_config, (utils::dropped_delimeters = ":",
988 utils::kept_delimeters = utils::dt_none) );
989
990 for( ; current_config != utils::string_token_iterator() ; ++current_config) {
991 std::string str_copy(current_config->begin(), current_config->end());
992 if( ( str_copy[0] == '\\' || str_copy[0] == '/' )
993 && v_processed_tokens.size() > 0) {
994 v_processed_tokens.back() += ":" + str_copy; // ':' has been eaten up
995 }
996 else {
997 v_processed_tokens.push_back(str_copy);
998 }
999 }
1000 }
1001
1002 BOOST_TEST_FOREACH( std::string const&, current_config, v_processed_tokens ) {
1003
1004 utils::string_token_iterator current_format_specs( current_config, (utils::keep_empty_tokens,
1005 utils::dropped_delimeters = ",",
1006 utils::kept_delimeters = utils::dt_none) );
1007
1008 output_format format = OF_INVALID ; // default
1009 if( current_format_specs != utils::string_token_iterator() &&
1010 current_format_specs->size() ) {
1011
1012 for(size_t elem=0; elem < sizeof(all_formats)/sizeof(all_formats[0]); elem++) {
1013 if(const_string(all_formats[elem].first) == *current_format_specs) {
1014 format = all_formats[elem].second;
1015 break;
1016 }
1017 }
1018 }
1019
1020 BOOST_TEST_I_ASSRT( format != OF_INVALID,
1021 boost::runtime::access_to_missing_argument()
1022 << "Unable to determine the logger type from '"
1023 << current_config
1024 << "'. Possible choices are: "
1025 << std::accumulate(all_formats,
1026 all_formats + sizeof(all_formats)/sizeof(all_formats[0]),
1027 std::string(""),
1028 sum_to_first_only())
1029 );
1030
1031 // activates this format
1032 if( is_first ) {
1033 unit_test_log.set_format( format );
1034 }
1035 else {
1036 unit_test_log.add_format( format );
1037 }
1038 is_first = false;
1039
1040 unit_test_log_formatter * const formatter = unit_test_log.get_formatter(format);
1041 BOOST_TEST_SETUP_ASSERT( formatter, "Logger setup error" );
1042
1043 log_level formatter_log_level = invalid_log_level;
1044 ++current_format_specs ;
1045 if( !current_format_specs->size() ) {
1046 formatter_log_level = formatter->get_log_level(); // default log level given by the formatter
1047 }
1048 else if( current_format_specs != utils::string_token_iterator() ) {
1049
1050 for(size_t elem=0; elem < sizeof(all_log_levels)/sizeof(all_log_levels[0]); elem++) {
1051 if(const_string(all_log_levels[elem].first) == *current_format_specs) {
1052 formatter_log_level = all_log_levels[elem].second;
1053 break;
1054 }
1055 }
1056 }
1057
1058 BOOST_TEST_I_ASSRT( formatter_log_level != invalid_log_level,
1059 boost::runtime::access_to_missing_argument()
1060 << "Unable to determine the log level from '"
1061 << current_config
1062 << "'. Possible choices are: "
1063 << std::accumulate(all_log_levels,
1064 all_log_levels + sizeof(all_log_levels)/sizeof(all_log_levels[0]),
1065 std::string(""),
1066 sum_to_first_only())
1067 );
1068
1069 unit_test_log.set_threshold_level( format, formatter_log_level );
1070
1071 runtime_config::stream_holder& stream_logger = s_frk_state().m_log_sinks[format];
1072 boost::function< void () > log_cleaner = boost::bind( &unit_test_log_t::set_stream,
1073 &unit_test_log,
1074 format,
1075 boost::ref(std::cout) );
1076 if( ++current_format_specs != utils::string_token_iterator() &&
1077 current_format_specs->size() ) {
1078 stream_logger.setup( *current_format_specs,
1079 log_cleaner );
1080 }
1081 else {
1082 stream_logger.setup( formatter->get_default_stream_description(),
1083 log_cleaner );
1084 }
1085 unit_test_log.set_stream( format, stream_logger.ref() );
1086 }
1087 #endif
1088 } // for each logger
1089
1090 } // if/else new logger API
1091 } // BOOST_TEST_I_TRY
1092 BOOST_TEST_I_CATCH( boost::runtime::init_error, ex ) {
1093 BOOST_TEST_SETUP_ASSERT( false, ex.msg );
1094 }
1095 BOOST_TEST_I_CATCH( boost::runtime::input_error, ex ) {
1096 std::cerr << ex.msg << "\n\n";
1097
1098 BOOST_TEST_I_THROW( framework::nothing_to_test( boost::exit_exception_failure ) );
1099 }
1100
1101
1102 }
1103
1104 //____________________________________________________________________________//
1105
1106 } // namespace impl
1107
1108 //____________________________________________________________________________//
1109
1110 // ************************************************************************** //
1111 // ************** framework::init ************** //
1112 // ************************************************************************** //
1113
1114 void
1115 init( init_unit_test_func init_func, int argc, char* argv[] )
1116 {
1117 using namespace impl;
1118
1119 // 10. Set up runtime parameters
1120 runtime_config::init( argc, argv );
1121
1122 // 20. Set the desired log level, format and sink
1123 impl::setup_loggers();
1124
1125 // 30. Set the desired report level, format and sink
1126 results_reporter::set_level( runtime_config::get<report_level>( runtime_config::btrt_report_level ) );
1127 results_reporter::set_format( runtime_config::get<output_format>( runtime_config::btrt_report_format ) );
1128
1129 if( runtime_config::has( runtime_config::btrt_report_sink ) ) {
1130 boost::function< void () > report_cleaner = boost::bind( &results_reporter::set_stream,
1131 boost::ref(std::cerr)
1132 );
1133 s_frk_state().m_report_sink.setup( runtime_config::get<std::string>( runtime_config::btrt_report_sink ),
1134 report_cleaner );
1135 }
1136
1137 results_reporter::set_stream( s_frk_state().m_report_sink.ref() );
1138
1139 // 40. Register default test observers
1140 register_observer( results_collector );
1141 register_observer( unit_test_log );
1142 register_observer( framework_init_observer );
1143
1144 if( runtime_config::get<bool>( runtime_config::btrt_show_progress ) ) {
1145 progress_monitor.set_stream( std::cout ); // defaults to stdout
1146 register_observer( progress_monitor );
1147 }
1148
1149 // 50. Set up memory leak detection
1150 unsigned long detect_mem_leak = runtime_config::get<unsigned long>( runtime_config::btrt_detect_mem_leaks );
1151 if( detect_mem_leak > 0 ) {
1152 debug::detect_memory_leaks( true, runtime_config::get<std::string>( runtime_config::btrt_report_mem_leaks ) );
1153 debug::break_memory_alloc( (long)detect_mem_leak );
1154 }
1155
1156 // 60. Initialize master unit test suite
1157 master_test_suite().argc = argc;
1158 master_test_suite().argv = argv;
1159
1160 // 70. Invoke test module initialization routine
1161 BOOST_TEST_I_TRY {
1162 s_frk_state().m_aux_em.vexecute( boost::bind( &impl::invoke_init_func, init_func ) );
1163 }
1164 BOOST_TEST_I_CATCH( execution_exception, ex ) {
1165 BOOST_TEST_SETUP_ASSERT( false, ex.what() );
1166 }
1167 }
1168
1169 //____________________________________________________________________________//
1170
1171 void
1172 finalize_setup_phase( test_unit_id master_tu_id )
1173 {
1174 if( master_tu_id == INV_TEST_UNIT_ID )
1175 master_tu_id = master_test_suite().p_id;
1176
1177 // 10. Apply all decorators to the auto test units
1178 class apply_decorators : public test_tree_visitor {
1179 private:
1180 // test_tree_visitor interface
1181 virtual bool visit( test_unit const& tu )
1182 {
1183 BOOST_TEST_FOREACH( decorator::base_ptr, d, tu.p_decorators.get() )
1184 d->apply( const_cast<test_unit&>(tu) );
1185
1186 return true;
1187 }
1188 } ad;
1189 traverse_test_tree( master_tu_id, ad, true );
1190
1191 // 20. Finalize setup phase
1192 impl::order_info_per_tu tuoi;
1193 impl::s_frk_state().deduce_siblings_order( master_tu_id, master_tu_id, tuoi );
1194 impl::s_frk_state().finalize_default_run_status( master_tu_id, test_unit::RS_INVALID );
1195 }
1196
1197 // ************************************************************************** //
1198 // ************** test_in_progress ************** //
1199 // ************************************************************************** //
1200
1201 bool
1202 test_in_progress()
1203 {
1204 return impl::s_frk_state().m_test_in_progress;
1205 }
1206
1207 //____________________________________________________________________________//
1208
1209 // ************************************************************************** //
1210 // ************** framework::shutdown ************** //
1211 // ************************************************************************** //
1212
1213 void
1214 shutdown()
1215 {
1216 impl::shutdown_loggers_and_reports();
1217 // eliminating some fake memory leak reports. See for more details:
1218 // http://connect.microsoft.com/VisualStudio/feedback/details/106937/memory-leaks-reported-by-debug-crt-inside-typeinfo-name
1219
1220 # if BOOST_WORKAROUND(BOOST_MSVC, <= 1600 ) && !defined(_DLL) && defined(_DEBUG)
1221 # if BOOST_WORKAROUND(BOOST_MSVC, < 1600 )
1222 #define _Next next
1223 #define _MemPtr memPtr
1224 #endif
1225 __type_info_node* pNode = __type_info_root_node._Next;
1226 __type_info_node* tmpNode = &__type_info_root_node;
1227
1228 for( ; pNode!=NULL; pNode = tmpNode ) {
1229 tmpNode = pNode->_Next;
1230 delete pNode->_MemPtr;
1231 delete pNode;
1232 }
1233 # if BOOST_WORKAROUND(BOOST_MSVC, < 1600 )
1234 #undef _Next
1235 #undef _MemPtr
1236 #endif
1237 # endif
1238 }
1239
1240 //____________________________________________________________________________//
1241
1242 // ************************************************************************** //
1243 // ************** register_test_unit ************** //
1244 // ************************************************************************** //
1245
1246 void
1247 register_test_unit( test_case* tc )
1248 {
1249 BOOST_TEST_SETUP_ASSERT( tc->p_id == INV_TEST_UNIT_ID, BOOST_TEST_L( "test case already registered" ) );
1250
1251 test_unit_id new_id = impl::s_frk_state().m_next_test_case_id;
1252
1253 BOOST_TEST_SETUP_ASSERT( new_id != MAX_TEST_CASE_ID, BOOST_TEST_L( "too many test cases" ) );
1254
1255 typedef state::test_unit_store::value_type map_value_type;
1256
1257 impl::s_frk_state().m_test_units.insert( map_value_type( new_id, tc ) );
1258 impl::s_frk_state().m_next_test_case_id++;
1259
1260 impl::s_frk_state().set_tu_id( *tc, new_id );
1261 }
1262
1263 //____________________________________________________________________________//
1264
1265 // ************************************************************************** //
1266 // ************** register_test_unit ************** //
1267 // ************************************************************************** //
1268
1269 void
1270 register_test_unit( test_suite* ts )
1271 {
1272 BOOST_TEST_SETUP_ASSERT( ts->p_id == INV_TEST_UNIT_ID, BOOST_TEST_L( "test suite already registered" ) );
1273
1274 test_unit_id new_id = impl::s_frk_state().m_next_test_suite_id;
1275
1276 BOOST_TEST_SETUP_ASSERT( new_id != MAX_TEST_SUITE_ID, BOOST_TEST_L( "too many test suites" ) );
1277
1278 typedef state::test_unit_store::value_type map_value_type;
1279
1280 impl::s_frk_state().m_test_units.insert( map_value_type( new_id, ts ) );
1281 impl::s_frk_state().m_next_test_suite_id++;
1282
1283 impl::s_frk_state().set_tu_id( *ts, new_id );
1284 }
1285
1286 //____________________________________________________________________________//
1287
1288 // ************************************************************************** //
1289 // ************** deregister_test_unit ************** //
1290 // ************************************************************************** //
1291
1292 void
1293 deregister_test_unit( test_unit* tu )
1294 {
1295 impl::s_frk_state().m_test_units.erase( tu->p_id );
1296 }
1297
1298 //____________________________________________________________________________//
1299
1300 // ************************************************************************** //
1301 // ************** clear ************** //
1302 // ************************************************************************** //
1303
1304 void
1305 clear()
1306 {
1307 impl::s_frk_state().clear();
1308 }
1309
1310 //____________________________________________________________________________//
1311
1312 // ************************************************************************** //
1313 // ************** register_observer ************** //
1314 // ************************************************************************** //
1315
1316 void
1317 register_observer( test_observer& to )
1318 {
1319 impl::s_frk_state().m_observers.insert( &to );
1320 }
1321
1322 //____________________________________________________________________________//
1323
1324 // ************************************************************************** //
1325 // ************** deregister_observer ************** //
1326 // ************************************************************************** //
1327
1328 void
1329 deregister_observer( test_observer& to )
1330 {
1331 impl::s_frk_state().m_observers.erase( &to );
1332 }
1333
1334 //____________________________________________________________________________//
1335
1336 // ************************************************************************** //
1337 // ************** register_global_fixture ************** //
1338 // ************************************************************************** //
1339
1340 void
1341 register_global_fixture( test_unit_fixture& tuf )
1342 {
1343 impl::s_frk_state().m_global_fixtures.insert( &tuf );
1344 }
1345
1346 //____________________________________________________________________________//
1347
1348 // ************************************************************************** //
1349 // ************** deregister_global_fixture ************** //
1350 // ************************************************************************** //
1351
1352 void
1353 deregister_global_fixture( test_unit_fixture &tuf )
1354 {
1355 impl::s_frk_state().m_global_fixtures.erase( &tuf );
1356 }
1357
1358 //____________________________________________________________________________//
1359
1360 // ************************************************************************** //
1361 // ************** add_context ************** //
1362 // ************************************************************************** //
1363
1364 int
1365 add_context( ::boost::unit_test::lazy_ostream const& context_descr, bool sticky )
1366 {
1367 std::stringstream buffer;
1368 context_descr( buffer );
1369 int res_idx = impl::s_frk_state().m_context_idx++;
1370
1371 impl::s_frk_state().m_context.push_back( state::context_frame( buffer.str(), res_idx, sticky ) );
1372
1373 return res_idx;
1374 }
1375
1376 //____________________________________________________________________________//
1377
1378 // ************************************************************************** //
1379 // ************** clear_context ************** //
1380 // ************************************************************************** //
1381
1382 struct frame_with_id {
1383 explicit frame_with_id( int id ) : m_id( id ) {}
1384
1385 bool operator()( state::context_frame const& f )
1386 {
1387 return f.frame_id == m_id;
1388 }
1389 int m_id;
1390 };
1391
1392 //____________________________________________________________________________//
1393
1394 void
1395 clear_context( int frame_id )
1396 {
1397 if( frame_id == -1 ) { // clear all non sticky frames
1398 for( int i=static_cast<int>(impl::s_frk_state().m_context.size())-1; i>=0; i-- )
1399 if( !impl::s_frk_state().m_context[i].is_sticky )
1400 impl::s_frk_state().m_context.erase( impl::s_frk_state().m_context.begin()+i );
1401 }
1402
1403 else { // clear specific frame
1404 state::context_data::iterator it =
1405 std::find_if( impl::s_frk_state().m_context.begin(), impl::s_frk_state().m_context.end(), frame_with_id( frame_id ) );
1406
1407 if( it != impl::s_frk_state().m_context.end() ) // really an internal error if this is not true
1408 impl::s_frk_state().m_context.erase( it );
1409 }
1410 }
1411
1412 //____________________________________________________________________________//
1413
1414 // ************************************************************************** //
1415 // ************** get_context ************** //
1416 // ************************************************************************** //
1417
1418 context_generator
1419 get_context()
1420 {
1421 return context_generator();
1422 }
1423
1424 //____________________________________________________________________________//
1425
1426 // ************************************************************************** //
1427 // ************** context_generator ************** //
1428 // ************************************************************************** //
1429
1430 bool
1431 context_generator::is_empty() const
1432 {
1433 return impl::s_frk_state().m_context.empty();
1434 }
1435
1436 //____________________________________________________________________________//
1437
1438 const_string
1439 context_generator::next() const
1440 {
1441 return m_curr_frame < impl::s_frk_state().m_context.size() ? impl::s_frk_state().m_context[m_curr_frame++].descr : const_string();
1442 }
1443
1444 //____________________________________________________________________________//
1445
1446 // ************************************************************************** //
1447 // ************** master_test_suite ************** //
1448 // ************************************************************************** //
1449
1450 master_test_suite_t&
1451 master_test_suite()
1452 {
1453 if( !impl::s_frk_state().m_master_test_suite )
1454 impl::s_frk_state().m_master_test_suite = new master_test_suite_t;
1455
1456 return *impl::s_frk_state().m_master_test_suite;
1457 }
1458
1459 //____________________________________________________________________________//
1460
1461 // ************************************************************************** //
1462 // ************** current_auto_test_suite ************** //
1463 // ************************************************************************** //
1464
1465 test_suite&
1466 current_auto_test_suite( test_suite* ts, bool push_or_pop )
1467 {
1468 if( impl::s_frk_state().m_auto_test_suites.empty() )
1469 impl::s_frk_state().m_auto_test_suites.push_back( &framework::master_test_suite() );
1470
1471 if( !push_or_pop )
1472 impl::s_frk_state().m_auto_test_suites.pop_back();
1473 else if( ts )
1474 impl::s_frk_state().m_auto_test_suites.push_back( ts );
1475
1476 return *impl::s_frk_state().m_auto_test_suites.back();
1477 }
1478
1479 //____________________________________________________________________________//
1480
1481 // ************************************************************************** //
1482 // ************** current_test_case ************** //
1483 // ************************************************************************** //
1484
1485 test_case const&
1486 current_test_case()
1487 {
1488 return get<test_case>( impl::s_frk_state().m_curr_test_unit );
1489 }
1490
1491
1492 test_unit const&
1493 current_test_unit()
1494 {
1495 return *impl::s_frk_state().m_test_units[impl::s_frk_state().m_curr_test_unit];
1496 }
1497
1498 //____________________________________________________________________________//
1499
1500 test_unit_id
1501 current_test_case_id()
1502 {
1503 return impl::s_frk_state().m_curr_test_unit;
1504 }
1505
1506 //____________________________________________________________________________//
1507
1508 // ************************************************************************** //
1509 // ************** framework::get ************** //
1510 // ************************************************************************** //
1511
1512 test_unit&
1513 get( test_unit_id id, test_unit_type t )
1514 {
1515 test_unit* res = impl::s_frk_state().m_test_units[id];
1516
1517 BOOST_TEST_I_ASSRT( (res->p_type & t) != 0, internal_error( "Invalid test unit type" ) );
1518
1519 return *res;
1520 }
1521
1522 //____________________________________________________________________________//
1523
1524 // ************************************************************************** //
1525 // ************** framework::run ************** //
1526 // ************************************************************************** //
1527
1528 template <class Cont>
1529 struct swap_on_delete {
1530 swap_on_delete(Cont& c1, Cont& c2) : m_c1(c1), m_c2(c2){}
1531 ~swap_on_delete() {
1532 m_c1.swap(m_c2);
1533 }
1534
1535 Cont& m_c1;
1536 Cont& m_c2;
1537 };
1538
1539 void
1540 run( test_unit_id id, bool continue_test )
1541 {
1542 if( id == INV_TEST_UNIT_ID )
1543 id = master_test_suite().p_id;
1544
1545 // Figure out run status for execution phase
1546 impl::s_frk_state().deduce_run_status( id );
1547
1548 test_case_counter tcc;
1549 traverse_test_tree( id, tcc );
1550
1551 BOOST_TEST_SETUP_ASSERT( tcc.p_count != 0 , runtime_config::get<std::vector<std::string> >( runtime_config::btrt_run_filters ).empty()
1552 ? BOOST_TEST_L( "test tree is empty" )
1553 : BOOST_TEST_L( "no test cases matching filter or all test cases were disabled" ) );
1554
1555 bool was_in_progress = framework::test_in_progress();
1556 bool call_start_finish = !continue_test || !was_in_progress;
1557 bool init_ok = true;
1558 const_string setup_error;
1559
1560 if( call_start_finish ) {
1561 // indicates the framework that no test is in progress now if observers need to be notified
1562 impl::s_frk_state().m_test_in_progress = false;
1563 // unit_test::framework_init_observer will get cleared first
1564 BOOST_TEST_FOREACH( test_observer*, to, impl::s_frk_state().m_observers ) {
1565 BOOST_TEST_I_TRY {
1566 ut_detail::test_unit_id_restore restore_current_test_unit(impl::s_frk_state().m_curr_test_unit, id);
1567 unit_test_monitor_t::error_level result = unit_test_monitor.execute_and_translate( boost::bind( &test_observer::test_start, to, tcc.p_count ) );
1568 if( init_ok ) {
1569 if( result != unit_test_monitor_t::test_ok ) {
1570 init_ok = false;
1571 }
1572 else {
1573 if( unit_test::framework_init_observer.has_failed() ) {
1574 init_ok = false;
1575 }
1576 }
1577 }
1578 }
1579 BOOST_TEST_I_CATCH( execution_exception, ex ) {
1580 if( init_ok ) {
1581 // log only the first error
1582 init_ok = false;
1583 setup_error = ex.what();
1584 }
1585 // break; // we should continue otherwise loggers may have improper structure (XML start missing for instance)
1586 }
1587 }
1588 }
1589
1590 if( init_ok ) {
1591
1592 // attaching the global fixtures to the main entry point
1593 test_unit& entry_test_unit = framework::get( id, TUT_ANY );
1594 std::vector<test_unit_fixture_ptr> v_saved_fixture(entry_test_unit.p_fixtures.value.begin(),
1595 entry_test_unit.p_fixtures.value.end());
1596
1597 BOOST_TEST_FOREACH( test_unit_fixture*, tuf, impl::s_frk_state().m_global_fixtures ) {
1598 entry_test_unit.p_fixtures.value.insert( entry_test_unit.p_fixtures.value.begin(),
1599 test_unit_fixture_ptr(new impl::global_fixture_handle(tuf)) );
1600 }
1601
1602 swap_on_delete< std::vector<test_unit_fixture_ptr> > raii_fixture(v_saved_fixture, entry_test_unit.p_fixtures.value);
1603
1604 // now work in progress
1605 impl::s_frk_state().m_test_in_progress = true;
1606 unsigned seed = runtime_config::get<unsigned>( runtime_config::btrt_random_seed );
1607 switch( seed ) {
1608 case 0:
1609 break;
1610 case 1:
1611 seed = static_cast<unsigned>( std::rand() ^ std::time( 0 ) ); // better init using std::rand() ^ ...
1612 BOOST_FALLTHROUGH;
1613 default:
1614 BOOST_TEST_FRAMEWORK_MESSAGE( "Test cases order is shuffled using seed: " << seed );
1615 std::srand( seed );
1616 }
1617
1618 // executing the test tree
1619 impl::s_frk_state().execute_test_tree( id );
1620
1621 // removing previously added global fixtures: dtor raii_fixture
1622 }
1623
1624 impl::s_frk_state().m_test_in_progress = false;
1625
1626 results_reporter::make_report( INV_REPORT_LEVEL, id );
1627
1628 unit_test::framework_init_observer.clear();
1629 if( call_start_finish ) {
1630 // indicates the framework that no test is in progress anymore if observers need to be notified
1631 // and this is a teardown, so assertions should not raise any exception otherwise an exception
1632 // might be raised in a dtor of a global fixture
1633 impl::s_frk_state().m_test_in_progress = false;
1634 BOOST_TEST_REVERSE_FOREACH( test_observer*, to, impl::s_frk_state().m_observers ) {
1635 ut_detail::test_unit_id_restore restore_current_test_unit(impl::s_frk_state().m_curr_test_unit, id);
1636 to->test_finish();
1637 }
1638 }
1639
1640 impl::s_frk_state().m_test_in_progress = was_in_progress;
1641
1642 // propagates the init/teardown error if any
1643 BOOST_TEST_SETUP_ASSERT( init_ok && !unit_test::framework_init_observer.has_failed(), setup_error );
1644 }
1645
1646 //____________________________________________________________________________//
1647
1648 void
1649 run( test_unit const* tu, bool continue_test )
1650 {
1651 run( tu->p_id, continue_test );
1652 }
1653
1654 //____________________________________________________________________________//
1655
1656 // ************************************************************************** //
1657 // ************** assertion_result ************** //
1658 // ************************************************************************** //
1659
1660 void
1661 assertion_result( unit_test::assertion_result ar )
1662 {
1663 BOOST_TEST_FOREACH( test_observer*, to, impl::s_frk_state().m_observers )
1664 to->assertion_result( ar );
1665 }
1666
1667 //____________________________________________________________________________//
1668
1669 // ************************************************************************** //
1670 // ************** exception_caught ************** //
1671 // ************************************************************************** //
1672
1673 void
1674 exception_caught( execution_exception const& ex )
1675 {
1676 BOOST_TEST_FOREACH( test_observer*, to, impl::s_frk_state().m_observers )
1677 to->exception_caught( ex );
1678 }
1679
1680 //____________________________________________________________________________//
1681
1682 // ************************************************************************** //
1683 // ************** test_unit_aborted ************** //
1684 // ************************************************************************** //
1685
1686 void
1687 test_unit_aborted( test_unit const& tu )
1688 {
1689 BOOST_TEST_FOREACH( test_observer*, to, impl::s_frk_state().m_observers )
1690 to->test_unit_aborted( tu );
1691 }
1692
1693 // ************************************************************************** //
1694 // ************** test_aborted ************** //
1695 // ************************************************************************** //
1696
1697 void
1698 test_aborted( )
1699 {
1700 BOOST_TEST_FOREACH( test_observer*, to, impl::s_frk_state().m_observers )
1701 to->test_aborted( );
1702 }
1703
1704
1705 //____________________________________________________________________________//
1706
1707 } // namespace framework
1708 } // namespace unit_test
1709 } // namespace boost
1710
1711 #include <boost/test/detail/enable_warnings.hpp>
1712
1713 #endif // BOOST_TEST_FRAMEWORK_IPP_021005GER