progress_monitor.ipp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 simple text based progress monitor
  12. // ***************************************************************************
  13. #ifndef BOOST_TEST_PROGRESS_MONITOR_IPP_020105GER
  14. #define BOOST_TEST_PROGRESS_MONITOR_IPP_020105GER
  15. // Boost.Test
  16. #include <boost/test/progress_monitor.hpp>
  17. #include <boost/test/unit_test_parameters.hpp>
  18. #include <boost/test/utils/setcolor.hpp>
  19. #include <boost/test/tree/test_unit.hpp>
  20. #include <boost/test/tree/test_case_counter.hpp>
  21. #include <boost/test/tree/traverse.hpp>
  22. // Boost
  23. #include <boost/scoped_ptr.hpp>
  24. #include <boost/test/detail/suppress_warnings.hpp>
  25. //____________________________________________________________________________//
  26. namespace boost {
  27. namespace unit_test {
  28. // ************************************************************************** //
  29. // ************** progress_monitor ************** //
  30. // ************************************************************************** //
  31. struct progress_display {
  32. progress_display( counter_t expected_count, std::ostream& os )
  33. : m_os(os)
  34. , m_count( 0 )
  35. , m_expected_count( expected_count )
  36. , m_next_tic_count( 0 )
  37. , m_tic( 0 )
  38. {
  39. m_os << "\n0% 10 20 30 40 50 60 70 80 90 100%"
  40. << "\n|----|----|----|----|----|----|----|----|----|----|"
  41. << std::endl;
  42. if( !m_expected_count )
  43. m_expected_count = 1; // prevent divide by zero
  44. }
  45. unsigned long operator+=( unsigned long increment )
  46. {
  47. if( (m_count += increment) < m_next_tic_count )
  48. return m_count;
  49. // use of floating point ensures that both large and small counts
  50. // work correctly. static_cast<>() is also used several places
  51. // to suppress spurious compiler warnings.
  52. unsigned int tics_needed = static_cast<unsigned int>(
  53. (static_cast<double>(m_count)/m_expected_count)*50.0 );
  54. do {
  55. m_os << '*' << std::flush;
  56. } while( ++m_tic < tics_needed );
  57. m_next_tic_count = static_cast<unsigned long>((m_tic/50.0) * m_expected_count);
  58. if( m_count == m_expected_count ) {
  59. if( m_tic < 51 )
  60. m_os << '*';
  61. m_os << std::endl;
  62. }
  63. return m_count;
  64. }
  65. unsigned long operator++() { return operator+=( 1 ); }
  66. unsigned long count() const { return m_count; }
  67. private:
  68. BOOST_DELETED_FUNCTION(progress_display(progress_display const&))
  69. BOOST_DELETED_FUNCTION(progress_display& operator=(progress_display const&))
  70. std::ostream& m_os; // may not be present in all imps
  71. unsigned long m_count;
  72. unsigned long m_expected_count;
  73. unsigned long m_next_tic_count;
  74. unsigned int m_tic;
  75. };
  76. namespace {
  77. struct progress_monitor_impl {
  78. // Constructor
  79. progress_monitor_impl()
  80. : m_stream( &std::cout )
  81. , m_color_output( false )
  82. {
  83. }
  84. std::ostream* m_stream;
  85. scoped_ptr<progress_display> m_progress_display;
  86. bool m_color_output;
  87. };
  88. progress_monitor_impl& s_pm_impl() { static progress_monitor_impl the_inst; return the_inst; }
  89. #define PM_SCOPED_COLOR() \
  90. BOOST_TEST_SCOPE_SETCOLOR( s_pm_impl().m_color_output, *s_pm_impl().m_stream, term_attr::BRIGHT, term_color::MAGENTA )
  91. } // local namespace
  92. //____________________________________________________________________________//
  93. BOOST_TEST_SINGLETON_CONS_IMPL(progress_monitor_t)
  94. //____________________________________________________________________________//
  95. void
  96. progress_monitor_t::test_start( counter_t test_cases_amount )
  97. {
  98. s_pm_impl().m_color_output = runtime_config::get<bool>( runtime_config::btrt_color_output );
  99. PM_SCOPED_COLOR();
  100. s_pm_impl().m_progress_display.reset( new progress_display( test_cases_amount, *s_pm_impl().m_stream ) );
  101. }
  102. //____________________________________________________________________________//
  103. void
  104. progress_monitor_t::test_aborted()
  105. {
  106. PM_SCOPED_COLOR();
  107. (*s_pm_impl().m_progress_display) += s_pm_impl().m_progress_display->count();
  108. }
  109. //____________________________________________________________________________//
  110. void
  111. progress_monitor_t::test_unit_finish( test_unit const& tu, unsigned long )
  112. {
  113. PM_SCOPED_COLOR();
  114. if( tu.p_type == TUT_CASE )
  115. ++(*s_pm_impl().m_progress_display);
  116. }
  117. //____________________________________________________________________________//
  118. void
  119. progress_monitor_t::test_unit_skipped( test_unit const& tu, const_string /*reason*/ )
  120. {
  121. PM_SCOPED_COLOR();
  122. test_case_counter tcc;
  123. traverse_test_tree( tu, tcc );
  124. (*s_pm_impl().m_progress_display) += tcc.p_count;
  125. }
  126. //____________________________________________________________________________//
  127. void
  128. progress_monitor_t::set_stream( std::ostream& ostr )
  129. {
  130. s_pm_impl().m_stream = &ostr;
  131. }
  132. //____________________________________________________________________________//
  133. #undef PM_SCOPED_COLOR
  134. } // namespace unit_test
  135. } // namespace boost
  136. #include <boost/test/detail/enable_warnings.hpp>
  137. #endif // BOOST_TEST_PROGRESS_MONITOR_IPP_020105GER