Git init
[external/mawk.git] / test / fpe_test
1 #!/bin/sh
2
3 # tests if mawk has been compiled to correctly handle
4 # floating point exceptions
5 #
6 # $Log: fpe_test,v $
7 # Revision 1.3  1995/08/29  14:17:18  mike
8 # exit 2 changes
9 #
10 # Revision 1.2  1994/12/18  18:51:55  mike
11 # recognize NAN printed as ? for hpux
12 #
13
14 PATH=.:$PATH
15
16 test1='BEGIN{ print 4/0 }'
17
18
19 test2='BEGIN { 
20   x = 100
21   do { y = x ; x *= 1000 } while ( y != x )
22   print "loop terminated"
23 }'
24
25 test3='BEGIN{ print log(-8) }'
26
27
28 echo "testing division by zero"
29 echo mawk "$test1"
30 mawk "$test1"
31 ret1=$?
32 echo
33
34 echo "testing overflow"
35 echo mawk "$test2"
36 mawk "$test2"
37 ret2=$?
38 echo
39
40 echo "testing domain error"
41 echo mawk "$test3"
42 mawk "$test3"  > temp$$
43 ret3=$?
44 cat temp$$
45 echo
46
47
48 # the returns should all be zero or all 2
49 # core dumps not allowed
50
51 trap '
52 echo compilation defines for floating point are incorrect
53 rm -f temp$$
54 exit 1'  0
55
56 echo
57 echo ==============================
58
59 echo return1 = $ret1
60 echo return2 = $ret2
61 echo return3 = $ret3
62
63
64 [ $ret1 -gt 128 ] && { echo test1 failed ; exception=1 ; }
65 [ $ret2 -gt 128 ] && { echo test2 failed ; exception=1 ; }
66 [ $ret3 -gt 128 ] && { echo test3 failed ; exception=1 ; }
67
68 [ "$exception" = 1 ] && { rm -f core temp$$ ; exit 1 ; }
69
70
71 same=0
72
73 [ $ret1 = $ret2 ] && [ $ret2 = $ret3 ] && same=1
74
75
76 if [ $same = 1 ]
77    then
78    if [ $ret1 = 0 ]
79       then 
80         echo results consistent: ignoring floating exceptions
81         # some versions of hpux print NAN as ?
82         if  egrep '[nN][aA][nN]|\?' temp$$  > /dev/null
83            then :
84            else
85              echo "but the library is not IEEE754 compatible"
86              echo "test 3 failed"
87              exit 1
88         fi
89       else echo results consistent: trapping floating exceptions
90    fi
91
92    trap  0
93    rm -f temp$$
94    exit 0
95
96    else
97    echo results are not consistent
98 echo 'return values should all be 0 if ignoring FPEs (e.g. with IEEE754)
99 or all 2 if trapping FPEs'
100
101 exit 1
102 fi
103