Commit b452c7f6 authored by Paul's avatar Paul
Browse files

Update tests to chow values that failed

parent 6668f42e
...@@ -7,48 +7,112 @@ ...@@ -7,48 +7,112 @@
#ifndef RTG_GUARD_TEST_TEST_HPP #ifndef RTG_GUARD_TEST_TEST_HPP
#define RTG_GUARD_TEST_TEST_HPP #define RTG_GUARD_TEST_TEST_HPP
inline void failed(bool b, const char* msg, const char* file, int line) namespace test {
{ // NOLINTNEXTLINE
if (!b) #define TEST_FOREACH_OPERATOR(m) \
std::cout << "FAILED: " << msg << ": " << file << ": " << line << std::endl; m(==, equal) \
} m(!=, not_equal) \
m(<=, less_than_equal) \
m(>=, greater_than_equal) \
m(<, less_than) \
m(>, greater_than)
// NOLINTNEXTLINE
#define TEST_EACH_OPERATOR_OBJECT(op, name) \
struct name \
{ \
static std::string as_string() { return #op; } \
template<class T, class U> \
static decltype(auto) call(T&& x, U&& y) { return x op y; } \
};
inline void failed_abort(bool b, const char* msg, const char* file, int line) TEST_FOREACH_OPERATOR(TEST_EACH_OPERATOR_OBJECT)
template<class T, class U, class Operator>
struct expression
{ {
if (!b) T lhs;
U rhs;
friend std::ostream& operator<<(std::ostream& s, const expression& self)
{ {
std::cout << "FAILED: " << msg << ": " << file << ": " << line << std::endl; s << " [ " << self.lhs << " " << Operator::as_string() << " " << self.rhs << " ]";
std::abort(); return s;
} }
}
template <class TLeft, class TRight> decltype(auto) value() const { return Operator::call(lhs, rhs); };
inline void expect_equality(const TLeft& left, };
const TRight& right,
const char* left_s, template<class T, class U, class Operator>
const char* riglt_s, expression<typename std::decay<T>::type, typename std::decay<U>::type, Operator>
const char* file, make_expression(T&& rhs, U&& lhs, Operator)
int line)
{ {
if(left == right) return { std::forward<T>(rhs), std::forward<U>(lhs) };
return; }
std::cout << "FAILED: " << left_s << "(" << left << ") == " << riglt_s << "(" << right template<class T>
<< "): " << file << ':' << line << std::endl; struct lhs_expression;
std::abort();
template<class T>
lhs_expression<typename std::decay<T>::type> make_lhs_expression(T&& lhs)
{
return lhs_expression<typename std::decay<T>::type>{ std::forward<T>(lhs) };
} }
template<class T>
struct lhs_expression
{
T lhs;
explicit lhs_expression(T e) : lhs(e)
{}
friend std::ostream& operator<<(std::ostream& s, const lhs_expression& self)
{
s << self.lhs;
return s;
}
T value() const
{
return lhs;
}
// NOLINTNEXTLINE // NOLINTNEXTLINE
#define CHECK(...) failed(__VA_ARGS__, #__VA_ARGS__, __FILE__, __LINE__) #define TEST_LHS_OPERATOR(op, name) \
// NOLINTNEXTLINE template<class U> \
#define EXPECT(...) failed_abort(__VA_ARGS__, #__VA_ARGS__, __FILE__, __LINE__) auto operator op(const U& rhs) const { return make_expression(lhs, rhs, name{}); } // NOLINT
// NOLINTNEXTLINE
#define EXPECT_EQUAL(LEFT, RIGHT) expect_equality(LEFT, RIGHT, #LEFT, #RIGHT, __FILE__, __LINE__)
// NOLINTNEXTLINE
#define STATUS(...) EXPECT((__VA_ARGS__) == 0)
TEST_FOREACH_OPERATOR(TEST_LHS_OPERATOR)
// NOLINTNEXTLINE // NOLINTNEXTLINE
#define FAIL(...) failed(false, __VA_ARGS__, __FILE__, __LINE__) #define TEST_LHS_REOPERATOR(op) \
template<class U> auto operator op(const U& rhs) const { return make_lhs_expression(lhs op rhs); }
TEST_LHS_REOPERATOR(+)
TEST_LHS_REOPERATOR(-)
TEST_LHS_REOPERATOR(*)
TEST_LHS_REOPERATOR(/)
TEST_LHS_REOPERATOR(%)
TEST_LHS_REOPERATOR(&)
TEST_LHS_REOPERATOR(|)
TEST_LHS_REOPERATOR(&&)
TEST_LHS_REOPERATOR(||)
};
struct capture
{
template<class T>
auto operator->* (const T& x) { return make_lhs_expression(x); }
};
template<class T, class F>
void failed(T x, const char* msg, const char* file, int line, F f)
{
if (!x.value())
{
std::cout << file << ":" << line << ":" << std::endl;
std::cout << " FAILED: " << msg << " " << x << std::endl;
f();
}
}
template <class F> template <class F>
bool throws(F f) bool throws(F f)
...@@ -85,4 +149,13 @@ void run_test() ...@@ -85,4 +149,13 @@ void run_test()
t.run(); t.run();
} }
} // namespace test
// NOLINTNEXTLINE
#define CHECK(...) test::failed(test::capture{} ->* __VA_ARGS__, #__VA_ARGS__, __FILE__, __LINE__, []{})
// NOLINTNEXTLINE
#define EXPECT(...) test::failed(test::capture{} ->* __VA_ARGS__, #__VA_ARGS__, __FILE__, __LINE__, &std::abort)
// NOLINTNEXTLINE
#define STATUS(...) EXPECT((__VA_ARGS__) == 0)
#endif #endif
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