blob: 2156afbefe69c5ef29597b3227176a9340af77e9 [file] [log] [blame]
Guido van Rossum52fa3a61997-02-14 22:59:58 +00001/*
2 ---------------------------------------------------------------------
3 / Copyright (c) 1996. \
4 | The Regents of the University of California. |
5 | All rights reserved. |
6 | |
7 | Permission to use, copy, modify, and distribute this software for |
8 | any purpose without fee is hereby granted, provided that this en- |
9 | tire notice is included in all copies of any software which is or |
10 | includes a copy or modification of this software and in all |
11 | copies of the supporting documentation for such software. |
12 | |
13 | This work was produced at the University of California, Lawrence |
14 | Livermore National Laboratory under contract no. W-7405-ENG-48 |
15 | between the U.S. Department of Energy and The Regents of the |
16 | University of California for the operation of UC LLNL. |
17 | |
18 | DISCLAIMER |
19 | |
20 | This software was prepared as an account of work sponsored by an |
21 | agency of the United States Government. Neither the United States |
22 | Government nor the University of California nor any of their em- |
23 | ployees, makes any warranty, express or implied, or assumes any |
24 | liability or responsibility for the accuracy, completeness, or |
25 | usefulness of any information, apparatus, product, or process |
26 | disclosed, or represents that its use would not infringe |
27 | privately-owned rights. Reference herein to any specific commer- |
28 | cial products, process, or service by trade name, trademark, |
29 | manufacturer, or otherwise, does not necessarily constitute or |
30 | imply its endorsement, recommendation, or favoring by the United |
31 | States Government or the University of California. The views and |
32 | opinions of authors expressed herein do not necessarily state or |
33 | reflect those of the United States Government or the University |
34 | of California, and shall not be used for advertising or product |
35 \ endorsement purposes. /
36 ---------------------------------------------------------------------
37*/
38
39/*
40 Floating point exception test module.
41
42 */
43
44#include "Python.h"
45
46static PyObject *fpe_error;
47void initfpetest(void);
48static PyObject *test(PyObject *self,PyObject *args);
49static int db0(void);
50static int overflow(void);
51static int nest1(double);
52static int nest2(double);
53static double nest3(double);
54
55static PyMethodDef fpetest_methods[] = {
56 {"test", (PyCFunction) test, 1},
57 {0,0}
58};
59
60static PyObject *test(PyObject *self,PyObject *args)
61{
62 int i = 0, r;
63
64 fprintf(stderr,"Test trapping overflow\n");
65 r = overflow();
66 if(r){
67 fprintf(stderr,"(Note: No exception was raised.)\n");
68 PyErr_Clear();
69 }else{
70 fprintf(stderr,"Trapped:: ");
71 PyErr_Print();
72 PyErr_Clear();
73 }
74 i += r;
75
76 fprintf(stderr,"Test trapping division by zero\n");
77 r = db0();
78 if(r){
79 fprintf(stderr,"(Note: No exception was raised.)\n");
80 PyErr_Clear();
81 }else{
82 fprintf(stderr,"Trapped:: ");
83 PyErr_Print();
84 PyErr_Clear();
85 }
86 i += r;
87
88 fprintf(stderr,"Test nested protection zones, outer zone\n");
89 r = nest1(0.0);
90 if(r){
91 fprintf(stderr,"(Note: No exception was raised.)\n");
92 PyErr_Clear();
93 }else{
94 fprintf(stderr,"Trapped:: ");
95 PyErr_Print();
96 PyErr_Clear();
97 }
98 i += r;
99
100 fprintf(stderr,"Test nested protection zones, inner zone\n");
101 fprintf(stderr,"(Note: Return will apparently come from outer zone.)\n");
102 r = nest1(1.0);
103 if(r){
104 fprintf(stderr,"(Note: No exception was raised.)\n");
105 PyErr_Clear();
106 }else{
107 fprintf(stderr,"Trapped:: ");
108 PyErr_Print();
109 PyErr_Clear();
110 }
111 i += r;
112
113 fprintf(stderr,"Test nested protection zones, trailing outer zone\n");
114 r = nest1(2.0);
115 if(r){
116 fprintf(stderr,"(Note: No exception was raised.)\n");
117 PyErr_Clear();
118 }else{
119 fprintf(stderr,"Trapped:: ");
120 PyErr_Print();
121 PyErr_Clear();
122 }
123 i += r;
124
125 fprintf(stderr,"Test nested function calls, prior error\n");
126 r = nest2(0.0);
127 if(r){
128 fprintf(stderr,"(Note: No exception was raised.)\n");
129 PyErr_Clear();
130 }else{
131 fprintf(stderr,"Trapped:: ");
132 PyErr_Print();
133 PyErr_Clear();
134 }
135 i += r;
136
137 fprintf(stderr,"Test nested function calls, interior error\n");
138 r = nest2(1.0);
139 if(r){
140 fprintf(stderr,"(Note: No exception was raised.)\n");
141 PyErr_Clear();
142 }else{
143 fprintf(stderr,"Trapped:: ");
144 PyErr_Print();
145 PyErr_Clear();
146 }
147 i += r;
148
149 fprintf(stderr,"Test nested function calls, trailing error\n");
150 r = nest2(2.0);
151 if(r){
152 fprintf(stderr,"(Note: No exception was raised.)\n");
153 PyErr_Clear();
154 }else{
155 fprintf(stderr,"Trapped:: ");
156 PyErr_Print();
157 PyErr_Clear();
158 }
159 i += r;
160
161 fprintf(stderr,"Number of tests failed: %d\n", i);
162 Py_INCREF (Py_None);
163 return Py_None;
164}
165
166static int nest1(double x)
167{
168 double a = 1.0;
169 PyFPE_START_PROTECT("Division by zero, outer zone", return 0)
170 a = 1./x;
171 PyFPE_START_PROTECT("Division by zero, inner zone", return 0)
172 a = 1./(1. - x);
173 PyFPE_END_PROTECT
174 a = 1./(2. - x);
175 PyFPE_END_PROTECT
176 return(1);
177}
178
179static int nest2(double x)
180{
181 double a = 1.0;
182 PyFPE_START_PROTECT("Division by zero, prior error", return 0)
183 a = 1./x;
184 a = nest3(x);
185 a = 1./(2. - x);
186 PyFPE_END_PROTECT
187 return(1);
188}
189
190static double nest3(double x)
191{
192 double result;
193 PyFPE_START_PROTECT("Division by zero, nest3 error", return 0)
194 result = 1./(1. - x);
195 PyFPE_END_PROTECT
196 return result;
197}
198
199static int db0(void)
200{
201 double a = 1.0;
202 PyFPE_START_PROTECT("Division by zero", return 0)
203 a = 1./(a - 1.);
204 PyFPE_END_PROTECT
205 return(1);
206}
207
208static int overflow(void)
209{
210 double a, b = 1.e200;
211 PyFPE_START_PROTECT("Overflow", return 0)
212 a = b*b;
213 PyFPE_END_PROTECT
214 return(1);
215}
216
217void initfpetest(void)
218{
219 PyObject *m, *d;
220
221 m = Py_InitModule("fpetest", fpetest_methods);
222 d = PyModule_GetDict(m);
223 fpe_error = PyString_FromString("fpetest.error");
224 PyDict_SetItemString(d, "error", fpe_error);
225
226 if (PyErr_Occurred())
227 Py_FatalError("Cannot initialize module fpetest");
228}