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)
6 // See http://www.boost.org/libs/test for the library home page.
10 // Version : $Revision$
12 // Description : implements simple text based progress monitor
13 // ***************************************************************************
15 #ifndef BOOST_TEST_PROGRESS_MONITOR_IPP_020105GER
16 #define BOOST_TEST_PROGRESS_MONITOR_IPP_020105GER
19 #include <boost/test/progress_monitor.hpp>
20 #include <boost/test/unit_test_parameters.hpp>
22 #include <boost/test/utils/setcolor.hpp>
24 #include <boost/test/tree/test_unit.hpp>
25 #include <boost/test/tree/test_case_counter.hpp>
26 #include <boost/test/tree/traverse.hpp>
29 #include <boost/scoped_ptr.hpp>
31 #include <boost/test/detail/suppress_warnings.hpp>
33 //____________________________________________________________________________//
38 // ************************************************************************** //
39 // ************** progress_monitor ************** //
40 // ************************************************************************** //
42 struct progress_display {
43 progress_display( counter_t expected_count, std::ostream& os )
46 , m_expected_count( expected_count )
47 , m_next_tic_count( 0 )
51 m_os << "\n0% 10 20 30 40 50 60 70 80 90 100%"
52 << "\n|----|----|----|----|----|----|----|----|----|----|"
55 if( !m_expected_count )
56 m_expected_count = 1; // prevent divide by zero
59 unsigned long operator+=( unsigned long increment )
61 if( (m_count += increment) < m_next_tic_count )
64 // use of floating point ensures that both large and small counts
65 // work correctly. static_cast<>() is also used several places
66 // to suppress spurious compiler warnings.
67 unsigned int tics_needed = static_cast<unsigned int>(
68 (static_cast<double>(m_count)/m_expected_count)*50.0 );
71 m_os << '*' << std::flush;
72 } while( ++m_tic < tics_needed );
74 m_next_tic_count = static_cast<unsigned long>((m_tic/50.0) * m_expected_count);
76 if( m_count == m_expected_count ) {
85 unsigned long operator++() { return operator+=( 1 ); }
86 unsigned long count() const { return m_count; }
89 BOOST_DELETED_FUNCTION(progress_display(progress_display const&))
90 BOOST_DELETED_FUNCTION(progress_display& operator=(progress_display const&))
92 std::ostream& m_os; // may not be present in all imps
94 unsigned long m_count;
95 unsigned long m_expected_count;
96 unsigned long m_next_tic_count;
102 struct progress_monitor_impl {
104 progress_monitor_impl()
105 : m_stream( &std::cout )
106 , m_color_output( false )
110 std::ostream* m_stream;
111 scoped_ptr<progress_display> m_progress_display;
115 progress_monitor_impl& s_pm_impl() { static progress_monitor_impl the_inst; return the_inst; }
117 #define PM_SCOPED_COLOR() \
118 BOOST_TEST_SCOPE_SETCOLOR( s_pm_impl().m_color_output, *s_pm_impl().m_stream, term_attr::BRIGHT, term_color::MAGENTA )
122 //____________________________________________________________________________//
125 progress_monitor_t::test_start( counter_t test_cases_amount )
127 s_pm_impl().m_color_output = runtime_config::get<bool>( runtime_config::COLOR_OUTPUT );
131 s_pm_impl().m_progress_display.reset( new progress_display( test_cases_amount, *s_pm_impl().m_stream ) );
134 //____________________________________________________________________________//
137 progress_monitor_t::test_aborted()
141 (*s_pm_impl().m_progress_display) += s_pm_impl().m_progress_display->count();
144 //____________________________________________________________________________//
147 progress_monitor_t::test_unit_finish( test_unit const& tu, unsigned long )
151 if( tu.p_type == TUT_CASE )
152 ++(*s_pm_impl().m_progress_display);
155 //____________________________________________________________________________//
158 progress_monitor_t::test_unit_skipped( test_unit const& tu, const_string /*reason*/ )
162 test_case_counter tcc;
163 traverse_test_tree( tu, tcc );
165 (*s_pm_impl().m_progress_display) += tcc.p_count;
168 //____________________________________________________________________________//
171 progress_monitor_t::set_stream( std::ostream& ostr )
173 s_pm_impl().m_stream = &ostr;
176 //____________________________________________________________________________//
178 #undef PM_SCOPED_COLOR
180 } // namespace unit_test
183 #include <boost/test/detail/enable_warnings.hpp>
185 #endif // BOOST_TEST_PROGRESS_MONITOR_IPP_020105GER