interplane.Get Omega
Read in your language

omega-feel

This project has not described itself yet.

Brought by drakestapleton

branch main Branches Tags History

omega-feel / tests / run.sh

306 lines · 17219 bytes Raw Blame History
1#!/bin/sh
2# tests/run.sh OMC: the gate for feel.om.
3# 1. OMC must be the compiler named in PIN (sha256).
4# 2. feel.om is built with it into a temporary directory.
5# 3. Each trace is made by tests/gen.py, must have the SHA-256 Wolfram printed, and feel's output on
6# it must be exactly the lines Wolfram printed (tests/expected-wolfram.txt).
7# 3a. The constants SPEC.md section 3 lists must be constants of feel.om with those values.
8# 3b. Each Wolfram script and its result must be the pair sealed in tests/wolfram.sha256 (sh tests/seal.sh
9# writes it); the rejection launches of tests/wolfram-slow-trial-result.txt must be the R launches of
10# scenario cap in tests/expected-wolfram.txt.
11# 4. Small cases worked out by hand from SPEC.md sections 3 and 4: end-of-input lines, bad input, memory
12# refused, the alarm on the growth of the class tables, and the engine entries (tests/api-main.om,
13# checked against tests/api-expected.txt).
14# 5. Mutants: feel.om with one rule or one check changed by sed. Each must differ from feel.om in one
15# line, compile, and get at least one trace, hand case or engine-entry check wrong. A mutant that
16# still passes everything fails the gate.
17# Last line: GATE PASS cases=N mutants_killed=M, or GATE FAIL with the reason (exit 1).
18set -u
19fail() { [ -n "${QUIET:-}" ] || echo "GATE FAIL $*"; exit 1; }
20[ $# -eq 1 ] || fail "usage: sh tests/run.sh OMC"
21omc=$1
22case $omc in */*) ;; *) omc=./$omc ;; esac
23root=$(cd "$(dirname "$0")/.." && pwd) || fail "cannot find the repository"
24exp=$root/tests/expected-wolfram.txt
25src=$root/feel.om
26T=$(mktemp -d) || fail "no temporary directory"
27trap 'rm -rf "$T"' EXIT
28trap 'exit 1' HUP INT TERM
29
30# 1. the compiler
31pin=$(sed -n 's/^omc-sha256 //p' "$root/PIN")
32[ -n "$pin" ] || fail "PIN has no omc-sha256 line"
33[ -f "$omc" ] || fail "no compiler at $omc"
34have=$(sha256sum < "$omc" | cut -d' ' -f1)
35[ "$have" = "$pin" ] || fail "compiler sha256 $have is not PIN's $pin"
36
37# 2. build SRC OUT
38build() { "$omc" < "$1" > "$2" 2> "$2.err" && chmod +x "$2"; }
39build "$src" "$T/feel" || fail "feel.om does not compile: $(cat "$T/feel.err")"
40
41# 3. traces and Wolfram's lines
42names="fast5 slow10 wrongfast equal tailC drift tailI two ties cap lateGain earlyGain equalSum lateWrong meanWin"
43# want SCENARIO CLASS FILE: Wolfram's lines for SCENARIO, with its name replaced by CLASS
44want() {
45 grep "^$1 " "$exp" | grep -v "^$1 sha256 " | sed "s/^$1 /$2 /" > "$3"
46 [ -s "$3" ] || fail "no expected lines for $1"
47}
48for n in $names; do
49 python3 "$root/tests/gen.py" "$n" > "$T/$n.trace" || fail "tests/gen.py $n"
50 w=$(sed -n "s/^$n sha256 //p" "$exp")
51 [ -n "$w" ] || fail "no sha256 line for $n in tests/expected-wolfram.txt"
52 have=$(sha256sum < "$T/$n.trace" | cut -d' ' -f1)
53 [ "$have" = "$w" ] || fail "trace $n has sha256 $have, Wolfram's is $w"
54 if [ "$n" = two ]; then
55 want twoA 0 "$T/two.want0"
56 want twoB 1 "$T/two.want1"
57 else
58 want "$n" 0 "$T/$n.want"
59 fi
60done
61# 3a. the constants SPEC.md section 3 lists are the constants of feel.om
62sed -n '/^Constants:/,/^$/p' "$root/SPEC.md" | grep -o '`[A-Z0-9]* = [0-9]*`' | tr -d '`' > "$T/specconst"
63[ "$(wc -l < "$T/specconst")" -ge 9 ] || fail "SPEC.md section 3 lists fewer than 9 constants"
64while read -r name eq val; do
65 grep -q "^const $name ulong $val\$" "$src" || fail "SPEC.md says $name = $val; feel.om has no such const"
66done < "$T/specconst"
67
68# the Wolfram scripts that are not checked line by line against feel (the simulation, the count chain, the
69# slow-trial timeline) restate its constants: they must be feel.om's
70cv() { sed -n "s/^const $1 ulong //p" "$src"; }
71H=$(cv H); NMIN=$(cv NMIN); NMAX=$(cv NMAX); V=$(cv V); WARM=$(cv WARM); REST0=$(cv REST0); RESTMAX=$(cv RESTMAX)
72MN=$(cv MNUM); MD=$(cv MDEN)
73grep -q "^H = $H; NMIN = $NMIN; NMAX = $NMAX;\$" "$root/tests/wolfram-rules-sim.wl" ||
74 fail "tests/wolfram-rules-sim.wl does not set H, NMIN and NMAX as feel.om does"
75grep -q "$MD hC < $MN hI" "$root/tests/wolfram-rules-sim.wl" ||
76 fail "tests/wolfram-rules-sim.wl does not use the mean rule $MD/$MN of feel.om"
77grep -q "duel\[p, $H, $NMIN, $NMAX\]" "$root/tests/wolfram-duel.wl" ||
78 fail "tests/wolfram-duel.wl does not use H, NMIN and NMAX of feel.om"
79grep -q "^WARM = $WARM; REST0 = $REST0; RESTMAX = $RESTMAX; V = $V; NMIN = $NMIN;\$" "$root/tests/wolfram-slow-trial.wl" ||
80 fail "tests/wolfram-slow-trial.wl does not set WARM, REST0, RESTMAX, V and NMIN as feel.om does"
81
82# 3b. the Wolfram scripts and their results
83man=$root/tests/wolfram.sha256
84[ -f "$man" ] || fail "no tests/wolfram.sha256 (evaluate the scripts, then sh tests/seal.sh)"
85for w in "$root"/tests/*.wl; do
86 b=$(basename "$w")
87 grep -q "^$b " "$man" || fail "tests/$b is not in tests/wolfram.sha256"
88done
89while read -r script h1 result h2; do
90 [ -f "$root/tests/$script" ] && [ -f "$root/tests/$result" ] || fail "tests/wolfram.sha256 names a missing file: $script $result"
91 [ "$(sha256sum < "$root/tests/$script" | cut -d' ' -f1)" = "$h1" ] ||
92 fail "tests/$script changed after its result was sealed: evaluate it again, write $result, then sh tests/seal.sh"
93 [ "$(sha256sum < "$root/tests/$result" | cut -d' ' -f1)" = "$h2" ] ||
94 fail "tests/$result is not the sealed result of tests/$script"
95done < "$man"
96slow=$(sed -n 's/^rejects //p' "$root/tests/wolfram-slow-trial-result.txt")
97capr=$(grep '^cap R ' "$exp" | awk '{printf "%s%s", sep, $4; sep=" "}')
98[ -n "$slow" ] && [ "$slow" = "$capr" ] ||
99 fail "the rejection launches of wolfram-slow-trial-result.txt ($slow) are not those of scenario cap ($capr)"
100
101# same BIN NAME: true when BIN, on trace NAME, exits 0, writes nothing to standard error and prints
102# exactly Wolfram's lines (two: class 0's lines are twoA's, class 1's are twoB's, and no other line)
103same() {
104 "$1" < "$T/$2.trace" > "$T/out" 2> "$T/err" || return 1
105 [ ! -s "$T/err" ] || return 1
106 if [ "$2" = two ]; then
107 grep '^0 ' "$T/out" > "$T/out0"
108 grep '^1 ' "$T/out" > "$T/out1"
109 cmp -s "$T/out0" "$T/two.want0" && cmp -s "$T/out1" "$T/two.want1" &&
110 [ "$(wc -l < "$T/out")" -eq "$(cat "$T/two.want0" "$T/two.want1" | wc -l)" ]
111 else
112 cmp -s "$T/out" "$T/$2.want"
113 fi
114}
115# apicheck SRC: true when SRC, built with tests/api-main.om in place of its own main, prints tests/api-expected.txt
116apibuild() {
117 awk '/^fn main to int$/ { exit } { print }' "$1" > "$2.om" && cat "$root/tests/api-main.om" >> "$2.om" &&
118 build "$2.om" "$2"
119}
120apicheck() {
121 apibuild "$1" "$T/api" && "$T/api" > "$T/api.out" 2> "$T/api.err" && [ ! -s "$T/api.err" ] &&
122 cmp -s "$T/api.out" "$root/tests/api-expected.txt"
123}
124
125# 4. cases by hand. They run against the binary named by B, and with QUIET set a failure is silent: the
126# mutants run them too (hand), in a subshell.
127cases=0
128# good LABEL INPUT WANT: exit 0, nothing on standard error, standard output exactly WANT
129good() {
130 printf '%b' "$2" | "$B" > "$T/out" 2> "$T/err"
131 rc=$?
132 [ $rc -eq 0 ] || fail "$1: exit $rc, want 0"
133 [ ! -s "$T/err" ] || fail "$1: wrote to standard error"
134 printf '%b' "$3" > "$T/wantout"
135 cmp -s "$T/out" "$T/wantout" || fail "$1: output is not the expected one"
136 cases=$((cases + 1))
137}
138# bad LABEL INPUT: exit 2, one line on standard error, nothing on standard output
139bad() {
140 printf '%b' "$2" | "$B" > "$T/out" 2> "$T/err"
141 rc=$?
142 [ $rc -eq 2 ] || fail "bad input ($1): exit $rc, want 2"
143 [ "$(wc -l < "$T/err")" -eq 1 ] || fail "bad input ($1): standard error is not one line"
144 [ ! -s "$T/out" ] || fail "bad input ($1): wrote to standard output"
145 cases=$((cases + 1))
146}
147# badafter LABEL INPUT WANTOUT WANTERR: exit 2, the events already decided stay printed (WANTOUT, with \n),
148# and standard error is exactly the one line WANTERR
149badafter() {
150 printf '%b' "$2" | "$B" > "$T/out" 2> "$T/err"
151 rc=$?
152 [ $rc -eq 2 ] || fail "bad input after events ($1): exit $rc, want 2"
153 printf '%b' "$3" > "$T/wantout"
154 cmp -s "$T/out" "$T/wantout" || fail "bad input after events ($1): the events printed are not the expected ones"
155 printf '%s\n' "$4" > "$T/wanterr"
156 cmp -s "$T/err" "$T/wanterr" || fail "bad input after events ($1): standard error is not '$4'"
157 cases=$((cases + 1))
158}
159# nomem LABEL KIB INPUT: with KIB KiB of address space (0: no limit) the host refuses the tables'
160# memory: exit 4, one line on standard error, nothing on standard output
161nomem() {
162 printf '%b' "$3" > "$T/in"
163 ( [ "$2" -eq 0 ] || ulimit -v "$2" || exit 99; "$B" < "$T/in" > "$T/out" 2> "$T/err" )
164 rc=$?
165 [ $rc -eq 4 ] || fail "memory refused ($1): exit $rc, want 4"
166 [ "$(wc -l < "$T/err")" -eq 1 ] || fail "memory refused ($1): standard error is not one line"
167 [ ! -s "$T/out" ] || fail "memory refused ($1): wrote to standard output"
168 cases=$((cases + 1))
169}
170# alarm: the class tables must grow with the classes declared, not faster. 100000 classes must fit in
171# 48 MiB of address space (the threshold, SPEC.md section 4: about 500 bytes per class). The number it
172# prints when it fails is the point of the test: it tells whoever sees it to look, it stops nothing.
173awk 'BEGIN { for (i = 0; i < 100000; i++) print "c " i " 2" }' > "$T/c100k"
174alarm() {
175 ( ulimit -v 49152 || exit 99; "$B" < "$T/c100k" > "$T/out" 2> "$T/err" )
176 rc=$?
177 [ $rc -eq 0 ] && [ "$(wc -l < "$T/out")" -eq 100000 ] ||
178 fail "ALARM: 100000 declared classes no longer fit in 49152 KiB of address space (exit $rc, $(wc -l < "$T/out") lines)"
179 cases=$((cases + 1))
180}
181hand() {
182 good "empty input" '' ''
183 good "a declared class with no launch" 'c 5 2\n' '5 F 0 1024 0 0 1 0\n'
184 good "class 255 with 8 arms, one launch" 'c 255 8\nt 255 1 2 3 4 5 6 7 8 255\n' \
185 '255 F 0 1024 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0\n'
186 good "classes end in increasing order" 'c 9 2\nc 3 3\nt 9 7 7 3\n' \
187 '3 F 0 1024 0 0 0 1 0 0\n9 F 0 1024 1 0 1 0\n'
188 good "class 256: the old limit of 256 classes is gone" 'c 256 2\n' '256 F 0 1024 0 0 1 0\n'
189 good "a launch of class 1000" 'c 1000 2\nt 1000 5 6 3\n' '1000 F 0 1024 1 0 1 0\n'
190 good "9 arms: the old limit of 8 arms is gone" 'c 0 9\n' '0 F 0 1024 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0\n'
191 z48=$(yes 0 | head -48 | tr '\n' ' ')
192 good "49 arms, the most the mask can name" "c 0 49\nt 0 $(seq -s ' ' 1 49) 562949953421311\n" "0 F 0 1024 1 ${z48}1 ${z48%?}\n"
193 bad "another line" 'x 0 2\n'
194 bad "an empty line" 'c 0 2\n\n'
195 bad "a class used before it is declared" 't 0 5 6 3\n'
196 bad "the other class is declared, this one is not" 'c 1 2\nt 0 5 6 3\n'
197 bad "a class declared twice" 'c 0 2\nc 0 2\n'
198 bad "class 256 in a launch" 'c 0 2\nt 256 5 6 3\n'
199 bad "1 arm" 'c 0 1\n'
200 bad "50 arms, more than the mask can name" 'c 0 50\n'
201 bad "0 arms" 'c 0 0\n'
202 bad "a time missing" 'c 0 2\nt 0 5 3\n'
203 bad "a time too many" 'c 0 2\nt 0 5 6 7 3\n'
204 bad "a declaration with a third number" 'c 0 2 2\n'
205 bad "a word where a number belongs" 'c 0 2\nt 0 5 x 3\n'
206 bad "a minus sign" 'c 0 2\nt 0 5 -6 3\n'
207 bad "two spaces" 'c 0 2\nt 0 5 6 3\n'
208 bad "a space at the end" 'c 0 2 \n'
209 bad "a mask bit past the last arm" 'c 0 2\nt 0 5 6 4\n'
210 bad "a mask of 2^49 for 49 arms" "c 0 49\nt 0 $(seq -s ' ' 1 49) 562949953421312\n"
211 bad "a number of 16 digits" 'c 0 2\nt 0 1000000000000000 6 3\n'
212 bad "a class number of 16 digits" 'c 1000000000000000 2\n'
213 bad "a last line without its newline" 'c 0 2\nt 0 5 6 3'
214 badafter "a bad line after 67 launches" "c 0 2\n$(yes 't 0 5 6 3\n' | head -67 | tr -d '\n')x\n" '0 M 1 67\n' 'feel: bad input, line 69'
215 nomem "a class number past the address space" 0 'c 999999999999999 2\n'
216 nomem "a million classes in 64 MiB" 65536 'c 999999 2\n'
217 alarm
218}
219B=$T/feel
220hand
221
222# the engine entries
223apicheck "$src" || fail "the engine entries differ from tests/api-expected.txt (built from tests/api-main.om): $(diff "$T/api.out" "$root/tests/api-expected.txt" | head -5 | tr '\n' '|')"
224cases=$((cases + 1))
225
226# wrong BIN SRC: what BIN, built from SRC, gets wrong: the traces, the engine entries and the hand cases
227wrong() {
228 f=""
229 for n in $names; do same "$1" "$n" || f="$f $n"; done
230 apicheck "$2" || f="$f api"
231 ( QUIET=1; B=$1; hand ) > /dev/null 2>&1 || f="$f hand"
232 echo "$f"
233}
234for n in $names; do
235 same "$T/feel" "$n" || fail "feel differs from Wolfram on trace $n"
236 cases=$((cases + 1))
237done
238
239# 5. mutants
240killed=0
241# mutant NAME SED-SCRIPT
242mutant() {
243 m=$T/mutant-$1
244 sed "$2" "$src" > "$m.om" || fail "mutant $1: sed failed"
245 cmp -s "$src" "$m.om" && fail "mutant $1: the edit changed nothing"
246 [ "$(diff "$src" "$m.om" | grep -c '^>')" -eq 1 ] || fail "mutant $1: not an edit of one line"
247 build "$m.om" "$m" || fail "mutant $1 does not compile: $(cat "$m.err")"
248 k=$(wrong "$m" "$m.om")
249 [ -n "$k" ] || fail "mutant $1 survived: every trace still matches Wolfram"
250 echo "mutant $1 killed by:$k"
251 killed=$((killed + 1))
252}
253mutant H-12-to-11 's/^const H ulong 12$/const H ulong 11/'
254mutant NMIN-32-to-31 's/^const NMIN ulong 32$/const NMIN ulong 31/'
255mutant NMAX-256-to-254 's/^const NMAX ulong 256$/const NMAX ulong 254/'
256mutant V-3-to-2 's/^const V ulong 3$/const V ulong 2/'
257mutant WARM-64-to-63 's/^const WARM ulong 64$/const WARM ulong 63/'
258mutant REST0-1024-to-1023 's/^const REST0 ulong 1024$/const REST0 ulong 1023/'
259mutant RESTMAX-65536-to-65535 's/^const RESTMAX ulong 65536$/const RESTMAX ulong 65535/'
260mutant RESTMAX-65536-to-65537 's/^const RESTMAX ulong 65536$/const RESTMAX ulong 65537/'
261mutant mean-63-to-64 's/^const MNUM ulong 63$/const MNUM ulong 64/'
262mutant mean-63-to-65 's/^const MNUM ulong 63$/const MNUM ulong 65/'
263mutant mean-63-to-32 's/^const MNUM ulong 63$/const MNUM ulong 32/'
264mutant mean-adopt-keeps-incumbent 's/^if or eq out 1 eq out 3$/if eq out 1/'
265mutant adopt-first-half-only 's/^if and lt hC hI and lt rC rI ge w add l H$/if and lt hC hI ge w add l H/'
266mutant adopt-second-half-only 's/^if and lt hC hI and lt rC rI ge w add l H$/if and lt rC rI ge w add l H/'
267mutant mean-first-half-only 's/^if and lt mul MDEN hC mul MNUM hI lt mul MDEN rC mul MNUM rI$/if lt mul MDEN hC mul MNUM hI/'
268mutant mean-second-half-only 's/^if and lt mul MDEN hC mul MNUM hI lt mul MDEN rC mul MNUM rI$/if lt mul MDEN rC mul MNUM rI/'
269mutant reject-strict-sum 's/^if and ge n NMIN and ge l add w H ge sC sI$/if and ge n NMIN and ge l add w H gt sC sI/'
270mutant adopt-whole-sum-not-halves 's/^if and lt hC hI and lt rC rI ge w add l H$/if and lt add hC rC add hI rI ge w add l H/'
271mutant adopt-without-count 's/^if and lt hC hI and lt rC rI ge w add l H$/if and lt hC hI lt rC rI/'
272mutant half-point-at-quarter 's/^if eq n div NMAX 2$/if eq n div NMAX 4/'
273mutant tie-counts-as-win 's/^if lt tC tI$/if le tC tI/'
274mutant tie-counts-as-loss 's/^if gt tC tI$/if ge tC tI/'
275mutant reject-without-NMIN 's/^if and ge n NMIN and ge l add w H ge sC sI$/if and ge l add w H ge sC sI/'
276mutant reject-ignores-sums 's/^if and ge n NMIN and ge l add w H ge sC sI$/if and ge n NMIN and ge l add w H ne sC sI/'
277mutant ban-admits 's/^put s_status ci 2$/put s_status ci 1/'
278mutant backoff-not-doubled 's/^rl = mul get s_restlen c 2$/rl = mul get s_restlen c 1/'
279mutant adopt-keeps-restlen '/^put s_inc c get s_ch c$/{n;s/^put s_restlen c REST0$/put s_restlen c RESTMAX/;}'
280mutant order-bit-16-to-15 's/^put s_o c and shr z 16 1$/put s_o c and shr z 15 1/'
281mutant order-fixed 's/^if ne and k 1 get s_o c$/if ne and k 1 0/'
282mutant round-robin-stays 's/^put s_next c found$/put s_next c inc/'
283mutant round-robin-skips-one 's/^a = rem add add nx s 1 kk$/a = rem add add nx s 2 kk/'
284mutant shadow-reads-arm-0 's/^e = verdict c and shr mask and shr r 16 65535 1$/e = verdict c and shr mask 0 1/'
285mutant api-arms-min-1 's/^if or ge c CTOP or lt kk 2 gt kk KMAX$/if or ge c CTOP or lt kk 1 gt kk KMAX/'
286mutant api-arms-max-plus-1 's/^if or ge c CTOP or lt kk 2 gt kk KMAX$/if or ge c CTOP or lt kk 2 gt kk add KMAX 1/'
287mutant api-class-top 's/^if or ge c CTOP or lt kk 2 gt kk KMAX$/if or gt c CTOP or lt kk 2 gt kk KMAX/'
288mutant api-declared-twice-accepted 's/^ret E_DUP$/ret 0/'
289mutant api-choose-undeclared-code 's/^ret add ERR E_CLASS$/ret add ERR E_STATE/'
290mutant api-choose-unsettled-accepted 's/^ret add ERR E_STATE$/ret 0/'
291mutant api-verdict-unowed-accepted 's/^if ne get s_owed c 1$/if gt get s_owed c 1/'
292mutant api-observe-unowed-accepted 's/^if ne get s_owed c 2$/if gt get s_owed c 2/'
293mutant api-verdict-equal-2-accepted 's/^if gt equal 1$/if gt equal 2/'
294mutant api-time-at-limit-accepted 's/^if ge t TMAX$/if gt t TMAX/'
295mutant api-arm-places-of-8 's/^ret add get s_base c a$/ret add mul c 8 a/'
296mutant api-choose-accepts-hole '/^fn choose /{n;s/^if eq known c 0$/if ge c len s_K/;}'
297mutant api-verdict-accepts-hole '/^fn verdict /{n;s/^if eq known c 0$/if ge c len s_K/;}'
298mutant api-observe-accepts-hole '/^fn observe /{n;s/^if eq known c 0$/if ge c len s_K/;}'
299mutant api-class-at-table-length 's/^if ge c len s_K$/if gt c len s_K/'
300mutant api-tables-64-times-too-big 's/^ret growby n$/ret growby mul n 64/'
301mutant api-owed-never-set 's/^put s_owed c kind$/put s_owed c 0/'
302mutant trace-times-table-not-grown 's/^m = enlarge g_tm kk$/m = ulong 0/'
303mutant trace-arms-max-plus-1 's/^if or lt kk 2 gt kk KTRACE$/if or lt kk 2 gt kk add KTRACE 1/'
304mutant memory-refused-exit-3 's/^exit 4$/exit 3/'
305
306echo "GATE PASS cases=$cases mutants_killed=$killed"