1 | /****************************************************************
|
---|
2 |
|
---|
3 | The author of this software is David M. Gay.
|
---|
4 |
|
---|
5 | Copyright (C) 1998-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
|
---|
30 | David M. Gay
|
---|
31 | Bell Laboratories, Room 2C-463
|
---|
32 | 600 Mountain Avenue
|
---|
33 | Murray Hill, NJ 07974-0636
|
---|
34 | U.S.A.
|
---|
35 | dmg@bell-labs.com
|
---|
36 | */
|
---|
37 |
|
---|
38 | /* Test program for g_xfmt, strtoIx, strtopx, and strtorx.
|
---|
39 | *
|
---|
40 | * Inputs (on stdin):
|
---|
41 | * r rounding_mode
|
---|
42 | * n ndig
|
---|
43 | * number
|
---|
44 | * #hex0 hex1 hex2 hex3 hex4
|
---|
45 | *
|
---|
46 | * rounding_mode values:
|
---|
47 | * 0 = toward zero
|
---|
48 | * 1 = nearest
|
---|
49 | * 2 = toward +Infinity
|
---|
50 | * 3 = toward -Infinity
|
---|
51 | *
|
---|
52 | * where number is a decimal floating-point number,
|
---|
53 | * hex0 is a string of <= 4 Hex digits for the most significant
|
---|
54 | * half-word of the number, hex1 is a similar string for the next
|
---|
55 | * half-word, etc., and ndig is a parameters to g_xfmt.
|
---|
56 | */
|
---|
57 |
|
---|
58 | #include "gdtoa.h"
|
---|
59 | #include <stdio.h>
|
---|
60 | #include <stdlib.h>
|
---|
61 | #include <string.h>
|
---|
62 |
|
---|
63 | extern int getround ANSI((int,char*));
|
---|
64 |
|
---|
65 | static char ibuf[2048], obuf[2048];
|
---|
66 |
|
---|
67 | #undef _0
|
---|
68 | #undef _1
|
---|
69 |
|
---|
70 | /* one or the other of IEEE_MC68k or IEEE_8087 should be #defined */
|
---|
71 |
|
---|
72 | #ifdef IEEE_MC68k
|
---|
73 | #define _0 0
|
---|
74 | #define _1 1
|
---|
75 | #define _2 2
|
---|
76 | #define _3 3
|
---|
77 | #define _4 4
|
---|
78 | #endif
|
---|
79 | #ifdef IEEE_8087
|
---|
80 | #define _0 4
|
---|
81 | #define _1 3
|
---|
82 | #define _2 2
|
---|
83 | #define _3 1
|
---|
84 | #define _4 0
|
---|
85 | #endif
|
---|
86 |
|
---|
87 | int
|
---|
88 | main(Void)
|
---|
89 | {
|
---|
90 | char *s, *se, *se1;
|
---|
91 | int i, dItry, ndig = 0, r = 1;
|
---|
92 | union { long double d; UShort bits[5]; } u, v[2];
|
---|
93 |
|
---|
94 | while(s = fgets(ibuf, sizeof(ibuf), stdin)) {
|
---|
95 | while(*s <= ' ')
|
---|
96 | if (!*s++)
|
---|
97 | continue;
|
---|
98 | dItry = 0;
|
---|
99 | switch(*s) {
|
---|
100 | case 'r':
|
---|
101 | r = getround(r, s);
|
---|
102 | continue;
|
---|
103 | case 'n':
|
---|
104 | i = s[1];
|
---|
105 | if (i <= ' ' || i >= '0' && i <= '9') {
|
---|
106 | ndig = atoi(s+1);
|
---|
107 | continue;
|
---|
108 | }
|
---|
109 | break; /* nan? */
|
---|
110 | case '#':
|
---|
111 | sscanf(s+1, "%hx %hx %hx %hx hx", &u.bits[_0],
|
---|
112 | &u.bits[_1], &u.bits[_2], &u.bits[_3],
|
---|
113 | &u.bits[_4]);
|
---|
114 | printf("\nInput: %s", ibuf);
|
---|
115 | printf(" --> f = #%x %x %x %x %x\n", u.bits[_0],
|
---|
116 | u.bits[_1], u.bits[_2], u.bits[_3], u.bits[4]);
|
---|
117 | goto fmt_test;
|
---|
118 | }
|
---|
119 | dItry = 1;
|
---|
120 | printf("\nInput: %s", ibuf);
|
---|
121 | i = strtorx(ibuf, &se, r, u.bits);
|
---|
122 | if (r == 1 && (i != strtopx(ibuf, &se1, v[0].bits) || se1 != se
|
---|
123 | || memcmp(u.bits, v[0].bits, 10)))
|
---|
124 | printf("***strtox and strtorx disagree!!\n:");
|
---|
125 | printf("\nstrtox consumes %d bytes and returns %d\n",
|
---|
126 | (int)(se-ibuf), i);
|
---|
127 | printf("with bits = #%x %x %x %x %x\n",
|
---|
128 | u.bits[_0], u.bits[_1], u.bits[_2],
|
---|
129 | u.bits[_3], u.bits[_4]);
|
---|
130 | if (sizeof(long double) == 12)
|
---|
131 | printf("printf(\"%%.21Lg\") gives %.21Lg\n", u.d);
|
---|
132 | fmt_test:
|
---|
133 | se = g_xfmt(obuf, u.bits, ndig, sizeof(obuf));
|
---|
134 | printf("g_xfmt(%d) gives %d bytes: \"%s\"\n\n",
|
---|
135 | ndig, (int)(se-obuf), se ? obuf : "<null>");
|
---|
136 | if (!dItry)
|
---|
137 | continue;
|
---|
138 | printf("strtoIx returns %d,",
|
---|
139 | strtoIx(ibuf, &se, v[0].bits, v[1].bits));
|
---|
140 | printf(" consuming %d bytes.\n", (int)(se-ibuf));
|
---|
141 | if (!memcmp(v[0].bits, v[1].bits, 10)) {
|
---|
142 | if (!memcmp(u.bits, v[0].bits, 10))
|
---|
143 | printf("fI[0] == fI[1] == strtox\n");
|
---|
144 | else {
|
---|
145 | printf("fI[0] == fI[1] = #%x %x %x %x %x\n",
|
---|
146 | v[0].bits[_0], v[0].bits[_1],
|
---|
147 | v[0].bits[_2], v[0].bits[_3],
|
---|
148 | v[0].bits[_4]);
|
---|
149 | if (sizeof(long double) == 12)
|
---|
150 | printf("= %.21Lg\n", v[0].d);
|
---|
151 | }
|
---|
152 | }
|
---|
153 | else {
|
---|
154 | printf("fI[0] = #%x %x %x %x %x\n",
|
---|
155 | v[0].bits[_0], v[0].bits[_1],
|
---|
156 | v[0].bits[_2], v[0].bits[_3],
|
---|
157 | v[0].bits[_4]);
|
---|
158 | if (sizeof(long double) == 12)
|
---|
159 | printf("= %.21Lg\n", v[0].d);
|
---|
160 | printf("fI[1] = #%x %x %x %x %x\n",
|
---|
161 | v[1].bits[_0], v[1].bits[_1],
|
---|
162 | v[1].bits[_2], v[0].bits[_3],
|
---|
163 | v[0].bits[_4]);
|
---|
164 | if (sizeof(long double) == 12)
|
---|
165 | printf("= %.21Lg\n", v[1].d);
|
---|
166 | if (!memcmp(v[0].bits, u.bits, 10))
|
---|
167 | printf("fI[0] == strtox\n");
|
---|
168 | else if (!memcmp(v[1].bits, u.bits, 10))
|
---|
169 | printf("fI[1] == strtox\n");
|
---|
170 | else
|
---|
171 | printf("**** Both differ from strtod ****\n");
|
---|
172 | }
|
---|
173 | printf("\n");
|
---|
174 | }
|
---|
175 | return 0;
|
---|
176 | }
|
---|