interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / exercise.sh

39 lines · 2071 bytes Raw Blame History
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.
15set -u
16here=$(cd "$(dirname "$0")" && pwd)
17[ $# -ge 1 ] || { echo "usage: sh exercise.sh exercises/<name> [solution.om]" >&2; exit 2; }
18dir=${1%/}
19name=$(basename "$dir")
20sol=${2:-$dir/$name.om}
21omc=${OMC:-$here/build/omega-mv/seed/out/omc}
22pin=$(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; }
26tmp=$(mktemp -d) || exit 2
27trap 'rm -rf "$tmp"' EXIT
28cat "$sol" "$dir/test.om" > "$tmp/unit.om"
29"$omc" < "$tmp/unit.om" > "$tmp/prog" 2> "$tmp/err"; crc=$?
30if [ "$crc" != 0 ] || [ -s "$tmp/err" ]; then
31 cat "$tmp/err" >&2
32 echo "EXERCISE FAIL $name compile rc=$crc"
33 exit 1
34fi
35chmod +x "$tmp/prog"
36timeout 60 "$tmp/prog"; rc=$?
37if [ "$rc" = 0 ]; then echo "EXERCISE PASS $name"; exit 0; fi
38if [ "$rc" -gt 100 ]; then echo "EXERCISE FAIL $name trap $((rc - 100)) (exit $rc)"; else echo "EXERCISE FAIL $name failed=$rc"; fi
39exit 1