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 # Given a <prefix>, override each variable <x> by <prefix>_<x>
54 function override-vars
60 OR=($(set | sed "s/^$1_//;t;d")) || true
62 for V in "${OR[@]}" ; do
71 pstart "Preparing sandbox... "
72 if [ -z "$TEST_USER" -o "$TEST_USER" == $EVAL_USER ] ; then
73 pcont "running locally (INSECURE), "
79 pcont "used account $TEST_USER, "
80 BOXDIR=$MO_ROOT/eval/$TEST_USER
81 BOXCMD=bin/box-$TEST_USER
83 [ -d $BOXDIR -a -f $BOXCMD ] || die "Sandbox set up incorrectly"
84 BOXCMD="$BOXCMD -c$BOXDIR"
85 echo "Sandbox directory: $BOXDIR"
86 echo "Sandbox command: $BOXCMD"
93 [ -n "$BOXCMD" ] || die "box-init not called"
97 # Initialization of testing directories
101 pstart "Initializing... "
103 PDIR=problems/$PROBLEM
104 SDIR=solutions/$CONTESTANT/$PROBLEM
105 TDIR=testing/$CONTESTANT/$PROBLEM
107 [ -d $PDIR ] || die "Problem $PROBLEM not known"
108 [ -d $SDIR ] || fatal "Solution of $PROBLEM not found"
109 mkdir -p $TDIR $TMPDIR
111 mkdir -p $TDIR $TMPDIR
113 Testing solution of $PROBLEM by $CONTESTANT
114 Test started at `date`
115 Contestant's solution directory: $SDIR
116 Problem directory: $PDIR
117 Testing directory: $TDIR
122 # Locate source file.
123 # If no parameter is given, locate it in SDIR and return name as SRCN and extension as SRCEXT
124 # Or a file name can be given and then SDIR, SRCN and SRCEXT are set.
125 # Beware, SDIR and SRCN can contain spaces and other strange user-supplied characters.
127 function locate-source
129 pstart "Finding source... "
131 if [ -n "$1" ] ; then
133 local S=`basename "$1"`
134 SBASE=$(echo "$S" | sed 's/\.\([^.]\+\)//')
135 SRCEXT=$(echo "$S" | sed '/\./!d; s/.*\.\([^.]\+\)/\1/')
136 if [ -n "$SRCEXT" ] ; then
137 # Full name given, so just check the extension and existence
139 [ -f "$SDIR/$SRCN" ] || die "Cannot find source file $SDIR/$SRCN"
141 for a in $EXTENSIONS ; do
142 if [ $a == $SRCEXT ] ; then
144 echo "Explicitly set source file: $SDIR/$SRCN"
148 die "Unknown extension .$SRCEXT"
153 for a in $EXTENSIONS ; do
154 if [ -f "$SDIR/$SBASE.$a" ] ; then
155 [ -z "$SRCN" ] || die "Multiple source files found: $SDIR/$PROBLEM.$a and $SDIR/$SRCN. Please fix."
160 [ -n "$SRCN" ] || fatal "NOT FOUND"
162 echo "Found source file: $SDIR/$SRCN"
165 # Compilation (compile SDIR/SRCN with PDIR/COMP_EXTRAS to EXE=TDIR/PROBLEM)
169 pstart "Compiling... "
170 override-vars "EXT_$SRCEXT"
171 # Beware, the original SRCN can be a strange user-supplied name
173 cp "$SDIR/$SRCN" $TDIR/$SRC
174 if [ -n "$COMP_EXTRAS" ] ; then
175 echo "Extras: $COMP_EXTRAS"
176 for a in $COMP_EXTRAS ; do cp $PDIR/$a $TDIR/ ; done
179 for a in $SRC $COMP_EXTRAS ; do cp $TDIR/$a $BOXDIR/ ; done
181 CCMD=`eval echo $COMP`
182 COMP_SANDBOX_OPTS=`eval echo $COMP_SANDBOX_OPTS`
183 echo "Compiler command: $CCMD"
184 echo "Compiler sandbox options: $COMP_SANDBOX_OPTS"
185 eval $COMP_SANDBOX_INIT
187 echo "Compiler input files:"
189 echo "Compiler output:"
190 if ! $BOXCMD $COMP_SANDBOX_OPTS -- $CCMD 2>$TDIR/compile.out ; then
191 COMPILE_MSG="`cat $TDIR/compile.out`"
192 pend "FAILED: $COMPILE_MSG"
196 cat $TDIR/compile.out
198 echo "Compiler output files:"
200 if [ ! -f $BOXDIR/$PROBLEM ] ; then
201 pend "FAILED: Missing executable file"
202 echo "Missing executable file"
206 cp -a $BOXDIR/$PROBLEM $EXE
207 echo "Compiled OK, result copied to $EXE"
211 # Running of test program according to current task type (returns exit code and TEST_MSG)
215 [ -f $PDIR/$TEST.config ] && . $PDIR/$TEST.config
216 override-vars "TEST_$TEST"
228 if [ -s $TDIR/$TEST.pts ] ; then
229 P=`cat $TDIR/$TEST.pts`
233 # Translate signal numbers to readable strings
234 SG=${M#Caught fatal signal }
235 SG=${SG#Committed suicide by signal }
236 if [ "$SG" != "$M" ] ; then
237 SG=`perl -MConfig -e '@s=split / /,$Config{sig_name}; print $s[$ARGV[0]]' $SG`
238 [ -z "$SG" ] || M="$M (SIG$SG)"
241 # Translate Free Pascal runtime errors to readable strings
242 RE=${M#Exited with error status }
243 if [ "$FREE_PASCAL_RTE" == 1 -a "$RE" != "$M" ] ; then
244 N="Runtime error $RE"
246 200) M="$N: Division by zero" ;;
247 201) M="$N: Range check error" ;;
248 202) M="$N: Stack overflow" ;;
249 203) M="$N: Heap overflow" ;;
250 205) M="$N: Floating point overflow" ;;
251 215) M="$N: Arithmetic overflow" ;;
252 216) M="$N: Segmentation fault" ;;
266 echo "Executable file: $TDIR/$PROBLEM"
267 if [ ! -x $TDIR/$PROBLEM ] ; then
268 test-result 0 "Compile error"
270 cp $TDIR/$PROBLEM $BOXDIR/
272 IN_TYPE=${IN_TYPE:-$IO_TYPE}
273 OUT_TYPE=${OUT_TYPE:-$IO_TYPE}
275 file) echo "Input file: $PROBLEM.in (from $PDIR/$TEST.in)"
276 try-ln $PDIR/$TEST.in $TDIR/$TEST.in
277 cp $PDIR/$TEST.in $BOXDIR/$PROBLEM.in
278 [ $TASK_TYPE == interactive ] || BOX_EXTRAS="$BOX_EXTRAS -i/dev/null"
280 stdio) echo "Input file: <stdin> (from $PDIR/$TEST.in)"
281 try-ln $PDIR/$TEST.in $TDIR/$TEST.in
282 cp $PDIR/$TEST.in $BOXDIR/.stdin
283 BOX_EXTRAS="$BOX_EXTRAS -i.stdin"
285 none) echo "Input file: <none>"
287 *) die "Unknown IN_TYPE $IN_TYPE"
290 if [ -n "$EV_PEDANT" -a $IN_TYPE != none ] ; then
292 if [ "$EV_PEDANT" = 1 ] ; then
295 bin/pedant <$TDIR/$TEST.in >$TDIR/$TEST.pedant $EV_PEDANT
296 if [ -s $TDIR/$TEST.pedant ] ; then
298 sed 's/^/\t/' <$TDIR/$TEST.pedant >&2
303 file) echo "Output file: $PROBLEM.out"
304 [ $TASK_TYPE == interactive ] || BOX_EXTRAS="$BOX_EXTRAS -o/dev/null"
306 stdio) echo "Output file: <stdout>"
307 BOX_EXTRAS="$BOX_EXTRAS -o.stdout"
309 none) echo "Output file: <none>"
311 *) die "Unknown OUT_TYPE $OUT_TYPE"
314 echo "Timeout: $TIME_LIMIT s"
315 echo "Memory: $MEM_LIMIT KB"
317 echo "Sandbox contents before start:"
323 echo "Sandbox contents after exit:"
325 case ${OUT_TYPE:-$IO_TYPE} in
326 file) [ -f $BOXDIR/$PROBLEM.out ] || test-result 0 "No output file"
327 cp $BOXDIR/$PROBLEM.out $TDIR/$TEST.out
329 stdio) [ -f $BOXDIR/.stdout ] || test-result 0 "No output file"
330 cp $BOXDIR/.stdout $TDIR/$TEST.out
334 if [ -n "$OUTPUT_FILTER" -a "$OUT_TYPE" != none -a -z "$EV_NOFILTER" ] ; then
336 FILTER=`eval echo \"$OUTPUT_FILTER\"`
337 echo "Output filter command: $FILTER"
338 mv $TDIR/$TEST.out $TDIR/$TEST.raw
339 if ! eval $FILTER 2>$TMPDIR/exec.out ; then
341 MSG=`tail -1 $TMPDIR/exec.out`
342 if [ -z "$MSG" ] ; then MSG="Filter failed" ; fi
349 # Running of test program with file input/output
351 function test-run-file
355 BOXOPTS="`eval echo $TEST_SANDBOX_OPTS`$BOX_EXTRAS"
356 echo "Sandbox options: $BOXOPTS"
357 if ! $BOXCMD $BOXOPTS -- ./$PROBLEM 2>$TMPDIR/exec.out ; then
359 MSG=`tail -1 $TMPDIR/exec.out`
366 # Running of interactive test programs
368 function test-run-interactive
372 BOXOPTS="`eval echo $TEST_SANDBOX_OPTS`$BOX_EXTRAS"
373 echo "Sandbox options: $BOXOPTS"
374 ICCMD=`eval echo $IA_CHECK`
375 echo "Interactive checker: $ICCMD"
376 if ! $HDIR/bin/iwrapper $BOXCMD $BOXOPTS -- ./$PROBLEM @@ $ICCMD 2>$TMPDIR/exec.out ; then
378 MSG="`head -1 $TMPDIR/exec.out`"
385 # "Running" of open-data problems
387 function test-run-open-data
389 [ -f $SDIR/$TEST.out ] || test-result 0 "No solution"
390 ln $SDIR/$TEST.out $TDIR/$TEST.out
395 function syntax-check
397 [ -n "$SYNTAX_CHECK" ] || return 0
398 [ -z "$EV_NOCHECK" ] || return 0
400 SCHECK=`eval echo \"$SYNTAX_CHECK\"`
401 echo "Syntax check command: $SCHECK"
402 if ! eval $SCHECK 2>$TMPDIR/exec.out ; then
404 MSG=`tail -1 $TMPDIR/exec.out`
405 if [ -z "$MSG" ] ; then MSG="Wrong syntax" ; fi
413 function output-check
416 if [ -n "$OUTPUT_CHECK" -a "$OUT_TYPE" != none -a -z "$EV_NOCHECK" ] ; then
418 [ -f $PDIR/$TEST.out ] && ln $PDIR/$TEST.out $TDIR/$TEST.ok
419 OCHECK=`eval echo \"$OUTPUT_CHECK\"`
420 echo "Output check command: $OCHECK"
421 if ! eval $OCHECK 2>$TMPDIR/exec.out ; then
423 MSG=`tail -1 $TMPDIR/exec.out`
424 if [ -z "$MSG" ] ; then MSG="Wrong answer" ; fi
428 MSG=`tail -1 $TMPDIR/exec.out`
430 if [ -z "$MSG" ] ; then MSG="OK" ; fi
431 test-result $POINTS_PER_TEST "$MSG"
434 # Setup of public commands
436 function public-setup
439 PDIR=$MO_ROOT/problems/$PROBLEM
443 [ -d $PDIR ] || die "Unknown problem $PROBLEM"
445 pstart "Initializing... "
451 BOXCMD="$MO_ROOT/bin/box -c$BOXDIR"
453 pend "OK (see 'check-log' for details)"
456 # Locate output of open data problem, test case TEST
457 # Beware, SDIR and SRCN can contain spaces and other strange user-supplied characters.
461 [ -f $PDIR/$TEST.in ] || die "Unknown test $TEST"
462 if [ -n "$1" ] ; then
466 SRCN=$SDIR/$PROBLEM$TEST.out
468 [ -f "$SDIR/$SRCN" ] || fatal "Output file $SRCN not found"