interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / seed / json / tests / gate-json.sh

163 lines · 8302 bytes Raw Blame History
1#!/bin/sh
2# seed/json gate (omega-mv #788). Host-only. CPython is a read-only oracle (tests/gen.py), never a
3# generator of compiled code. Prints JSONGATE PASS/FAIL/NOT_RUN; exit 0 PASS, 1 FAIL, 3 NOT_RUN.
4set -eu
5cd "$(dirname "$0")/../../.."
6B=${JSON_BUILD:-build/seed/json}
7OMC=${OMC:-seed/out/omc}
8# Compiler pin: the hash seed/omega-from-seed.sh wrote to seed/out/omc.sha256 (JSON_OMC_PIN overrides it).
9PIN=${JSON_OMC_PIN:-$(cut -d ' ' -f 1 seed/out/omc.sha256)}
10J=seed/json
11fail() { printf 'JSONGATE FAIL %s\n' "$*"; exit 1; }
12notrun() { printf 'JSONGATE NOT_RUN %s\n' "$*"; exit 3; }
13[ "$(uname -sm)" = 'Linux aarch64' ] || notrun 'requires Linux aarch64'
14[ -x "$OMC" ] || notrun "no compiler at $OMC"
15case $PIN in *[!0-9a-f]*) PIN= ;; esac
16[ ${#PIN} -eq 64 ] || notrun 'gate-json: no compiler pin in seed/out/omc.sha256'
17h=$(sha256sum < "$OMC" | cut -c1-64)
18[ "$h" = "$PIN" ] || notrun "gate-json: compiler $OMC sha256 $h differs from pin $PIN"
19command -v python3 >/dev/null || notrun 'requires python3 (read-only oracle)'
20python3 -c 'import sys; sys.exit(0 if sys.version_info >= (3, 11) else 1)' || notrun 'requires python3 >= 3.11'
21if command -v ionice >/dev/null; then LOW='ionice -c3 nice -n 19'; else LOW='nice -n 19'; fi
22rm -rf "$B"
23mkdir -p "$B/out"
24printf 'SOURCE %s\n' "$(git rev-parse HEAD 2>/dev/null || echo unknown)"
25printf 'COMPILER %s\n' "$PIN"
26python3 --version
27for f in "$J/json.om" "$J/jsonw.om" "$J/jsonf.om" "$J/tests/jsdrv.om"; do
28 printf 'SRC %s %s\n' "$(sha256sum "$f" | cut -d ' ' -f 1)" "$f"
29done
30
31# 1. The library units declare no globals, texts or om2 line, use no host or I/O word, and compile
32# alone (with an empty main: omc before 2c824805 refused any `const` in function mode with error 9;
33# #806 lifted that, and the empty main keeps this check identical across pins).
34HOST='read|write|pread|pwrite|exit|fclose|fsync|fchmod|fseek|socknew|listen|accept|fork|dupfd|monons|fopen|unlink|makedir|getdnts|connect|bind|waitpid|fstat|recv|send|sleepns|argcnt|argread|waitio|execprog|renames|rmdir|killpid|pipefds|getpid|realns|sockname|cwd|readlnk|sockopt|random|envcnt|envread|chdir|ftrunc|stat|link|symlnk|chmod|faccess|lstat|truncate|chown|fchdir|umask|fdsync|memfd|shutdown|devctl|mapnew|mapread|mapwrite'
35for f in "$J/json.om" "$J/jsonw.om" "$J/jsonf.om"; do
36 if grep -Eq '^(global|text|om2)( |$)' "$f"; then fail "$f declares a global, text or om2 line"; fi
37 if grep -v '^note' "$f" | grep -Eq "(^| )($HOST)( |$)"; then fail "$f uses a host word"; fi
38done
39{ printf 'om2\n'; cat "$J/json.om" "$J/jsonw.om" "$J/jsonf.om"; printf 'fn main to int\nret 0\nend\n'; } > "$B/lib.om"
40set +e
41$LOW "$OMC" < "$B/lib.om" > "$B/lib.bin" 2> "$B/lib.err"
42rc=$?
43set -e
44[ "$rc" = 0 ] || fail "library standalone compile rc=$rc $(cat "$B/lib.err")"
45echo 'PASS library compiles standalone (no globals, texts, host words)'
46
47# compose UNIT_DIR OUT: build the driver from the units in UNIT_DIR
48compose() {
49 { printf 'om2\n'; cat "$1/json.om" "$1/jsonw.om" "$1/jsonf.om" "$J/tests/jsdrv.om"; } > "$2.om"
50 set +e
51 $LOW "$OMC" < "$2.om" > "$2" 2> "$2.err"
52 crc=$?
53 set -e
54 [ "$crc" = 0 ] || return 1
55 chmod +x "$2"
56}
57compose "$J" "$B/jsdrv" || fail "driver compile: $(cat "$B/jsdrv.err")"
58printf 'DRIVER %s\n' "$(sha256sum "$B/jsdrv" | cut -d ' ' -f 1)"
59
60# 2. Corpus and expected outputs from the oracle (deterministic, seed 788).
61$LOW python3 "$J/tests/gen.py" "$B/corpus"
62C=$B/corpus
63[ -s "$C/cases.tsv" ] || fail 'empty corpus'
64rows=$(wc -l < "$C/cases.tsv")
65
66# run DRIVER QUIET: every row; QUIET=1 stops at the first mismatch and prints nothing for it.
67# Sets bad to the number of mismatching rows.
68run() {
69 bad=0
70 tab=$(printf '\t')
71 while IFS="$tab" read -r id mode a2 a3 kind; do
72 set -- "$mode"
73 [ "$a2" = - ] || set -- "$@" "$a2"
74 [ "$a3" = - ] || set -- "$@" "$a3"
75 o=$B/out/$id.$mode
76 set +e
77 $LOW timeout 60 "$DRV" "$@" < "$C/$id.in" > "$o" 2> "$o.err"
78 rc=$?
79 set -e
80 ok=1
81 if [ "$rc" != 0 ]; then
82 ok=0
83 elif [ "$kind" = err ]; then
84 case "$(head -c 4 "$o")" in 'ERR ') ;; *) ok=0 ;; esac
85 else
86 cmp -s "$o" "$C/$id.$mode.want" || ok=0
87 fi
88 if [ "$ok" = 0 ]; then
89 bad=$((bad + 1))
90 [ "$QUIET" = 1 ] && return 0
91 printf 'MISMATCH %s mode=%s args=%s rc=%s\n' "$id" "$mode" "$*" "$rc"
92 [ "$bad" -gt 20 ] && return 0
93 fi
94 done < "$C/cases.tsv"
95}
96
97# 3. Parity: every corpus row, every mode.
98DRV=$B/jsdrv QUIET=0 run
99[ "$bad" = 0 ] || fail "parity rows=$rows mismatches=$bad (first ones listed above)"
100printf 'PASS parity rows=%s (tokens, walk, f64, rewrite, writer scripts)\n' "$rows"
101
102# 4. Driver boundary: input larger than its array is refused with exit 93, not truncated.
103head -c 524289 /dev/zero | tr '\0' ' ' > "$B/big.in"
104set +e
105$LOW "$B/jsdrv" t < "$B/big.in" > /dev/null 2>&1
106rc=$?
107set -e
108[ "$rc" = 93 ] || fail "oversized driver input status=$rc (want 93)"
109echo 'PASS oversized driver input refused (93)'
110
111# 4b. Above-old-limit reader, writer and formatter parity; EOF/error/overflow and true uint
112# field edges through documented native function-mode slots; every new defect has a killed twin.
113$LOW python3 "$J/tests/growing.py" "$OMC" "$B/growing" || fail 'growing consumer/boundary cases'
114# Optimization must not remove either test execution or enforcement. Require the deliberate
115# expectation failure specifically, not an arbitrary compile/setup failure with the same exit.
116set +e
117$LOW env PYTHONOPTIMIZE=1 python3 "$J/tests/growing.py" "$OMC" "$B/optimized-growth-control" --wrong-expectation > "$B/optimized-growth-control.log" 2>&1
118orc=$?
119set -e
120[ "$orc" = 1 ] && grep -q 'RuntimeError: reader expectation mismatch' "$B/optimized-growth-control.log" || fail 'optimized growth wrong-expectation survived or failed for another reason'
121echo 'PASS optimized growing wrong expectation rejected (1)'
122set +e
123$LOW env PYTHONOPTIMIZE=1 python3 "$J/tests/field.py" "$OMC" "$B/optimized-field-control" --wrong-expectation > "$B/optimized-field-control.log" 2>&1
124orc=$?
125set -e
126[ "$orc" = 1 ] && grep -q 'FAIL field/stop-copy status=0 got=-21 want=-22' "$B/optimized-field-control.log" || fail 'optimized field wrong-expectation survived or failed for another reason'
127echo 'PASS optimized field wrong expectation rejected (1)'
128
129# Consumer applications must continue compiling, including their independently owned buffer types.
130if [ -n "${JSON_COMPAT_BASE:-}" ]; then
131 $LOW python3 "$J/tests/consumers.py" "$OMC" "$B/consumers" --base "$JSON_COMPAT_BASE" || fail 'base/head consumer composition parity'
132else
133 $LOW python3 "$J/tests/consumers.py" "$OMC" "$B/consumers" || fail 'consumer compositions'
134fi
135
136set +e
137$LOW env PYTHONOPTIMIZE=1 python3 "$J/tests/consumers.py" "$OMC" "$B/optimized-consumer-control" --wrong-expectation > "$B/optimized-consumer-control.log" 2>&1
138orc=$?
139set -e
140[ "$orc" = 1 ] && grep -q 'RuntimeError: composition expectation mismatch: bench-chat' "$B/optimized-consumer-control.log" || fail 'optimized consumer wrong-expectation survived or failed for another reason'
141echo 'PASS optimized consumer wrong expectation rejected (1)'
142
143# 5. Mutants: each must make the parity run fail.
144mutant() { # name file old new
145 rm -rf "$B/m"
146 mkdir -p "$B/m"
147 cp "$J/json.om" "$J/jsonw.om" "$J/jsonf.om" "$B/m/"
148 n=$(grep -cxF "$3" "$B/m/$2" || true)
149 [ "$n" = 1 ] || fail "mutant $1: pattern found $n times in $2"
150 awk -v old="$3" -v new="$4" '$0 == old { print new; next } { print }' "$B/m/$2" > "$B/m/x"
151 mv "$B/m/x" "$B/m/$2"
152 if ! compose "$B/m" "$B/m/jsdrv"; then fail "mutant $1 does not compile: $(cat "$B/m/jsdrv.err")"; fi
153 DRV=$B/m/jsdrv QUIET=1 run
154 [ "$bad" != 0 ] || fail "mutant $1 survived"
155 printf 'PASS mutant %s caught\n' "$1"
156}
157mutant overlong-e0 json.om 'lo = uchar 160' 'lo = uchar 128'
158mutant raw-control json.om 'if lt c 32' 'if lt c 31'
159mutant depth-off-by-one json.om 'if ge d js_hg tape jshdlim' 'if gt d js_hg tape jshdlim'
160mutant writer-escape jsonw.om 'if ge c 32' 'if ge c 31'
161mutant ties-to-even jsonf.om 'ret eq rem js_dget fs q ulong sub i 1 2 1' 'ret true'
162rm -rf "$B/m"
163printf 'JSONGATE PASS rows=%s mutants=5 growing=PASS source=%s\n' "$rows" "$(git rev-parse HEAD)"