]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/gdtoa/test/dtest.c
merge fix for boot-time hang on centos' xen
[FreeBSD/FreeBSD.git] / contrib / gdtoa / test / dtest.c
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
30         David M. Gay
31         Bell Laboratories, Room 2C-463
32         600 Mountain Avenue
33         Murray Hill, NJ 07974-0636
34         U.S.A.
35         dmg@bell-labs.com
36  */
37
38 /* Test program for g_dfmt, strtoId, strtod, strtopd, and strtord.
39  *
40  * Inputs (on stdin):
41  *              r rounding_mode
42  *              n ndig
43  *              number
44  *              #hex0 hex1
45  *
46  *      rounding_mode values:
47  *              0 = toward zero
48  *              1 = nearest
49  *              2 = toward +Infinity
50  *              3 = toward -Infinity
51  *
52  * where number is a decimal floating-point number,
53  * hex0 is a string of Hex <= 8 digits for the most significant
54  * word of the number, hex1 is a similar string for the other
55  * (least significant) word, and ndig is a parameters to g_dfmt.
56  */
57
58 #include "gdtoaimp.h"
59 #include <stdio.h>
60 #include <stdlib.h>
61
62  extern int getround ANSI((int,char*));
63
64  static char ibuf[2048], obuf[1024];
65
66 #define U (unsigned long)
67
68  int
69 main(Void)
70 {
71         ULong *L;
72         char *s, *se, *se1;
73         double f, f1, fI[2];
74         int i, i1, ndig = 0, r = 1;
75         long LL[2];
76
77         L = (ULong*)&f;
78         while( (s = fgets(ibuf, sizeof(ibuf), stdin)) !=0) {
79                 while(*s <= ' ')
80                         if (!*s++)
81                                 continue;
82                 switch(*s) {
83                   case 'r':
84                         r = getround(r, s);
85                         continue;
86                   case 'n':
87                         i = s[1];
88                         if (i <= ' ' || i >= '0' && i <= '9') {
89                                 ndig = atoi(s+1);
90                                 continue;
91                                 }
92                         break; /* nan? */
93                   case '#':
94                         LL[0] = L[_0];
95                         LL[1] = L[_1];
96                         sscanf(s+1, "%lx %lx", &LL[0], &LL[1]);
97                         L[_0] = LL[0];
98                         L[_1] = LL[1];
99                         printf("\nInput: %s", ibuf);
100                         printf("--> f = #%lx %lx\n", (long)L[_0], (long)L[_1]);
101                         goto fmt_test;
102                         }
103                 printf("\nInput: %s", ibuf);
104                 i = strtord(ibuf, &se, r, &f);
105                 if (r == 1) {
106                         if ((f != strtod(ibuf, &se1) || se1 != se))
107                                 printf("***strtod and strtord disagree!!\n");
108                         i1 = strtopd(ibuf, &se, &f1);
109                         if (i != i1 || f != f1 || se != se1)
110                                 printf("***strtord and strtopd disagree!!\n");
111                         }
112                 printf("strtod consumes %d bytes and returns %d with f = %.17g = #%lx %lx\n",
113                                 (int)(se-ibuf), i, f, U L[_0], U L[_1]);
114  fmt_test:
115                 se = g_dfmt(obuf, &f, ndig, sizeof(obuf));
116                 printf("g_dfmt(%d) gives %d bytes: \"%s\"\n\n",
117                         ndig, (int)(se-obuf), se ? obuf : "<null>");
118                 if (*s == '#')
119                         continue;
120                 printf("strtoId returns %d,", strtoId(ibuf, &se, fI, &fI[1]));
121                 printf(" consuming %d bytes.\n", (int)(se-ibuf));
122                 if (fI[0] == fI[1]) {
123                         if (fI[0] == f)
124                                 printf("fI[0] == fI[1] == strtod\n");
125                         else
126                                 printf("fI[0] == fI[1] = #%lx %lx = %.17g\n",
127                                         U ((ULong*)fI)[_0], U ((ULong*)fI)[_1],
128                                         fI[0]);
129                         }
130                 else {
131                         printf("fI[0] = #%lx %lx = %.17g\n",
132                                 U ((ULong*)fI)[_0], U ((ULong*)fI)[_1], fI[0]);
133                         printf("fI[1] = #%lx %lx = %.17g\n",
134                                 U ((ULong*)&fI[1])[_0], U ((ULong*)&fI[1])[_1],
135                                 fI[1]);
136                         if (fI[0] == f)
137                                 printf("fI[0] == strtod\n");
138                         else if (fI[1] == f)
139                                 printf("fI[1] == strtod\n");
140                         else
141                                 printf("**** Both differ from strtod ****\n");
142                         }
143                 printf("\n");
144                 }
145         return 0;
146         }