Skip to content

Instantly share code, notes, and snippets.

@sebix
Created July 16, 2011 15:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebix/1086477 to your computer and use it in GitHub Desktop.
Save sebix/1086477 to your computer and use it in GitHub Desktop.
Check your programs with Input files
#!/bin/bash
# Version: 06.03.2012 17:00
# Author: sebix (https://github.com/sebix)
# URL: https://gist.github.com/gists/1086477
CORRECT=0
FALSE=0
SUM=0
COLOR=1
SUMWITHOUT=0
GREEN="\033[32m"
RED="\033[31m"
NORMAL="\033[30m"
HELP="Check your programs with Input files
Usage: check.sh [-hc] [-p PROG] [-g/r/n color]
check.sh detects the executable (last modified) automatically,
as well as the Testdata
Supported Input-Files:
* *.in
* grader.in.* (RunC)
* [name].in.* (COCI)
Supported Output-Files:
* *.out
* *.sol
* *.ans
* grader.expect.* (RunC)
* [name].out.* (COCI)
PARAMETERS:
* To turn off colors use the optional Parameter -c
* To show this help message give me a -h
* and to manually define the program use -p PROGNAME
use a format like this: \"-p ./prog\"
* You can also define your own colors with
\t-r for the red color
\t-g for green and
\t-n for default / normal color
Author: sebix (https://github.com/sebix)
https://gist.github.com/gists/1086477"
# Define useful output commands
function printgreen {
if [ $COLOR = "1" ]; then
echo -e "$GREEN$1$NORMAL"
else
echo -e $1
fi
}
function printred {
if [ $COLOR -eq 1 ]; then
echo -e "$RED$1$NORMAL"
else
echo -e $1
fi
}
# Find executable (latest modified executable)
PROG=`find . -type f -executable -exec ls -t {} + | head -n 1`
# and get params
while getopts hcp:g:r:n: opt
do
case $opt in
c)
COLOR=0;;
p)
PROG=$OPTARG;;
g)
GREEN=$OPTARG;;
r)
RED=$OPTARG;;
n)
NORMAL=$OPTARG;;
h)
echo -e "$HELP" # newlines are only printed with double quotes
exit 1
;;
esac
done
if [ -z $PROG ]; then
printred "No executable found!"
exit 1
fi
echo "Program: $PROG"
#for i in `ls -v *.in* 2>/dev/null`; do
for i in `find . -name "*.in*" -follow | xargs ls -v 2>/dev/null`; do
# use natural sort with -v
# Do not show Errors (e.g. "No such files in directory")
NAME=`echo $i | sed 's/\.in//'`
INFILE="$NAME.in"
OUTFILE="" # Support more Outfiles
if [ -r "$NAME.out" ]; then
OUTFILE="$NAME.out"
else if [ -r "$NAME.sol" ]; then
OUTFILE="$NAME.sol"
else if [ -r "$NAME.ans" ]; then
OUTFILE="$NAME.sol"
else if [ `echo $i | grep grader` ]; then # RunC
OUTFILE=`echo $i | sed 's/in/expect/'`
INFILE=$i
else if [ `echo $i | grep \.in\.` ]; then # COCI
OUTFILE=`echo $i | sed 's/in/out/'`
INFILE=$i
fi
fi
fi
fi
fi
# OUTPUT=`$PROG < $INFILE 2>&1 | tr -d '\n'`
OUTPUT=`$PROG < $INFILE 2>&1 | tail -n 1`
# 2>&1 to print Segfaults
# ignore debugging-outputs
if [ $OUTFILE != "" -a -f $OUTFILE ]; then
# Compare with output file
SOLUTION=`cat $OUTFILE | tr -d '\r'`
if [ "$OUTPUT" = "$SOLUTION" ]; then
printgreen "$NAME is correct\t($SOLUTION)"
((CORRECT=$CORRECT+1))
else
printred "$NAME is incorrect\t($OUTPUT instead of $SOLUTION)"
((FALSE=$FALSE+1))
fi
else
echo -e "$NAME: \t\t($OUTPUT)"
((SUMWITHOUT=$SUMWITHOUT+1))
fi
((SUM=$SUM+1))
done
if [ $SUMWITHOUT -ne 0 ]; then
echo -e "Correct: $CORRECT\tFalse: $FALSE\tTotal: $SUM\twithout Test: $SUMWITHOUT"
else
echo -e "Correct: $CORRECT\tFalse: $FALSE\tTotal: $SUM"
fi
# Everything correct?
if [ $FALSE -eq 0 ]; then
printgreen "Well done!"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment