interplane.Get Omega
Read in your language

babel

aien-dev/babel

Brought by drakestapleton

branch main Branches Tags History

babel / tests / run.sh

296 lines · 17951 bytes Raw Blame History
1#!/bin/sh
2# babel end-to-end tests. Coordination script only: every check here inspects babel's
3# output with independent tools (cmp, python3 -m json.tool); it implements nothing.
4# usage: BABEL_ADAPTERS=<built omega-mv adapter clone> tests/run.sh [targets...]
5# Optional: ZIG=<zig>, GO=<go> when those toolchains are not on PATH.
6set -u
7here=$(cd "$(dirname "$0")/.." && pwd)
8B="$here/build/babel"
9: "${BABEL_ADAPTERS:?set BABEL_ADAPTERS to a built omega-mv adapter clone}"
10targets=${*:-rust c python cpp zig go}
11work=$(mktemp -d)
12trap 'rm -rf "$work"' EXIT
13fails=0
14ok() { echo "PASS $*"; }
15bad() { echo "FAIL $*"; fails=$((fails + 1)); }
16
17# 1. lint: a statement starting with an unknown name silently becomes an assignment in
18# OM-2, so every bare statement word must be a defined function or a host word.
19awk '/^fn /{print $2}' "$here"/build/babel.om | sort -u > "$work/fns"
20cat "$here"/src/*.om | awk '$1 ~ /^[a-z][a-z0-9]*$/ && $2 != "=" {print $1}' | sort -u \
21 | grep -vxF -f "$work/fns" \
22 | grep -vxE 'if|while|for|end|else|ret|put|break|continue|note|text|global|fn|elif|exit|reset' > "$work/lint"
23if [ -s "$work/lint" ]; then bad "lint: unknown statement words: $(tr '\n' ' ' < "$work/lint")"; else ok "lint"; fi
24
25# 2. usage paths
26"$B" --version > /dev/null; [ $? -eq 0 ] && ok "--version" || bad "--version"
27"$B" > /dev/null 2>&1; [ $? -eq 2 ] && ok "no path exits 2" || bad "no path exits 2"
28"$B" "$here/examples/sample" --adapters "$BABEL_ADAPTERS" < /dev/null > /dev/null 2>&1; r=$?
29[ $r -eq 64 ] && ok "no --to without a terminal exits 64" || bad "no --to without a terminal exits 64 (got $r)"
30
31# 3. every target on the sample: exit status, verdict counts, JSON validity, independent cmp
32cp -r "$here/examples/sample" "$work/sample"
33for t in $targets; do
34 (cd "$work" && "$B" sample --to "$t" --adapters "$BABEL_ADAPTERS" --json > "$work/$t.json" 2> "$work/$t.err")
35 r=$?
36 [ $r -eq 0 ] && ok "$t: exit 0" || { bad "$t: exit $r"; cat "$work/$t.err"; }
37 if python3 -m json.tool "$work/$t.json" > /dev/null && python3 -m json.tool "$work/sample.babel/report.json" > /dev/null; then
38 ok "$t: --json and report.json parse"
39 else
40 bad "$t: JSON does not parse"
41 fi
42 s=$(python3 -c 'import json,sys; d=json.load(open(sys.argv[1]))["summary"]; print(d["translated"],d["unsupported"],d["missing_implementation"],d["fail"],d["not_run"],d["absorbed"])' "$work/sample.babel/report.json")
43 [ "$s" = "5 7 5 0 0 2" ] && ok "$t: verdicts 5 TRANSLATED / 7 UNSUPPORTED / 5 MISSING / 0 FAIL / 0 NOT_RUN / 2 ABSORBED" || bad "$t: verdicts $s"
44 f0=$fails
45 for u in fold square add3 poly mix6; do
46 d="$work/sample.babel/diff/$u"
47 n=$(head -1 "$d/vectors.txt" | cut -d' ' -f2)
48 if [ -s "$d/c-original.out" ] && cmp -s "$d/c-original.out" "$d/omega.out" && cmp -s "$d/c-original.out" "$d/target.out" \
49 && [ "$(wc -l < "$d/c-original.out")" -eq "$n" ]; then
50 :
51 else
52 bad "$t: $u three legs not byte-identical (independent cmp)"
53 fi
54 done
55 [ $fails -eq $f0 ] && ok "$t: independent cmp: 3 legs byte-identical, one line per vector, 5 units"
56done
57
58# 4. negative control: a cc wrapper that changes fold's original C (- k becomes + k)
59# when it compiles the differential oracle. babel must report FAIL, never TRANSLATED.
60mkdir -p "$work/bin"
61realcc=$(command -v cc)
62cat > "$work/bin/cc" <<EOF
63#!/bin/sh
64for a in "\$@"; do
65 case "\$a" in
66 */diff/fold/oracle.c)
67 sed 's/h \* k + h - k/h * k + h + k/' "\$a" > "$work/mut.c"
68 grep -q 'h \* k + h + k' "$work/mut.c" || { echo "mutation did not apply" >&2; exit 99; }
69 set -- \$(for b in "\$@"; do [ "\$b" = "\$a" ] && echo "$work/mut.c" || echo "\$b"; done)
70 ;;
71 esac
72done
73exec "$realcc" "\$@"
74EOF
75chmod +x "$work/bin/cc"
76(cd "$work" && PATH="$work/bin:$PATH" "$B" sample --to c --adapters "$BABEL_ADAPTERS" > "$work/neg.out" 2>&1)
77r=$?
78v=$(python3 -c 'import json,sys
79for u in json.load(open(sys.argv[1]))["units"]:
80 if u.get("name") == "fold": print(u["verdict"], u.get("reason",""))' "$work/sample.babel/report.json")
81case "$v" in
82 FAIL*differs*) ok "negative control: mutated oracle gives FAIL (exit $r): $v" ;;
83 *) bad "negative control: expected FAIL, got exit $r: $v" ;;
84esac
85[ $r -eq 1 ] && ok "negative control: exit 1" || bad "negative control: exit $r, expected 1"
86
87# 5. completion record (policy complete-v1) and --complete exit status.
88# comp DIR prints "state denominator covered n_open" from DIR/report.json and validates the JSON;
89# openl DIR prints one "kind|item|name|verdict|reason" line per open item.
90comp() {
91 python3 -m json.tool "$1/report.json" > /dev/null || { echo "JSON-INVALID"; return; }
92 python3 -c 'import json,sys; c=json.load(open(sys.argv[1]))["completion"]; assert c["policy"]=="complete-v1"; print(c["state"],c["denominator"],c["covered"],len(c["open"]))' "$1/report.json"
93}
94openl() {
95 python3 -c 'import json,sys
96for o in json.load(open(sys.argv[1]))["completion"]["open"]: print("|".join(str(o[k]) for k in ("kind","item","name","verdict","reason")))' "$1/report.json"
97}
98# (f) legacy: no --complete, the sample still exits 0; the record is computed but not enforced
99rm -rf "$work/sample.babel"
100(cd "$work" && "$B" sample --to c --adapters "$BABEL_ADAPTERS" > "$work/cf.out" 2>&1); r=$?
101s=$(comp "$work/sample.babel")
102e=$(python3 -c 'import json,sys; print(json.load(open(sys.argv[1]))["completion"]["enforced"])' "$work/sample.babel/report.json")
103[ $r -eq 0 ] && [ "${s%% *}" = PARTIAL ] && [ "$e" = False ] && ok "complete (f): sample without --complete exits 0, record $s, enforced $e" \
104 || bad "complete (f): exit $r, record $s, enforced $e"
105# (a) the sample under --complete is PARTIAL (exit 3) and names its open items
106rm -rf "$work/sample.babel"
107(cd "$work" && "$B" sample --to c --adapters "$BABEL_ADAPTERS" --complete > "$work/ca.out" 2>&1); r=$?
108s=$(comp "$work/sample.babel")
109openl "$work/sample.babel" > "$work/ca.open"
110if [ $r -eq 3 ] && [ "${s%% *}" = PARTIAL ] && [ "${s##* }" -gt 0 ] \
111 && grep -q '^unit|src/mix.c:15-17|zero|UNSUPPORTED(68)|' "$work/ca.open" \
112 && grep -q '^unit|tools/report.py:1|None|MISSING_IMPLEMENTATION|' "$work/ca.open" \
113 && grep -q '^file|README.txt|None|MISSING_IMPLEMENTATION|' "$work/ca.open" \
114 && grep -q '^## Completion' "$work/sample.babel/REPORT.md" && grep -q 'State: \*\*PARTIAL\*\*' "$work/sample.babel/REPORT.md"; then
115 ok "complete (a): sample --complete exits 3, record $s (state den covered open), open list names zero, tools/report.py, README.txt"
116else
117 bad "complete (a): exit $r, record $s"; cat "$work/ca.open"
118fi
119# (d) negative control: the mutated oracle under --complete is FAILED (exit 1)
120rm -rf "$work/sample.babel"
121(cd "$work" && PATH="$work/bin:$PATH" "$B" sample --to c --adapters "$BABEL_ADAPTERS" --complete > "$work/cd.out" 2>&1); r=$?
122s=$(comp "$work/sample.babel")
123openl "$work/sample.babel" > "$work/cd.open"
124if [ $r -eq 1 ] && [ "${s%% *}" = FAILED ] && grep -q '^unit|src/hash.c:3-5|fold|FAIL|' "$work/cd.open" \
125 && grep -q '^check|src/hash.c:3-5|fold|FAIL|differential leg: Omega' "$work/cd.open"; then
126 ok "complete (d) negative control: mutated oracle under --complete exits 1, record $s, fold unit and Omega leg FAIL"
127else
128 bad "complete (d): exit $r, record $s"; cat "$work/cd.open"
129fi
130# (b) examples/complete (three S1 functions + the stdint include) is COMPLETE (exit 0)
131cp -r "$here/examples/complete" "$work/complete"
132for t in c rust; do
133 rm -rf "$work/complete.babel"
134 (cd "$work" && "$B" complete --to "$t" --adapters "$BABEL_ADAPTERS" --complete > "$work/cb.out" 2>&1); r=$?
135 s=$(comp "$work/complete.babel")
136 set -- $s
137 if [ $r -eq 0 ] && [ "$1" = COMPLETE ] && [ "$2" -eq "$3" ] && [ "$4" -eq 0 ] && grep -q 'State: \*\*COMPLETE\*\*' "$work/complete.babel/REPORT.md"; then
138 ok "complete (b) $t: examples/complete --complete exits 0, record $s, covered == denominator"
139 else
140 bad "complete (b) $t: exit $r, record $s"; openl "$work/complete.babel"
141 fi
142done
143# (c) negative controls: the same input plus one hidden file, or plus one symbolic link, is PARTIAL (exit 3)
144for kind in hidden symlink; do
145 rm -rf "$work/cc" "$work/cc.babel"
146 cp -r "$here/examples/complete" "$work/cc"
147 if [ $kind = hidden ]; then
148 echo 'uint64_t ghost(uint64_t x) { return x; }' > "$work/cc/src/.ghost.c"; want='^skipped|src/.ghost.c|None|SKIPPED|hidden'
149 else
150 ln -s arith.c "$work/cc/src/alias.c"; want='^skipped|src/alias.c|None|SKIPPED|symbolic link'
151 fi
152 (cd "$work" && "$B" cc --to c --adapters "$BABEL_ADAPTERS" --complete > "$work/ccc.out" 2>&1); r=$?
153 s=$(comp "$work/cc.babel")
154 openl "$work/cc.babel" > "$work/cc.open"
155 if [ $r -eq 3 ] && [ "${s%% *}" = PARTIAL ] && [ "$(wc -l < "$work/cc.open")" -eq 1 ] && grep -q "$want" "$work/cc.open"; then
156 ok "complete (c) negative control ($kind): exits 3, record $s, the only open item: $(cut -d'|' -f2,4 "$work/cc.open")"
157 else
158 bad "complete (c) $kind: exit $r, record $s"; cat "$work/cc.open"
159 fi
160done
161# (e) a missing target toolchain is BLOCKED (exit 4)
162rm -rf "$work/complete.babel"
163(cd "$work" && ZIG=/nonexistent "$B" complete --to zig --adapters "$BABEL_ADAPTERS" --complete > "$work/ce.out" 2>&1); r=$?
164s=$(comp "$work/complete.babel")
165openl "$work/complete.babel" > "$work/ce.open"
166if [ $r -eq 4 ] && [ "${s%% *}" = BLOCKED ] && grep -q '^toolchain|zig|None|NOT_RUN|' "$work/ce.open" \
167 && grep -q '^check|src/arith.c:3-5|blend|NOT_RUN|differential leg: target zig did not run' "$work/ce.open"; then
168 ok "complete (e): ZIG=/nonexistent --to zig --complete exits 4, record $s"
169else
170 bad "complete (e): exit $r, record $s"; cat "$work/ce.open"
171fi
172
173# 6. --exclude manifest. exl DIR prints "excluded_count walked_as" and one "kind|item|reason|matched_by"
174# line per excluded item. Manifests live in $work, outside the walked tree, unless a test says so.
175exl() {
176 python3 -c 'import json,sys
177c=json.load(open(sys.argv[1]))["completion"]; m=c["manifest"]
178assert c["excluded_count"]==len(c["excluded"])
179print(c["excluded_count"], m["walked_as"] if m else "-")
180for x in c["excluded"]: assert x["verdict"]=="EXCLUDED"; print("|".join(str(x[k]) for k in ("kind","item","reason","matched_by")))' "$1/report.json"
181}
182rm -rf "$work/cg" "$work/cg.babel"
183cp -r "$here/examples/complete" "$work/cg"
184echo 'notes for people' > "$work/cg/README.txt"
185# (g) README.txt makes it PARTIAL (exit 3); a manifest excluding it with a reason makes it COMPLETE (exit 0)
186(cd "$work" && "$B" cg --to c --adapters "$BABEL_ADAPTERS" --complete > "$work/cg0.out" 2>&1); r0=$?
187s0=$(comp "$work/cg.babel")
188printf '# documentation is not translated\n\nREADME.txt human notes, no code \n' > "$work/g.exclude"
189(cd "$work" && "$B" cg --to c --adapters "$BABEL_ADAPTERS" --complete --exclude g.exclude --no-color > "$work/cg1.out" 2>&1); r1=$?
190s1=$(comp "$work/cg.babel")
191exl "$work/cg.babel" > "$work/cg1.exl"
192sha8=$(sha256sum "$work/g.exclude" | cut -c1-8)
193set -- $s0; d0=$2
194set -- $s1
195if [ $r0 -eq 3 ] && [ "${s0%% *}" = PARTIAL ] && [ $r1 -eq 0 ] && [ "$1" = COMPLETE ] && [ "$2" -eq "$d0" ] && [ "$4" -eq 0 ] \
196 && [ "$(head -1 "$work/cg1.exl")" = "1 None" ] && grep -qx 'file|README.txt|human notes, no code|README.txt' "$work/cg1.exl" \
197 && grep -q "COMPLETE $3 of $2 items covered, 1 excluded by manifest $sha8" "$work/cg1.out" \
198 && grep -qF '| file | `README.txt` | EXCLUDED | `README.txt` (line 3) | human notes, no code |' "$work/cg.babel/REPORT.md"; then
199 ok "complete (g): +README.txt exits $r0 ($s0); --exclude README.txt exits 0, record $s1, denominator unchanged $d0, excluded_count 1, manifest $sha8"
200else
201 bad "complete (g): exits $r0/$r1, records $s0 / $s1"; cat "$work/cg1.exl" "$work/cg1.out"
202fi
203# (g2) the manifest inside the walked tree is counted (covered); a hidden non-source entry may be excluded
204printf 'x\n' > "$work/cg/.gitignore"
205printf 'README.txt human notes\n.gitignore VCS metadata, no code\n' > "$work/cg/babel.exclude"
206(cd "$work" && "$B" cg --to c --adapters "$BABEL_ADAPTERS" --complete --exclude cg/babel.exclude > "$work/cg2.out" 2>&1); r=$?
207s=$(comp "$work/cg.babel")
208exl "$work/cg.babel" > "$work/cg2.exl"
209set -- $s
210if [ $r -eq 0 ] && [ "$1" = COMPLETE ] && [ "$2" -eq $((d0 + 2)) ] && [ "$3" -eq "$d0" ] && [ "$(head -1 "$work/cg2.exl")" = "2 babel.exclude" ] \
211 && grep -qx 'skipped|.gitignore|VCS metadata, no code|.gitignore' "$work/cg2.exl"; then
212 ok "complete (g2): in-tree manifest covered, .gitignore excluded: exits 0, record $s, excluded_count 2, walked_as babel.exclude"
213else
214 bad "complete (g2): exit $r, record $s"; cat "$work/cg2.exl"; openl "$work/cg.babel"
215fi
216rm -f "$work/cg/.gitignore" "$work/cg/babel.exclude"
217# (h)-(k) negative controls: each bad manifest exits 2 with a line diagnostic and writes no output folder
218xneg() { # name manifest-text expected-stderr
219 rm -rf "$work/cg.babel"
220 printf "$2" > "$work/$1.exclude"
221 (cd "$work" && "$B" cg --to c --adapters "$BABEL_ADAPTERS" --complete --exclude "$1.exclude" > "$work/$1.out" 2>&1); r=$?
222 if [ $r -eq 2 ] && grep -qF "$3" "$work/$1.out" && [ ! -e "$work/cg.babel" ]; then
223 ok "complete ($1) negative control: exits 2, no output folder: $(grep -- '--exclude' "$work/$1.out" | head -1)"
224 else
225 bad "complete ($1): exit $r, expected 2 and /$3/"; cat "$work/$1.out"
226 fi
227}
228xneg h 'src/arith.c not wanted\n' "line 1: entry names source babel splits, which cannot be excluded: src/arith.c -> src/arith.c"
229xneg h2 'src/ the whole tree\n' "line 1: entry names source babel splits, which cannot be excluded: src -> src/arith.c"
230xneg i 'README.txt human notes\nnothere.txt gone\n' "line 2: entry matches nothing in the walked tree (stale manifest): nothere.txt"
231xneg j '# no reason below\nREADME.txt\n' "line 2: entry has no reason (each line is <path> <reason>): README.txt"
232xneg j2 'src/*.txt glob\n' "line 1: globs are not supported"
233echo 'uint64_t ghost(uint64_t x) { return x; }' > "$work/cg/src/.ghost.c"
234xneg k 'README.txt human notes\nsrc/.ghost.c hidden scratch\n' "line 2: entry names source babel splits, which cannot be excluded: src/.ghost.c -> src/.ghost.c"
235rm -f "$work/cg/src/.ghost.c"
236ln -s arith.c "$work/cg/src/alias.c"
237xneg k2 'README.txt human notes\nsrc/alias.c a link\n' "line 2: entry names source babel splits, which cannot be excluded: src/alias.c -> src/alias.c"
238rm -f "$work/cg/src/alias.c"
239
240# 7. completion gaps: other non-regular file, unreadable directory, missing cc, failing generated test
241rm -rf "$work/cf" "$work/cf.babel"
242cp -r "$here/examples/complete" "$work/cf"
243mkfifo "$work/cf/src/pipe"
244(cd "$work" && "$B" cf --to c --adapters "$BABEL_ADAPTERS" --complete > "$work/cf7.out" 2>&1); r=$?
245s=$(comp "$work/cf.babel"); openl "$work/cf.babel" > "$work/cf7.open"
246if [ $r -eq 3 ] && [ "${s%% *}" = PARTIAL ] && [ "$(wc -l < "$work/cf7.open")" -eq 1 ] && grep -q '^skipped|src/pipe|None|SKIPPED|not a regular file' "$work/cf7.open"; then
247 ok "complete gap (fifo): exits 3, record $s, the only open item: $(cut -d'|' -f2,4,5 "$work/cf7.open")"
248else
249 bad "complete gap (fifo): exit $r, record $s"; cat "$work/cf7.open"
250fi
251rm -f "$work/cf/src/pipe"
252if [ "$(id -u)" = 0 ]; then
253 echo "NOT_RUN (root) complete gap (unreadable directory): root reads a mode-000 directory"
254else
255 mkdir "$work/cf/locked"; echo x > "$work/cf/locked/a.txt"; chmod 000 "$work/cf/locked"
256 trap 'chmod 755 "$work/cf/locked" 2>/dev/null; rm -rf "$work"' EXIT
257 (cd "$work" && "$B" cf --to c --adapters "$BABEL_ADAPTERS" --complete > "$work/cu.out" 2>&1); r=$?
258 chmod 755 "$work/cf/locked"; rm -rf "$work/cf/locked"
259 s=$(comp "$work/cf.babel"); openl "$work/cf.babel" > "$work/cu.open"
260 if [ $r -eq 3 ] && [ "${s%% *}" = PARTIAL ] && [ "$(wc -l < "$work/cu.open")" -eq 1 ] && grep -q '^skipped|locked|None|SKIPPED|directory could not be read' "$work/cu.open"; then
261 ok "complete gap (unreadable directory): exits 3, record $s, the only open item: $(cut -d'|' -f2,4,5 "$work/cu.open")"
262 else
263 bad "complete gap (unreadable directory): exit $r, record $s"; cat "$work/cu.open"
264 fi
265fi
266# a PATH with every tool but cc: the oracle cannot be built, so the run is BLOCKED (exit 4)
267mkdir "$work/nocc"
268for d in $(echo "$PATH" | tr ':' ' '); do
269 [ -d "$d" ] || continue
270 for f in "$d"/*; do
271 n=${f##*/}; [ "$n" = cc ] && continue; [ -e "$work/nocc/$n" ] || [ -L "$work/nocc/$n" ] || ln -s "$f" "$work/nocc/$n"
272 done
273done
274(cd "$work" && PATH="$work/nocc" "$B" cf --to python --adapters "$BABEL_ADAPTERS" --complete > "$work/cn.out" 2>&1); r=$?
275s=$(comp "$work/cf.babel"); openl "$work/cf.babel" > "$work/cn.open"
276if [ $r -eq 4 ] && [ "${s%% *}" = BLOCKED ] && grep -q '^toolchain|cc|None|NOT_RUN|cc was not found' "$work/cn.open" && ! grep -q '|FAIL|' "$work/cn.open"; then
277 ok "complete gap (no cc): exits 4, record $s, $(grep -c NOT_RUN "$work/cn.open") NOT_RUN items incl. toolchain cc"
278else
279 bad "complete gap (no cc): exit $r, record $s"; cat "$work/cn.open"
280fi
281# a cargo that fails only "cargo test": the generated target test is the one FAIL (exit 1)
282realcargo=$(command -v cargo)
283mkdir "$work/cbin"
284printf '#!/bin/sh\nif [ "$1" = test ]; then echo "shim: cargo test forced to fail" >&2; exit 101; fi\nexec "%s" "$@"\n' "$realcargo" > "$work/cbin/cargo"
285chmod +x "$work/cbin/cargo"
286(cd "$work" && PATH="$work/cbin:$PATH" "$B" cf --to rust --adapters "$BABEL_ADAPTERS" --complete > "$work/ct.out" 2>&1); r=$?
287s=$(comp "$work/cf.babel"); openl "$work/cf.babel" > "$work/ct.open"
288if [ $r -eq 1 ] && [ "${s%% *}" = FAILED ] && [ "$(wc -l < "$work/ct.open")" -eq 1 ] && grep -q '^check|diff/test.log|None|FAIL|generated target test failed (see diff/test.log), exit 101' "$work/ct.open"; then
289 ok "complete gap (generated test): cargo test fails, exits 1, record $s, the only open item: $(cut -d'|' -f2,4,5 "$work/ct.open")"
290else
291 bad "complete gap (generated test): exit $r, record $s"; cat "$work/ct.open"
292fi
293echo "NOT_RUN complete gap (lstat failure, skip reason 6): no unprivileged way to make lstat fail on an entry getdents returned"
294
295echo "failures: $fails"
296[ $fails -eq 0 ]