1#!/bin/sh
2
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
24top_builddir=${top_builddir}/tests
25
26progname=`echo "$0" | sed 's,^.*/,,'`
27testname=`echo "$progname" | sed 's,-.*$,,'`
28testsubdir=${testsubdir-testSubDir}
29testsubdir=${testsubdir}/${progname}
30
31# User can set VERBOSE to cause output redirection
32case "$VERBOSE" in
33[Nn]|[Nn][Oo]|0|"")
34	VERBOSE=0
35	exec > /dev/null
36	;;
37[Yy]|[Yy][Ee][Ss])
38	VERBOSE=1
39	;;
40esac
41
42rm -rf "$testsubdir" > /dev/null 2>&1
43mkdir -p "$testsubdir"
44CURDIR=$(pwd)
45cd "$testsubdir" \
46   || { echo "Cannot make or change into $testsubdir"; exit 1; }
47
48echo "=== Running test $progname"
49
50CMP="${CMP-cmp}"
51
52use_valgrind=${USE_VALGRIND-1}
53case "${use_valgrind}" in
54	[0Nn]*)
55		use_valgrind=0
56		;;
57	*)
58		use_valgrind=1
59		;;
60esac
61valgrind_path=$(which valgrind 2> /dev/null)
62if [ -z "${valgrind_path}" -o ! -x "${valgrind_path}" ] ; then
63	use_valgrind=0
64fi
65
66#
67# This is a common function to check the results of a test program
68# that is intended to generate consistent output across runs.
69#
70# ${top_builddir} must be set to the top level build directory.
71#
72# Output will be written to the current directory.
73#
74# It must be passed the name of the test command to run, which must be present
75#  in the ${top_builddir} directory.
76#
77# It will compare the output of running that against <name of command>.expected
78#
79run_output_test()
80{
81	if [ "$1" = "-o" ] ; then
82		TEST_OUTPUT="$2"
83		shift
84		shift
85	fi
86	TEST_COMMAND="$1"
87	shift
88	if [ -z "${TEST_OUTPUT}" ] ; then
89		TEST_OUTPUT=${TEST_COMMAND}
90	fi
91
92	REDIR_OUTPUT="> \"${TEST_OUTPUT}.out\""
93	if [ $VERBOSE -gt 1 ] ; then
94		REDIR_OUTPUT="| tee \"${TEST_OUTPUT}.out\""
95	fi
96
97	if [ $use_valgrind -eq 1 ] ; then
98		eval valgrind --tool=memcheck \
99			--trace-children=yes \
100			--demangle=yes \
101			--log-file="${TEST_OUTPUT}.vg.out" \
102			--leak-check=full \
103			--show-reachable=yes \
104			--run-libc-freeres=yes \
105		"\"${top_builddir}/${TEST_COMMAND}\"" \"\$@\" ${REDIR_OUTPUT}
106		err=$?
107
108	else
109		eval "\"${top_builddir}/${TEST_COMMAND}"\" \"\$@\" ${REDIR_OUTPUT}
110		err=$?
111	fi
112
113	if [ $err -ne 0 ] ; then
114		echo "ERROR: \"${TEST_COMMAND} $@\" exited with non-zero exit status: $err" 1>&2
115	fi
116
117	if [ $use_valgrind -eq 1 ] ; then
118		if ! tail -1 "${TEST_OUTPUT}.vg.out" | grep -q "ERROR SUMMARY: 0 errors" ; then
119			echo "ERROR: valgrind found errors during execution:" 1>&2
120			cat "${TEST_OUTPUT}.vg.out"
121			err=1
122		fi
123	fi
124
125	if ! "$CMP" -s "${srcdir}/${TEST_OUTPUT}.expected" "${TEST_OUTPUT}.out" ; then
126		echo "ERROR: \"${TEST_COMMAND} $@\" (${TEST_OUTPUT}) failed (set VERBOSE=1 to see full output):" 1>&2
127		(cd "${CURDIR}" ; set -x ; diff "${srcdir}/${TEST_OUTPUT}.expected" "$testsubdir/${TEST_OUTPUT}.out")
128		echo "cp \"$testsubdir/${TEST_OUTPUT}.out\" \"${srcdir}/${TEST_OUTPUT}.expected\"" 1>&2
129
130		err=1
131	fi
132
133	return $err
134}
135