OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-stable/] [newlib-1.18.0/] [newlib/] [libm/] [test/] [convert.c] - Blame information for rev 207

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 207 jeremybenn
/* 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
_DEFUN_VOID(test_strtod)
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
_DEFUN_VOID(test_strtof)
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
_DEFUN_VOID(test_atof)
39
{
40
  test_mok(atof(pd->string), pd->value, 64);
41
}
42
 
43
void
44
_DEFUN_VOID(test_atoff)
45
{
46
  test_mok(atoff(pd->string), pd->value, 32);
47
}
48
 
49
 
50
static
51
void
52
_DEFUN(iterate,(func, name),
53
       void _EXFUN((*func),(void)) _AND
54
       char *name)
55
{
56
 
57
  newfunc(name);
58
  pd = doubles;
59
  while (pd->string) {
60
    line(pd->line);
61
    func();
62
    pd++;
63
  }
64
}
65
 
66
 
67
extern int_type ints[];
68
 
69
int_type *p = ints;
70
 
71
 
72
static void
73
_DEFUN(int_iterate,(func, name),
74
       void (*func)() _AND
75
       char *name)
76
{
77
  newfunc(name);
78
 
79
  p = ints;
80
  while (p->string) {
81
    line(p->line);
82
    errno = 0;
83
    func();
84
    p++;
85
  }
86
}
87
 
88
void
89
_DEFUN(test_strtol_base,(base, pi, string),
90
       int base _AND
91
       int_scan_type *pi _AND
92
       char *string)
93
{
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);
101
}
102
 
103
void
104
_DEFUN_VOID(test_strtol)
105
{
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);
111
}
112
 
113
void
114
_DEFUN_VOID(test_atoi)
115
{
116
  test_iok(atoi(p->string), p->decimal.value);
117
  test_eok(errno, p->decimal.errno_val);
118
}
119
 
120
void
121
_DEFUN_VOID(test_atol)
122
{
123
  test_iok(atol(p->string), p->decimal.value);
124
  test_eok(errno, p->decimal.errno_val);
125
}
126
 
127
/* test ECVT and friends */
128
extern ddouble_type ddoubles[];
129
ddouble_type *pdd;
130
void
131
_DEFUN_VOID(test_ecvtbuf)
132
{
133
  int a2,a3;
134
  char *s;
135
  s =  ecvtbuf(pdd->value, pdd->e1, &a2, &a3, buffer);
136
 
137
  test_sok(s,pdd->estring);
138
  test_iok(pdd->e2,a2);
139
  test_iok(pdd->e3,a3);
140
}
141
 
142
void
143
_DEFUN_VOID(test_ecvt)
144
{
145
  int a2,a3;
146
  char *s;
147
  s =  ecvt(pdd->value, pdd->e1, &a2, &a3);
148
 
149
  test_sok(s,pdd->estring);
150
  test_iok(pdd->e2,a2);
151
  test_iok(pdd->e3,a3);
152
 
153
  s =  ecvtf(pdd->value, pdd->e1, &a2, &a3);
154
 
155
  test_sok(s,pdd->estring);
156
  test_iok(pdd->e2,a2);
157
  test_iok(pdd->e3,a3);
158
}
159
 
160
void
161
_DEFUN_VOID(test_fcvtbuf)
162
{
163
  int a2,a3;
164
  char *s;
165
  s =  fcvtbuf(pdd->value, pdd->f1, &a2, &a3, buffer);
166
 
167
  test_scok(s,pdd->fstring,10);
168
  test_iok(pdd->f2,a2);
169
  test_iok(pdd->f3,a3);
170
}
171
 
172
void
173
_DEFUN_VOID(test_gcvt)
174
{
175
  char *s = gcvt(pdd->value, pdd->g1, buffer);
176
  test_scok(s, pdd->gstring, 9);
177
 
178
  s = gcvtf(pdd->value, pdd->g1, buffer);
179
  test_scok(s, pdd->gstring, 9);
180
 
181
}
182
 
183
void
184
_DEFUN_VOID(test_fcvt)
185
{
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);
192
 
193
  test_scok(sd,pdd->fstring,10);
194
  test_iok(pdd->f2,a2);
195
  test_iok(pdd->f3,a3);
196
 
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);
205
}
206
 
207
static void
208
 
209
_DEFUN(diterate,(func, name),
210
       void (*func)() _AND
211
       char *name)
212
{
213
  newfunc(name);
214
 
215
  pdd = ddoubles;
216
  while (pdd->estring) {
217
    line(pdd->line);
218
    errno = 0;
219
    func();
220
    pdd++;
221
  }
222
}
223
 
224
 
225
void
226
_DEFUN_VOID(deltest)
227
{
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");
253
}
254
 
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)
259
{
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;
264
 
265
 
266
  newfunc( "sprintf");
267
 
268
 
269
  while (s->line)
270
  {
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++;
276
  }
277
 
278
  while (si->line)
279
  {
280
    line( si->line);
281
    sprintf(buffer, si->format_string, si->value);
282
    test_sok(buffer, si->result);
283
    si++;
284
  }
285
}
286
 
287
/* Scanf calls strtod etc tested elsewhere, but also has some pattern matching skills */
288
void
289
_DEFUN_VOID(test_scan)
290
{
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;
296
 
297
  newfunc( "scanf");
298
 
299
  /* Test scanf by converting all the numbers in the sprint vectors
300
     to and from their source and making sure nothing breaks */
301
 
302
  while (s->line)
303
  {
304
 
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++;
312
  }
313
 
314
  /* And integers too */
315
  while (si->line)
316
  {
317
 
318
    long d0,d1;
319
 
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++;
326
  }
327
 
328
  /* And the string matching */
329
 
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);
335
 
336
  sscanf("magicXYZZYfoobar","magic%[XYZ]", buffer);
337
  test_sok("XYZZY", buffer);
338
  sscanf("magicXYZZYfoobar","%[^XYZ]", buffer);
339
  test_sok("magic", buffer);
340
}
341
 
342
void
343
_DEFUN_VOID(test_cvt)
344
{
345
  deltest();
346
 
347
  diterate(test_fcvtbuf,"fcvtbuf");
348
  diterate(test_fcvt,"fcvt/fcvtf");
349
 
350
  diterate(test_gcvt,"gcvt/gcvtf");
351
  diterate(test_ecvtbuf,"ecvtbuf");
352
  diterate(test_ecvt,"ecvt/ecvtf");
353
 
354
  iterate(test_strtod, "strtod");
355
 
356
  test_scan();
357
  test_sprint();
358
  iterate(test_atof, "atof");
359
  iterate(test_atoff, "atoff");
360
 
361
  iterate(test_strtof, "strtof");
362
 
363
  int_iterate(test_atoi,"atoi");
364
  int_iterate(test_atol,"atol");
365
  int_iterate(test_strtol, "strtol");
366
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.