Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

Testing and assertions

Tests compare program behavior with expectations, while assertions express conditions that should hold. Learn property-based testing, idempotence, and a deterministic test corpus, plus the distinction between assert, static_assert, and checks that remain active when NDEBUG is defined.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

assert(condition); static_assert(constant_condition, "message");

Testing checks program behavior for selected inputs. An example-based test compares a result with a specific expected value; property-based testing checks a general rule across many inputs, often generated automatically. Passing tests alone does not prove correctness for every possible input.

idempotence means f(f(x)) == f(x): applying an operation again does not change its result. The function clamp_score limits a number to [0, 10]. The example checks expected values, the output range, and idempotence. Idempotence alone is insufficient: an incorrect function that always returns 0 also satisfies it.

A deterministic test corpus is a fixed, ordered collection of cases that makes a test run repeatable. Here it contains seven inputs, including boundaries and out-of-range values. This is a small property check over a fixed corpus, not a random generator or exhaustive domain coverage. When generating inputs, retain the seed, generator configuration, and failing cases; a seed alone does not guarantee reproduction after the generator changes.

assert(condition) from <cassert> checks a condition at runtime; failure of an enabled assertion produces a diagnostic and aborts the program. If NDEBUG is defined when the header is included, the condition is not evaluated, so never put required side effects inside it. static_assert checks a constant condition at compile time. The example uses explicit if and return 1 to keep runtime checks active even with NDEBUG; assertions do not replace validation of external input.

The example uses C++23-compatible syntax. The eel.is source is an evolving working draft, not a frozen C++23 publication. A repository compiler flag does not establish production runtime support. The displayed source was run unchanged in an isolated environment, and its standard output (stdout) matched the output below exactly.

Examples

#include <array>
#include <iostream>

constexpr int clamp_score(int x) {
    return x < 0 ? 0 : (x > 10 ? 10 : x);
}

static_assert(clamp_score(-1) == 0, "lower boundary");
static_assert(clamp_score(11) == 10, "upper boundary");

struct Case {
    int input;
    int expected;
};

int main() {
    constexpr std::array<Case, 7> corpus{{
        {-3, 0}, {-1, 0}, {0, 0}, {1, 1},
        {9, 9}, {10, 10}, {11, 10}
    }};

    for (const auto& test : corpus) {
        const int y = clamp_score(test.input);
        if (y != test.expected || y < 0 || y > 10 ||
            clamp_score(y) != y) {
            std::cerr << "FAIL input=" << test.input << '\n';
            return 1;
        }
    }

    std::cout << corpus.size() << " cases passed\n";
    return 0;
}

Result

7 cases passed

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources