minimal.hpp 4.6 KB

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