Skip to content

Instantly share code, notes, and snippets.

@dbyr
Created January 5, 2019 00:42
Show Gist options
  • Select an option

  • Save dbyr/0c7c21d95f421b426d35f63d635d726d to your computer and use it in GitHub Desktop.

Select an option

Save dbyr/0c7c21d95f421b426d35f63d635d726d to your computer and use it in GitHub Desktop.

Revisions

  1. David Byrne created this gist Jan 5, 2019.
    42 changes: 42 additions & 0 deletions javatest.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    #!/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."