framework.ipp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. // (C) Copyright Gennadiy Rozental 2005-2008.
  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: 57991 $
  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_suite_impl.hpp>
  20. #include <boost/test/unit_test_log.hpp>
  21. #include <boost/test/unit_test_monitor.hpp>
  22. #include <boost/test/test_observer.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_tools.hpp>
  27. #include <boost/test/detail/unit_test_parameters.hpp>
  28. #include <boost/test/detail/global_typedef.hpp>
  29. #include <boost/test/utils/foreach.hpp>
  30. // Boost
  31. #include <boost/timer.hpp>
  32. // STL
  33. #include <map>
  34. #include <set>
  35. #include <cstdlib>
  36. #include <ctime>
  37. #ifdef BOOST_NO_STDC_NAMESPACE
  38. namespace std { using ::time; using ::srand; }
  39. #endif
  40. #include <boost/test/detail/suppress_warnings.hpp>
  41. //____________________________________________________________________________//
  42. namespace boost {
  43. namespace unit_test {
  44. // ************************************************************************** //
  45. // ************** test_start calls wrapper ************** //
  46. // ************************************************************************** //
  47. namespace ut_detail {
  48. struct test_start_caller {
  49. test_start_caller( test_observer* to, counter_t tc_amount )
  50. : m_to( to )
  51. , m_tc_amount( tc_amount )
  52. {}
  53. int operator()()
  54. {
  55. m_to->test_start( m_tc_amount );
  56. return 0;
  57. }
  58. private:
  59. // Data members
  60. test_observer* m_to;
  61. counter_t m_tc_amount;
  62. };
  63. //____________________________________________________________________________//
  64. struct test_init_caller {
  65. explicit test_init_caller( init_unit_test_func init_func )
  66. : m_init_func( init_func )
  67. {}
  68. int operator()()
  69. {
  70. #ifdef BOOST_TEST_ALTERNATIVE_INIT_API
  71. if( !(*m_init_func)() )
  72. throw std::runtime_error( "test module initialization failed" );
  73. #else
  74. test_suite* manual_test_units = (*m_init_func)( framework::master_test_suite().argc, framework::master_test_suite().argv );
  75. if( manual_test_units )
  76. framework::master_test_suite().add( manual_test_units );
  77. #endif
  78. return 0;
  79. }
  80. // Data members
  81. init_unit_test_func m_init_func;
  82. };
  83. }
  84. // ************************************************************************** //
  85. // ************** framework ************** //
  86. // ************************************************************************** //
  87. class framework_impl : public test_tree_visitor {
  88. public:
  89. framework_impl()
  90. : m_master_test_suite( 0 )
  91. , m_curr_test_case( INV_TEST_UNIT_ID )
  92. , m_next_test_case_id( MIN_TEST_CASE_ID )
  93. , m_next_test_suite_id( MIN_TEST_SUITE_ID )
  94. , m_is_initialized( false )
  95. , m_test_in_progress( false )
  96. {}
  97. ~framework_impl() { clear(); }
  98. void clear()
  99. {
  100. while( !m_test_units.empty() ) {
  101. test_unit_store::value_type const& tu = *m_test_units.begin();
  102. test_unit* tu_ptr = tu.second;
  103. // the delete will erase this element from map
  104. if( ut_detail::test_id_2_unit_type( tu.second->p_id ) == tut_suite )
  105. delete (test_suite const*)tu_ptr;
  106. else
  107. delete (test_case const*)tu_ptr;
  108. }
  109. }
  110. void set_tu_id( test_unit& tu, test_unit_id id ) { tu.p_id.value = id; }
  111. // test_tree_visitor interface implementation
  112. void visit( test_case const& tc )
  113. {
  114. if( !tc.check_dependencies() ) {
  115. BOOST_TEST_FOREACH( test_observer*, to, m_observers )
  116. to->test_unit_skipped( tc );
  117. return;
  118. }
  119. BOOST_TEST_FOREACH( test_observer*, to, m_observers )
  120. to->test_unit_start( tc );
  121. boost::timer tc_timer;
  122. test_unit_id bkup = m_curr_test_case;
  123. m_curr_test_case = tc.p_id;
  124. unit_test_monitor_t::error_level run_result = unit_test_monitor.execute_and_translate( tc );
  125. unsigned long elapsed = static_cast<unsigned long>( tc_timer.elapsed() * 1e6 );
  126. if( unit_test_monitor.is_critical_error( run_result ) ) {
  127. BOOST_TEST_FOREACH( test_observer*, to, m_observers )
  128. to->test_aborted();
  129. }
  130. BOOST_TEST_FOREACH( test_observer*, to, m_observers )
  131. to->test_unit_finish( tc, elapsed );
  132. m_curr_test_case = bkup;
  133. if( unit_test_monitor.is_critical_error( run_result ) )
  134. throw test_being_aborted();
  135. }
  136. bool test_suite_start( test_suite const& ts )
  137. {
  138. if( !ts.check_dependencies() ) {
  139. BOOST_TEST_FOREACH( test_observer*, to, m_observers )
  140. to->test_unit_skipped( ts );
  141. return false;
  142. }
  143. BOOST_TEST_FOREACH( test_observer*, to, m_observers )
  144. to->test_unit_start( ts );
  145. return true;
  146. }
  147. void test_suite_finish( test_suite const& ts )
  148. {
  149. BOOST_TEST_FOREACH( test_observer*, to, m_observers )
  150. to->test_unit_finish( ts, 0 );
  151. }
  152. //////////////////////////////////////////////////////////////////
  153. struct priority_order {
  154. bool operator()( test_observer* lhs, test_observer* rhs ) const
  155. {
  156. return (lhs->priority() < rhs->priority()) || ((lhs->priority() == rhs->priority()) && (lhs < rhs));
  157. }
  158. };
  159. typedef std::map<test_unit_id,test_unit*> test_unit_store;
  160. typedef std::set<test_observer*,priority_order> observer_store;
  161. master_test_suite_t* m_master_test_suite;
  162. test_unit_id m_curr_test_case;
  163. test_unit_store m_test_units;
  164. test_unit_id m_next_test_case_id;
  165. test_unit_id m_next_test_suite_id;
  166. bool m_is_initialized;
  167. bool m_test_in_progress;
  168. observer_store m_observers;
  169. };
  170. //____________________________________________________________________________//
  171. namespace {
  172. #if defined(__CYGWIN__)
  173. framework_impl& s_frk_impl() { static framework_impl* the_inst = 0; if(!the_inst) the_inst = new framework_impl; return *the_inst; }
  174. #else
  175. framework_impl& s_frk_impl() { static framework_impl the_inst; return the_inst; }
  176. #endif
  177. } // local namespace
  178. //____________________________________________________________________________//
  179. namespace framework {
  180. void
  181. init( init_unit_test_func init_func, int argc, char* argv[] )
  182. {
  183. runtime_config::init( argc, argv );
  184. // set the log level and format
  185. unit_test_log.set_threshold_level( runtime_config::log_level() );
  186. unit_test_log.set_format( runtime_config::log_format() );
  187. // set the report level and format
  188. results_reporter::set_level( runtime_config::report_level() );
  189. results_reporter::set_format( runtime_config::report_format() );
  190. register_observer( results_collector );
  191. register_observer( unit_test_log );
  192. if( runtime_config::show_progress() )
  193. register_observer( progress_monitor );
  194. if( runtime_config::detect_memory_leaks() > 0 ) {
  195. debug::detect_memory_leaks( true );
  196. debug::break_memory_alloc( runtime_config::detect_memory_leaks() );
  197. }
  198. // init master unit test suite
  199. master_test_suite().argc = argc;
  200. master_test_suite().argv = argv;
  201. try {
  202. boost::execution_monitor em;
  203. ut_detail::test_init_caller tic( init_func );
  204. em.execute( tic );
  205. }
  206. catch( execution_exception const& ex ) {
  207. throw setup_error( ex.what() );
  208. }
  209. s_frk_impl().m_is_initialized = true;
  210. }
  211. //____________________________________________________________________________//
  212. bool
  213. is_initialized()
  214. {
  215. return s_frk_impl().m_is_initialized;
  216. }
  217. //____________________________________________________________________________//
  218. void
  219. register_test_unit( test_case* tc )
  220. {
  221. BOOST_TEST_SETUP_ASSERT( tc->p_id == INV_TEST_UNIT_ID, BOOST_TEST_L( "test case already registered" ) );
  222. test_unit_id new_id = s_frk_impl().m_next_test_case_id;
  223. BOOST_TEST_SETUP_ASSERT( new_id != MAX_TEST_CASE_ID, BOOST_TEST_L( "too many test cases" ) );
  224. typedef framework_impl::test_unit_store::value_type map_value_type;
  225. s_frk_impl().m_test_units.insert( map_value_type( new_id, tc ) );
  226. s_frk_impl().m_next_test_case_id++;
  227. s_frk_impl().set_tu_id( *tc, new_id );
  228. }
  229. //____________________________________________________________________________//
  230. void
  231. register_test_unit( test_suite* ts )
  232. {
  233. BOOST_TEST_SETUP_ASSERT( ts->p_id == INV_TEST_UNIT_ID, BOOST_TEST_L( "test suite already registered" ) );
  234. test_unit_id new_id = s_frk_impl().m_next_test_suite_id;
  235. BOOST_TEST_SETUP_ASSERT( new_id != MAX_TEST_SUITE_ID, BOOST_TEST_L( "too many test suites" ) );
  236. typedef framework_impl::test_unit_store::value_type map_value_type;
  237. s_frk_impl().m_test_units.insert( map_value_type( new_id, ts ) );
  238. s_frk_impl().m_next_test_suite_id++;
  239. s_frk_impl().set_tu_id( *ts, new_id );
  240. }
  241. //____________________________________________________________________________//
  242. void
  243. deregister_test_unit( test_unit* tu )
  244. {
  245. s_frk_impl().m_test_units.erase( tu->p_id );
  246. }
  247. //____________________________________________________________________________//
  248. void
  249. clear()
  250. {
  251. s_frk_impl().clear();
  252. }
  253. //____________________________________________________________________________//
  254. void
  255. register_observer( test_observer& to )
  256. {
  257. s_frk_impl().m_observers.insert( &to );
  258. }
  259. //____________________________________________________________________________//
  260. void
  261. deregister_observer( test_observer& to )
  262. {
  263. s_frk_impl().m_observers.erase( &to );
  264. }
  265. //____________________________________________________________________________//
  266. void
  267. reset_observers()
  268. {
  269. s_frk_impl().m_observers.clear();
  270. }
  271. //____________________________________________________________________________//
  272. master_test_suite_t&
  273. master_test_suite()
  274. {
  275. if( !s_frk_impl().m_master_test_suite )
  276. s_frk_impl().m_master_test_suite = new master_test_suite_t;
  277. return *s_frk_impl().m_master_test_suite;
  278. }
  279. //____________________________________________________________________________//
  280. test_case const&
  281. current_test_case()
  282. {
  283. return get<test_case>( s_frk_impl().m_curr_test_case );
  284. }
  285. //____________________________________________________________________________//
  286. test_unit&
  287. get( test_unit_id id, test_unit_type t )
  288. {
  289. test_unit* res = s_frk_impl().m_test_units[id];
  290. if( (res->p_type & t) == 0 )
  291. throw internal_error( "Invalid test unit type" );
  292. return *res;
  293. }
  294. //____________________________________________________________________________//
  295. void
  296. run( test_unit_id id, bool continue_test )
  297. {
  298. if( id == INV_TEST_UNIT_ID )
  299. id = master_test_suite().p_id;
  300. test_case_counter tcc;
  301. traverse_test_tree( id, tcc );
  302. BOOST_TEST_SETUP_ASSERT( tcc.p_count != 0 , runtime_config::test_to_run().is_empty()
  303. ? BOOST_TEST_L( "test tree is empty" )
  304. : BOOST_TEST_L( "no test cases matching filter" ) );
  305. bool call_start_finish = !continue_test || !s_frk_impl().m_test_in_progress;
  306. bool was_in_progress = s_frk_impl().m_test_in_progress;
  307. s_frk_impl().m_test_in_progress = true;
  308. if( call_start_finish ) {
  309. BOOST_TEST_FOREACH( test_observer*, to, s_frk_impl().m_observers ) {
  310. boost::execution_monitor em;
  311. try {
  312. em.execute( ut_detail::test_start_caller( to, tcc.p_count ) );
  313. }
  314. catch( execution_exception const& ex ) {
  315. throw setup_error( ex.what() );
  316. }
  317. }
  318. }
  319. switch( runtime_config::random_seed() ) {
  320. case 0:
  321. break;
  322. case 1: {
  323. unsigned int seed = static_cast<unsigned int>( std::time( 0 ) );
  324. BOOST_TEST_MESSAGE( "Test cases order is shuffled using seed: " << seed );
  325. std::srand( seed );
  326. break;
  327. }
  328. default:
  329. BOOST_TEST_MESSAGE( "Test cases order is shuffled using seed: " << runtime_config::random_seed() );
  330. std::srand( runtime_config::random_seed() );
  331. }
  332. try {
  333. traverse_test_tree( id, s_frk_impl() );
  334. }
  335. catch( test_being_aborted const& ) {
  336. // abort already reported
  337. }
  338. if( call_start_finish ) {
  339. BOOST_TEST_FOREACH( test_observer*, to, s_frk_impl().m_observers )
  340. to->test_finish();
  341. }
  342. s_frk_impl().m_test_in_progress = was_in_progress;
  343. }
  344. //____________________________________________________________________________//
  345. void
  346. run( test_unit const* tu, bool continue_test )
  347. {
  348. run( tu->p_id, continue_test );
  349. }
  350. //____________________________________________________________________________//
  351. void
  352. assertion_result( bool passed )
  353. {
  354. BOOST_TEST_FOREACH( test_observer*, to, s_frk_impl().m_observers )
  355. to->assertion_result( passed );
  356. }
  357. //____________________________________________________________________________//
  358. void
  359. exception_caught( execution_exception const& ex )
  360. {
  361. BOOST_TEST_FOREACH( test_observer*, to, s_frk_impl().m_observers )
  362. to->exception_caught( ex );
  363. }
  364. //____________________________________________________________________________//
  365. void
  366. test_unit_aborted( test_unit const& tu )
  367. {
  368. BOOST_TEST_FOREACH( test_observer*, to, s_frk_impl().m_observers )
  369. to->test_unit_aborted( tu );
  370. }
  371. //____________________________________________________________________________//
  372. } // namespace framework
  373. } // namespace unit_test
  374. } // namespace boost
  375. //____________________________________________________________________________//
  376. #include <boost/test/detail/enable_warnings.hpp>
  377. #endif // BOOST_TEST_FRAMEWORK_IPP_021005GER