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_dfmt, strtoId, strtod, strtopd, and strtord.
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
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 Hex <= 8 digits for the most significant
48*f14fb602SLionel Sambuc * word of the number, hex1 is a similar string for the other
49*f14fb602SLionel Sambuc * (least significant) word, and ndig is a parameters to g_dfmt.
50*f14fb602SLionel Sambuc */
51*f14fb602SLionel Sambuc
52*f14fb602SLionel Sambuc #include "gdtoaimp.h"
53*f14fb602SLionel Sambuc #include <stdio.h>
54*f14fb602SLionel Sambuc #include <stdlib.h>
55*f14fb602SLionel Sambuc
56*f14fb602SLionel Sambuc extern int getround ANSI((int,char*));
57*f14fb602SLionel Sambuc
58*f14fb602SLionel Sambuc static char ibuf[2048], obuf[1024];
59*f14fb602SLionel Sambuc
60*f14fb602SLionel Sambuc #define U (unsigned long)
61*f14fb602SLionel Sambuc
62*f14fb602SLionel Sambuc int
main(Void)63*f14fb602SLionel Sambuc main(Void)
64*f14fb602SLionel Sambuc {
65*f14fb602SLionel Sambuc char *s, *se, *se1;
66*f14fb602SLionel Sambuc double f1, fI[2];
67*f14fb602SLionel Sambuc int i, i1, ndig = 0, r = 1;
68*f14fb602SLionel Sambuc long LL[2];
69*f14fb602SLionel Sambuc union { double f; ULong L[2]; } u;
70*f14fb602SLionel Sambuc
71*f14fb602SLionel Sambuc while( (s = fgets(ibuf, sizeof(ibuf), stdin)) !=0) {
72*f14fb602SLionel Sambuc while(*s <= ' ')
73*f14fb602SLionel Sambuc if (!*s++)
74*f14fb602SLionel Sambuc continue;
75*f14fb602SLionel Sambuc switch(*s) {
76*f14fb602SLionel Sambuc case 'r':
77*f14fb602SLionel Sambuc r = getround(r, s);
78*f14fb602SLionel Sambuc continue;
79*f14fb602SLionel Sambuc case 'n':
80*f14fb602SLionel Sambuc i = s[1];
81*f14fb602SLionel Sambuc if (i <= ' ' || (i >= '0' && i <= '9')) {
82*f14fb602SLionel Sambuc ndig = atoi(s+1);
83*f14fb602SLionel Sambuc continue;
84*f14fb602SLionel Sambuc }
85*f14fb602SLionel Sambuc break; /* nan? */
86*f14fb602SLionel Sambuc case '#':
87*f14fb602SLionel Sambuc LL[0] = u.L[_0];
88*f14fb602SLionel Sambuc LL[1] = u.L[_1];
89*f14fb602SLionel Sambuc sscanf(s+1, "%lx %lx", &LL[0], &LL[1]);
90*f14fb602SLionel Sambuc u.L[_0] = LL[0];
91*f14fb602SLionel Sambuc u.L[_1] = LL[1];
92*f14fb602SLionel Sambuc printf("\nInput: %s", ibuf);
93*f14fb602SLionel Sambuc printf("--> f = #%lx %lx\n", (long)u.L[_0], (long)u.L[_1]);
94*f14fb602SLionel Sambuc goto fmt_test;
95*f14fb602SLionel Sambuc }
96*f14fb602SLionel Sambuc printf("\nInput: %s", ibuf);
97*f14fb602SLionel Sambuc i = strtord(ibuf, &se, r, &u.f);
98*f14fb602SLionel Sambuc if (r == 1) {
99*f14fb602SLionel Sambuc if ((u.f != strtod(ibuf, &se1) || se1 != se))
100*f14fb602SLionel Sambuc printf("***strtod and strtord disagree!!\n");
101*f14fb602SLionel Sambuc i1 = strtopd(ibuf, &se, &f1);
102*f14fb602SLionel Sambuc if (i != i1 || u.f != f1 || se != se1)
103*f14fb602SLionel Sambuc printf("***strtord and strtopd disagree!!\n");
104*f14fb602SLionel Sambuc }
105*f14fb602SLionel Sambuc printf("strtod consumes %d bytes and returns %d with f = %.17g = #%lx %lx\n",
106*f14fb602SLionel Sambuc (int)(se-ibuf), i, u.f, U u.L[_0], U u.L[_1]);
107*f14fb602SLionel Sambuc fmt_test:
108*f14fb602SLionel Sambuc se = g_dfmt(obuf, &u.f, ndig, sizeof(obuf));
109*f14fb602SLionel Sambuc printf("g_dfmt(%d) gives %d bytes: \"%s\"\n\n",
110*f14fb602SLionel Sambuc ndig, (int)(se-obuf), se ? obuf : "<null>");
111*f14fb602SLionel Sambuc if (*s == '#')
112*f14fb602SLionel Sambuc continue;
113*f14fb602SLionel Sambuc printf("strtoId returns %d,", strtoId(ibuf, &se, fI, &fI[1]));
114*f14fb602SLionel Sambuc printf(" consuming %d bytes.\n", (int)(se-ibuf));
115*f14fb602SLionel Sambuc if (fI[0] == fI[1]) {
116*f14fb602SLionel Sambuc if (fI[0] == u.f)
117*f14fb602SLionel Sambuc printf("fI[0] == fI[1] == strtod\n");
118*f14fb602SLionel Sambuc else
119*f14fb602SLionel Sambuc printf("fI[0] == fI[1] = #%lx %lx = %.17g\n",
120*f14fb602SLionel Sambuc U ((ULong*)fI)[_0], U ((ULong*)fI)[_1],
121*f14fb602SLionel Sambuc fI[0]);
122*f14fb602SLionel Sambuc }
123*f14fb602SLionel Sambuc else {
124*f14fb602SLionel Sambuc printf("fI[0] = #%lx %lx = %.17g\n",
125*f14fb602SLionel Sambuc U ((ULong*)fI)[_0], U ((ULong*)fI)[_1], fI[0]);
126*f14fb602SLionel Sambuc printf("fI[1] = #%lx %lx = %.17g\n",
127*f14fb602SLionel Sambuc U ((ULong*)&fI[1])[_0], U ((ULong*)&fI[1])[_1],
128*f14fb602SLionel Sambuc fI[1]);
129*f14fb602SLionel Sambuc if (fI[0] == u.f)
130*f14fb602SLionel Sambuc printf("fI[0] == strtod\n");
131*f14fb602SLionel Sambuc else if (fI[1] == u.f)
132*f14fb602SLionel Sambuc printf("fI[1] == strtod\n");
133*f14fb602SLionel Sambuc else
134*f14fb602SLionel Sambuc printf("**** Both differ from strtod ****\n");
135*f14fb602SLionel Sambuc }
136*f14fb602SLionel Sambuc printf("\n");
137*f14fb602SLionel Sambuc }
138*f14fb602SLionel Sambuc return 0;
139*f14fb602SLionel Sambuc }
140