Imported Upstream version 0.10
[platform/upstream/json-c.git] / tests / test-defs.sh
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 #
7 srcdir=${srcdir-.}
8 if test ! -d $srcdir ; then
9     echo "test-defs.sh: installation error" 1>&2
10     exit 1
11 fi
12
13 # Use absolute paths
14 case "$srcdir" in
15     /* | [A-Za-z]:\\*) ;;
16     *) srcdir=`\cd $srcdir && pwd` ;;
17 esac
18
19 case "$top_builddir" in
20     /* | [A-Za-z]:\\*) ;;
21     *) top_builddir=`\cd ${top_builddir-..} && pwd` ;;
22 esac
23
24 top_builddir=${top_builddir}/tests
25
26 progname=`echo "$0" | sed 's,^.*/,,'`
27 testname=`echo "$progname" | sed 's,-.*$,,'`
28 testsubdir=${testsubdir-testSubDir}
29 testsubdir=${testsubdir}/${progname}
30
31 # User can set VERBOSE to cause output redirection
32 case "$VERBOSE" in
33 [Nn]|[Nn][Oo]|0|"")
34         VERBOSE=0
35         exec > /dev/null
36         ;;
37 [Yy]|[Yy][Ee][Ss])
38         VERBOSE=1
39         ;;
40 esac
41
42 rm -rf "$testsubdir" > /dev/null 2>&1
43 mkdir -p "$testsubdir"
44 CURDIR=$(pwd)
45 cd "$testsubdir" \
46    || { echo "Cannot make or change into $testsubdir"; exit 1; }
47
48 echo "=== Running test $progname"
49
50 CMP="${CMP-cmp}"
51
52 use_valgrind=${USE_VALGRIND-1}
53 valgrind_path=$(which valgrind 2> /dev/null)
54 if [ -z "${valgrind_path}" -o ! -x "${valgrind_path}" ] ; then
55         use_valgrind=0
56 fi
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 #
71 run_output_test()
72 {
73         if [ "$1" = "-o" ] ; then
74                 TEST_OUTPUT="$2"
75                 shift
76                 shift
77         fi
78         TEST_COMMAND="$1"
79         shift
80         if [ -z "${TEST_OUTPUT}" ] ; then       
81                 TEST_OUTPUT=${TEST_COMMAND}
82         fi
83
84         REDIR_OUTPUT="> \"${TEST_OUTPUT}.out\""
85         if [ $VERBOSE -gt 1 ] ; then
86                 REDIR_OUTPUT="| tee \"${TEST_OUTPUT}.out\""
87         fi
88
89         if [ $use_valgrind -eq 1 ] ; then
90                 eval valgrind --tool=memcheck \
91                         --trace-children=yes \
92                         --demangle=yes \
93                         --log-file="${TEST_OUTPUT}.vg.out" \
94                         --leak-check=full \
95                         --show-reachable=yes \
96                         --run-libc-freeres=yes \
97                 "\"${top_builddir}/${TEST_COMMAND}\"" \"\$@\" ${REDIR_OUTPUT}
98                 err=$?
99
100         else
101                 eval "\"${top_builddir}/${TEST_COMMAND}"\" \"\$@\" ${REDIR_OUTPUT}
102                 err=$?
103         fi
104
105         if [ $err -ne 0 ] ; then
106                 echo "ERROR: \"${TEST_COMMAND} $@\" exited with non-zero exit status: $err" 1>&2
107         fi
108
109         if [ $use_valgrind -eq 1 ] ; then
110                 if ! tail -1 "${TEST_OUTPUT}.vg.out" | grep -q "ERROR SUMMARY: 0 errors" ; then
111                         echo "ERROR: valgrind found errors during execution:" 1>&2
112                         cat "${TEST_OUTPUT}.vg.out"
113                         err=1
114                 fi
115         fi
116
117         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
122                 err=1
123         fi
124
125         return $err
126 }
127
128