unit_test.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //
  2. // unit_test.hpp
  3. // ~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef UNIT_TEST_HPP
  11. #define UNIT_TEST_HPP
  12. #include <boost/asio/detail/config.hpp>
  13. #include <iostream>
  14. #include <boost/asio/detail/atomic_count.hpp>
  15. #if defined(__sun)
  16. # include <stdlib.h> // Needed for lrand48.
  17. #endif // defined(__sun)
  18. #if defined(__BORLANDC__)
  19. // Prevent use of intrinsic for strcmp.
  20. # include <cstring>
  21. # undef strcmp
  22. // Suppress error about condition always being true.
  23. # pragma option -w-ccc
  24. #endif // defined(__BORLANDC__)
  25. #if defined(BOOST_ASIO_MSVC)
  26. # pragma warning (disable:4127)
  27. # pragma warning (push)
  28. # pragma warning (disable:4244)
  29. # pragma warning (disable:4702)
  30. #endif // defined(BOOST_ASIO_MSVC)
  31. #if !defined(BOOST_ASIO_TEST_IOSTREAM)
  32. # define BOOST_ASIO_TEST_IOSTREAM std::cerr
  33. #endif // !defined(BOOST_ASIO_TEST_IOSTREAM)
  34. namespace boost {
  35. namespace asio {
  36. namespace detail {
  37. inline const char*& test_name()
  38. {
  39. static const char* name = 0;
  40. return name;
  41. }
  42. inline atomic_count& test_errors()
  43. {
  44. static atomic_count errors(0);
  45. return errors;
  46. }
  47. inline void begin_test_suite(const char* name)
  48. {
  49. boost::asio::detail::test_name();
  50. boost::asio::detail::test_errors();
  51. BOOST_ASIO_TEST_IOSTREAM << name << " test suite begins" << std::endl;
  52. }
  53. inline int end_test_suite(const char* name)
  54. {
  55. BOOST_ASIO_TEST_IOSTREAM << name << " test suite ends" << std::endl;
  56. BOOST_ASIO_TEST_IOSTREAM << "\n*** ";
  57. long errors = boost::asio::detail::test_errors();
  58. if (errors == 0)
  59. BOOST_ASIO_TEST_IOSTREAM << "No errors detected.";
  60. else if (errors == 1)
  61. BOOST_ASIO_TEST_IOSTREAM << "1 error detected.";
  62. else
  63. BOOST_ASIO_TEST_IOSTREAM << errors << " errors detected." << std::endl;
  64. BOOST_ASIO_TEST_IOSTREAM << std::endl;
  65. return errors == 0 ? 0 : 1;
  66. }
  67. template <void (*Test)()>
  68. inline void run_test(const char* name)
  69. {
  70. test_name() = name;
  71. long errors_before = boost::asio::detail::test_errors();
  72. Test();
  73. if (test_errors() == errors_before)
  74. BOOST_ASIO_TEST_IOSTREAM << name << " passed" << std::endl;
  75. else
  76. BOOST_ASIO_TEST_IOSTREAM << name << " failed" << std::endl;
  77. }
  78. template <void (*)()>
  79. inline void compile_test(const char* name)
  80. {
  81. BOOST_ASIO_TEST_IOSTREAM << name << " passed" << std::endl;
  82. }
  83. #if defined(BOOST_ASIO_NO_EXCEPTIONS)
  84. template <typename T>
  85. void throw_exception(const T& t)
  86. {
  87. BOOST_ASIO_TEST_IOSTREAM << "Exception: " << t.what() << std::endl;
  88. std::abort();
  89. }
  90. #endif // defined(BOOST_ASIO_NO_EXCEPTIONS)
  91. } // namespace detail
  92. } // namespace asio
  93. } // namespace boost
  94. #define BOOST_ASIO_CHECK(expr) \
  95. do { if (!(expr)) { \
  96. BOOST_ASIO_TEST_IOSTREAM << __FILE__ << "(" << __LINE__ << "): " \
  97. << boost::asio::detail::test_name() << ": " \
  98. << "check '" << #expr << "' failed" << std::endl; \
  99. ++boost::asio::detail::test_errors(); \
  100. } } while (0)
  101. #define BOOST_ASIO_CHECK_MESSAGE(expr, msg) \
  102. do { if (!(expr)) { \
  103. BOOST_ASIO_TEST_IOSTREAM << __FILE__ << "(" << __LINE__ << "): " \
  104. << boost::asio::detail::test_name() << ": " \
  105. << msg << std::endl; \
  106. ++boost::asio::detail::test_errors(); \
  107. } } while (0)
  108. #define BOOST_ASIO_WARN_MESSAGE(expr, msg) \
  109. do { if (!(expr)) { \
  110. BOOST_ASIO_TEST_IOSTREAM << __FILE__ << "(" << __LINE__ << "): " \
  111. << boost::asio::detail::test_name() << ": " \
  112. << msg << std::endl; \
  113. } } while (0)
  114. #define BOOST_ASIO_ERROR(msg) \
  115. do { \
  116. BOOST_ASIO_TEST_IOSTREAM << __FILE__ << "(" << __LINE__ << "): " \
  117. << boost::asio::detail::test_name() << ": " \
  118. << msg << std::endl; \
  119. ++boost::asio::detail::test_errors(); \
  120. } while (0)
  121. #define BOOST_ASIO_TEST_SUITE(name, tests) \
  122. int main() \
  123. { \
  124. boost::asio::detail::begin_test_suite(name); \
  125. tests \
  126. return boost::asio::detail::end_test_suite(name); \
  127. }
  128. #define BOOST_ASIO_TEST_CASE(test) \
  129. boost::asio::detail::run_test<&test>(#test);
  130. #define BOOST_ASIO_COMPILE_TEST_CASE(test) \
  131. boost::asio::detail::compile_test<&test>(#test);
  132. inline void null_test()
  133. {
  134. }
  135. #if defined(__GNUC__) && defined(_AIX)
  136. // AIX needs this symbol defined in asio, even if it doesn't do anything.
  137. int test_main(int, char**)
  138. {
  139. }
  140. #endif // defined(__GNUC__) && defined(_AIX)
  141. #if defined(BOOST_ASIO_MSVC)
  142. # pragma warning (pop)
  143. #endif // defined(BOOST_ASIO_MSVC)
  144. #endif // UNIT_TEST_HPP