summaryrefslogtreecommitdiff
path: root/libs/asio/test/unit_test.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'libs/asio/test/unit_test.hpp')
-rw-r--r--libs/asio/test/unit_test.hpp155
1 files changed, 104 insertions, 51 deletions
diff --git a/libs/asio/test/unit_test.hpp b/libs/asio/test/unit_test.hpp
index 8f780b17b..849b4db77 100644
--- a/libs/asio/test/unit_test.hpp
+++ b/libs/asio/test/unit_test.hpp
@@ -2,7 +2,7 @@
// unit_test.hpp
// ~~~~~~~~~~~~~
//
-// Copyright (c) 2003-2014 Christopher M. Kohlhoff (chris at kohlhoff dot com)
+// Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
@@ -12,6 +12,8 @@
#define UNIT_TEST_HPP
#include <boost/asio/detail/config.hpp>
+#include <iostream>
+#include <boost/asio/detail/atomic_count.hpp>
#if defined(__sun)
# include <stdlib.h> // Needed for lrand48.
@@ -29,89 +31,136 @@
#endif // defined(__BORLANDC__)
#if defined(BOOST_ASIO_MSVC)
+# pragma warning (disable:4127)
# pragma warning (push)
# pragma warning (disable:4244)
# pragma warning (disable:4702)
#endif // defined(BOOST_ASIO_MSVC)
-#if defined(BOOST_ASIO_STANDALONE)
-
-#include <cassert>
-#include <iostream>
+#if !defined(BOOST_ASIO_TEST_IOSTREAM)
+# define BOOST_ASIO_TEST_IOSTREAM std::cerr
+#endif // !defined(BOOST_ASIO_TEST_IOSTREAM)
-#if defined(NDEBUG)
-# error NDEBUG must not be defined when building these unit tests
-#endif // defined(NDEBUG)
+namespace boost {
+namespace asio {
+namespace detail {
-#define BOOST_ASIO_CHECK(expr) assert(expr)
+inline const char*& test_name()
+{
+ static const char* name = 0;
+ return name;
+}
-#define BOOST_ASIO_CHECK_MESSAGE(expr, msg) \
- do { if (!(expr)) { std::cout << msg << std::endl; assert(expr); } } while (0)
+inline atomic_count& test_errors()
+{
+ static atomic_count errors(0);
+ return errors;
+}
-#define BOOST_ASIO_WARN_MESSAGE(expr, msg) \
- do { if (!(expr)) { std::cout << msg << std::endl; } } while (0)
+inline void begin_test_suite(const char* name)
+{
+ boost::asio::detail::test_name();
+ boost::asio::detail::test_errors();
+ BOOST_ASIO_TEST_IOSTREAM << name << " test suite begins" << std::endl;
+}
-#define BOOST_ASIO_ERROR(msg) assert(0 && msg)
+inline int end_test_suite(const char* name)
+{
+ BOOST_ASIO_TEST_IOSTREAM << name << " test suite ends" << std::endl;
+ BOOST_ASIO_TEST_IOSTREAM << "\n*** ";
+ long errors = boost::asio::detail::test_errors();
+ if (errors == 0)
+ BOOST_ASIO_TEST_IOSTREAM << "No errors detected.";
+ else if (errors == 1)
+ BOOST_ASIO_TEST_IOSTREAM << "1 error detected.";
+ else
+ BOOST_ASIO_TEST_IOSTREAM << errors << " errors detected." << std::endl;
+ BOOST_ASIO_TEST_IOSTREAM << std::endl;
+ return errors == 0 ? 0 : 1;
+}
-#define BOOST_ASIO_TEST_SUITE(name, tests) \
- int main() \
- { \
- std::cout << name << " test suite begins" << std::endl; \
- tests \
- std::cout << name << " test suite ends" << std::endl; \
- return 0; \
- }
+template <void (*Test)()>
+inline void run_test(const char* name)
+{
+ test_name() = name;
+ long errors_before = boost::asio::detail::test_errors();
+ Test();
+ if (test_errors() == errors_before)
+ BOOST_ASIO_TEST_IOSTREAM << name << " passed" << std::endl;
+ else
+ BOOST_ASIO_TEST_IOSTREAM << name << " failed" << std::endl;
+}
-#define BOOST_ASIO_TEST_CASE(test) \
- test(); \
- std::cout << #test << " passed" << std::endl;
+template <void (*)()>
+inline void compile_test(const char* name)
+{
+ BOOST_ASIO_TEST_IOSTREAM << name << " passed" << std::endl;
+}
-#define BOOST_ASIO_COMPILE_TEST_CASE(test) \
- compile_test<&test>(); \
- std::cout << #test << " passed" << std::endl;
+#if defined(BOOST_ASIO_NO_EXCEPTIONS)
-#else // defined(BOOST_ASIO_STANDALONE)
+template <typename T>
+void throw_exception(const T& t)
+{
+ BOOST_ASIO_TEST_IOSTREAM << "Exception: " << t.what() << std::endl;
+ std::abort();
+}
-#include <boost/test/unit_test.hpp>
-using boost::unit_test::test_suite;
+#endif // defined(BOOST_ASIO_NO_EXCEPTIONS)
-#define BOOST_ASIO_CHECK(expr) BOOST_CHECK(expr)
+} // namespace detail
+} // namespace asio
+} // namespace boost
-#define BOOST_ASIO_CHECK_MESSAGE(expr, msg) BOOST_CHECK_MESSAGE(expr, msg)
+#define BOOST_ASIO_CHECK(expr) \
+ do { if (!(expr)) { \
+ BOOST_ASIO_TEST_IOSTREAM << __FILE__ << "(" << __LINE__ << "): " \
+ << boost::asio::detail::test_name() << ": " \
+ << "check '" << #expr << "' failed" << std::endl; \
+ ++boost::asio::detail::test_errors(); \
+ } } while (0)
-#define BOOST_ASIO_WARN_MESSAGE(expr, msg) BOOST_WARN_MESSAGE(expr, msg)
+#define BOOST_ASIO_CHECK_MESSAGE(expr, msg) \
+ do { if (!(expr)) { \
+ BOOST_ASIO_TEST_IOSTREAM << __FILE__ << "(" << __LINE__ << "): " \
+ << boost::asio::detail::test_name() << ": " \
+ << msg << std::endl; \
+ ++boost::asio::detail::test_errors(); \
+ } } while (0)
-#define BOOST_ASIO_ERROR(expr) BOOST_ERROR(expr)
+#define BOOST_ASIO_WARN_MESSAGE(expr, msg) \
+ do { if (!(expr)) { \
+ BOOST_ASIO_TEST_IOSTREAM << __FILE__ << "(" << __LINE__ << "): " \
+ << boost::asio::detail::test_name() << ": " \
+ << msg << std::endl; \
+ } } while (0)
+
+#define BOOST_ASIO_ERROR(msg) \
+ do { \
+ BOOST_ASIO_TEST_IOSTREAM << __FILE__ << "(" << __LINE__ << "): " \
+ << boost::asio::detail::test_name() << ": " \
+ << msg << std::endl; \
+ ++boost::asio::detail::test_errors(); \
+ } while (0)
#define BOOST_ASIO_TEST_SUITE(name, tests) \
- test_suite* init_unit_test_suite(int, char*[]) \
+ int main() \
{ \
- test_suite* t = BOOST_TEST_SUITE(name); \
+ boost::asio::detail::begin_test_suite(name); \
tests \
- return t; \
+ return boost::asio::detail::end_test_suite(name); \
}
#define BOOST_ASIO_TEST_CASE(test) \
- t->add(BOOST_TEST_CASE(&test));
+ boost::asio::detail::run_test<&test>(#test);
#define BOOST_ASIO_COMPILE_TEST_CASE(test) \
- t->add(BOOST_TEST_CASE(&compile_test<&test>));
-
-#endif // defined(BOOST_ASIO_STANDALONE)
-
-#if defined(BOOST_ASIO_MSVC)
-# pragma warning (pop)
-#endif // defined(BOOST_ASIO_MSVC)
+ boost::asio::detail::compile_test<&test>(#test);
inline void null_test()
{
}
-template <void (*)()>
-inline void compile_test()
-{
-}
-
#if defined(__GNUC__) && defined(_AIX)
// AIX needs this symbol defined in asio, even if it doesn't do anything.
@@ -121,4 +170,8 @@ int test_main(int, char**)
#endif // defined(__GNUC__) && defined(_AIX)
+#if defined(BOOST_ASIO_MSVC)
+# pragma warning (pop)
+#endif // defined(BOOST_ASIO_MSVC)
+
#endif // UNIT_TEST_HPP