From 8d3b77add40605f66ac4c864bb7e5554c3eeabf2 Mon Sep 17 00:00:00 2001 From: Davide Cappellini Date: Sun, 8 Nov 2026 19:19:58 +0100 Subject: [PATCH] mutation: parallel jobs in temp copies, per-mutation test files, FFF_MUTATION reduced iterations, trap cleanup, sleep inhibit Co-Authored-By: Claude Sonnet 5.5 --- tests/testutil.nim | 6 +++- tests/tgeometry.nim | 19 +++++++---- tools/mutation.sh | 79 +++++++++++++++++++++++++++++---------------- 3 files changed, 69 insertions(+), 35 deletions(-) diff --git a/tests/testutil.nim b/tests/testutil.nim index 4e197e0..4df9b5f 100644 --- a/tests/testutil.nim +++ b/tests/testutil.nim @@ -1,7 +1,11 @@ ## shared test helpers (not a test file) -import std/math +import std/[math, os] import geometry +proc scaled*(n: int): int = + ## FFF_MUTATION=1 (set only by tools/mutation.sh) divides random/oracle iteration counts by 10; normal runs use n. + if getEnv("FFF_MUTATION") != "": max(1, n div 10) else: n + proc W*(ax, ay, bx, by: float, door = false, open = false): Wall = Wall(a: (ax, ay), b: (bx, by), door: door, open: open) diff --git a/tests/tgeometry.nim b/tests/tgeometry.nim index f6dbf61..ca17a26 100644 --- a/tests/tgeometry.nim +++ b/tests/tgeometry.nim @@ -1,3 +1,4 @@ +# FFF_MUTATION=1 (set only by tools/mutation.sh) cuts iteration counts ~10x via scaled(); plain `devbox run test` runs full counts. import std/[unittest, json, os, math] import geometry, refold, testutil @@ -233,7 +234,10 @@ suite "corpus (prototype leak transcripts)": var sc = loadScene() let base = sc.walls var n, doorsSeen = 0 + var li = 0 for line in lines(currentSourcePath().parentDir / "fixtures" / "corpus.jsonl"): + inc li + if li mod (300 div scaled(300)) != 0: continue # quick mode: every 10th record let j = parseJson(line) var walls = base for w in walls.mitems: @@ -247,7 +251,7 @@ suite "corpus (prototype leak transcripts)": check cellsTouched(poly, sc.cell) == cellsTouchedOld(poly, sc.cell) check cellsTouched(rec, sc.cell) == cellsTouchedOld(rec, sc.cell) inc n - check n >= 300 # corpus is not silently empty + check n >= scaled(300) # corpus is not silently empty check doorsSeen > 0 # and exercises opened doors # ---- differential: scanline vs prototype per-cell --------------------------------------------------------------- @@ -255,7 +259,7 @@ suite "cellsTouched differential vs prototype": test "random visibility polygons: identical cell lists": var r = Rng(s: 0x9E3779B97F4A7C15'u64) var total = 0 - for iter in 0 ..< 300: + for iter in 0 ..< scaled(300): let walls = randomWalls(r, 5 + iter mod 40, 1200) let o: Vec = (r.uni(100, 1100), r.uni(100, 1100)) let cell = [32.0, 64.0, 25.0][iter mod 3] @@ -263,10 +267,10 @@ suite "cellsTouched differential vs prototype": let (a, b) = (cellsTouched(poly, cell), cellsTouchedOld(poly, cell)) check a == b total += a.len - check total > 10000 + check total > scaled(10000) test "random simple polygons (star-shaped, concave): identical cell lists": var r = Rng(s: 12345'u64) - for iter in 0 ..< 300: + for iter in 0 ..< scaled(300): let c: Vec = (r.uni(100, 500), r.uni(100, 500)) let n = 3 + iter mod 20 var poly: seq[Vec] @@ -277,7 +281,7 @@ suite "cellsTouched differential vs prototype": check cellsTouched(poly, 32) == cellsTouchedOld(poly, 32) test "grid-aligned integer polygons (edges on cell lines): identical cell lists": var r = Rng(s: 777'u64) - for iter in 0 ..< 300: + for iter in 0 ..< scaled(300): let x0 = float(int(r.uni(0, 10))) * 16 let y0 = float(int(r.uni(0, 10))) * 16 let poly = [(x0, y0), (x0 + float(1 + int(r.uni(0, 8))) * 16, y0), @@ -359,12 +363,13 @@ suite "explored oracle (independent, tolerance 0.5 px)": if w.door: w.open = true var sure = 0 for walls in [sc.walls, opened]: - for p in sc.tokens: + for ti, p in sc.tokens: + if ti mod (300 div scaled(300)) != 0: continue # quick mode: every 10th token let r = oracleOf((p.x + 7.3, p.y - 3.1), walls, sc.radius, sc.cell) check r.miss == 0 check r.extra == 0 sure += r.sure - check sure > 100000 # the oracle really sampled + check sure > scaled(100000) # the oracle really sampled test "random wall fields, cell 32 and 25": var r = Rng(s: 4242'u64) for iter in 0 ..< 6: diff --git a/tools/mutation.sh b/tools/mutation.sh index fbb450f..1eaef8d 100755 --- a/tools/mutation.sh +++ b/tools/mutation.sh @@ -8,11 +8,50 @@ # h: capsule reduced to centre path + destination circle (prototype H10) | i: no step cap | j: blocked trigger needs centre-on-wall # k: wall removal / door open clears blocked unconditionally (no KEEP) | l: GM moves never set/clear blocked # m: door push goes to the wrong side first | n: door push skips the validity check | o: push ignores the previous-position rule +# Parallel: MUTATION_JOBS (default nproc/4) jobs, each in its own temp copy + nimcache; output collected, printed in fixed order. +# Tests run with FFF_MUTATION=1 (iteration counts ~/10, see tests/testutil.nim scaled()); only test files holding an expected +# test are run per mutation (control runs all). Debug build. Wrapped in systemd-inhibit (sleep:idle) when that works. +# Ctrl-C / TERM / EXIT: kills all jobs and removes the temp dir (fffmut.* under $TMPDIR or /tmp). # Log: results/mutation.log cd "$(dirname "$0")/.." || exit 1 +job() { # runs in its own session: $1 id $2 title $3 file to mutate $4 sed script ("" = control) $5 expected-failure regex + local id=$1 title=$2 file=$3 sedscr=$4 expect=$5 w=$T/$1/w res=$T/$1/res tfs tf out=$T/$1/out r=0 rr F verdict=ok rc=0 + mkdir -p "$w"; cp -r server tests tools nim.cfg "$w"/ + find "$w/tests" -maxdepth 1 -type f ! -name '*.nim' -delete + echo "## $id: $title" > "$res" + if [ -n "$sedscr" ]; then + sed -i "$sedscr" "$w/$file" + if cmp -s "$file" "$w/$file"; then + echo "MUTATION $id DID NOT APPLY (sed matched nothing)" >> "$res"; echo "## $id: sed no-op" >> "$res" + echo "$id: sed no-op" > "$T/$id/verdict"; echo 1 > "$T/$id/rc"; return + fi + { echo "# diff $file -> mutated copy:"; diff "$file" "$w/$file" | sed 's/^/# /'; } >> "$res" + tfs=$(grep -lE "(test|suite) \".*($expect)" tests/t*.nim); [ -n "$tfs" ] || tfs=$(ls tests/t*.nim) + else tfs=$(ls tests/t*.nim); fi + : > "$out" + for tf in $tfs; do + (cd "$w" && nim c -r --hints:off --nimcache:"$T/$id/nc-$(basename "$tf" .nim)" "$tf") >> "$out" 2>&1; rr=$? + [ $rr -eq 0 ] || r=$rr + done + grep -E '\[(OK|FAILED)\]|Success|Failure|Error' "$out" | sed 's/^ *\.* */ /' >> "$res" + F=$(grep '\[FAILED\]' "$out" | grep -E "$expect") + { echo "# test exit=$r; expected tests failed: $(printf '%s' "$F" | grep -c .)"; [ -n "$F" ] && echo "$F"; echo; } >> "$res" + if [ -z "$sedscr" ]; then [ $r -eq 0 ] || { verdict="CONTROL FAILED"; rc=1; } + else [ -n "$F" ] || { verdict="SURVIVED"; rc=1; }; fi + echo "$id: exit=$r expected_failed=$(printf '%s' "$F" | grep -c .) $verdict" > "$T/$id/verdict"; echo $rc > "$T/$id/rc" +} +if [ "$1" = __job ]; then shift; job "$@"; exit 0; fi +if [ -z "$MUT_INHIBITED" ] && systemd-inhibit --what=sleep:idle --who=mutation --why=test true 2>/dev/null; then + MUT_INHIBITED=1 exec systemd-inhibit --what=sleep:idle --who=mutation --why="mutation run" "$0" "$@" +fi mkdir -p results LOG=$PWD/results/mutation.log -T=$(mktemp -d); trap 'rm -rf "$T"' EXIT +JOBS=${MUTATION_JOBS:-$(( $(nproc) / 4 ))}; [ "$JOBS" -ge 1 ] || JOBS=1 +export FFF_MUTATION=1 +T=$(mktemp -d -t fffmut.XXXXXX) +PIDS=() +cleanup() { trap - EXIT INT TERM; local p; for p in "${PIDS[@]}"; do kill -TERM -- "-$p" 2>/dev/null; done; wait 2>/dev/null; rm -rf "$T"; } +trap cleanup EXIT; trap 'exit 130' INT; trap 'exit 143' TERM NEW="shared endpoint: no leak at the vertex|'<' apex|every segment endpoint|rays nudged past a corner" D="shared vertex: both arms required" E="random visibility polygons|random simple polygons|corpus|prototype scene, all tokens|random wall fields|concave polygon|square polygon" @@ -26,32 +65,11 @@ L="GM moves a stuck token|GM drops a token|GM manual clear inside|GM: ignores wa M="Q14: a door closed|push: centre on the door line|push: one side blocked|doorway: token in an open doorway|GM drops a token" N="push: one side blocked|push: both sides invalid" O="push: centre on the door line" -rc=0 -: > "$LOG" -run() { # $1 id $2 title $3 file to mutate $4 sed script ("" = control) $5 expected-failure regex - local id=$1 title=$2 file=$3 sedscr=$4 expect=$5 w=$T/w - rm -rf "$w"; mkdir -p "$w"; cp -r server tests tools nim.cfg "$w"/ - find "$w/tests" -maxdepth 1 -type f ! -name '*.nim' -delete - { echo "## $id: $title" - if [ -n "$sedscr" ]; then - sed -i "$sedscr" "$w/$file" - if cmp -s "$file" "$w/$file"; then echo "MUTATION $id DID NOT APPLY (sed matched nothing)"; echo "## $id: sed no-op" ; rc=1; return; fi - echo "# diff $file -> mutated copy:"; diff "$file" "$w/$file" | sed 's/^/# /' - fi - } >> "$LOG" - local out="$T/out-$id.txt" r=0 rr tf F - : > "$out" - for tf in tests/t*.nim; do - (cd "$w" && nim c -r --hints:off --nimcache:"$T/nc-$(basename "$tf" .nim)" "$tf") >> "$out" 2>&1; rr=$? - [ $rr -eq 0 ] || r=$rr - done - grep -E '\[(OK|FAILED)\]|Success|Failure|Error' "$out" | sed 's/^ *\.* */ /' >> "$LOG" - F=$(grep '\[FAILED\]' "$out" | grep -E "$expect") - { echo "# test exit=$r; expected tests failed: $(printf '%s' "$F" | grep -c .)"; [ -n "$F" ] && echo "$F"; echo; } >> "$LOG" - local verdict=ok - if [ -z "$sedscr" ]; then [ $r -eq 0 ] || { verdict="CONTROL FAILED"; rc=1; } - else [ -n "$F" ] || { verdict="SURVIVED"; rc=1; }; fi - echo "$id: exit=$r expected_failed=$(printf '%s' "$F" | grep -c .) $verdict" | tee -a "$LOG" +IDS=() +run() { + while [ "$(jobs -rp | wc -l)" -ge "$JOBS" ]; do wait -n; done + mkdir -p "$T/$1"; IDS+=("$1") + setsid env T="$T" FFF_MUTATION=1 "$0" __job "$@" & PIDS+=($!) } run control "unmutated copy (must pass)" server/geometry.nim "" "" run a "remove the +-RayEps rays" server/geometry.nim '/angles.add ang [-+] RayEps/d' "$NEW" @@ -70,5 +88,12 @@ run l "GM moves never set or clear blocked" server/moverules.nim '/^ if who.rol run m "door push: wrong side first" server/moverules.nim 's/for sg in \[(if side > 0: 1.0 else: -1.0), (if side > 0: -1.0 else: 1.0)\]:/for sg in [(if side > 0: -1.0 else: 1.0), (if side > 0: 1.0 else: -1.0)]:/' "$M" run n "door push: no validity check" server/moverules.nim 's/if inBounds(sc, p) and not overlapsAny(sc, tt): return (true, p)/return (true, p)/' "$N" run o "door push: previous position ignored on the line" server/moverules.nim 's/side = if t.hasPrev: dot(t.prev - w.a, n) else: 0.0/side = 0.0/' "$O" +wait +rc=0; : > "$LOG" +for id in control a b c d1 d2 e f g h i j k l m n o; do + [ -f "$T/$id/res" ] && cat "$T/$id/res" >> "$LOG" + if [ -f "$T/$id/verdict" ]; then tee -a "$LOG" < "$T/$id/verdict"; [ "$(cat "$T/$id/rc")" = 0 ] || rc=1 + else echo "$id: NO RESULT (job killed?)" | tee -a "$LOG"; rc=1; fi +done echo "exit=$rc" | tee -a "$LOG" exit $rc