blob: 658a75d9e489d6c5c82985987fbde4f968f3c979 [file] [log] [blame]
Eric Haszlakiewiczc1b88912012-04-22 10:33:41 -05001#!/bin/sh
Michael Clarkc4dceae2010-10-06 16:39:20 +00002
3# Make sure srcdir is an absolute path. Supply the variable
4# if it does not exist. We want to be able to run the tests
5# stand-alone!!
6#
7srcdir=${srcdir-.}
8if test ! -d $srcdir ; then
9 echo "test-defs.sh: installation error" 1>&2
10 exit 1
11fi
12
13# Use absolute paths
14case "$srcdir" in
15 /* | [A-Za-z]:\\*) ;;
16 *) srcdir=`\cd $srcdir && pwd` ;;
17esac
18
19case "$top_builddir" in
20 /* | [A-Za-z]:\\*) ;;
21 *) top_builddir=`\cd ${top_builddir-..} && pwd` ;;
22esac
23
Eric Haszlakiewiczc1b88912012-04-22 10:33:41 -050024top_builddir=${top_builddir}/tests
25
Michael Clarkc4dceae2010-10-06 16:39:20 +000026progname=`echo "$0" | sed 's,^.*/,,'`
27testname=`echo "$progname" | sed 's,-.*$,,'`
28testsubdir=${testsubdir-testSubDir}
Eric Haszlakiewicz4c7f38e2012-04-28 14:14:26 -050029testsubdir=${testsubdir}/${progname}
Michael Clarkc4dceae2010-10-06 16:39:20 +000030
31# User can set VERBOSE to cause output redirection
32case "$VERBOSE" in
33[Nn]|[Nn][Oo]|0|"")
34 VERBOSE=0
Eric Haszlakiewicz4c7f38e2012-04-28 14:14:26 -050035 exec > /dev/null
Michael Clarkc4dceae2010-10-06 16:39:20 +000036 ;;
37[Yy]|[Yy][Ee][Ss])
38 VERBOSE=1
39 ;;
40esac
41
Eric Haszlakiewicz4c7f38e2012-04-28 14:14:26 -050042rm -rf "$testsubdir" > /dev/null 2>&1
43mkdir -p "$testsubdir"
44CURDIR=$(pwd)
45cd "$testsubdir" \
46 || { echo "Cannot make or change into $testsubdir"; exit 1; }
Michael Clarkc4dceae2010-10-06 16:39:20 +000047
48echo "=== Running test $progname"
49
50CMP="${CMP-cmp}"
51
52use_valgrind=${USE_VALGRIND-1}
53valgrind_path=$(which valgrind 2> /dev/null)
54if [ -z "${valgrind_path}" -o ! -x "${valgrind_path}" ] ; then
55 use_valgrind=0
56fi
57
58#
59# This is a common function to check the results of a test program
60# that is intended to generate consistent output across runs.
61#
62# ${top_builddir} must be set to the top level build directory.
63#
64# Output will be written to the current directory.
65#
66# It must be passed the name of the test command to run, which must be present
67# in the ${top_builddir} directory.
68#
69# It will compare the output of running that against <name of command>.expected
70#
71run_output_test()
72{
Eric Haszlakiewicz4c7f38e2012-04-28 14:14:26 -050073 if [ "$1" = "-o" ] ; then
74 TEST_OUTPUT="$2"
75 shift
76 shift
77 fi
Michael Clarkc4dceae2010-10-06 16:39:20 +000078 TEST_COMMAND="$1"
Eric Haszlakiewicz4c7f38e2012-04-28 14:14:26 -050079 shift
80 if [ -z "${TEST_OUTPUT}" ] ; then
81 TEST_OUTPUT=${TEST_COMMAND}
82 fi
Michael Clarkc4dceae2010-10-06 16:39:20 +000083
Eric Haszlakiewicz4c7f38e2012-04-28 14:14:26 -050084 REDIR_OUTPUT="> \"${TEST_OUTPUT}.out\""
Michael Clarkc4dceae2010-10-06 16:39:20 +000085 if [ $VERBOSE -gt 1 ] ; then
Eric Haszlakiewicz4c7f38e2012-04-28 14:14:26 -050086 REDIR_OUTPUT="| tee \"${TEST_OUTPUT}.out\""
Michael Clarkc4dceae2010-10-06 16:39:20 +000087 fi
88
89 if [ $use_valgrind -eq 1 ] ; then
90 eval valgrind --tool=memcheck \
91 --trace-children=yes \
92 --demangle=yes \
Eric Haszlakiewicz4c7f38e2012-04-28 14:14:26 -050093 --log-file="${TEST_OUTPUT}.vg.out" \
Michael Clarkc4dceae2010-10-06 16:39:20 +000094 --leak-check=full \
95 --show-reachable=yes \
96 --run-libc-freeres=yes \
Eric Haszlakiewicz4c7f38e2012-04-28 14:14:26 -050097 "\"${top_builddir}/${TEST_COMMAND}\"" \"\$@\" ${REDIR_OUTPUT}
Michael Clarkc4dceae2010-10-06 16:39:20 +000098 err=$?
99
100 else
Eric Haszlakiewicz4c7f38e2012-04-28 14:14:26 -0500101 eval "\"${top_builddir}/${TEST_COMMAND}"\" \"\$@\" ${REDIR_OUTPUT}
Michael Clarkc4dceae2010-10-06 16:39:20 +0000102 err=$?
103 fi
104
105 if [ $err -ne 0 ] ; then
Eric Haszlakiewicz4c7f38e2012-04-28 14:14:26 -0500106 echo "ERROR: \"${TEST_COMMAND} $@\" exited with non-zero exit status: $err" 1>&2
Michael Clarkc4dceae2010-10-06 16:39:20 +0000107 fi
108
109 if [ $use_valgrind -eq 1 ] ; then
Eric Haszlakiewicz4c7f38e2012-04-28 14:14:26 -0500110 if ! tail -1 "${TEST_OUTPUT}.vg.out" | grep -q "ERROR SUMMARY: 0 errors" ; then
Michael Clarkc4dceae2010-10-06 16:39:20 +0000111 echo "ERROR: valgrind found errors during execution:" 1>&2
Eric Haszlakiewicz4c7f38e2012-04-28 14:14:26 -0500112 cat "${TEST_OUTPUT}.vg.out"
Michael Clarkc4dceae2010-10-06 16:39:20 +0000113 err=1
114 fi
115 fi
116
Eric Haszlakiewicz4c7f38e2012-04-28 14:14:26 -0500117 if ! "$CMP" -s "${top_builddir}/${TEST_OUTPUT}.expected" "${TEST_OUTPUT}.out" ; then
118 echo "ERROR: \"${TEST_COMMAND} $@\" (${TEST_OUTPUT}) failed (set VERBOSE=1 to see full output):" 1>&2
119 (cd "${CURDIR}" ; set -x ; diff "${top_builddir}/${TEST_OUTPUT}.expected" "$testsubdir/${TEST_OUTPUT}.out")
120 echo "cp \"$testsubdir/${TEST_OUTPUT}.out\" \"${top_builddir}/${TEST_OUTPUT}.expected\"" 1>&2
121
Michael Clarkc4dceae2010-10-06 16:39:20 +0000122 err=1
123 fi
124
125 return $err
126}
127
128