请选择行业
请选择职位
请选择省份
请选择城市

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

时间: 2023-05-05 15:22   作者: 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

打印本页 | 加入收藏

上一篇: 2023年,软件测试还值得入行吗?    

关闭  
主要城市: 北京 上海 杭州 广州 南京 武汉 长沙
全部城市: