Skip to content

Instantly share code, notes, and snippets.

@seri
Created February 6, 2015 06:58
Show Gist options
  • Save seri/855a04c8f5895d7c7a68 to your computer and use it in GitHub Desktop.
Save seri/855a04c8f5895d7c7a68 to your computer and use it in GitHub Desktop.
runner.sh Feb 06, 2005
# runner.sh mode /path/to/solver /path/to/input/dir /path/to/output/dir
# mode = quiet|verbose|file
mode="$1"
solver="$2"
data_d="$3"
output_d="$4"
ostream=/dev/null
command_exists () {
type "$1" > /dev/null 2>&1 ;
}
timeit () {
if command_exists /usr/bin/time ; then
/usr/bin/time -o /dev/null echo "testing availability of the time command" &>/dev/null
if [ $? -eq 0 ]; then
/usr/bin/time -o $ostream "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9"
return
elif [ $mode = 'verbose' ]; then
/usr/bin/time "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9"
return
fi
elif [ $mode = 'verbose' ]; then
if command_exists time ; then
time "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9"
return
fi
fi
"$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9"
}
puts () {
echo "$1" > $ostream
}
write () {
echo -n "$1" > $ostream
}
view_file () {
filename="$1"
write '<'
cat "$filename" > $ostream
puts '>'
}
report_case () {
write ' input: '
view_file "$data_d/$test_case.in"
write ' expected: '
view_file "$data_d/$test_case.out"
write ' received: '
view_file "$output_d/$test_case.out"
}
init () {
export time="time: %es - memory: %mk"
export timeformat=%3lr
if [ ! -d "$output_d" ]; then
mkdir "$output_d"
fi
if [ "$mode" = 'verbose' ]; then
ostream=/dev/stdout
elif [ "$mode" = 'file' ]; then
ostream=log
fi
dir=`dirname $0`
checker="$dir/../build/check"
if [ ! -x "$checker" ]; then
undo=`pwd`
cd "$dir/../util/check"
make
cd "$undo"
fi
}
main () {
cases=0
fails=0
errors=0
failstr=''
errorstr=''
for input in "$data_d"/*.in; do
if [ -f "$input" ]; then
cases=$(( cases + 1 ))
test_case=`basename "$input"`
test_case=${test_case%.in}
write "check $test_case ... "
timeit "$solver" "$data_d/$test_case.in" "$output_d/$test_case.out"
"$checker" "$data_d/$test_case.out" "$output_d/$test_case.out" > $ostream
retcode=$?
if [ $retcode -eq 0 ]; then
puts 'passed'
elif [ $retcode -eq 1 ]; then
fails=$(( fails + 1 ))
failstr="$failstr $test_case"
puts 'failed'
report_case "$test_case"
else
errors=$(( errors + 1 ))
errorstr="$errorstr $test_case"
puts 'error'
report_case "$test_case"
fi
fi
done
echo "$cases cases checked, $fails failed, $errors errored"
if [ $fails -gt 0 ]; then
echo "failed cases:${failstr}"
fi
if [ $errors -gt 0 ]; then
echo "errored cases:${errorstr}"
fi
}
init
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment