Debian Codegraph @

test/fpe_test

raw
1 #!/bin/sh
2 # $MawkId: fpe_test,v 1.10 2023/11/02 23:20:31 tom Exp $
3 ###############################################################################
4 # copyright 2009-2022,2023, Thomas E. Dickey
5 # copyright 2010, Guido Berhoerster
6 # copyright 1994,1995, Michael D. Brennan
7 #
8 # This is a source file for mawk, an implementation of
9 # the AWK programming language.
10 #
11 # Mawk is distributed without warranty under the terms of
12 # the GNU General Public License, version 2, 1991.
13 ###############################################################################
14
15 # tests if mawk has been compiled to correctly handle
16 # floating point exceptions
17
18 PROG="${MAWK:-../mawk}"
19
20 : "${EGREP:=grep -E}"
21 : "${FGREP:=grep -F}"
22
23 echo "testing floating point exception handling"
24
25 STDOUT=${TMPDIR-/tmp}/mawktest$$
26
27 test1='BEGIN{ print 4/0 }'
28
29
30 test2='BEGIN {
31 x = 100
32 do { y = x ; x *= 1000 } while ( y != x )
33 print "loop terminated"
34 }'
35
36 # this used to test log(-8), but that was too challenging for cygwin hackers
37 test3='BEGIN{ print sqrt(-8) }'
38
39
40 echo "testing division by zero"
41 echo "$PROG" "$test1"
42 $PROG "$test1"
43 ret1=$?
44 echo
45
46 echo "testing overflow"
47 echo "$PROG" "$test2"
48 $PROG "$test2"
49 ret2=$?
50 echo
51
52 echo "testing domain error"
53 echo "$PROG" "$test3"
54 $PROG "$test3" > "$STDOUT"
55 ret3=$?
56 cat "$STDOUT"
57 echo
58
59
60 # the returns should all be zero or all 2
61 # core dumps not allowed
62
63 trap '
64 echo compilation defines for floating point are incorrect
65 rm -f $STDOUT
66 exit 1' 0
67
68 echo
69 echo ==============================
70
71 echo return1 = $ret1
72 echo return2 = $ret2
73 echo return3 = $ret3
74
75
76 [ $ret1 -gt 128 ] && { echo test1 failed ; exception=1 ; }
77 [ $ret2 -gt 128 ] && { echo test2 failed ; exception=1 ; }
78 [ $ret3 -gt 128 ] && { echo test3 failed ; exception=1 ; }
79
80 [ "$exception" = 1 ] && { rm -f ./*core* "$STDOUT" ; exit 1 ; }
81
82
83 same=0
84
85 [ $ret1 = $ret2 ] && [ $ret2 = $ret3 ] && same=1
86
87
88 if [ $same = 1 ]
89 then
90 if [ $ret1 = 0 ]
91 then
92 echo "results consistent: ignoring floating exceptions"
93 # some versions of hpux print NAN as ?
94 if $EGREP '[nN][aA][nN]|\?' "$STDOUT" > /dev/null
95 then
96 :
97 # MSYS / MinGW uses a different string...
98 elif $FGREP '#IND' "$STDOUT" > /dev/null
99 then
100 echo "found MinGW hack for NaN..."
101 else
102 echo "but the library is not IEEE754 compatible"
103 echo "test 3 failed"
104 exit 1
105 fi
106 else echo "results consistent: trapping floating exceptions"
107 fi
108
109 trap 0
110 rm -f "$STDOUT"
111 exit 0
112
113 else
114 cat <<-EOF
115 results are not consistent
116 return values should all be 0 if ignoring FPEs (e.g. with IEEE754)
117 or all 2 if trapping FPEs
118 EOF
119
120 exit 1
121 fi
122 # vile: ts=4 sw=4