xref: /minix3/lib/libc/gdtoa/test/ftest.c (revision f14fb602092e015ff630df58e17c2a9cd57d29b3)
1*f14fb602SLionel Sambuc /****************************************************************
2*f14fb602SLionel Sambuc 
3*f14fb602SLionel Sambuc The author of this software is David M. Gay.
4*f14fb602SLionel Sambuc 
5*f14fb602SLionel Sambuc Copyright (C) 1998-2001 by Lucent Technologies
6*f14fb602SLionel Sambuc All Rights Reserved
7*f14fb602SLionel Sambuc 
8*f14fb602SLionel Sambuc Permission to use, copy, modify, and distribute this software and
9*f14fb602SLionel Sambuc its documentation for any purpose and without fee is hereby
10*f14fb602SLionel Sambuc granted, provided that the above copyright notice appear in all
11*f14fb602SLionel Sambuc copies and that both that the copyright notice and this
12*f14fb602SLionel Sambuc permission notice and warranty disclaimer appear in supporting
13*f14fb602SLionel Sambuc documentation, and that the name of Lucent or any of its entities
14*f14fb602SLionel Sambuc not be used in advertising or publicity pertaining to
15*f14fb602SLionel Sambuc distribution of the software without specific, written prior
16*f14fb602SLionel Sambuc permission.
17*f14fb602SLionel Sambuc 
18*f14fb602SLionel Sambuc LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19*f14fb602SLionel Sambuc INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
20*f14fb602SLionel Sambuc IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
21*f14fb602SLionel Sambuc SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
22*f14fb602SLionel Sambuc WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
23*f14fb602SLionel Sambuc IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
24*f14fb602SLionel Sambuc ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
25*f14fb602SLionel Sambuc THIS SOFTWARE.
26*f14fb602SLionel Sambuc 
27*f14fb602SLionel Sambuc ****************************************************************/
28*f14fb602SLionel Sambuc 
29*f14fb602SLionel Sambuc /* Please send bug reports to David M. Gay (dmg at acm dot org,
30*f14fb602SLionel Sambuc  * with " at " changed at "@" and " dot " changed to ".").	*/
31*f14fb602SLionel Sambuc 
32*f14fb602SLionel Sambuc /* Test program for g_ffmt, strtof, strtoIf, strtopf, and strtorf.
33*f14fb602SLionel Sambuc  *
34*f14fb602SLionel Sambuc  * Inputs (on stdin):
35*f14fb602SLionel Sambuc  *		r rounding_mode
36*f14fb602SLionel Sambuc  *		n ndig
37*f14fb602SLionel Sambuc  *		number
38*f14fb602SLionel Sambuc  *		#hex
39*f14fb602SLionel Sambuc  *
40*f14fb602SLionel Sambuc  *	rounding_mode values:
41*f14fb602SLionel Sambuc  *		0 = toward zero
42*f14fb602SLionel Sambuc  *		1 = nearest
43*f14fb602SLionel Sambuc  *		2 = toward +Infinity
44*f14fb602SLionel Sambuc  *		3 = toward -Infinity
45*f14fb602SLionel Sambuc  *
46*f14fb602SLionel Sambuc  * where number is a decimal floating-point number,
47*f14fb602SLionel Sambuc  * hex is a string of <= 8 Hex digits for the internal representation
48*f14fb602SLionel Sambuc  * of the number, and ndig is a parameters to g_ffmt.
49*f14fb602SLionel Sambuc  */
50*f14fb602SLionel Sambuc 
51*f14fb602SLionel Sambuc #include "gdtoa.h"
52*f14fb602SLionel Sambuc #include <stdio.h>
53*f14fb602SLionel Sambuc #include <stdlib.h>
54*f14fb602SLionel Sambuc 
55*f14fb602SLionel Sambuc  extern int getround ANSI((int,char*));
56*f14fb602SLionel Sambuc 
57*f14fb602SLionel Sambuc  static char ibuf[2048], obuf[1024];
58*f14fb602SLionel Sambuc 
59*f14fb602SLionel Sambuc #define U (unsigned long)
60*f14fb602SLionel Sambuc 
61*f14fb602SLionel Sambuc  int
main(Void)62*f14fb602SLionel Sambuc main(Void)
63*f14fb602SLionel Sambuc {
64*f14fb602SLionel Sambuc 	char *s, *se, *se1;
65*f14fb602SLionel Sambuc 	int dItry, i, i1, ndig = 0, r = 1;
66*f14fb602SLionel Sambuc 	float f1, fI[2];
67*f14fb602SLionel Sambuc 	union { float f; ULong L[1]; } u;
68*f14fb602SLionel Sambuc 
69*f14fb602SLionel Sambuc 	while( (s = fgets(ibuf, sizeof(ibuf), stdin)) !=0) {
70*f14fb602SLionel Sambuc 		while(*s <= ' ')
71*f14fb602SLionel Sambuc 			if (!*s++)
72*f14fb602SLionel Sambuc 				continue;
73*f14fb602SLionel Sambuc 		dItry = 0;
74*f14fb602SLionel Sambuc 		switch(*s) {
75*f14fb602SLionel Sambuc 		  case 'r':
76*f14fb602SLionel Sambuc 			r = getround(r, s);
77*f14fb602SLionel Sambuc 			continue;
78*f14fb602SLionel Sambuc 		  case 'n':
79*f14fb602SLionel Sambuc 			i = s[1];
80*f14fb602SLionel Sambuc 			if (i <= ' ' || (i >= '0' && i <= '9')) {
81*f14fb602SLionel Sambuc 				ndig = atoi(s+1);
82*f14fb602SLionel Sambuc 				continue;
83*f14fb602SLionel Sambuc 				}
84*f14fb602SLionel Sambuc 			break; /* nan? */
85*f14fb602SLionel Sambuc 		  case '#':
86*f14fb602SLionel Sambuc 			/* sscanf(s+1, "%lx", &u.L[0]); */
87*f14fb602SLionel Sambuc 			u.L[0] = (ULong)strtoul(s+1, &se, 16);
88*f14fb602SLionel Sambuc 			printf("\nInput: %s", ibuf);
89*f14fb602SLionel Sambuc 			printf(" --> f = #%lx\n", U u.L[0]);
90*f14fb602SLionel Sambuc 			goto fmt_test;
91*f14fb602SLionel Sambuc 			}
92*f14fb602SLionel Sambuc 		dItry = 1;
93*f14fb602SLionel Sambuc 		printf("\nInput: %s", ibuf);
94*f14fb602SLionel Sambuc 		i = strtorf(ibuf, &se, r, &u.f);
95*f14fb602SLionel Sambuc 		if (r == 1) {
96*f14fb602SLionel Sambuc 		    if (u.f != (i1 = strtopf(ibuf, &se1, &f1), f1)
97*f14fb602SLionel Sambuc 				 || se != se1 || i != i1) {
98*f14fb602SLionel Sambuc 			printf("***strtopf and strtorf disagree!!\n");
99*f14fb602SLionel Sambuc 			if (u.f != f1)
100*f14fb602SLionel Sambuc 				printf("\tf1 = %g\n", (double)f1);
101*f14fb602SLionel Sambuc 			if (i != i1)
102*f14fb602SLionel Sambuc 				printf("\ti = %d but i1 = %d\n", i, i1);
103*f14fb602SLionel Sambuc 			if (se != se1)
104*f14fb602SLionel Sambuc 				printf("se - se1 = %d\n", (int)(se-se1));
105*f14fb602SLionel Sambuc 			}
106*f14fb602SLionel Sambuc 		    if (u.f != strtof(ibuf, &se1) || se != se1)
107*f14fb602SLionel Sambuc 			printf("***strtof and strtorf disagree!\n");
108*f14fb602SLionel Sambuc 		    }
109*f14fb602SLionel Sambuc 		printf("strtof consumes %d bytes and returns %.8g = #%lx\n",
110*f14fb602SLionel Sambuc 				(int)(se-ibuf), u.f, U u.L[0]);
111*f14fb602SLionel Sambuc  fmt_test:
112*f14fb602SLionel Sambuc 		se = g_ffmt(obuf, &u.f, ndig, sizeof(obuf));
113*f14fb602SLionel Sambuc 		printf("g_ffmt(%d) gives %d bytes: \"%s\"\n\n",
114*f14fb602SLionel Sambuc 			ndig, (int)(se-obuf), se ? obuf : "<null>");
115*f14fb602SLionel Sambuc 		if (!dItry)
116*f14fb602SLionel Sambuc 			continue;
117*f14fb602SLionel Sambuc 		printf("strtoIf returns %d,", strtoIf(ibuf, &se, fI, &fI[1]));
118*f14fb602SLionel Sambuc 		printf(" consuming %d bytes.\n", (int)(se-ibuf));
119*f14fb602SLionel Sambuc 		if (fI[0] == fI[1]) {
120*f14fb602SLionel Sambuc 			if (fI[0] == u.f)
121*f14fb602SLionel Sambuc 				printf("fI[0] == fI[1] == strtof\n");
122*f14fb602SLionel Sambuc 			else
123*f14fb602SLionel Sambuc 				printf("fI[0] == fI[1] = #%lx = %.8g\n",
124*f14fb602SLionel Sambuc 					U *(ULong*)fI, fI[0]);
125*f14fb602SLionel Sambuc 			}
126*f14fb602SLionel Sambuc 		else {
127*f14fb602SLionel Sambuc 			printf("fI[0] = #%lx = %.8g\nfI[1] = #%lx = %.8g\n",
128*f14fb602SLionel Sambuc 				U *(ULong*)fI, fI[0],
129*f14fb602SLionel Sambuc 				U *(ULong*)&fI[1], fI[1]);
130*f14fb602SLionel Sambuc 			if (fI[0] == u.f)
131*f14fb602SLionel Sambuc 				printf("fI[0] == strtof\n");
132*f14fb602SLionel Sambuc 			else if (fI[1] == u.f)
133*f14fb602SLionel Sambuc 				printf("fI[1] == strtof\n");
134*f14fb602SLionel Sambuc 			else
135*f14fb602SLionel Sambuc 				printf("**** Both differ from strtof ****\n");
136*f14fb602SLionel Sambuc 			}
137*f14fb602SLionel Sambuc 		printf("\n");
138*f14fb602SLionel Sambuc 		}
139*f14fb602SLionel Sambuc 	return 0;
140*f14fb602SLionel Sambuc 	}
141