framework.ipp 68 KB

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