ยปCore Development>Code coverage>Modules/fpetestmodule.c

Python code coverage for Modules/fpetestmodule.c

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