lightweight_test.hpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #ifndef BOOST_DETAIL_LIGHTWEIGHT_TEST_HPP_INCLUDED
  2. #define BOOST_DETAIL_LIGHTWEIGHT_TEST_HPP_INCLUDED
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  5. # pragma once
  6. #endif
  7. //
  8. // boost/detail/lightweight_test.hpp - lightweight test library
  9. //
  10. // Copyright (c) 2002, 2009 Peter Dimov
  11. //
  12. // Distributed under the Boost Software License, Version 1.0.
  13. // See accompanying file LICENSE_1_0.txt or copy at
  14. // http://www.boost.org/LICENSE_1_0.txt
  15. //
  16. // BOOST_TEST(expression)
  17. // BOOST_ERROR(message)
  18. // BOOST_TEST_EQ(expr1, expr2)
  19. //
  20. // int boost::report_errors()
  21. //
  22. #include <boost/current_function.hpp>
  23. #include <iostream>
  24. namespace boost
  25. {
  26. namespace detail
  27. {
  28. inline int & test_errors()
  29. {
  30. static int x = 0;
  31. return x;
  32. }
  33. inline void test_failed_impl(char const * expr, char const * file, int line, char const * function)
  34. {
  35. std::cerr << file << "(" << line << "): test '" << expr << "' failed in function '" << function << "'" << std::endl;
  36. ++test_errors();
  37. }
  38. inline void error_impl(char const * msg, char const * file, int line, char const * function)
  39. {
  40. std::cerr << file << "(" << line << "): " << msg << " in function '" << function << "'" << std::endl;
  41. ++test_errors();
  42. }
  43. template<class T, class U> inline void test_eq_impl( char const * expr1, char const * expr2, char const * file, int line, char const * function, T const & t, U const & u )
  44. {
  45. if( t == u )
  46. {
  47. }
  48. else
  49. {
  50. std::cerr << file << "(" << line << "): test '" << expr1 << " == " << expr2
  51. << "' failed in function '" << function << "': "
  52. << "'" << t << "' != '" << u << "'" << std::endl;
  53. ++test_errors();
  54. }
  55. }
  56. } // namespace detail
  57. inline int report_errors()
  58. {
  59. int errors = detail::test_errors();
  60. if( errors == 0 )
  61. {
  62. std::cerr << "No errors detected." << std::endl;
  63. return 0;
  64. }
  65. else
  66. {
  67. std::cerr << errors << " error" << (errors == 1? "": "s") << " detected." << std::endl;
  68. return 1;
  69. }
  70. }
  71. } // namespace boost
  72. #define BOOST_TEST(expr) ((expr)? (void)0: ::boost::detail::test_failed_impl(#expr, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION))
  73. #define BOOST_ERROR(msg) ::boost::detail::error_impl(msg, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION)
  74. #define BOOST_TEST_EQ(expr1,expr2) ( ::boost::detail::test_eq_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
  75. #endif // #ifndef BOOST_DETAIL_LIGHTWEIGHT_TEST_HPP_INCLUDED