Commit a376b31a authored by Dominik Charousset's avatar Dominik Charousset

Make sure unit test output is not messed up

parent 6aea8222
......@@ -9,6 +9,11 @@ using namespace caf;
namespace {
atomic<size_t> s_error_count{0};
std::mutex s_stdout_mtx;
}
std::mutex& caf_stdout_mtx() {
return s_stdout_mtx;
}
size_t caf_error_count() {
......
#ifndef TEST_HPP
#define TEST_HPP
#include <mutex>
#include <vector>
#include <string>
#include <thread>
......@@ -27,6 +28,7 @@ constexpr char to_dev_null[] = "";
void set_default_test_settings();
std::mutex& caf_stdout_mtx();
size_t caf_error_count();
void caf_inc_error_count();
std::string caf_fill4(size_t value);
......@@ -37,34 +39,47 @@ void caf_unexpected_timeout(const char* file, size_t line);
#define CAF_STREAMIFY(fname, line, message) \
caf_strip_path(fname) << ":" << caf_fill4(line) << " " << message
#define CAF_PRINTC(filename, linenum, message) \
CAF_LOGF_INFO(CAF_STREAMIFY(filename, linenum, message)); \
std::cout << CAF_STREAMIFY(filename, linenum, message) << std::endl
#define CAF_PRINTC(filename, line, message) \
CAF_LOGF_INFO(CAF_STREAMIFY(filename, line, message)); \
{ \
std::lock_guard<std::mutex> guard{caf_stdout_mtx()}; \
std::cout << CAF_STREAMIFY(filename, line, message) << std::endl; \
} \
static_cast<void>(0)
#define CAF_PRINT(message) CAF_PRINTC(__FILE__, __LINE__, message)
#if defined(CAF_LOG_LEVEL) && CAF_LOG_LEVEL > 1
#define CAF_PRINTERRC(fname, linenum, msg) \
CAF_LOGF_ERROR(CAF_STREAMIFY(fname, linenum, msg)); \
std::cerr << "ERROR: " << CAF_STREAMIFY(fname, linenum, msg) << std::endl
# define CAF_PRINTERRC(fname, line, msg) \
CAF_LOGF_ERROR(CAF_STREAMIFY(fname, line, msg)); \
{ \
std::lock_guard<std::mutex> guard{caf_stdout_mtx()}; \
std::cerr << "ERROR: " << CAF_STREAMIFY(fname, line, msg) << std::endl; \
} \
static_cast<void>(0)
#else
#define CAF_PRINTERRC(fname, linenum, msg) \
std::cerr << "ERROR: " << CAF_STREAMIFY(fname, linenum, msg) << std::endl
# define CAF_PRINTERRC(fname, line, msg) \
{ \
std::lock_guard<std::mutex> guard{caf_stdout_mtx()}; \
std::cerr << "ERROR: " << CAF_STREAMIFY(fname, line, msg) << std::endl; \
} \
static_cast<void>(0)
#endif
#define CAF_PRINTERR(message) CAF_PRINTERRC(__FILE__, __LINE__, message)
template <class T1, typename T2>
struct both_integral {
static constexpr bool value =
std::is_integral<T1>::value && std::is_integral<T2>::value;
static constexpr bool value = std::is_integral<T1>::value
&& std::is_integral<T2>::value;
};
template <bool V, typename T1, typename T2>
struct enable_integral : std::enable_if<both_integral<T1, T2>::value ==
V&& not std::is_pointer<T1>::value&& not
std::is_pointer<T2>::value> {};
struct enable_integral
: std::enable_if<
both_integral<T1, T2>::value == V
&& not std::is_pointer<T1>::value
&& not std::is_pointer<T2>::value> { };
template <class T>
const T& caf_stream_arg(const T& value) {
......@@ -99,10 +114,11 @@ inline void caf_failed(const V1& v1, const V2& v2, const char* fname,
inline void caf_check_value(const std::string& v1, const std::string& v2,
const char* fname, size_t line,
bool expected = true) {
if ((v1 == v2) == expected)
if ((v1 == v2) == expected) {
caf_passed(fname, line);
else
} else {
caf_failed(v1, v2, fname, line);
}
}
template <class V1, typename V2>
......@@ -110,10 +126,11 @@ inline void caf_check_value(const V1& v1, const V2& v2, const char* fname,
size_t line, bool expected = true,
typename enable_integral<false, V1, V2>::type* =
0) {
if (caf::detail::safe_equal(v1, v2) == expected)
if (caf::detail::safe_equal(v1, v2) == expected) {
caf_passed(fname, line);
else
} else {
caf_failed(v1, v2, fname, line);
}
}
template <class V1, typename V2>
......@@ -121,10 +138,11 @@ inline void caf_check_value(V1 v1, V2 v2, const char* fname, size_t line,
bool expected = true,
typename enable_integral<true, V1, V2>::type* =
0) {
if ((v1 == static_cast<V1>(v2)) == expected)
if ((v1 == static_cast<V1>(v2)) == expected) {
caf_passed(fname, line);
else
} else {
caf_failed(v1, v2, fname, line);
}
}
#define CAF_VERBOSE_EVAL(LineOfCode) \
......@@ -160,7 +178,7 @@ inline void caf_check_value(V1 v1, V2 v2, const char* fname, size_t line,
} else { \
CAF_PRINT("passed"); \
} \
CAF_VOID_STMT
static_cast<void>(0)
#define CAF_CHECK_EQUAL(lhs_loc, rhs_loc) \
caf_check_value((lhs_loc), (rhs_loc), __FILE__, __LINE__)
......@@ -173,7 +191,7 @@ inline void caf_check_value(V1 v1, V2 v2, const char* fname, size_t line,
CAF_PRINTERR("ERROR: " << err_msg); \
caf_inc_error_count(); \
} \
((void)0)
static_cast<void>(0)
#define CAF_CHECKPOINT() CAF_PRINT("passed")
......@@ -185,12 +203,16 @@ inline void caf_check_value(V1 v1, V2 v2, const char* fname, size_t line,
// some convenience macros for defining callbacks
#define CAF_CHECKPOINT_CB() \
[] { CAF_CHECKPOINT(); }
#define CAF_FAILURE_CB(err_msg) \
[] { CAF_FAILURE(err_msg); }
#define CAF_UNEXPECTED_MSG_CB(selfptr) \
[=] { CAF_UNEXPECTED_MSG(selfptr); }
#define CAF_UNEXPECTED_MSG_CB_REF(selfref) \
[&] { CAF_UNEXPECTED_MSG(selfref); }
#define CAF_UNEXPECTED_TOUT_CB() \
[] { CAF_UNEXPECTED_TOUT(); }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment