5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
14 #include <boost/asio/detail/config.hpp>
16 #include <boost/asio/detail/atomic_count.hpp>
19 # include <stdlib.h> // Needed for lrand48.
20 #endif // defined(__sun)
22 #if defined(__BORLANDC__)
24 // Prevent use of intrinsic for strcmp.
28 // Suppress error about condition always being true.
29 # pragma option -w-ccc
31 #endif // defined(__BORLANDC__)
33 #if defined(BOOST_ASIO_MSVC)
34 # pragma warning (disable:4127)
35 # pragma warning (push)
36 # pragma warning (disable:4244)
37 # pragma warning (disable:4702)
38 #endif // defined(BOOST_ASIO_MSVC)
40 #if !defined(BOOST_ASIO_TEST_IOSTREAM)
41 # define BOOST_ASIO_TEST_IOSTREAM std::cerr
42 #endif // !defined(BOOST_ASIO_TEST_IOSTREAM)
48 inline const char*& test_name()
50 static const char* name = 0;
54 inline atomic_count& test_errors()
56 static atomic_count errors(0);
60 inline void begin_test_suite(const char* name)
62 boost::asio::detail::test_name();
63 boost::asio::detail::test_errors();
64 BOOST_ASIO_TEST_IOSTREAM << name << " test suite begins" << std::endl;
67 inline int end_test_suite(const char* name)
69 BOOST_ASIO_TEST_IOSTREAM << name << " test suite ends" << std::endl;
70 BOOST_ASIO_TEST_IOSTREAM << "\n*** ";
71 long errors = boost::asio::detail::test_errors();
73 BOOST_ASIO_TEST_IOSTREAM << "No errors detected.";
75 BOOST_ASIO_TEST_IOSTREAM << "1 error detected.";
77 BOOST_ASIO_TEST_IOSTREAM << errors << " errors detected." << std::endl;
78 BOOST_ASIO_TEST_IOSTREAM << std::endl;
79 return errors == 0 ? 0 : 1;
82 template <void (*Test)()>
83 inline void run_test(const char* name)
86 long errors_before = boost::asio::detail::test_errors();
88 if (test_errors() == errors_before)
89 BOOST_ASIO_TEST_IOSTREAM << name << " passed" << std::endl;
91 BOOST_ASIO_TEST_IOSTREAM << name << " failed" << std::endl;
95 inline void compile_test(const char* name)
97 BOOST_ASIO_TEST_IOSTREAM << name << " passed" << std::endl;
100 #if defined(BOOST_ASIO_NO_EXCEPTIONS)
102 template <typename T>
103 void throw_exception(const T& t)
105 BOOST_ASIO_TEST_IOSTREAM << "Exception: " << t.what() << std::endl;
109 #endif // defined(BOOST_ASIO_NO_EXCEPTIONS)
111 } // namespace detail
115 #define BOOST_ASIO_CHECK(expr) \
116 do { if (!(expr)) { \
117 BOOST_ASIO_TEST_IOSTREAM << __FILE__ << "(" << __LINE__ << "): " \
118 << boost::asio::detail::test_name() << ": " \
119 << "check '" << #expr << "' failed" << std::endl; \
120 ++boost::asio::detail::test_errors(); \
123 #define BOOST_ASIO_CHECK_MESSAGE(expr, msg) \
124 do { if (!(expr)) { \
125 BOOST_ASIO_TEST_IOSTREAM << __FILE__ << "(" << __LINE__ << "): " \
126 << boost::asio::detail::test_name() << ": " \
127 << msg << std::endl; \
128 ++boost::asio::detail::test_errors(); \
131 #define BOOST_ASIO_WARN_MESSAGE(expr, msg) \
132 do { if (!(expr)) { \
133 BOOST_ASIO_TEST_IOSTREAM << __FILE__ << "(" << __LINE__ << "): " \
134 << boost::asio::detail::test_name() << ": " \
135 << msg << std::endl; \
138 #define BOOST_ASIO_ERROR(msg) \
140 BOOST_ASIO_TEST_IOSTREAM << __FILE__ << "(" << __LINE__ << "): " \
141 << boost::asio::detail::test_name() << ": " \
142 << msg << std::endl; \
143 ++boost::asio::detail::test_errors(); \
146 #define BOOST_ASIO_TEST_SUITE(name, tests) \
149 boost::asio::detail::begin_test_suite(name); \
151 return boost::asio::detail::end_test_suite(name); \
154 #define BOOST_ASIO_TEST_CASE(test) \
155 boost::asio::detail::run_test<&test>(#test);
157 #define BOOST_ASIO_COMPILE_TEST_CASE(test) \
158 boost::asio::detail::compile_test<&test>(#test);
160 inline void null_test()
164 #if defined(__GNUC__) && defined(_AIX)
166 // AIX needs this symbol defined in asio, even if it doesn't do anything.
167 int test_main(int, char**)
171 #endif // defined(__GNUC__) && defined(_AIX)
173 #if defined(BOOST_ASIO_MSVC)
174 # pragma warning (pop)
175 #endif // defined(BOOST_ASIO_MSVC)
177 #endif // UNIT_TEST_HPP