omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / tests / test_mutation_runner.sh
| 1 | #!/bin/sh |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | # Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | # Self-test of tools/mutation_runner.sh. Host only, CPU only, light. Builds a |
| 18 | # tiny C project in a temp dir, runs the runner over fake mutants and checks |
| 19 | # every verdict, the exit code and the JSON bytes. Each check can fail: the |
| 20 | # always-passing test run and the corrupted expected file prove the self-test |
| 21 | # notices a runner that reports the wrong thing. |
| 22 | set -u |
| 23 | root=$(cd "$(dirname "$0")/.." && pwd) |
| 24 | runner=$root/tools/mutation_runner.sh |
| 25 | expected=$root/tests/fixtures/mutation_runner/expected.json |
| 26 | tmp=$(mktemp -d "${TMPDIR:-/tmp}/mutrun-selftest.XXXXXX") || exit 2 |
| 27 | trap 'rm -rf "$tmp"' EXIT INT TERM |
| 28 | proj=$tmp/proj |
| 29 | mkdir -p "$proj" |
| 30 | cat > "$proj/add.c" <<'CEOF' |
| 31 | int add(int a, int b) { return a + b; } /* MUT:ADD_PLUS */ |
| 32 | int clamp(int x) { return x>100?100:x; } |
| 33 | int twice(int x) { return x; } |
| 34 | CEOF |
| 35 | cat > "$proj/check.c" <<'CEOF' |
| 36 | #include <stdio.h> |
| 37 | int add(int a, int b); |
| 38 | int main(void) { |
| 39 | if (add(2, 3) != 5) { puts("FAIL add"); return 1; } |
| 40 | puts("PASS"); |
| 41 | return 0; |
| 42 | } |
| 43 | CEOF |
| 44 | BUILD='cc -o check check.c add.c' |
| 45 | TEST=./check |
| 46 | fails=0; checks=0 |
| 47 | ok() { checks=$((checks + 1)); if [ "$2" = 0 ]; then echo "ok $1"; else echo "FAIL $1"; fails=$((fails + 1)); fi; } |
| 48 | has() { grep -q -- "$2" "$1"; } # file pattern |
| 49 | |
| 50 | # run <rowsfile-content> <extra args...>: sets rc, $tmp/err, $tmp/outp, $tmp/j.json |
| 51 | run() { |
| 52 | rows=$1; shift |
| 53 | printf '%s\n' "$rows" > "$tmp/rows" |
| 54 | rm -f "$tmp/j.json" |
| 55 | "$runner" -m "$tmp/rows" -d "$proj" -o "$tmp/j.json" "$@" > "$tmp/outp" 2> "$tmp/err" |
| 56 | rc=$? |
| 57 | } |
| 58 | before=$(find "$proj" -type f | sort | xargs sha256sum | sha256sum) |
| 59 | |
| 60 | # (1) a mutant that breaks the code is KILLED, exit 0 |
| 61 | run 'K~add.c~s/a + b/a - b/~addition' -k table -t "$TEST" -b "$BUILD" |
| 62 | [ $rc -eq 0 ] && has "$tmp/err" '^K KILLED'; ok "1 breaking mutant is KILLED, exit 0" $? |
| 63 | |
| 64 | # (2) a mutant the test cannot see SURVIVES, exit nonzero |
| 65 | run 'S~add.c~s/x>100/x>101/~clamp' -k table -t "$TEST" -b "$BUILD" |
| 66 | [ $rc -ne 0 ] && has "$tmp/err" '^S SURVIVED ' && ! has "$tmp/err" 'SURVIVED_'; ok "2 unseen mutant is SURVIVED, exit nonzero" $? |
| 67 | |
| 68 | # (3) same mutant marked redundant (new column form and legacy prose form) |
| 69 | run 'S~add.c~s/x>100/x>101/~clamp~redundant~covered elsewhere' -k table -t "$TEST" -b "$BUILD" |
| 70 | [ $rc -eq 0 ] && has "$tmp/err" '^S SURVIVED_REDUNDANT' && has "$tmp/j.json" '"note": "redundant: covered elsewhere"'; ok "3a redundant (class column) is SURVIVED_REDUNDANT, exit 0" $? |
| 71 | run 'S~add.c~s/x>100/x>101/~clamp~redundant: covered elsewhere' -k table -t "$TEST" -b "$BUILD" |
| 72 | [ $rc -eq 0 ] && has "$tmp/err" '^S SURVIVED_REDUNDANT' && has "$tmp/j.json" '"note": "redundant: covered elsewhere"'; ok "3b redundant (legacy prose) is SURVIVED_REDUNDANT, exit 0" $? |
| 73 | run 'S~add.c~s/x>100/x>101/~clamp~gap~no test reaches it' -k table -t "$TEST" -b "$BUILD" |
| 74 | [ $rc -eq 0 ] && has "$tmp/err" '^S SURVIVED_GAP'; ok "3c gap is SURVIVED_GAP, exit 0" $? |
| 75 | run 'S~add.c~s/x>100/x>101/~clamp~none~' -k table -t "$TEST" -b "$BUILD" |
| 76 | [ $rc -ne 0 ] && has "$tmp/err" '^S SURVIVED '; ok "3d class none is a plain SURVIVED, exit nonzero" $? |
| 77 | |
| 78 | # (4) a no-op edit is ERROR |
| 79 | run 'N~add.c~s/NO_SUCH_TEXT/y/~noop' -k table -t "$TEST" -b "$BUILD" |
| 80 | [ $rc -ne 0 ] && has "$tmp/err" '^N ERROR' && has "$tmp/j.json" '"note": "edit did not apply"'; ok "4a unmatched sed is ERROR edit did not apply" $? |
| 81 | run 'N~add.c~R:return a + b;@>@return a + b;~noop replace' -k table -t "$TEST" -b "$BUILD" |
| 82 | [ $rc -ne 0 ] && has "$tmp/err" '^N ERROR' && has "$tmp/j.json" '"note": "edit did not apply"'; ok "4b replace with identical text is ERROR" $? |
| 83 | run 'N~missing.c~s/a/b/~no file' -k table -t "$TEST" -b "$BUILD" |
| 84 | [ $rc -ne 0 ] && has "$tmp/err" '^N ERROR'; ok "4c missing file is ERROR" $? |
| 85 | run 'B~add.c~s/int add/int add_/~breaks the build' -k table -t "$TEST" -b "$BUILD" |
| 86 | [ $rc -ne 0 ] && has "$tmp/err" '^B ERROR' && has "$tmp/j.json" 'build failed'; ok "4d build failure is ERROR" $? |
| 87 | # -e TEXT: the note of a build that failed with an EMPTY log. Without -e it stays "build failed: ". |
| 88 | run 'E~add.c~s/a + b/a - b/~empty build log' -k table -t "$TEST" -b 'exit 1' -e 'empty make log (make killed or never started)' |
| 89 | [ $rc -ne 0 ] && has "$tmp/err" '^E ERROR' && has "$tmp/j.json" '"note": "build failed: empty make log (make killed or never started)"'; ok "4e empty build log with -e: note is build failed: TEXT" $? |
| 90 | run 'E~add.c~s/a + b/a - b/~empty build log' -k table -t "$TEST" -b 'exit 1' |
| 91 | [ $rc -ne 0 ] && has "$tmp/err" '^E ERROR' && has "$tmp/j.json" '"note": "build failed: "'; ok "4f empty build log without -e: note stays build failed: (unchanged)" $? |
| 92 | run 'E~add.c~s/a + b/a - b/~error in log' -k table -t "$TEST" -b 'echo "x error: boom"; exit 1' -e 'empty make log (make killed or never started)' |
| 93 | [ $rc -ne 0 ] && has "$tmp/err" '^E ERROR' && has "$tmp/j.json" '"note": "build failed: x error: boom"' && ! has "$tmp/j.json" 'empty make log'; ok "4g non-empty build log with -e keeps the first error line" $? |
| 94 | |
| 95 | # (5) replace edit: exactly once works, twice is ERROR, @NL@ is a newline |
| 96 | run 'R1~add.c~R:return a + b;@>@return a - b;~replace once' -k table -t "$TEST" -b "$BUILD" |
| 97 | [ $rc -eq 0 ] && has "$tmp/err" '^R1 KILLED'; ok "5a replace matching once is applied (KILLED)" $? |
| 98 | run 'R2~add.c~R:return@>@return 0 +~replace twice' -k table -t "$TEST" -b "$BUILD" |
| 99 | [ $rc -ne 0 ] && has "$tmp/err" '^R2 ERROR' && has "$tmp/j.json" 'not found exactly once'; ok "5b replace matching twice is ERROR" $? |
| 100 | run 'R3~add.c~R:return a + b;@>@return a - b;@NL@int zzz(void) { return 1; }~newline escape' -k table -t "$TEST" -b "$BUILD" |
| 101 | [ $rc -eq 0 ] && has "$tmp/err" '^R3 KILLED'; ok "5c replace with @NL@ builds and is KILLED" $? |
| 102 | |
| 103 | # (6) marker reader |
| 104 | run 'ADD_PLUS|add.c|s/a + b/a * b/' -k marker -t "$TEST" -b "$BUILD" |
| 105 | [ $rc -eq 0 ] && has "$tmp/err" '^ADD_PLUS KILLED'; ok "6a marker row is KILLED" $? |
| 106 | run 'NO_MARKER|add.c|s/a + b/a * b/' -k marker -t "$TEST" -b "$BUILD" |
| 107 | [ $rc -ne 0 ] && has "$tmp/err" '^NO_MARKER ERROR'; ok "6b marker not on any line is ERROR (edit does not apply)" $? |
| 108 | run 'ADD_PLUS|add.c|s/a + b/a * b/' -k marker -t "$TEST" -b "$BUILD" -B |
| 109 | [ $rc -eq 0 ]; ok "6c marker baseline passing is accepted" $? |
| 110 | run 'ADD_PLUS|add.c|s/a + b/a * b/' -k marker -t 'exit 1' -b "$BUILD" -B |
| 111 | [ $rc -eq 1 ] && has "$tmp/outp" 'baseline does not pass'; ok "6d failing baseline FAILS the sweep" $? |
| 112 | |
| 113 | # (7) sed-table row with two files and @@ separated expressions |
| 114 | cp "$proj/add.c" "$proj/other.c" |
| 115 | run 'T~add.c,other.c~s/a + b/a - b/@@s/return x;/return x + 1;/~two files' -k table -t "$TEST" -b "$BUILD" -f FAIL |
| 116 | [ $rc -eq 0 ] && has "$tmp/err" '^T KILLED' && has "$tmp/j.json" '"file": "add.c,other.c"' && has "$tmp/j.json" 'first: add'; ok "7 two-file sed row is applied per file and KILLED" $? |
| 117 | rm -f "$proj/other.c" |
| 118 | |
| 119 | # (8) JSON byte-compare against the committed expected receipt |
| 120 | cat > "$tmp/rows8" <<'REOF' |
| 121 | K1~add.c~s/a + b/a - b/~addition is computed |
| 122 | S1~add.c~s/x>100/x>101/~clamp upper bound |
| 123 | S2~add.c~s/x>100/x>101/~clamp upper bound again~redundant~covered by the caller, UNVERIFIED |
| 124 | L1~add.c~s/x>100/x>101/~legacy gap row~gap: legacy prose with "quotes" and \back |
| 125 | E1~add.c~s/NO_SUCH_TEXT/y/~no-op edit |
| 126 | REOF |
| 127 | "$runner" -k table -m "$tmp/rows8" -d "$proj" -o "$tmp/j8.json" -t "$TEST" -b "$BUILD" -f FAIL >/dev/null 2>"$tmp/err8" |
| 128 | rc8=$? |
| 129 | [ $rc8 -ne 0 ]; ok "8a sweep with a plain SURVIVED and an ERROR exits nonzero" $? |
| 130 | cmp -s "$tmp/j8.json" "$expected"; ok "8b JSON is byte-identical to tests/fixtures/mutation_runner/expected.json" $? |
| 131 | sed 's/KILLED/SURVIVED/' "$expected" > "$tmp/bad.json" |
| 132 | ! cmp -s "$tmp/j8.json" "$tmp/bad.json"; ok "8c the byte compare notices a wrong receipt" $? |
| 133 | ONLY='K1 S2' "$runner" -k table -m "$tmp/rows8" -d "$proj" -o "$tmp/j9.json" -t "$TEST" -b "$BUILD" >/dev/null 2>&1 |
| 134 | [ "$(grep -c '"id"' "$tmp/j9.json")" -eq 2 ]; ok "8d ONLY restricts the sweep" $? |
| 135 | |
| 136 | # (9) deliberately wrong run: a test that always passes must make the breaking |
| 137 | # mutant SURVIVE, never KILLED, and the sweep must fail. |
| 138 | run 'K~add.c~s/a + b/a - b/~addition' -k table -t true -b "$BUILD" |
| 139 | [ $rc -ne 0 ] && has "$tmp/err" '^K SURVIVED' && ! has "$tmp/err" '^K KILLED'; ok "9a always-passing test reports SURVIVED, not KILLED" $? |
| 140 | run 'K~add.c~s/a + b/a - b/~addition' -k table -t false -b "$BUILD" |
| 141 | [ $rc -eq 0 ] && has "$tmp/err" '^K KILLED'; ok "9b always-failing test reports KILLED (the runner trusts the exit code)" $? |
| 142 | run '' -k table -t "$TEST" -b "$BUILD" |
| 143 | [ $rc -ne 0 ]; ok "9c an empty sweep fails instead of passing vacuously" $? |
| 144 | |
| 145 | # (10) the source tree is never edited |
| 146 | after=$(find "$proj" -type f | sort | xargs sha256sum | sha256sum) |
| 147 | [ "$before" = "$after" ]; ok "10 source tree unchanged (no build artifacts, no edits)" $? |
| 148 | |
| 149 | "$runner" >/dev/null 2>&1; [ $? -eq 2 ]; ok "11 missing arguments exit 2" $? |
| 150 | |
| 151 | if [ $fails -ne 0 ]; then echo "mutation runner self-test: FAIL ($fails of $checks)"; exit 1; fi |
| 152 | echo "mutation runner self-test: PASS ($checks checks)" |