summaryrefslogtreecommitdiff
path: root/projects/SelfTest/UsageTests/Matchers.tests.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'projects/SelfTest/UsageTests/Matchers.tests.cpp')
-rw-r--r--projects/SelfTest/UsageTests/Matchers.tests.cpp117
1 files changed, 94 insertions, 23 deletions
diff --git a/projects/SelfTest/UsageTests/Matchers.tests.cpp b/projects/SelfTest/UsageTests/Matchers.tests.cpp
index b0b5d58e..23db7b53 100644
--- a/projects/SelfTest/UsageTests/Matchers.tests.cpp
+++ b/projects/SelfTest/UsageTests/Matchers.tests.cpp
@@ -52,11 +52,17 @@ namespace { namespace MatchersTests {
int i;
};
-#if !defined(CATCH_CONFIG_DISABLE_EXCEPTIONS)
+ struct DerivedException : std::exception {
+ char const* what() const noexcept override {
+ return "DerivedException::what";
+ }
+ };
+
void doesNotThrow() {}
+#if !defined(CATCH_CONFIG_DISABLE_EXCEPTIONS)
[[noreturn]]
- void throws(int i) {
+ void throwsSpecialException(int i) {
throw SpecialException{i};
}
@@ -64,6 +70,11 @@ namespace { namespace MatchersTests {
void throwsAsInt(int i) {
throw i;
}
+
+ [[noreturn]]
+ void throwsDerivedException() {
+ throw DerivedException{};
+ }
#endif
class ExceptionMatcher : public Catch::MatcherBase<SpecialException> {
@@ -322,8 +333,8 @@ namespace { namespace MatchersTests {
#if !defined(CATCH_CONFIG_DISABLE_EXCEPTIONS)
TEST_CASE("Exception matchers that succeed", "[matchers][exceptions][!throws]") {
- CHECK_THROWS_MATCHES(throws(1), SpecialException, ExceptionMatcher{1});
- REQUIRE_THROWS_MATCHES(throws(2), SpecialException, ExceptionMatcher{2});
+ CHECK_THROWS_MATCHES(throwsSpecialException(1), SpecialException, ExceptionMatcher{1});
+ REQUIRE_THROWS_MATCHES(throwsSpecialException(2), SpecialException, ExceptionMatcher{2});
}
TEST_CASE("Exception matchers that fail", "[matchers][exceptions][!throws][.failing]") {
@@ -336,13 +347,27 @@ namespace { namespace MatchersTests {
REQUIRE_THROWS_MATCHES(throwsAsInt(1), SpecialException, ExceptionMatcher{1});
}
SECTION("Contents are wrong") {
- CHECK_THROWS_MATCHES(throws(3), SpecialException, ExceptionMatcher{1});
- REQUIRE_THROWS_MATCHES(throws(4), SpecialException, ExceptionMatcher{1});
+ CHECK_THROWS_MATCHES(throwsSpecialException(3), SpecialException, ExceptionMatcher{1});
+ REQUIRE_THROWS_MATCHES(throwsSpecialException(4), SpecialException, ExceptionMatcher{1});
}
}
#endif
TEST_CASE("Floating point matchers: float", "[matchers][floating-point]") {
+ SECTION("Relative") {
+ REQUIRE_THAT(10.f, WithinRel(11.1f, 0.1f));
+ REQUIRE_THAT(10.f, !WithinRel(11.2f, 0.1f));
+ REQUIRE_THAT( 1.f, !WithinRel(0.f, 0.99f));
+ REQUIRE_THAT(-0.f, WithinRel(0.f));
+ SECTION("Some subnormal values") {
+ auto v1 = std::numeric_limits<float>::min();
+ auto v2 = v1;
+ for (int i = 0; i < 5; ++i) {
+ v2 = std::nextafter(v1, 0.f);
+ }
+ REQUIRE_THAT(v1, WithinRel(v2));
+ }
+ }
SECTION("Margin") {
REQUIRE_THAT(1.f, WithinAbs(1.f, 0));
REQUIRE_THAT(0.f, WithinAbs(1.f, 1));
@@ -351,7 +376,6 @@ namespace { namespace MatchersTests {
REQUIRE_THAT(0.f, !WithinAbs(1.f, 0.99f));
REQUIRE_THAT(0.f, WithinAbs(-0.f, 0));
- REQUIRE_THAT(NAN, !WithinAbs(NAN, 0));
REQUIRE_THAT(11.f, !WithinAbs(10.f, 0.5f));
REQUIRE_THAT(10.f, !WithinAbs(11.f, 0.5f));
@@ -362,30 +386,46 @@ namespace { namespace MatchersTests {
REQUIRE_THAT(1.f, WithinULP(1.f, 0));
REQUIRE_THAT(nextafter(1.f, 2.f), WithinULP(1.f, 1));
- REQUIRE_THAT(nextafter(1.f, 0.f), WithinULP(1.f, 1));
- REQUIRE_THAT(nextafter(1.f, 2.f), !WithinULP(1.f, 0));
+ REQUIRE_THAT(0.f, WithinULP(nextafter(0.f, 1.f), 1));
+ REQUIRE_THAT(1.f, WithinULP(nextafter(1.f, 0.f), 1));
+ REQUIRE_THAT(1.f, !WithinULP(nextafter(1.f, 2.f), 0));
REQUIRE_THAT(1.f, WithinULP(1.f, 0));
REQUIRE_THAT(-0.f, WithinULP(0.f, 0));
-
- REQUIRE_THAT(NAN, !WithinULP(NAN, 123));
}
SECTION("Composed") {
REQUIRE_THAT(1.f, WithinAbs(1.f, 0.5) || WithinULP(1.f, 1));
REQUIRE_THAT(1.f, WithinAbs(2.f, 0.5) || WithinULP(1.f, 0));
-
- REQUIRE_THAT(NAN, !(WithinAbs(NAN, 100) || WithinULP(NAN, 123)));
+ REQUIRE_THAT(0.0001f, WithinAbs(0.f, 0.001f) || WithinRel(0.f, 0.1f));
}
SECTION("Constructor validation") {
REQUIRE_NOTHROW(WithinAbs(1.f, 0.f));
REQUIRE_THROWS_AS(WithinAbs(1.f, -1.f), std::domain_error);
REQUIRE_NOTHROW(WithinULP(1.f, 0));
- REQUIRE_THROWS_AS(WithinULP(1.f, -1), std::domain_error);
+ REQUIRE_THROWS_AS(WithinULP(1.f, static_cast<uint64_t>(-1)), std::domain_error);
+
+ REQUIRE_NOTHROW(WithinRel(1.f, 0.f));
+ REQUIRE_THROWS_AS(WithinRel(1.f, -0.2f), std::domain_error);
+ REQUIRE_THROWS_AS(WithinRel(1.f, 1.f), std::domain_error);
}
}
TEST_CASE("Floating point matchers: double", "[matchers][floating-point]") {
+ SECTION("Relative") {
+ REQUIRE_THAT(10., WithinRel(11.1, 0.1));
+ REQUIRE_THAT(10., !WithinRel(11.2, 0.1));
+ REQUIRE_THAT(1., !WithinRel(0., 0.99));
+ REQUIRE_THAT(-0., WithinRel(0.));
+ SECTION("Some subnormal values") {
+ auto v1 = std::numeric_limits<double>::min();
+ auto v2 = v1;
+ for (int i = 0; i < 5; ++i) {
+ v2 = std::nextafter(v1, 0);
+ }
+ REQUIRE_THAT(v1, WithinRel(v2));
+ }
+ }
SECTION("Margin") {
REQUIRE_THAT(1., WithinAbs(1., 0));
REQUIRE_THAT(0., WithinAbs(1., 1));
@@ -393,8 +433,6 @@ namespace { namespace MatchersTests {
REQUIRE_THAT(0., !WithinAbs(1., 0.99));
REQUIRE_THAT(0., !WithinAbs(1., 0.99));
- REQUIRE_THAT(NAN, !WithinAbs(NAN, 0));
-
REQUIRE_THAT(11., !WithinAbs(10., 0.5));
REQUIRE_THAT(10., !WithinAbs(11., 0.5));
REQUIRE_THAT(-10., WithinAbs(-10., 0.5));
@@ -404,29 +442,43 @@ namespace { namespace MatchersTests {
REQUIRE_THAT(1., WithinULP(1., 0));
REQUIRE_THAT(nextafter(1., 2.), WithinULP(1., 1));
- REQUIRE_THAT(nextafter(1., 0.), WithinULP(1., 1));
- REQUIRE_THAT(nextafter(1., 2.), !WithinULP(1., 0));
+ REQUIRE_THAT(0., WithinULP(nextafter(0., 1.), 1));
+ REQUIRE_THAT(1., WithinULP(nextafter(1., 0.), 1));
+ REQUIRE_THAT(1., !WithinULP(nextafter(1., 2.), 0));
REQUIRE_THAT(1., WithinULP(1., 0));
REQUIRE_THAT(-0., WithinULP(0., 0));
-
- REQUIRE_THAT(NAN, !WithinULP(NAN, 123));
}
SECTION("Composed") {
REQUIRE_THAT(1., WithinAbs(1., 0.5) || WithinULP(2., 1));
REQUIRE_THAT(1., WithinAbs(2., 0.5) || WithinULP(1., 0));
-
- REQUIRE_THAT(NAN, !(WithinAbs(NAN, 100) || WithinULP(NAN, 123)));
+ REQUIRE_THAT(0.0001, WithinAbs(0., 0.001) || WithinRel(0., 0.1));
}
SECTION("Constructor validation") {
REQUIRE_NOTHROW(WithinAbs(1., 0.));
REQUIRE_THROWS_AS(WithinAbs(1., -1.), std::domain_error);
REQUIRE_NOTHROW(WithinULP(1., 0));
- REQUIRE_THROWS_AS(WithinULP(1., -1), std::domain_error);
+
+ REQUIRE_NOTHROW(WithinRel(1., 0.));
+ REQUIRE_THROWS_AS(WithinRel(1., -0.2), std::domain_error);
+ REQUIRE_THROWS_AS(WithinRel(1., 1.), std::domain_error);
}
}
+ TEST_CASE("Floating point matchers that are problematic in approvals", "[approvals][matchers][floating-point]") {
+ REQUIRE_THAT(NAN, !WithinAbs(NAN, 0));
+ REQUIRE_THAT(NAN, !(WithinAbs(NAN, 100) || WithinULP(NAN, 123)));
+ REQUIRE_THAT(NAN, !WithinULP(NAN, 123));
+ REQUIRE_THAT(INFINITY, WithinRel(INFINITY));
+ REQUIRE_THAT(-INFINITY, !WithinRel(INFINITY));
+ REQUIRE_THAT(1., !WithinRel(INFINITY));
+ REQUIRE_THAT(INFINITY, !WithinRel(1.));
+ REQUIRE_THAT(NAN, !WithinRel(NAN));
+ REQUIRE_THAT(1., !WithinRel(NAN));
+ REQUIRE_THAT(NAN, !WithinRel(1.));
+ }
+
TEST_CASE("Arbitrary predicate matcher", "[matchers][generic]") {
SECTION("Function pointer") {
REQUIRE_THAT(1, Predicate<int>(alwaysTrue, "always true"));
@@ -498,6 +550,25 @@ namespace { namespace MatchersTests {
}
}
+#if !defined(CATCH_CONFIG_DISABLE_EXCEPTIONS)
+ TEST_CASE("Exceptions matchers", "[matchers][exceptions][!throws]") {
+ REQUIRE_THROWS_MATCHES(throwsDerivedException(), DerivedException, Message("DerivedException::what"));
+ REQUIRE_THROWS_MATCHES(throwsDerivedException(), DerivedException, !Message("derivedexception::what"));
+ REQUIRE_THROWS_MATCHES(throwsSpecialException(2), SpecialException, !Message("DerivedException::what"));
+ REQUIRE_THROWS_MATCHES(throwsSpecialException(2), SpecialException, Message("SpecialException::what"));
+ }
+#endif
+
+ TEST_CASE("Composed matchers are distinct", "[matchers][composed]") {
+ auto m1 = Contains("string");
+ auto m2 = Contains("random");
+ auto composed1 = m1 || m2;
+ auto m3 = Contains("different");
+ auto composed2 = composed1 || m3;
+ REQUIRE_THAT(testStringForMatching2(), !composed1);
+ REQUIRE_THAT(testStringForMatching2(), composed2);
+ }
+
} } // namespace MatchersTests
#endif // CATCH_CONFIG_DISABLE_MATCHERS