]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - contrib/gdtoa/test/strtodt.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / contrib / gdtoa / test / strtodt.c
1 /****************************************************************
2
3 The author of this software is David M. Gay.
4
5 Copyright (C) 2001 by Lucent Technologies
6 All Rights Reserved
7
8 Permission to use, copy, modify, and distribute this software and
9 its documentation for any purpose and without fee is hereby
10 granted, provided that the above copyright notice appear in all
11 copies and that both that the copyright notice and this
12 permission notice and warranty disclaimer appear in supporting
13 documentation, and that the name of Lucent or any of its entities
14 not be used in advertising or publicity pertaining to
15 distribution of the software without specific, written prior
16 permission.
17
18 LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
20 IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
21 SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
22 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
23 IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
24 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
25 THIS SOFTWARE.
26
27 ****************************************************************/
28
29 /* Please send bug reports to David M. Gay (dmg at acm dot org,
30  * with " at " changed at "@" and " dot " changed to ".").      */
31
32 /* Test strtod.  */
33
34 /* On stdin, read triples: d x y:
35  *      d = decimal string
36  *      x = high-order Hex value expected from strtod
37  *      y = low-order Hex value
38  * Complain about errors.
39  */
40
41 #include "gdtoa.h"      /* for ULong */
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45
46  static int W0, W1;
47  typedef union {
48                 double d;
49                 ULong L[2];
50                 } U;
51
52  static int
53 process(char *fname, FILE *f)
54 {
55         U a, b;
56         char buf[2048];
57         char *s, *s1, *se;
58         int line, n;
59
60         line = n = 0;
61
62  top:
63         while(fgets(s = buf, sizeof(buf), f)) {
64                 line++;
65                 while(*s <= ' ')
66                         if (!*s++)
67                                 goto top; /* break 2 */
68                 if (*s == '#')
69                         continue;
70                 while(*s > ' ')
71                         s++;
72                 /* if (sscanf(s,"\t%lx\t%lx", &a.L[0], &a.L[1]) != 2) */
73                 if ((a.L[0] = (ULong)strtoul(s, &s1,16), s1 <= s)
74                  || (a.L[1] = (ULong)strtoul(s1,&se,16), se <= s1)) {
75                         printf("Badly formatted line %d of %s\n",
76                                 line, fname);
77                         n++;
78                         continue;
79                         }
80                 b.d = strtod(buf,0);
81                 if (b.L[W0] != a.L[0] || b.L[W1] != a.L[1]) {
82                         n++;
83                         printf("Line %d of %s: got %lx %lx; expected %lx %lx\n",
84                                 line, fname, b.L[W0], b.L[W1], a.L[0], a.L[1]);
85                         }
86                 }
87         return n;
88         }
89
90  int
91 main(int argc, char **argv)
92 {
93         FILE *f;
94         char *prog, *s;
95         int n, rc;
96         U u;
97
98         prog = argv[0];
99         if (argc == 2 && !strcmp(argv[1],"-?")) {
100                 fprintf(stderr, "Usage: %s [file [file...]]\n"
101                         "\tto read data file(s) of tab-separated triples d x y with\n"
102                         "\t\td decimal string\n"
103                         "\t\tx = high-order Hex value expected from strtod\n"
104                         "\t\ty = low-order Hex value\n"
105                         "\tComplain about errors by strtod.\n"
106                         "\tIf no files, read triples from stdin.\n",
107                         prog);
108                 return 0;
109                 }
110
111         /* determine endian-ness */
112
113         u.d = 1.;
114         W0 = u.L[0] == 0;
115         W1 = 1 - W0;
116
117         /* test */
118
119         n = rc = 0;
120         if (argc <= 1)
121                 n = process("<stdin>", stdin);
122         else
123                 while(s = *++argv)
124                         if (f = fopen(s,"r")) {
125                                 n += process(s, f);
126                                 fclose(f);
127                                 }
128                         else {
129                                 rc = 2;
130                                 fprintf(stderr, "Cannot open %s\n", s);
131                                 }
132         printf("%d bad conversions\n", n);
133         if (n)
134                 rc |= 1;
135         return rc;
136         }