chiark / gitweb /
debugging for thing that crashed
[inn-innduct.git] / tests / lib / xmalloc.t
1 #! /bin/sh
2 # $Id: xmalloc.t 5379 2002-03-31 21:45:12Z rra $
3 #
4 # Test suite for xmalloc and friends.
5
6 # The count starts at 1 and is updated each time ok is printed.  printcount
7 # takes "ok" or "not ok".
8 count=1
9 printcount () {
10     echo "$1 $count $2"
11     count=`expr $count + 1`
12 }
13
14 # Run a program expected to succeed, and print ok if it does.
15 runsuccess () {
16     output=`$xmalloc "$1" "$2" "$3" 2>&1 >/dev/null`
17     status=$?
18     if test $status = 0 && test -z "$output" ; then
19         printcount "ok"
20     else
21         if test $status = 2 ; then
22             printcount "ok" "# skip - no data limit support"
23         else
24             printcount "not ok"
25             echo "  $output"
26         fi
27     fi
28 }
29
30 # Run a program expected to fail and make sure it fails with an exit status
31 # of 2 and the right failure message.  Strip the colon and everything after
32 # it off the error message since it's system-specific.
33 runfailure () {
34     output=`$xmalloc "$1" "$2" "$3" 2>&1 >/dev/null`
35     status=$?
36     output=`echo "$output" | sed 's/:.*//'`
37     if test $status = 1 && test x"$output" = x"$4" ; then
38         printcount "ok"
39     else
40         if test $status = 2 ; then
41             printcount "ok" "# skip - no data limit support"
42         else
43             printcount "not ok"
44             echo "  saw: $output"
45             echo "  not: $4"
46         fi
47     fi
48 }
49
50 # Find where the helper program is.
51 xmalloc=xmalloc
52 for file in ./xmalloc lib/xmalloc ../xmalloc ; do
53     [ -x $file ] && xmalloc=$file
54 done
55
56 # Total tests.
57 echo 26
58
59 # First run the tests expected to succeed.
60 runsuccess "m" "21"     "0"
61 runsuccess "m" "128000" "0"
62 runsuccess "m" "0"      "0"
63 runsuccess "r" "21"     "0"
64 runsuccess "r" "128000" "0"
65 runsuccess "s" "21"     "0"
66 runsuccess "s" "128000" "0"
67 runsuccess "n" "21"     "0"
68 runsuccess "n" "128000" "0"
69 runsuccess "c" "24"     "0"
70 runsuccess "c" "128000" "0"
71
72 # Now limit our memory to 96KB and then try the large ones again, all of
73 # which should fail.
74 runfailure "m" "128000" "96000" \
75     "failed to malloc 128000 bytes at lib/xmalloc.c line 36"
76 runfailure "r" "128000" "96000" \
77     "failed to realloc 128000 bytes at lib/xmalloc.c line 62"
78 runfailure "s" "64000"  "96000" \
79     "failed to strdup 64000 bytes at lib/xmalloc.c line 91"
80 runfailure "n" "64000"  "96000" \
81     "failed to strndup 64000 bytes at lib/xmalloc.c line 115"
82 runfailure "c" "128000" "96000" \
83     "failed to calloc 128000 bytes at lib/xmalloc.c line 137"
84
85 # Check our custom error handler.
86 runfailure "M" "128000" "96000" "malloc 128000 lib/xmalloc.c 36"
87 runfailure "R" "128000" "96000" "realloc 128000 lib/xmalloc.c 62"
88 runfailure "S" "64000"  "96000" "strdup 64000 lib/xmalloc.c 91"
89 runfailure "N" "64000"  "96000" "strndup 64000 lib/xmalloc.c 115"
90 runfailure "C" "128000" "96000" "calloc 128000 lib/xmalloc.c 137"
91
92 # Check the smaller ones again just for grins.
93 runsuccess "m" "21" "96000"
94 runsuccess "r" "32" "96000"
95 runsuccess "s" "64" "96000"
96 runsuccess "n" "20" "96000"
97 runsuccess "c" "24" "96000"