Skip to content

Instantly share code, notes, and snippets.

@jeremiah-ang
Last active August 23, 2017 05:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremiah-ang/a80f43fa470fa95e3323521285cb78dd to your computer and use it in GitHub Desktop.
Save jeremiah-ang/a80f43fa470fa95e3323521285cb78dd to your computer and use it in GitHub Desktop.
Execute java project with input and output
#!/bin/sh
# Set variables
PROJECT_NAME=$1
PROJECT_NAME_LOWER=$(echo $PROJECT_NAME | tr '[:upper:]' '[:lower:]')
TEST_CASE=$PROJECT_NAME_LOWER$2
PATH_TO_TEST_INPUTS=input
PATH_TO_TEST_OUTPUTS=output
echo "Project: $PROJECT_NAME"
# Compile Java
javac $PROJECT_NAME.java
if [ $? -ne 0 ]
then
echo -e ">> " + $1 + ".java didn't compile!"
exit 1
fi
echo -e ">> Compiled Successfully!";
# Execute Java with input and output into result
cp $PATH_TO_TEST_INPUTS/$TEST_CASE.in $TEST_CASE.in.tmp
java $PROJECT_NAME < $TEST_CASE.in.tmp > result
rm $TEST_CASE.in.tmp
# Check for difference
DIFF=$(diff $PATH_TO_TEST_OUTPUTS/$TEST_CASE.out result)
if [ "$DIFF" != "" ]
then
echo -e ">> Test Failed!"
echo -e $DIFF
exit 1
else
echo -e ">> Test Success!"
fi
# Remove other unneccesary files
rm result
rm *.class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment