1 # The Evaluator -- Shell Function Library
2 # (c) 2001--2007 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
56 pstart "Preparing sandbox... "
57 if [ -z "$TEST_USER" -o "$TEST_USER" == $EVAL_USER ] ; then
58 pcont "running locally (INSECURE), "
64 pcont "used account $TEST_USER, "
65 BOXDIR=$MO_ROOT/eval/$TEST_USER
66 BOXCMD=bin/box-$TEST_USER
68 [ -d $BOXDIR -a -f $BOXCMD ] || die "Sandbox set up incorrectly"
69 BOXCMD="$BOXCMD -c$BOXDIR"
70 echo "Sandbox directory: $BOXDIR"
71 echo "Sandbox command: $BOXCMD"
78 [ -n "$BOXCMD" ] || die "box-init not called"
82 # Initialization of testing directories
86 pstart "Initializing... "
88 PDIR=problems/$PROBLEM
89 SDIR=solutions/$CONTESTANT/$PROBLEM
90 TDIR=testing/$CONTESTANT/$PROBLEM
92 [ -d $PDIR ] || die "Problem $PROBLEM not known"
93 [ -d $SDIR ] || fatal "Solution of $PROBLEM not found"
94 mkdir -p $TDIR $TMPDIR
96 mkdir -p $TDIR $TMPDIR
98 Testing solution of $PROBLEM by $CONTESTANT
99 Test started at `date`
100 Contestant's solution directory: $SDIR
101 Problem directory: $PDIR
102 Testing directory: $TDIR
107 # Locate source file.
108 # If no parameter is given, locate it in SDIR and return name as SRCN and extension as SRCEXT
109 # Or a file name can be given and then SDIR, SRCN and SRCEXT are set.
110 # Beware, SDIR and SRCN can contain spaces and other strange user-supplied characters.
112 function locate-source
114 pstart "Finding source... "
116 if [ -n "$1" ] ; then
118 local S=`basename "$1"`
119 SBASE=$(echo "$S" | sed 's/\.\([^.]\+\)//')
120 SRCEXT=$(echo "$S" | sed '/\./!d; s/.*\.\([^.]\+\)/\1/')
121 if [ -n "$SRCEXT" ] ; then
122 # Full name given, so just check the extension and existence
124 [ -f "$SDIR/$SRCN" ] || die "Cannot find source file $SDIR/$SRCN"
126 for a in $EXTENSIONS ; do
127 if [ $a == $SRCEXT ] ; then
129 echo "Explicitly set source file: $SDIR/$SRCN"
133 die "Unknown extension .$SRCEXT"
138 for a in $EXTENSIONS ; do
139 if [ -f "$SDIR/$SBASE.$a" ] ; then
140 [ -z "$SRCN" ] || die "Multiple source files found: $SDIR/$PROBLEM.$a and $SDIR/$SRCN. Please fix."
145 [ -n "$SRCN" ] || fatal "NOT FOUND"
147 echo "Found source file: $SDIR/$SRCN"
150 # Compilation (compile SDIR/SRCN with PDIR/COMP_EXTRAS to EXE=TDIR/PROBLEM)
154 pstart "Compiling... "
155 # Beware, the original SRCN can be a strange user-supplied name
157 cp "$SDIR/$SRCN" $TDIR/$SRC
158 if [ -n "$COMP_EXTRAS" ] ; then
159 echo "Extras: $COMP_EXTRAS"
160 for a in $COMP_EXTRAS ; do cp $PDIR/$a $TDIR/ ; done
163 for a in $SRC $COMP_EXTRAS ; do cp $TDIR/$a $BOXDIR/ ; done
166 CCMD=`eval echo ${!CCMD}`
167 COMP_SANDBOX_OPTS=`eval echo $COMP_SANDBOX_OPTS`
168 echo "Compiler command: $CCMD"
169 echo "Compiler sandbox options: $COMP_SANDBOX_OPTS"
170 eval $COMP_SANDBOX_INIT
172 echo "Compiler input files:"
174 echo "Compiler output:"
175 if ! $BOXCMD $COMP_SANDBOX_OPTS -- $CCMD 2>$TDIR/compile.out ; then
176 COMPILE_MSG="`cat $TDIR/compile.out`"
177 pend "FAILED: $COMPILE_MSG"
181 cat $TDIR/compile.out
183 echo "Compiler output files:"
185 if [ ! -f $BOXDIR/$PROBLEM ] ; then
186 pend "FAILED: Missing executable file"
187 echo "Missing executable file"
191 cp -a $BOXDIR/$PROBLEM $EXE
192 echo "Compiled OK, result copied to $EXE"
196 # Running of test program according to current task type (returns exit code and TEST_MSG)
207 if [ -s $TDIR/$TEST.pts ] ; then
208 P=`cat $TDIR/$TEST.pts`
212 # Translate signal numbers to readable strings
213 SG=${M#Caught fatal signal }
214 SG=${SG#Committed suicide by signal }
215 if [ "$SG" != "$M" ] ; then
216 SG=`perl -MConfig -e '@s=split / /,$Config{sig_name}; print $s[$ARGV[0]]' $SG`
217 [ -z "$SG" ] || M="$M (SIG$SG)"
220 # Translate Free Pascal runtime errors to readable strings
221 RE=${M#Exited with error status }
222 if [ "$FREE_PASCAL_RTE" == 1 -a "$RE" != "$M" ] ; then
223 N="Runtime error $RE"
225 200) M="$N: Division by zero" ;;
226 201) M="$N: Range check error" ;;
227 202) M="$N: Stack overflow" ;;
228 203) M="$N: Heap overflow" ;;
229 205) M="$N: Floating point overflow" ;;
230 215) M="$N: Arithmetic overflow" ;;
231 216) M="$N: Segmentation fault" ;;
245 echo "Executable file: $TDIR/$PROBLEM"
246 if [ ! -x $TDIR/$PROBLEM ] ; then
247 test-result 0 "Compile error"
249 cp $TDIR/$PROBLEM $BOXDIR/
251 IN_TYPE=${IN_TYPE:-$IO_TYPE}
252 OUT_TYPE=${OUT_TYPE:-$IO_TYPE}
254 file) echo "Input file: $PROBLEM.in (from $PDIR/$TEST.in)"
255 try-ln $PDIR/$TEST.in $TDIR/$TEST.in
256 cp $PDIR/$TEST.in $BOXDIR/$PROBLEM.in
257 [ $TASK_TYPE == interactive ] || BOX_EXTRAS="$BOX_EXTRAS -i/dev/null"
259 stdio) echo "Input file: <stdin> (from $PDIR/$TEST.in)"
260 try-ln $PDIR/$TEST.in $TDIR/$TEST.in
261 cp $PDIR/$TEST.in $BOXDIR/.stdin
262 BOX_EXTRAS="$BOX_EXTRAS -i.stdin"
264 none) echo "Input file: <none>"
266 *) die "Unknown IN_TYPE $IN_TYPE"
269 if [ -n "$EV_PEDANT" -a $IN_TYPE != none ] ; then
271 bin/pedant <$TDIR/$TEST.in | tr '\n' ' ' >&2
274 file) echo "Output file: $PROBLEM.out"
275 [ $TASK_TYPE == interactive ] || BOX_EXTRAS="$BOX_EXTRAS -o/dev/null"
277 stdio) echo "Output file: <stdout>"
278 BOX_EXTRAS="$BOX_EXTRAS -o.stdout"
280 none) echo "Output file: <none>"
282 *) die "Unknown OUT_TYPE $OUT_TYPE"
285 echo "Timeout: $TIME_LIMIT s"
286 echo "Memory: $MEM_LIMIT KB"
288 echo "Sandbox contents before start:"
294 echo "Sandbox contents after exit:"
296 case ${OUT_TYPE:-$IO_TYPE} in
297 file) [ -f $BOXDIR/$PROBLEM.out ] || test-result 0 "No output file"
298 cp $BOXDIR/$PROBLEM.out $TDIR/$TEST.out
300 stdio) [ -f $BOXDIR/.stdout ] || test-result 0 "No output file"
301 cp $BOXDIR/.stdout $TDIR/$TEST.out
306 # Running of test program with file input/output
308 function test-run-file
312 BOXOPTS="`eval echo $TEST_SANDBOX_OPTS`$BOX_EXTRAS"
313 echo "Sandbox options: $BOXOPTS"
314 if ! $BOXCMD $BOXOPTS -- ./$PROBLEM 2>$TMPDIR/exec.out ; then
316 MSG=`tail -1 $TMPDIR/exec.out`
323 # Running of interactive test programs
325 function test-run-interactive
329 BOXOPTS="`eval echo $TEST_SANDBOX_OPTS`$BOX_EXTRAS"
330 echo "Sandbox options: $BOXOPTS"
331 ICCMD=`eval echo $IA_CHECK`
332 echo "Interactive checker: $ICCMD"
333 if ! $HDIR/bin/iwrapper $BOXCMD $BOXOPTS -- ./$PROBLEM @@ $ICCMD 2>$TMPDIR/exec.out ; then
335 MSG="`head -1 $TMPDIR/exec.out`"
342 # "Running" of open-data problems
344 function test-run-open-data
346 [ -f $SDIR/$TEST.out ] || test-result 0 "No solution"
347 ln $SDIR/$TEST.out $TDIR/$TEST.out
352 function syntax-check
354 [ -n "$SYNTAX_CHECK" ] || return 0
355 [ -z "$EV_NOCHECK" ] || return 0
357 SCHECK=`eval echo $SYNTAX_CHECK`
358 echo "Syntax check command: $SCHECK"
359 if ! eval $SCHECK 2>$TMPDIR/exec.out ; then
361 MSG=`tail -1 $TMPDIR/exec.out`
362 if [ -z "$MSG" ] ; then MSG="Wrong syntax" ; fi
370 function output-check
373 if [ -n "$OUTPUT_CHECK" -a "$OUT_TYPE" != none -a -z "$EV_NOCHECK" ] ; then
375 [ -f $PDIR/$TEST.out ] && ln $PDIR/$TEST.out $TDIR/$TEST.ok
376 OCHECK=`eval echo $OUTPUT_CHECK`
377 echo "Output check command: $OCHECK"
378 if ! eval $OCHECK 2>$TMPDIR/exec.out ; then
380 MSG=`tail -1 $TMPDIR/exec.out`
381 if [ -z "$MSG" ] ; then MSG="Wrong answer" ; fi
385 MSG=`tail -1 $TMPDIR/exec.out`
387 if [ -z "$MSG" ] ; then MSG="OK" ; fi
388 test-result $POINTS_PER_TEST "$MSG"
391 # Setup of public commands
393 function public-setup
396 PDIR=$MO_ROOT/problems/$PROBLEM
400 [ -d $PDIR ] || die "Unknown problem $PROBLEM"
402 pstart "Initializing... "
408 BOXCMD="$MO_ROOT/bin/box -c$BOXDIR"
410 pend "OK (see 'check-log' for details)"
413 # Locate output of open data problem, test case TEST
414 # Beware, SDIR and SRCN can contain spaces and other strange user-supplied characters.
418 [ -f $PDIR/$TEST.in ] || die "Unknown test $TEST"
419 if [ -n "$1" ] ; then
423 SRCN=$SDIR/$PROBLEM$TEST.out
425 [ -f "$SDIR/$SRCN" ] || fatal "Output file $SRCN not found"