framework.ipp 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819
  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/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. void visit( test_case const& tc ) BOOST_OVERRIDE
  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. bool test_suite_start( test_suite const& ts ) BOOST_OVERRIDE
  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. void test_suite_finish( test_suite const& /*ts*/ ) BOOST_OVERRIDE
  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. bool visit( test_unit const& tu ) BOOST_OVERRIDE
  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. bool visit( test_unit const& tu ) BOOST_OVERRIDE
  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() BOOST_OVERRIDE {}
  374. void setup() BOOST_OVERRIDE {
  375. m_global_fixture->setup();
  376. }
  377. void teardown() BOOST_OVERRIDE {
  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 dependencies 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 siblings 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()) ||
  689. ((lhs->priority() == rhs->priority()) && std::less<test_observer*>()(lhs, rhs));
  690. }
  691. };
  692. // Data members
  693. typedef std::map<test_unit_id,test_unit*> test_unit_store;
  694. typedef std::set<test_observer*,priority_order> observer_store;
  695. struct context_frame {
  696. context_frame( std::string const& d, int id, bool sticky )
  697. : descr( d )
  698. , frame_id( id )
  699. , is_sticky( sticky )
  700. {}
  701. std::string descr;
  702. int frame_id;
  703. bool is_sticky;
  704. };
  705. typedef std::vector<context_frame> context_data;
  706. master_test_suite_t* m_master_test_suite;
  707. std::vector<test_suite*> m_auto_test_suites;
  708. test_unit_id m_curr_test_unit;
  709. test_unit_store m_test_units;
  710. test_unit_id m_next_test_case_id;
  711. test_unit_id m_next_test_suite_id;
  712. bool m_test_in_progress;
  713. observer_store m_observers;
  714. context_data m_context;
  715. int m_context_idx;
  716. std::set<global_fixture*> m_global_fixtures;
  717. boost::execution_monitor m_aux_em;
  718. std::map<output_format, runtime_config::stream_holder> m_log_sinks;
  719. runtime_config::stream_holder m_report_sink;
  720. };
  721. //____________________________________________________________________________//
  722. namespace impl {
  723. namespace {
  724. #if defined(__CYGWIN__)
  725. framework::state& s_frk_state() { static framework::state* the_inst = 0; if(!the_inst) the_inst = new framework::state; return *the_inst; }
  726. #else
  727. framework::state& s_frk_state() { static framework::state the_inst; return the_inst; }
  728. #endif
  729. } // local namespace
  730. void
  731. setup_for_execution( test_unit const& tu )
  732. {
  733. s_frk_state().deduce_run_status( tu.p_id );
  734. }
  735. struct sum_to_first_only {
  736. sum_to_first_only() : is_first(true) {}
  737. template <class T, class U>
  738. T operator()(T const& l_, U const& r_) {
  739. if(is_first) {
  740. is_first = false;
  741. return l_ + r_.first;
  742. }
  743. return l_ + ", " + r_.first;
  744. }
  745. bool is_first;
  746. };
  747. void
  748. shutdown_loggers_and_reports()
  749. {
  750. s_frk_state().m_log_sinks.clear();
  751. s_frk_state().m_report_sink.setup( "stderr" );
  752. }
  753. void
  754. unregister_global_fixture_and_configuration()
  755. {
  756. // we make a copy as the set will change in the iteration
  757. std::set<global_fixture*> gfixture_copy(s_frk_state().m_global_fixtures);
  758. BOOST_TEST_FOREACH( global_fixture*, tuf, gfixture_copy ) {
  759. tuf->unregister_from_framework();
  760. }
  761. s_frk_state().m_global_fixtures.clear();
  762. state::observer_store gobserver_copy(s_frk_state().m_observers);
  763. BOOST_TEST_FOREACH( test_observer*, to, gobserver_copy ) {
  764. framework::deregister_observer( *to );
  765. }
  766. s_frk_state().m_observers.clear();
  767. }
  768. void
  769. setup_loggers()
  770. {
  771. BOOST_TEST_I_TRY {
  772. #ifdef BOOST_TEST_SUPPORT_TOKEN_ITERATOR
  773. bool has_combined_logger = runtime_config::has( runtime_config::btrt_combined_logger )
  774. && !runtime_config::get< std::vector<std::string> >( runtime_config::btrt_combined_logger ).empty();
  775. #else
  776. bool has_combined_logger = false;
  777. #endif
  778. if( !has_combined_logger ) {
  779. unit_test_log.set_threshold_level( runtime_config::get<log_level>( runtime_config::btrt_log_level ) );
  780. const output_format format = runtime_config::get<output_format>( runtime_config::btrt_log_format );
  781. unit_test_log.set_format( format );
  782. runtime_config::stream_holder& stream_logger = s_frk_state().m_log_sinks[format];
  783. if( runtime_config::has( runtime_config::btrt_log_sink ) ) {
  784. // we remove all streams in this case, so we do not specify the format
  785. boost::function< void () > log_cleaner = boost::bind( &unit_test_log_t::set_stream,
  786. &unit_test_log,
  787. boost::ref(std::cout)
  788. );
  789. stream_logger.setup( runtime_config::get<std::string>( runtime_config::btrt_log_sink ),
  790. log_cleaner );
  791. }
  792. unit_test_log.set_stream( stream_logger.ref() );
  793. unit_test_log.configure();
  794. }
  795. else
  796. {
  797. const std::vector<std::string>& v_output_format = runtime_config::get< std::vector<std::string> >( runtime_config::btrt_combined_logger ) ;
  798. static const std::pair<const char*, log_level> all_log_levels[] = {
  799. std::make_pair( "all" , log_successful_tests ),
  800. std::make_pair( "success" , log_successful_tests ),
  801. std::make_pair( "test_suite" , log_test_units ),
  802. std::make_pair( "unit_scope" , log_test_units ),
  803. std::make_pair( "message" , log_messages ),
  804. std::make_pair( "warning" , log_warnings ),
  805. std::make_pair( "error" , log_all_errors ),
  806. std::make_pair( "cpp_exception" , log_cpp_exception_errors ),
  807. std::make_pair( "system_error" , log_system_errors ),
  808. std::make_pair( "fatal_error" , log_fatal_errors ),
  809. std::make_pair( "nothing" , log_nothing )
  810. };
  811. static const std::pair<const char*, output_format> all_formats[] = {
  812. std::make_pair( "HRF" , OF_CLF ),
  813. std::make_pair( "CLF" , OF_CLF ),
  814. std::make_pair( "XML" , OF_XML ),
  815. std::make_pair( "JUNIT", OF_JUNIT )
  816. };
  817. bool is_first = true;
  818. BOOST_TEST_FOREACH( const_string, current_multi_config, v_output_format ) {
  819. #ifdef BOOST_TEST_SUPPORT_TOKEN_ITERATOR
  820. // ':' may be used for file names: C:/tmp/mylogsink.xml
  821. // we merge the tokens that start with / or \ with the previous one.
  822. std::vector<std::string> v_processed_tokens;
  823. {
  824. utils::string_token_iterator current_config( current_multi_config, (utils::dropped_delimeters = ":",
  825. utils::kept_delimeters = utils::dt_none) );
  826. for( ; current_config != utils::string_token_iterator() ; ++current_config) {
  827. std::string str_copy(current_config->begin(), current_config->end());
  828. if( ( str_copy[0] == '\\' || str_copy[0] == '/' )
  829. && v_processed_tokens.size() > 0) {
  830. v_processed_tokens.back() += ":" + str_copy; // ':' has been eaten up
  831. }
  832. else {
  833. v_processed_tokens.push_back(str_copy);
  834. }
  835. }
  836. }
  837. BOOST_TEST_FOREACH( std::string const&, current_config, v_processed_tokens ) {
  838. utils::string_token_iterator current_format_specs( current_config, (utils::keep_empty_tokens,
  839. utils::dropped_delimeters = ",",
  840. utils::kept_delimeters = utils::dt_none) );
  841. output_format format = OF_INVALID ; // default
  842. if( current_format_specs != utils::string_token_iterator() &&
  843. current_format_specs->size() ) {
  844. for(size_t elem=0; elem < sizeof(all_formats)/sizeof(all_formats[0]); elem++) {
  845. if(const_string(all_formats[elem].first) == *current_format_specs) {
  846. format = all_formats[elem].second;
  847. break;
  848. }
  849. }
  850. }
  851. BOOST_TEST_I_ASSRT( format != OF_INVALID,
  852. boost::runtime::access_to_missing_argument()
  853. << "Unable to determine the logger type from '"
  854. << current_config
  855. << "'. Possible choices are: "
  856. << std::accumulate(all_formats,
  857. all_formats + sizeof(all_formats)/sizeof(all_formats[0]),
  858. std::string(""),
  859. sum_to_first_only())
  860. );
  861. // activates this format
  862. if( is_first ) {
  863. unit_test_log.set_format( format );
  864. }
  865. else {
  866. unit_test_log.add_format( format );
  867. }
  868. is_first = false;
  869. unit_test_log_formatter * const formatter = unit_test_log.get_formatter(format);
  870. BOOST_TEST_SETUP_ASSERT( formatter, "Logger setup error" );
  871. log_level formatter_log_level = invalid_log_level;
  872. ++current_format_specs ;
  873. if( !current_format_specs->size() ) {
  874. formatter_log_level = formatter->get_log_level(); // default log level given by the formatter
  875. }
  876. else if( current_format_specs != utils::string_token_iterator() ) {
  877. for(size_t elem=0; elem < sizeof(all_log_levels)/sizeof(all_log_levels[0]); elem++) {
  878. if(const_string(all_log_levels[elem].first) == *current_format_specs) {
  879. formatter_log_level = all_log_levels[elem].second;
  880. break;
  881. }
  882. }
  883. }
  884. BOOST_TEST_I_ASSRT( formatter_log_level != invalid_log_level,
  885. boost::runtime::access_to_missing_argument()
  886. << "Unable to determine the log level from '"
  887. << current_config
  888. << "'. Possible choices are: "
  889. << std::accumulate(all_log_levels,
  890. all_log_levels + sizeof(all_log_levels)/sizeof(all_log_levels[0]),
  891. std::string(""),
  892. sum_to_first_only())
  893. );
  894. unit_test_log.set_threshold_level( format, formatter_log_level );
  895. runtime_config::stream_holder& stream_logger = s_frk_state().m_log_sinks[format];
  896. boost::function< void () > log_cleaner = boost::bind( &unit_test_log_t::set_stream,
  897. &unit_test_log,
  898. format,
  899. boost::ref(std::cout) );
  900. if( ++current_format_specs != utils::string_token_iterator() &&
  901. current_format_specs->size() ) {
  902. stream_logger.setup( *current_format_specs,
  903. log_cleaner );
  904. }
  905. else {
  906. stream_logger.setup( formatter->get_default_stream_description(),
  907. log_cleaner );
  908. }
  909. unit_test_log.set_stream( format, stream_logger.ref() );
  910. }
  911. #endif
  912. } // for each logger
  913. } // if/else new logger API
  914. } // BOOST_TEST_I_TRY
  915. BOOST_TEST_I_CATCH( boost::runtime::init_error, ex ) {
  916. BOOST_TEST_SETUP_ASSERT( false, ex.msg );
  917. }
  918. BOOST_TEST_I_CATCH( boost::runtime::input_error, ex ) {
  919. std::cerr << ex.msg << "\n\n";
  920. BOOST_TEST_I_THROW( framework::nothing_to_test( boost::exit_exception_failure ) );
  921. }
  922. }
  923. //____________________________________________________________________________//
  924. } // namespace impl
  925. //____________________________________________________________________________//
  926. // ************************************************************************** //
  927. // ************** framework::init ************** //
  928. // ************************************************************************** //
  929. void
  930. init( init_unit_test_func init_func, int argc, char* argv[] )
  931. {
  932. using namespace impl;
  933. // 10. Set up runtime parameters
  934. runtime_config::init( argc, argv );
  935. // 20. Set the desired log level, format and sink
  936. impl::setup_loggers();
  937. // 30. Set the desired report level, format and sink
  938. results_reporter::set_level( runtime_config::get<report_level>( runtime_config::btrt_report_level ) );
  939. results_reporter::set_format( runtime_config::get<output_format>( runtime_config::btrt_report_format ) );
  940. if( runtime_config::has( runtime_config::btrt_report_sink ) ) {
  941. boost::function< void () > report_cleaner = boost::bind( &results_reporter::set_stream,
  942. boost::ref(std::cerr)
  943. );
  944. s_frk_state().m_report_sink.setup( runtime_config::get<std::string>( runtime_config::btrt_report_sink ),
  945. report_cleaner );
  946. }
  947. results_reporter::set_stream( s_frk_state().m_report_sink.ref() );
  948. // 40. Register default test observers
  949. register_observer( results_collector );
  950. register_observer( unit_test_log );
  951. if( runtime_config::get<bool>( runtime_config::btrt_show_progress ) ) {
  952. progress_monitor.set_stream( std::cout ); // defaults to stdout
  953. register_observer( progress_monitor );
  954. }
  955. // 50. Set up memory leak detection
  956. unsigned long detect_mem_leak = runtime_config::get<unsigned long>( runtime_config::btrt_detect_mem_leaks );
  957. if( detect_mem_leak > 0 ) {
  958. debug::detect_memory_leaks( true, runtime_config::get<std::string>( runtime_config::btrt_report_mem_leaks ) );
  959. debug::break_memory_alloc( (long)detect_mem_leak );
  960. }
  961. // 60. Initialize master unit test suite
  962. master_test_suite().argc = argc;
  963. master_test_suite().argv = argv;
  964. // 70. Invoke test module initialization routine
  965. BOOST_TEST_I_TRY {
  966. s_frk_state().m_aux_em.vexecute( boost::bind( &impl::invoke_init_func, init_func ) );
  967. }
  968. BOOST_TEST_I_CATCH( execution_exception, ex ) {
  969. BOOST_TEST_SETUP_ASSERT( false, ex.what() );
  970. }
  971. }
  972. //____________________________________________________________________________//
  973. void
  974. finalize_setup_phase( test_unit_id master_tu_id )
  975. {
  976. if( master_tu_id == INV_TEST_UNIT_ID )
  977. master_tu_id = master_test_suite().p_id;
  978. // 10. Apply all decorators to the auto test units
  979. // 10. checks for consistency (duplicate names, etc)
  980. class apply_decorators : public test_tree_visitor {
  981. private:
  982. // test_tree_visitor interface
  983. bool test_suite_start( test_suite const& ts) BOOST_OVERRIDE
  984. {
  985. const_cast<test_suite&>(ts).generate();
  986. const_cast<test_suite&>(ts).check_for_duplicate_test_cases();
  987. return test_tree_visitor::test_suite_start(ts);
  988. }
  989. bool visit( test_unit const& tu ) BOOST_OVERRIDE
  990. {
  991. BOOST_TEST_FOREACH( decorator::base_ptr, d, tu.p_decorators.get() )
  992. d->apply( const_cast<test_unit&>(tu) );
  993. return true;
  994. }
  995. } ad;
  996. traverse_test_tree( master_tu_id, ad, true );
  997. // 20. Finalize setup phase
  998. impl::order_info_per_tu tuoi;
  999. impl::s_frk_state().deduce_siblings_order( master_tu_id, master_tu_id, tuoi );
  1000. impl::s_frk_state().finalize_default_run_status( master_tu_id, test_unit::RS_INVALID );
  1001. }
  1002. // ************************************************************************** //
  1003. // ************** test_in_progress ************** //
  1004. // ************************************************************************** //
  1005. bool
  1006. test_in_progress()
  1007. {
  1008. return impl::s_frk_state().m_test_in_progress;
  1009. }
  1010. //____________________________________________________________________________//
  1011. // ************************************************************************** //
  1012. // ************** framework::shutdown ************** //
  1013. // ************************************************************************** //
  1014. void
  1015. shutdown()
  1016. {
  1017. // shuts down the loggers singleton to avoid any further reference to the
  1018. // framework during the destruction of those
  1019. impl::shutdown_loggers_and_reports();
  1020. // unregisters any global fixture and configuration object
  1021. impl::unregister_global_fixture_and_configuration();
  1022. // eliminating some fake memory leak reports. See for more details:
  1023. // http://connect.microsoft.com/VisualStudio/feedback/details/106937/memory-leaks-reported-by-debug-crt-inside-typeinfo-name
  1024. # if BOOST_WORKAROUND(BOOST_MSVC, <= 1600 ) && !defined(_DLL) && defined(_DEBUG)
  1025. # if BOOST_WORKAROUND(BOOST_MSVC, < 1600 )
  1026. #define _Next next
  1027. #define _MemPtr memPtr
  1028. #endif
  1029. __type_info_node* pNode = __type_info_root_node._Next;
  1030. __type_info_node* tmpNode = &__type_info_root_node;
  1031. for( ; pNode!=NULL; pNode = tmpNode ) {
  1032. tmpNode = pNode->_Next;
  1033. delete pNode->_MemPtr;
  1034. delete pNode;
  1035. }
  1036. # if BOOST_WORKAROUND(BOOST_MSVC, < 1600 )
  1037. #undef _Next
  1038. #undef _MemPtr
  1039. #endif
  1040. # endif
  1041. }
  1042. //____________________________________________________________________________//
  1043. // ************************************************************************** //
  1044. // ************** register_test_unit ************** //
  1045. // ************************************************************************** //
  1046. void
  1047. register_test_unit( test_case* tc )
  1048. {
  1049. BOOST_TEST_SETUP_ASSERT( tc->p_id == INV_TEST_UNIT_ID, BOOST_TEST_L( "test case already registered" ) );
  1050. test_unit_id new_id = impl::s_frk_state().m_next_test_case_id;
  1051. BOOST_TEST_SETUP_ASSERT( new_id != MAX_TEST_CASE_ID, BOOST_TEST_L( "too many test cases" ) );
  1052. typedef state::test_unit_store::value_type map_value_type;
  1053. impl::s_frk_state().m_test_units.insert( map_value_type( new_id, tc ) );
  1054. impl::s_frk_state().m_next_test_case_id++;
  1055. impl::s_frk_state().set_tu_id( *tc, new_id );
  1056. }
  1057. //____________________________________________________________________________//
  1058. // ************************************************************************** //
  1059. // ************** register_test_unit ************** //
  1060. // ************************************************************************** //
  1061. void
  1062. register_test_unit( test_suite* ts )
  1063. {
  1064. BOOST_TEST_SETUP_ASSERT( ts->p_id == INV_TEST_UNIT_ID, BOOST_TEST_L( "test suite already registered" ) );
  1065. test_unit_id new_id = impl::s_frk_state().m_next_test_suite_id;
  1066. BOOST_TEST_SETUP_ASSERT( new_id != MAX_TEST_SUITE_ID, BOOST_TEST_L( "too many test suites" ) );
  1067. typedef state::test_unit_store::value_type map_value_type;
  1068. impl::s_frk_state().m_test_units.insert( map_value_type( new_id, ts ) );
  1069. impl::s_frk_state().m_next_test_suite_id++;
  1070. impl::s_frk_state().set_tu_id( *ts, new_id );
  1071. }
  1072. //____________________________________________________________________________//
  1073. // ************************************************************************** //
  1074. // ************** deregister_test_unit ************** //
  1075. // ************************************************************************** //
  1076. void
  1077. deregister_test_unit( test_unit* tu )
  1078. {
  1079. impl::s_frk_state().m_test_units.erase( tu->p_id );
  1080. }
  1081. //____________________________________________________________________________//
  1082. // ************************************************************************** //
  1083. // ************** clear ************** //
  1084. // ************************************************************************** //
  1085. void
  1086. clear()
  1087. {
  1088. impl::s_frk_state().clear();
  1089. }
  1090. //____________________________________________________________________________//
  1091. // ************************************************************************** //
  1092. // ************** register_observer ************** //
  1093. // ************************************************************************** //
  1094. void
  1095. register_observer( test_observer& to )
  1096. {
  1097. impl::s_frk_state().m_observers.insert( &to );
  1098. }
  1099. //____________________________________________________________________________//
  1100. // ************************************************************************** //
  1101. // ************** deregister_observer ************** //
  1102. // ************************************************************************** //
  1103. void
  1104. deregister_observer( test_observer& to )
  1105. {
  1106. impl::s_frk_state().m_observers.erase( &to );
  1107. }
  1108. //____________________________________________________________________________//
  1109. // ************************************************************************** //
  1110. // ************** register_global_fixture ************** //
  1111. // ************************************************************************** //
  1112. void
  1113. register_global_fixture( global_fixture& tuf )
  1114. {
  1115. impl::s_frk_state().m_global_fixtures.insert( &tuf );
  1116. }
  1117. //____________________________________________________________________________//
  1118. // ************************************************************************** //
  1119. // ************** deregister_global_fixture ************** //
  1120. // ************************************************************************** //
  1121. void
  1122. deregister_global_fixture( global_fixture &tuf )
  1123. {
  1124. impl::s_frk_state().m_global_fixtures.erase( &tuf );
  1125. }
  1126. //____________________________________________________________________________//
  1127. // ************************************************************************** //
  1128. // ************** add_context ************** //
  1129. // ************************************************************************** //
  1130. int
  1131. add_context( ::boost::unit_test::lazy_ostream const& context_descr, bool sticky )
  1132. {
  1133. std::stringstream buffer;
  1134. context_descr( buffer );
  1135. int res_idx = impl::s_frk_state().m_context_idx++;
  1136. impl::s_frk_state().m_context.push_back( state::context_frame( buffer.str(), res_idx, sticky ) );
  1137. return res_idx;
  1138. }
  1139. //____________________________________________________________________________//
  1140. // ************************************************************************** //
  1141. // ************** clear_context ************** //
  1142. // ************************************************************************** //
  1143. struct frame_with_id {
  1144. explicit frame_with_id( int id ) : m_id( id ) {}
  1145. bool operator()( state::context_frame const& f )
  1146. {
  1147. return f.frame_id == m_id;
  1148. }
  1149. int m_id;
  1150. };
  1151. //____________________________________________________________________________//
  1152. void
  1153. clear_context( int frame_id )
  1154. {
  1155. if( frame_id == -1 ) { // clear all non sticky frames
  1156. for( int i=static_cast<int>(impl::s_frk_state().m_context.size())-1; i>=0; i-- )
  1157. if( !impl::s_frk_state().m_context[i].is_sticky )
  1158. impl::s_frk_state().m_context.erase( impl::s_frk_state().m_context.begin()+i );
  1159. }
  1160. else { // clear specific frame
  1161. state::context_data::iterator it =
  1162. std::find_if( impl::s_frk_state().m_context.begin(), impl::s_frk_state().m_context.end(), frame_with_id( frame_id ) );
  1163. if( it != impl::s_frk_state().m_context.end() ) // really an internal error if this is not true
  1164. impl::s_frk_state().m_context.erase( it );
  1165. }
  1166. }
  1167. //____________________________________________________________________________//
  1168. // ************************************************************************** //
  1169. // ************** get_context ************** //
  1170. // ************************************************************************** //
  1171. context_generator
  1172. get_context()
  1173. {
  1174. return context_generator();
  1175. }
  1176. //____________________________________________________________________________//
  1177. // ************************************************************************** //
  1178. // ************** context_generator ************** //
  1179. // ************************************************************************** //
  1180. bool
  1181. context_generator::is_empty() const
  1182. {
  1183. return impl::s_frk_state().m_context.empty();
  1184. }
  1185. //____________________________________________________________________________//
  1186. const_string
  1187. context_generator::next() const
  1188. {
  1189. return m_curr_frame < impl::s_frk_state().m_context.size() ? impl::s_frk_state().m_context[m_curr_frame++].descr : const_string();
  1190. }
  1191. //____________________________________________________________________________//
  1192. // ************************************************************************** //
  1193. // ************** master_test_suite ************** //
  1194. // ************************************************************************** //
  1195. master_test_suite_t&
  1196. master_test_suite()
  1197. {
  1198. if( !impl::s_frk_state().m_master_test_suite )
  1199. impl::s_frk_state().m_master_test_suite = new master_test_suite_t;
  1200. return *impl::s_frk_state().m_master_test_suite;
  1201. }
  1202. namespace impl {
  1203. master_test_suite_name_setter::master_test_suite_name_setter(const_string name) {
  1204. assign_op( master_test_suite().p_name.value, name.trim( "\"" ), 0 );
  1205. }
  1206. }
  1207. //____________________________________________________________________________//
  1208. // ************************************************************************** //
  1209. // ************** current_auto_test_suite ************** //
  1210. // ************************************************************************** //
  1211. test_suite&
  1212. current_auto_test_suite( test_suite* ts, bool push_or_pop )
  1213. {
  1214. if( impl::s_frk_state().m_auto_test_suites.empty() )
  1215. impl::s_frk_state().m_auto_test_suites.push_back( &framework::master_test_suite() );
  1216. if( !push_or_pop )
  1217. impl::s_frk_state().m_auto_test_suites.pop_back();
  1218. else if( ts )
  1219. impl::s_frk_state().m_auto_test_suites.push_back( ts );
  1220. return *impl::s_frk_state().m_auto_test_suites.back();
  1221. }
  1222. //____________________________________________________________________________//
  1223. // ************************************************************************** //
  1224. // ************** current_test_case ************** //
  1225. // ************************************************************************** //
  1226. test_case const&
  1227. current_test_case()
  1228. {
  1229. return get<test_case>( impl::s_frk_state().m_curr_test_unit );
  1230. }
  1231. test_unit const&
  1232. current_test_unit()
  1233. {
  1234. return *impl::s_frk_state().m_test_units[impl::s_frk_state().m_curr_test_unit];
  1235. }
  1236. //____________________________________________________________________________//
  1237. test_unit_id
  1238. current_test_case_id()
  1239. {
  1240. return impl::s_frk_state().m_curr_test_unit;
  1241. }
  1242. //____________________________________________________________________________//
  1243. // ************************************************************************** //
  1244. // ************** framework::get ************** //
  1245. // ************************************************************************** //
  1246. test_unit&
  1247. get( test_unit_id id, test_unit_type t )
  1248. {
  1249. test_unit* res = impl::s_frk_state().m_test_units[id];
  1250. BOOST_TEST_I_ASSRT( (res->p_type & t) != 0, internal_error( "Invalid test unit type" ) );
  1251. return *res;
  1252. }
  1253. //____________________________________________________________________________//
  1254. // ************************************************************************** //
  1255. // ************** framework::run ************** //
  1256. // ************************************************************************** //
  1257. template <class Cont>
  1258. struct swap_on_delete {
  1259. swap_on_delete(Cont& c1, Cont& c2) : m_c1(c1), m_c2(c2){}
  1260. ~swap_on_delete() {
  1261. m_c1.swap(m_c2);
  1262. }
  1263. Cont& m_c1;
  1264. Cont& m_c2;
  1265. };
  1266. struct register_observer_helper {
  1267. register_observer_helper(test_observer& observer)
  1268. : m_observer(observer)
  1269. {
  1270. register_obs();
  1271. }
  1272. ~register_observer_helper() {
  1273. if(m_registered)
  1274. deregister_observer( m_observer );
  1275. }
  1276. void deregister_obs() {
  1277. m_registered = false;
  1278. deregister_observer( m_observer );
  1279. }
  1280. void register_obs() {
  1281. m_registered = true;
  1282. register_observer( m_observer );
  1283. }
  1284. test_observer& m_observer;
  1285. bool m_registered;
  1286. };
  1287. void
  1288. run( test_unit_id id, bool continue_test )
  1289. {
  1290. if( id == INV_TEST_UNIT_ID )
  1291. id = master_test_suite().p_id;
  1292. // Figure out run status for execution phase
  1293. impl::s_frk_state().deduce_run_status( id );
  1294. test_case_counter tcc;
  1295. traverse_test_tree( id, tcc );
  1296. BOOST_TEST_SETUP_ASSERT( tcc.p_count != 0 , runtime_config::get<std::vector<std::string> >( runtime_config::btrt_run_filters ).empty()
  1297. ? BOOST_TEST_L( "test tree is empty" )
  1298. : BOOST_TEST_L( "no test cases matching filter or all test cases were disabled" ) );
  1299. bool was_in_progress = framework::test_in_progress();
  1300. bool call_start_finish = !continue_test || !was_in_progress;
  1301. bool init_ok = true;
  1302. const_string setup_error;
  1303. framework_init_observer_t local_init_observer;
  1304. register_observer_helper init_observer_helper( local_init_observer );
  1305. if( call_start_finish ) {
  1306. // indicates the framework that no test is in progress now if observers need to be notified
  1307. impl::s_frk_state().m_test_in_progress = false;
  1308. // unit_test::framework_init_observer will get cleared first
  1309. BOOST_TEST_FOREACH( test_observer*, to, impl::s_frk_state().m_observers ) {
  1310. BOOST_TEST_I_TRY {
  1311. ut_detail::test_unit_id_restore restore_current_test_unit(impl::s_frk_state().m_curr_test_unit, id);
  1312. unit_test_monitor_t::error_level result = unit_test_monitor.execute_and_translate( boost::bind( &test_observer::test_start, to, tcc.p_count, id ) );
  1313. if( init_ok ) {
  1314. if( result != unit_test_monitor_t::test_ok ) {
  1315. init_ok = false;
  1316. }
  1317. else {
  1318. if( local_init_observer.has_failed() ) {
  1319. init_ok = false;
  1320. }
  1321. }
  1322. }
  1323. }
  1324. BOOST_TEST_I_CATCH( execution_exception, ex ) {
  1325. if( init_ok ) {
  1326. // log only the first error
  1327. init_ok = false;
  1328. setup_error = ex.what();
  1329. }
  1330. // break; // we should continue otherwise loggers may have improper structure (XML start missing for instance)
  1331. }
  1332. }
  1333. }
  1334. // removing this observer as it should not be of any use for the tests
  1335. init_observer_helper.deregister_obs();
  1336. if( init_ok ) {
  1337. // attaching the global fixtures to the main entry point
  1338. test_unit& entry_test_unit = framework::get( id, TUT_ANY );
  1339. std::vector<test_unit_fixture_ptr> v_saved_fixture(entry_test_unit.p_fixtures.value.begin(),
  1340. entry_test_unit.p_fixtures.value.end());
  1341. BOOST_TEST_FOREACH( test_unit_fixture*, tuf, impl::s_frk_state().m_global_fixtures ) {
  1342. entry_test_unit.p_fixtures.value.insert( entry_test_unit.p_fixtures.value.begin(),
  1343. test_unit_fixture_ptr(new impl::global_fixture_handle(tuf)) );
  1344. }
  1345. swap_on_delete< std::vector<test_unit_fixture_ptr> > raii_fixture(v_saved_fixture, entry_test_unit.p_fixtures.value);
  1346. // now work in progress
  1347. impl::s_frk_state().m_test_in_progress = true;
  1348. unsigned seed = runtime_config::get<unsigned>( runtime_config::btrt_random_seed );
  1349. switch( seed ) {
  1350. case 0:
  1351. break;
  1352. case 1:
  1353. seed = static_cast<unsigned>( std::rand() ^ std::time( 0 ) ); // better init using std::rand() ^ ...
  1354. BOOST_FALLTHROUGH;
  1355. default:
  1356. BOOST_TEST_FRAMEWORK_MESSAGE( "Test cases order is shuffled using seed: " << seed );
  1357. std::srand( seed );
  1358. }
  1359. // executing the test tree
  1360. impl::s_frk_state().execute_test_tree( id );
  1361. // removing previously added global fixtures: dtor raii_fixture
  1362. }
  1363. impl::s_frk_state().m_test_in_progress = false;
  1364. results_reporter::make_report( INV_REPORT_LEVEL, id );
  1365. // reinstalling this observer
  1366. init_observer_helper.register_obs();
  1367. local_init_observer.clear();
  1368. if( call_start_finish ) {
  1369. // indicates the framework that no test is in progress anymore if observers need to be notified
  1370. // and this is a teardown, so assertions should not raise any exception otherwise an exception
  1371. // might be raised in a dtor of a global fixture
  1372. impl::s_frk_state().m_test_in_progress = false;
  1373. BOOST_TEST_REVERSE_FOREACH( test_observer*, to, impl::s_frk_state().m_observers ) {
  1374. ut_detail::test_unit_id_restore restore_current_test_unit(impl::s_frk_state().m_curr_test_unit, id);
  1375. to->test_finish();
  1376. }
  1377. }
  1378. impl::s_frk_state().m_test_in_progress = was_in_progress;
  1379. // propagates the init/teardown error if any
  1380. BOOST_TEST_SETUP_ASSERT( init_ok && !local_init_observer.has_failed(), setup_error );
  1381. }
  1382. //____________________________________________________________________________//
  1383. void
  1384. run( test_unit const* tu, bool continue_test )
  1385. {
  1386. run( tu->p_id, continue_test );
  1387. }
  1388. //____________________________________________________________________________//
  1389. // ************************************************************************** //
  1390. // ************** assertion_result ************** //
  1391. // ************************************************************************** //
  1392. void
  1393. assertion_result( unit_test::assertion_result ar )
  1394. {
  1395. BOOST_TEST_FOREACH( test_observer*, to, impl::s_frk_state().m_observers )
  1396. to->assertion_result( ar );
  1397. }
  1398. //____________________________________________________________________________//
  1399. // ************************************************************************** //
  1400. // ************** exception_caught ************** //
  1401. // ************************************************************************** //
  1402. void
  1403. exception_caught( execution_exception const& ex )
  1404. {
  1405. BOOST_TEST_FOREACH( test_observer*, to, impl::s_frk_state().m_observers )
  1406. to->exception_caught( ex );
  1407. }
  1408. //____________________________________________________________________________//
  1409. // ************************************************************************** //
  1410. // ************** test_unit_aborted ************** //
  1411. // ************************************************************************** //
  1412. void
  1413. test_unit_aborted( test_unit const& tu )
  1414. {
  1415. BOOST_TEST_FOREACH( test_observer*, to, impl::s_frk_state().m_observers )
  1416. to->test_unit_aborted( tu );
  1417. }
  1418. // ************************************************************************** //
  1419. // ************** test_aborted ************** //
  1420. // ************************************************************************** //
  1421. void
  1422. test_aborted( )
  1423. {
  1424. BOOST_TEST_FOREACH( test_observer*, to, impl::s_frk_state().m_observers )
  1425. to->test_aborted( );
  1426. }
  1427. //____________________________________________________________________________//
  1428. } // namespace framework
  1429. } // namespace unit_test
  1430. } // namespace boost
  1431. #include <boost/test/detail/enable_warnings.hpp>
  1432. #endif // BOOST_TEST_FRAMEWORK_IPP_021005GER