blob: 49a15a46f5bbd0fd9e482ec84227f6dd2ec2c0bf [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +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 Greif1e2db032008-03-20 22:50:54 +000014# %t - temporary file name (derived from testcase name)
Reid Spencer5f016e22007-07-11 17:01:13 +000015#
16
17FILENAME=$1
18TESTNAME=$1
19SUBST=$1
Ted Kremenek6a18c762007-11-28 19:16:54 +000020
21OUTPUT=Output/$1.out
Reid Spencer5f016e22007-07-11 17:01:13 +000022
23# create the output directory if it does not already exist
Ted Kremenek6a18c762007-11-28 19:16:54 +000024mkdir -p `dirname $OUTPUT` > /dev/null 2>&1
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner32a39992007-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 Greif1e2db032008-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 Bishop4cc34cb2008-03-23 03:42:18 +000047 CLANG="valgrind --leak-check=full --quiet --log-file=$OUTPUT.vg.%p $CLANG"
Gabor Greif1e2db032008-03-20 22:50:54 +000048fi
49
Reid Spencer5f016e22007-07-11 17:01:13 +000050SCRIPT=$OUTPUT.script
Gabor Greif0f233032008-03-17 13:45:47 +000051TEMPOUTPUT=$OUTPUT.tmp
Gabor Greif1e2db032008-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
Reid Spencer5f016e22007-07-11 17:01:13 +000060
61grep -q XFAIL $FILENAME && (printf "XFAILED '$TESTNAME': "; grep XFAIL $FILENAME)
62
Gabor Greif1e2db032008-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
Reid Spencer5f016e22007-07-11 17:01:13 +000073 echo "******************** TEST '$TESTNAME' FAILED! ********************"
74 echo "Command: "
75 cat $SCRIPT
Gabor Greif1e2db032008-03-20 22:50:54 +000076 if [ $SCRIPT_STATUS -eq 0 ]; then
77 echo "Output:"
78 else
79 echo "Incorrect Output:"
80 fi
Reid Spencer5f016e22007-07-11 17:01:13 +000081 cat $OUTPUT
Gabor Greif1e2db032008-03-20 22:50:54 +000082 if [ $VG_STATUS -ne 0 ]; then
83 echo "Valgrind Output:"
84 cat $OUTPUT.vg.*
85 fi
Reid Spencer5f016e22007-07-11 17:01:13 +000086 echo "******************** TEST '$TESTNAME' FAILED! ********************"
Gabor Greif5ca1b5a2008-03-17 12:35:00 +000087 exit 1
Gabor Greif1e2db032008-03-20 22:50:54 +000088fi
Reid Spencer5f016e22007-07-11 17:01:13 +000089