7 static char buffer
[500];
9 extern double_type doubles
[];
13 double_type
*pd
= doubles
;
16 _DEFUN_VOID(test_strtod
)
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
);
27 _DEFUN_VOID(test_strtof
)
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
);
38 _DEFUN_VOID(test_atof
)
40 test_mok(atof(pd
->string
), pd
->value
, 64);
44 _DEFUN_VOID(test_atoff
)
46 test_mok(atoff(pd
->string
), pd
->value
, 32);
52 _DEFUN(iterate
,(func
, name
),
53 void _EXFUN((*func
),(void)) _AND
67 extern int_type ints
[];
73 _DEFUN(int_iterate
,(func
, name
),
89 _DEFUN(test_strtol_base
,(base
, pi
, string
),
91 int_scan_type
*pi _AND
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
);
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
);
114 _DEFUN_VOID(test_atoi
)
116 test_iok(atoi(p
->string
), p
->decimal
.value
);
117 test_eok(errno
, p
->decimal
.errno_val
);
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
[];
131 _DEFUN_VOID(test_ecvtbuf
)
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
);
143 _DEFUN_VOID(test_ecvt
)
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
);
161 _DEFUN_VOID(test_fcvtbuf
)
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
);
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);
184 _DEFUN_VOID(test_fcvt
)
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
);
203 test_iok(pdd
->f2
,a2
);
204 test_iok(pdd
->f3
,a3
);
209 _DEFUN(diterate
,(func
, name
),
216 while (pdd
->estring
) {
230 sprintf(buffer
,"%.2f", 9.999);
231 test_sok(buffer
,"10.00");
233 sprintf(buffer
,"%.2g", 1.0);
234 test_sok(buffer
,"1");
236 sprintf(buffer
,"%.2g", 1.2e-6);
237 test_sok(buffer
,"1.2e-06");
239 sprintf(buffer
,"%.0g", 1.0);
240 test_sok(buffer
,"1");
242 sprintf(buffer
,"%.0e",1e1
);
243 test_sok(buffer
,"1e+01");
245 sprintf(buffer
, "%f", 12.3456789);
246 test_sok(buffer
, "12.345679");
248 sprintf(buffer
, "%6.3f", 12.3456789);
249 test_sok(buffer
, "12.346");
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 */
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
;
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 */
281 sprintf(buffer
, si
->format_string
, si
->value
);
282 test_sok(buffer
, si
->result
);
287 /* Scanf calls strtod etc tested elsewhere, but also has some pattern matching skills */
289 _DEFUN_VOID(test_scan
)
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
;
299 /* Test scanf by converting all the numbers in the sprint vectors
300 to and from their source and making sure nothing breaks */
307 sscanf(s
->result
, "%lg", &d0
);
308 sprintf(buffer
, "%20.17e", d0
);
309 sscanf(buffer
, "%lg", &d1
);
314 /* And integers too */
321 sscanf(si
->result
, "%d", &d0
);
322 sprintf(buffer
, "%d", d0
);
323 sscanf(buffer
, "%d", &d1
);
328 /* And the string matching */
330 sscanf(" 9","%d", &i
);
332 sscanf("foo bar 123 zap 456","foo bar %d zap %d", &i
, &j
);
336 sscanf("magicXYZZYfoobar","magic%[XYZ]", buffer
);
337 test_sok("XYZZY", buffer
);
338 sscanf("magicXYZZYfoobar","%[^XYZ]", buffer
);
339 test_sok("magic", buffer
);
343 _DEFUN_VOID(test_cvt
)
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");
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");