omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / exercise.sh
| 1 | #!/bin/sh |
| 2 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | # exercise.sh: test one Exercism-style exercise. Omega has no imports (units compose by |
| 4 | # concatenation), so the solution file and the exercise's test.om are joined into one unit, |
| 5 | # compiled with omc, and run. The test program prints one "ok N - ..." or "not ok N - ..." line |
| 6 | # per case and returns the number of failed cases. |
| 7 | # |
| 8 | # sh exercise.sh exercises/leap test your exercises/leap/leap.om |
| 9 | # sh exercise.sh exercises/leap path/to/sol.om test another solution (CI uses .meta/example.om) |
| 10 | # |
| 11 | # The omc used is $OMC, else build/omega-mv/seed/out/omc left by run.sh; its sha256 must match |
| 12 | # omega.ref. stdout: the test lines, then "EXERCISE PASS <name>" or "EXERCISE FAIL <name> ...". |
| 13 | # Exit 0 when every case passes, 1 when a case fails or the solution does not compile or traps, |
| 14 | # 2 when setup fails. |
| 15 | set -u |
| 16 | here=$(cd "$(dirname "$0")" && pwd) |
| 17 | [ $# -ge 1 ] || { echo "usage: sh exercise.sh exercises/<name> [solution.om]" >&2; exit 2; } |
| 18 | dir=${1%/} |
| 19 | name=$(basename "$dir") |
| 20 | sol=${2:-$dir/$name.om} |
| 21 | omc=${OMC:-$here/build/omega-mv/seed/out/omc} |
| 22 | pin=$(awk '$1=="omc_sha256"{print $2}' "$here/omega.ref") |
| 23 | [ -f "$omc" ] || { echo "exercise.sh: no omc at $omc (run sh run.sh first, or set OMC)" >&2; exit 2; } |
| 24 | [ "$(sha256sum "$omc" | awk '{print $1}')" = "$pin" ] || { echo "exercise.sh: omc sha256 is not the pin in omega.ref" >&2; exit 2; } |
| 25 | [ -f "$sol" ] && [ -f "$dir/test.om" ] || { echo "exercise.sh: missing $sol or $dir/test.om" >&2; exit 2; } |
| 26 | tmp=$(mktemp -d) || exit 2 |
| 27 | trap 'rm -rf "$tmp"' EXIT |
| 28 | cat "$sol" "$dir/test.om" > "$tmp/unit.om" |
| 29 | "$omc" < "$tmp/unit.om" > "$tmp/prog" 2> "$tmp/err"; crc=$? |
| 30 | if [ "$crc" != 0 ] || [ -s "$tmp/err" ]; then |
| 31 | cat "$tmp/err" >&2 |
| 32 | echo "EXERCISE FAIL $name compile rc=$crc" |
| 33 | exit 1 |
| 34 | fi |
| 35 | chmod +x "$tmp/prog" |
| 36 | timeout 60 "$tmp/prog"; rc=$? |
| 37 | if [ "$rc" = 0 ]; then echo "EXERCISE PASS $name"; exit 0; fi |
| 38 | if [ "$rc" -gt 100 ]; then echo "EXERCISE FAIL $name trap $((rc - 100)) (exit $rc)"; else echo "EXERCISE FAIL $name failed=$rc"; fi |
| 39 | exit 1 |