1 | /* Test conversions */ |
---|
2 | |
---|
3 | #include "test.h" |
---|
4 | #include <stdlib.h> |
---|
5 | #include <errno.h> |
---|
6 | |
---|
7 | static char buffer[500]; |
---|
8 | |
---|
9 | extern double_type doubles[]; |
---|
10 | |
---|
11 | /* TEST ATOF ATOFF */ |
---|
12 | |
---|
13 | double_type *pd = doubles; |
---|
14 | |
---|
15 | void |
---|
16 | test_strtod (void) |
---|
17 | { |
---|
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); |
---|
24 | } |
---|
25 | |
---|
26 | void |
---|
27 | test_strtof (void) |
---|
28 | { |
---|
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); |
---|
35 | } |
---|
36 | |
---|
37 | void |
---|
38 | test_atof (void) |
---|
39 | { |
---|
40 | test_mok(atof(pd->string), pd->value, 64); |
---|
41 | } |
---|
42 | |
---|
43 | void |
---|
44 | test_atoff (void) |
---|
45 | { |
---|
46 | test_mok(atoff(pd->string), pd->value, 32); |
---|
47 | } |
---|
48 | |
---|
49 | |
---|
50 | static |
---|
51 | void |
---|
52 | iterate (void (*func) (void), |
---|
53 | char *name) |
---|
54 | { |
---|
55 | |
---|
56 | newfunc(name); |
---|
57 | pd = doubles; |
---|
58 | while (pd->string) { |
---|
59 | line(pd->line); |
---|
60 | func(); |
---|
61 | pd++; |
---|
62 | } |
---|
63 | } |
---|
64 | |
---|
65 | |
---|
66 | extern int_type ints[]; |
---|
67 | |
---|
68 | int_type *p = ints; |
---|
69 | |
---|
70 | |
---|
71 | static void |
---|
72 | int_iterate (void (*func)(), |
---|
73 | char *name) |
---|
74 | { |
---|
75 | newfunc(name); |
---|
76 | |
---|
77 | p = ints; |
---|
78 | while (p->string) { |
---|
79 | line(p->line); |
---|
80 | errno = 0; |
---|
81 | func(); |
---|
82 | p++; |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | void |
---|
87 | test_strtol_base (int base, |
---|
88 | int_scan_type *pi, |
---|
89 | char *string) |
---|
90 | { |
---|
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); |
---|
98 | } |
---|
99 | |
---|
100 | void |
---|
101 | test_strtol (void) |
---|
102 | { |
---|
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); |
---|
108 | } |
---|
109 | |
---|
110 | void |
---|
111 | test_atoi (void) |
---|
112 | { |
---|
113 | test_iok(atoi(p->string), p->decimal.value); |
---|
114 | test_eok(errno, p->decimal.errno_val); |
---|
115 | } |
---|
116 | |
---|
117 | void |
---|
118 | test_atol (void) |
---|
119 | { |
---|
120 | test_iok(atol(p->string), p->decimal.value); |
---|
121 | test_eok(errno, p->decimal.errno_val); |
---|
122 | } |
---|
123 | |
---|
124 | /* test ECVT and friends */ |
---|
125 | extern ddouble_type ddoubles[]; |
---|
126 | ddouble_type *pdd; |
---|
127 | void |
---|
128 | test_ecvtbuf (void) |
---|
129 | { |
---|
130 | int a2,a3; |
---|
131 | char *s; |
---|
132 | s = ecvtbuf(pdd->value, pdd->e1, &a2, &a3, buffer); |
---|
133 | |
---|
134 | test_sok(s,pdd->estring); |
---|
135 | test_iok(pdd->e2,a2); |
---|
136 | test_iok(pdd->e3,a3); |
---|
137 | } |
---|
138 | |
---|
139 | void |
---|
140 | test_ecvt (void) |
---|
141 | { |
---|
142 | int a2,a3; |
---|
143 | char *s; |
---|
144 | s = ecvt(pdd->value, pdd->e1, &a2, &a3); |
---|
145 | |
---|
146 | test_sok(s,pdd->estring); |
---|
147 | test_iok(pdd->e2,a2); |
---|
148 | test_iok(pdd->e3,a3); |
---|
149 | |
---|
150 | s = ecvtf(pdd->value, pdd->e1, &a2, &a3); |
---|
151 | |
---|
152 | test_sok(s,pdd->estring); |
---|
153 | test_iok(pdd->e2,a2); |
---|
154 | test_iok(pdd->e3,a3); |
---|
155 | } |
---|
156 | |
---|
157 | void |
---|
158 | test_fcvtbuf (void) |
---|
159 | { |
---|
160 | int a2,a3; |
---|
161 | char *s; |
---|
162 | s = fcvtbuf(pdd->value, pdd->f1, &a2, &a3, buffer); |
---|
163 | |
---|
164 | test_scok(s,pdd->fstring,10); |
---|
165 | test_iok(pdd->f2,a2); |
---|
166 | test_iok(pdd->f3,a3); |
---|
167 | } |
---|
168 | |
---|
169 | void |
---|
170 | test_gcvt (void) |
---|
171 | { |
---|
172 | char *s = gcvt(pdd->value, pdd->g1, buffer); |
---|
173 | test_scok(s, pdd->gstring, 9); |
---|
174 | |
---|
175 | s = gcvtf(pdd->value, pdd->g1, buffer); |
---|
176 | test_scok(s, pdd->gstring, 9); |
---|
177 | |
---|
178 | } |
---|
179 | |
---|
180 | void |
---|
181 | test_fcvt (void) |
---|
182 | { |
---|
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); |
---|
189 | |
---|
190 | test_scok(sd,pdd->fstring,10); |
---|
191 | test_iok(pdd->f2,a2); |
---|
192 | test_iok(pdd->f3,a3); |
---|
193 | |
---|
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); |
---|
202 | } |
---|
203 | |
---|
204 | static void |
---|
205 | |
---|
206 | diterate (void (*func)(), |
---|
207 | char *name) |
---|
208 | { |
---|
209 | newfunc(name); |
---|
210 | |
---|
211 | pdd = ddoubles; |
---|
212 | while (pdd->estring) { |
---|
213 | line(pdd->line); |
---|
214 | errno = 0; |
---|
215 | func(); |
---|
216 | pdd++; |
---|
217 | } |
---|
218 | } |
---|
219 | |
---|
220 | |
---|
221 | void |
---|
222 | deltest (void) |
---|
223 | { |
---|
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"); |
---|
249 | } |
---|
250 | |
---|
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) |
---|
255 | { |
---|
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; |
---|
260 | |
---|
261 | |
---|
262 | newfunc( "sprintf"); |
---|
263 | |
---|
264 | |
---|
265 | while (s->line) |
---|
266 | { |
---|
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++; |
---|
272 | } |
---|
273 | |
---|
274 | while (si->line) |
---|
275 | { |
---|
276 | line( si->line); |
---|
277 | sprintf(buffer, si->format_string, si->value); |
---|
278 | test_sok(buffer, si->result); |
---|
279 | si++; |
---|
280 | } |
---|
281 | } |
---|
282 | |
---|
283 | /* Scanf calls strtod etc tested elsewhere, but also has some pattern matching skills */ |
---|
284 | void |
---|
285 | test_scan (void) |
---|
286 | { |
---|
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; |
---|
292 | |
---|
293 | newfunc( "scanf"); |
---|
294 | |
---|
295 | /* Test scanf by converting all the numbers in the sprint vectors |
---|
296 | to and from their source and making sure nothing breaks */ |
---|
297 | |
---|
298 | while (s->line) |
---|
299 | { |
---|
300 | |
---|
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++; |
---|
308 | } |
---|
309 | |
---|
310 | /* And integers too */ |
---|
311 | while (si->line) |
---|
312 | { |
---|
313 | |
---|
314 | long d0,d1; |
---|
315 | |
---|
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++; |
---|
322 | } |
---|
323 | |
---|
324 | /* And the string matching */ |
---|
325 | |
---|
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); |
---|
331 | |
---|
332 | sscanf("magicXYZZYfoobar","magic%[XYZ]", buffer); |
---|
333 | test_sok("XYZZY", buffer); |
---|
334 | sscanf("magicXYZZYfoobar","%[^XYZ]", buffer); |
---|
335 | test_sok("magic", buffer); |
---|
336 | } |
---|
337 | |
---|
338 | void |
---|
339 | test_cvt (void) |
---|
340 | { |
---|
341 | deltest(); |
---|
342 | |
---|
343 | diterate(test_fcvtbuf,"fcvtbuf"); |
---|
344 | diterate(test_fcvt,"fcvt/fcvtf"); |
---|
345 | |
---|
346 | diterate(test_gcvt,"gcvt/gcvtf"); |
---|
347 | diterate(test_ecvtbuf,"ecvtbuf"); |
---|
348 | diterate(test_ecvt,"ecvt/ecvtf"); |
---|
349 | |
---|
350 | iterate(test_strtod, "strtod"); |
---|
351 | |
---|
352 | test_scan(); |
---|
353 | test_sprint(); |
---|
354 | iterate(test_atof, "atof"); |
---|
355 | iterate(test_atoff, "atoff"); |
---|
356 | |
---|
357 | iterate(test_strtof, "strtof"); |
---|
358 | |
---|
359 | int_iterate(test_atoi,"atoi"); |
---|
360 | int_iterate(test_atol,"atol"); |
---|
361 | int_iterate(test_strtol, "strtol"); |
---|
362 | } |
---|