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