framework.ipp 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724
  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. #if BOOST_TEST_SUPPORT_TOKEN_ITERATOR
  33. #include <boost/test/utils/iterator/token_iterator.hpp>
  34. #endif
  35. #include <boost/test/utils/foreach.hpp>
  36. #include <boost/test/utils/basic_cstring/io.hpp>
  37. #include <boost/test/utils/basic_cstring/compare.hpp>
  38. #include <boost/test/detail/global_typedef.hpp>
  39. #include <boost/test/detail/throw_exception.hpp>
  40. // Boost
  41. #include <boost/timer.hpp>
  42. #include <boost/bind.hpp>
  43. // STL
  44. #include <limits>
  45. #include <map>
  46. #include <set>
  47. #include <cstdlib>
  48. #include <ctime>
  49. #include <numeric>
  50. #ifdef BOOST_NO_CXX98_RANDOM_SHUFFLE
  51. #include <iterator>
  52. #endif
  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. #ifdef BOOST_NO_CXX98_RANDOM_SHUFFLE
  354. // a poor man's implementation of random_shuffle
  355. template< class RandomIt, class RandomFunc >
  356. void random_shuffle( RandomIt first, RandomIt last, RandomFunc &r )
  357. {
  358. typedef typename std::iterator_traits<RandomIt>::difference_type difference_type;
  359. difference_type n = last - first;
  360. for (difference_type i = n-1; i > 0; --i) {
  361. difference_type j = r(i+1);
  362. if (j != i) {
  363. using std::swap;
  364. swap(first[i], first[j]);
  365. }
  366. }
  367. }
  368. #endif
  369. // A simple handle for registering the global fixtures to the master test suite
  370. // without deleting an existing static object (the global fixture itself) when the program
  371. // terminates (shared_ptr).
  372. class global_fixture_handle : public test_unit_fixture {
  373. public:
  374. global_fixture_handle(test_unit_fixture* fixture) : m_global_fixture(fixture) {}
  375. ~global_fixture_handle() {}
  376. virtual void setup() {
  377. m_global_fixture->setup();
  378. }
  379. virtual void teardown() {
  380. m_global_fixture->teardown();
  381. }
  382. private:
  383. test_unit_fixture* m_global_fixture;
  384. };
  385. } // namespace impl
  386. // ************************************************************************** //
  387. // ************** framework::state ************** //
  388. // ************************************************************************** //
  389. unsigned const TIMEOUT_EXCEEDED = static_cast<unsigned>( -1 );
  390. class state {
  391. public:
  392. state()
  393. : m_master_test_suite( 0 )
  394. , m_curr_test_unit( INV_TEST_UNIT_ID )
  395. , m_next_test_case_id( MIN_TEST_CASE_ID )
  396. , m_next_test_suite_id( MIN_TEST_SUITE_ID )
  397. , m_test_in_progress( false )
  398. , m_context_idx( 0 )
  399. , m_log_sinks( )
  400. , m_report_sink( std::cerr )
  401. {
  402. }
  403. ~state() { clear(); }
  404. void clear()
  405. {
  406. while( !m_test_units.empty() ) {
  407. test_unit_store::value_type const& tu = *m_test_units.begin();
  408. test_unit const* tu_ptr = tu.second;
  409. // the delete will erase this element from map
  410. if( ut_detail::test_id_2_unit_type( tu.second->p_id ) == TUT_SUITE )
  411. delete static_cast<test_suite const*>(tu_ptr);
  412. else
  413. delete static_cast<test_case const*>(tu_ptr);
  414. }
  415. }
  416. void set_tu_id( test_unit& tu, test_unit_id id ) { tu.p_id.value = id; }
  417. //////////////////////////////////////////////////////////////////
  418. // Validates the dependency graph and deduces the sibling dependency rank for each child
  419. void deduce_siblings_order( test_unit_id tu_id, test_unit_id master_tu_id, impl::order_info_per_tu& tuoi )
  420. {
  421. test_unit& tu = framework::get( tu_id, TUT_ANY );
  422. // collect all sibling dependancy from tu own list
  423. BOOST_TEST_FOREACH( test_unit_id, dep_id, tu.p_dependencies.get() )
  424. collect_dependant_siblings( tu_id, dep_id, master_tu_id, tuoi );
  425. if( tu.p_type != TUT_SUITE )
  426. return;
  427. test_suite& ts = static_cast<test_suite&>(tu);
  428. // recursive call to children first
  429. BOOST_TEST_FOREACH( test_unit_id, chld_id, ts.m_children )
  430. deduce_siblings_order( chld_id, master_tu_id, tuoi );
  431. ts.m_ranked_children.clear();
  432. BOOST_TEST_FOREACH( test_unit_id, chld_id, ts.m_children ) {
  433. counter_t rank = assign_sibling_rank( chld_id, tuoi );
  434. ts.m_ranked_children.insert( std::make_pair( rank, chld_id ) );
  435. }
  436. }
  437. //////////////////////////////////////////////////////////////////
  438. // Finalize default run status:
  439. // 1) inherit run status from parent where applicable
  440. // 2) if any of test units in test suite enabled enable it as well
  441. bool finalize_default_run_status( test_unit_id tu_id, test_unit::run_status parent_status )
  442. {
  443. test_unit& tu = framework::get( tu_id, TUT_ANY );
  444. if( tu.p_default_status == test_suite::RS_INHERIT )
  445. tu.p_default_status.value = parent_status;
  446. // go through list of children
  447. if( tu.p_type == TUT_SUITE ) {
  448. bool has_enabled_child = false;
  449. BOOST_TEST_FOREACH( test_unit_id, chld_id, static_cast<test_suite const&>(tu).m_children )
  450. has_enabled_child |= finalize_default_run_status( chld_id, tu.p_default_status );
  451. tu.p_default_status.value = has_enabled_child ? test_suite::RS_ENABLED : test_suite::RS_DISABLED;
  452. }
  453. return tu.p_default_status == test_suite::RS_ENABLED;
  454. }
  455. //////////////////////////////////////////////////////////////////
  456. bool finalize_run_status( test_unit_id tu_id )
  457. {
  458. test_unit& tu = framework::get( tu_id, TUT_ANY );
  459. // go through list of children
  460. if( tu.p_type == TUT_SUITE ) {
  461. bool has_enabled_child = false;
  462. BOOST_TEST_FOREACH( test_unit_id, chld_id, static_cast<test_suite const&>(tu).m_children)
  463. has_enabled_child |= finalize_run_status( chld_id );
  464. tu.p_run_status.value = has_enabled_child ? test_suite::RS_ENABLED : test_suite::RS_DISABLED;
  465. }
  466. return tu.is_enabled();
  467. }
  468. //////////////////////////////////////////////////////////////////
  469. void deduce_run_status( test_unit_id master_tu_id )
  470. {
  471. using namespace framework::impl;
  472. test_unit_id_list tu_to_enable;
  473. test_unit_id_list tu_to_disable;
  474. // 10. If there are any filters supplied, figure out lists of test units to enable/disable
  475. bool had_selector_filter = !runtime_config::get<std::vector<std::string> >( runtime_config::btrt_run_filters ).empty() &&
  476. parse_filters( master_tu_id, tu_to_enable, tu_to_disable );
  477. // 20. Set the stage: either use default run status or disable all test units
  478. set_run_status initial_setter( had_selector_filter ? test_unit::RS_DISABLED : test_unit::RS_INVALID );
  479. traverse_test_tree( master_tu_id, initial_setter, true );
  480. // 30. Apply all selectors and enablers.
  481. while( !tu_to_enable.empty() ) {
  482. test_unit& tu = framework::get( tu_to_enable.back(), TUT_ANY );
  483. tu_to_enable.pop_back();
  484. // 35. Ignore test units which are already enabled
  485. if( tu.is_enabled() )
  486. continue;
  487. // set new status and add all dependencies into tu_to_enable
  488. set_run_status enabler( test_unit::RS_ENABLED, &tu_to_enable );
  489. traverse_test_tree( tu.p_id, enabler, true );
  490. // Add the dependencies of the parent suites, see trac #13149
  491. test_unit_id parent_id = tu.p_parent_id;
  492. while( parent_id != INV_TEST_UNIT_ID
  493. && parent_id != master_tu_id )
  494. {
  495. // we do not use the traverse_test_tree as otherwise it would enable the sibblings and subtree
  496. // of the test case we want to enable (we need to enable the parent suites and their dependencies only)
  497. // the parent_id needs to be enabled in order to be properly parsed by finalize_run_status, the visit
  498. // does the job
  499. test_unit& tu_parent = framework::get( parent_id, TUT_ANY );
  500. enabler.visit( tu_parent );
  501. parent_id = tu_parent.p_parent_id;
  502. }
  503. }
  504. // 40. Apply all disablers
  505. while( !tu_to_disable.empty() ) {
  506. test_unit const& tu = framework::get( tu_to_disable.back(), TUT_ANY );
  507. tu_to_disable.pop_back();
  508. // 35. Ignore test units which already disabled
  509. if( !tu.is_enabled() )
  510. continue;
  511. set_run_status disabler( test_unit::RS_DISABLED );
  512. traverse_test_tree( tu.p_id, disabler, true );
  513. }
  514. // 50. Make sure parents of enabled test units are also enabled
  515. finalize_run_status( master_tu_id );
  516. }
  517. //////////////////////////////////////////////////////////////////
  518. typedef unit_test_monitor_t::error_level execution_result;
  519. // Random generator using the std::rand function (seeded prior to the call)
  520. struct random_generator_helper {
  521. size_t operator()(size_t i) const {
  522. return std::rand() % i;
  523. }
  524. };
  525. // Executes the test tree with the root at specified test unit
  526. execution_result execute_test_tree( test_unit_id tu_id,
  527. unsigned timeout = 0,
  528. random_generator_helper const * const p_random_generator = 0)
  529. {
  530. test_unit const& tu = framework::get( tu_id, TUT_ANY );
  531. execution_result result = unit_test_monitor_t::test_ok;
  532. if( !tu.is_enabled() )
  533. return result;
  534. // 10. Check preconditions, including zero time left for execution and
  535. // successful execution of all dependencies
  536. if( timeout == TIMEOUT_EXCEEDED ) {
  537. // notify all observers about skipped test unit
  538. BOOST_TEST_FOREACH( test_observer*, to, m_observers )
  539. to->test_unit_skipped( tu, "timeout for the test unit is exceeded" );
  540. return unit_test_monitor_t::os_timeout;
  541. }
  542. else if( timeout == 0 || timeout > tu.p_timeout ) // deduce timeout for this test unit
  543. timeout = tu.p_timeout;
  544. test_tools::assertion_result const precondition_res = tu.check_preconditions();
  545. if( !precondition_res ) {
  546. // notify all observers about skipped test unit
  547. BOOST_TEST_FOREACH( test_observer*, to, m_observers )
  548. to->test_unit_skipped( tu, precondition_res.message() );
  549. // It is not an error to skip the test if any of the parent tests
  550. // have failed. This one should be reported as skipped as if it was
  551. // disabled
  552. return unit_test_monitor_t::test_ok;
  553. }
  554. // 20. Notify all observers about the start of the test unit
  555. BOOST_TEST_FOREACH( test_observer*, to, m_observers )
  556. to->test_unit_start( tu );
  557. // 30. Execute setup fixtures if any; any failure here leads to test unit abortion
  558. BOOST_TEST_FOREACH( test_unit_fixture_ptr, F, tu.p_fixtures.get() ) {
  559. ut_detail::test_unit_id_restore restore_current_test_unit(m_curr_test_unit, tu.p_id);
  560. result = unit_test_monitor.execute_and_translate( boost::bind( &test_unit_fixture::setup, F ) );
  561. if( result != unit_test_monitor_t::test_ok )
  562. break;
  563. test_results const& test_rslt = unit_test::results_collector.results( m_curr_test_unit );
  564. if( test_rslt.aborted() ) {
  565. result = unit_test_monitor_t::test_setup_failure;
  566. break;
  567. }
  568. }
  569. // This is the time we are going to spend executing the test unit
  570. unsigned long elapsed = 0;
  571. if( result == unit_test_monitor_t::test_ok ) {
  572. // 40. We are going to time the execution
  573. boost::timer tu_timer;
  574. // we pass the random generator
  575. const random_generator_helper& rand_gen = p_random_generator ? *p_random_generator : random_generator_helper();
  576. if( tu.p_type == TUT_SUITE ) {
  577. test_suite const& ts = static_cast<test_suite const&>( tu );
  578. if( runtime_config::get<unsigned>( runtime_config::btrt_random_seed ) == 0 ) {
  579. typedef std::pair<counter_t,test_unit_id> value_type;
  580. BOOST_TEST_FOREACH( value_type, chld, ts.m_ranked_children ) {
  581. unsigned chld_timeout = child_timeout( timeout, tu_timer.elapsed() );
  582. result = (std::min)( result, execute_test_tree( chld.second, chld_timeout, &rand_gen ) );
  583. if( unit_test_monitor.is_critical_error( result ) )
  584. break;
  585. }
  586. }
  587. else {
  588. // Go through ranges of children with the same dependency rank and shuffle them
  589. // independently. Execute each subtree in this order
  590. test_unit_id_list children_with_the_same_rank;
  591. typedef test_suite::children_per_rank::const_iterator it_type;
  592. it_type it = ts.m_ranked_children.begin();
  593. while( it != ts.m_ranked_children.end() ) {
  594. children_with_the_same_rank.clear();
  595. std::pair<it_type,it_type> range = ts.m_ranked_children.equal_range( it->first );
  596. it = range.first;
  597. while( it != range.second ) {
  598. children_with_the_same_rank.push_back( it->second );
  599. it++;
  600. }
  601. #ifdef BOOST_NO_CXX98_RANDOM_SHUFFLE
  602. impl::random_shuffle( children_with_the_same_rank.begin(), children_with_the_same_rank.end(), rand_gen );
  603. #else
  604. std::random_shuffle( children_with_the_same_rank.begin(), children_with_the_same_rank.end(), rand_gen );
  605. #endif
  606. BOOST_TEST_FOREACH( test_unit_id, chld, children_with_the_same_rank ) {
  607. unsigned chld_timeout = child_timeout( timeout, tu_timer.elapsed() );
  608. result = (std::min)( result, execute_test_tree( chld, chld_timeout, &rand_gen ) );
  609. if( unit_test_monitor.is_critical_error( result ) )
  610. break;
  611. }
  612. }
  613. }
  614. elapsed = static_cast<unsigned long>( tu_timer.elapsed() * 1e6 );
  615. }
  616. else { // TUT_CASE
  617. test_case const& tc = static_cast<test_case const&>( tu );
  618. // setup contexts
  619. m_context_idx = 0;
  620. // setup current test case
  621. ut_detail::test_unit_id_restore restore_current_test_unit(m_curr_test_unit, tc.p_id);
  622. // execute the test case body
  623. result = unit_test_monitor.execute_and_translate( tc.p_test_func, timeout );
  624. elapsed = static_cast<unsigned long>( tu_timer.elapsed() * 1e6 );
  625. // cleanup leftover context
  626. m_context.clear();
  627. // restore state (scope exit) and abort if necessary
  628. }
  629. }
  630. // if run error is critical skip teardown, who knows what the state of the program at this point
  631. if( !unit_test_monitor.is_critical_error( result ) ) {
  632. // execute teardown fixtures if any in reverse order
  633. BOOST_TEST_REVERSE_FOREACH( test_unit_fixture_ptr, F, tu.p_fixtures.get() ) {
  634. ut_detail::test_unit_id_restore restore_current_test_unit(m_curr_test_unit, tu.p_id);
  635. result = (std::min)( result, unit_test_monitor.execute_and_translate( boost::bind( &test_unit_fixture::teardown, F ), 0 ) );
  636. if( unit_test_monitor.is_critical_error( result ) )
  637. break;
  638. }
  639. }
  640. // notify all observers about abortion
  641. if( unit_test_monitor.is_critical_error( result ) ) {
  642. BOOST_TEST_FOREACH( test_observer*, to, m_observers )
  643. to->test_aborted();
  644. }
  645. // notify all observers about completion
  646. BOOST_TEST_REVERSE_FOREACH( test_observer*, to, m_observers )
  647. to->test_unit_finish( tu, elapsed );
  648. return result;
  649. }
  650. //////////////////////////////////////////////////////////////////
  651. unsigned child_timeout( unsigned tu_timeout, double elapsed )
  652. {
  653. if( tu_timeout == 0U )
  654. return 0U;
  655. unsigned elpsed_sec = static_cast<unsigned>(elapsed); // rounding to number of whole seconds
  656. return tu_timeout > elpsed_sec ? tu_timeout - elpsed_sec : TIMEOUT_EXCEEDED;
  657. }
  658. struct priority_order {
  659. bool operator()( test_observer* lhs, test_observer* rhs ) const
  660. {
  661. return (lhs->priority() < rhs->priority()) || ((lhs->priority() == rhs->priority()) && (lhs < rhs));
  662. }
  663. };
  664. // Data members
  665. typedef std::map<test_unit_id,test_unit*> test_unit_store;
  666. typedef std::set<test_observer*,priority_order> observer_store;
  667. struct context_frame {
  668. context_frame( std::string const& d, int id, bool sticky )
  669. : descr( d )
  670. , frame_id( id )
  671. , is_sticky( sticky )
  672. {}
  673. std::string descr;
  674. int frame_id;
  675. bool is_sticky;
  676. };
  677. typedef std::vector<context_frame> context_data;
  678. master_test_suite_t* m_master_test_suite;
  679. std::vector<test_suite*> m_auto_test_suites;
  680. test_unit_id m_curr_test_unit;
  681. test_unit_store m_test_units;
  682. test_unit_id m_next_test_case_id;
  683. test_unit_id m_next_test_suite_id;
  684. bool m_test_in_progress;
  685. observer_store m_observers;
  686. context_data m_context;
  687. int m_context_idx;
  688. std::set<test_unit_fixture*> m_global_fixtures;
  689. boost::execution_monitor m_aux_em;
  690. std::map<output_format, runtime_config::stream_holder> m_log_sinks;
  691. runtime_config::stream_holder m_report_sink;
  692. };
  693. //____________________________________________________________________________//
  694. namespace impl {
  695. namespace {
  696. #if defined(__CYGWIN__)
  697. framework::state& s_frk_state() { static framework::state* the_inst = 0; if(!the_inst) the_inst = new framework::state; return *the_inst; }
  698. #else
  699. framework::state& s_frk_state() { static framework::state the_inst; return the_inst; }
  700. #endif
  701. } // local namespace
  702. void
  703. setup_for_execution( test_unit const& tu )
  704. {
  705. s_frk_state().deduce_run_status( tu.p_id );
  706. }
  707. struct sum_to_first_only {
  708. sum_to_first_only() : is_first(true) {}
  709. template <class T, class U>
  710. T operator()(T const& l_, U const& r_) {
  711. if(is_first) {
  712. is_first = false;
  713. return l_ + r_.first;
  714. }
  715. return l_ + ", " + r_.first;
  716. }
  717. bool is_first;
  718. };
  719. void
  720. shutdown_loggers_and_reports()
  721. {
  722. s_frk_state().m_log_sinks.clear();
  723. s_frk_state().m_report_sink.setup( "stderr" );
  724. }
  725. void
  726. setup_loggers()
  727. {
  728. BOOST_TEST_I_TRY {
  729. #ifdef BOOST_TEST_SUPPORT_TOKEN_ITERATOR
  730. bool has_combined_logger = runtime_config::has( runtime_config::btrt_combined_logger )
  731. && !runtime_config::get< std::vector<std::string> >( runtime_config::btrt_combined_logger ).empty();
  732. #else
  733. bool has_combined_logger = false;
  734. #endif
  735. if( !has_combined_logger ) {
  736. unit_test_log.set_threshold_level( runtime_config::get<log_level>( runtime_config::btrt_log_level ) );
  737. const output_format format = runtime_config::get<output_format>( runtime_config::btrt_log_format );
  738. unit_test_log.set_format( format );
  739. runtime_config::stream_holder& stream_logger = s_frk_state().m_log_sinks[format];
  740. if( runtime_config::has( runtime_config::btrt_log_sink ) ) {
  741. // we remove all streams in this case, so we do not specify the format
  742. boost::function< void () > log_cleaner = boost::bind( &unit_test_log_t::set_stream,
  743. &unit_test_log,
  744. boost::ref(std::cout)
  745. );
  746. stream_logger.setup( runtime_config::get<std::string>( runtime_config::btrt_log_sink ),
  747. log_cleaner );
  748. }
  749. unit_test_log.set_stream( stream_logger.ref() );
  750. }
  751. else
  752. {
  753. const std::vector<std::string>& v_output_format = runtime_config::get< std::vector<std::string> >( runtime_config::btrt_combined_logger ) ;
  754. static const std::pair<const char*, log_level> all_log_levels[] = {
  755. std::make_pair( "all" , log_successful_tests ),
  756. std::make_pair( "success" , log_successful_tests ),
  757. std::make_pair( "test_suite" , log_test_units ),
  758. std::make_pair( "unit_scope" , log_test_units ),
  759. std::make_pair( "message" , log_messages ),
  760. std::make_pair( "warning" , log_warnings ),
  761. std::make_pair( "error" , log_all_errors ),
  762. std::make_pair( "cpp_exception" , log_cpp_exception_errors ),
  763. std::make_pair( "system_error" , log_system_errors ),
  764. std::make_pair( "fatal_error" , log_fatal_errors ),
  765. std::make_pair( "nothing" , log_nothing )
  766. };
  767. static const std::pair<const char*, output_format> all_formats[] = {
  768. std::make_pair( "HRF" , OF_CLF ),
  769. std::make_pair( "CLF" , OF_CLF ),
  770. std::make_pair( "XML" , OF_XML ),
  771. std::make_pair( "JUNIT", OF_JUNIT )
  772. };
  773. bool is_first = true;
  774. BOOST_TEST_FOREACH( const_string, current_multi_config, v_output_format ) {
  775. #ifdef BOOST_TEST_SUPPORT_TOKEN_ITERATOR
  776. // ':' may be used for file names: C:/tmp/mylogsink.xml
  777. // we merge the tokens that start with / or \ with the previous one.
  778. std::vector<std::string> v_processed_tokens;
  779. {
  780. utils::string_token_iterator current_config( current_multi_config, (utils::dropped_delimeters = ":",
  781. utils::kept_delimeters = utils::dt_none) );
  782. for( ; current_config != utils::string_token_iterator() ; ++current_config) {
  783. std::string str_copy(current_config->begin(), current_config->end());
  784. if( ( str_copy[0] == '\\' || str_copy[0] == '/' )
  785. && v_processed_tokens.size() > 0) {
  786. v_processed_tokens.back() += ":" + str_copy; // ':' has been eaten up
  787. }
  788. else {
  789. v_processed_tokens.push_back(str_copy);
  790. }
  791. }
  792. }
  793. BOOST_TEST_FOREACH( std::string const&, current_config, v_processed_tokens ) {
  794. utils::string_token_iterator current_format_specs( current_config, (utils::keep_empty_tokens,
  795. utils::dropped_delimeters = ",",
  796. utils::kept_delimeters = utils::dt_none) );
  797. output_format format = OF_INVALID ; // default
  798. if( current_format_specs != utils::string_token_iterator() &&
  799. current_format_specs->size() ) {
  800. for(size_t elem=0; elem < sizeof(all_formats)/sizeof(all_formats[0]); elem++) {
  801. if(const_string(all_formats[elem].first) == *current_format_specs) {
  802. format = all_formats[elem].second;
  803. break;
  804. }
  805. }
  806. }
  807. BOOST_TEST_I_ASSRT( format != OF_INVALID,
  808. boost::runtime::access_to_missing_argument()
  809. << "Unable to determine the logger type from '"
  810. << current_config
  811. << "'. Possible choices are: "
  812. << std::accumulate(all_formats,
  813. all_formats + sizeof(all_formats)/sizeof(all_formats[0]),
  814. std::string(""),
  815. sum_to_first_only())
  816. );
  817. // activates this format
  818. if( is_first ) {
  819. unit_test_log.set_format( format );
  820. }
  821. else {
  822. unit_test_log.add_format( format );
  823. }
  824. is_first = false;
  825. unit_test_log_formatter * const formatter = unit_test_log.get_formatter(format);
  826. BOOST_TEST_SETUP_ASSERT( formatter, "Logger setup error" );
  827. log_level formatter_log_level = invalid_log_level;
  828. ++current_format_specs ;
  829. if( !current_format_specs->size() ) {
  830. formatter_log_level = formatter->get_log_level(); // default log level given by the formatter
  831. }
  832. else if( current_format_specs != utils::string_token_iterator() ) {
  833. for(size_t elem=0; elem < sizeof(all_log_levels)/sizeof(all_log_levels[0]); elem++) {
  834. if(const_string(all_log_levels[elem].first) == *current_format_specs) {
  835. formatter_log_level = all_log_levels[elem].second;
  836. break;
  837. }
  838. }
  839. }
  840. BOOST_TEST_I_ASSRT( formatter_log_level != invalid_log_level,
  841. boost::runtime::access_to_missing_argument()
  842. << "Unable to determine the log level from '"
  843. << current_config
  844. << "'. Possible choices are: "
  845. << std::accumulate(all_log_levels,
  846. all_log_levels + sizeof(all_log_levels)/sizeof(all_log_levels[0]),
  847. std::string(""),
  848. sum_to_first_only())
  849. );
  850. unit_test_log.set_threshold_level( format, formatter_log_level );
  851. runtime_config::stream_holder& stream_logger = s_frk_state().m_log_sinks[format];
  852. boost::function< void () > log_cleaner = boost::bind( &unit_test_log_t::set_stream,
  853. &unit_test_log,
  854. format,
  855. boost::ref(std::cout) );
  856. if( ++current_format_specs != utils::string_token_iterator() &&
  857. current_format_specs->size() ) {
  858. stream_logger.setup( *current_format_specs,
  859. log_cleaner );
  860. }
  861. else {
  862. stream_logger.setup( formatter->get_default_stream_description(),
  863. log_cleaner );
  864. }
  865. unit_test_log.set_stream( format, stream_logger.ref() );
  866. }
  867. #endif
  868. } // for each logger
  869. } // if/else new logger API
  870. } // BOOST_TEST_I_TRY
  871. BOOST_TEST_I_CATCH( boost::runtime::init_error, ex ) {
  872. BOOST_TEST_SETUP_ASSERT( false, ex.msg );
  873. }
  874. BOOST_TEST_I_CATCH( boost::runtime::input_error, ex ) {
  875. std::cerr << ex.msg << "\n\n";
  876. BOOST_TEST_I_THROW( framework::nothing_to_test( boost::exit_exception_failure ) );
  877. }
  878. }
  879. //____________________________________________________________________________//
  880. } // namespace impl
  881. //____________________________________________________________________________//
  882. // ************************************************************************** //
  883. // ************** framework::init ************** //
  884. // ************************************************************************** //
  885. void
  886. init( init_unit_test_func init_func, int argc, char* argv[] )
  887. {
  888. using namespace impl;
  889. // 10. Set up runtime parameters
  890. runtime_config::init( argc, argv );
  891. // 20. Set the desired log level, format and sink
  892. impl::setup_loggers();
  893. // 30. Set the desired report level, format and sink
  894. results_reporter::set_level( runtime_config::get<report_level>( runtime_config::btrt_report_level ) );
  895. results_reporter::set_format( runtime_config::get<output_format>( runtime_config::btrt_report_format ) );
  896. if( runtime_config::has( runtime_config::btrt_report_sink ) ) {
  897. boost::function< void () > report_cleaner = boost::bind( &results_reporter::set_stream,
  898. boost::ref(std::cerr)
  899. );
  900. s_frk_state().m_report_sink.setup( runtime_config::get<std::string>( runtime_config::btrt_report_sink ),
  901. report_cleaner );
  902. }
  903. results_reporter::set_stream( s_frk_state().m_report_sink.ref() );
  904. // 40. Register default test observers
  905. register_observer( results_collector );
  906. register_observer( unit_test_log );
  907. register_observer( framework_init_observer );
  908. if( runtime_config::get<bool>( runtime_config::btrt_show_progress ) ) {
  909. progress_monitor.set_stream( std::cout ); // defaults to stdout
  910. register_observer( progress_monitor );
  911. }
  912. // 50. Set up memory leak detection
  913. unsigned long detect_mem_leak = runtime_config::get<unsigned long>( runtime_config::btrt_detect_mem_leaks );
  914. if( detect_mem_leak > 0 ) {
  915. debug::detect_memory_leaks( true, runtime_config::get<std::string>( runtime_config::btrt_report_mem_leaks ) );
  916. debug::break_memory_alloc( (long)detect_mem_leak );
  917. }
  918. // 60. Initialize master unit test suite
  919. master_test_suite().argc = argc;
  920. master_test_suite().argv = argv;
  921. // 70. Invoke test module initialization routine
  922. BOOST_TEST_I_TRY {
  923. s_frk_state().m_aux_em.vexecute( boost::bind( &impl::invoke_init_func, init_func ) );
  924. }
  925. BOOST_TEST_I_CATCH( execution_exception, ex ) {
  926. BOOST_TEST_SETUP_ASSERT( false, ex.what() );
  927. }
  928. }
  929. //____________________________________________________________________________//
  930. void
  931. finalize_setup_phase( test_unit_id master_tu_id )
  932. {
  933. if( master_tu_id == INV_TEST_UNIT_ID )
  934. master_tu_id = master_test_suite().p_id;
  935. // 10. Apply all decorators to the auto test units
  936. class apply_decorators : public test_tree_visitor {
  937. private:
  938. // test_tree_visitor interface
  939. virtual bool test_suite_start( test_suite const& ts)
  940. {
  941. const_cast<test_suite&>(ts).generate();
  942. return test_tree_visitor::test_suite_start(ts);
  943. }
  944. virtual bool visit( test_unit const& tu )
  945. {
  946. BOOST_TEST_FOREACH( decorator::base_ptr, d, tu.p_decorators.get() )
  947. d->apply( const_cast<test_unit&>(tu) );
  948. return true;
  949. }
  950. } ad;
  951. traverse_test_tree( master_tu_id, ad, true );
  952. // 20. Finalize setup phase
  953. impl::order_info_per_tu tuoi;
  954. impl::s_frk_state().deduce_siblings_order( master_tu_id, master_tu_id, tuoi );
  955. impl::s_frk_state().finalize_default_run_status( master_tu_id, test_unit::RS_INVALID );
  956. }
  957. // ************************************************************************** //
  958. // ************** test_in_progress ************** //
  959. // ************************************************************************** //
  960. bool
  961. test_in_progress()
  962. {
  963. return impl::s_frk_state().m_test_in_progress;
  964. }
  965. //____________________________________________________________________________//
  966. // ************************************************************************** //
  967. // ************** framework::shutdown ************** //
  968. // ************************************************************************** //
  969. void
  970. shutdown()
  971. {
  972. impl::shutdown_loggers_and_reports();
  973. // eliminating some fake memory leak reports. See for more details:
  974. // http://connect.microsoft.com/VisualStudio/feedback/details/106937/memory-leaks-reported-by-debug-crt-inside-typeinfo-name
  975. # if BOOST_WORKAROUND(BOOST_MSVC, <= 1600 ) && !defined(_DLL) && defined(_DEBUG)
  976. # if BOOST_WORKAROUND(BOOST_MSVC, < 1600 )
  977. #define _Next next
  978. #define _MemPtr memPtr
  979. #endif
  980. __type_info_node* pNode = __type_info_root_node._Next;
  981. __type_info_node* tmpNode = &__type_info_root_node;
  982. for( ; pNode!=NULL; pNode = tmpNode ) {
  983. tmpNode = pNode->_Next;
  984. delete pNode->_MemPtr;
  985. delete pNode;
  986. }
  987. # if BOOST_WORKAROUND(BOOST_MSVC, < 1600 )
  988. #undef _Next
  989. #undef _MemPtr
  990. #endif
  991. # endif
  992. }
  993. //____________________________________________________________________________//
  994. // ************************************************************************** //
  995. // ************** register_test_unit ************** //
  996. // ************************************************************************** //
  997. void
  998. register_test_unit( test_case* tc )
  999. {
  1000. BOOST_TEST_SETUP_ASSERT( tc->p_id == INV_TEST_UNIT_ID, BOOST_TEST_L( "test case already registered" ) );
  1001. test_unit_id new_id = impl::s_frk_state().m_next_test_case_id;
  1002. BOOST_TEST_SETUP_ASSERT( new_id != MAX_TEST_CASE_ID, BOOST_TEST_L( "too many test cases" ) );
  1003. typedef state::test_unit_store::value_type map_value_type;
  1004. impl::s_frk_state().m_test_units.insert( map_value_type( new_id, tc ) );
  1005. impl::s_frk_state().m_next_test_case_id++;
  1006. impl::s_frk_state().set_tu_id( *tc, new_id );
  1007. }
  1008. //____________________________________________________________________________//
  1009. // ************************************************************************** //
  1010. // ************** register_test_unit ************** //
  1011. // ************************************************************************** //
  1012. void
  1013. register_test_unit( test_suite* ts )
  1014. {
  1015. BOOST_TEST_SETUP_ASSERT( ts->p_id == INV_TEST_UNIT_ID, BOOST_TEST_L( "test suite already registered" ) );
  1016. test_unit_id new_id = impl::s_frk_state().m_next_test_suite_id;
  1017. BOOST_TEST_SETUP_ASSERT( new_id != MAX_TEST_SUITE_ID, BOOST_TEST_L( "too many test suites" ) );
  1018. typedef state::test_unit_store::value_type map_value_type;
  1019. impl::s_frk_state().m_test_units.insert( map_value_type( new_id, ts ) );
  1020. impl::s_frk_state().m_next_test_suite_id++;
  1021. impl::s_frk_state().set_tu_id( *ts, new_id );
  1022. }
  1023. //____________________________________________________________________________//
  1024. // ************************************************************************** //
  1025. // ************** deregister_test_unit ************** //
  1026. // ************************************************************************** //
  1027. void
  1028. deregister_test_unit( test_unit* tu )
  1029. {
  1030. impl::s_frk_state().m_test_units.erase( tu->p_id );
  1031. }
  1032. //____________________________________________________________________________//
  1033. // ************************************************************************** //
  1034. // ************** clear ************** //
  1035. // ************************************************************************** //
  1036. void
  1037. clear()
  1038. {
  1039. impl::s_frk_state().clear();
  1040. }
  1041. //____________________________________________________________________________//
  1042. // ************************************************************************** //
  1043. // ************** register_observer ************** //
  1044. // ************************************************************************** //
  1045. void
  1046. register_observer( test_observer& to )
  1047. {
  1048. impl::s_frk_state().m_observers.insert( &to );
  1049. }
  1050. //____________________________________________________________________________//
  1051. // ************************************************************************** //
  1052. // ************** deregister_observer ************** //
  1053. // ************************************************************************** //
  1054. void
  1055. deregister_observer( test_observer& to )
  1056. {
  1057. impl::s_frk_state().m_observers.erase( &to );
  1058. }
  1059. //____________________________________________________________________________//
  1060. // ************************************************************************** //
  1061. // ************** register_global_fixture ************** //
  1062. // ************************************************************************** //
  1063. void
  1064. register_global_fixture( test_unit_fixture& tuf )
  1065. {
  1066. impl::s_frk_state().m_global_fixtures.insert( &tuf );
  1067. }
  1068. //____________________________________________________________________________//
  1069. // ************************************************************************** //
  1070. // ************** deregister_global_fixture ************** //
  1071. // ************************************************************************** //
  1072. void
  1073. deregister_global_fixture( test_unit_fixture &tuf )
  1074. {
  1075. impl::s_frk_state().m_global_fixtures.erase( &tuf );
  1076. }
  1077. //____________________________________________________________________________//
  1078. // ************************************************************************** //
  1079. // ************** add_context ************** //
  1080. // ************************************************************************** //
  1081. int
  1082. add_context( ::boost::unit_test::lazy_ostream const& context_descr, bool sticky )
  1083. {
  1084. std::stringstream buffer;
  1085. context_descr( buffer );
  1086. int res_idx = impl::s_frk_state().m_context_idx++;
  1087. impl::s_frk_state().m_context.push_back( state::context_frame( buffer.str(), res_idx, sticky ) );
  1088. return res_idx;
  1089. }
  1090. //____________________________________________________________________________//
  1091. // ************************************************************************** //
  1092. // ************** clear_context ************** //
  1093. // ************************************************************************** //
  1094. struct frame_with_id {
  1095. explicit frame_with_id( int id ) : m_id( id ) {}
  1096. bool operator()( state::context_frame const& f )
  1097. {
  1098. return f.frame_id == m_id;
  1099. }
  1100. int m_id;
  1101. };
  1102. //____________________________________________________________________________//
  1103. void
  1104. clear_context( int frame_id )
  1105. {
  1106. if( frame_id == -1 ) { // clear all non sticky frames
  1107. for( int i=static_cast<int>(impl::s_frk_state().m_context.size())-1; i>=0; i-- )
  1108. if( !impl::s_frk_state().m_context[i].is_sticky )
  1109. impl::s_frk_state().m_context.erase( impl::s_frk_state().m_context.begin()+i );
  1110. }
  1111. else { // clear specific frame
  1112. state::context_data::iterator it =
  1113. std::find_if( impl::s_frk_state().m_context.begin(), impl::s_frk_state().m_context.end(), frame_with_id( frame_id ) );
  1114. if( it != impl::s_frk_state().m_context.end() ) // really an internal error if this is not true
  1115. impl::s_frk_state().m_context.erase( it );
  1116. }
  1117. }
  1118. //____________________________________________________________________________//
  1119. // ************************************************************************** //
  1120. // ************** get_context ************** //
  1121. // ************************************************************************** //
  1122. context_generator
  1123. get_context()
  1124. {
  1125. return context_generator();
  1126. }
  1127. //____________________________________________________________________________//
  1128. // ************************************************************************** //
  1129. // ************** context_generator ************** //
  1130. // ************************************************************************** //
  1131. bool
  1132. context_generator::is_empty() const
  1133. {
  1134. return impl::s_frk_state().m_context.empty();
  1135. }
  1136. //____________________________________________________________________________//
  1137. const_string
  1138. context_generator::next() const
  1139. {
  1140. return m_curr_frame < impl::s_frk_state().m_context.size() ? impl::s_frk_state().m_context[m_curr_frame++].descr : const_string();
  1141. }
  1142. //____________________________________________________________________________//
  1143. // ************************************************************************** //
  1144. // ************** master_test_suite ************** //
  1145. // ************************************************************************** //
  1146. master_test_suite_t&
  1147. master_test_suite()
  1148. {
  1149. if( !impl::s_frk_state().m_master_test_suite )
  1150. impl::s_frk_state().m_master_test_suite = new master_test_suite_t;
  1151. return *impl::s_frk_state().m_master_test_suite;
  1152. }
  1153. //____________________________________________________________________________//
  1154. // ************************************************************************** //
  1155. // ************** current_auto_test_suite ************** //
  1156. // ************************************************************************** //
  1157. test_suite&
  1158. current_auto_test_suite( test_suite* ts, bool push_or_pop )
  1159. {
  1160. if( impl::s_frk_state().m_auto_test_suites.empty() )
  1161. impl::s_frk_state().m_auto_test_suites.push_back( &framework::master_test_suite() );
  1162. if( !push_or_pop )
  1163. impl::s_frk_state().m_auto_test_suites.pop_back();
  1164. else if( ts )
  1165. impl::s_frk_state().m_auto_test_suites.push_back( ts );
  1166. return *impl::s_frk_state().m_auto_test_suites.back();
  1167. }
  1168. //____________________________________________________________________________//
  1169. // ************************************************************************** //
  1170. // ************** current_test_case ************** //
  1171. // ************************************************************************** //
  1172. test_case const&
  1173. current_test_case()
  1174. {
  1175. return get<test_case>( impl::s_frk_state().m_curr_test_unit );
  1176. }
  1177. test_unit const&
  1178. current_test_unit()
  1179. {
  1180. return *impl::s_frk_state().m_test_units[impl::s_frk_state().m_curr_test_unit];
  1181. }
  1182. //____________________________________________________________________________//
  1183. test_unit_id
  1184. current_test_case_id()
  1185. {
  1186. return impl::s_frk_state().m_curr_test_unit;
  1187. }
  1188. //____________________________________________________________________________//
  1189. // ************************************************************************** //
  1190. // ************** framework::get ************** //
  1191. // ************************************************************************** //
  1192. test_unit&
  1193. get( test_unit_id id, test_unit_type t )
  1194. {
  1195. test_unit* res = impl::s_frk_state().m_test_units[id];
  1196. BOOST_TEST_I_ASSRT( (res->p_type & t) != 0, internal_error( "Invalid test unit type" ) );
  1197. return *res;
  1198. }
  1199. //____________________________________________________________________________//
  1200. // ************************************************************************** //
  1201. // ************** framework::run ************** //
  1202. // ************************************************************************** //
  1203. template <class Cont>
  1204. struct swap_on_delete {
  1205. swap_on_delete(Cont& c1, Cont& c2) : m_c1(c1), m_c2(c2){}
  1206. ~swap_on_delete() {
  1207. m_c1.swap(m_c2);
  1208. }
  1209. Cont& m_c1;
  1210. Cont& m_c2;
  1211. };
  1212. void
  1213. run( test_unit_id id, bool continue_test )
  1214. {
  1215. if( id == INV_TEST_UNIT_ID )
  1216. id = master_test_suite().p_id;
  1217. // Figure out run status for execution phase
  1218. impl::s_frk_state().deduce_run_status( id );
  1219. test_case_counter tcc;
  1220. traverse_test_tree( id, tcc );
  1221. BOOST_TEST_SETUP_ASSERT( tcc.p_count != 0 , runtime_config::get<std::vector<std::string> >( runtime_config::btrt_run_filters ).empty()
  1222. ? BOOST_TEST_L( "test tree is empty" )
  1223. : BOOST_TEST_L( "no test cases matching filter or all test cases were disabled" ) );
  1224. bool was_in_progress = framework::test_in_progress();
  1225. bool call_start_finish = !continue_test || !was_in_progress;
  1226. bool init_ok = true;
  1227. const_string setup_error;
  1228. if( call_start_finish ) {
  1229. // indicates the framework that no test is in progress now if observers need to be notified
  1230. impl::s_frk_state().m_test_in_progress = false;
  1231. // unit_test::framework_init_observer will get cleared first
  1232. BOOST_TEST_FOREACH( test_observer*, to, impl::s_frk_state().m_observers ) {
  1233. BOOST_TEST_I_TRY {
  1234. ut_detail::test_unit_id_restore restore_current_test_unit(impl::s_frk_state().m_curr_test_unit, id);
  1235. unit_test_monitor_t::error_level result = unit_test_monitor.execute_and_translate( boost::bind( &test_observer::test_start, to, tcc.p_count ) );
  1236. if( init_ok ) {
  1237. if( result != unit_test_monitor_t::test_ok ) {
  1238. init_ok = false;
  1239. }
  1240. else {
  1241. if( unit_test::framework_init_observer.has_failed() ) {
  1242. init_ok = false;
  1243. }
  1244. }
  1245. }
  1246. }
  1247. BOOST_TEST_I_CATCH( execution_exception, ex ) {
  1248. if( init_ok ) {
  1249. // log only the first error
  1250. init_ok = false;
  1251. setup_error = ex.what();
  1252. }
  1253. // break; // we should continue otherwise loggers may have improper structure (XML start missing for instance)
  1254. }
  1255. }
  1256. }
  1257. if( init_ok ) {
  1258. // attaching the global fixtures to the main entry point
  1259. test_unit& entry_test_unit = framework::get( id, TUT_ANY );
  1260. std::vector<test_unit_fixture_ptr> v_saved_fixture(entry_test_unit.p_fixtures.value.begin(),
  1261. entry_test_unit.p_fixtures.value.end());
  1262. BOOST_TEST_FOREACH( test_unit_fixture*, tuf, impl::s_frk_state().m_global_fixtures ) {
  1263. entry_test_unit.p_fixtures.value.insert( entry_test_unit.p_fixtures.value.begin(),
  1264. test_unit_fixture_ptr(new impl::global_fixture_handle(tuf)) );
  1265. }
  1266. swap_on_delete< std::vector<test_unit_fixture_ptr> > raii_fixture(v_saved_fixture, entry_test_unit.p_fixtures.value);
  1267. // now work in progress
  1268. impl::s_frk_state().m_test_in_progress = true;
  1269. unsigned seed = runtime_config::get<unsigned>( runtime_config::btrt_random_seed );
  1270. switch( seed ) {
  1271. case 0:
  1272. break;
  1273. case 1:
  1274. seed = static_cast<unsigned>( std::rand() ^ std::time( 0 ) ); // better init using std::rand() ^ ...
  1275. BOOST_FALLTHROUGH;
  1276. default:
  1277. BOOST_TEST_FRAMEWORK_MESSAGE( "Test cases order is shuffled using seed: " << seed );
  1278. std::srand( seed );
  1279. }
  1280. // executing the test tree
  1281. impl::s_frk_state().execute_test_tree( id );
  1282. // removing previously added global fixtures: dtor raii_fixture
  1283. }
  1284. impl::s_frk_state().m_test_in_progress = false;
  1285. results_reporter::make_report( INV_REPORT_LEVEL, id );
  1286. unit_test::framework_init_observer.clear();
  1287. if( call_start_finish ) {
  1288. // indicates the framework that no test is in progress anymore if observers need to be notified
  1289. // and this is a teardown, so assertions should not raise any exception otherwise an exception
  1290. // might be raised in a dtor of a global fixture
  1291. impl::s_frk_state().m_test_in_progress = false;
  1292. BOOST_TEST_REVERSE_FOREACH( test_observer*, to, impl::s_frk_state().m_observers ) {
  1293. ut_detail::test_unit_id_restore restore_current_test_unit(impl::s_frk_state().m_curr_test_unit, id);
  1294. to->test_finish();
  1295. }
  1296. }
  1297. impl::s_frk_state().m_test_in_progress = was_in_progress;
  1298. // propagates the init/teardown error if any
  1299. BOOST_TEST_SETUP_ASSERT( init_ok && !unit_test::framework_init_observer.has_failed(), setup_error );
  1300. }
  1301. //____________________________________________________________________________//
  1302. void
  1303. run( test_unit const* tu, bool continue_test )
  1304. {
  1305. run( tu->p_id, continue_test );
  1306. }
  1307. //____________________________________________________________________________//
  1308. // ************************************************************************** //
  1309. // ************** assertion_result ************** //
  1310. // ************************************************************************** //
  1311. void
  1312. assertion_result( unit_test::assertion_result ar )
  1313. {
  1314. BOOST_TEST_FOREACH( test_observer*, to, impl::s_frk_state().m_observers )
  1315. to->assertion_result( ar );
  1316. }
  1317. //____________________________________________________________________________//
  1318. // ************************************************************************** //
  1319. // ************** exception_caught ************** //
  1320. // ************************************************************************** //
  1321. void
  1322. exception_caught( execution_exception const& ex )
  1323. {
  1324. BOOST_TEST_FOREACH( test_observer*, to, impl::s_frk_state().m_observers )
  1325. to->exception_caught( ex );
  1326. }
  1327. //____________________________________________________________________________//
  1328. // ************************************************************************** //
  1329. // ************** test_unit_aborted ************** //
  1330. // ************************************************************************** //
  1331. void
  1332. test_unit_aborted( test_unit const& tu )
  1333. {
  1334. BOOST_TEST_FOREACH( test_observer*, to, impl::s_frk_state().m_observers )
  1335. to->test_unit_aborted( tu );
  1336. }
  1337. // ************************************************************************** //
  1338. // ************** test_aborted ************** //
  1339. // ************************************************************************** //
  1340. void
  1341. test_aborted( )
  1342. {
  1343. BOOST_TEST_FOREACH( test_observer*, to, impl::s_frk_state().m_observers )
  1344. to->test_aborted( );
  1345. }
  1346. //____________________________________________________________________________//
  1347. } // namespace framework
  1348. } // namespace unit_test
  1349. } // namespace boost
  1350. #include <boost/test/detail/enable_warnings.hpp>
  1351. #endif // BOOST_TEST_FRAMEWORK_IPP_021005GER