Skip to content

Instantly share code, notes, and snippets.

@dbyr
Created January 5, 2019 00:42
Show Gist options
  • Save dbyr/0c7c21d95f421b426d35f63d635d726d to your computer and use it in GitHub Desktop.
Save dbyr/0c7c21d95f421b426d35f63d635d726d to your computer and use it in GitHub Desktop.
Since university programming assignments often require "input/output" type of programming, I have made this simple script to run any number of input/output tests on a Java program.
#!/bin/bash
# this script will run input/output tests for java programs
USAGE="Usage:
javatest.sh <progpath> <inputs_folder_path> <expected_outputs_folder_path>
The names of the input files is expected to be the same as the name of the corresponding expected output files."
if [[ $# != 3 ]]; then
echo "$USAGE"
exit
fi
PROGRAM=$1
INPUTS=$2
EXPECTED=$3
passed=0
total=$(ls $INPUTS | wc -l)
failed_tests=""
for file in $(ls $INPUTS); do
inputfile="$INPUTS/$file"
expectedfile="$EXPECTED/$file"
echo "Running test '$file'"
output=$(java $PROGRAM $(cat $inputfile))
retcode=$?
if [[ $retcode != 0 ]]; then
echo "Program returned code $retcode"
continue
fi
if [[ $output == $(cat $expectedfile) ]]; then
passed=$((passed+1))
else
failed_tests="${failed_tests}Test $file failed.\nGot '$output'\nExpected '$(cat $expectedfile)'\n\n"
fi
done
echo
echo
echo -e "$failed_tests"
echo "$passed of $total tests passed."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment