fixed more binutils issues (newer gcc/libc)
[zpugcc/jano.git] / toolchain / gcc / newlib / libm / test / convert.c
bloba748280275e74dd7d54da4f616f3743bba13320b
1 /* Test conversions */
3 #include "test.h"
4 #include <stdlib.h>
5 #include <errno.h>
7 static char buffer[500];
9 extern double_type doubles[];
11 /* TEST ATOF ATOFF */
13 double_type *pd = doubles;
15 void
16 _DEFUN_VOID(test_strtod)
18 char *tail;
19 double v;
20 /* On average we'll loose 1/2 a bit, so the test is for within 1 bit */
21 v = strtod(pd->string, &tail);
22 test_mok(v, pd->value, 64);
23 test_iok(tail - pd->string, pd->endscan);
26 void
27 _DEFUN_VOID(test_strtof)
29 char *tail;
30 double v;
31 /* On average we'll loose 1/2 a bit, so the test is for within 1 bit */
32 v = strtof(pd->string, &tail);
33 test_mok(v, pd->value, 32);
34 test_iok(tail - pd->string, pd->endscan);
37 void
38 _DEFUN_VOID(test_atof)
40 test_mok(atof(pd->string), pd->value, 64);
43 void
44 _DEFUN_VOID(test_atoff)
46 test_mok(atoff(pd->string), pd->value, 32);
50 static
51 void
52 _DEFUN(iterate,(func, name),
53 void _EXFUN((*func),(void)) _AND
54 char *name)
57 newfunc(name);
58 pd = doubles;
59 while (pd->string) {
60 line(pd->line);
61 func();
62 pd++;
67 extern int_type ints[];
69 int_type *p = ints;
72 static void
73 _DEFUN(int_iterate,(func, name),
74 void (*func)() _AND
75 char *name)
77 newfunc(name);
79 p = ints;
80 while (p->string) {
81 line(p->line);
82 errno = 0;
83 func();
84 p++;
88 void
89 _DEFUN(test_strtol_base,(base, pi, string),
90 int base _AND
91 int_scan_type *pi _AND
92 char *string)
94 long r;
95 char *ptr;
96 errno = 0;
97 r = strtol(string, &ptr, base);
98 test_iok(r, pi->value);
99 test_eok(errno, pi->errno_val);
100 test_iok(ptr - string, pi->end);
103 void
104 _DEFUN_VOID(test_strtol)
106 test_strtol_base(8,&(p->octal), p->string);
107 test_strtol_base(10,&(p->decimal), p->string);
108 test_strtol_base(16, &(p->hex), p->string);
109 test_strtol_base(0, &(p->normal), p->string);
110 test_strtol_base(26, &(p->alphabetical), p->string);
113 void
114 _DEFUN_VOID(test_atoi)
116 test_iok(atoi(p->string), p->decimal.value);
117 test_eok(errno, p->decimal.errno_val);
120 void
121 _DEFUN_VOID(test_atol)
123 test_iok(atol(p->string), p->decimal.value);
124 test_eok(errno, p->decimal.errno_val);
127 /* test ECVT and friends */
128 extern ddouble_type ddoubles[];
129 ddouble_type *pdd;
130 void
131 _DEFUN_VOID(test_ecvtbuf)
133 int a2,a3;
134 char *s;
135 s = ecvtbuf(pdd->value, pdd->e1, &a2, &a3, buffer);
137 test_sok(s,pdd->estring);
138 test_iok(pdd->e2,a2);
139 test_iok(pdd->e3,a3);
142 void
143 _DEFUN_VOID(test_ecvt)
145 int a2,a3;
146 char *s;
147 s = ecvt(pdd->value, pdd->e1, &a2, &a3);
149 test_sok(s,pdd->estring);
150 test_iok(pdd->e2,a2);
151 test_iok(pdd->e3,a3);
153 s = ecvtf(pdd->value, pdd->e1, &a2, &a3);
155 test_sok(s,pdd->estring);
156 test_iok(pdd->e2,a2);
157 test_iok(pdd->e3,a3);
160 void
161 _DEFUN_VOID(test_fcvtbuf)
163 int a2,a3;
164 char *s;
165 s = fcvtbuf(pdd->value, pdd->f1, &a2, &a3, buffer);
167 test_scok(s,pdd->fstring,10);
168 test_iok(pdd->f2,a2);
169 test_iok(pdd->f3,a3);
172 void
173 _DEFUN_VOID(test_gcvt)
175 char *s = gcvt(pdd->value, pdd->g1, buffer);
176 test_scok(s, pdd->gstring, 9);
178 s = gcvtf(pdd->value, pdd->g1, buffer);
179 test_scok(s, pdd->gstring, 9);
183 void
184 _DEFUN_VOID(test_fcvt)
186 int a2,a3;
187 char *sd;
188 char *sf;
189 double v1;
190 double v2;
191 sd = fcvt(pdd->value, pdd->f1, &a2, &a3);
193 test_scok(sd,pdd->fstring,10);
194 test_iok(pdd->f2,a2);
195 test_iok(pdd->f3,a3);
197 /* Test the float version by converting and inspecting the numbers 3
198 after reconverting */
199 sf = fcvtf(pdd->value, pdd->f1, &a2, &a3);
200 sscanf(sd, "%lg", &v1);
201 sscanf(sf, "%lg", &v2);
202 test_mok(v1, v2,32);
203 test_iok(pdd->f2,a2);
204 test_iok(pdd->f3,a3);
207 static void
209 _DEFUN(diterate,(func, name),
210 void (*func)() _AND
211 char *name)
213 newfunc(name);
215 pdd = ddoubles;
216 while (pdd->estring) {
217 line(pdd->line);
218 errno = 0;
219 func();
220 pdd++;
225 void
226 _DEFUN_VOID(deltest)
228 newfunc("rounding");
229 line(1);
230 sprintf(buffer,"%.2f", 9.999);
231 test_sok(buffer,"10.00");
232 line(2);
233 sprintf(buffer,"%.2g", 1.0);
234 test_sok(buffer,"1");
235 line(3);
236 sprintf(buffer,"%.2g", 1.2e-6);
237 test_sok(buffer,"1.2e-06");
238 line(4);
239 sprintf(buffer,"%.0g", 1.0);
240 test_sok(buffer,"1");
241 line(5);
242 sprintf(buffer,"%.0e",1e1);
243 test_sok(buffer,"1e+01");
244 line(6);
245 sprintf(buffer, "%f", 12.3456789);
246 test_sok(buffer, "12.345679");
247 line(7);
248 sprintf(buffer, "%6.3f", 12.3456789);
249 test_sok(buffer, "12.346");
250 line(8);
251 sprintf(buffer,"%.0f", 12.3456789);
252 test_sok(buffer,"12");
255 /* Most of what sprint does is tested with the tests of
256 fcvt/ecvt/gcvt, but here are some more */
257 void
258 _DEFUN_VOID(test_sprint)
260 extern sprint_double_type sprint_doubles[];
261 sprint_double_type *s = sprint_doubles;
262 extern sprint_int_type sprint_ints[];
263 sprint_int_type *si = sprint_ints;
266 newfunc( "sprintf");
269 while (s->line)
271 line( s->line);
272 sprintf(buffer, s->format_string, s->value);
273 test_scok(buffer, s->result, 12); /* Only check the first 12 digs,
274 other stuff is random */
275 s++;
278 while (si->line)
280 line( si->line);
281 sprintf(buffer, si->format_string, si->value);
282 test_sok(buffer, si->result);
283 si++;
287 /* Scanf calls strtod etc tested elsewhere, but also has some pattern matching skills */
288 void
289 _DEFUN_VOID(test_scan)
291 int i,j;
292 extern sprint_double_type sprint_doubles[];
293 sprint_double_type *s = sprint_doubles;
294 extern sprint_int_type sprint_ints[];
295 sprint_int_type *si = sprint_ints;
297 newfunc( "scanf");
299 /* Test scanf by converting all the numbers in the sprint vectors
300 to and from their source and making sure nothing breaks */
302 while (s->line)
305 double d0,d1;
306 line( s->line);
307 sscanf(s->result, "%lg", &d0);
308 sprintf(buffer, "%20.17e", d0);
309 sscanf(buffer, "%lg", &d1);
310 test_mok(d0,d1, 64);
311 s++;
314 /* And integers too */
315 while (si->line)
318 long d0,d1;
320 line(si->line);
321 sscanf(si->result, "%d", &d0);
322 sprintf(buffer, "%d", d0);
323 sscanf(buffer, "%d", &d1);
324 test_iok(d0,d1);
325 si++;
328 /* And the string matching */
330 sscanf(" 9","%d", &i);
331 test_iok(i, 9);
332 sscanf("foo bar 123 zap 456","foo bar %d zap %d", &i, &j);
333 test_iok(i, 123);
334 test_iok(j, 456);
336 sscanf("magicXYZZYfoobar","magic%[XYZ]", buffer);
337 test_sok("XYZZY", buffer);
338 sscanf("magicXYZZYfoobar","%[^XYZ]", buffer);
339 test_sok("magic", buffer);
342 void
343 _DEFUN_VOID(test_cvt)
345 deltest();
347 diterate(test_fcvtbuf,"fcvtbuf");
348 diterate(test_fcvt,"fcvt/fcvtf");
350 diterate(test_gcvt,"gcvt/gcvtf");
351 diterate(test_ecvtbuf,"ecvtbuf");
352 diterate(test_ecvt,"ecvt/ecvtf");
354 iterate(test_strtod, "strtod");
356 test_scan();
357 test_sprint();
358 iterate(test_atof, "atof");
359 iterate(test_atoff, "atoff");
361 iterate(test_strtof, "strtof");
363 int_iterate(test_atoi,"atoi");
364 int_iterate(test_atol,"atol");
365 int_iterate(test_strtol, "strtol");