笔试实战:本地环境测试&调节的小工具

发表于:2023-5-04 09:25

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:reget    来源:牛客网

  前几天在赛码网笔试,本地环境(WSL2)测试&调试,每次都需要输入测试样例,肉眼对比输出结果,感觉特别麻烦。
  笔试完后就写了个小脚本,在这里分享一下,本人 shell 脚本写的也不是太很熟练,如有问题还望轻喷。
  效果图:
  使用方法为:./ojrun.sh [要执行的程序] [测试样例],例如 ./ojrun.sh ./a.out test.txt。
  测试样例的文本文件如下,每个 case 由 INPUT OUPUT END 三部分组成,INPUT-OUTPUT 为输入数据,OUTPUT-END 为输出数据。
  现在还有许多情况没考虑:
  1、运行超时,目前只能手动解决
  2、输入/输出数据可能存在 INPUT OUPUT END 这三个标志字符。
# case 1
INPUT
1 3
2
1 1
3 1
OUTPUT
2
END

# case 2
INPUT
1 3
3
2 2
3 2
1 1
OUTPUT
3
END
#!/usr/bin/env bash
# this file is created by reget on 2023/04/27
# require: echo, test, grep, sed, mktemp, diff

input_flag="INPUT"
output_flag="OUTPUT"
end_flag="END"
# check the args num
if [ $# != "2" ]; then
    echo "how to use: $0 program test_file"
    echo "example: $0 ./a.out test.txt"
    exit 1
fi

program=$1
test_file=$2

if ! test -x "${program}";then
    echo -e "\e[1;31mError: program can not exec, program is ""${program}"""
    exit 1
fi

if ! test -r "${test_file}";then
    echo -e "\e[1;31mError: test file can not read, test file is ""${test_file}"""
    exit 1
fi

input_flag_lines=$(grep -c "^${input_flag}$" "${test_file}")
output_flag_lines=$(grep -c "^${output_flag}$" "${test_file}")
end_flag_lines=$(grep -c "^${end_flag}$" "${test_file}")

# check the test file
if [ "${input_flag_lines}" == "${output_flag_lines}" ] && [ "${input_flag_lines}" == "${end_flag_lines}" ]; then
    tmp_input_file=$(mktemp)
    tmp_output_file=$(mktemp)
    tmp_program_output_file=$(mktemp)
    line_id=1
    case_id=1
    while read -r line
    do
        if [[ "$line" == "INPUT" ]]; then
            input_start=${line_id}
            elif [[ "$line" == "OUTPUT" ]]; then
            input_end=${line_id}
            output_start=${line_id}
            elif [[ "$line" == "END" ]]; then
            output_end=${line_id}
            # run program
            $(sed -n "$((input_start+1)), $((input_end-1))p" "${test_file}" > "${tmp_input_file}")
            $(sed -n "$((output_start+1)), $((output_end-1))p" "${test_file}" > "${tmp_output_file}")
            $(${program} < "${tmp_input_file}" > "${tmp_program_output_file}")
            # check result
            if diff "${tmp_program_output_file}" "${tmp_output_file}" > /dev/null; then
                echo -e "\e[1;32mPass: pass case ${case_id}, ^v^"
                ((case_id++))
            else
                echo -e "\e[1;31mFail: fail to pass case ${case_id}, ToT"
                echo -e "\e[1;36mInput:"
                cat ${tmp_input_file}
                echo -e "\e[1;36mAssert Output:"
                cat ${tmp_output_file}
                echo -e "\e[1;31mYour Output:"
                cat ${tmp_program_output_file}
                mv "${tmp_program_output_file}" your_output.tmp
                mv "${tmp_output_file}" assert_output.tmp
                mv "${tmp_input_file}" input.tmp
                exit 1
            fi
        fi
        ((line_id++))
    done < "${test_file}"
    rm "${tmp_program_output_file}"
    rm "${tmp_output_file}"
    rm "${tmp_input_file}"
    echo -e "\e[1;32mPass: pass all test case!!!"
else
    echo -e "\e[1;31mError: illgal test file, file is ""${test_file}"""
    exit 1
fi
  本文内容不用于商业目的,如涉及知识产权问题,请权利人联系51Testing小编(021-64471599-8017),我们将立即处理
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号