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