Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.07 KB | None | 0 0
  1. #!/bin/bash
  2. # https://stackoverflow.com/questions/10118381/bash-script-to-automatically-test-program-output-c
  3.  
  4. # If number of arguments less then 1; print usage and exit
  5. if [ $# -lt 1 ]; then
  6.     printf "Usage: %s <application>\n" "$0" >&2
  7.     exit 1
  8. fi
  9.  
  10. bin="$1"                        # The application (from command arg)
  11. diff="colordiff -ya"                    # Diff command, or what ever
  12. folder="Comp2019/meta1"
  13.  
  14. # An array, do not have to declare it, but is supposedly faster
  15. # declare -a file_base=("file1" "file2" "file3")
  16. file_base=($(ls -1 ${folder} -I "*.out"))
  17.  
  18. # Loop the array
  19. for file in "${file_base[@]}"; do
  20.     # Padd file_base with suffixes
  21.     file_in="${folder}/$file"                           # The in file
  22.     file_out="${file//.dgo/}"
  23.     file_out_val="${folder}/${file_out//.go/}.out"      # The out file to check against
  24.     file_out_tst="$(mktemp)"                            # The outfile from test application
  25.  
  26.     # Validate infile exists (do the same for out validate file)
  27.     if [ ! -f "$file_in" ]; then
  28.         printf "In file %s is missing\n" "$file_in"
  29.         continue;
  30.     fi
  31.     if [ ! -f "$file_out_val" ]; then
  32.         printf "Validation file %s is missing\n" "$file_out_val"
  33.         continue;
  34.     fi
  35.  
  36.     printf "Testing against %s\n" "$file_in"
  37.  
  38.     # Run application, redirect in file to app, and output to out file
  39.     "./$bin" < "$file_in" > "$file_out_tst"
  40.  
  41.     # Execute diff
  42.     echo "----------------------------------------------------------------------------------"
  43.     $diff "$file_out_tst" "$file_out_val"
  44.     e_code=$?
  45.     echo "----------------------------------------------------------------------------------"
  46.  
  47.     # Check exit code from previous command (ie diff)
  48.     # We need to add this to a variable else we can't print it
  49.     # as it will be changed by the if [
  50.     # If not 0 then the files differ (at least with diff)
  51.     if [ $e_code != 0 ]; then
  52.             printf "TEST FAIL: %s\n" "$e_code"
  53.     else
  54.             printf "TEST OK!: %s\n" "$e_code"
  55.     fi
  56.  
  57.     # Pause by prompt
  58.     read -p "Enter a/q to abort, anything else to continue: " input_data
  59.     # If input is "a" then abort
  60.     [ "$input_data" == "a" ] && break
  61.     [ "$input_data" == "q" ] && break
  62.  
  63. done
  64.  
  65. # Clean exit with status 0
  66. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement