Skip to content

Instantly share code, notes, and snippets.

@Kyly
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save Kyly/96cf8223c972b0c8207a to your computer and use it in GitHub Desktop.

Select an option

Save Kyly/96cf8223c972b0c8207a to your computer and use it in GitHub Desktop.
Tester Bash Script
#!/bin/bash
hw=${PWD##*/}
parent=${PWD%/*/*}
writeusage () {
echo
echo " tester - UCSD CSE30"
echo " Makes and tests your program simultaneously with the sample program."
echo " The differences in stdout/stderr are shown in a vimdiff."
echo
echo " Usage: tester [program] [solution program] [filename]"
echo
echo " Be sure to run this in a \"pa#\" directory."
echo
}
# If the user doesn't know how to use this
if [[ "$#" < 3 ]]; then
writeusage
exit 1
fi
# First arg is the driver
driver="$1"
my_prog="./$driver"
shift
# Second arg is solution driver name
soldriver="$1"
sample_prog="$parent/public/$soldriver"
shift
#Third arg is the text file
input="$1"
shift
# Make your file
# This makes the default target
make
if [ "$?" -ne 0 ];then
echo
echo
echo " **********************************************"
echo " * Make failed!! Exiting script. *"
echo " **********************************************"
echo " ~ Sorry Dude :( ~"
echo
echo
#Quit script
exit 1
fi
# Remove old output files
rm -f mine sol
count=0 # Count the number of tests performed
passedCount=0 # Count the number of tests passed
OLD_IFS=$IFS # Save the delimiter
IFS=$'\n' # Make the delimiter a newline
# Get lines from file
for line in $(cat $input)
do
# Reset delimiter
IFS=$OLD_IFS
let count++
count_msg="TEST: $count\n"
echo $count_msg
# number test in file to make the results easer to understend
echo "$count_msg: $args" >> mine
echo "$count_msg: $args" >> sol
# Read arguments
args=""
for num in $line; do
args="$args $num"
done
# Create output file taking in stderr & stdout
echo " Attempting to run $my_prog$args ..."
$my_prog$args >> mine 2>&1
# Capture exit code and write it below current test
echo "EXIT CODE: $?\n" >> mine
echo " Running $my_prog$args...\n"
# Save solution output
echo " Attempting to run $sample_prog $args..."
$sample_prog$args >> sol 2>&1
# Capture exit code and write it below current test
echo "EXIT CODE: $?\n" >> sol
echo " Running $sample_prog$args...\n"
if [ "`diff mine sol`" = "" ]; then
let passedCount++
fi
IFS=$'\n'
done < $input
# Compare output files
echo $passedCount/$count tests passed.
echo "Would you like to see the diff file? [y/n]"
read continueKey
if [ "${continueKey}" = "y" ]; then
vimdiff mine sol
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment