[backend]解决了测试程序可能的挂起问题,引入了用于单个用例测试的新脚本

This commit is contained in:
Lixuanwang
2025-07-21 17:49:06 +08:00
parent bbfbf96b5e
commit 3baccbc03a
3 changed files with 301 additions and 115 deletions

View File

@ -1,16 +1,8 @@
#!/bin/bash
# run_vm_tests.sh - 用于在 RISC-V 虚拟机内部汇编、链接和测试 SysY 程序的脚本
# runit-riscv64.sh - 用于在 RISC-V 虚拟机内部汇编、链接和测试 SysY 程序的脚本
# 此脚本应位于您的项目根目录 (例如 /home/ubuntu/debug)
# 假设当前运行环境已经是 RISC-V 64 位架构,可以直接执行编译后的程序。
# 脚本的目录结构应该为:
# .
# ├── runit.sh
# ├── lib
# │ └── libsysy_riscv.a
# └── testdata
# ├── functional
# └── performance
# 定义相对于脚本位置的目录
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
@ -22,30 +14,23 @@ TESTDATA_DIR="${SCRIPT_DIR}/testdata"
GCC_NATIVE="gcc" # VM 内部的 gcc
# --- 新增功能: 初始化变量 ---
TIMEOUT_SECONDS=5 # 默认运行时超时时间为 5 秒
COMPILE_TIMEOUT_SECONDS=10 # 默认编译超时时间为 10 秒
GCC_TIMEOUT=10 # 默认 gcc 编译超时 (秒)
EXEC_TIMEOUT=5 # 默认运行时超时 (秒)
TOTAL_CASES=0
PASSED_CASES=0
FAILED_CASES_LIST="" # 用于存储未通过的测例列表
# 显示帮助信息的函数
show_help() {
echo "用法: $0 [选项]"
echo "此脚本用于在 RISC-V 虚拟机内部,对之前生成的 .s 汇编文件进行汇编、链接和测试。"
echo "假设当前运行环境已经是 RISC-V 64 位架构,可以直接执行编译后的程序。"
echo "测试会按文件名升序进行。"
echo ""
echo "选项:"
echo " -c, --clean 清理 'tmp' 目录下的所有生成文件。"
echo " -t, --timeout N 设置每个测试用例的运行时超时N 秒 (默认: 5)。"
echo " -ct, --compile-timeout M 设置 gcc 编译的超时时间M 秒 (默认: 10)。"
echo " -ct M 设置 gcc 编译的超时时间M 秒 (默认: 10)。"
echo " -t N 设置每个测试用例的运行时超时N 秒 (默认: 5)。"
echo " -h, --help 显示此帮助信息并退出。"
echo ""
echo "执行步骤:"
echo "1. 遍历 'tmp/' 目录下的所有 .s 汇编文件。"
echo "2. 在指定的超时时间内使用 VM 内部的 gcc 将 .s 文件汇编并链接为可执行文件。"
echo "3. 在指定的超时时间内运行编译后的可执行文件。"
echo "4. 根据对应的 .out 文件内容进行返回值和/或标准输出的比较。"
echo "5. 输出比较时会忽略行尾多余的换行符。"
echo "6. 所有测试结束后,报告总通过率。"
}
# 清理临时文件的函数
@ -69,23 +54,11 @@ while [[ "$#" -gt 0 ]]; do
clean_tmp
exit 0
;;
-t|--timeout)
if [[ -n "$2" && "$2" =~ ^[0-9]+$ ]]; then
TIMEOUT_SECONDS="$2"
shift # 移过参数值
else
echo "错误: --timeout 需要一个正整数参数。" >&2
exit 1
fi
-t)
if [[ -n "$2" && "$2" =~ ^[0-9]+$ ]]; then EXEC_TIMEOUT="$2"; shift; else echo "错误: -t 需要一个正整数参数。" >&2; exit 1; fi
;;
-ct|--compile-timeout)
if [[ -n "$2" && "$2" =~ ^[0-9]+$ ]]; then
COMPILE_TIMEOUT_SECONDS="$2"
shift # 移过参数值
else
echo "错误: --compile-timeout 需要一个正整数参数。" >&2
exit 1
fi
-ct)
if [[ -n "$2" && "$2" =~ ^[0-9]+$ ]]; then GCC_TIMEOUT="$2"; shift; else echo "错误: -ct 需要一个正整数参数。" >&2; exit 1; fi
;;
-h|--help)
show_help
@ -101,32 +74,26 @@ while [[ "$#" -gt 0 ]]; do
done
echo "SysY VM 内部测试运行器启动..."
echo "编译超时设置为: ${COMPILE_TIMEOUT_SECONDS}"
echo "运行时超时设置为: ${TIMEOUT_SECONDS}"
echo "GCC 编译超时设置为: ${GCC_TIMEOUT}"
echo "运行时超时设置为: ${EXEC_TIMEOUT}"
echo "汇编文件目录: ${TMP_DIR}"
echo "库文件目录: ${LIB_DIR}"
echo "测试数据目录: ${TESTDATA_DIR}"
echo ""
# 查找 tmp 目录下的所有 .s 汇编文件
s_files=$(find "${TMP_DIR}" -maxdepth 1 -name "*.s")
# 查找 tmp 目录下的所有 .s 汇编文件并排序
s_files=$(find "${TMP_DIR}" -maxdepth 1 -name "*.s" | sort -V)
TOTAL_CASES=$(echo "$s_files" | wc -w)
# 遍历找到的每个 .s 文件
echo "$s_files" | while read s_file; do
# --- 新增功能: 初始化用例通过状态 ---
# 使用 here-string (<<<) 避免子 shell 问题
while IFS= read -r s_file; do
is_passed=1 # 1 表示通过, 0 表示失败
# 从 .s 文件名中提取原始的测试用例名称部分
base_name_from_s_file=$(basename "$s_file" .s)
original_test_name_underscored=$(echo "$base_name_from_s_file" | sed 's/_sysyc_riscv64$//')
# 将 `original_test_name_underscored` 分割成类别和文件名
category=$(echo "$original_test_name_underscored" | cut -d'_' -f1)
test_file_base=$(echo "$original_test_name_underscored" | cut -d'_' -f2-)
original_relative_path="${category}/${test_file_base}"
# 定义可执行文件、输入文件、参考输出文件和实际输出文件的路径
executable_file="${TMP_DIR}/${base_name_from_s_file}"
input_file="${TESTDATA_DIR}/${original_relative_path}.in"
output_reference_file="${TESTDATA_DIR}/${original_relative_path}.out"
@ -136,42 +103,43 @@ echo "$s_files" | while read s_file; do
echo " 对应的测试用例路径: ${original_relative_path}"
# 步骤 1: 使用 VM 内部的 gcc 编译 .s 到可执行文件
echo " 使用 gcc 汇编并链接 (超时 ${COMPILE_TIMEOUT_SECONDS}s)..."
# --- 修改点: 为 gcc 增加 timeout ---
timeout ${COMPILE_TIMEOUT_SECONDS} "${GCC_NATIVE}" "${s_file}" -o "${executable_file}" -L"${LIB_DIR}" -lsysy_riscv -static -g
echo " 使用 gcc 汇编并链接 (超时 ${GCC_TIMEOUT}s)..."
timeout -s KILL ${GCC_TIMEOUT} "${GCC_NATIVE}" "${s_file}" -o "${executable_file}" -L"${LIB_DIR}" -lsysy_riscv -static -g
GCC_STATUS=$?
if [ $GCC_STATUS -eq 124 ]; then
echo -e "\e[31m错误: GCC 编译/链接 ${s_file} 超时 (超过 ${COMPILE_TIMEOUT_SECONDS} 秒)\e[0m"
echo -e "\e[31m错误: GCC 编译/链接 ${s_file} 超时\e[0m"
is_passed=0
elif [ $GCC_STATUS -ne 0 ]; then
echo -e "\e[31m错误: GCC 汇编/链接 ${s_file} 失败,退出码: ${GCC_STATUS}\e[0m"
is_passed=0
else
fi
# 步骤 2: 只有当编译成功时才执行
if [ "$is_passed" -eq 1 ]; then
echo " 生成的可执行文件: ${executable_file}"
echo " 正在执行 (超时 ${TIMEOUT_SECONDS}s): \"${executable_file}\""
echo " 正在执行 (超时 ${EXEC_TIMEOUT}s)..."
# 步骤 2: 执行编译后的文件并比较/报告结果
if [ -f "${output_reference_file}" ]; then
LAST_LINE_TRIMMED=$(tail -n 1 "${output_reference_file}" | tr -d '[:space:]')
if [[ "$LAST_LINE_TRIMMED" =~ ^[-+]?[0-9]+$ ]]; then
EXPECTED_RETURN_CODE="$LAST_LINE_TRIMMED"
EXPECTED_STDOUT_FILE="${TMP_DIR}/${base_name_from_s_file}.expected_stdout"
head -n -1 "${output_reference_file}" > "${EXPECTED_STDOUT_FILE}"
echo " 检测到 .out 文件同时包含标准输出和期望的返回码。"
echo " 期望返回码: ${EXPECTED_RETURN_CODE}"
exec_cmd="\"${executable_file}\""
if [ -f "${input_file}" ]; then
exec_cmd+=" < \"${input_file}\""
fi
exec_cmd+=" > \"${output_actual_file}\""
eval "timeout -s KILL ${EXEC_TIMEOUT} ${exec_cmd}"
ACTUAL_RETURN_CODE=$?
if [ -f "${input_file}" ]; then
timeout ${TIMEOUT_SECONDS} "${executable_file}" < "${input_file}" > "${output_actual_file}"
else
timeout ${TIMEOUT_SECONDS} "${executable_file}" > "${output_actual_file}"
fi
ACTUAL_RETURN_CODE=$?
if [ "$ACTUAL_RETURN_CODE" -eq 124 ]; then
echo -e "\e[31m 执行超时: ${original_relative_path}.sy 运行超过 ${EXEC_TIMEOUT} 秒\e[0m"
is_passed=0
else
if [ -f "${output_reference_file}" ]; then
LAST_LINE_TRIMMED=$(tail -n 1 "${output_reference_file}" | tr -d '[:space:]')
if [[ "$LAST_LINE_TRIMMED" =~ ^[-+]?[0-9]+$ ]]; then
EXPECTED_RETURN_CODE="$LAST_LINE_TRIMMED"
EXPECTED_STDOUT_FILE="${TMP_DIR}/${base_name_from_s_file}.expected_stdout"
head -n -1 "${output_reference_file}" > "${EXPECTED_STDOUT_FILE}"
if [ "$ACTUAL_RETURN_CODE" -eq 124 ]; then
echo -e "\e[31m 执行超时: ${original_relative_path}.sy 运行超过 ${TIMEOUT_SECONDS} 秒\e[0m"
is_passed=0
else
if [ "$ACTUAL_RETURN_CODE" -eq "$EXPECTED_RETURN_CODE" ]; then
echo -e "\e[32m 返回码测试成功: (${ACTUAL_RETURN_CODE}) 与期望值 (${EXPECTED_RETURN_CODE}) 匹配\e[0m"
else
@ -183,61 +151,53 @@ echo "$s_files" | while read s_file; do
echo -e "\e[32m 标准输出测试成功\e[0m"
else
echo -e "\e[31m 标准输出测试失败\e[0m"
echo " 差异:"
diff "${output_actual_file}" "${EXPECTED_STDOUT_FILE}"
is_passed=0
echo -e " \e[36m---------- 期望输出 ----------\e[0m"
cat "${EXPECTED_STDOUT_FILE}"
echo -e " \e[36m---------- 实际输出 ----------\e[0m"
cat "${output_actual_file}"
echo -e " \e[36m------------------------------\e[0m"
fi
fi
else
echo " 检测到 .out 文件为纯标准输出参考。"
if [ -f "${input_file}" ]; then
timeout ${TIMEOUT_SECONDS} "${executable_file}" < "${input_file}" > "${output_actual_file}"
else
timeout ${TIMEOUT_SECONDS} "${executable_file}" > "${output_actual_file}"
fi
EXEC_STATUS=$?
if [ $EXEC_STATUS -eq 124 ]; then
echo -e "\e[31m 执行超时: ${original_relative_path}.sy 运行超过 ${TIMEOUT_SECONDS} 秒\e[0m"
is_passed=0
else
if [ $EXEC_STATUS -ne 0 ]; then
echo -e "\e[33m警告: 程序以非零状态 ${EXEC_STATUS} 退出 (纯输出比较模式)。\e[0m"
if [ $ACTUAL_RETURN_CODE -ne 0 ]; then
echo -e "\e[33m警告: 程序以非零状态 ${ACTUAL_RETURN_CODE} 退出 (纯输出比较模式)。\e[0m"
fi
if diff -q <(sed ':a;N;$!ba;s/\n*$//' "${output_actual_file}") <(sed ':a;N;$!ba;s/\n*$//' "${output_reference_file}") >/dev/null 2>&1; then
echo -e "\e[32m 成功: 输出与参考输出匹配\e[0m"
else
echo -e "\e[31m 失败: 输出不匹配\e[0m"
echo " 差异:"
diff "${output_actual_file}" "${output_reference_file}"
is_passed=0
echo -e " \e[36m---------- 期望输出 ----------\e[0m"
cat "${output_reference_file}"
echo -e " \e[36m---------- 实际输出 ----------\e[0m"
cat "${output_actual_file}"
echo -e " \e[36m------------------------------\e[0m"
fi
fi
fi
else
echo " 未找到 .out 文件。正在运行并报告返回码。"
timeout ${TIMEOUT_SECONDS} "${executable_file}"
EXEC_STATUS=$?
if [ $EXEC_STATUS -eq 124 ]; then
echo -e "\e[31m 执行超时: ${original_relative_path}.sy 运行超过 ${TIMEOUT_SECONDS} 秒\e[0m"
is_passed=0
else
echo " ${original_relative_path}.sy 的返回码: ${EXEC_STATUS}"
echo " 无参考输出文件。程序返回码: ${ACTUAL_RETURN_CODE}"
fi
fi
fi
# --- 新增功能: 更新通过用例计数 ---
if [ "$is_passed" -eq 1 ]; then
((PASSED_CASES++))
else
FAILED_CASES_LIST+="${original_relative_path}.sy\n"
fi
echo "" # 为测试用例之间添加一个空行
done
echo ""
done <<< "$s_files"
# --- 新增功能: 打印最终总结 ---
echo "========================================"
echo "测试完成"
echo "测试通过率: [${PASSED_CASES}/${TOTAL_CASES}]"
if [ -n "$FAILED_CASES_LIST" ]; then
echo ""
echo -e "\e[31m未通过的测例:\e[0m"
echo -e "${FAILED_CASES_LIST}"
fi
echo "========================================"
if [ "$PASSED_CASES" -eq "$TOTAL_CASES" ]; then

225
test_script/runit-single.sh Normal file
View File

@ -0,0 +1,225 @@
#!/bin/bash
# runit-single.sh - 用于编译和测试单个或少量 SysY 程序的脚本
# 模仿 runit.sh 的功能,但以具体文件路径作为输入。
# --- 配置区 ---
# 请根据你的环境修改这些路径
# 假设此脚本位于你的项目根目录或一个脚本目录中
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
# 默认寻找项目根目录下的 build 和 lib
BUILD_BIN_DIR="${SCRIPT_DIR}/../build/bin"
LIB_DIR="${SCRIPT_DIR}/../lib"
# 临时文件会存储在脚本所在目录的 tmp 子目录中
TMP_DIR="${SCRIPT_DIR}/tmp"
# 定义编译器和模拟器
SYSYC="${BUILD_BIN_DIR}/sysyc"
GCC_RISCV64="riscv64-linux-gnu-gcc"
QEMU_RISCV64="qemu-riscv64"
# --- 初始化变量 ---
EXECUTE_MODE=false
SYSYC_TIMEOUT=10 # sysyc 编译超时 (秒)
GCC_TIMEOUT=10 # gcc 编译超时 (秒)
EXEC_TIMEOUT=5 # qemu 自动化执行超时 (秒)
SY_FILES=() # 存储用户提供的 .sy 文件列表
PASSED_CASES=0
FAILED_CASES_LIST=""
# --- 函数定义 ---
show_help() {
echo "用法: $0 [文件1.sy] [文件2.sy] ... [选项]"
echo "编译并测试指定的 .sy 文件。"
echo ""
echo "如果找到对应的 .in/.out 文件,则进行自动化测试。否则,进入交互模式。"
echo ""
echo "选项:"
echo " -e, --executable 编译为可执行文件并运行测试 (必须)。"
echo " -sct N 设置 sysyc 编译超时为 N 秒 (默认: 10)。"
echo " -gct N 设置 gcc 交叉编译超时为 N 秒 (默认: 10)。"
echo " -et N 设置 qemu 自动化执行超时为 N 秒 (默认: 5)。"
echo " -h, --help 显示此帮助信息并退出。"
}
# --- 参数解析 ---
# 从参数中分离出 .sy 文件和选项
for arg in "$@"; do
case "$arg" in
-e|--executable)
EXECUTE_MODE=true
;;
-sct|-gct|-et)
# 选项和其值将在下一个循环中处理
;;
-h|--help)
show_help
exit 0
;;
-*)
# 检查是否是带值的选项
if ! [[ ${args_processed+x} ]]; then
args_processed=true # 标记已处理过参数
# 重新处理所有参数
while [[ "$#" -gt 0 ]]; do
case "$1" in
-sct) if [[ -n "$2" && "$2" =~ ^[0-9]+$ ]]; then SYSYC_TIMEOUT="$2"; shift; else echo "错误: -sct 需要一个正整数参数。" >&2; exit 1; fi ;;
-gct) if [[ -n "$2" && "$2" =~ ^[0-9]+$ ]]; then GCC_TIMEOUT="$2"; shift; else echo "错误: -gct 需要一个正整数参数。" >&2; exit 1; fi ;;
-et) if [[ -n "$2" && "$2" =~ ^[0-9]+$ ]]; then EXEC_TIMEOUT="$2"; shift; else echo "错误: -et 需要一个正整数参数。" >&2; exit 1; fi ;;
*.sy) SY_FILES+=("$1") ;;
-e|--executable) ;; # 已在外部处理
*) if ! [[ "$1" =~ ^[0-9]+$ ]]; then echo "未知选项或无效文件: $1"; show_help; exit 1; fi ;;
esac
shift
done
fi
;;
*.sy)
if [[ -f "$arg" ]]; then
SY_FILES+=("$arg")
else
echo "警告: 文件不存在,已忽略: $arg"
fi
;;
esac
done
# --- 主逻辑开始 ---
if ! ${EXECUTE_MODE}; then
echo "错误: 请提供 -e 或 --executable 选项来运行测试。"
show_help
exit 1
fi
if [ ${#SY_FILES[@]} -eq 0 ]; then
echo "错误: 未提供任何 .sy 文件作为输入。"
show_help
exit 1
fi
mkdir -p "${TMP_DIR}"
TOTAL_CASES=${#SY_FILES[@]}
echo "SysY 单例测试运行器启动..."
echo "超时设置: sysyc=${SYSYC_TIMEOUT}s, gcc=${GCC_TIMEOUT}s, qemu=${EXEC_TIMEOUT}s"
echo ""
for sy_file in "${SY_FILES[@]}"; do
is_passed=1
base_name=$(basename "${sy_file}" .sy)
source_dir=$(dirname "${sy_file}")
assembly_file="${TMP_DIR}/${base_name}.s"
executable_file="${TMP_DIR}/${base_name}"
input_file="${source_dir}/${base_name}.in"
output_reference_file="${source_dir}/${base_name}.out"
output_actual_file="${TMP_DIR}/${base_name}.actual_out"
echo "======================================================================"
echo "正在处理: ${sy_file}"
# 步骤 1: sysyc 编译
echo " 使用 sysyc 编译 (超时 ${SYSYC_TIMEOUT}s)..."
timeout -s KILL ${SYSYC_TIMEOUT} "${SYSYC}" -S "${sy_file}" -o "${assembly_file}"
if [ $? -ne 0 ]; then
echo -e "\e[31m错误: SysY 编译失败或超时。\e[0m"
is_passed=0
fi
# 步骤 2: GCC 编译
if [ "$is_passed" -eq 1 ]; then
echo " 使用 gcc 编译 (超时 ${GCC_TIMEOUT}s)..."
timeout -s KILL ${GCC_TIMEOUT} "${GCC_RISCV64}" "${assembly_file}" -o "${executable_file}" -L"${LIB_DIR}" -lsysy_riscv -static
if [ $? -ne 0 ]; then
echo -e "\e[31m错误: GCC 编译失败或超时。\e[0m"
is_passed=0
fi
fi
# 步骤 3: 执行与测试
if [ "$is_passed" -eq 1 ]; then
# 检查是自动化测试还是交互模式
if [ -f "${input_file}" ] || [ -f "${output_reference_file}" ]; then
# --- 自动化测试模式 ---
echo " 检测到 .in/.out 文件,进入自动化测试模式..."
echo " 正在执行 (超时 ${EXEC_TIMEOUT}s)..."
exec_cmd="\"${executable_file}\""
[ -f "${input_file}" ] && exec_cmd+=" < \"${input_file}\""
exec_cmd+=" > \"${output_actual_file}\""
eval "timeout -s KILL ${EXEC_TIMEOUT} ${exec_cmd}"
ACTUAL_RETURN_CODE=$?
if [ "$ACTUAL_RETURN_CODE" -eq 124 ]; then
echo -e "\e[31m 执行超时。\e[0m"
is_passed=0
else
if [ -f "${output_reference_file}" ]; then
# 此处逻辑与 runit.sh 相同
LAST_LINE_TRIMMED=$(tail -n 1 "${output_reference_file}" | tr -d '[:space:]')
if [[ "$LAST_LINE_TRIMMED" =~ ^[-+]?[0-9]+$ ]]; then
EXPECTED_RETURN_CODE="$LAST_LINE_TRIMMED"
EXPECTED_STDOUT_FILE="${TMP_DIR}/${base_name}.expected_stdout"
head -n -1 "${output_reference_file}" > "${EXPECTED_STDOUT_FILE}"
if [ "$ACTUAL_RETURN_CODE" -ne "$EXPECTED_RETURN_CODE" ]; then echo -e "\e[31m 返回码测试失败: 期望 ${EXPECTED_RETURN_CODE}, 实际 ${ACTUAL_RETURN_CODE}\e[0m"; is_passed=0; fi
if ! diff -q <(sed ':a;N;$!ba;s/\n*$//' "${output_actual_file}") <(sed ':a;N;$!ba;s/\n*$//' "${EXPECTED_STDOUT_FILE}") >/dev/null 2>&1; then
echo -e "\e[31m 标准输出测试失败。\e[0m"
is_passed=0
echo -e " \e[36m--- 期望输出 ---\e[0m"; cat "${EXPECTED_STDOUT_FILE}"; echo -e " \e[36m--- 实际输出 ---\e[0m"; cat "${output_actual_file}"; echo -e " \e[36m----------------\e[0m"
fi
else
if ! diff -q <(sed ':a;N;$!ba;s/\n*$//' "${output_actual_file}") <(sed ':a;N;$!ba;s/\n*$//' "${output_reference_file}") >/dev/null 2>&1; then
echo -e "\e[31m 标准输出测试失败。\e[0m"
is_passed=0
echo -e " \e[36m--- 期望输出 ---\e[0m"; cat "${output_reference_file}"; echo -e " \e[36m--- 实际输出 ---\e[0m"; cat "${output_actual_file}"; echo -e " \e[36m----------------\e[0m"
fi
fi
else
echo " 无参考输出文件。程序返回码: ${ACTUAL_RETURN_CODE}"
fi
fi
else
# --- 交互模式 ---
echo -e "\e[33m"
echo " **********************************************************"
echo " ** 未找到 .in 或 .out 文件,进入交互模式。 **"
echo " ** 程序即将运行,你可以直接在终端中输入。 **"
echo " ** 按下 Ctrl+D (EOF) 或以其他方式结束程序以继续。 **"
echo " **********************************************************"
echo -e "\e[0m"
"${QEMU_RISCV64}" "${executable_file}"
INTERACTIVE_RET_CODE=$?
echo -e "\e[33m\n 交互模式执行完毕,程序返回码: ${INTERACTIVE_RET_CODE}\e[0m"
# 交互模式无法自动判断对错,默认算通过,但会提示
echo " 注意: 交互模式的结果未经验证。"
fi
fi
if [ "$is_passed" -eq 1 ]; then
echo -e "\e[32m状态: 通过\e[0m"
((PASSED_CASES++))
else
echo -e "\e[31m状态: 失败\e[0m"
FAILED_CASES_LIST+="${sy_file}\n"
fi
done
# --- 打印最终总结 ---
echo "======================================================================"
echo "所有测试完成"
echo "测试通过率: [${PASSED_CASES}/${TOTAL_CASES}]"
if [ -n "$FAILED_CASES_LIST" ]; then
echo ""
echo -e "\e[31m未通过的测例:\e[0m"
echo -e "${FAILED_CASES_LIST}"
fi
echo "======================================================================"
if [ "$PASSED_CASES" -eq "$TOTAL_CASES" ]; then
exit 0
else
exit 1
fi

View File

@ -93,7 +93,7 @@ echo ""
sy_files=$(find "${TESTDATA_DIR}" -name "*.sy" | sort -V)
TOTAL_CASES=$(echo "$sy_files" | wc -w)
# --- 本次修复: 使用 here-string (<<<) 代替管道 (|) 来避免子 shell 问题 ---
# --- 修复: 使用 here-string (<<<) 代替管道 (|) 来避免子 shell 问题 ---
# 这样可以确保循环内的 PASSED_CASES 变量修改在循环结束后依然有效
while IFS= read -r sy_file; do
is_passed=1 # 1 表示通过, 0 表示失败
@ -111,7 +111,8 @@ while IFS= read -r sy_file; do
# 步骤 1: 使用 sysyc 编译 .sy 到 .s
echo " 使用 sysyc 编译 (超时 ${SYSYC_TIMEOUT}s)..."
timeout ${SYSYC_TIMEOUT} "${SYSYC}" -S "${sy_file}" -o "${assembly_file}"
# --- 本次修改点: 增加 -s KILL 确保超时后进程被终止 ---
timeout -s KILL ${SYSYC_TIMEOUT} "${SYSYC}" -S "${sy_file}" -o "${assembly_file}"
SYSYC_STATUS=$?
if [ $SYSYC_STATUS -eq 124 ]; then
echo -e "\e[31m错误: SysY 编译 ${sy_file} 超时\e[0m"
@ -125,7 +126,8 @@ while IFS= read -r sy_file; do
if ${EXECUTE_MODE} && [ "$is_passed" -eq 1 ]; then
# 步骤 2: 使用 riscv64-linux-gnu-gcc 编译 .s 到可执行文件
echo " 使用 gcc 编译 (超时 ${GCC_TIMEOUT}s)..."
timeout ${GCC_TIMEOUT} "${GCC_RISCV64}" "${assembly_file}" -o "${executable_file}" -L"${LIB_DIR}" -lsysy_riscv -static
# --- 本次修改点: 增加 -s KILL 确保超时后进程被终止 ---
timeout -s KILL ${GCC_TIMEOUT} "${GCC_RISCV64}" "${assembly_file}" -o "${executable_file}" -L"${LIB_DIR}" -lsysy_riscv -static
GCC_STATUS=$?
if [ $GCC_STATUS -eq 124 ]; then
echo -e "\e[31m错误: GCC 编译 ${assembly_file} 超时\e[0m"
@ -140,7 +142,6 @@ while IFS= read -r sy_file; do
if [ "$is_passed" -eq 1 ]; then
((PASSED_CASES++))
else
# --- 本次修改点 ---
FAILED_CASES_LIST+="${relative_path_no_ext}.sy\n"
fi
echo ""
@ -159,7 +160,8 @@ while IFS= read -r sy_file; do
exec_cmd+=" > \"${output_actual_file}\""
# 执行并捕获返回码
eval "timeout ${EXEC_TIMEOUT} ${exec_cmd}"
# --- 本次修改点: 增加 -s KILL 确保超时后进程被终止 ---
eval "timeout -s KILL ${EXEC_TIMEOUT} ${exec_cmd}"
ACTUAL_RETURN_CODE=$?
if [ "$ACTUAL_RETURN_CODE" -eq 124 ]; then
@ -219,7 +221,6 @@ while IFS= read -r sy_file; do
fi
# 更新通过用例计数
# --- 本次修改点 ---
if [ "$is_passed" -eq 1 ]; then
((PASSED_CASES++))
else
@ -234,7 +235,7 @@ echo "========================================"
echo "测试完成"
echo "测试通过率: [${PASSED_CASES}/${TOTAL_CASES}]"
# --- 本次修改点: 打印未通过的测例列表 ---
# --- 打印未通过的测例列表 ---
if [ -n "$FAILED_CASES_LIST" ]; then
echo ""
echo -e "\e[31m未通过的测例:\e[0m"