unit_test_suite.ipp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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: 54633 $
  10. //
  11. // Description : privides core implementation for Unit Test Framework.
  12. // Extensions can be provided in separate files
  13. // ***************************************************************************
  14. #ifndef BOOST_TEST_UNIT_TEST_SUITE_IPP_012205GER
  15. #define BOOST_TEST_UNIT_TEST_SUITE_IPP_012205GER
  16. // Boost.Test
  17. #include <boost/detail/workaround.hpp>
  18. #include <boost/test/unit_test_suite_impl.hpp>
  19. #include <boost/test/framework.hpp>
  20. #include <boost/test/utils/foreach.hpp>
  21. #include <boost/test/results_collector.hpp>
  22. #include <boost/test/detail/unit_test_parameters.hpp>
  23. // Boost
  24. #include <boost/timer.hpp>
  25. // STL
  26. #include <algorithm>
  27. #include <vector>
  28. #include <boost/test/detail/suppress_warnings.hpp>
  29. #if BOOST_WORKAROUND(__BORLANDC__, < 0x600) && \
  30. BOOST_WORKAROUND(_STLPORT_VERSION, <= 0x450) \
  31. /**/
  32. using std::rand; // rand is in std and random_shuffle is in _STL
  33. #endif
  34. //____________________________________________________________________________//
  35. namespace boost {
  36. namespace unit_test {
  37. // ************************************************************************** //
  38. // ************** test_unit ************** //
  39. // ************************************************************************** //
  40. test_unit::test_unit( const_string name, test_unit_type t )
  41. : p_type( t )
  42. , p_type_name( t == tut_case ? "case" : "suite" )
  43. , p_id( INV_TEST_UNIT_ID )
  44. , p_name( std::string( name.begin(), name.size() ) )
  45. , p_enabled( true )
  46. {
  47. }
  48. //____________________________________________________________________________//
  49. test_unit::~test_unit()
  50. {
  51. framework::deregister_test_unit( this );
  52. }
  53. //____________________________________________________________________________//
  54. void
  55. test_unit::depends_on( test_unit* tu )
  56. {
  57. m_dependencies.push_back( tu->p_id );
  58. }
  59. //____________________________________________________________________________//
  60. bool
  61. test_unit::check_dependencies() const
  62. {
  63. BOOST_TEST_FOREACH( test_unit_id, tu_id, m_dependencies ) {
  64. if( !unit_test::results_collector.results( tu_id ).passed() )
  65. return false;
  66. }
  67. return true;
  68. }
  69. //____________________________________________________________________________//
  70. void
  71. test_unit::increase_exp_fail( unsigned num )
  72. {
  73. p_expected_failures.value += num;
  74. if( p_parent_id != 0 )
  75. framework::get<test_suite>( p_parent_id ).increase_exp_fail( num );
  76. }
  77. //____________________________________________________________________________//
  78. // ************************************************************************** //
  79. // ************** test_case ************** //
  80. // ************************************************************************** //
  81. test_case::test_case( const_string name, callback0<> const& test_func )
  82. : test_unit( name, static_cast<test_unit_type>(type) )
  83. , m_test_func( test_func )
  84. {
  85. // !! weirdest MSVC BUG; try to remove this statement; looks like it eats first token of next statement
  86. #if BOOST_WORKAROUND(BOOST_MSVC,<1300)
  87. 0;
  88. #endif
  89. framework::register_test_unit( this );
  90. }
  91. //____________________________________________________________________________//
  92. // ************************************************************************** //
  93. // ************** test_suite ************** //
  94. // ************************************************************************** //
  95. //____________________________________________________________________________//
  96. test_suite::test_suite( const_string name )
  97. : test_unit( name, static_cast<test_unit_type>(type) )
  98. {
  99. framework::register_test_unit( this );
  100. }
  101. //____________________________________________________________________________//
  102. void
  103. test_suite::add( test_unit* tu, counter_t expected_failures, unsigned timeout )
  104. {
  105. if( timeout != 0 )
  106. tu->p_timeout.value = timeout;
  107. m_members.push_back( tu->p_id );
  108. tu->p_parent_id.value = p_id;
  109. if( tu->p_expected_failures )
  110. increase_exp_fail( tu->p_expected_failures );
  111. if( expected_failures )
  112. tu->increase_exp_fail( expected_failures );
  113. }
  114. //____________________________________________________________________________//
  115. void
  116. test_suite::add( test_unit_generator const& gen, unsigned timeout )
  117. {
  118. test_unit* tu;
  119. while((tu = gen.next(), tu))
  120. add( tu, 0, timeout );
  121. }
  122. //____________________________________________________________________________//
  123. void
  124. test_suite::remove( test_unit_id id )
  125. {
  126. std::vector<test_unit_id>::iterator it = std::find( m_members.begin(), m_members.end(), id );
  127. if( it != m_members.end() )
  128. m_members.erase( it );
  129. }
  130. //____________________________________________________________________________//
  131. test_unit_id
  132. test_suite::get( const_string tu_name ) const
  133. {
  134. BOOST_TEST_FOREACH( test_unit_id, id, m_members ) {
  135. if( tu_name == framework::get( id, ut_detail::test_id_2_unit_type( id ) ).p_name.get() )
  136. return id;
  137. }
  138. return INV_TEST_UNIT_ID;
  139. }
  140. //____________________________________________________________________________//
  141. // ************************************************************************** //
  142. // ************** traverse_test_tree ************** //
  143. // ************************************************************************** //
  144. void
  145. traverse_test_tree( test_case const& tc, test_tree_visitor& V )
  146. {
  147. if( tc.p_enabled )
  148. V.visit( tc );
  149. }
  150. //____________________________________________________________________________//
  151. void
  152. traverse_test_tree( test_suite const& suite, test_tree_visitor& V )
  153. {
  154. if( !suite.p_enabled || !V.test_suite_start( suite ) )
  155. return;
  156. try {
  157. if( runtime_config::random_seed() == 0 ) {
  158. BOOST_TEST_FOREACH( test_unit_id, id, suite.m_members )
  159. traverse_test_tree( id, V );
  160. }
  161. else {
  162. std::vector<test_unit_id> members( suite.m_members );
  163. std::random_shuffle( members.begin(), members.end() );
  164. BOOST_TEST_FOREACH( test_unit_id, id, members )
  165. traverse_test_tree( id, V );
  166. }
  167. } catch( test_being_aborted const& ) {
  168. V.test_suite_finish( suite );
  169. framework::test_unit_aborted( suite );
  170. throw;
  171. }
  172. V.test_suite_finish( suite );
  173. }
  174. //____________________________________________________________________________//
  175. void
  176. traverse_test_tree( test_unit_id id, test_tree_visitor& V )
  177. {
  178. if( ut_detail::test_id_2_unit_type( id ) == tut_case )
  179. traverse_test_tree( framework::get<test_case>( id ), V );
  180. else
  181. traverse_test_tree( framework::get<test_suite>( id ), V );
  182. }
  183. //____________________________________________________________________________//
  184. // ************************************************************************** //
  185. // ************** test_case_counter ************** //
  186. // ************************************************************************** //
  187. void
  188. test_case_counter::visit( test_case const& tc )
  189. {
  190. if( tc.p_enabled )
  191. ++p_count.value;
  192. }
  193. //____________________________________________________________________________//
  194. // ************************************************************************** //
  195. // ************** object generators ************** //
  196. // ************************************************************************** //
  197. namespace ut_detail {
  198. std::string
  199. normalize_test_case_name( const_string name )
  200. {
  201. return ( name[0] == '&'
  202. ? std::string( name.begin()+1, name.size()-1 )
  203. : std::string( name.begin(), name.size() ) );
  204. }
  205. //____________________________________________________________________________//
  206. // ************************************************************************** //
  207. // ************** auto_test_unit_registrar ************** //
  208. // ************************************************************************** //
  209. auto_test_unit_registrar::auto_test_unit_registrar( test_case* tc, counter_t exp_fail )
  210. {
  211. curr_ts_store().back()->add( tc, exp_fail );
  212. }
  213. //____________________________________________________________________________//
  214. auto_test_unit_registrar::auto_test_unit_registrar( const_string ts_name )
  215. {
  216. test_unit_id id = curr_ts_store().back()->get( ts_name );
  217. test_suite* ts;
  218. if( id != INV_TEST_UNIT_ID ) {
  219. ts = &framework::get<test_suite>( id ); // !! test for invalid tu type
  220. BOOST_ASSERT( ts->p_parent_id == curr_ts_store().back()->p_id );
  221. }
  222. else {
  223. ts = new test_suite( ts_name );
  224. curr_ts_store().back()->add( ts );
  225. }
  226. curr_ts_store().push_back( ts );
  227. }
  228. //____________________________________________________________________________//
  229. auto_test_unit_registrar::auto_test_unit_registrar( test_unit_generator const& tc_gen )
  230. {
  231. curr_ts_store().back()->add( tc_gen );
  232. }
  233. //____________________________________________________________________________//
  234. auto_test_unit_registrar::auto_test_unit_registrar( int )
  235. {
  236. if( curr_ts_store().size() == 0 )
  237. return; // report error?
  238. curr_ts_store().pop_back();
  239. }
  240. //____________________________________________________________________________//
  241. std::list<test_suite*>&
  242. auto_test_unit_registrar::curr_ts_store()
  243. {
  244. static std::list<test_suite*> inst( 1, &framework::master_test_suite() );
  245. return inst;
  246. }
  247. //____________________________________________________________________________//
  248. } // namespace ut_detail
  249. // ************************************************************************** //
  250. // ************** global_fixture ************** //
  251. // ************************************************************************** //
  252. global_fixture::global_fixture()
  253. {
  254. framework::register_observer( *this );
  255. }
  256. //____________________________________________________________________________//
  257. } // namespace unit_test
  258. } // namespace boost
  259. //____________________________________________________________________________//
  260. #include <boost/test/detail/enable_warnings.hpp>
  261. #endif // BOOST_TEST_UNIT_TEST_SUITE_IPP_012205GER