Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libm / test / convert.c
blob026bd277bd6bdde75863be91cfd021d4b81f916b
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 test_strtod (void)
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 test_strtof (void)
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 test_atof (void)
40 test_mok(atof(pd->string), pd->value, 64);
43 void
44 test_atoff (void)
46 test_mok(atoff(pd->string), pd->value, 32);
50 static
51 void
52 iterate (void (*func) (void),
53 char *name)
56 newfunc(name);
57 pd = doubles;
58 while (pd->string) {
59 line(pd->line);
60 func();
61 pd++;
66 extern int_type ints[];
68 int_type *p = ints;
71 static void
72 int_iterate (void (*func)(),
73 char *name)
75 newfunc(name);
77 p = ints;
78 while (p->string) {
79 line(p->line);
80 errno = 0;
81 func();
82 p++;
86 void
87 test_strtol_base (int base,
88 int_scan_type *pi,
89 char *string)
91 long r;
92 char *ptr;
93 errno = 0;
94 r = strtol(string, &ptr, base);
95 test_iok(r, pi->value);
96 test_eok(errno, pi->errno_val);
97 test_iok(ptr - string, pi->end);
100 void
101 test_strtol (void)
103 test_strtol_base(8,&(p->octal), p->string);
104 test_strtol_base(10,&(p->decimal), p->string);
105 test_strtol_base(16, &(p->hex), p->string);
106 test_strtol_base(0, &(p->normal), p->string);
107 test_strtol_base(26, &(p->alphabetical), p->string);
110 void
111 test_atoi (void)
113 test_iok(atoi(p->string), p->decimal.value);
114 test_eok(errno, p->decimal.errno_val);
117 void
118 test_atol (void)
120 test_iok(atol(p->string), p->decimal.value);
121 test_eok(errno, p->decimal.errno_val);
124 /* test ECVT and friends */
125 extern ddouble_type ddoubles[];
126 ddouble_type *pdd;
127 void
128 test_ecvtbuf (void)
130 int a2,a3;
131 char *s;
132 s = ecvtbuf(pdd->value, pdd->e1, &a2, &a3, buffer);
134 test_sok(s,pdd->estring);
135 test_iok(pdd->e2,a2);
136 test_iok(pdd->e3,a3);
139 void
140 test_ecvt (void)
142 int a2,a3;
143 char *s;
144 s = ecvt(pdd->value, pdd->e1, &a2, &a3);
146 test_sok(s,pdd->estring);
147 test_iok(pdd->e2,a2);
148 test_iok(pdd->e3,a3);
150 s = ecvtf(pdd->value, pdd->e1, &a2, &a3);
152 test_sok(s,pdd->estring);
153 test_iok(pdd->e2,a2);
154 test_iok(pdd->e3,a3);
157 void
158 test_fcvtbuf (void)
160 int a2,a3;
161 char *s;
162 s = fcvtbuf(pdd->value, pdd->f1, &a2, &a3, buffer);
164 test_scok(s,pdd->fstring,10);
165 test_iok(pdd->f2,a2);
166 test_iok(pdd->f3,a3);
169 void
170 test_gcvt (void)
172 char *s = gcvt(pdd->value, pdd->g1, buffer);
173 test_scok(s, pdd->gstring, 9);
175 s = gcvtf(pdd->value, pdd->g1, buffer);
176 test_scok(s, pdd->gstring, 9);
180 void
181 test_fcvt (void)
183 int a2,a3;
184 char *sd;
185 char *sf;
186 double v1;
187 double v2;
188 sd = fcvt(pdd->value, pdd->f1, &a2, &a3);
190 test_scok(sd,pdd->fstring,10);
191 test_iok(pdd->f2,a2);
192 test_iok(pdd->f3,a3);
194 /* Test the float version by converting and inspecting the numbers 3
195 after reconverting */
196 sf = fcvtf(pdd->value, pdd->f1, &a2, &a3);
197 sscanf(sd, "%lg", &v1);
198 sscanf(sf, "%lg", &v2);
199 test_mok(v1, v2,32);
200 test_iok(pdd->f2,a2);
201 test_iok(pdd->f3,a3);
204 static void
206 diterate (void (*func)(),
207 char *name)
209 newfunc(name);
211 pdd = ddoubles;
212 while (pdd->estring) {
213 line(pdd->line);
214 errno = 0;
215 func();
216 pdd++;
221 void
222 deltest (void)
224 newfunc("rounding");
225 line(1);
226 sprintf(buffer,"%.2f", 9.999);
227 test_sok(buffer,"10.00");
228 line(2);
229 sprintf(buffer,"%.2g", 1.0);
230 test_sok(buffer,"1");
231 line(3);
232 sprintf(buffer,"%.2g", 1.2e-6);
233 test_sok(buffer,"1.2e-06");
234 line(4);
235 sprintf(buffer,"%.0g", 1.0);
236 test_sok(buffer,"1");
237 line(5);
238 sprintf(buffer,"%.0e",1e1);
239 test_sok(buffer,"1e+01");
240 line(6);
241 sprintf(buffer, "%f", 12.3456789);
242 test_sok(buffer, "12.345679");
243 line(7);
244 sprintf(buffer, "%6.3f", 12.3456789);
245 test_sok(buffer, "12.346");
246 line(8);
247 sprintf(buffer,"%.0f", 12.3456789);
248 test_sok(buffer,"12");
251 /* Most of what sprint does is tested with the tests of
252 fcvt/ecvt/gcvt, but here are some more */
253 void
254 test_sprint (void)
256 extern sprint_double_type sprint_doubles[];
257 sprint_double_type *s = sprint_doubles;
258 extern sprint_int_type sprint_ints[];
259 sprint_int_type *si = sprint_ints;
262 newfunc( "sprintf");
265 while (s->line)
267 line( s->line);
268 sprintf(buffer, s->format_string, s->value);
269 test_scok(buffer, s->result, 12); /* Only check the first 12 digs,
270 other stuff is random */
271 s++;
274 while (si->line)
276 line( si->line);
277 sprintf(buffer, si->format_string, si->value);
278 test_sok(buffer, si->result);
279 si++;
283 /* Scanf calls strtod etc tested elsewhere, but also has some pattern matching skills */
284 void
285 test_scan (void)
287 int i,j;
288 extern sprint_double_type sprint_doubles[];
289 sprint_double_type *s = sprint_doubles;
290 extern sprint_int_type sprint_ints[];
291 sprint_int_type *si = sprint_ints;
293 newfunc( "scanf");
295 /* Test scanf by converting all the numbers in the sprint vectors
296 to and from their source and making sure nothing breaks */
298 while (s->line)
301 double d0,d1;
302 line( s->line);
303 sscanf(s->result, "%lg", &d0);
304 sprintf(buffer, "%20.17e", d0);
305 sscanf(buffer, "%lg", &d1);
306 test_mok(d0,d1, 64);
307 s++;
310 /* And integers too */
311 while (si->line)
314 long d0,d1;
316 line(si->line);
317 sscanf(si->result, "%d", &d0);
318 sprintf(buffer, "%d", d0);
319 sscanf(buffer, "%d", &d1);
320 test_iok(d0,d1);
321 si++;
324 /* And the string matching */
326 sscanf(" 9","%d", &i);
327 test_iok(i, 9);
328 sscanf("foo bar 123 zap 456","foo bar %d zap %d", &i, &j);
329 test_iok(i, 123);
330 test_iok(j, 456);
332 sscanf("magicXYZZYfoobar","magic%[XYZ]", buffer);
333 test_sok("XYZZY", buffer);
334 sscanf("magicXYZZYfoobar","%[^XYZ]", buffer);
335 test_sok("magic", buffer);
338 void
339 test_cvt (void)
341 deltest();
343 diterate(test_fcvtbuf,"fcvtbuf");
344 diterate(test_fcvt,"fcvt/fcvtf");
346 diterate(test_gcvt,"gcvt/gcvtf");
347 diterate(test_ecvtbuf,"ecvtbuf");
348 diterate(test_ecvt,"ecvt/ecvtf");
350 iterate(test_strtod, "strtod");
352 test_scan();
353 test_sprint();
354 iterate(test_atof, "atof");
355 iterate(test_atoff, "atoff");
357 iterate(test_strtof, "strtof");
359 int_iterate(test_atoi,"atoi");
360 int_iterate(test_atol,"atol");
361 int_iterate(test_strtol, "strtol");