xtest.c revision 1.2 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_xfmt, strtoIx, strtopx, and strtorx.
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 hex3 hex4
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 <= 4 Hex digits for the most significant
48 1.1 christos * half-word of the number, hex1 is a similar string for the next
49 1.1 christos * half-word, etc., and ndig is a parameters to g_xfmt.
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 #undef _0
62 1.1 christos #undef _1
63 1.1 christos
64 1.2 christos /* one or the other of IEEE_BIG_ENDIAN or IEEE_LITTLE_ENDIAN should be #defined */
65 1.1 christos
66 1.2 christos #ifdef IEEE_BIG_ENDIAN
67 1.1 christos #define _0 0
68 1.1 christos #define _1 1
69 1.1 christos #define _2 2
70 1.1 christos #define _3 3
71 1.1 christos #define _4 4
72 1.1 christos #endif
73 1.2 christos #ifdef IEEE_LITTLE_ENDIAN
74 1.1 christos #define _0 4
75 1.1 christos #define _1 3
76 1.1 christos #define _2 2
77 1.1 christos #define _3 1
78 1.1 christos #define _4 0
79 1.1 christos #endif
80 1.1 christos
81 1.1 christos int
82 1.1 christos main(Void)
83 1.1 christos {
84 1.1 christos char *s, *se, *se1;
85 1.1 christos int i, dItry, ndig = 0, r = 1;
86 1.1 christos union { long double d; UShort bits[5]; } u, v[2];
87 1.1 christos
88 1.1 christos while((s = fgets(ibuf, sizeof(ibuf), stdin))) {
89 1.1 christos while(*s <= ' ')
90 1.1 christos if (!*s++)
91 1.1 christos continue;
92 1.1 christos dItry = 0;
93 1.1 christos switch(*s) {
94 1.1 christos case 'r':
95 1.1 christos r = getround(r, s);
96 1.1 christos continue;
97 1.1 christos case 'n':
98 1.1 christos i = s[1];
99 1.1 christos if (i <= ' ' || (i >= '0' && i <= '9')) {
100 1.1 christos ndig = atoi(s+1);
101 1.1 christos continue;
102 1.1 christos }
103 1.1 christos break; /* nan? */
104 1.1 christos case '#':
105 1.1 christos sscanf(s+1, "%hx %hx %hx %hx %hx", &u.bits[_0],
106 1.1 christos &u.bits[_1], &u.bits[_2], &u.bits[_3],
107 1.1 christos &u.bits[_4]);
108 1.1 christos printf("\nInput: %s", ibuf);
109 1.1 christos printf(" --> f = #%x %x %x %x %x\n", u.bits[_0],
110 1.1 christos u.bits[_1], u.bits[_2], u.bits[_3], u.bits[_4]);
111 1.1 christos goto fmt_test;
112 1.1 christos }
113 1.1 christos dItry = 1;
114 1.1 christos printf("\nInput: %s", ibuf);
115 1.1 christos i = strtorx(ibuf, &se, r, u.bits);
116 1.1 christos if (r == 1 && (i != strtopx(ibuf, &se1, v[0].bits) || se1 != se
117 1.1 christos || memcmp(u.bits, v[0].bits, 10)))
118 1.1 christos printf("***strtox and strtorx disagree!!\n:");
119 1.1 christos printf("\nstrtox consumes %d bytes and returns %d\n",
120 1.1 christos (int)(se-ibuf), i);
121 1.1 christos printf("with bits = #%x %x %x %x %x\n",
122 1.1 christos u.bits[_0], u.bits[_1], u.bits[_2],
123 1.1 christos u.bits[_3], u.bits[_4]);
124 1.1 christos if (sizeof(long double) == 12)
125 1.1 christos printf("printf(\"%%.21Lg\") gives %.21Lg\n", u.d);
126 1.1 christos fmt_test:
127 1.1 christos se = g_xfmt(obuf, u.bits, ndig, sizeof(obuf));
128 1.1 christos printf("g_xfmt(%d) gives %d bytes: \"%s\"\n\n",
129 1.1 christos ndig, (int)(se-obuf), se ? obuf : "<null>");
130 1.1 christos if (!dItry)
131 1.1 christos continue;
132 1.1 christos printf("strtoIx returns %d,",
133 1.1 christos strtoIx(ibuf, &se, v[0].bits, v[1].bits));
134 1.1 christos printf(" consuming %d bytes.\n", (int)(se-ibuf));
135 1.1 christos if (!memcmp(v[0].bits, v[1].bits, 10)) {
136 1.1 christos if (!memcmp(u.bits, v[0].bits, 10))
137 1.1 christos printf("fI[0] == fI[1] == strtox\n");
138 1.1 christos else {
139 1.1 christos printf("fI[0] == fI[1] = #%x %x %x %x %x\n",
140 1.1 christos v[0].bits[_0], v[0].bits[_1],
141 1.1 christos v[0].bits[_2], v[0].bits[_3],
142 1.1 christos v[0].bits[_4]);
143 1.1 christos if (sizeof(long double) == 12)
144 1.1 christos printf("= %.21Lg\n", v[0].d);
145 1.1 christos }
146 1.1 christos }
147 1.1 christos else {
148 1.1 christos printf("fI[0] = #%x %x %x %x %x\n",
149 1.1 christos v[0].bits[_0], v[0].bits[_1],
150 1.1 christos v[0].bits[_2], v[0].bits[_3],
151 1.1 christos v[0].bits[_4]);
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] = #%x %x %x %x %x\n",
155 1.1 christos v[1].bits[_0], v[1].bits[_1],
156 1.1 christos v[1].bits[_2], v[1].bits[_3],
157 1.1 christos v[1].bits[_4]);
158 1.1 christos if (sizeof(long double) == 12)
159 1.1 christos printf("= %.21Lg\n", v[1].d);
160 1.1 christos if (!memcmp(v[0].bits, u.bits, 10))
161 1.1 christos printf("fI[0] == strtox\n");
162 1.1 christos else if (!memcmp(v[1].bits, u.bits, 10))
163 1.1 christos printf("fI[1] == strtox\n");
164 1.1 christos else
165 1.1 christos printf("**** Both differ from strtod ****\n");
166 1.1 christos }
167 1.1 christos printf("\n");
168 1.1 christos }
169 1.1 christos return 0;
170 1.1 christos }
171