Unverified Commit b618678f authored by Shariar Azad Riday's avatar Shariar Azad Riday Committed by GitHub

Add missing check predicates

parent dabbb358
......@@ -58,10 +58,7 @@ public:
bool check_eq(const T0& lhs, const T1& rhs,
const detail::source_location& location
= detail::source_location::current()) {
if (std::is_integral_v<T0> && std::is_integral_v<T1>) {
static_assert(std::is_signed_v<T0> == std::is_signed_v<T1>,
"comparing signed and unsigned integers is unsafe");
}
assert_save_comparison<T0, T1>();
if (lhs == rhs) {
reporter::instance->pass(location);
return true;
......@@ -71,6 +68,81 @@ public:
return false;
}
/// Checks whether `lhs` and `rhs` are unequal.
template <class T0, class T1>
bool check_ne(const T0& lhs, const T1& rhs,
const detail::source_location& location
= detail::source_location::current()) {
assert_save_comparison<T0, T1>();
if (lhs != rhs) {
reporter::instance->pass(location);
return true;
}
reporter::instance->fail(binary_predicate::ne, stringify(lhs),
stringify(rhs), location);
return false;
}
/// Checks whether `lhs` is less than `rhs`.
template <class T0, class T1>
bool check_lt(const T0& lhs, const T1& rhs,
const detail::source_location& location
= detail::source_location::current()) {
assert_save_comparison<T0, T1>();
if (lhs < rhs) {
reporter::instance->pass(location);
return true;
}
reporter::instance->fail(binary_predicate::lt, stringify(lhs),
stringify(rhs), location);
return false;
}
/// Checks whether `lhs` less than or equal to `rhs`.
template <class T0, class T1>
bool check_le(const T0& lhs, const T1& rhs,
const detail::source_location& location
= detail::source_location::current()) {
assert_save_comparison<T0, T1>();
if (lhs <= rhs) {
reporter::instance->pass(location);
return true;
}
reporter::instance->fail(binary_predicate::le, stringify(lhs),
stringify(rhs), location);
return false;
}
/// Checks whether `lhs` is greater than `rhs`.
template <class T0, class T1>
bool check_gt(const T0& lhs, const T1& rhs,
const detail::source_location& location
= detail::source_location::current()) {
assert_save_comparison<T0, T1>();
if (lhs > rhs) {
reporter::instance->pass(location);
return true;
}
reporter::instance->fail(binary_predicate::gt, stringify(lhs),
stringify(rhs), location);
return false;
}
/// Checks whether `lhs` greater than or equal to `rhs`.
template <class T0, class T1>
bool check_ge(const T0& lhs, const T1& rhs,
const detail::source_location& location
= detail::source_location::current()) {
assert_save_comparison<T0, T1>();
if (lhs >= rhs) {
reporter::instance->pass(location);
return true;
}
reporter::instance->fail(binary_predicate::ge, stringify(lhs),
stringify(rhs), location);
return false;
}
bool check(bool value, const detail::source_location& location
= detail::source_location::current());
......@@ -103,6 +175,18 @@ public:
}
#endif
template <class Expr>
void should_fail(Expr&& expr, const caf::detail::source_location& location
= caf::detail::source_location::current()) {
auto* rep = reporter::instance;
auto before = rep->test_stats();
expr();
auto after = rep->test_stats();
check_eq(before.passed, after.passed, location);
if (check_eq(before.failed + 1, after.failed, location))
rep->test_stats({before.passed + 1, before.failed});
}
protected:
context_ptr ctx_;
std::string_view description_;
......@@ -110,6 +194,14 @@ protected:
detail::source_location loc_;
private:
template <class T0, class T1>
static void assert_save_comparison() {
if constexpr (std::is_integral_v<T0> && std::is_integral_v<T1>) {
static_assert(std::is_signed_v<T0> == std::is_signed_v<T1>,
"comparing signed and unsigned integers is unsafe");
}
}
template <class T>
std::string stringify(const T& value) {
if constexpr (std::is_convertible_v<T, std::string>) {
......
......@@ -7,13 +7,37 @@
using caf::test::block_type;
TEST("tests can contain checks") {
TEST("tests can contain different types of checks") {
auto* rep = caf::test::reporter::instance;
for (int i = 0; i < 3; ++i)
check_eq(i, i);
auto stats = rep->test_stats();
check_eq(stats.passed, 3u);
check_eq(stats.failed, 0u);
SECTION("check_ne checks for inequality") {
check_ne(0, 1);
should_fail([this]() { check_ne(0, 0); });
}
SECTION("check_eq checks for equality") {
check_eq(1, 1);
should_fail([this]() { check_eq(1, 0); });
}
SECTION("check_ge checks that lhs is greater than or equal to rhs") {
check_ge(0, 0);
check_ge(2, 1);
should_fail([this]() { check_ge(1, 2); });
}
SECTION("check_gt checks that lhs is greater than rhs") {
check_gt(2, 1);
should_fail([this]() { check_gt(0, 0); });
should_fail([this]() { check_gt(1, 2); });
}
SECTION("check_le checks that lhs is less than or equal to rhs") {
check_le(0, 0);
check_le(1, 2);
should_fail([this]() { check_le(2, 1); });
}
SECTION("check_lt checks that lhs is less than rhs") {
check_lt(1, 2);
should_fail([this]() { check_lt(1, 1); });
should_fail([this]() { check_lt(2, 1); });
}
info("this test had {} checks", rep->test_stats().total());
}
......
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