#!/bin/sh # Regression for M2comp step 2 (Coco/R + SymTab semantic analysis). # Usage: ./run_tests.sh (from the m2comp directory) pass=0; fail=0 expect_ok() { if ./M2comp "$1" 2>&1 | grep -q "Parsed correctly"; then pass=$((pass+1)); echo "PASS(ok): $1" else fail=$((fail+1)); echo "FAIL(ok, rejected): $1" fi } expect_fail() { if ./M2comp "$1" 2>&1 | grep -q "Incorrect source"; then pass=$((pass+1)); echo "PASS(fail): $1" else fail=$((fail+1)); echo "FAIL(fail, accepted): $1" fi } expect_dump() { if ./M2comp "$1" 2>&1 | grep -q "$2"; then pass=$((pass+1)); echo "PASS(ast): $1 [$2]" else fail=$((fail+1)); echo "FAIL(ast): $1 [$2]" fi } MCINT="${MCINT:-../mc64/mcint}" MCTIMEOUT="${MCTIMEOUT:-10}" expect_run() { # $1 = test file, $2 = expected printed ExitCode value name="$1" want="$2" mod=$(head -n 1 "tests/$name" | cut -d' ' -f2 | cut -d';' -f1) mcd="$mod.MC4" rm -f "$mcd" if ./M2comp "tests/$name" 2>&1 | grep -q "Incorrect source"; then fail=$((fail+1)); echo "FAIL(run): $name rejected"; return fi if [ ! -f "$mcd" ]; then fail=$((fail+1)); echo "FAIL(run): $name no $mcd"; return fi timeout "$MCTIMEOUT" "$MCINT" "$mcd" > /tmp/m2comp_out.txt 2>&1; rc=$? if [ "$rc" -ne 0 ]; then fail=$((fail+1)); echo "FAIL(run): $name vm rc=$rc (diverges?)"; return fi got=$(head -n 1 /tmp/m2comp_out.txt | tr -d '\r') if [ "$got" = "$want" ]; then pass=$((pass+1)); echo "PASS(run): $name -> $got" else fail=$((fail+1)); echo "FAIL(run): $name printed [$got], want [$want]" fi } expect_run_none() { # $1 = test file — must compile and run silent (terminating) name="$1" mod=$(head -n 1 "tests/$name" | cut -d' ' -f2 | cut -d';' -f1) mcd="$mod.MC4" rm -f "$mcd" if ./M2comp "tests/$name" 2>&1 | grep -q "Incorrect source"; then fail=$((fail+1)); echo "FAIL(silent): $name rejected"; return fi if [ ! -f "$mcd" ]; then fail=$((fail+1)); echo "FAIL(silent): $name no $mcd"; return fi timeout "$MCTIMEOUT" "$MCINT" "$mcd" > /tmp/m2comp_out.txt 2>&1; rc=$? if [ "$rc" -ne 0 ]; then fail=$((fail+1)); echo "FAIL(silent): $name vm rc=$rc (diverges?)"; return fi got=$(head -n 1 /tmp/m2comp_out.txt) if [ -z "$got" ]; then pass=$((pass+1)); echo "PASS(silent): $name runs silent" else fail=$((fail+1)); echo "FAIL(silent): $name output [$got]" fi } expect_ok tests/ok_minimal.mod expect_ok tests/ok_proc.mod expect_ok tests/showcase.mod expect_fail tests/bad_mismatch.mod expect_fail tests/t_dup.mod expect_fail tests/t_undecl.mod expect_fail tests/t_assign.mod expect_fail tests/t_arith.mod expect_fail tests/t_bool.mod expect_fail tests/t_rel.mod expect_fail tests/t_cond.mod expect_fail tests/t_field.mod expect_fail tests/t_index.mod expect_fail tests/t_ptr.mod expect_fail tests/t_for.mod expect_fail tests/t_call.mod expect_fail tests/t_call2.mod expect_fail tests/t_return.mod expect_fail tests/t_exit.mod expect_dump tests/showcase.mod "Module Showcase" expect_dump tests/showcase.mod "Proc Work" expect_dump tests/showcase.mod "CallExpr" expect_dump tests/showcase.mod "Field q" expect_dump tests/showcase.mod "Int 255" expect_dump tests/ok_proc.mod "Proc P" expect_run_none ok_minimal.mod expect_run_none ok_proc.mod expect_run r_arith.mod 8 expect_run r_flow.mod 69 expect_run r_bool.mod 11 expect_run r_real.mod 1111 expect_run r_proc.mod 162 expect_run r_varpar.mod 48 expect_run r_nested.mod 213 expect_run r_array.mod 39 expect_run r_record.mod 15 expect_run r_set.mod 12 expect_run r_string.mod 111 expect_run r_ptr.mod 111 expect_run r_module.mod 83 expect_run r_const.mod 1102 echo "--- $pass passed, $fail failed ---" [ "$fail" -eq 0 ]