Expected outputs are the second type of input.
For example, if you want to test a square function, you should enter as (0, 1, 2, -1, -2) and the expected result as (0, 1, 4, 1, 4).
Then you can compare each input result with the expected output and reports, for example.
You can work with arrays:
in=(0 1 2 -1 -2) out=(0 1 4 2 4) for i in $(seq 0 $((${#in[@]}-1)) ) do (( ${in[i]} * ${in[i]} - ${out[i]} )) && echo -n bad" " || echo -n fine" " echo $i ": " ${in[i]}"² ?= " ${out[i]} done fine 0 : 0² ?= 0 fine 1 : 1² ?= 1 fine 2 : 2² ?= 4 bad 3 : -1² ?= 2 fine 4 : -2² ?= 4
Of course, you can read both arrays from a file.
Testing with ((...)) can cause arithmetic expressions, strings, and files. Try
help test
for review.
Reading Wordwise strings from a file:
for n in $(< f1); do echo $n "-" ; done
Reading to an array:
arr=($(< file1))
Read linewise file:
for i in $(seq 1 $(cat file1 | wc -l )) do line=$(sed -n ${i}p file1) echo $line"#" done
Testing for program output sounds like comparing strings and capturing n=$(cmd param1 param2)
program output n=$(cmd param1 param2)
:
asux:~/prompt > echo -e "foo\nbar\nbaz" foo bar baz asux:~/prompt > echo -e "foo\nbar\nbaz" > file asux:~/prompt > for i in $(seq 1 3); do line=$(sed -n ${i}p file); test "$line" = "bar" && echo match || echo fail ; done fail match fail
Further use: comparison of regular expressions in lines with = ~ in [[...]] brackets:
for i in $(seq 1 3) do line=$(sed -n ${i}p file) echo -n $line if [[ "$line" =~ ba. ]]; then echo " "match else echo " "fail fi done foo fail bar match baz match