#!/bin/bash # this script will run input/output tests for java programs USAGE="Usage: javatest.sh 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."