summaryrefslogtreecommitdiff
path: root/cpp/tests/setup
blob: 40acffd58456ec9955e31048f284ebbe26e4d545 (plain)
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
# -*- sh -*-

test "$VERBOSE" = yes && set -x

pwd=`pwd`
t0=`echo "$0"|sed 's,.*/,,'`.tmp; tmp=$t0/$$
pid=0
test -z "$TEST_DEBUG" &&
trap 's=$?;test $pid = 0||kill -2 $pid;cd "$pwd" && rm -rf $t0 && exit $s' 0
test -z "$TEST_DEBUG" && trap '(exit $?); exit $?' 1 2 13 15

framework_failure=0
mkdir -p $tmp || framework_failure=1
cd $tmp || framework_failure=1

gen_supp=--gen-suppressions=all
# This option makes valgrind significantly slower.
full_leak_check=--leak-check=full

vg_options="
  --suppressions=$abs_srcdir/.vg-supp
  --num-callers=25
  --demangle=no
  --track-fds=yes
  $full_leak_check
  $gen_supp
  "
# configure tests for the existence of valgrind.
# If it's not available, then make $vg and vg_check no-ops.
if test x$VALGRIND = x; then
  vg=
else
  vg="libtool --debug --mode=execute valgrind `echo $vg_options` --"
fi

vg_leak_check()
{
  local file=$1
  local fail
  # If we detect a leak, dump all output to stderr.
  grep -E '^==[0-9]+== +definitely lost: [^0]' $file \
      && { fail=1; cat $file 1>&2;
           echo "found memory leaks (see log file, $file); see above" 1>&2; }
  test "$fail" = ''
}


# Ensure 1) that there is an ERROR SUMMARY line, and
# 2) that the number of errors is 0.
# An offending line looks like this:
# ==29302== ERROR SUMMARY: 4 errors from 2 contexts (suppressed: 16 from 5)
vg_error_check()
{
  local file=$1
  local fail
  # If we detect a leak, dump all output to stderr.
  grep -E '^==[0-9]+== ERROR SUMMARY:' $file > /dev/null \
      || { fail=1; cat $file 1>&2;
           echo "no valgrind ERROR SUMMARY line in $file" 1>&2; }
  if test "$fail" = ''; then
    grep -E '^==[0-9]+== ERROR SUMMARY: [^0] ' $file \
      && { fail=1; cat $file 1>&2;
	   echo "valgrind reported errors in $file; see above" 1>&2; }
  fi
  test "$fail" = ''
}

vg_check()
{
  local file=$1
  if test x$VALGRIND != x; then
    vg_error_check $file && vg_leak_check $file
  fi
}