blob: 2014b37a51834de6e1c4990d8571ebcffa70c22d [file] [log] [blame]
Michael Clarkc4dceae2010-10-06 16:39:20 +00001
2#! /bin/sh
3
4# Make sure srcdir is an absolute path. Supply the variable
5# if it does not exist. We want to be able to run the tests
6# stand-alone!!
7#
8srcdir=${srcdir-.}
9if test ! -d $srcdir ; then
10 echo "test-defs.sh: installation error" 1>&2
11 exit 1
12fi
13
14# Use absolute paths
15case "$srcdir" in
16 /* | [A-Za-z]:\\*) ;;
17 *) srcdir=`\cd $srcdir && pwd` ;;
18esac
19
20case "$top_builddir" in
21 /* | [A-Za-z]:\\*) ;;
22 *) top_builddir=`\cd ${top_builddir-..} && pwd` ;;
23esac
24
25progname=`echo "$0" | sed 's,^.*/,,'`
26testname=`echo "$progname" | sed 's,-.*$,,'`
27testsubdir=${testsubdir-testSubDir}
28
29# User can set VERBOSE to cause output redirection
30case "$VERBOSE" in
31[Nn]|[Nn][Oo]|0|"")
32 VERBOSE=0
33 exec > /dev/null 2>&1
34 ;;
35[Yy]|[Yy][Ee][Ss])
36 VERBOSE=1
37 ;;
38esac
39
40rm -rf "$testsubdir/$progname" > /dev/null 2>&1
41mkdir -p "$testsubdir/$progname"
42cd "$testsubdir/$progname" \
43 || { echo "Cannot make or change into $testsubdir/$progname"; exit 1; }
44
45echo "=== Running test $progname"
46
47CMP="${CMP-cmp}"
48
49use_valgrind=${USE_VALGRIND-1}
50valgrind_path=$(which valgrind 2> /dev/null)
51if [ -z "${valgrind_path}" -o ! -x "${valgrind_path}" ] ; then
52 use_valgrind=0
53fi
54
55#
56# This is a common function to check the results of a test program
57# that is intended to generate consistent output across runs.
58#
59# ${top_builddir} must be set to the top level build directory.
60#
61# Output will be written to the current directory.
62#
63# It must be passed the name of the test command to run, which must be present
64# in the ${top_builddir} directory.
65#
66# It will compare the output of running that against <name of command>.expected
67#
68run_output_test()
69{
70 TEST_COMMAND="$1"
71
72 REDIR_OUTPUT="> \"${TEST_COMMAND}.out\""
73 if [ $VERBOSE -gt 1 ] ; then
74 REDIR_OUTPUT="| tee \"${TEST_COMMAND}.out\""
75 fi
76
77 if [ $use_valgrind -eq 1 ] ; then
78 eval valgrind --tool=memcheck \
79 --trace-children=yes \
80 --demangle=yes \
81 --log-file=vg.out \
82 --leak-check=full \
83 --show-reachable=yes \
84 --run-libc-freeres=yes \
85 "\"${top_builddir}/${TEST_COMMAND}\"" ${REDIR_OUTPUT}
86 err=$?
87
88 else
89 eval "\"${top_builddir}/${TEST_COMMAND}"\" ${REDIR_OUTPUT}
90 err=$?
91 fi
92
93 if [ $err -ne 0 ] ; then
94 echo "ERROR: ${TEST_COMMAND} exited with non-zero exit status: $err" 1>&2
95 fi
96
97 if [ $use_valgrind -eq 1 ] ; then
98 if ! tail -1 "vg.out" | grep -q "ERROR SUMMARY: 0 errors" ; then
99 echo "ERROR: valgrind found errors during execution:" 1>&2
100 cat vg.out
101 err=1
102 fi
103 fi
104
105 if ! "$CMP" -s "${top_builddir}/${TEST_COMMAND}.expected" "${TEST_COMMAND}.out" ; then
106 echo "ERROR: ${TEST_COMMAND} failed:" 1>&2
107 diff "${top_builddir}/${TEST_COMMAND}.expected" "${TEST_COMMAND}.out" 1>&2
108 err=1
109 fi
110
111 return $err
112}
113
114