1 | #include <signal.h> |
---|
2 | #include "test.h" |
---|
3 | #include <math.h> |
---|
4 | #include <ieeefp.h> |
---|
5 | #include <string.h> |
---|
6 | int verbose; |
---|
7 | static int count; |
---|
8 | int inacc; |
---|
9 | |
---|
10 | |
---|
11 | int |
---|
12 | main (int ac, |
---|
13 | char **av) |
---|
14 | { |
---|
15 | int i; |
---|
16 | int math2 = 1; |
---|
17 | int string= 1; |
---|
18 | int is = 1; |
---|
19 | int math= 1; |
---|
20 | int cvt = 1; |
---|
21 | int ieee= 1; |
---|
22 | bt(); |
---|
23 | for (i = 1; i < ac; i++) |
---|
24 | { |
---|
25 | if (strcmp(av[i],"-v")==0) |
---|
26 | verbose ++; |
---|
27 | if (strcmp(av[i],"-nomath2") == 0) |
---|
28 | math2 = 0; |
---|
29 | if (strcmp(av[i],"-nostrin") == 0) |
---|
30 | string= 0; |
---|
31 | if (strcmp(av[i],"-nois") == 0) |
---|
32 | is = 0; |
---|
33 | if (strcmp(av[i],"-nomath") == 0) |
---|
34 | math= 0; |
---|
35 | if (strcmp(av[i],"-nocvt") == 0) |
---|
36 | cvt = 0; |
---|
37 | if (strcmp(av[i],"-noiee") == 0) |
---|
38 | ieee= 0; |
---|
39 | } |
---|
40 | if (cvt) |
---|
41 | test_cvt(); |
---|
42 | |
---|
43 | if (math2) |
---|
44 | test_math2(); |
---|
45 | if (string) |
---|
46 | test_string(); |
---|
47 | if (math) |
---|
48 | test_math(); |
---|
49 | if (is) |
---|
50 | test_is(); |
---|
51 | if (ieee) test_ieee(); |
---|
52 | printf("Tested %d functions, %d errors detected\n", count, inacc); |
---|
53 | return 0; |
---|
54 | } |
---|
55 | |
---|
56 | |
---|
57 | static const char *iname = "foo"; |
---|
58 | void |
---|
59 | newfunc (const char *string) |
---|
60 | { |
---|
61 | if (strcmp(iname, string)) |
---|
62 | { |
---|
63 | printf("testing %s\n", string); |
---|
64 | fflush(stdout); |
---|
65 | iname = string; |
---|
66 | } |
---|
67 | |
---|
68 | } |
---|
69 | |
---|
70 | |
---|
71 | static int theline; |
---|
72 | |
---|
73 | void line(li) |
---|
74 | int li; |
---|
75 | { |
---|
76 | if (verbose) |
---|
77 | { |
---|
78 | printf(" %d\n", li); |
---|
79 | } |
---|
80 | theline = li; |
---|
81 | |
---|
82 | count++; |
---|
83 | } |
---|
84 | |
---|
85 | |
---|
86 | |
---|
87 | int redo = 0; |
---|
88 | int reduce = 0; |
---|
89 | |
---|
90 | int strtod_vector = 0; |
---|
91 | |
---|
92 | int |
---|
93 | bigger (__ieee_double_shape_type *a, |
---|
94 | __ieee_double_shape_type *b) |
---|
95 | { |
---|
96 | |
---|
97 | if (a->parts.msw > b->parts.msw) |
---|
98 | { |
---|
99 | |
---|
100 | return 1; |
---|
101 | } |
---|
102 | else if (a->parts.msw == b->parts.msw) |
---|
103 | { |
---|
104 | if (a->parts.lsw > b->parts.lsw) |
---|
105 | { |
---|
106 | return 1; |
---|
107 | } |
---|
108 | } |
---|
109 | return 0; |
---|
110 | } |
---|
111 | |
---|
112 | |
---|
113 | |
---|
114 | /* Return the first bit different between two double numbers */ |
---|
115 | int |
---|
116 | mag_of_error (double is, |
---|
117 | double shouldbe) |
---|
118 | { |
---|
119 | __ieee_double_shape_type a,b; |
---|
120 | int i; |
---|
121 | int a_big; |
---|
122 | unsigned int mask; |
---|
123 | unsigned long int __x; |
---|
124 | unsigned long int msw, lsw; |
---|
125 | a.value = is; |
---|
126 | |
---|
127 | b.value = shouldbe; |
---|
128 | |
---|
129 | if (a.parts.msw == b.parts.msw |
---|
130 | && a.parts.lsw== b.parts.lsw) return 64; |
---|
131 | |
---|
132 | |
---|
133 | /* Subtract the larger from the smaller number */ |
---|
134 | |
---|
135 | a_big = bigger(&a, &b); |
---|
136 | |
---|
137 | if (!a_big) { |
---|
138 | int t; |
---|
139 | t = a.parts.msw; |
---|
140 | a.parts.msw = b.parts.msw; |
---|
141 | b.parts.msw = t; |
---|
142 | |
---|
143 | t = a.parts.lsw; |
---|
144 | a.parts.lsw = b.parts.lsw; |
---|
145 | b.parts.lsw = t; |
---|
146 | } |
---|
147 | |
---|
148 | |
---|
149 | |
---|
150 | __x = (a.parts.lsw) - (b.parts.lsw); |
---|
151 | msw = (a.parts.msw) - (b.parts.msw) - (__x > (a.parts.lsw)); |
---|
152 | lsw = __x; |
---|
153 | |
---|
154 | |
---|
155 | |
---|
156 | |
---|
157 | /* Find out which bit the difference is in */ |
---|
158 | mask = 0x80000000; |
---|
159 | for (i = 0; i < 32; i++) |
---|
160 | { |
---|
161 | if (((msw) & mask)!=0) return i; |
---|
162 | mask >>=1; |
---|
163 | } |
---|
164 | |
---|
165 | mask = 0x80000000; |
---|
166 | for (i = 0; i < 32; i++) |
---|
167 | { |
---|
168 | |
---|
169 | if (((lsw) & mask)!=0) return i+32; |
---|
170 | mask >>=1; |
---|
171 | } |
---|
172 | |
---|
173 | return 64; |
---|
174 | |
---|
175 | } |
---|
176 | |
---|
177 | int ok_mag; |
---|
178 | |
---|
179 | |
---|
180 | |
---|
181 | void |
---|
182 | test_sok (char *is, |
---|
183 | char *shouldbe) |
---|
184 | { |
---|
185 | if (strcmp(is,shouldbe)) |
---|
186 | { |
---|
187 | printf("%s:%d, inacurate answer: (%s should be %s)\n", |
---|
188 | iname, |
---|
189 | theline, |
---|
190 | is, shouldbe); |
---|
191 | inacc++; |
---|
192 | } |
---|
193 | } |
---|
194 | void |
---|
195 | test_iok (int is, |
---|
196 | int shouldbe) |
---|
197 | { |
---|
198 | if (is != shouldbe){ |
---|
199 | printf("%s:%d, inacurate answer: (%08x should be %08x)\n", |
---|
200 | iname, |
---|
201 | theline, |
---|
202 | is, shouldbe); |
---|
203 | inacc++; |
---|
204 | } |
---|
205 | } |
---|
206 | |
---|
207 | |
---|
208 | /* Compare counted strings upto a certain length - useful to test single |
---|
209 | prec float conversions against double results |
---|
210 | */ |
---|
211 | void |
---|
212 | test_scok (char *is, |
---|
213 | char *shouldbe, |
---|
214 | int count) |
---|
215 | { |
---|
216 | if (strncmp(is,shouldbe, count)) |
---|
217 | { |
---|
218 | printf("%s:%d, inacurate answer: (%s should be %s)\n", |
---|
219 | iname, |
---|
220 | theline, |
---|
221 | is, shouldbe); |
---|
222 | inacc++; |
---|
223 | } |
---|
224 | } |
---|
225 | |
---|
226 | void |
---|
227 | test_eok (int is, |
---|
228 | int shouldbe) |
---|
229 | { |
---|
230 | if (is != shouldbe){ |
---|
231 | printf("%s:%d, bad errno answer: (%d should be %d)\n", |
---|
232 | iname, |
---|
233 | theline, |
---|
234 | is, shouldbe); |
---|
235 | inacc++; |
---|
236 | } |
---|
237 | } |
---|
238 | |
---|
239 | void |
---|
240 | test_mok (double value, |
---|
241 | double shouldbe, |
---|
242 | int okmag) |
---|
243 | { |
---|
244 | __ieee_double_shape_type a,b; |
---|
245 | int mag = mag_of_error(value, shouldbe); |
---|
246 | if (mag == 0) |
---|
247 | { |
---|
248 | /* error in the first bit is ok if the numbers are both 0 */ |
---|
249 | if (value == 0.0 && shouldbe == 0.0) |
---|
250 | return; |
---|
251 | |
---|
252 | } |
---|
253 | a.value = shouldbe; |
---|
254 | b.value = value; |
---|
255 | |
---|
256 | if (mag < okmag) |
---|
257 | { |
---|
258 | printf("%s:%d, wrong answer: bit %d ", |
---|
259 | iname, |
---|
260 | theline, |
---|
261 | mag); |
---|
262 | printf("%08x%08x %08x%08x) ", |
---|
263 | a.parts.msw, a.parts.lsw, |
---|
264 | b.parts.msw, b.parts.lsw); |
---|
265 | printf("(%g %g)\n", a.value, b.value); |
---|
266 | inacc++; |
---|
267 | } |
---|
268 | } |
---|
269 | |
---|
270 | #ifdef __PCCNECV70__ |
---|
271 | kill() {} |
---|
272 | getpid() {} |
---|
273 | #endif |
---|
274 | |
---|
275 | bt(){ |
---|
276 | |
---|
277 | double f1,f2; |
---|
278 | f1 = 0.0; |
---|
279 | f2 = 0.0/f1; |
---|
280 | printf("(%g)\n", f2); |
---|
281 | |
---|
282 | } |
---|