cortex
aien-dev/cortex
Brought by drakestapleton
cortex / tests / pageunit / run.sh
| 1 | #!/bin/sh |
| 2 | # tests/pageunit/run.sh : gate for tools/pageunit/enc.om (page text -> om2 unit that prints it back). |
| 3 | # The inputs come from tests/pageunit/gen.om and the committed synthetic fixture.txt; this script only |
| 4 | # invokes programs and compares bytes and exit codes. Work files go to $WORK (default |
| 5 | # build/pageunit-work). Cases: |
| 6 | # - round trips, byte-identical: fixture, empty page, 8000, 8001 and 10000 lines, the 900000-byte |
| 7 | # throughput page, 999999 bytes of quotes, 1000000 bytes in one line, 300000 bytes in one line, |
| 8 | # 50000 and 200000 lines (enc has no size refusal; the units compile with the pinned omc); |
| 9 | # - refusals by exit code (enc streams, so a refused run leaves output that is discarded): control |
| 10 | # bytes 1 and 127 exit 4, stdin a directory exits 2, stdout /dev/full exits 6; |
| 11 | # - old-compiler control: omc f4fa7951 (omega-mv b2ad967e, the pin until 2026-11-11) must refuse the |
| 12 | # 50000-line unit with error 14, so the 50000 and 200000 line cases show the lifted limit; |
| 13 | # - mutant control: one byte changed in a unit must fail the comparison; |
| 14 | # - pin control: a compiler with another sha256 is refused by tools/pageunit/omc.sh; |
| 15 | # - size alarm: what a page still meets is the memory of the compiler that builds its unit. Every real |
| 16 | # page given as an argument or as a *.txt file in $PAGEUNIT_PAGES is encoded, its unit is compiled in |
| 17 | # omc's usage mode, and the page FAILS, with the arena record, when the unit puts the threshold |
| 18 | # ALARMAT of tests/pageunit/gen.om, in 8-byte words, or more in the compiler's arena (a stated |
| 19 | # threshold: 80 percent of the 16777216-word arena that was fixed before omega-mv's run-time pools; |
| 20 | # the arena of later compilers grows). Controls: the fixture is quiet, the 200000-line unit alarms, |
| 21 | # and records one word below the threshold, at it, and with a full arena give 0, 8 and 8 (their two |
| 22 | # numbers are written here on purpose: an independent expectation of where the threshold is). |
| 23 | # Exit 0 only when every case passes. |
| 24 | set -u |
| 25 | R=$(cd "$(dirname "$0")/../.." && pwd) |
| 26 | T=$R/tests/pageunit |
| 27 | W=${WORK:-$R/build/pageunit-work} |
| 28 | OMC=$(sh "$R/tools/pageunit/omc.sh") || { echo "FAIL pageunit: no pinned omc"; exit 2; } |
| 29 | rm -rf "$W"; mkdir -p "$W" |
| 30 | pass=0; fail=0 |
| 31 | ok() { echo "PASS $*"; pass=$((pass + 1)); } |
| 32 | bad() { echo "FAIL $*"; fail=$((fail + 1)); } |
| 33 | echo "omc $OMC sha256 $(sha256sum "$OMC" | cut -d' ' -f1)" |
| 34 | echo "enc.om sha256 $(sha256sum "$R/tools/pageunit/enc.om" | cut -d' ' -f1)" |
| 35 | build() { # build NAME SRC: compile with rc 0 and empty stderr |
| 36 | "$OMC" < "$2" > "$W/$1" 2> "$W/$1.err"; rc=$? |
| 37 | if [ $rc -ne 0 ] || [ -s "$W/$1.err" ]; then echo "FAIL compile $1 rc=$rc $(cat "$W/$1.err")"; exit 1; fi |
| 38 | chmod +x "$W/$1" |
| 39 | } |
| 40 | { echo om2; cat "$T/gen.om"; } > "$W/gen.om" |
| 41 | build gen "$W/gen.om" |
| 42 | build enc "$R/tools/pageunit/enc.om" |
| 43 | cp "$T/fixture.txt" "$W/in.f" |
| 44 | : > "$W/in.e" |
| 45 | for m in k j l m w b g x y c d; do |
| 46 | "$W/gen" $m > "$W/in.$m" || { echo "FAIL gen $m"; exit 1; } |
| 47 | done |
| 48 | trip() { # trip CASE: enc, compile the unit, run it, compare with the input |
| 49 | "$W/enc" < "$W/in.$1" > "$W/u.$1"; e=$? |
| 50 | [ $e -eq 0 ] || { bad "trip $1: enc exit $e"; return; } |
| 51 | "$OMC" < "$W/u.$1" > "$W/b.$1" 2> "$W/b.$1.err"; c=$? |
| 52 | if [ $c -ne 0 ] || [ -s "$W/b.$1.err" ]; then bad "trip $1: unit compile rc=$c $(cat "$W/b.$1.err")"; return; fi |
| 53 | chmod +x "$W/b.$1" |
| 54 | "$W/b.$1" > "$W/o.$1"; r=$? |
| 55 | [ $r -eq 0 ] || { bad "trip $1: unit exit $r"; return; } |
| 56 | cmp -s "$W/in.$1" "$W/o.$1" || { bad "trip $1: output differs from input"; return; } |
| 57 | ok "trip $1: input $(wc -c < "$W/in.$1") bytes $(wc -l < "$W/in.$1") newlines, unit $(wc -c < "$W/u.$1") bytes, output identical" |
| 58 | rm -f "$W/b.$1" "$W/o.$1" |
| 59 | } |
| 60 | for m in f e k j l m w b g x y; do trip $m; done |
| 61 | refuse() { # refuse NAME WANT: enc exited $e, which must be WANT |
| 62 | if [ "$e" -eq "$2" ]; then ok "refuse $1: exit $e"; else bad "refuse $1: exit $e (want $2)"; fi |
| 63 | } |
| 64 | "$W/enc" < "$W/in.c" > /dev/null; e=$?; refuse "byte 1" 4 |
| 65 | "$W/enc" < "$W/in.d" > /dev/null; e=$?; refuse "byte 127" 4 |
| 66 | "$W/enc" < "$T" > /dev/null; e=$?; refuse "stdin a directory" 2 |
| 67 | "$W/enc" < "$W/in.f" > /dev/full; e=$?; refuse "stdout /dev/full" 6 |
| 68 | # old-compiler control: the pin before 2026-11-11 refuses a unit past 8192 texts |
| 69 | OLD_REV=b2ad967e57146172c1bedf1eac98babb75baef65 |
| 70 | OLD_SHA=f4fa7951816bbd09c5eaaae6692852a70c3c305164b365027c5b683dec7d63e9 |
| 71 | REPO=${OMEGA_REPO:-${RAD_HOME:-$HOME/.radicle}/storage/z6do9WDBhuSnvopDkgFdFr1yWA62} |
| 72 | git -C "$REPO" show "$OLD_REV:seed/out/omc" > "$W/omc-old" 2> /dev/null; chmod +x "$W/omc-old" |
| 73 | if [ "$(sha256sum "$W/omc-old" | cut -d' ' -f1)" != "$OLD_SHA" ]; then bad "old compiler: cannot read $OLD_REV:seed/out/omc from $REPO" |
| 74 | else |
| 75 | "$W/omc-old" < "$W/u.x" > /dev/null 2> "$W/old.err"; c=$? |
| 76 | if [ $c -eq 14 ]; then ok "old compiler refuses the 50000-line unit: $(head -n 1 "$W/old.err")"; else bad "old compiler: rc=$c on the 50000-line unit (want 14)"; fi |
| 77 | fi |
| 78 | # mutant control: one letter in the first text record of the fixture unit changed |
| 79 | "$W/gen" u < "$W/u.f" > "$W/mut.om"; g=$? |
| 80 | if [ $g -eq 0 ] && ! cmp -s "$W/u.f" "$W/mut.om"; then |
| 81 | "$OMC" < "$W/mut.om" > "$W/mut" 2> "$W/mut.err"; c=$? |
| 82 | chmod +x "$W/mut"; "$W/mut" > "$W/mut.out"; r=$? |
| 83 | if [ $c -eq 0 ] && [ $r -eq 0 ] && ! cmp -s "$W/in.f" "$W/mut.out"; then ok "mutant: one changed unit byte fails the comparison" |
| 84 | else bad "mutant: compile rc=$c run rc=$r, output not caught"; fi |
| 85 | else bad "mutant: gen u exit $g or unit unchanged"; fi |
| 86 | # pin control: a compiler whose sha256 differs (the pinned binary plus one byte) is refused |
| 87 | cp "$OMC" "$W/omc-wrong"; printf x >> "$W/omc-wrong" |
| 88 | p=$(OMC="$W/omc-wrong" sh "$R/tools/pageunit/omc.sh" 2> "$W/wrong.err"); rc=$? |
| 89 | if [ $rc -eq 1 ] && [ -z "$p" ]; then ok "wrong compiler refused: $(cat "$W/wrong.err")"; else bad "wrong compiler: rc=$rc path '$p'"; fi |
| 90 | # size alarm (gen a reads the omc-use record of a unit compiled in usage mode) |
| 91 | usage() { # usage UNIT REC: the arena record of UNIT (stdout on success, stderr when the arena is full) |
| 92 | { echo usage; cat "$1"; } | "$OMC" > "$2" 2>&1 |
| 93 | } |
| 94 | ctl() { # ctl NAME REC WANT: the alarm on record REC must give exit WANT |
| 95 | a=$("$W/gen" a < "$2"); e=$? |
| 96 | if [ $e -eq "$3" ]; then ok "alarm control $1: $a, exit $e"; else bad "alarm control $1: $a, exit $e (want $3)"; fi |
| 97 | } |
| 98 | usage "$W/u.f" "$W/rec.f"; ctl fixture "$W/rec.f" 0 |
| 99 | usage "$W/u.y" "$W/rec.y"; ctl 200000-lines "$W/rec.y" 8 |
| 100 | printf 'omc-use 1 arena=16777216 top=13421772 waste=0 full=0\n' > "$W/rec.79"; ctl below-threshold "$W/rec.79" 0 |
| 101 | printf 'omc-use 1 arena=33554432 top=13421773 waste=0 full=0\n' > "$W/rec.80"; ctl at-threshold-grown-arena "$W/rec.80" 8 |
| 102 | printf 'omc: line 9: error 14\nomc-use 1 arena=16777216 top=5 waste=0 full=1\n' > "$W/rec.full"; ctl arena-full "$W/rec.full" 8 |
| 103 | np=0 |
| 104 | for p in "$@" ${PAGEUNIT_PAGES:+"$PAGEUNIT_PAGES"/*.txt}; do |
| 105 | [ -f "$p" ] || { bad "page $p: not a file"; continue; } |
| 106 | np=$((np + 1)) |
| 107 | "$W/enc" < "$p" > "$W/page.om"; e=$? |
| 108 | [ $e -eq 0 ] || { bad "page $p: enc exit $e"; continue; } |
| 109 | usage "$W/page.om" "$W/page.rec" |
| 110 | a=$("$W/gen" a < "$W/page.rec"); e=$? |
| 111 | case $e in |
| 112 | 0) ok "page $p: $(wc -c < "$p") bytes, $a" ;; |
| 113 | 8) bad "ALARM page $p: $(wc -c < "$p") bytes, $a: its unit reaches the stated compiler-arena threshold" ;; |
| 114 | *) bad "page $p: no arena record (gen a exit $e): $(head -c 200 "$W/page.rec")" ;; |
| 115 | esac |
| 116 | done |
| 117 | echo "pageunit: pass=$pass fail=$fail pages=$np" |
| 118 | [ $fail -eq 0 ] && [ $pass -eq $((23 + np)) ] |