# Properties, fuzzing and mutation

> The deep tests, for whoever adds to them: a RapidCheck property with the shared generators and a // covers: line, Hypothesis for the Python, a fuzz target with its corpus and regressions, a counterexample made a named case, mutation floors that only rise, and the nightly and weekly runs that open a nightly-finding issue.

Source: https://omardev29.github.io/rmp-docs/framework/contributing/properties-fuzzing-mutation.html

A unit test checks the cases its author thought of. The deep tests look for the ones nobody did: a property is checked against hundreds of generated inputs, a fuzz target is fed bytes nobody would write, and mutation testing changes the framework's code on purpose to see whether any test notices. The properties of the C++ and the fuzz targets run under AddressSanitizer and UndefinedBehaviorSanitizer, every report fatal.

## What runs when

Each has two halves. On every push the half that gives the same answer every time runs, so that a red is the commit's and never the dice's; and on a schedule the half that searches:

|  | Every push: the `deep` job | The search, `.github/workflows/deep.yml` |
|---|---|---|
| RapidCheck | a fixed seed, 200 cases a property | every night: a random seed, printed, and 5000 cases |
| Hypothesis | derandomized: the same 200 cases a test on every machine | every night: 5000 random cases a test |
| Fuzzing | every committed input replayed | every night: ten minutes of libFuzzer per target, from the committed corpus |
| Mutation | the floor files' own checks | every Sunday: every module, a job each |

`rmp test property` is part of the plain `rmp test`: first the campaign's own checks — `tests/campaign_test.py`, which holds every rule on this page to the files — then the RapidCheck properties, then the Hypothesis ones. `rmp test fuzz` and `rmp test mutation` are asked for by name. In CI the push's half is the `deep` job.

## A RapidCheck property

A property of a module is `tests/property/<module>_prop.cpp`. Every such file is compiled into one program, `property_test`, with doctest's `main()` and RapidCheck, and a new one needs no change to CMake. It says, in its header, what must hold for every input, and then checks it. This is a whole one, the clock's:

[tests/property/clock_prop.cpp](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/tests/property/clock_prop.cpp#L1-L127)

```cpp
// ---------------------------------------------------------------------------
// The framework's one clock (src/rmp/app/clock.cpp), held to the campaign's rules
// for any reading a machine or a test can hand it:
//
//   - what a frame hands the game is a finite number of seconds, 0 or more,
//     and never more than [app] max_delta when there is one -- a NaN or an
//     infinity from the reading would be every position in the world the
//     next frame;
//   - a reading the clamp has nothing to say about goes through as it is,
//     and one past max_delta is max_delta;
//   - a fixed step is what every frame hands the game and what the UI reads
//     back, whatever number the test fixed;
//   - the test seed and the frame observer are kept as they were given.
//
// Nothing here is public API: rmp::app::detail is the seam the runners and
// tests/e2e/driver.cpp use.
// ---------------------------------------------------------------------------

#include "check.h"
#include "gen.h"

#include "../../src/rmp/internal.h"

#include <rmp/app.h>
#include <rmp/config.h>
#include <rmp/object.h> // rmp::Callback

#include <doctest.h>
#include <rapidcheck.h>

#include <cmath>
#include <cstdint>
#include <optional>

namespace {

// Back to the real clock, no seed, no observer, whatever a case did.
void reset_clock() {
    rmp::app::detail::set_fixed_delta(0);
    rmp::app::detail::set_test_seed(std::nullopt);
    rmp::app::detail::set_frame_observer({});
    rmp::app::detail::forget_steps_for_tests();
}

// What [app] max_delta allows a frame to be: no ceiling when it is 0.
bool within_ceiling(float seconds, float max) {
    return !(max > 0) || !std::isfinite(max) || seconds <= max;
}

} // namespace

TEST_SUITE("property: clock") {
    TEST_CASE("a frame is a finite number of seconds, 0 or more, whatever the reading") {
        rmp::test::check(
            "clamp_delta is a finite number of seconds, 0 or more, at most max", [] {
                const float raw = *rmp::test::gen::hostile_float();
                const float max = *rmp::test::gen::hostile_float();
                const float out = rmp::app::detail::clamp_delta(raw, max);
                RC_ASSERT(std::isfinite(out));
                RC_ASSERT(out >= 0.0f);
                RC_ASSERT(within_ceiling(out, max));
            });
    }

    TEST_CASE("a reading the clamp has nothing to say about goes through as it is") {
        rmp::test::check(
            "clamp_delta keeps a reading inside the clamp, and cuts one past it", [] {
                const float raw = *rmp::test::gen::hostile_float();
                const float max = *rmp::test::gen::hostile_float();
                RC_PRE(std::isfinite(raw) && raw >= 0.0f);
                const float out = rmp::app::detail::clamp_delta(raw, max);
                const bool clamps = max > 0 && std::isfinite(max) && raw > max;
                RC_ASSERT(out == (clamps ? max : raw));
            });
    }

    TEST_CASE("a fixed step is what every frame hands the game, clamped") {
        rmp::test::check(
            "step_delta and frame_delta hand over the fixed step, clamped, every frame",
            [] {
                reset_clock();
                const float seconds = *rmp::test::gen::hostile_float();
                const int frames = *rc::gen::inRange(1, 5);
                rmp::app::detail::set_fixed_delta(seconds);
                for (int i = 0; i < frames; ++i) {
                    const float step = rmp::app::detail::step_delta();
                    RC_ASSERT(std::isfinite(step));
                    RC_ASSERT(step >= 0.0f);
                    RC_ASSERT(within_ceiling(step, RMP_MAX_DELTA));
                    RC_ASSERT(rmp::app::detail::frame_delta() == step);
                    // A fixed step that is a number of seconds is that number,
                    // clamped; anything else is the real clock, which no
                    // window here has ticked.
                    if (std::isfinite(seconds) && seconds > 0) {
                        RC_ASSERT(step ==
                                  rmp::app::detail::clamp_delta(seconds, RMP_MAX_DELTA));
                    }
                }
                reset_clock();
            });
    }

    TEST_CASE("the test seed and the frame observer are kept as given") {
        rmp::test::check(
            "set_test_seed reads back, and the observer hears every frame_ended", [] {
                reset_clock();
                std::optional<std::uint64_t> seed;
                if (*rc::gen::arbitrary<bool>()) {
                    seed = *rc::gen::arbitrary<std::uint64_t>();
                }
                rmp::app::detail::set_test_seed(seed);
                RC_ASSERT(rmp::app::detail::test_seed() == seed);

                static int heard = 0;
                heard = 0;
                const int frames = *rc::gen::inRange(0, 8);
                rmp::app::detail::set_frame_observer([] { ++heard; });
                for (int i = 0; i < frames; ++i) rmp::app::detail::frame_ended();
                RC_ASSERT(heard == frames);
                rmp::app::detail::set_frame_observer({});
                rmp::app::detail::frame_ended();
                RC_ASSERT(heard == frames);
                reset_clock();
            });
    }
}
```

Each property is a `check()` of `tests/property/check.h`: a description, and a function whose `RC_ASSERT` lines must hold for whatever RapidCheck draws. It is one doctest check, silent when it holds and counted in doctest's summary; when it does not hold, the failure says the smallest input RapidCheck could shrink the counterexample to.

- **The description is a string literal, and no two are alike.** It is the key a reproduce string finds the property by: two properties with one description would both be replayed against one case.
- **Never RapidCheck's own `rc::check()`.** It prints and returns a bool, so a property that fails through it is a line in the log and a green run.
- **Draw what a game can hand the framework by mistake**, from `tests/property/gen.h`. RapidCheck's own floats almost never draw a NaN, and its strings almost never hold a NUL — exactly the inputs the framework's rules are about. `hostile_float()` and `hostile_double()` draw NaN, both infinities, the largest and smallest normals, denormals, `-0` and values past `int`'s range half the time; `hostile_rect()` four of those; `hostile_string()` a NUL inside, several-byte UTF-8 and bytes that are not UTF-8; `value()` a `rmp::Value` tree, sometimes deeper than a save reads back.
- **A new generator is held to what it promises**, in `tests/property/gen_prop.cpp`: a generator that quietly stopped drawing NaN would turn every property that relies on it green by not looking.

The program runs at a fixed seed and 200 cases a property unless the environment says otherwise, so `rmp test` and every push play the same cases. A seed of your own, more cases, or one suite:

```shell
rmp test property
cmake --build --preset debug --target property_test
./build/debug/property_test -ts="property: random"
RC_PARAMS="seed=7 max_success=5000" ./build/debug/property_test
```

When a property fails, RapidCheck prints, as the program ends, an `RC_PARAMS` value of the form `reproduce=…` that plays exactly that case again. Paste it in front of the program the same way as the seed above.

### Every public function, held by a property

The properties hold the framework to four rules for any argument a game can pass: no undefined behaviour on any input; a documented safe answer, and one report, for a game's mistake; no NaN kept in the state; and nothing changed by a refusal. Every public function of `include/rmp/` is held to them, or is waived.

A property says which functions it holds with a comment above the case, one function a line, spelled as the coverage report prints it — as in the [random generator's properties](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/tests/property/random_prop.cpp#L102-L105):

```text
TEST_SUITE("property: random") {
    // covers: rmp::random::seed(uint64_t)
    // covers: rmp::random::value()
```

- **A public function no property covers and `tests/property/waivers.txt` does not list fails.** A new public function arrives with its property.
- **The waivers only shrink.** A waived function a property covers fails until its line goes, and so does a line for a function that is gone.
- **A `covers` line is a property's, not a promise.** It counts only in a file whose code calls the function, so a comment cannot take a function off the waivers with nothing behind it.

RapidCheck's `RC_ASSERT` throws an exception, which is allowed in the test programs that link RapidCheck and nowhere else: nothing under `src/rmp/` or `include/rmp/` throws or includes RapidCheck, and a test fails the day something does.

## A counterexample becomes a named case

What a property finds is written down as a case of its own, in the module's unit tests, with a comment that says which property found it. The property finds it again only when its seed happens to; the named case checks it on every run. The random generator's properties found that `range()` on floats could land on its upper end where rounding put it there, from one seed; `tests/unit_test.cpp` holds that seed to it, by name:

```text
TEST_CASE("range(float) never lands on max, even where rounding puts it there") {
    // random_prop.cpp's counterexample: the seed whose first value() is
    ...
```

The order is the one every test follows: the named case first, seen red on the code as it was, then the fix, then green — [seen red first](https://omardev29.github.io/rmp-docs/framework/contributing/tests.html#red-first).

## Hypothesis, for the Python

The tools are Python, and their properties are Hypothesis's, in `tests/<what>_property_test.py`, which the stage finds by that name. `tests/cpp_text_property_test.py` is written as the one to copy:

- **`import
hypothesis_profiles` comes first.** It loads the profile every property test shares and, where Hypothesis is not installed, skips the module and says so.
- **The tool is loaded by its path, as `tools.<name>`**, the name mutation testing records a test's reach under.
- **The strategies lean on what the code decides on.** Text drawn at random almost never writes the start of a raw string literal; the scanner's property mixes those pieces in on purpose.

There are two profiles. `ci` is the default: derandomized, the same 200 cases a test on every run and every machine, no example database and no deadline — the sanitizers and a loaded runner make timings meaningless. `nightly` draws 5000 at random and keeps what it found in `build/hypothesis/`, where a counterexample found once is tried first next time:

```shell
python3 -m unittest discover -s tests -p '*_property_test.py' -v
RMP_HYPOTHESIS_PROFILE=nightly python3 -m unittest discover -s tests -p cpp_text_property_test.py -v
RMP_REQUIRE_HYPOTHESIS=1 python3 -m unittest discover -s tests -p '*_property_test.py' -v
```

The last line is how CI runs them: there, a missing Hypothesis is a failure, because the build image has it and a skip would be a green that tested nothing. On your machine, install the version `thirdparty/FROZEN_VERSIONS.md` pins. A counterexample prints the `@reproduce_failure` line that plays it again, and becomes a named case in the tool's own unit tests, like a RapidCheck one.

## A fuzz target

A fuzz target is `tests/fuzz/<name>.cpp`, one parser of the framework fed whatever bytes libFuzzer makes up. It includes `tests/fuzz/harness.h`, which makes a replay depend on the input and nothing else: no log, resources from an empty folder of its own instead of the game's, `[dev] strict` off, and the once-only reports forgotten before each input. The Tiled reader's is the smallest:

[tests/fuzz/tiled.cpp](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/tests/fuzz/tiled.cpp#L1-L50)

```cpp
// ---------------------------------------------------------------------------
// The Tiled reader (src/rmp/tilemap/tilemap.cpp over thirdparty/cute_tiled), fuzzed.
//
// The input is the bytes of a .json map, exactly as rmp::assets::load_data
// hands them over: no NUL after them. The oracle is the reader's promise --
// it returns a map or refuses one, never crashes, never reads past the bytes
// and never does UB on the way (cute_tiled is instrumented like our code:
// cmake/sanitize_ignore.txt leaves it nothing) -- and then everything
// tests/fuzz/map_walk.h asks of any map a reader accepted.
//
// Inputs: tests/fuzz/corpus/tiled/ (seeded with tests/fixtures/map_*.json and
// what fuzzing merged in) and tests/fuzz/regressions/tiled/.
// tests/fuzz/tiled.dict hands libFuzzer the keys cute_tiled knows.
//
//   bash tools/fuzz.sh replay tiled         every committed input, once
//   bash tools/fuzz.sh run tiled 60         a minute of libFuzzer
// ---------------------------------------------------------------------------

#include "harness.h"
#include "map_walk.h"

#include "../../src/rmp/tilemap/tilemap_internal.h"

#include <rmp/tilemap.h>

#include <cstddef>
#include <cstdint>
#include <limits>
#include <span>

// NOLINTNEXTLINE(readability-identifier-naming): libFuzzer's name
extern "C" int LLVMFuzzerInitialize(int * /*argc*/, char *** /*argv*/) {
    rmp::test::fuzz::initialize();
    return 0;
}

// NOLINTNEXTLINE(readability-identifier-naming): libFuzzer's name
extern "C" int LLVMFuzzerTestOneInput(const std::uint8_t *data, std::size_t size) {
    const std::span<const std::uint8_t> input = rmp::test::fuzz::input({ data, size });
    if (input.size() > static_cast<std::size_t>(std::numeric_limits<int>::max()))
        return 0;
    rmp::Tilemap map;
    rmp::tilemap::detail::Access::adopt(
        map,
        rmp::tilemap::detail::parse_map(input.data(), static_cast<int>(input.size()),
                                        "fuzz.json"));
    rmp::test::fuzz::maps::walk(map);
    return 0;
}
```

Its oracle is everything the reader's header promises about bad input: it returns, without undefined behaviour, and refuses what it cannot read. A promise of its own that the sanitizers cannot see — a refusal leaves its output as it was, what reads back writes the same — is a `require()` of the harness, with the promise in words, which ends the run as a crash with that line first, so the nightly issue is named after it. `tests/fuzz/value_json.cpp` is one made of those.

A target comes with its inputs, committed:

- **`tests/fuzz/corpus/<name>/`**, the inputs fuzzing found that add coverage, merged in; seeded at first with real files of the format;
- **`tests/fuzz/regressions/<name>/`**, every input that once failed, or sits at the edge of what the reader takes, each named for what it is — `tests/fuzz/regressions/value_json/` has `1e400` and `coins_1e309`;
- **`tests/fuzz/<name>.dict`**, if it helps: the words of the format, for libFuzzer to put together;
- **`tests/fuzz/<name>.options`**, if it needs one: libFuzzer's flags for it, one a line, such as the longest input worth trying.

Each of the two folders needs at least one input, and nothing may be committed for a target that does not exist. Every clone carries the corpus, so all of it together stays under 2 MB.

```shell
rmp test fuzz
bash tools/fuzz.sh replay value_json
bash tools/fuzz.sh run value_json 60
bash tools/fuzz.sh merge value_json
```

`rmp test fuzz` builds every target and replays every committed input, and it fails unless every file it named ran: a replay that stopped early, saying nothing, would look like one that passed. `run` fuzzes one target for that many seconds and `merge` adds what found new coverage to its corpus.

Fuzzing needs libFuzzer, which is clang's and comes with its compiler-rt. The build image has it; a machine without it builds the same targets around `tests/fuzz/replay_main.cpp`, which replays every input and cannot fuzz, and says so. To fuzz there:

```shell
tools/dev_shell.sh bash tools/fuzz.sh build
tools/dev_shell.sh bash tools/fuzz.sh run value_json 60
```

What a run finds is in `build/fuzz/crashes/<name>/`. It becomes two things: the input, copied into `regressions/<name>/` under a name that says what it is, which every push replays from then on; and a named case in the module's unit tests built from it, which says in a sentence what went wrong. `tests/tilemap_test.cpp` holds what the Tiled reader's target found that way, case by case, beside the inputs in `regressions/tiled/`. Then the fix.

A target is linked to the framework library. One whose code is not in it — the e2e scripts' reader is a part of the tests — names its extra source in `tests/deep.cmake`, the one line a new target may add to that file.

## Mutation testing

Coverage says which lines ran; mutation testing says whether anything checked what they did. Mull, for the C++, and mutmut, for the Python, change one operator, constant or call at a time — a mutant — and run the module's tests against each. A mutant no test notices is a test nobody wrote, or a change nobody could see.

Each module is a floor file, `tests/mutation/floors/<module>.txt`: what it mutates, which tests must notice, the score that has to hold, and the mutants nobody can kill, each with why. The random generator's:

[tests/mutation/floors/random.txt](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/tests/mutation/floors/random.txt#L1-L13)

```text
# rmp::random (package P5). The pilot of 2026-10-11 killed 51 of 62 (82.26%):
# xoshiro's arithmetic had no known-answer test, and two comparisons at their
# edge (range(int) with max == min, chance()) no case. P5 measured it again in
# the image the same day, after the known-answer case, the two seeds whose
# first value() is 0 and 1 - 2^-24, and random_prop.cpp's reference
# generator: 81 of 81 detected, one equivalent -- 100%.
kind     cxx
sources  src/rmp/random/random.cpp
tests    unit_test -ts=random
tests    property_test -ts=*random
floor    100
equivalent cxx_lt_to_le:src/rmp/random/random.cpp:135:13 range(float)'s `min < max` at the clamp: the two differ only at min == max, where both sides answer min -- drawn is min, and nextafter(min, min) is min
```

| Line | What it says |
|---|---|
| `kind` | `cxx` for Mull, `py` for mutmut |
| `sources` | the files mutated |
| `tests` | a test program and its doctest options, one a line, for the C++ — `unit_test`, `ui_layout_test` or `property_test`; the test files, or one class of a file, for the Python. A mutant killed by any of them is killed. |
| `select` | for the Python only: which of mutmut's mutants count, by name |
| `floor` | the score that has to hold, 0 to 100 |
| `equivalent` | a mutant no test can kill, and why, in words |

The score is the mutants a test detected — killed, timed out, crashed — over every mutant but the equivalent ones, in percent. A mutant no test reaches counts against it.

- **The floor only rises.** Below it fails; and a floor below the score, rounded down to 5, fails too, until it is raised in the commit that earned it. `update` writes it. A lower floor is a hand edit a reviewer reads.
- **The equivalent mutants only shrink.** One a test now kills, or one that is gone — Mull's ids carry the line, so moved code makes them new — fails until its line goes.
- **Every file of `src/rmp/` is in exactly one floor file**, but the few that only compile a vendored library. A new file goes into its module's `sources`, or comes with its module's file.
- **A test filter has to select something.** A `-sf`, `-ts` or `-tc` that matches nothing runs no test, passes, and lets every mutant survive, which reads as a module nobody tested. A source filter begins with `*`, because doctest matches the whole path.

Mull's plugin has to match the clang that compiles, exactly, so the C++ modules run inside the build image, which has both. One module is `run`; every module, one after another, is hours, and is `rmp test mutation`:

```shell
bash tools/mutation.sh modules
tools/dev_shell.sh bash tools/mutation.sh run random
tools/dev_shell.sh bash tools/mutation.sh update random
```

A Python module is mutated on a copy of the tree, under `build/mutation/`, and its tests read the mutated copy. mutmut follows calls inside one process: code a test reaches only by starting the tool as a program counts as reached by none, so a Python floor file lists the tests that call the tool directly, and selects the functions they reach.

## The nightly and weekly runs

`.github/workflows/deep.yml` is the search: every night at 03:00 UTC, ten minutes of fuzzing per target and the properties at a random seed; every Sunday, every mutation module, one job each. It runs only in the framework's own repository: a repository made from it is a copy, not a fork, and would open issues on itself. It can also be started by hand, for one target or module:

```shell
gh workflow run deep.yml -f mode=nightly -f only=value_json -f seconds=60
gh workflow run deep.yml -f mode=weekly -f only=random
```

What a job finds becomes an issue labelled `nightly-finding`, one per signature: a sanitizer's report by its kind and the top three of the framework's own frames, a fuzz oracle by its promise, a property by its case and assertion, a mutation module by falling below its floor — nothing that changes from one run of the same failure to the next. Found again, it is a comment on its open issue, never a second one. Found again after it was closed, it is a new issue that says it came back. Each issue carries the command that plays it again, and the run keeps the input.

Those issues are taken first, before other work:

```shell
gh issue list --label nightly-finding
```

One is closed the way any bug is: played again on your machine with the command it gives, written down as a named case — and, for a fuzz input, a file in `regressions/` — seen red, fixed, and seen green. What the night's fuzzing merged into a corpus is in the run's `fuzz-<target>` artifact; committing it is a pull request, never the workflow's.
