minimal.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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
  8. /// @brief Deprecated implementation of simple minimal testing
  9. /// @deprecated
  10. /// To convert to Unit Test Framework simply rewrite:
  11. /// @code
  12. /// #include <boost/test/minimal.hpp>
  13. ///
  14. /// int test_main( int, char *[] )
  15. /// {
  16. /// ...
  17. /// }
  18. /// @endcode
  19. /// as
  20. /// @code
  21. /// #include <boost/test/included/unit_test.hpp>
  22. ///
  23. /// BOOST_AUTO_TEST_CASE(test_main)
  24. /// {
  25. /// ...
  26. /// }
  27. /// @endcode
  28. // ***************************************************************************
  29. #ifndef BOOST_TEST_MINIMAL_HPP_071894GER
  30. #define BOOST_TEST_MINIMAL_HPP_071894GER
  31. #include <boost/config/header_deprecated.hpp>
  32. BOOST_HEADER_DEPRECATED( "<boost/test/included/unit_test.hpp>" )
  33. #if defined(BOOST_ALLOW_DEPRECATED_HEADERS)
  34. BOOST_PRAGMA_MESSAGE( "Boost.Test minimal is deprecated. Please convert to the header only variant of Boost.Test." )
  35. #endif
  36. #define BOOST_CHECK(exp) \
  37. ( (exp) \
  38. ? static_cast<void>(0) \
  39. : boost::minimal_test::report_error(#exp,__FILE__,__LINE__, BOOST_CURRENT_FUNCTION) )
  40. #define BOOST_REQUIRE(exp) \
  41. ( (exp) \
  42. ? static_cast<void>(0) \
  43. : boost::minimal_test::report_critical_error(#exp,__FILE__,__LINE__,BOOST_CURRENT_FUNCTION))
  44. #define BOOST_ERROR( msg_ ) \
  45. boost::minimal_test::report_error( (msg_),__FILE__,__LINE__, BOOST_CURRENT_FUNCTION, true )
  46. #define BOOST_FAIL( msg_ ) \
  47. boost::minimal_test::report_critical_error( (msg_),__FILE__,__LINE__, BOOST_CURRENT_FUNCTION, true )
  48. //____________________________________________________________________________//
  49. // Boost.Test
  50. #include <boost/test/detail/global_typedef.hpp>
  51. #include <boost/test/impl/execution_monitor.ipp>
  52. #include <boost/test/impl/debug.ipp>
  53. #include <boost/test/utils/class_properties.hpp>
  54. #include <boost/test/utils/basic_cstring/io.hpp>
  55. // Boost
  56. #include <boost/cstdlib.hpp> // for exit codes
  57. #include <boost/current_function.hpp> // for BOOST_CURRENT_FUNCTION
  58. // STL
  59. #include <iostream> // std::cerr, std::endl
  60. #include <string> // std::string
  61. #include <boost/test/detail/suppress_warnings.hpp>
  62. //____________________________________________________________________________//
  63. int test_main( int argc, char* argv[] ); // prototype for users test_main()
  64. namespace boost {
  65. namespace minimal_test {
  66. typedef boost::unit_test::const_string const_string;
  67. inline unit_test::counter_t& errors_counter() { static unit_test::counter_t ec = 0; return ec; }
  68. inline void
  69. report_error( const char* msg, const char* file, int line, const_string func_name, bool is_msg = false )
  70. {
  71. ++errors_counter();
  72. std::cerr << file << "(" << line << "): ";
  73. if( is_msg )
  74. std::cerr << msg;
  75. else
  76. std::cerr << "test " << msg << " failed";
  77. if( func_name != "(unknown)" )
  78. std::cerr << " in function: '" << func_name << "'";
  79. std::cerr << std::endl;
  80. }
  81. inline void
  82. report_critical_error( const char* msg, const char* file, int line, const_string func_name, bool is_msg = false )
  83. {
  84. report_error( msg, file, line, func_name, is_msg );
  85. throw boost::execution_aborted();
  86. }
  87. class caller {
  88. public:
  89. // constructor
  90. caller( int argc, char** argv )
  91. : m_argc( argc ), m_argv( argv ) {}
  92. // execution monitor hook implementation
  93. int operator()() { return test_main( m_argc, m_argv ); }
  94. private:
  95. // Data members
  96. int m_argc;
  97. char** m_argv;
  98. }; // monitor
  99. } // namespace minimal_test
  100. } // namespace boost
  101. //____________________________________________________________________________//
  102. int BOOST_TEST_CALL_DECL main( int argc, char* argv[] )
  103. {
  104. using namespace boost::minimal_test;
  105. try {
  106. ::boost::execution_monitor ex_mon;
  107. int run_result = ex_mon.execute( caller( argc, argv ) );
  108. BOOST_CHECK( run_result == 0 || run_result == boost::exit_success );
  109. }
  110. catch( boost::execution_exception const& exex ) {
  111. if( exex.code() != boost::execution_exception::no_error )
  112. BOOST_ERROR( (std::string( "exception \"" ) + exex.what() + "\" caught").c_str() );
  113. std::cerr << "\n**** Testing aborted.";
  114. }
  115. if( boost::minimal_test::errors_counter() != 0 ) {
  116. std::cerr << "\n**** " << errors_counter()
  117. << " error" << (errors_counter() > 1 ? "s" : "" ) << " detected\n";
  118. return boost::exit_test_failure;
  119. }
  120. std::cout << "\n**** no errors detected\n";
  121. return boost::exit_success;
  122. }
  123. //____________________________________________________________________________//
  124. #include <boost/test/detail/enable_warnings.hpp>
  125. #endif // BOOST_TEST_MINIMAL_HPP_071894GER