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