Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

testing and assertions — concept C23

`assert` checks an invariant in debug builds but `NDEBUG` may remove it; tests must separately cover boundaries, failures, and resources.

Version: C23

Syntax

testing and assertions

assert checks an invariant in debug builds but NDEBUG may remove it; tests must separately cover boundaries, failures, and resources.

A unit test should call the public contract with typical input, boundaries, and a deliberately triggered failure, checking both the result and resource state. assert suits programmer invariants, not user-data validation, because NDEBUG may remove the condition and its side effects.

Run tests with sanitizers and different optimization levels, but absence of a report does not prove absence of UB. Code depending on time, locale, randomness, or I/O needs an injected source or controlled environment to remain deterministic.

Examples

#include <assert.h>
#include <stdio.h>

static int add(int a, int b) { return a + b; }
int main(void) { assert(add(2, 3) == 5); puts("passed"); return 0; }

Result

passed

`assert` checks an invariant in debug builds but `NDEBUG` may remove it; tests must separately cover boundaries, failures, and resources.

Sources