blob: 49a15a46f5bbd0fd9e482ec84227f6dd2ec2c0bf [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001#!/bin/sh
2#
3# TestRunner.sh - This script is used to run arbitrary unit tests. Unit
4# tests must contain the command used to run them in the input file, starting
5# immediately after a "RUN:" string.
6#
7# This runner recognizes and replaces the following strings in the command:
8#
9# %s - Replaced with the input name of the program, or the program to
10# execute, as appropriate.
11# %llvmgcc - llvm-gcc command
12# %llvmgxx - llvm-g++ command
13# %prcontext - prcontext.tcl script
Gabor Greifa1b615d2008-03-20 22:50:54 +000014# %t - temporary file name (derived from testcase name)
Chris Lattner4b009652007-07-25 00:24:17 +000015#
16
17FILENAME=$1
18TESTNAME=$1
19SUBST=$1
Ted Kremenek5df51ba2007-11-28 19:16:54 +000020
21OUTPUT=Output/$1.out
Chris Lattner4b009652007-07-25 00:24:17 +000022
23# create the output directory if it does not already exist
Ted Kremenek5df51ba2007-11-28 19:16:54 +000024mkdir -p `dirname $OUTPUT` > /dev/null 2>&1
Chris Lattner4b009652007-07-25 00:24:17 +000025
26if test $# != 1; then
27 # If more than one parameter is passed in, there must be three parameters:
28 # The filename to read from (already processed), the command used to execute,
29 # and the file to output to.
30 SUBST=$2
31 OUTPUT=$3
32 TESTNAME=$3
33fi
34
35ulimit -t 40
36
Chris Lattnerc8c6cec2007-12-12 06:19:22 +000037# Verify the script contains a run line.
38grep -q 'RUN:' $FILENAME || (
39 echo "******************** TEST '$TESTNAME' HAS NO RUN LINE! ********************"
40 exit 1
41)
42
Gabor Greifa1b615d2008-03-20 22:50:54 +000043# Run under valgrind if the VG environment variable has been set.
44CLANG="clang"
45if [ -n "$VG" ]; then
46 rm -f $OUTPUT.vg.*
Sam Bishop6fe13172008-03-23 03:42:18 +000047 CLANG="valgrind --leak-check=full --quiet --log-file=$OUTPUT.vg.%p $CLANG"
Gabor Greifa1b615d2008-03-20 22:50:54 +000048fi
49
Chris Lattner4b009652007-07-25 00:24:17 +000050SCRIPT=$OUTPUT.script
Gabor Greif4ca525f2008-03-17 13:45:47 +000051TEMPOUTPUT=$OUTPUT.tmp
Gabor Greifa1b615d2008-03-20 22:50:54 +000052grep 'RUN:' $FILENAME | \
53 sed -e "s|^.*RUN:\(.*\)$|\1|g" \
54 -e "s|%s|$SUBST|g" \
55 -e "s|%llvmgcc|llvm-gcc -emit-llvm|g" \
56 -e "s|%llvmgxx|llvm-g++ -emit-llvm|g" \
57 -e "s|%prcontext|prcontext.tcl|g" \
58 -e "s|%t|$TEMPOUTPUT|g" \
59 -e "s|clang|$CLANG|g" > $SCRIPT
Chris Lattner4b009652007-07-25 00:24:17 +000060
61grep -q XFAIL $FILENAME && (printf "XFAILED '$TESTNAME': "; grep XFAIL $FILENAME)
62
Gabor Greifa1b615d2008-03-20 22:50:54 +000063/bin/sh $SCRIPT > $OUTPUT 2>&1
64SCRIPT_STATUS=$?
65
66if [ -n "$VG" ]; then
67 VG_STATUS=`cat $OUTPUT.vg.* | wc -l`
68else
69 VG_STATUS=0
70fi
71
72if [ $SCRIPT_STATUS -ne 0 -o $VG_STATUS -ne 0 ]; then
Chris Lattner4b009652007-07-25 00:24:17 +000073 echo "******************** TEST '$TESTNAME' FAILED! ********************"
74 echo "Command: "
75 cat $SCRIPT
Gabor Greifa1b615d2008-03-20 22:50:54 +000076 if [ $SCRIPT_STATUS -eq 0 ]; then
77 echo "Output:"
78 else
79 echo "Incorrect Output:"
80 fi
Chris Lattner4b009652007-07-25 00:24:17 +000081 cat $OUTPUT
Gabor Greifa1b615d2008-03-20 22:50:54 +000082 if [ $VG_STATUS -ne 0 ]; then
83 echo "Valgrind Output:"
84 cat $OUTPUT.vg.*
85 fi
Chris Lattner4b009652007-07-25 00:24:17 +000086 echo "******************** TEST '$TESTNAME' FAILED! ********************"
Gabor Greifc6668c02008-03-17 12:35:00 +000087 exit 1
Gabor Greifa1b615d2008-03-20 22:50:54 +000088fi
Chris Lattner4b009652007-07-25 00:24:17 +000089