#!/usr/bin/env bash set -euo pipefail # Lab2 quick/full verification helper. # Usage: # bash scripts/test_lab2.sh # Optional env vars: # COMPILER=./build/bin/compiler # CASE_DIR=test/test_case # OUT_DIR=test/test_result/lab2_ir # LOG_FILE=test/test_result/lab2_test.log COMPILER="${COMPILER:-./build/bin/compiler}" CASE_DIR="${CASE_DIR:-test/test_case}" OUT_DIR="${OUT_DIR:-test/test_result/lab2_ir}" LOG_FILE="${LOG_FILE:-test/test_result/lab2_test.log}" VERIFY_SCRIPT="./scripts/verify_ir.sh" if [[ ! -x "$COMPILER" ]]; then echo "compiler not found or not executable: $COMPILER" >&2 echo "build first:" >&2 echo " cmake -S . -B build -DCMAKE_BUILD_TYPE=Release" >&2 echo " cmake --build build -j \"\$(nproc)\"" >&2 exit 1 fi if [[ ! -x "$VERIFY_SCRIPT" ]]; then echo "verify script not found or not executable: $VERIFY_SCRIPT" >&2 exit 1 fi if [[ ! -d "$CASE_DIR" ]]; then echo "case dir not found: $CASE_DIR" >&2 exit 1 fi mkdir -p "$OUT_DIR" # Preflight: ensure compiler supports IR emission (not parse-only build). probe_input="$CASE_DIR/simple_add.sy" probe_err="$OUT_DIR/.lab2_probe.err" if [[ -f "$probe_input" ]]; then set +e "$COMPILER" --emit-ir "$probe_input" > /dev/null 2> "$probe_err" probe_rc=$? set -e if [[ $probe_rc -ne 0 ]] && grep -Eiq "parse-only|IR/汇编输出已禁用" "$probe_err"; then echo "detected parse-only compiler build, cannot run Lab2 IR tests." >&2 echo "rebuild with IR enabled:" >&2 echo " cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DCOMPILER_PARSE_ONLY=OFF" >&2 echo " cmake --build build -j \"\$(nproc)\"" >&2 rm -f "$probe_err" exit 2 fi rm -f "$probe_err" fi mkdir -p "$(dirname "$LOG_FILE")" : > "$LOG_FILE" echo "[Lab2] start test" | tee -a "$LOG_FILE" echo "compiler : $COMPILER" | tee -a "$LOG_FILE" echo "cases : $CASE_DIR" | tee -a "$LOG_FILE" echo "out dir : $OUT_DIR" | tee -a "$LOG_FILE" echo "[Step 1] single sample check: simple_add.sy" | tee -a "$LOG_FILE" sample_input="$(find "$CASE_DIR" -type f -name "simple_add.sy" -print -quit)" if [[ -z "$sample_input" ]]; then sample_input="$(find "$CASE_DIR" -type f -name "*.sy" | sort | head -n 1)" fi if [[ -z "$sample_input" ]]; then echo "single sample: FAIL (no .sy case found under $CASE_DIR)" | tee -a "$LOG_FILE" echo "stop here. see log: $LOG_FILE" >&2 exit 1 fi if "$VERIFY_SCRIPT" "$sample_input" "$OUT_DIR" --run >> "$LOG_FILE" 2>&1; then echo "single sample: PASS" | tee -a "$LOG_FILE" else echo "single sample: FAIL" | tee -a "$LOG_FILE" echo "stop here. see log: $LOG_FILE" >&2 exit 1 fi echo "[Step 2] full Lab2 regression" | tee -a "$LOG_FILE" pass=0 fail=0 total=0 failed_list=() while IFS= read -r -d '' sy; do total=$((total + 1)) name="$(basename "$sy")" echo "[$total] $name" | tee -a "$LOG_FILE" if "$VERIFY_SCRIPT" "$sy" "$OUT_DIR" --run >> "$LOG_FILE" 2>&1; then pass=$((pass + 1)) echo " PASS" | tee -a "$LOG_FILE" else fail=$((fail + 1)) failed_list+=("$sy") echo " FAIL" | tee -a "$LOG_FILE" fi done < <(find "$CASE_DIR" -type f -name "*.sy" -print0 | sort -z) echo "" | tee -a "$LOG_FILE" echo "[Summary]" | tee -a "$LOG_FILE" echo "total: $total" | tee -a "$LOG_FILE" echo "pass : $pass" | tee -a "$LOG_FILE" echo "fail : $fail" | tee -a "$LOG_FILE" if [[ $fail -gt 0 ]]; then echo "failed cases:" | tee -a "$LOG_FILE" for f in "${failed_list[@]}"; do echo " - $f" | tee -a "$LOG_FILE" done echo "Lab2 target is not fully met yet." | tee -a "$LOG_FILE" echo "see details in $LOG_FILE" exit 1 fi echo "All Lab2 cases passed. Lab2 target regression is met." | tee -a "$LOG_FILE" echo "see details in $LOG_FILE"