minimal.hpp 4.5 KB

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