Debian Codegraph @

runtime/gdtoa/test/ftest.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_ffmt, strtof, strtoIf, strtopf, and strtorf.
33 *
34 * Inputs (on stdin):
35 * r rounding_mode
36 * n ndig
37 * number
38 * #hex
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 * hex is a string of <= 8 Hex digits for the internal representation
48 * of the number, and ndig is a parameters to g_ffmt.
49 */
50
51 #include "gdtoa.h"
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.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 typedef union { float f; ULong L; } Uf;
62
63 int
64 main(Void)
65 {
66 Uf fI[2], u;
67 char *s, *se, *se1;
68 float f1;
69 int dItry, i, i1, ndig = 0, nik, nike, r = 1;
70
71 while( (s = fgets(ibuf, sizeof(ibuf), stdin)) !=0) {
72 while(*s <= ' ')
73 if (!*s++)
74 continue;
75 dItry = 0;
76 switch(*s) {
77 case 'r':
78 r = getround(r, s);
79 continue;
80 case 'n':
81 i = s[1];
82 if (i <= ' ' || (i >= '0' && i <= '9')) {
83 ndig = atoi(s+1);
84 continue;
85 }
86 break; /* nan? */
87 case '#':
88 /* sscanf(s+1, "%lx", &u.L); */
89 u.L = (ULong)strtoul(s+1, &se, 16);
90 printf("\nInput: %s", ibuf);
91 printf(" --> f = #%lx\n", UL u.L);
92 i = 0;
93 goto fmt_test;
94 }
95 dItry = 1;
96 printf("\nInput: %s", ibuf);
97 i = strtorf(ibuf, &se, r, &u.f);
98 if (r == 1) {
99 if (u.f != (i1 = strtopf(ibuf, &se1, &f1), f1)
100 || se != se1 || i != i1) {
101 printf("***strtopf and strtorf disagree!!\n");
102 if (u.f != f1)
103 printf("\tf1 = %g\n", (double)f1);
104 if (i != i1)
105 printf("\ti = %d but i1 = %d\n", i, i1);
106 if (se != se1)
107 printf("se - se1 = %d\n", (int)(se-se1));
108 }
109 if (u.f != strtof(ibuf, &se1) || se != se1)
110 printf("***strtof and strtorf disagree!\n");
111 }
112 printf("strtof consumes %d bytes and returns %.8g = #%lx\n",
113 (int)(se-ibuf), u.f, UL u.L);
114 fmt_test:
115 se = g_ffmt(obuf, &u.f, ndig, sizeof(obuf));
116 printf("g_ffmt(%d) gives %d bytes: \"%s\"\n\n",
117 ndig, (int)(se-obuf), se ? obuf : "<null>");
118 se1 = g_ffmt_p(obuf1, &u.f, ndig, sizeof(obuf1), 0);
119 if (se1 - obuf1 != se - obuf || strcmp(obuf, obuf1))
120 printf("Botch: g_ffmt_p gives \"%s\" rather than \"%s\"\n",
121 obuf1, obuf);
122 if (!dItry)
123 continue;
124 printf("strtoIf returns %d,", strtoIf(ibuf, &se, &fI[0].f, &fI[1].f));
125 printf(" consuming %d bytes.\n", (int)(se-ibuf));
126 if (fI[0].f == fI[1].f) {
127 if (fI[0].f == u.f)
128 printf("fI[0] == fI[1] == strtof\n");
129 else
130 printf("fI[0] == fI[1] = #%lx = %.8g\n",
131 UL fI[0].L, fI[0].f);
132 }
133 else {
134 printf("fI[0] = #%lx = %.8g\nfI[1] = #%lx = %.8g\n",
135 UL fI[0].L, fI[0].f,
136 UL fI[1].L, fI[1].f);
137 if (fI[0].f == u.f)
138 printf("fI[0] == strtof\n");
139 else if (fI[1].f == u.f)
140 printf("fI[1] == strtof\n");
141 else
142 printf("**** Both differ from strtof ****\n");
143 }
144 printf("\n");
145 switch(i & STRTOG_Retmask) {
146 case STRTOG_Infinite:
147 for(nik = 0; nik < 6; ++nik) {
148 se1 = g_ffmt_p(obuf1, &u.f, ndig, sizeof(obuf1), nik);
149 printf("g_ffmt_p(...,%d): \"%s\"\n", nik, obuf1);
150 }
151 break;
152 case STRTOG_NaN:
153 case STRTOG_NaNbits:
154 for(i = 0; i < 3; ++i)
155 for(nik = 6*i, nike = nik + 3; nik < nike; ++nik) {
156 se1 = g_ffmt_p(obuf1, &u.f, ndig, sizeof(obuf1), nik);
157 printf("g_ffmt_p(...,%d): \"%s\"\n", nik, obuf1);
158 }
159 }
160 }
161 return 0;
162 }