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_Qfmt, strtoIQ, strtopQ, and strtorQ.
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 * #hex0 hex1 hex2 hex3
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 * hex0 is a string of <= 8 Hex digits for the most significant
48*f14fb602SLionel Sambuc * word of the number, hex1 is a similar string for the next
49*f14fb602SLionel Sambuc * word, etc., and ndig is a parameters to g_Qfmt.
50*f14fb602SLionel Sambuc */
51*f14fb602SLionel Sambuc
52*f14fb602SLionel Sambuc #include "gdtoa.h"
53*f14fb602SLionel Sambuc #include <stdio.h>
54*f14fb602SLionel Sambuc #include <stdlib.h>
55*f14fb602SLionel Sambuc #include <string.h>
56*f14fb602SLionel Sambuc
57*f14fb602SLionel Sambuc extern int getround ANSI((int,char*));
58*f14fb602SLionel Sambuc
59*f14fb602SLionel Sambuc static char ibuf[2048], obuf[2048];
60*f14fb602SLionel Sambuc #undef _0
61*f14fb602SLionel Sambuc #undef _1
62*f14fb602SLionel Sambuc
63*f14fb602SLionel Sambuc /* one or the other of IEEE_BIG_ENDIAN or IEEE_LITTLE_ENDIAN should be #defined */
64*f14fb602SLionel Sambuc
65*f14fb602SLionel Sambuc #ifdef IEEE_BIG_ENDIAN
66*f14fb602SLionel Sambuc #define _0 0
67*f14fb602SLionel Sambuc #define _1 1
68*f14fb602SLionel Sambuc #define _2 2
69*f14fb602SLionel Sambuc #define _3 3
70*f14fb602SLionel Sambuc #endif
71*f14fb602SLionel Sambuc #ifdef IEEE_LITTLE_ENDIAN
72*f14fb602SLionel Sambuc #define _0 3
73*f14fb602SLionel Sambuc #define _1 2
74*f14fb602SLionel Sambuc #define _2 1
75*f14fb602SLionel Sambuc #define _3 0
76*f14fb602SLionel Sambuc #endif
77*f14fb602SLionel Sambuc
78*f14fb602SLionel Sambuc #define U (unsigned long)
79*f14fb602SLionel Sambuc
80*f14fb602SLionel Sambuc int
main(Void)81*f14fb602SLionel Sambuc main(Void)
82*f14fb602SLionel Sambuc {
83*f14fb602SLionel Sambuc char *s, *s1, *se, *se1;
84*f14fb602SLionel Sambuc int Ltest, i, dItry, ndig = 0, r = 1;
85*f14fb602SLionel Sambuc union { long double d; ULong bits[4]; } u, v[2], w;
86*f14fb602SLionel Sambuc
87*f14fb602SLionel Sambuc w.bits[0] = w.bits[3] = 0;
88*f14fb602SLionel Sambuc w.d = 1.;
89*f14fb602SLionel Sambuc u.d = 3.;
90*f14fb602SLionel Sambuc w.d = w.d / u.d;
91*f14fb602SLionel Sambuc Ltest = sizeof(long double) == 16 && w.bits[0] && w.bits[3];
92*f14fb602SLionel Sambuc while( (s = fgets(ibuf, sizeof(ibuf), stdin)) !=0) {
93*f14fb602SLionel Sambuc while(*s <= ' ')
94*f14fb602SLionel Sambuc if (!*s++)
95*f14fb602SLionel Sambuc continue;
96*f14fb602SLionel Sambuc dItry = 0;
97*f14fb602SLionel Sambuc switch(*s) {
98*f14fb602SLionel Sambuc case 'r':
99*f14fb602SLionel Sambuc r = getround(r, s);
100*f14fb602SLionel Sambuc continue;
101*f14fb602SLionel Sambuc case 'n':
102*f14fb602SLionel Sambuc i = s[1];
103*f14fb602SLionel Sambuc if (i <= ' ' || (i >= '0' && i <= '9')) {
104*f14fb602SLionel Sambuc ndig = atoi(s+1);
105*f14fb602SLionel Sambuc continue;
106*f14fb602SLionel Sambuc }
107*f14fb602SLionel Sambuc break; /* nan? */
108*f14fb602SLionel Sambuc case '#':
109*f14fb602SLionel Sambuc /* sscanf(s+1, "%lx %lx %lx %lx", &u.bits[_0], */
110*f14fb602SLionel Sambuc /* &u.bits[_1], &u.bits[_2], &u.bits[_3]); */
111*f14fb602SLionel Sambuc u.bits[_0] = (ULong)strtoul(s1 = s+1, &se, 16);
112*f14fb602SLionel Sambuc if (se > s1) {
113*f14fb602SLionel Sambuc u.bits[_1] = (ULong)strtoul(s1 = se, &se, 16);
114*f14fb602SLionel Sambuc if (se > s1) {
115*f14fb602SLionel Sambuc u.bits[_2] = (ULong)strtoul(s1 = se, &se, 16);
116*f14fb602SLionel Sambuc if (se > s1)
117*f14fb602SLionel Sambuc u.bits[_3] = (ULong)strtoul(s1 = se, &se, 16);
118*f14fb602SLionel Sambuc }
119*f14fb602SLionel Sambuc }
120*f14fb602SLionel Sambuc printf("\nInput: %s", ibuf);
121*f14fb602SLionel Sambuc printf(" --> f = #%lx %lx %lx %lx\n", U u.bits[_0],
122*f14fb602SLionel Sambuc U u.bits[_1], U u.bits[_2], U u.bits[_3]);
123*f14fb602SLionel Sambuc goto fmt_test;
124*f14fb602SLionel Sambuc }
125*f14fb602SLionel Sambuc dItry = 1;
126*f14fb602SLionel Sambuc printf("\nInput: %s", ibuf);
127*f14fb602SLionel Sambuc i = strtorQ(ibuf, &se, r, u.bits);
128*f14fb602SLionel Sambuc if (r == 1 && (strtopQ(ibuf,&se1,v[0].bits) != i
129*f14fb602SLionel Sambuc || se != se1 || memcmp(u.bits, v[0].bits, 16)))
130*f14fb602SLionel Sambuc printf("***strtoQ and strtorQ disagree!!\n:");
131*f14fb602SLionel Sambuc printf("\nstrtoQ consumes %d bytes and returns %d\n",
132*f14fb602SLionel Sambuc (int)(se-ibuf), i);
133*f14fb602SLionel Sambuc printf("with bits = #%lx %lx %lx %lx\n",
134*f14fb602SLionel Sambuc U u.bits[_0], U u.bits[_1], U u.bits[_2], U u.bits[_3]);
135*f14fb602SLionel Sambuc fmt_test:
136*f14fb602SLionel Sambuc if (Ltest)
137*f14fb602SLionel Sambuc printf("printf(\"%%.35Lg\") gives %.35Lg\n", u.d);
138*f14fb602SLionel Sambuc se = g_Qfmt(obuf, u.bits, ndig, sizeof(obuf));
139*f14fb602SLionel Sambuc printf("g_Qfmt(%d) gives %d bytes: \"%s\"\n\n", ndig,
140*f14fb602SLionel Sambuc (int)(se-obuf), se ? obuf : "<null>");
141*f14fb602SLionel Sambuc if (!dItry)
142*f14fb602SLionel Sambuc continue;
143*f14fb602SLionel Sambuc printf("strtoIQ returns %d,",
144*f14fb602SLionel Sambuc strtoIQ(ibuf, &se, v[0].bits, v[1].bits));
145*f14fb602SLionel Sambuc printf(" consuming %d bytes.\n", (int)(se-ibuf));
146*f14fb602SLionel Sambuc if (!memcmp(v[0].bits, v[1].bits, 16)) {
147*f14fb602SLionel Sambuc if (!memcpy(u.bits, v[0].bits, 16))
148*f14fb602SLionel Sambuc printf("fI[0] == fI[1] == strtoQ\n");
149*f14fb602SLionel Sambuc else {
150*f14fb602SLionel Sambuc printf("fI[0] == fI[1] = #%lx %lx %lx %lx\n",
151*f14fb602SLionel Sambuc U v[0].bits[_0], U v[0].bits[_1],
152*f14fb602SLionel Sambuc U v[0].bits[_2], U v[0].bits[_3]);
153*f14fb602SLionel Sambuc if (Ltest)
154*f14fb602SLionel Sambuc printf("= %.35Lg\n", v[0].d);
155*f14fb602SLionel Sambuc }
156*f14fb602SLionel Sambuc }
157*f14fb602SLionel Sambuc else {
158*f14fb602SLionel Sambuc printf("fI[0] = #%lx %lx %lx %lx\n",
159*f14fb602SLionel Sambuc U v[0].bits[_0], U v[0].bits[_1],
160*f14fb602SLionel Sambuc U v[0].bits[_2], U v[0].bits[_3]);
161*f14fb602SLionel Sambuc if (Ltest)
162*f14fb602SLionel Sambuc printf("= %.35Lg\n", v[0].d);
163*f14fb602SLionel Sambuc printf("fI[1] = #%lx %lx %lx %lx\n",
164*f14fb602SLionel Sambuc U v[1].bits[_0], U v[1].bits[_1],
165*f14fb602SLionel Sambuc U v[1].bits[_2], U v[1].bits[_3]);
166*f14fb602SLionel Sambuc if (Ltest)
167*f14fb602SLionel Sambuc printf("= %.35Lg\n", v[1].d);
168*f14fb602SLionel Sambuc if (!memcmp(v[0].bits, u.bits, 16))
169*f14fb602SLionel Sambuc printf("fI[0] == strtod\n");
170*f14fb602SLionel Sambuc else if (!memcmp(v[1].bits, u.bits, 16))
171*f14fb602SLionel Sambuc printf("fI[1] == strtod\n");
172*f14fb602SLionel Sambuc else
173*f14fb602SLionel Sambuc printf("**** Both differ from strtod ****\n");
174*f14fb602SLionel Sambuc }
175*f14fb602SLionel Sambuc printf("\n");
176*f14fb602SLionel Sambuc }
177*f14fb602SLionel Sambuc return 0;
178*f14fb602SLionel Sambuc }
179