1 # The Evaluator -- Shell Function Library
2 # (c) 2001--2008 Martin Mares <mj@ucw.cz>
8 # File handles used: fd1=log, fd2=progress
33 # Report an internal error
35 [ -n "$HAVE_LOG" ] && echo "Fatal error: $@"
41 # Report a fatal error in the program being tested
43 [ -n "$HAVE_LOG" ] && echo "Fatal error: $@"
49 ln $1 $2 2>/dev/null || cp $1 $2
52 # Expand occurrences of `$var' in a given variable
59 # Given a <prefix>, override each variable <x> by <prefix>_<x>
61 function override-vars
65 # `${!${1}_@}' does not work, so we have to use eval
66 OR=($(eval echo '${!'$1'_@}'))
67 for V in "${OR[@]}" ; do
77 pstart "Preparing sandbox... "
78 if [ -z "$TEST_USER" -o "$TEST_USER" == $EVAL_USER ] ; then
79 pcont "running locally (INSECURE), "
85 pcont "used account $TEST_USER, "
86 BOXDIR=$MO_ROOT/eval/$TEST_USER
87 BOXCMD=bin/box-$TEST_USER
89 [ -d $BOXDIR -a -f $BOXCMD ] || die "Sandbox set up incorrectly"
90 BOXCMD="$BOXCMD -c$BOXDIR"
91 echo "Sandbox directory: $BOXDIR"
92 echo "Sandbox command: $BOXCMD"
99 [ -n "$BOXCMD" ] || die "box-init not called"
103 # Initialization of testing directories
107 pstart "Initializing... "
109 PDIR=problems/$PROBLEM
110 SDIR=solutions/$CONTESTANT/$PROBLEM
111 TDIR=testing/$CONTESTANT/$PROBLEM
113 [ -d $PDIR ] || die "Problem $PROBLEM not known"
114 [ -d $SDIR ] || fatal "Solution of $PROBLEM not found"
115 mkdir -p $TDIR $TMPDIR
117 mkdir -p $TDIR $TMPDIR
119 Testing solution of $PROBLEM by $CONTESTANT
120 Test started at `date`
121 Contestant's solution directory: $SDIR
122 Problem directory: $PDIR
123 Testing directory: $TDIR
128 # Locate source file.
129 # If no parameter is given, locate it in SDIR and return name as SRCN and extension as SRCEXT
130 # Or a file name can be given and then SDIR, SRCN and SRCEXT are set.
131 # Beware, SDIR and SRCN can contain spaces and other strange user-supplied characters.
133 function locate-source
135 pstart "Finding source... "
137 if [ -n "$1" ] ; then
139 local S=`basename "$1"`
140 SBASE=$(echo "$S" | sed 's/\.\([^.]\+\)//')
141 SRCEXT=$(echo "$S" | sed '/\./!d; s/.*\.\([^.]\+\)/\1/')
142 if [ -n "$SRCEXT" ] ; then
143 # Full name given, so just check the extension and existence
145 [ -f "$SDIR/$SRCN" ] || die "Cannot find source file $SDIR/$SRCN"
147 for a in $EXTENSIONS ; do
148 if [ $a == $SRCEXT ] ; then
150 echo "Explicitly set source file: $SDIR/$SRCN"
154 die "Unknown extension .$SRCEXT"
159 for a in $EXTENSIONS ; do
160 if [ -f "$SDIR/$SBASE.$a" ] ; then
161 [ -z "$SRCN" ] || die "Multiple source files found: $SDIR/$PROBLEM.$a and $SDIR/$SRCN. Please fix."
166 [ -n "$SRCN" ] || fatal "NOT FOUND"
168 echo "Found source file: $SDIR/$SRCN"
171 # Compilation (compile SDIR/SRCN with PDIR/COMP_EXTRAS to EXE=TDIR/PROBLEM)
175 pstart "Compiling... "
177 a="ALIAS_EXT_$SRCEXT"
178 if [ -n "${!a}" ] ; then
180 echo "Normalized file extension: $SRCEXT"
182 override-vars "EXT_$SRCEXT"
184 # Beware, the original SRCN can be a strange user-supplied name
186 cp "$SDIR/$SRCN" $TDIR/$SRC
187 if [ -n "$COMP_EXTRAS" ] ; then
188 echo "Extras: $COMP_EXTRAS"
189 for a in $COMP_EXTRAS ; do cp $PDIR/$a $TDIR/ ; done
193 for a in $SRC $COMP_EXTRAS ; do cp $TDIR/$a $BOXDIR/ ; done
195 CCMD=$(expand-var COMP)
196 COMP_SANDBOX_OPTS=$(expand-var COMP_SANDBOX_OPTS)
197 echo "Compiler command: $CCMD"
198 echo "Compiler sandbox options: $COMP_SANDBOX_OPTS"
199 eval $COMP_SANDBOX_INIT
201 echo "Compiler input files:"
203 echo "Compiler output:"
204 if ! $BOXCMD $COMP_SANDBOX_OPTS -- $CCMD 2>$TDIR/compile.out ; then
205 COMPILE_MSG="`cat $TDIR/compile.out`"
206 pend "FAILED: $COMPILE_MSG"
210 cat $TDIR/compile.out
212 echo "Compiler output files:"
214 if [ ! -f $BOXDIR/$PROBLEM ] ; then
215 pend "FAILED: Missing executable file"
216 echo "Missing executable file"
220 cp -a $BOXDIR/$PROBLEM $EXE
221 echo "Compiled OK, result copied to $EXE"
225 # Running of test program according to current task type (returns exit code and TEST_MSG)
229 [ -f $PDIR/$TEST.config ] && . $PDIR/$TEST.config
230 override-vars "TEST_$TEST"
242 if [ -s $TDIR/$TEST.pts ] ; then
243 P=`cat $TDIR/$TEST.pts`
247 # Translate signal numbers to readable strings
248 SG=${M#Caught fatal signal }
249 SG=${SG#Committed suicide by signal }
250 if [ "$SG" != "$M" ] ; then
251 SG=`perl -MConfig -e '@s=split / /,$Config{sig_name}; print $s[$ARGV[0]]' $SG`
252 [ -z "$SG" ] || M="$M (SIG$SG)"
255 # Translate runtime errors to readable strings
256 RE=${M#Exited with error status }
257 if [ -n "$EXIT_CODE_HOOK" -a "$RE" != "$M" ] ; then
258 NEWMSG=`$EXIT_CODE_HOOK $RE`
259 if [ -n "$NEWMSG" ] ; then
260 M="Runtime error $RE: $NEWMSG"
273 echo "Executable file: $TDIR/$PROBLEM"
274 if [ ! -x $TDIR/$PROBLEM ] ; then
275 test-result 0 "Compile error"
277 cp $TDIR/$PROBLEM $BOXDIR/
279 IN_TYPE=${IN_TYPE:-$IO_TYPE}
280 OUT_TYPE=${OUT_TYPE:-$IO_TYPE}
282 file) echo "Input file: $PROBLEM.in (from $PDIR/$TEST.in)"
283 try-ln $PDIR/$TEST.in $TDIR/$TEST.in
284 cp $PDIR/$TEST.in $BOXDIR/$PROBLEM.in
285 [ $TASK_TYPE == interactive ] || BOX_EXTRAS="$BOX_EXTRAS -i/dev/null"
287 stdio) echo "Input file: <stdin> (from $PDIR/$TEST.in)"
288 try-ln $PDIR/$TEST.in $TDIR/$TEST.in
289 cp $PDIR/$TEST.in $BOXDIR/.stdin
290 BOX_EXTRAS="$BOX_EXTRAS -i.stdin"
292 none) echo "Input file: <none>"
294 *) die "Unknown IN_TYPE $IN_TYPE"
297 if [ -n "$EV_PEDANT" -a $IN_TYPE != none ] ; then
299 if [ "$EV_PEDANT" = 1 ] ; then
302 bin/pedant <$TDIR/$TEST.in >$TDIR/$TEST.pedant $EV_PEDANT
303 if [ -s $TDIR/$TEST.pedant ] ; then
305 sed 's/^/\t/' <$TDIR/$TEST.pedant >&2
310 file) echo "Output file: $PROBLEM.out"
311 [ $TASK_TYPE == interactive ] || BOX_EXTRAS="$BOX_EXTRAS -o/dev/null"
313 stdio) echo "Output file: <stdout>"
314 BOX_EXTRAS="$BOX_EXTRAS -o.stdout"
316 none) echo "Output file: <none>"
318 *) die "Unknown OUT_TYPE $OUT_TYPE"
321 echo "Timeout: $TIME_LIMIT s"
322 echo "Memory: $MEM_LIMIT KB"
324 echo "Sandbox contents before start:"
330 echo "Sandbox contents after exit:"
332 case ${OUT_TYPE:-$IO_TYPE} in
333 file) [ -f $BOXDIR/$PROBLEM.out ] || test-result 0 "No output file"
334 cp $BOXDIR/$PROBLEM.out $TDIR/$TEST.out
336 stdio) [ -f $BOXDIR/.stdout ] || test-result 0 "No output file"
337 cp $BOXDIR/.stdout $TDIR/$TEST.out
341 if [ -n "$OUTPUT_FILTER" -a "$OUT_TYPE" != none -a -z "$EV_NOFILTER" ] ; then
343 FILTER=$(expand-var OUTPUT_FILTER)
344 echo "Output filter command: $FILTER"
345 mv $TDIR/$TEST.out $TDIR/$TEST.raw
346 if ! eval $FILTER 2>$TMPDIR/exec.out ; then
348 MSG=`tail -1 $TMPDIR/exec.out`
349 if [ -z "$MSG" ] ; then MSG="Filter failed" ; fi
356 # Running of test program with file input/output
358 function test-run-file
362 BOXOPTS=$(expand-var TEST_SANDBOX_OPTS)
363 echo "Sandbox options: $BOXOPTS"
364 if ! $BOXCMD $BOXOPTS -- ./$PROBLEM 2>$TMPDIR/exec.out ; then
366 MSG=`tail -1 $TMPDIR/exec.out`
373 # Running of interactive test programs
375 function test-run-interactive
379 BOXOPTS=$(expand-var TEST_SANDBOX_OPTS)
380 echo "Sandbox options: $BOXOPTS"
381 ICCMD=$(expand-var IA_CHECK)
382 echo "Interactive checker: $ICCMD"
383 if ! $HDIR/bin/iwrapper $BOXCMD $BOXOPTS -- ./$PROBLEM @@ $ICCMD 2>$TMPDIR/exec.out ; then
385 MSG="`head -1 $TMPDIR/exec.out`"
392 # "Running" of open-data problems
394 function test-run-open-data
396 [ -f $SDIR/$TEST.out ] || test-result 0 "No solution"
397 ln $SDIR/$TEST.out $TDIR/$TEST.out
402 function syntax-check
404 [ -n "$SYNTAX_CHECK" ] || return 0
405 [ -z "$EV_NOCHECK" ] || return 0
407 SCHECK=$(expand-var SYNTAX_CHECK)
408 echo "Syntax check command: $SCHECK"
409 if ! eval $SCHECK 2>$TMPDIR/exec.out ; then
411 MSG=`tail -1 $TMPDIR/exec.out`
412 if [ -z "$MSG" ] ; then MSG="Wrong syntax" ; fi
420 function output-check
423 if [ -n "$OUTPUT_CHECK" -a "$OUT_TYPE" != none -a -z "$EV_NOCHECK" ] ; then
425 [ -f $PDIR/$TEST.out ] && ln $PDIR/$TEST.out $TDIR/$TEST.ok
426 OCHECK=$(expand-var OUTPUT_CHECK)
427 echo "Output check command: $OCHECK"
428 if ! eval $OCHECK 2>$TMPDIR/exec.out ; then
430 MSG=`tail -1 $TMPDIR/exec.out`
431 if [ -z "$MSG" ] ; then MSG="Wrong answer" ; fi
435 MSG=`tail -1 $TMPDIR/exec.out`
437 if [ -z "$MSG" ] ; then MSG="OK" ; fi
438 test-result $POINTS_PER_TEST "$MSG"
441 # Setup of public commands
443 function public-setup
446 PDIR=$MO_ROOT/problems/$PROBLEM
450 [ -d $PDIR ] || die "Unknown problem $PROBLEM"
452 pstart "Initializing... "
458 BOXCMD="$MO_ROOT/bin/box -c$BOXDIR"
460 pend "OK (see 'check-log' for details)"
463 # Locate output of open data problem, test case TEST
464 # Beware, SDIR and SRCN can contain spaces and other strange user-supplied characters.
468 [ -f $PDIR/$TEST.in ] || die "Unknown test $TEST"
469 if [ -n "$1" ] ; then
473 SRCN=$SDIR/$PROBLEM$TEST.out
475 [ -f "$SDIR/$SRCN" ] || fatal "Output file $SRCN not found"
478 # Translation of runtime error codes for various compilers
480 function fpc-exit-code
483 200) echo -n "Division by zero" ;;
484 201) echo -n "Range check error" ;;
485 202) echo -n "Stack overflow" ;;
486 203) echo -n "Heap overflow" ;;
487 205) echo -n "Floating point overflow" ;;
488 215) echo -n "Arithmetic overflow" ;;
489 216) echo -n "Segmentation fault" ;;