Progmasoft Git

catch3

Progmasoft Catch3: an independently maintained C++17 test framework with a parallel runner, property checks, snapshots and structured results.

Clonegit clone https://git.progmasoft.com/catch3

Add CHECK_EQ, REQUIRE_EQ and the other comparison assertions

CHECK_EQ/NE/LT/LE/GT/GE and REQUIRE_EQ/NE/LT/LE/GT/GE take two values and
compare them with the operator their name gives. Each is CHECK( a op b ) or
REQUIRE( a op b ) with both sides in parentheses, so the expression is
decomposed as usual: both values are shown on failure, each argument is
evaluated once, and the assertion is reported as written. An argument with a
comma outside parentheses goes in parentheses.

They live in Progmasoft/Catch3/Comparisons.hpp, which TestMacros.hpp
includes, and follow CATCH_CONFIG_PREFIX_ALL (CATCH_CHECK_EQ, ...) and
CATCH_CONFIG_DISABLE like the other assertions. The header is listed in the
CMake and Meson builds and is in the amalgamation; Bazel and Xmake pick it
up already. The deprecated Premake build gets no new tests.

Tests: passing relations of every macro, an argument with a comma, single
evaluation, operator binding (flag ? 1 : 2 and 6 & 3), and expected failures
of each relation and of REQUIRE. CMake with clang 42/42 (Progmasoft suite 187
passed, 5 failed as expected), Bazel property tests, GCC syntax check, and
the prefixed and disabled configurations compile with -Werror.
Commit
047e22debbc0c6227621e07f1e119fcec3c663dc
Author
Leitwolf11 ·
Parents
f8c84ccf6344

Files · Download .tar.gz

Changes

 docs/assertions.md                     |  28 +++++
 extras/catch_amalgamated.hpp           |   3 +
 src/CMakeLists.txt                     |   1 +
 src/Progmasoft/Catch3/Comparisons.hpp  | 224 +++++++++++++++++++++++++++++++++
 src/Progmasoft/Catch3/TestMacros.hpp   |   3 +
 src/Progmasoft/Catch3/meson.build      |   1 +
 tests/CMakeLists.txt                   |   1 +
 tests/Progmasoft/BUILD.bazel           |   1 +
 tests/Progmasoft/Comparisons.tests.cpp |  91 ++++++++++++++
 tests/meson.build                      |   1 +
 10 files changed, 354 insertions(+)

diff --git a/docs/assertions.md b/docs/assertions.md
index 3ac48f30..585eeb53 100644
--- a/docs/assertions.md
+++ b/docs/assertions.md
@@ -6,6 +6,7 @@
 
 **Contents**<br>
 [Natural Expressions](#natural-expressions)<br>
+[Comparison macros](#comparison-macros)<br>
 [Floating point comparisons](#floating-point-comparisons)<br>
 [Exceptions](#exceptions)<br>
 [Matcher expressions](#matcher-expressions)<br>
@@ -83,6 +84,33 @@ options.
    your tests.
 
 
+## Comparison macros
+
+Progmasoft Catch3 also has a macro for each comparison operator, for code
+that reads better with the two values apart, or that comes from a framework
+written that way:
+
+| Macro | Holds when |
+| --- | --- |
+| `CHECK_EQ( a, b )`, `REQUIRE_EQ( a, b )` | `a == b` |
+| `CHECK_NE( a, b )`, `REQUIRE_NE( a, b )` | `a != b` |
+| `CHECK_LT( a, b )`, `REQUIRE_LT( a, b )` | `a < b` |
+| `CHECK_LE( a, b )`, `REQUIRE_LE( a, b )` | `a <= b` |
+| `CHECK_GT( a, b )`, `REQUIRE_GT( a, b )` | `a > b` |
+| `CHECK_GE( a, b )`, `REQUIRE_GE( a, b )` | `a >= b` |
+
+`REQUIRE_EQ( a, b )` is `REQUIRE( a == b )` with each side in parentheses:
+both values are shown when it fails, each argument is evaluated once, and
+the assertion is reported as written. An argument that holds a comma
+outside parentheses, such as `std::pair<int, int>{ 1, 2 }`, goes in
+parentheses: `REQUIRE_EQ( ( std::pair<int, int>{ 1, 2 } ), p )`.
+
+They follow the configuration of the other assertions: with
+`CATCH_CONFIG_PREFIX_ALL` they are `CATCH_CHECK_EQ`, `CATCH_REQUIRE_EQ` and
+so on, and with `CATCH_CONFIG_DISABLE` they do nothing. They come with
+`<Progmasoft/Catch3/TestMacros.hpp>`.
+
+
 ## Floating point comparisons
 
 Comparing floating point numbers is complex, and [so it has its own
diff --git a/extras/catch_amalgamated.hpp b/extras/catch_amalgamated.hpp
index e08d1062..f68e3a60 100644
--- a/extras/catch_amalgamated.hpp
+++ b/extras/catch_amalgamated.hpp
@@ -11558,6 +11558,9 @@ namespace Catch {
 
 // end of user facing macros
 
+// Progmasoft Catch3: CHECK_EQ, REQUIRE_EQ and the other assertions that
+// compare two values.
+#include <Progmasoft/Catch3/Comparisons.hpp>
 
 
 #if defined(CATCH_CONFIG_PREFIX_ALL) && !defined(CATCH_CONFIG_DISABLE)
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index f0de95b4..d3d94f82 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -49,6 +49,7 @@ set(IMPL_HEADERS
   ${SOURCES_DIR}/AssertionInfo.hpp
   ${SOURCES_DIR}/AssertionResult.hpp
   ${SOURCES_DIR}/CaseSensitive.hpp
+  ${SOURCES_DIR}/Comparisons.hpp
   ${SOURCES_DIR}/Config.hpp
   ${SOURCES_DIR}/GetRandomSeed.hpp
   ${SOURCES_DIR}/Message.hpp
diff --git a/src/Progmasoft/Catch3/Comparisons.hpp b/src/Progmasoft/Catch3/Comparisons.hpp
new file mode 100644
index 00000000..b98b02a3
--- /dev/null
+++ b/src/Progmasoft/Catch3/Comparisons.hpp
@@ -0,0 +1,224 @@
+// Copyright (c) 2026 Progmasoft.
+// SPDX-License-Identifier: LGPL-3.0-or-later
+#pragma once
+
+/// @file
+/// Assertions that compare two values: `CHECK_EQ( a, b )` and `REQUIRE_EQ( a, b
+/// )` with `NE`, `LT`, `LE`, `GT` and `GE` beside `EQ`. Each one is `CHECK( a
+/// == b )` or `REQUIRE( a == b )` with the operator named by the macro: both
+/// values are shown when it fails, and the assertion is reported as written,
+/// `REQUIRE_EQ( a, b )`. An argument that holds a comma outside parentheses,
+/// such as `std::pair<int, int>{ 1, 2 }`, goes in parentheses.
+///
+/// They follow the configuration of `CHECK` and `REQUIRE`: with
+/// `CATCH_CONFIG_PREFIX_ALL` they are `CATCH_CHECK_EQ` and the like, and with
+/// `CATCH_CONFIG_DISABLE` they do nothing.
+
+#include <Progmasoft/Catch3/Internal/TestMacroImpl.hpp>
+
+/// The assertion `macroName( lhs, rhs )`: decomposes `(lhs) op (rhs)` as
+/// `REQUIRE` decomposes its expression, so that both values are captured, and
+/// reports the text the user wrote.
+#define CATCH3_INTERNAL_COMPARE(macroName, resultDisposition, op, lhs, rhs)    \
+    do                                                                         \
+    { /* NOLINT(bugprone-infinite-loop) */                                     \
+        CATCH_INTERNAL_IGNORE_BUT_WARN((lhs)op(rhs));                          \
+        Catch::AssertionHandler catchAssertionHandler(                         \
+            macroName##_catch_sr,                                              \
+            CATCH_INTERNAL_LINEINFO,                                           \
+            CATCH_INTERNAL_STRINGIFY(lhs) ", " CATCH_INTERNAL_STRINGIFY(rhs),  \
+            resultDisposition);                                                \
+        INTERNAL_CATCH_TRY                                                     \
+        {                                                                      \
+            CATCH_INTERNAL_START_WARNINGS_SUPPRESSION                          \
+            CATCH_INTERNAL_SUPPRESS_PARENTHESES_WARNINGS                       \
+            catchAssertionHandler.handleExpr(                                  \
+                Catch::Decomposer()                                            \
+                <= (lhs)op(rhs)); /* NOLINT(bugprone-chained-comparison) */    \
+            CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION                           \
+        }                                                                      \
+        INTERNAL_CATCH_CATCH(catchAssertionHandler)                            \
+        catchAssertionHandler.complete();                                      \
+    } while (INTERNAL_CATCH_TEST_LOOP_CONDITION((lhs)op(rhs)))
+
+#if defined(CATCH_CONFIG_PREFIX_ALL) && !defined(CATCH_CONFIG_DISABLE)
+
+#    define CATCH_CHECK_EQ(lhs, rhs)                                           \
+        CATCH3_INTERNAL_COMPARE("CATCH_CHECK_EQ",                              \
+                                Catch::ResultDisposition::ContinueOnFailure,   \
+                                ==,                                            \
+                                lhs,                                           \
+                                rhs)
+#    define CATCH_CHECK_NE(lhs, rhs)                                           \
+        CATCH3_INTERNAL_COMPARE("CATCH_CHECK_NE",                              \
+                                Catch::ResultDisposition::ContinueOnFailure,   \
+                                !=,                                            \
+                                lhs,                                           \
+                                rhs)
+#    define CATCH_CHECK_LT(lhs, rhs)                                           \
+        CATCH3_INTERNAL_COMPARE("CATCH_CHECK_LT",                              \
+                                Catch::ResultDisposition::ContinueOnFailure,   \
+                                <,                                             \
+                                lhs,                                           \
+                                rhs)
+#    define CATCH_CHECK_LE(lhs, rhs)                                           \
+        CATCH3_INTERNAL_COMPARE("CATCH_CHECK_LE",                              \
+                                Catch::ResultDisposition::ContinueOnFailure,   \
+                                <=,                                            \
+                                lhs,                                           \
+                                rhs)
+#    define CATCH_CHECK_GT(lhs, rhs)                                           \
+        CATCH3_INTERNAL_COMPARE("CATCH_CHECK_GT",                              \
+                                Catch::ResultDisposition::ContinueOnFailure,   \
+                                >,                                             \
+                                lhs,                                           \
+                                rhs)
+#    define CATCH_CHECK_GE(lhs, rhs)                                           \
+        CATCH3_INTERNAL_COMPARE("CATCH_CHECK_GE",                              \
+                                Catch::ResultDisposition::ContinueOnFailure,   \
+                                >=,                                            \
+                                lhs,                                           \
+                                rhs)
+#    define CATCH_REQUIRE_EQ(lhs, rhs)                                         \
+        CATCH3_INTERNAL_COMPARE("CATCH_REQUIRE_EQ",                            \
+                                Catch::ResultDisposition::Normal,              \
+                                ==,                                            \
+                                lhs,                                           \
+                                rhs)
+#    define CATCH_REQUIRE_NE(lhs, rhs)                                         \
+        CATCH3_INTERNAL_COMPARE("CATCH_REQUIRE_NE",                            \
+                                Catch::ResultDisposition::Normal,              \
+                                !=,                                            \
+                                lhs,                                           \
+                                rhs)
+#    define CATCH_REQUIRE_LT(lhs, rhs)                                         \
+        CATCH3_INTERNAL_COMPARE("CATCH_REQUIRE_LT",                            \
+                                Catch::ResultDisposition::Normal,              \
+                                <,                                             \
+                                lhs,                                           \
+                                rhs)
+#    define CATCH_REQUIRE_LE(lhs, rhs)                                         \
+        CATCH3_INTERNAL_COMPARE("CATCH_REQUIRE_LE",                            \
+                                Catch::ResultDisposition::Normal,              \
+                                <=,                                            \
+                                lhs,                                           \
+                                rhs)
+#    define CATCH_REQUIRE_GT(lhs, rhs)                                         \
+        CATCH3_INTERNAL_COMPARE("CATCH_REQUIRE_GT",                            \
+                                Catch::ResultDisposition::Normal,              \
+                                >,                                             \
+                                lhs,                                           \
+                                rhs)
+#    define CATCH_REQUIRE_GE(lhs, rhs)                                         \
+        CATCH3_INTERNAL_COMPARE("CATCH_REQUIRE_GE",                            \
+                                Catch::ResultDisposition::Normal,              \
+                                >=,                                            \
+                                lhs,                                           \
+                                rhs)
+
+#elif defined(CATCH_CONFIG_PREFIX_ALL) && defined(CATCH_CONFIG_DISABLE)
+
+#    define CATCH_CHECK_EQ(lhs, rhs) (void)(0)
+#    define CATCH_CHECK_NE(lhs, rhs) (void)(0)
+#    define CATCH_CHECK_LT(lhs, rhs) (void)(0)
+#    define CATCH_CHECK_LE(lhs, rhs) (void)(0)
+#    define CATCH_CHECK_GT(lhs, rhs) (void)(0)
+#    define CATCH_CHECK_GE(lhs, rhs) (void)(0)
+#    define CATCH_REQUIRE_EQ(lhs, rhs) (void)(0)
+#    define CATCH_REQUIRE_NE(lhs, rhs) (void)(0)
+#    define CATCH_REQUIRE_LT(lhs, rhs) (void)(0)
+#    define CATCH_REQUIRE_LE(lhs, rhs) (void)(0)
+#    define CATCH_REQUIRE_GT(lhs, rhs) (void)(0)
+#    define CATCH_REQUIRE_GE(lhs, rhs) (void)(0)
+
+#elif !defined(CATCH_CONFIG_PREFIX_ALL) && !defined(CATCH_CONFIG_DISABLE)
+
+#    define CHECK_EQ(lhs, rhs)                                                 \
+        CATCH3_INTERNAL_COMPARE("CHECK_EQ",                                    \
+                                Catch::ResultDisposition::ContinueOnFailure,   \
+                                ==,                                            \
+                                lhs,                                           \
+                                rhs)
+#    define CHECK_NE(lhs, rhs)                                                 \
+        CATCH3_INTERNAL_COMPARE("CHECK_NE",                                    \
+                                Catch::ResultDisposition::ContinueOnFailure,   \
+                                !=,                                            \
+                                lhs,                                           \
+                                rhs)
+#    define CHECK_LT(lhs, rhs)                                                 \
+        CATCH3_INTERNAL_COMPARE("CHECK_LT",                                    \
+                                Catch::ResultDisposition::ContinueOnFailure,   \
+                                <,                                             \
+                                lhs,                                           \
+                                rhs)
+#    define CHECK_LE(lhs, rhs)                                                 \
+        CATCH3_INTERNAL_COMPARE("CHECK_LE",                                    \
+                                Catch::ResultDisposition::ContinueOnFailure,   \
+                                <=,                                            \
+                                lhs,                                           \
+                                rhs)
+#    define CHECK_GT(lhs, rhs)                                                 \
+        CATCH3_INTERNAL_COMPARE("CHECK_GT",                                    \
+                                Catch::ResultDisposition::ContinueOnFailure,   \
+                                >,                                             \
+                                lhs,                                           \
+                                rhs)
+#    define CHECK_GE(lhs, rhs)                                                 \
+        CATCH3_INTERNAL_COMPARE("CHECK_GE",                                    \
+                                Catch::ResultDisposition::ContinueOnFailure,   \
+                                >=,                                            \
+                                lhs,                                           \
+                                rhs)
+#    define REQUIRE_EQ(lhs, rhs)                                               \
+        CATCH3_INTERNAL_COMPARE("REQUIRE_EQ",                                  \
+                                Catch::ResultDisposition::Normal,              \
+                                ==,                                            \
+                                lhs,                                           \
+                                rhs)
+#    define REQUIRE_NE(lhs, rhs)                                               \
+        CATCH3_INTERNAL_COMPARE("REQUIRE_NE",                                  \
+                                Catch::ResultDisposition::Normal,              \
+                                !=,                                            \
+                                lhs,                                           \
+                                rhs)
+#    define REQUIRE_LT(lhs, rhs)                                               \
+        CATCH3_INTERNAL_COMPARE("REQUIRE_LT",                                  \
+                                Catch::ResultDisposition::Normal,              \
+                                <,                                             \
+                                lhs,                                           \
+                                rhs)
+#    define REQUIRE_LE(lhs, rhs)                                               \
+        CATCH3_INTERNAL_COMPARE("REQUIRE_LE",                                  \
+                                Catch::ResultDisposition::Normal,              \
+                                <=,                                            \
+                                lhs,                                           \
+                                rhs)
+#    define REQUIRE_GT(lhs, rhs)                                               \
+        CATCH3_INTERNAL_COMPARE("REQUIRE_GT",                                  \
+                                Catch::ResultDisposition::Normal,              \
+                                >,                                             \
+                                lhs,                                           \
+                                rhs)
+#    define REQUIRE_GE(lhs, rhs)                                               \
+        CATCH3_INTERNAL_COMPARE("REQUIRE_GE",                                  \
+                                Catch::ResultDisposition::Normal,              \
+                                >=,                                            \
+                                lhs,                                           \
+                                rhs)
+
+#elif !defined(CATCH_CONFIG_PREFIX_ALL) && defined(CATCH_CONFIG_DISABLE)
+
+#    define CHECK_EQ(lhs, rhs) (void)(0)
+#    define CHECK_NE(lhs, rhs) (void)(0)
+#    define CHECK_LT(lhs, rhs) (void)(0)
+#    define CHECK_LE(lhs, rhs) (void)(0)
+#    define CHECK_GT(lhs, rhs) (void)(0)
+#    define CHECK_GE(lhs, rhs) (void)(0)
+#    define REQUIRE_EQ(lhs, rhs) (void)(0)
+#    define REQUIRE_NE(lhs, rhs) (void)(0)
+#    define REQUIRE_LT(lhs, rhs) (void)(0)
+#    define REQUIRE_LE(lhs, rhs) (void)(0)
+#    define REQUIRE_GT(lhs, rhs) (void)(0)
+#    define REQUIRE_GE(lhs, rhs) (void)(0)
+
+#endif
diff --git a/src/Progmasoft/Catch3/TestMacros.hpp b/src/Progmasoft/Catch3/TestMacros.hpp
index 718e4b5f..87d565d2 100644
--- a/src/Progmasoft/Catch3/TestMacros.hpp
+++ b/src/Progmasoft/Catch3/TestMacros.hpp
@@ -239,3 +239,6 @@
 
 // end of user facing macros
 
+// Progmasoft Catch3: CHECK_EQ, REQUIRE_EQ and the other assertions that
+// compare two values.
+#include <Progmasoft/Catch3/Comparisons.hpp>
diff --git a/src/Progmasoft/Catch3/meson.build b/src/Progmasoft/Catch3/meson.build
index 4bbab28e..25e9eb7b 100644
--- a/src/Progmasoft/Catch3/meson.build
+++ b/src/Progmasoft/Catch3/meson.build
@@ -167,6 +167,7 @@ internal_headers = [
   'AssertionInfo.hpp',
   'AssertionResult.hpp',
   'CaseSensitive.hpp',
+  'Comparisons.hpp',
   'Config.hpp',
   'GetRandomSeed.hpp',
   'Message.hpp',
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 32d1ce57..e9ff5935 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -154,6 +154,7 @@ target_link_libraries(SelfTest PRIVATE Catch3WithMain)
 
 # Exercise the Progmasoft layer independently of the upstream Catch2 SelfTest.
 add_executable(ProgmasoftSelfTest
+  Progmasoft/Comparisons.tests.cpp
   Progmasoft/Generators.tests.cpp
   Progmasoft/Mock.tests.cpp
   Progmasoft/Property.tests.cpp
diff --git a/tests/Progmasoft/BUILD.bazel b/tests/Progmasoft/BUILD.bazel
index b95c5b44..6dfe6481 100644
--- a/tests/Progmasoft/BUILD.bazel
+++ b/tests/Progmasoft/BUILD.bazel
@@ -7,6 +7,7 @@ package(default_visibility = ["//visibility:public"])
 cc_test(
     name = "property_tests",
     srcs = [
+        "Comparisons.tests.cpp",
         "ForAll.tests.cpp",
         "Generators.tests.cpp",
         "Mock.tests.cpp",
diff --git a/tests/Progmasoft/Comparisons.tests.cpp b/tests/Progmasoft/Comparisons.tests.cpp
new file mode 100644
index 00000000..aa24d73d
--- /dev/null
+++ b/tests/Progmasoft/Comparisons.tests.cpp
@@ -0,0 +1,91 @@
+// Copyright (c) 2026 Progmasoft.
+// SPDX-License-Identifier: LGPL-3.0-or-later
+
+#include <Progmasoft/Catch3/TestMacros.hpp>
+#include <string>
+#include <utility>
+#include <vector>
+
+namespace
+{
+    int
+    Next(int &calls)
+    {
+        ++calls;
+        return calls;
+    }
+} // namespace
+
+TEST_CASE("Comparison assertions pass when the relation holds",
+          "[progmasoft][comparisons]")
+{
+    CHECK_EQ(2 + 2, 4);
+    CHECK_NE(1, 2);
+    CHECK_LT(1, 2);
+    CHECK_LE(2, 2);
+    CHECK_GT(3, 2);
+    CHECK_GE(3, 3);
+    REQUIRE_EQ(std::string("catch"), "catch");
+    REQUIRE_NE(std::string("catch"), "catch3");
+    REQUIRE_LT(1.5, 2.0);
+    REQUIRE_LE('a', 'a');
+    REQUIRE_GT(10u, 9u);
+    REQUIRE_GE((std::vector<int>{ 1, 2 }.size()), 2u);
+}
+
+TEST_CASE("An argument with a comma goes in parentheses",
+          "[progmasoft][comparisons]")
+{
+    REQUIRE_EQ((std::pair<int, int>{ 1, 2 }), (std::pair<int, int>{ 1, 2 }));
+}
+
+TEST_CASE("Each argument is evaluated once", "[progmasoft][comparisons]")
+{
+    int calls = 0;
+    CHECK_EQ(Next(calls), 1);
+    REQUIRE_LT(Next(calls), 3);
+    REQUIRE_EQ(calls, 2);
+}
+
+TEST_CASE("The operator binds to each whole argument",
+          "[progmasoft][comparisons]")
+{
+    // Without parentheses around each side, 1 + 1 == 2 * 1 would still hold,
+    // but a < b ? x : y would not parse as intended.
+    const bool flag = true;
+    REQUIRE_EQ(flag ? 1 : 2, 1);
+    REQUIRE_EQ(6 & 3, 2);
+}
+
+TEST_CASE("CHECK_EQ fails when the values differ",
+          "[progmasoft][comparisons][!shouldfail]")
+{
+    CHECK_EQ(1, 2);
+}
+
+TEST_CASE("CHECK_NE fails when the values are equal",
+          "[progmasoft][comparisons][!shouldfail]")
+{
+    CHECK_NE(2, 2);
+}
+
+TEST_CASE("CHECK_LT and CHECK_GT fail on equal values",
+          "[progmasoft][comparisons][!shouldfail]")
+{
+    CHECK_LT(2, 2);
+    CHECK_GT(2, 2);
+}
+
+TEST_CASE("CHECK_LE and CHECK_GE fail on the wrong side",
+          "[progmasoft][comparisons][!shouldfail]")
+{
+    CHECK_LE(3, 2);
+    CHECK_GE(2, 3);
+}
+
+TEST_CASE("REQUIRE_EQ stops the test case when it fails",
+          "[progmasoft][comparisons][!shouldfail]")
+{
+    REQUIRE_EQ(std::string("a"), "b");
+    FAIL("REQUIRE_EQ did not stop the test case");
+}
diff --git a/tests/meson.build b/tests/meson.build
index f4953a19..06734272 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -72,6 +72,7 @@ test('SelfTest', self_test, args: ['--no-history'])
 progmasoft_property_tests = executable(
   'ProgmasoftPropertyTests',
   [
+    'Progmasoft/Comparisons.tests.cpp',
     'Progmasoft/Generators.tests.cpp',
     'Progmasoft/Mock.tests.cpp',
     'Progmasoft/Property.tests.cpp',