minimal.hpp 4.7 KB

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