I am trying to write test cases in a script template which runs the function run_test_args
that takes three commands: an optional command-line argument, a string of integers each separated by a space (ex "1 2 3 4 5") that serves as the user input of the program's prompt, and a string of the expected output. Since the program is to be tested using a large sequence of numbers, I am trying to use seq
in place of the second argument in the following form: $(seq -s " " 100000)
to generate 1 to 100,000 separated by spaces and formatted as a string.
However, when I run the test script, the function always recognizes the first element from the output of seq
, which is 1 in the above case, as the user input, and the second element as the expected output.
How can I take the output from seq
and format it as all one string rather than an array of strings (which is seems to be doing given how the function is receiving arguments)?
Here is the run_test_args
function and the tests that use the seq
command:
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"