Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # https://stackoverflow.com/questions/10118381/bash-script-to-automatically-test-program-output-c
- # If number of arguments less then 1; print usage and exit
- if [ $# -lt 1 ]; then
- printf "Usage: %s <application>\n" "$0" >&2
- exit 1
- fi
- bin="$1" # The application (from command arg)
- diff="colordiff -ya" # Diff command, or what ever
- folder="Comp2019/meta1"
- # An array, do not have to declare it, but is supposedly faster
- # declare -a file_base=("file1" "file2" "file3")
- file_base=($(ls -1 ${folder} -I "*.out"))
- # Loop the array
- for file in "${file_base[@]}"; do
- # Padd file_base with suffixes
- file_in="${folder}/$file" # The in file
- file_out="${file//.dgo/}"
- file_out_val="${folder}/${file_out//.go/}.out" # The out file to check against
- file_out_tst="$(mktemp)" # The outfile from test application
- # Validate infile exists (do the same for out validate file)
- if [ ! -f "$file_in" ]; then
- printf "In file %s is missing\n" "$file_in"
- continue;
- fi
- if [ ! -f "$file_out_val" ]; then
- printf "Validation file %s is missing\n" "$file_out_val"
- continue;
- fi
- printf "Testing against %s\n" "$file_in"
- # Run application, redirect in file to app, and output to out file
- "./$bin" < "$file_in" > "$file_out_tst"
- # Execute diff
- echo "----------------------------------------------------------------------------------"
- $diff "$file_out_tst" "$file_out_val"
- e_code=$?
- echo "----------------------------------------------------------------------------------"
- # Check exit code from previous command (ie diff)
- # We need to add this to a variable else we can't print it
- # as it will be changed by the if [
- # If not 0 then the files differ (at least with diff)
- if [ $e_code != 0 ]; then
- printf "TEST FAIL: %s\n" "$e_code"
- else
- printf "TEST OK!: %s\n" "$e_code"
- fi
- # Pause by prompt
- read -p "Enter a/q to abort, anything else to continue: " input_data
- # If input is "a" then abort
- [ "$input_data" == "a" ] && break
- [ "$input_data" == "q" ] && break
- done
- # Clean exit with status 0
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement