Debian Codegraph @

runtime/gdtoa/test/dtest.c

raw symbols SCIP
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 David M. Gay (dmg at acm dot org,
30 * with " at " changed at "@" and " dot " changed to "."). */
31
32 /* Test program for g_dfmt, strtoId, strtod, strtopd, and strtord.
33 *
34 * Inputs (on stdin):
35 * r rounding_mode
36 * n ndig
37 * number
38 * #hex0 hex1
39 *
40 * rounding_mode values:
41 * 0 = toward zero
42 * 1 = nearest
43 * 2 = toward +Infinity
44 * 3 = toward -Infinity
45 *
46 * where number is a decimal floating-point number,
47 * hex0 is a string of Hex <= 8 digits for the most significant
48 * word of the number, hex1 is a similar string for the other
49 * (least significant) word, and ndig is a parameters to g_dfmt.
50 */
51
52 #include "gdtoaimp.h"
53 #include <stdio.h>
54 #include <stdlib.h>
55
56 extern int getround ANSI((int,char*));
57
58 static char ibuf[2048], obuf[2048], obuf1[2048];
59
60 #define UL (unsigned long)
61
62 int
63 main(Void)
64 {
65 U fI[2], u;
66 char *s, *se, *se1;
67 double f1;
68 int i, i1, ndig = 0, nik, nike, r = 1;
69 long LL[2];
70
71 while( (s = fgets(ibuf, sizeof(ibuf), stdin)) !=0) {
72 while(*s <= ' ')
73 if (!*s++)
74 continue;
75 switch(*s) {
76 case 'r':
77 r = getround(r, s);
78 continue;
79 case 'n':
80 i = s[1];
81 if (i <= ' ' || (i >= '0' && i <= '9')) {
82 ndig = atoi(s+1);
83 continue;
84 }
85 break; /* nan? */
86 case '#':
87 LL[0] = u.L[_0];
88 LL[1] = u.L[_1];
89 sscanf(s+1, "%lx %lx", &LL[0], &LL[1]);
90 u.L[_0] = LL[0];
91 u.L[_1] = LL[1];
92 printf("\nInput: %s", ibuf);
93 printf("--> f = #%lx %lx\n", (long)u.L[_0], (long)u.L[_1]);
94 i = 0;
95 goto fmt_test;
96 }
97 printf("\nInput: %s", ibuf);
98 i = strtord(ibuf, &se, r, &u.d);
99 if (r == 1) {
100 if ((u.d != strtod(ibuf, &se1) || se1 != se))
101 printf("***strtod and strtord disagree!!\n");
102 i1 = strtopd(ibuf, &se, &f1);
103 if (i != i1 || u.d != f1 || se != se1)
104 printf("***strtord and strtopd disagree!!\n");
105 }
106 printf("strtord consumes %d bytes and returns %d with f = %.17g = #%lx %lx\n",
107 (int)(se-ibuf), i, u.d, UL u.L[_0], UL u.L[_1]);
108 fmt_test:
109 se = g_dfmt(obuf, &u.d, ndig, sizeof(obuf));
110 printf("g_dfmt(%d) gives %d bytes: \"%s\"\n\n",
111 ndig, (int)(se-obuf), se ? obuf : "<null>");
112 se1 = g_dfmt_p(obuf1, &u.d, ndig, sizeof(obuf1), 0);
113 if (se1 - obuf1 != se - obuf || strcmp(obuf, obuf1))
114 printf("Botch: g_dfmt_p gives \"%s\" rather than \"%s\"\n",
115 obuf1, obuf);
116 if (*s == '#')
117 continue;
118 printf("strtoId returns %d,", strtoId(ibuf, &se, &fI[0].d, &fI[1].d));
119 printf(" consuming %d bytes.\n", (int)(se-ibuf));
120 if (fI[0].d == fI[1].d) {
121 if (fI[0].d == u.d)
122 printf("fI[0] == fI[1] == strtod\n");
123 else
124 printf("fI[0] == fI[1] = #%lx %lx = %.17g\n",
125 UL fI[0].L[_0], UL fI[0].L[_1], fI[0].d);
126 }
127 else {
128 printf("fI[0] = #%lx %lx = %.17g\n",
129 UL fI[0].L[_0], UL fI[0].L[_1], fI[0].d);
130 printf("fI[1] = #%lx %lx = %.17g\n",
131 UL fI[1].L[_0], UL fI[1].L[_1], fI[1].d);
132 if (fI[0].d == u.d)
133 printf("fI[0] == strtod\n");
134 else if (fI[1].d == u.d)
135 printf("fI[1] == strtod\n");
136 else
137 printf("**** Both differ from strtod ****\n");
138 }
139 printf("\n");
140 switch(i & STRTOG_Retmask) {
141 case STRTOG_Infinite:
142 for(nik = 0; nik < 6; ++nik) {
143 se1 = g_dfmt_p(obuf1, &u.d, ndig, sizeof(obuf1), nik);
144 printf("g_dfmt_p(...,%d): \"%s\"\n", nik, obuf1);
145 }
146 break;
147 case STRTOG_NaN:
148 case STRTOG_NaNbits:
149 for(i = 0; i < 3; ++i)
150 for(nik = 6*i, nike = nik + 3; nik < nike; ++nik) {
151 se1 = g_dfmt_p(obuf1, &u.d, ndig, sizeof(obuf1), nik);
152 printf("g_dfmt_p(...,%d): \"%s\"\n", nik, obuf1);
153 }
154 }
155 }
156 return 0;
157 }