Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:01:00

0001 //  (C) Copyright Gennadiy Rozental 2001.
0002 //  Distributed under the Boost Software License, Version 1.0.
0003 //  (See accompanying file LICENSE_1_0.txt or copy at
0004 //  http://www.boost.org/LICENSE_1_0.txt)
0005 
0006 //  See http://www.boost.org/libs/test for the library home page.
0007 //
0008 /// @file
0009 /// Test results collecting facility.
0010 ///
0011 // ***************************************************************************
0012 
0013 #ifndef BOOST_TEST_RESULTS_COLLECTOR_IPP_021105GER
0014 #define BOOST_TEST_RESULTS_COLLECTOR_IPP_021105GER
0015 
0016 // Boost.Test
0017 #include <boost/test/unit_test_log.hpp>
0018 #include <boost/test/results_collector.hpp>
0019 #include <boost/test/framework.hpp>
0020 #include <boost/test/execution_monitor.hpp>
0021 
0022 #include <boost/test/tree/test_unit.hpp>
0023 #include <boost/test/tree/visitor.hpp>
0024 #include <boost/test/tree/test_case_counter.hpp>
0025 #include <boost/test/tree/traverse.hpp>
0026 
0027 // Boost
0028 #include <boost/cstdlib.hpp>
0029 
0030 // STL
0031 #include <map>
0032 
0033 #include <boost/test/detail/suppress_warnings.hpp>
0034 
0035 //____________________________________________________________________________//
0036 
0037 namespace boost {
0038 namespace unit_test {
0039 
0040 // ************************************************************************** //
0041 // **************                 test_results                 ************** //
0042 // ************************************************************************** //
0043 
0044 test_results::test_results()
0045 {
0046     clear();
0047 }
0048 
0049 //____________________________________________________________________________//
0050 
0051 bool
0052 test_results::passed() const
0053 {
0054     // if it is skipped, it is not passed. However, if any children is not failed/aborted
0055     // then their skipped status is not taken into account.
0056     return  !p_skipped                                  &&
0057             p_test_cases_failed == 0                    &&
0058             p_assertions_failed <= p_expected_failures  &&
0059             // p_test_cases_skipped == 0                   &&
0060             !p_timed_out                                 &&
0061             p_test_cases_timed_out == 0                  &&
0062             !aborted();
0063 }
0064 
0065 //____________________________________________________________________________//
0066 
0067 bool
0068 test_results::aborted() const
0069 {
0070     return  p_aborted;
0071 }
0072 
0073 //____________________________________________________________________________//
0074 
0075 bool
0076 test_results::skipped() const
0077 {
0078     return  p_skipped;
0079 }
0080 
0081 //____________________________________________________________________________//
0082 
0083 int
0084 test_results::result_code() const
0085 {
0086     return passed() ? exit_success
0087            : ( (p_assertions_failed > p_expected_failures || p_skipped || p_timed_out || p_test_cases_timed_out )
0088                     ? exit_test_failure
0089                     : exit_exception_failure );
0090 }
0091 
0092 //____________________________________________________________________________//
0093 
0094 void
0095 test_results::operator+=( test_results const& tr )
0096 {
0097     p_test_suites.value         += tr.p_test_suites;
0098     p_assertions_passed.value   += tr.p_assertions_passed;
0099     p_assertions_failed.value   += tr.p_assertions_failed;
0100     p_warnings_failed.value     += tr.p_warnings_failed;
0101     p_test_cases_passed.value   += tr.p_test_cases_passed;
0102     p_test_cases_warned.value   += tr.p_test_cases_warned;
0103     p_test_cases_failed.value   += tr.p_test_cases_failed;
0104     p_test_cases_skipped.value  += tr.p_test_cases_skipped;
0105     p_test_cases_aborted.value  += tr.p_test_cases_aborted;
0106     p_test_cases_timed_out.value += tr.p_test_cases_timed_out;
0107     p_test_suites_timed_out.value += tr.p_test_suites_timed_out;
0108     p_duration_microseconds.value += tr.p_duration_microseconds;
0109 }
0110 
0111 //____________________________________________________________________________//
0112 
0113 void
0114 test_results::clear()
0115 {
0116     p_test_suites.value         = 0;
0117     p_assertions_passed.value   = 0;
0118     p_assertions_failed.value   = 0;
0119     p_warnings_failed.value     = 0;
0120     p_expected_failures.value   = 0;
0121     p_test_cases_passed.value   = 0;
0122     p_test_cases_warned.value   = 0;
0123     p_test_cases_failed.value   = 0;
0124     p_test_cases_skipped.value  = 0;
0125     p_test_cases_aborted.value  = 0;
0126     p_test_cases_timed_out.value = 0;
0127     p_test_suites_timed_out.value = 0;
0128     p_duration_microseconds.value= 0;
0129     p_aborted.value             = false;
0130     p_skipped.value             = false;
0131     p_timed_out.value           = false;
0132 }
0133 
0134 //____________________________________________________________________________//
0135 
0136 // ************************************************************************** //
0137 // **************               results_collector              ************** //
0138 // ************************************************************************** //
0139 
0140 namespace {
0141 
0142 struct results_collector_impl {
0143     std::map<test_unit_id,test_results> m_results_store;
0144 };
0145 
0146 results_collector_impl& s_rc_impl() { static results_collector_impl the_inst; return the_inst; }
0147 
0148 // deletes the entries of results_collector_impl
0149 class clear_subtree_result : public test_tree_visitor {
0150 public:
0151     clear_subtree_result(results_collector_impl& store)
0152     : m_store( store )
0153     {}
0154 
0155 private:
0156     bool visit( test_unit const& tu) BOOST_OVERRIDE
0157     {
0158       typedef std::map<test_unit_id,test_results>::iterator iterator;
0159       iterator found = m_store.m_results_store.find(tu.p_id);
0160       if(found != m_store.m_results_store.end()) {
0161         m_store.m_results_store.erase( found );
0162       }
0163       return true;
0164     }
0165 
0166     results_collector_impl& m_store;
0167 };
0168 
0169 } // local namespace
0170 
0171 //____________________________________________________________________________//
0172 
0173 BOOST_TEST_SINGLETON_CONS_IMPL( results_collector_t )
0174 
0175 //____________________________________________________________________________//
0176 
0177 void
0178 results_collector_t::test_start( counter_t, test_unit_id id )
0179 {
0180     // deletes the results under id only
0181     clear_subtree_result tree_clear(s_rc_impl());
0182     traverse_test_tree( id, tree_clear );
0183 }
0184 
0185 //____________________________________________________________________________//
0186 
0187 void
0188 results_collector_t::test_unit_start( test_unit const& tu )
0189 {
0190     // init test_results entry
0191     test_results& tr = s_rc_impl().m_results_store[tu.p_id];
0192 
0193     tr.clear();
0194 
0195     tr.p_expected_failures.value = tu.p_expected_failures;
0196 }
0197 
0198 //____________________________________________________________________________//
0199 
0200 class results_collect_helper : public test_tree_visitor {
0201 public:
0202     explicit results_collect_helper( test_results& tr, test_unit const& ts ) : m_tr( tr ), m_ts( ts ) {}
0203 
0204     void    visit( test_case const& tc ) BOOST_OVERRIDE
0205     {
0206         test_results const& tr = results_collector.results( tc.p_id );
0207         m_tr += tr;
0208 
0209         if( tr.passed() ) {
0210             if( tr.p_warnings_failed )
0211                 m_tr.p_test_cases_warned.value++;
0212             else
0213                 m_tr.p_test_cases_passed.value++;
0214         }
0215         else if( tr.p_timed_out ) {
0216             m_tr.p_test_cases_timed_out.value++;
0217         }
0218         else if( tr.p_skipped || !tc.is_enabled() ) {
0219             m_tr.p_test_cases_skipped.value++;
0220         }
0221         else {
0222             if( tr.p_aborted )
0223                 m_tr.p_test_cases_aborted.value++;
0224 
0225             m_tr.p_test_cases_failed.value++;
0226         }
0227     }
0228     bool    test_suite_start( test_suite const& ts ) BOOST_OVERRIDE
0229     {
0230         if( m_ts.p_id == ts.p_id )
0231             return true;
0232 
0233         m_tr += results_collector.results( ts.p_id );
0234         m_tr.p_test_suites.value++;
0235 
0236         if( results_collector.results( ts.p_id ).p_timed_out )
0237             m_tr.p_test_suites_timed_out.value++;
0238         return false;
0239     }
0240 
0241 private:
0242     // Data members
0243     test_results&       m_tr;
0244     test_unit const&    m_ts;
0245 };
0246 
0247 //____________________________________________________________________________//
0248 
0249 void
0250 results_collector_t::test_unit_finish( test_unit const& tu, unsigned long elapsed_in_microseconds )
0251 {
0252     test_results & tr = s_rc_impl().m_results_store[tu.p_id];
0253     if( tu.p_type == TUT_SUITE ) {
0254         results_collect_helper ch( tr, tu );
0255         traverse_test_tree( tu, ch, true ); // true to ignore the status: we need to count the skipped/disabled tests
0256     }
0257     else {
0258         bool num_failures_match = tr.p_aborted || tr.p_assertions_failed >= tr.p_expected_failures;
0259         if( !num_failures_match )
0260             BOOST_TEST_FRAMEWORK_MESSAGE( "Test case " << tu.full_name() << " has fewer failures than expected" );
0261 
0262         bool check_any_assertions = tr.p_aborted || (tr.p_assertions_failed != 0) || (tr.p_assertions_passed != 0);
0263         if( !check_any_assertions )
0264             BOOST_TEST_FRAMEWORK_MESSAGE( "Test case " << tu.full_name() << " did not check any assertions" );
0265     }
0266     tr.p_duration_microseconds.value = elapsed_in_microseconds;
0267 }
0268 
0269 //____________________________________________________________________________//
0270 
0271 void
0272 results_collector_t::test_unit_skipped( test_unit const& tu, const_string /*reason*/ )
0273 {
0274     test_results& tr = s_rc_impl().m_results_store[tu.p_id];
0275     tr.clear();
0276 
0277     tr.p_skipped.value = true;
0278 
0279     if( tu.p_type == TUT_SUITE ) {
0280         test_case_counter tcc(true);
0281         traverse_test_tree( tu, tcc, true ); // true because need to count the disabled tests/units
0282 
0283         tr.p_test_cases_skipped.value = tcc.p_count;
0284     }
0285 }
0286 
0287 //____________________________________________________________________________//
0288 
0289 void
0290 results_collector_t::test_unit_timed_out(test_unit const& tu)
0291 {
0292     test_results& tr = s_rc_impl().m_results_store[tu.p_id];
0293     tr.p_timed_out.value = true;
0294 }
0295 
0296 //____________________________________________________________________________//
0297 
0298 void
0299 results_collector_t::assertion_result( unit_test::assertion_result ar )
0300 {
0301     test_results& tr = s_rc_impl().m_results_store[framework::current_test_case_id()];
0302 
0303     switch( ar ) {
0304     case AR_PASSED: tr.p_assertions_passed.value++; break;
0305     case AR_FAILED: tr.p_assertions_failed.value++; break;
0306     case AR_TRIGGERED: tr.p_warnings_failed.value++; break;
0307     }
0308 
0309     if( tr.p_assertions_failed == 1 )
0310         first_failed_assertion();
0311 }
0312 
0313 //____________________________________________________________________________//
0314 
0315 void
0316 results_collector_t::exception_caught( execution_exception const& ex)
0317 {
0318     test_results& tr = s_rc_impl().m_results_store[framework::current_test_case_id()];
0319 
0320     tr.p_assertions_failed.value++;
0321     if( ex.code() == execution_exception::timeout_error ) {
0322         tr.p_timed_out.value = true;
0323     }
0324 }
0325 
0326 //____________________________________________________________________________//
0327 
0328 void
0329 results_collector_t::test_unit_aborted( test_unit const& tu )
0330 {
0331     s_rc_impl().m_results_store[tu.p_id].p_aborted.value = true;
0332 }
0333 
0334 //____________________________________________________________________________//
0335 
0336 test_results const&
0337 results_collector_t::results( test_unit_id id ) const
0338 {
0339     return s_rc_impl().m_results_store[id];
0340 }
0341 
0342 //____________________________________________________________________________//
0343 
0344 } // namespace unit_test
0345 } // namespace boost
0346 
0347 #include <boost/test/detail/enable_warnings.hpp>
0348 
0349 #endif // BOOST_TEST_RESULTS_COLLECTOR_IPP_021105GER