blob: 767d9660d584331d3826495e25ca5b1133ae4579 [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.
Daniel Dunbar5e380732008-07-25 01:05:44 +000044CLANG=$CLANG
Eli Friedmanfde9fe72008-07-27 05:05:07 +000045if [ ! -n "$CLANG" ]; then
Daniel Dunbar5e380732008-07-25 01:05:44 +000046 CLANG="clang"
47fi
Gabor Greif1e2db032008-03-20 22:50:54 +000048if [ -n "$VG" ]; then
49 rm -f $OUTPUT.vg.*
Sam Bishop4cc34cb2008-03-23 03:42:18 +000050 CLANG="valgrind --leak-check=full --quiet --log-file=$OUTPUT.vg.%p $CLANG"
Gabor Greif1e2db032008-03-20 22:50:54 +000051fi
52
Reid Spencer5f016e22007-07-11 17:01:13 +000053SCRIPT=$OUTPUT.script
Gabor Greif0f233032008-03-17 13:45:47 +000054TEMPOUTPUT=$OUTPUT.tmp
Gabor Greif1e2db032008-03-20 22:50:54 +000055grep 'RUN:' $FILENAME | \
56 sed -e "s|^.*RUN:\(.*\)$|\1|g" \
57 -e "s|%s|$SUBST|g" \
58 -e "s|%llvmgcc|llvm-gcc -emit-llvm|g" \
59 -e "s|%llvmgxx|llvm-g++ -emit-llvm|g" \
60 -e "s|%prcontext|prcontext.tcl|g" \
61 -e "s|%t|$TEMPOUTPUT|g" \
62 -e "s|clang|$CLANG|g" > $SCRIPT
Reid Spencer5f016e22007-07-11 17:01:13 +000063
64grep -q XFAIL $FILENAME && (printf "XFAILED '$TESTNAME': "; grep XFAIL $FILENAME)
65
Gabor Greif1e2db032008-03-20 22:50:54 +000066/bin/sh $SCRIPT > $OUTPUT 2>&1
67SCRIPT_STATUS=$?
68
69if [ -n "$VG" ]; then
70 VG_STATUS=`cat $OUTPUT.vg.* | wc -l`
71else
72 VG_STATUS=0
73fi
74
75if [ $SCRIPT_STATUS -ne 0 -o $VG_STATUS -ne 0 ]; then
Reid Spencer5f016e22007-07-11 17:01:13 +000076 echo "******************** TEST '$TESTNAME' FAILED! ********************"
77 echo "Command: "
78 cat $SCRIPT
Gabor Greif1e2db032008-03-20 22:50:54 +000079 if [ $SCRIPT_STATUS -eq 0 ]; then
80 echo "Output:"
81 else
82 echo "Incorrect Output:"
83 fi
Reid Spencer5f016e22007-07-11 17:01:13 +000084 cat $OUTPUT
Gabor Greif1e2db032008-03-20 22:50:54 +000085 if [ $VG_STATUS -ne 0 ]; then
86 echo "Valgrind Output:"
87 cat $OUTPUT.vg.*
88 fi
Reid Spencer5f016e22007-07-11 17:01:13 +000089 echo "******************** TEST '$TESTNAME' FAILED! ********************"
Gabor Greif5ca1b5a2008-03-17 12:35:00 +000090 exit 1
Gabor Greif1e2db032008-03-20 22:50:54 +000091fi
Reid Spencer5f016e22007-07-11 17:01:13 +000092