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