File indexing completed on 2025-01-30 10:00:59
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #ifndef BOOST_TEST_FRAMEWORK_IPP_021005GER
0016 #define BOOST_TEST_FRAMEWORK_IPP_021005GER
0017
0018
0019 #include <boost/test/framework.hpp>
0020 #include <boost/test/execution_monitor.hpp>
0021 #include <boost/test/debug.hpp>
0022 #include <boost/test/unit_test_parameters.hpp>
0023
0024 #include <boost/test/unit_test_log.hpp>
0025 #include <boost/test/unit_test_log_formatter.hpp>
0026 #include <boost/test/unit_test_monitor.hpp>
0027 #include <boost/test/results_collector.hpp>
0028 #include <boost/test/progress_monitor.hpp>
0029 #include <boost/test/results_reporter.hpp>
0030 #include <boost/test/test_framework_init_observer.hpp>
0031
0032 #include <boost/test/tree/observer.hpp>
0033 #include <boost/test/tree/test_unit.hpp>
0034 #include <boost/test/tree/visitor.hpp>
0035 #include <boost/test/tree/traverse.hpp>
0036 #include <boost/test/tree/test_case_counter.hpp>
0037 #include <boost/test/tree/global_fixture.hpp>
0038
0039 #if BOOST_TEST_SUPPORT_TOKEN_ITERATOR
0040 #include <boost/test/utils/iterator/token_iterator.hpp>
0041 #endif
0042
0043 #include <boost/test/utils/foreach.hpp>
0044 #include <boost/test/utils/basic_cstring/io.hpp>
0045 #include <boost/test/utils/basic_cstring/compare.hpp>
0046
0047 #include <boost/test/detail/global_typedef.hpp>
0048 #include <boost/test/detail/throw_exception.hpp>
0049
0050
0051 #include <boost/test/utils/timer.hpp>
0052 #include <boost/bind/bind.hpp>
0053
0054
0055 #include <limits>
0056 #include <map>
0057 #include <set>
0058 #include <cstdlib>
0059 #include <ctime>
0060 #include <numeric>
0061 #include <cmath>
0062 #include <iterator>
0063
0064 #ifdef BOOST_NO_STDC_NAMESPACE
0065 namespace std { using ::time; using ::srand; }
0066 #endif
0067
0068 #include <boost/test/detail/suppress_warnings.hpp>
0069
0070
0071
0072 namespace boost {
0073 namespace unit_test {
0074 namespace framework {
0075
0076 namespace impl {
0077
0078
0079
0080
0081
0082 struct order_info {
0083 order_info() : depth(-1) {}
0084
0085 int depth;
0086 std::vector<test_unit_id> dependant_siblings;
0087 };
0088
0089 typedef std::set<test_unit_id> tu_id_set;
0090 typedef std::map<test_unit_id,order_info> order_info_per_tu;
0091
0092
0093
0094 static test_unit_id
0095 get_tu_parent( test_unit_id tu_id )
0096 {
0097 return framework::get( tu_id, TUT_ANY ).p_parent_id;
0098 }
0099
0100
0101
0102 static int
0103 tu_depth( test_unit_id tu_id, test_unit_id master_tu_id, order_info_per_tu& tuoi )
0104 {
0105 if( tu_id == master_tu_id )
0106 return 0;
0107
0108 order_info& info = tuoi[tu_id];
0109
0110 if( info.depth == -1 )
0111 info.depth = tu_depth( get_tu_parent( tu_id ), master_tu_id, tuoi ) + 1;
0112
0113 return info.depth;
0114 }
0115
0116
0117
0118 static void
0119 collect_dependant_siblings( test_unit_id from, test_unit_id to, test_unit_id master_tu_id, order_info_per_tu& tuoi )
0120 {
0121 int from_depth = tu_depth( from, master_tu_id, tuoi );
0122 int to_depth = tu_depth( to, master_tu_id, tuoi );
0123
0124 while(from_depth > to_depth) {
0125 from = get_tu_parent( from );
0126 --from_depth;
0127 }
0128
0129 while(from_depth < to_depth) {
0130 to = get_tu_parent( to );
0131 --to_depth;
0132 }
0133
0134 while(true) {
0135 test_unit_id from_parent = get_tu_parent( from );
0136 test_unit_id to_parent = get_tu_parent( to );
0137 if( from_parent == to_parent )
0138 break;
0139 from = from_parent;
0140 to = to_parent;
0141 }
0142
0143 tuoi[from].dependant_siblings.push_back( to );
0144 }
0145
0146
0147
0148 static counter_t
0149 assign_sibling_rank( test_unit_id tu_id, order_info_per_tu& tuoi )
0150 {
0151 test_unit& tu = framework::get( tu_id, TUT_ANY );
0152
0153 BOOST_TEST_SETUP_ASSERT( tu.p_sibling_rank != (std::numeric_limits<counter_t>::max)(),
0154 "Cyclic dependency detected involving test unit \"" + tu.full_name() + "\"" );
0155
0156 if( tu.p_sibling_rank != 0 )
0157 return tu.p_sibling_rank;
0158
0159 order_info const& info = tuoi[tu_id];
0160
0161
0162 tu.p_sibling_rank.value = (std::numeric_limits<counter_t>::max)();
0163
0164 counter_t new_rank = 1;
0165 BOOST_TEST_FOREACH( test_unit_id, sibling_id, info.dependant_siblings )
0166 new_rank = (std::max)(new_rank, assign_sibling_rank( sibling_id, tuoi ) + 1);
0167
0168 return tu.p_sibling_rank.value = new_rank;
0169 }
0170
0171
0172
0173
0174
0175
0176
0177 static void
0178 invoke_init_func( init_unit_test_func init_func )
0179 {
0180 #ifdef BOOST_TEST_ALTERNATIVE_INIT_API
0181 BOOST_TEST_I_ASSRT( (*init_func)(), std::runtime_error( "test module initialization failed" ) );
0182 #else
0183 test_suite* manual_test_units = (*init_func)( framework::master_test_suite().argc, framework::master_test_suite().argv );
0184
0185 if( manual_test_units )
0186 framework::master_test_suite().add( manual_test_units );
0187 #endif
0188 }
0189
0190
0191
0192
0193
0194 class name_filter : public test_tree_visitor {
0195 struct component {
0196 component( const_string name )
0197 {
0198 if( name == "*" )
0199 m_kind = SFK_ALL;
0200 else if( first_char( name ) == '*' && last_char( name ) == '*' ) {
0201 m_kind = SFK_SUBSTR;
0202 m_name = name.substr( 1, name.size()-1 );
0203 }
0204 else if( first_char( name ) == '*' ) {
0205 m_kind = SFK_TRAILING;
0206 m_name = name.substr( 1 );
0207 }
0208 else if( last_char( name ) == '*' ) {
0209 m_kind = SFK_LEADING;
0210 m_name = name.substr( 0, name.size()-1 );
0211 }
0212 else {
0213 m_kind = SFK_MATCH;
0214 m_name = name;
0215 }
0216 }
0217
0218 bool pass( test_unit const& tu ) const
0219 {
0220 const_string name( tu.p_name );
0221
0222 switch( m_kind ) {
0223 default:
0224 case SFK_ALL:
0225 return true;
0226 case SFK_LEADING:
0227 return name.substr( 0, m_name.size() ) == m_name;
0228 case SFK_TRAILING:
0229 return name.size() >= m_name.size() && name.substr( name.size() - m_name.size() ) == m_name;
0230 case SFK_SUBSTR:
0231 return name.find( m_name ) != const_string::npos;
0232 case SFK_MATCH:
0233 return m_name == tu.p_name.get();
0234 }
0235 }
0236 enum kind { SFK_ALL, SFK_LEADING, SFK_TRAILING, SFK_SUBSTR, SFK_MATCH };
0237
0238 kind m_kind;
0239 const_string m_name;
0240 };
0241
0242 public:
0243
0244 name_filter( test_unit_id_list& targ_list, const_string filter_expr ) : m_targ_list( targ_list ), m_depth( 0 )
0245 {
0246 #ifdef BOOST_TEST_SUPPORT_TOKEN_ITERATOR
0247 utils::string_token_iterator tit( filter_expr, (utils::dropped_delimeters = "/",
0248 utils::kept_delimeters = utils::dt_none) );
0249
0250 while( tit != utils::string_token_iterator() ) {
0251 m_components.push_back(
0252 std::vector<component>( utils::string_token_iterator( *tit, (utils::dropped_delimeters = ",",
0253 utils::kept_delimeters = utils::dt_none) ),
0254 utils::string_token_iterator() ) );
0255
0256 ++tit;
0257 }
0258 #endif
0259 }
0260
0261 private:
0262 bool filter_unit( test_unit const& tu )
0263 {
0264
0265 if( m_depth == 0 )
0266 return true;
0267
0268
0269 std::vector<component> const& filters = m_components[m_depth-1];
0270
0271
0272 using namespace boost::placeholders;
0273 return std::find_if( filters.begin(), filters.end(), bind( &component::pass, _1, boost::ref(tu) ) ) != filters.end();
0274 }
0275
0276
0277 void visit( test_case const& tc ) BOOST_OVERRIDE
0278 {
0279
0280 if( m_depth == m_components.size() && filter_unit( tc ) )
0281 m_targ_list.push_back( tc.p_id );
0282 }
0283 bool test_suite_start( test_suite const& ts ) BOOST_OVERRIDE
0284 {
0285 if( !filter_unit( ts ) )
0286 return false;
0287
0288 if( m_depth < m_components.size() ) {
0289 ++m_depth;
0290 return true;
0291 }
0292
0293 m_targ_list.push_back( ts.p_id );
0294
0295 return false;
0296 }
0297 void test_suite_finish( test_suite const& ) BOOST_OVERRIDE
0298 {
0299 --m_depth;
0300 }
0301
0302
0303 typedef std::vector<std::vector<component> > components_per_level;
0304
0305 components_per_level m_components;
0306 test_unit_id_list& m_targ_list;
0307 unsigned m_depth;
0308 };
0309
0310
0311
0312
0313
0314 class label_filter : public test_tree_visitor {
0315 public:
0316 label_filter( test_unit_id_list& targ_list, const_string label )
0317 : m_targ_list( targ_list )
0318 , m_label( label )
0319 {}
0320
0321 private:
0322
0323 bool visit( test_unit const& tu ) BOOST_OVERRIDE
0324 {
0325 if( tu.has_label( m_label ) ) {
0326
0327 m_targ_list.push_back( tu.p_id );
0328 return false;
0329 }
0330
0331 return true;
0332 }
0333
0334
0335 test_unit_id_list& m_targ_list;
0336 const_string m_label;
0337 };
0338
0339
0340
0341
0342
0343 class set_run_status : public test_tree_visitor {
0344 public:
0345 explicit set_run_status( test_unit::run_status rs, test_unit_id_list* dep_collector = 0 )
0346 : m_new_status( rs )
0347 , m_dep_collector( dep_collector )
0348 {}
0349
0350
0351 bool visit( test_unit const& tu ) BOOST_OVERRIDE
0352 {
0353 const_cast<test_unit&>(tu).p_run_status.value = m_new_status == test_unit::RS_INVALID ? tu.p_default_status : m_new_status;
0354 if( m_dep_collector ) {
0355 BOOST_TEST_FOREACH( test_unit_id, dep_id, tu.p_dependencies.get() ) {
0356 test_unit const& dep = framework::get( dep_id, TUT_ANY );
0357
0358 if( dep.p_run_status == tu.p_run_status )
0359 continue;
0360
0361 BOOST_TEST_FRAMEWORK_MESSAGE( "Including test " << dep.p_type_name << ' ' << dep.full_name() <<
0362 " as a dependency of test " << tu.p_type_name << ' ' << tu.full_name() );
0363
0364 m_dep_collector->push_back( dep_id );
0365 }
0366 }
0367 return true;
0368 }
0369
0370 private:
0371
0372 test_unit::run_status m_new_status;
0373 test_unit_id_list* m_dep_collector;
0374 };
0375
0376
0377
0378
0379
0380 static void
0381 add_filtered_test_units( test_unit_id master_tu_id, const_string filter, test_unit_id_list& targ )
0382 {
0383
0384 if( filter[0] == '@' ) {
0385 filter.trim_left( 1 );
0386 label_filter lf( targ, filter );
0387 traverse_test_tree( master_tu_id, lf, true );
0388 }
0389 else {
0390 name_filter nf( targ, filter );
0391 traverse_test_tree( master_tu_id, nf, true );
0392 }
0393 }
0394
0395
0396
0397 static bool
0398 parse_filters( test_unit_id master_tu_id, test_unit_id_list& tu_to_enable, test_unit_id_list& tu_to_disable )
0399 {
0400
0401 bool had_selector_filter = false;
0402
0403 std::vector<std::string> const& filters = runtime_config::get<std::vector<std::string> >( runtime_config::btrt_run_filters );
0404
0405 BOOST_TEST_FOREACH( const_string, filter, filters ) {
0406 BOOST_TEST_SETUP_ASSERT( !filter.is_empty(), "Invalid filter specification" );
0407
0408
0409 utils::string_token_iterator t_filter_it( filter, (utils::dropped_delimeters = ":",
0410 utils::kept_delimeters = utils::dt_none) );
0411
0412 while( t_filter_it != utils::string_token_iterator() ) {
0413 const_string filter_token = *t_filter_it;
0414
0415 enum { SELECTOR, ENABLER, DISABLER } filter_type = SELECTOR;
0416
0417
0418 if( filter_token[0] == '!' || filter_token[0] == '+' ) {
0419 filter_type = filter_token[0] == '+' ? ENABLER : DISABLER;
0420 filter_token.trim_left( 1 );
0421 BOOST_TEST_SETUP_ASSERT( !filter_token.is_empty(), "Invalid filter specification" );
0422 }
0423
0424 had_selector_filter |= filter_type == SELECTOR;
0425
0426
0427 switch( filter_type ) {
0428 case SELECTOR:
0429 case ENABLER: add_filtered_test_units( master_tu_id, filter_token, tu_to_enable ); break;
0430 case DISABLER: add_filtered_test_units( master_tu_id, filter_token, tu_to_disable ); break;
0431 }
0432
0433 ++t_filter_it;
0434 }
0435 }
0436
0437 return had_selector_filter;
0438 }
0439
0440
0441
0442
0443 template< class RandomIt, class RandomFunc >
0444 void random_shuffle( RandomIt first, RandomIt last, RandomFunc &r )
0445 {
0446 typedef typename std::iterator_traits<RandomIt>::difference_type difference_type;
0447 difference_type n = last - first;
0448 for (difference_type i = n-1; i > 0; --i) {
0449 difference_type j = r(i+1);
0450 if (j != i) {
0451 using std::swap;
0452 swap(first[i], first[j]);
0453 }
0454 }
0455 }
0456
0457
0458
0459
0460 class global_fixture_handle : public test_unit_fixture {
0461 public:
0462 global_fixture_handle(test_unit_fixture* fixture) : m_global_fixture(fixture) {}
0463 ~global_fixture_handle() BOOST_OVERRIDE {}
0464
0465 void setup() BOOST_OVERRIDE {
0466 m_global_fixture->setup();
0467 }
0468 void teardown() BOOST_OVERRIDE {
0469 m_global_fixture->teardown();
0470 }
0471
0472 private:
0473 test_unit_fixture* m_global_fixture;
0474 };
0475
0476
0477 }
0478
0479
0480
0481
0482
0483 unsigned long int const TIMEOUT_EXCEEDED = static_cast<unsigned long int>( -1 );
0484
0485 class state {
0486 public:
0487 state()
0488 : m_master_test_suite( 0 )
0489 , m_curr_test_unit( INV_TEST_UNIT_ID )
0490 , m_next_test_case_id( MIN_TEST_CASE_ID )
0491 , m_next_test_suite_id( MIN_TEST_SUITE_ID )
0492 , m_test_in_progress( false )
0493 , m_context_idx( 0 )
0494 , m_log_sinks( )
0495 , m_report_sink( std::cerr )
0496 {
0497 }
0498
0499 ~state() { clear(); }
0500
0501 void clear()
0502 {
0503 while( !m_test_units.empty() ) {
0504 test_unit_store::value_type const& tu = *m_test_units.begin();
0505 test_unit const* tu_ptr = tu.second;
0506
0507
0508 if( ut_detail::test_id_2_unit_type( tu.second->p_id ) == TUT_SUITE )
0509 delete static_cast<test_suite const*>(tu_ptr);
0510 else
0511 delete static_cast<test_case const*>(tu_ptr);
0512 }
0513 }
0514
0515 void set_tu_id( test_unit& tu, test_unit_id id ) { tu.p_id.value = id; }
0516
0517
0518
0519
0520 void deduce_siblings_order( test_unit_id tu_id, test_unit_id master_tu_id, impl::order_info_per_tu& tuoi )
0521 {
0522 test_unit& tu = framework::get( tu_id, TUT_ANY );
0523
0524
0525 BOOST_TEST_FOREACH( test_unit_id, dep_id, tu.p_dependencies.get() )
0526 collect_dependant_siblings( tu_id, dep_id, master_tu_id, tuoi );
0527
0528 if( tu.p_type != TUT_SUITE )
0529 return;
0530
0531 test_suite& ts = static_cast<test_suite&>(tu);
0532
0533
0534 BOOST_TEST_FOREACH( test_unit_id, chld_id, ts.m_children )
0535 deduce_siblings_order( chld_id, master_tu_id, tuoi );
0536
0537 ts.m_ranked_children.clear();
0538 BOOST_TEST_FOREACH( test_unit_id, chld_id, ts.m_children ) {
0539 counter_t rank = assign_sibling_rank( chld_id, tuoi );
0540 ts.m_ranked_children.insert( std::make_pair( rank, chld_id ) );
0541 }
0542 }
0543
0544
0545
0546
0547
0548
0549 bool finalize_default_run_status( test_unit_id tu_id, test_unit::run_status parent_status )
0550 {
0551 test_unit& tu = framework::get( tu_id, TUT_ANY );
0552
0553 if( tu.p_default_status == test_suite::RS_INHERIT )
0554 tu.p_default_status.value = parent_status;
0555
0556
0557 if( tu.p_type == TUT_SUITE ) {
0558 bool has_enabled_child = false;
0559 BOOST_TEST_FOREACH( test_unit_id, chld_id, static_cast<test_suite const&>(tu).m_children )
0560 has_enabled_child |= finalize_default_run_status( chld_id, tu.p_default_status );
0561
0562 tu.p_default_status.value = has_enabled_child ? test_suite::RS_ENABLED : test_suite::RS_DISABLED;
0563 }
0564
0565 return tu.p_default_status == test_suite::RS_ENABLED;
0566 }
0567
0568
0569
0570 bool finalize_run_status( test_unit_id tu_id )
0571 {
0572 test_unit& tu = framework::get( tu_id, TUT_ANY );
0573
0574
0575 if( tu.p_type == TUT_SUITE ) {
0576 bool has_enabled_child = false;
0577 BOOST_TEST_FOREACH( test_unit_id, chld_id, static_cast<test_suite const&>(tu).m_children)
0578 has_enabled_child |= finalize_run_status( chld_id );
0579
0580 tu.p_run_status.value = has_enabled_child ? test_suite::RS_ENABLED : test_suite::RS_DISABLED;
0581 }
0582
0583 return tu.is_enabled();
0584 }
0585
0586
0587
0588 void deduce_run_status( test_unit_id master_tu_id )
0589 {
0590 using namespace framework::impl;
0591 test_unit_id_list tu_to_enable;
0592 test_unit_id_list tu_to_disable;
0593
0594
0595 bool had_selector_filter = !runtime_config::get<std::vector<std::string> >( runtime_config::btrt_run_filters ).empty() &&
0596 parse_filters( master_tu_id, tu_to_enable, tu_to_disable );
0597
0598
0599 set_run_status initial_setter( had_selector_filter ? test_unit::RS_DISABLED : test_unit::RS_INVALID );
0600 traverse_test_tree( master_tu_id, initial_setter, true );
0601
0602
0603 while( !tu_to_enable.empty() ) {
0604 test_unit& tu = framework::get( tu_to_enable.back(), TUT_ANY );
0605
0606 tu_to_enable.pop_back();
0607
0608
0609 if( tu.is_enabled() )
0610 continue;
0611
0612
0613 set_run_status enabler( test_unit::RS_ENABLED, &tu_to_enable );
0614 traverse_test_tree( tu.p_id, enabler, true );
0615
0616
0617 test_unit_id parent_id = tu.p_parent_id;
0618 while( parent_id != INV_TEST_UNIT_ID
0619 && parent_id != master_tu_id )
0620 {
0621
0622
0623
0624
0625 test_unit& tu_parent = framework::get( parent_id, TUT_ANY );
0626 enabler.visit( tu_parent );
0627 parent_id = tu_parent.p_parent_id;
0628 }
0629 }
0630
0631
0632 while( !tu_to_disable.empty() ) {
0633 test_unit const& tu = framework::get( tu_to_disable.back(), TUT_ANY );
0634
0635 tu_to_disable.pop_back();
0636
0637
0638 if( !tu.is_enabled() )
0639 continue;
0640
0641 set_run_status disabler( test_unit::RS_DISABLED );
0642 traverse_test_tree( tu.p_id, disabler, true );
0643 }
0644
0645
0646 finalize_run_status( master_tu_id );
0647 }
0648
0649
0650
0651 typedef unit_test_monitor_t::error_level execution_result;
0652
0653
0654 struct random_generator_helper {
0655 size_t operator()(size_t i) const {
0656 return std::rand() % i;
0657 }
0658 };
0659
0660
0661 execution_result execute_test_tree( test_unit_id tu_id,
0662 unsigned long int timeout_microseconds = 0,
0663 random_generator_helper const * const p_random_generator = 0)
0664 {
0665 test_unit const& tu = framework::get( tu_id, TUT_ANY );
0666
0667 execution_result result = unit_test_monitor_t::test_ok;
0668
0669 if( !tu.is_enabled() ) {
0670 BOOST_TEST_FOREACH( test_observer*, to, m_observers )
0671 to->test_unit_skipped( tu, "disabled" );
0672 return result;
0673 }
0674
0675
0676
0677 if( timeout_microseconds == TIMEOUT_EXCEEDED ) {
0678
0679 BOOST_TEST_FOREACH( test_observer*, to, m_observers )
0680 to->test_unit_skipped( tu, "timeout for the test unit is exceeded" );
0681
0682 return unit_test_monitor_t::os_timeout;
0683 }
0684 else if( timeout_microseconds == 0 || (tu.p_timeout > 0 && timeout_microseconds > (tu.p_timeout * 1000000) ) )
0685 timeout_microseconds = tu.p_timeout * 1000000;
0686
0687
0688 test_tools::assertion_result const precondition_res = tu.check_preconditions();
0689 if( !precondition_res ) {
0690
0691 BOOST_TEST_FOREACH( test_observer*, to, m_observers )
0692 to->test_unit_skipped( tu, precondition_res.message() );
0693
0694
0695
0696
0697 return unit_test_monitor_t::test_ok;
0698 }
0699
0700
0701 BOOST_TEST_FOREACH( test_observer*, to, m_observers )
0702 to->test_unit_start( tu );
0703
0704
0705 BOOST_TEST_FOREACH( test_unit_fixture_ptr, F, tu.p_fixtures.get() ) {
0706 ut_detail::test_unit_id_restore restore_current_test_unit(m_curr_test_unit, tu.p_id);
0707 result = unit_test_monitor.execute_and_translate( boost::bind( &test_unit_fixture::setup, F ) );
0708 if( result != unit_test_monitor_t::test_ok )
0709 break;
0710 test_results const& test_rslt = unit_test::results_collector.results( m_curr_test_unit );
0711 if( test_rslt.aborted() ) {
0712 result = unit_test_monitor_t::test_setup_failure;
0713 break;
0714 }
0715 }
0716
0717
0718
0719 unsigned long elapsed_microseconds = 0;
0720
0721 if( result == unit_test_monitor_t::test_ok ) {
0722
0723 boost::unit_test::timer::timer tu_timer;
0724
0725
0726 const random_generator_helper& rand_gen = p_random_generator ? *p_random_generator : random_generator_helper();
0727
0728 if( tu.p_type == TUT_SUITE ) {
0729 test_suite const& ts = static_cast<test_suite const&>( tu );
0730
0731 if( runtime_config::get<unsigned>( runtime_config::btrt_random_seed ) == 0 ) {
0732 typedef std::pair<counter_t,test_unit_id> value_type;
0733
0734 BOOST_TEST_FOREACH( value_type, chld, ts.m_ranked_children ) {
0735
0736 unsigned long int chld_timeout = child_timeout(
0737 timeout_microseconds,
0738 static_cast<unsigned long int>( microsecond_wall_time(tu_timer.elapsed()) ));
0739
0740 result = (std::min)( result, execute_test_tree( chld.second, chld_timeout, &rand_gen ) );
0741
0742 if( unit_test_monitor.is_critical_error( result ) )
0743 break;
0744
0745
0746 elapsed_microseconds = static_cast<unsigned long int>( microsecond_wall_time(tu_timer.elapsed()) );
0747
0748 if( (timeout_microseconds > 0) && (elapsed_microseconds > timeout_microseconds) && (timeout_microseconds != TIMEOUT_EXCEEDED ) ) {
0749 BOOST_TEST_FOREACH( test_observer*, to, m_observers ) {
0750 to->test_unit_timed_out(tu);
0751 }
0752 result = (std::min)( result, unit_test_monitor_t::os_timeout );
0753 timeout_microseconds = TIMEOUT_EXCEEDED;
0754
0755
0756
0757 }
0758 }
0759 }
0760 else {
0761
0762
0763 test_unit_id_list children_with_the_same_rank;
0764
0765 typedef test_suite::children_per_rank::const_iterator it_type;
0766 it_type it = ts.m_ranked_children.begin();
0767 while( it != ts.m_ranked_children.end() ) {
0768 children_with_the_same_rank.clear();
0769
0770 std::pair<it_type,it_type> range = ts.m_ranked_children.equal_range( it->first );
0771 it = range.first;
0772 while( it != range.second ) {
0773 children_with_the_same_rank.push_back( it->second );
0774 it++;
0775 }
0776
0777 impl::random_shuffle( children_with_the_same_rank.begin(), children_with_the_same_rank.end(), rand_gen );
0778
0779 BOOST_TEST_FOREACH( test_unit_id, chld, children_with_the_same_rank ) {
0780 unsigned long int chld_timeout = child_timeout(
0781 timeout_microseconds,
0782 static_cast<unsigned long int>(microsecond_wall_time(tu_timer.elapsed())) );
0783
0784 result = (std::min)( result, execute_test_tree( chld, chld_timeout, &rand_gen ) );
0785
0786 if( unit_test_monitor.is_critical_error( result ) )
0787 break;
0788
0789
0790 elapsed_microseconds = static_cast<unsigned long int>( microsecond_wall_time(tu_timer.elapsed()) );
0791 if( (timeout_microseconds > 0) && (elapsed_microseconds > timeout_microseconds) && (timeout_microseconds != TIMEOUT_EXCEEDED ) ) {
0792 BOOST_TEST_FOREACH( test_observer*, to, m_observers ) {
0793 to->test_unit_timed_out(tu);
0794 }
0795 result = (std::min)( result, unit_test_monitor_t::os_timeout );
0796 timeout_microseconds = TIMEOUT_EXCEEDED;
0797
0798
0799
0800 }
0801 }
0802 }
0803 }
0804 }
0805 else {
0806 test_case const& tc = static_cast<test_case const&>( tu );
0807
0808
0809 m_context_idx = 0;
0810
0811
0812 ut_detail::test_unit_id_restore restore_current_test_unit(m_curr_test_unit, tc.p_id);
0813
0814
0815 result = unit_test_monitor.execute_and_translate( tc.p_test_func, timeout_microseconds );
0816 elapsed_microseconds = static_cast<unsigned long int>( microsecond_wall_time(tu_timer.elapsed()) );
0817
0818
0819 m_context.clear();
0820
0821
0822 }
0823 }
0824
0825
0826 if( !unit_test_monitor.is_critical_error( result ) ) {
0827
0828 BOOST_TEST_REVERSE_FOREACH( test_unit_fixture_ptr, F, tu.p_fixtures.get() ) {
0829 ut_detail::test_unit_id_restore restore_current_test_unit(m_curr_test_unit, tu.p_id);
0830 result = (std::min)( result, unit_test_monitor.execute_and_translate( boost::bind( &test_unit_fixture::teardown, F ), 0 ) );
0831
0832 if( unit_test_monitor.is_critical_error( result ) )
0833 break;
0834 }
0835 }
0836
0837
0838 if( unit_test_monitor.is_critical_error( result ) ) {
0839 BOOST_TEST_FOREACH( test_observer*, to, m_observers )
0840 to->test_aborted();
0841 }
0842
0843
0844 BOOST_TEST_REVERSE_FOREACH( test_observer*, to, m_observers )
0845 to->test_unit_finish( tu, elapsed_microseconds );
0846
0847 return result;
0848 }
0849
0850
0851
0852 unsigned long int child_timeout( unsigned long tu_timeout_microseconds, unsigned long elpsed_microsec )
0853 {
0854 if( tu_timeout_microseconds == 0UL || tu_timeout_microseconds == TIMEOUT_EXCEEDED)
0855 return tu_timeout_microseconds;
0856
0857 return tu_timeout_microseconds > elpsed_microsec ?
0858 tu_timeout_microseconds - elpsed_microsec
0859 : TIMEOUT_EXCEEDED;
0860 }
0861
0862 struct priority_order {
0863 bool operator()( test_observer* lhs, test_observer* rhs ) const
0864 {
0865 return (lhs->priority() < rhs->priority()) ||
0866 ((lhs->priority() == rhs->priority()) && std::less<test_observer*>()(lhs, rhs));
0867 }
0868 };
0869
0870
0871 typedef std::map<test_unit_id,test_unit*> test_unit_store;
0872 typedef std::set<test_observer*,priority_order> observer_store;
0873 struct context_frame {
0874 context_frame( std::string const& d, int id, bool sticky )
0875 : descr( d )
0876 , frame_id( id )
0877 , is_sticky( sticky )
0878 {}
0879
0880 std::string descr;
0881 int frame_id;
0882 bool is_sticky;
0883 };
0884 typedef std::vector<context_frame> context_data;
0885
0886 master_test_suite_t* m_master_test_suite;
0887 std::vector<test_suite*> m_auto_test_suites;
0888
0889 test_unit_id m_curr_test_unit;
0890 test_unit_store m_test_units;
0891
0892 test_unit_id m_next_test_case_id;
0893 test_unit_id m_next_test_suite_id;
0894
0895 bool m_test_in_progress;
0896
0897 observer_store m_observers;
0898 context_data m_context;
0899 int m_context_idx;
0900
0901 std::set<global_fixture*> m_global_fixtures;
0902
0903 boost::execution_monitor m_aux_em;
0904
0905 std::map<output_format, runtime_config::stream_holder> m_log_sinks;
0906 runtime_config::stream_holder m_report_sink;
0907 };
0908
0909
0910
0911 namespace impl {
0912 namespace {
0913
0914 #if defined(__CYGWIN__)
0915 framework::state& s_frk_state() { static framework::state* the_inst = 0; if(!the_inst) the_inst = new framework::state; return *the_inst; }
0916 #else
0917 framework::state& s_frk_state() { static framework::state the_inst; return the_inst; }
0918 #endif
0919
0920 }
0921
0922 void
0923 setup_for_execution( test_unit const& tu )
0924 {
0925 s_frk_state().deduce_run_status( tu.p_id );
0926 }
0927
0928 struct sum_to_first_only {
0929 sum_to_first_only() : is_first(true) {}
0930 template <class T, class U>
0931 T operator()(T const& l_, U const& r_) {
0932 if(is_first) {
0933 is_first = false;
0934 return l_ + r_.first;
0935 }
0936 return l_ + ", " + r_.first;
0937 }
0938
0939 bool is_first;
0940 };
0941
0942 void
0943 shutdown_loggers_and_reports()
0944 {
0945 s_frk_state().m_log_sinks.clear();
0946 s_frk_state().m_report_sink.setup( "stderr" );
0947 }
0948
0949 void
0950 unregister_global_fixture_and_configuration()
0951 {
0952
0953 std::set<global_fixture*> gfixture_copy(s_frk_state().m_global_fixtures);
0954 BOOST_TEST_FOREACH( global_fixture*, tuf, gfixture_copy ) {
0955 tuf->unregister_from_framework();
0956 }
0957 s_frk_state().m_global_fixtures.clear();
0958
0959 state::observer_store gobserver_copy(s_frk_state().m_observers);
0960 BOOST_TEST_FOREACH( test_observer*, to, gobserver_copy ) {
0961 framework::deregister_observer( *to );
0962 }
0963 s_frk_state().m_observers.clear();
0964 }
0965
0966 void
0967 setup_loggers()
0968 {
0969
0970 BOOST_TEST_I_TRY {
0971
0972 #ifdef BOOST_TEST_SUPPORT_TOKEN_ITERATOR
0973 bool has_combined_logger = runtime_config::has( runtime_config::btrt_combined_logger )
0974 && !runtime_config::get< std::vector<std::string> >( runtime_config::btrt_combined_logger ).empty();
0975 #else
0976 bool has_combined_logger = false;
0977 #endif
0978
0979 if( !has_combined_logger ) {
0980 unit_test_log.set_threshold_level( runtime_config::get<log_level>( runtime_config::btrt_log_level ) );
0981 const output_format format = runtime_config::get<output_format>( runtime_config::btrt_log_format );
0982 unit_test_log.set_format( format );
0983
0984 runtime_config::stream_holder& stream_logger = s_frk_state().m_log_sinks[format];
0985 if( runtime_config::has( runtime_config::btrt_log_sink ) ) {
0986
0987 boost::function< void () > log_cleaner = boost::bind( &unit_test_log_t::set_stream,
0988 &unit_test_log,
0989 boost::ref(std::cout)
0990 );
0991 stream_logger.setup( runtime_config::get<std::string>( runtime_config::btrt_log_sink ),
0992 log_cleaner );
0993 }
0994 unit_test_log.set_stream( stream_logger.ref() );
0995 unit_test_log.configure();
0996 }
0997 else
0998 {
0999
1000 const std::vector<std::string>& v_output_format = runtime_config::get< std::vector<std::string> >( runtime_config::btrt_combined_logger ) ;
1001
1002 static const std::pair<const char*, log_level> all_log_levels[] = {
1003 std::make_pair( "all" , log_successful_tests ),
1004 std::make_pair( "success" , log_successful_tests ),
1005 std::make_pair( "test_suite" , log_test_units ),
1006 std::make_pair( "unit_scope" , log_test_units ),
1007 std::make_pair( "message" , log_messages ),
1008 std::make_pair( "warning" , log_warnings ),
1009 std::make_pair( "error" , log_all_errors ),
1010 std::make_pair( "cpp_exception" , log_cpp_exception_errors ),
1011 std::make_pair( "system_error" , log_system_errors ),
1012 std::make_pair( "fatal_error" , log_fatal_errors ),
1013 std::make_pair( "nothing" , log_nothing )
1014 };
1015
1016 static const std::pair<const char*, output_format> all_formats[] = {
1017 std::make_pair( "HRF" , OF_CLF ),
1018 std::make_pair( "CLF" , OF_CLF ),
1019 std::make_pair( "XML" , OF_XML ),
1020 std::make_pair( "JUNIT", OF_JUNIT )
1021 };
1022
1023
1024 bool is_first = true;
1025
1026 BOOST_TEST_FOREACH( const_string, current_multi_config, v_output_format ) {
1027
1028 #ifdef BOOST_TEST_SUPPORT_TOKEN_ITERATOR
1029
1030
1031
1032 std::vector<std::string> v_processed_tokens;
1033
1034 {
1035 utils::string_token_iterator current_config( current_multi_config, (utils::dropped_delimeters = ":",
1036 utils::kept_delimeters = utils::dt_none) );
1037
1038 for( ; current_config != utils::string_token_iterator() ; ++current_config) {
1039 std::string str_copy(current_config->begin(), current_config->end());
1040 if( ( str_copy[0] == '\\' || str_copy[0] == '/' )
1041 && v_processed_tokens.size() > 0) {
1042 v_processed_tokens.back() += ":" + str_copy;
1043 }
1044 else {
1045 v_processed_tokens.push_back(str_copy);
1046 }
1047 }
1048 }
1049
1050 BOOST_TEST_FOREACH( std::string const&, current_config, v_processed_tokens ) {
1051
1052 utils::string_token_iterator current_format_specs( current_config, (utils::keep_empty_tokens,
1053 utils::dropped_delimeters = ",",
1054 utils::kept_delimeters = utils::dt_none) );
1055
1056 output_format format = OF_INVALID ;
1057 if( current_format_specs != utils::string_token_iterator() &&
1058 current_format_specs->size() ) {
1059
1060 for(size_t elem=0; elem < sizeof(all_formats)/sizeof(all_formats[0]); elem++) {
1061 if(const_string(all_formats[elem].first) == *current_format_specs) {
1062 format = all_formats[elem].second;
1063 break;
1064 }
1065 }
1066 }
1067
1068 BOOST_TEST_I_ASSRT( format != OF_INVALID,
1069 boost::runtime::access_to_missing_argument()
1070 << "Unable to determine the logger type from '"
1071 << current_config
1072 << "'. Possible choices are: "
1073 << std::accumulate(all_formats,
1074 all_formats + sizeof(all_formats)/sizeof(all_formats[0]),
1075 std::string(""),
1076 sum_to_first_only())
1077 );
1078
1079
1080 if( is_first ) {
1081 unit_test_log.set_format( format );
1082 }
1083 else {
1084 unit_test_log.add_format( format );
1085 }
1086 is_first = false;
1087
1088 unit_test_log_formatter * const formatter = unit_test_log.get_formatter(format);
1089 BOOST_TEST_SETUP_ASSERT( formatter, "Logger setup error" );
1090
1091 log_level formatter_log_level = invalid_log_level;
1092 ++current_format_specs ;
1093 if( !current_format_specs->size() ) {
1094 formatter_log_level = formatter->get_log_level();
1095 }
1096 else if( current_format_specs != utils::string_token_iterator() ) {
1097
1098 for(size_t elem=0; elem < sizeof(all_log_levels)/sizeof(all_log_levels[0]); elem++) {
1099 if(const_string(all_log_levels[elem].first) == *current_format_specs) {
1100 formatter_log_level = all_log_levels[elem].second;
1101 break;
1102 }
1103 }
1104 }
1105
1106 BOOST_TEST_I_ASSRT( formatter_log_level != invalid_log_level,
1107 boost::runtime::access_to_missing_argument()
1108 << "Unable to determine the log level from '"
1109 << current_config
1110 << "'. Possible choices are: "
1111 << std::accumulate(all_log_levels,
1112 all_log_levels + sizeof(all_log_levels)/sizeof(all_log_levels[0]),
1113 std::string(""),
1114 sum_to_first_only())
1115 );
1116
1117 unit_test_log.set_threshold_level( format, formatter_log_level );
1118
1119 runtime_config::stream_holder& stream_logger = s_frk_state().m_log_sinks[format];
1120 boost::function< void () > log_cleaner = boost::bind( &unit_test_log_t::set_stream,
1121 &unit_test_log,
1122 format,
1123 boost::ref(std::cout) );
1124 if( ++current_format_specs != utils::string_token_iterator() &&
1125 current_format_specs->size() ) {
1126 stream_logger.setup( *current_format_specs,
1127 log_cleaner );
1128 }
1129 else {
1130 stream_logger.setup( formatter->get_default_stream_description(),
1131 log_cleaner );
1132 }
1133 unit_test_log.set_stream( format, stream_logger.ref() );
1134 }
1135 #endif
1136 }
1137
1138 }
1139 }
1140 BOOST_TEST_I_CATCH( boost::runtime::init_error, ex ) {
1141 BOOST_TEST_SETUP_ASSERT( false, ex.msg );
1142 }
1143 BOOST_TEST_I_CATCH( boost::runtime::input_error, ex ) {
1144 std::cerr << ex.msg << "\n\n";
1145
1146 BOOST_TEST_I_THROW( framework::nothing_to_test( boost::exit_exception_failure ) );
1147 }
1148
1149
1150 }
1151
1152
1153
1154 }
1155
1156
1157
1158
1159
1160
1161
1162 void
1163 init( init_unit_test_func init_func, int argc, char* argv[] )
1164 {
1165 using namespace impl;
1166
1167
1168 runtime_config::init( argc, argv );
1169
1170
1171 impl::setup_loggers();
1172
1173
1174 results_reporter::set_level( runtime_config::get<report_level>( runtime_config::btrt_report_level ) );
1175 results_reporter::set_format( runtime_config::get<output_format>( runtime_config::btrt_report_format ) );
1176
1177 if( runtime_config::has( runtime_config::btrt_report_sink ) ) {
1178 boost::function< void () > report_cleaner = boost::bind( &results_reporter::set_stream,
1179 boost::ref(std::cerr)
1180 );
1181 s_frk_state().m_report_sink.setup( runtime_config::get<std::string>( runtime_config::btrt_report_sink ),
1182 report_cleaner );
1183 }
1184
1185 results_reporter::set_stream( s_frk_state().m_report_sink.ref() );
1186
1187
1188 register_observer( results_collector );
1189 register_observer( unit_test_log );
1190
1191 if( runtime_config::get<bool>( runtime_config::btrt_show_progress ) ) {
1192 progress_monitor.set_stream( std::cout );
1193 register_observer( progress_monitor );
1194 }
1195
1196
1197 unsigned long detect_mem_leak = runtime_config::get<unsigned long>( runtime_config::btrt_detect_mem_leaks );
1198 if( detect_mem_leak > 0 ) {
1199 debug::detect_memory_leaks( true, runtime_config::get<std::string>( runtime_config::btrt_report_mem_leaks ) );
1200 debug::break_memory_alloc( (long)detect_mem_leak );
1201 }
1202
1203
1204 master_test_suite().argc = argc;
1205 master_test_suite().argv = argv;
1206
1207
1208 BOOST_TEST_I_TRY {
1209 s_frk_state().m_aux_em.vexecute( boost::bind( &impl::invoke_init_func, init_func ) );
1210 }
1211 BOOST_TEST_I_CATCH( execution_exception, ex ) {
1212 BOOST_TEST_SETUP_ASSERT( false, ex.what() );
1213 }
1214 }
1215
1216
1217
1218 void
1219 finalize_setup_phase( test_unit_id master_tu_id )
1220 {
1221 if( master_tu_id == INV_TEST_UNIT_ID )
1222 master_tu_id = master_test_suite().p_id;
1223
1224
1225
1226 class apply_decorators : public test_tree_visitor {
1227 private:
1228
1229
1230 bool test_suite_start( test_suite const& ts) BOOST_OVERRIDE
1231 {
1232 const_cast<test_suite&>(ts).generate();
1233 const_cast<test_suite&>(ts).check_for_duplicate_test_cases();
1234 return test_tree_visitor::test_suite_start(ts);
1235 }
1236
1237 bool visit( test_unit const& tu ) BOOST_OVERRIDE
1238 {
1239 BOOST_TEST_FOREACH( decorator::base_ptr, d, tu.p_decorators.get() )
1240 d->apply( const_cast<test_unit&>(tu) );
1241
1242 return true;
1243 }
1244 } ad;
1245 traverse_test_tree( master_tu_id, ad, true );
1246
1247
1248 impl::order_info_per_tu tuoi;
1249 impl::s_frk_state().deduce_siblings_order( master_tu_id, master_tu_id, tuoi );
1250 impl::s_frk_state().finalize_default_run_status( master_tu_id, test_unit::RS_INVALID );
1251 }
1252
1253
1254
1255
1256
1257 bool
1258 test_in_progress()
1259 {
1260 return impl::s_frk_state().m_test_in_progress;
1261 }
1262
1263
1264
1265
1266
1267
1268
1269 void
1270 shutdown()
1271 {
1272
1273
1274 impl::shutdown_loggers_and_reports();
1275
1276
1277 impl::unregister_global_fixture_and_configuration();
1278
1279
1280
1281
1282 # if BOOST_WORKAROUND(BOOST_MSVC, <= 1600 ) && !defined(_DLL) && defined(_DEBUG)
1283 # if BOOST_WORKAROUND(BOOST_MSVC, < 1600 )
1284 #define _Next next
1285 #define _MemPtr memPtr
1286 #endif
1287 __type_info_node* pNode = __type_info_root_node._Next;
1288 __type_info_node* tmpNode = &__type_info_root_node;
1289
1290 for( ; pNode!=NULL; pNode = tmpNode ) {
1291 tmpNode = pNode->_Next;
1292 delete pNode->_MemPtr;
1293 delete pNode;
1294 }
1295 # if BOOST_WORKAROUND(BOOST_MSVC, < 1600 )
1296 #undef _Next
1297 #undef _MemPtr
1298 #endif
1299 # endif
1300 }
1301
1302
1303
1304
1305
1306
1307
1308 void
1309 register_test_unit( test_case* tc )
1310 {
1311 BOOST_TEST_SETUP_ASSERT( tc->p_id == INV_TEST_UNIT_ID, BOOST_TEST_L( "test case already registered" ) );
1312
1313 test_unit_id new_id = impl::s_frk_state().m_next_test_case_id;
1314
1315 BOOST_TEST_SETUP_ASSERT( new_id != MAX_TEST_CASE_ID, BOOST_TEST_L( "too many test cases" ) );
1316
1317 typedef state::test_unit_store::value_type map_value_type;
1318
1319 impl::s_frk_state().m_test_units.insert( map_value_type( new_id, tc ) );
1320 impl::s_frk_state().m_next_test_case_id++;
1321
1322 impl::s_frk_state().set_tu_id( *tc, new_id );
1323 }
1324
1325
1326
1327
1328
1329
1330
1331 void
1332 register_test_unit( test_suite* ts )
1333 {
1334 BOOST_TEST_SETUP_ASSERT( ts->p_id == INV_TEST_UNIT_ID, BOOST_TEST_L( "test suite already registered" ) );
1335
1336 test_unit_id new_id = impl::s_frk_state().m_next_test_suite_id;
1337
1338 BOOST_TEST_SETUP_ASSERT( new_id != MAX_TEST_SUITE_ID, BOOST_TEST_L( "too many test suites" ) );
1339
1340 typedef state::test_unit_store::value_type map_value_type;
1341
1342 impl::s_frk_state().m_test_units.insert( map_value_type( new_id, ts ) );
1343 impl::s_frk_state().m_next_test_suite_id++;
1344
1345 impl::s_frk_state().set_tu_id( *ts, new_id );
1346 }
1347
1348
1349
1350
1351
1352
1353
1354 void
1355 deregister_test_unit( test_unit* tu )
1356 {
1357 impl::s_frk_state().m_test_units.erase( tu->p_id );
1358 }
1359
1360
1361
1362
1363
1364
1365
1366 void
1367 clear()
1368 {
1369 impl::s_frk_state().clear();
1370 }
1371
1372
1373
1374
1375
1376
1377
1378 void
1379 register_observer( test_observer& to )
1380 {
1381 impl::s_frk_state().m_observers.insert( &to );
1382 }
1383
1384
1385
1386
1387
1388
1389
1390 void
1391 deregister_observer( test_observer& to )
1392 {
1393 impl::s_frk_state().m_observers.erase( &to );
1394 }
1395
1396
1397
1398
1399
1400
1401
1402 void
1403 register_global_fixture( global_fixture& tuf )
1404 {
1405 impl::s_frk_state().m_global_fixtures.insert( &tuf );
1406 }
1407
1408
1409
1410
1411
1412
1413
1414 void
1415 deregister_global_fixture( global_fixture &tuf )
1416 {
1417 impl::s_frk_state().m_global_fixtures.erase( &tuf );
1418 }
1419
1420
1421
1422
1423
1424
1425
1426 int
1427 add_context( ::boost::unit_test::lazy_ostream const& context_descr, bool sticky )
1428 {
1429 std::stringstream buffer;
1430 context_descr( buffer );
1431 int res_idx = impl::s_frk_state().m_context_idx++;
1432
1433 impl::s_frk_state().m_context.push_back( state::context_frame( buffer.str(), res_idx, sticky ) );
1434
1435 return res_idx;
1436 }
1437
1438
1439
1440
1441
1442
1443
1444 struct frame_with_id {
1445 explicit frame_with_id( int id ) : m_id( id ) {}
1446
1447 bool operator()( state::context_frame const& f )
1448 {
1449 return f.frame_id == m_id;
1450 }
1451 int m_id;
1452 };
1453
1454
1455
1456 void
1457 clear_context( int frame_id )
1458 {
1459 if( frame_id == -1 ) {
1460 for( int i=static_cast<int>(impl::s_frk_state().m_context.size())-1; i>=0; i-- )
1461 if( !impl::s_frk_state().m_context[i].is_sticky )
1462 impl::s_frk_state().m_context.erase( impl::s_frk_state().m_context.begin()+i );
1463 }
1464
1465 else {
1466 state::context_data::iterator it =
1467 std::find_if( impl::s_frk_state().m_context.begin(), impl::s_frk_state().m_context.end(), frame_with_id( frame_id ) );
1468
1469 if( it != impl::s_frk_state().m_context.end() )
1470 impl::s_frk_state().m_context.erase( it );
1471 }
1472 }
1473
1474
1475
1476
1477
1478
1479
1480 context_generator
1481 get_context()
1482 {
1483 return context_generator();
1484 }
1485
1486
1487
1488
1489
1490
1491
1492 bool
1493 context_generator::is_empty() const
1494 {
1495 return impl::s_frk_state().m_context.empty();
1496 }
1497
1498
1499
1500 const_string
1501 context_generator::next() const
1502 {
1503 return m_curr_frame < impl::s_frk_state().m_context.size() ? impl::s_frk_state().m_context[m_curr_frame++].descr : const_string();
1504 }
1505
1506
1507
1508
1509
1510
1511
1512 master_test_suite_t&
1513 master_test_suite()
1514 {
1515 if( !impl::s_frk_state().m_master_test_suite )
1516 impl::s_frk_state().m_master_test_suite = new master_test_suite_t;
1517
1518 return *impl::s_frk_state().m_master_test_suite;
1519 }
1520
1521 namespace impl {
1522
1523 master_test_suite_name_setter::master_test_suite_name_setter(const_string name) {
1524 assign_op( master_test_suite().p_name.value, name.trim( "\"" ), 0 );
1525 }
1526
1527 }
1528
1529
1530
1531
1532
1533
1534
1535 test_suite&
1536 current_auto_test_suite( test_suite* ts, bool push_or_pop )
1537 {
1538 if( impl::s_frk_state().m_auto_test_suites.empty() )
1539 impl::s_frk_state().m_auto_test_suites.push_back( &framework::master_test_suite() );
1540
1541 if( !push_or_pop )
1542 impl::s_frk_state().m_auto_test_suites.pop_back();
1543 else if( ts )
1544 impl::s_frk_state().m_auto_test_suites.push_back( ts );
1545
1546 return *impl::s_frk_state().m_auto_test_suites.back();
1547 }
1548
1549
1550
1551
1552
1553
1554
1555 test_case const&
1556 current_test_case()
1557 {
1558 return get<test_case>( impl::s_frk_state().m_curr_test_unit );
1559 }
1560
1561
1562 test_unit const&
1563 current_test_unit()
1564 {
1565 return *impl::s_frk_state().m_test_units[impl::s_frk_state().m_curr_test_unit];
1566 }
1567
1568
1569
1570 test_unit_id
1571 current_test_case_id()
1572 {
1573 return impl::s_frk_state().m_curr_test_unit;
1574 }
1575
1576
1577
1578
1579
1580
1581
1582 test_unit&
1583 get( test_unit_id id, test_unit_type t )
1584 {
1585 test_unit* res = impl::s_frk_state().m_test_units[id];
1586
1587 BOOST_TEST_I_ASSRT( (res->p_type & t) != 0, internal_error( "Invalid test unit type" ) );
1588
1589 return *res;
1590 }
1591
1592
1593
1594
1595
1596
1597
1598 template <class Cont>
1599 struct swap_on_delete {
1600 swap_on_delete(Cont& c1, Cont& c2) : m_c1(c1), m_c2(c2){}
1601 ~swap_on_delete() {
1602 m_c1.swap(m_c2);
1603 }
1604
1605 Cont& m_c1;
1606 Cont& m_c2;
1607 };
1608
1609 struct register_observer_helper {
1610 register_observer_helper(test_observer& observer)
1611 : m_observer(observer)
1612 {
1613 register_obs();
1614 }
1615
1616 ~register_observer_helper() {
1617 if(m_registered)
1618 deregister_observer( m_observer );
1619 }
1620
1621 void deregister_obs() {
1622 m_registered = false;
1623 deregister_observer( m_observer );
1624 }
1625
1626 void register_obs() {
1627 m_registered = true;
1628 register_observer( m_observer );
1629 }
1630
1631
1632 test_observer& m_observer;
1633 bool m_registered;
1634 };
1635
1636 void
1637 run( test_unit_id id, bool continue_test )
1638 {
1639 if( id == INV_TEST_UNIT_ID )
1640 id = master_test_suite().p_id;
1641
1642
1643 impl::s_frk_state().deduce_run_status( id );
1644
1645 test_case_counter tcc;
1646 traverse_test_tree( id, tcc );
1647
1648 BOOST_TEST_SETUP_ASSERT( tcc.p_count != 0 , runtime_config::get<std::vector<std::string> >( runtime_config::btrt_run_filters ).empty()
1649 ? BOOST_TEST_L( "test tree is empty" )
1650 : BOOST_TEST_L( "no test cases matching filter or all test cases were disabled" ) );
1651
1652 bool was_in_progress = framework::test_in_progress();
1653 bool call_start_finish = !continue_test || !was_in_progress;
1654 bool init_ok = true;
1655 const_string setup_error;
1656
1657 framework_init_observer_t local_init_observer;
1658 register_observer_helper init_observer_helper( local_init_observer );
1659
1660 if( call_start_finish ) {
1661
1662 impl::s_frk_state().m_test_in_progress = false;
1663
1664 BOOST_TEST_FOREACH( test_observer*, to, impl::s_frk_state().m_observers ) {
1665 BOOST_TEST_I_TRY {
1666 ut_detail::test_unit_id_restore restore_current_test_unit(impl::s_frk_state().m_curr_test_unit, id);
1667 unit_test_monitor_t::error_level result = unit_test_monitor.execute_and_translate( boost::bind( &test_observer::test_start, to, tcc.p_count, id ) );
1668 if( init_ok ) {
1669 if( result != unit_test_monitor_t::test_ok ) {
1670 init_ok = false;
1671 }
1672 else {
1673 if( local_init_observer.has_failed() ) {
1674 init_ok = false;
1675 }
1676 }
1677 }
1678 }
1679 BOOST_TEST_I_CATCH( execution_exception, ex ) {
1680 if( init_ok ) {
1681
1682 init_ok = false;
1683 setup_error = ex.what();
1684 }
1685
1686 }
1687 }
1688 }
1689
1690
1691 init_observer_helper.deregister_obs();
1692
1693 if( init_ok ) {
1694
1695
1696 test_unit& entry_test_unit = framework::get( id, TUT_ANY );
1697 std::vector<test_unit_fixture_ptr> v_saved_fixture(entry_test_unit.p_fixtures.value.begin(),
1698 entry_test_unit.p_fixtures.value.end());
1699
1700 BOOST_TEST_FOREACH( test_unit_fixture*, tuf, impl::s_frk_state().m_global_fixtures ) {
1701 entry_test_unit.p_fixtures.value.insert( entry_test_unit.p_fixtures.value.begin(),
1702 test_unit_fixture_ptr(new impl::global_fixture_handle(tuf)) );
1703 }
1704
1705 swap_on_delete< std::vector<test_unit_fixture_ptr> > raii_fixture(v_saved_fixture, entry_test_unit.p_fixtures.value);
1706
1707
1708 impl::s_frk_state().m_test_in_progress = true;
1709 unsigned seed = runtime_config::get<unsigned>( runtime_config::btrt_random_seed );
1710 switch( seed ) {
1711 case 0:
1712 break;
1713 case 1:
1714 seed = static_cast<unsigned>( std::rand() ^ std::time( 0 ) );
1715 BOOST_FALLTHROUGH;
1716 default:
1717 BOOST_TEST_FRAMEWORK_MESSAGE( "Test cases order is shuffled using seed: " << seed );
1718 std::srand( seed );
1719 }
1720
1721
1722 impl::s_frk_state().execute_test_tree( id );
1723
1724
1725 }
1726
1727 impl::s_frk_state().m_test_in_progress = false;
1728
1729 results_reporter::make_report( INV_REPORT_LEVEL, id );
1730
1731
1732 init_observer_helper.register_obs();
1733
1734 local_init_observer.clear();
1735 if( call_start_finish ) {
1736
1737
1738
1739 impl::s_frk_state().m_test_in_progress = false;
1740 BOOST_TEST_REVERSE_FOREACH( test_observer*, to, impl::s_frk_state().m_observers ) {
1741 ut_detail::test_unit_id_restore restore_current_test_unit(impl::s_frk_state().m_curr_test_unit, id);
1742 to->test_finish();
1743 }
1744 }
1745
1746 impl::s_frk_state().m_test_in_progress = was_in_progress;
1747
1748
1749 BOOST_TEST_SETUP_ASSERT( init_ok && !local_init_observer.has_failed(), setup_error );
1750 }
1751
1752
1753
1754 void
1755 run( test_unit const* tu, bool continue_test )
1756 {
1757 run( tu->p_id, continue_test );
1758 }
1759
1760
1761
1762
1763
1764
1765
1766 void
1767 assertion_result( unit_test::assertion_result ar )
1768 {
1769 BOOST_TEST_FOREACH( test_observer*, to, impl::s_frk_state().m_observers )
1770 to->assertion_result( ar );
1771 }
1772
1773
1774
1775
1776
1777
1778
1779 void
1780 exception_caught( execution_exception const& ex )
1781 {
1782 BOOST_TEST_FOREACH( test_observer*, to, impl::s_frk_state().m_observers )
1783 to->exception_caught( ex );
1784 }
1785
1786
1787
1788
1789
1790
1791
1792 void
1793 test_unit_aborted( test_unit const& tu )
1794 {
1795 BOOST_TEST_FOREACH( test_observer*, to, impl::s_frk_state().m_observers )
1796 to->test_unit_aborted( tu );
1797 }
1798
1799
1800
1801
1802
1803 void
1804 test_aborted( )
1805 {
1806 BOOST_TEST_FOREACH( test_observer*, to, impl::s_frk_state().m_observers )
1807 to->test_aborted( );
1808 }
1809
1810
1811
1812
1813 }
1814 }
1815 }
1816
1817 #include <boost/test/detail/enable_warnings.hpp>
1818
1819 #endif