我试图在运行函数run_test_args
的脚本模板中编写测试用例,该函数接受三个命令:一个可选的命令行参数,一个由空格分隔的整数字符串(例如" 1 2 3 4 5")用作程序提示的用户输入,以及预期输出的字符串。由于程序是使用大量数字进行测试的,因此我尝试使用seq
代替以下形式的第二个参数:$(seq -s " " 100000)
生成1到100,000,用空格分隔并格式化作为一个字符串。
但是,当我运行测试脚本时,该函数始终识别seq
输出中的第一个元素,在上述情况下为1,作为用户输入,第二个元素作为预期输出
如何从seq
获取输出并将其格式化为所有一个字符串而不是字符串数组(考虑到函数如何接收参数,似乎在做什么)?
以下是run_test_args
函数和使用seq
命令的测试:
run_test_args() {
(( ++total ))
echo -n "Running test $total..."
expected=$3
local ismac=0
date --version >/dev/null 2>&1
if [ $? -ne 0 ]; then
ismac=1
fi
local start=0
if (( ismac )); then
start=$(python -c 'import time; print time.time()')
else
start=$(date +%s.%N)
fi
(cat << ENDOFTEXT
$2
ENDOFTEXT
) > input.txt
if timeout $MAXTIME "cat input.txt | $command $1 2>&1 | tr -d '\r' > tmp.txt"; then
echo "failure [timed out after $MAXTIME seconds]"
else
received=$(cat tmp.txt)
local end=$(date +%s.%N)
if (( ismac )); then
end=$(python -c 'import time; print time.time()')
else
end=$(date +%s.%N)
fi
local elapsed=$(echo "scale=3; $end - $start" | bc | awk '{printf "%.3f", $0}')
if [ "$expected" != "$received" ]; then
echo -e "failure\n\nExpected$line\n$expected\n"
echo -e "Received$line\n$received\n"
else
echo "success [$elapsed seconds]"
(( ++num_right ))
fi
fi
}
run_test_args "slow" $(seq 1 100000) "Enter sequence of integers, each followed by a space: Number of inversions: 0"
run_test_args "slow" $(seq -s " " 100000 -1 1) "Enter sequence of integers, each followed by a space: Number of inversions: 4999950000"
run_test_args "" $(seq -s " " 100000) "Enter sequence of integers, each followed by a space: Number of inversions: 0"
run_test_args "" $(seq -s " " 100000 -1 1) "Enter sequence of integers, each followed by a space: Number of inversions: 4999950000"