xref: /netbsd-src/lib/libc/gdtoa/test/dtest.c (revision 185969dd493322e82156f5a549d980f1f2832be1)
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[1024];
59 
60 #define U (unsigned long)
61 
62  int
main(Void)63 main(Void)
64 {
65 	char *s, *se, *se1;
66 	double f1, fI[2];
67 	int i, i1, ndig = 0, r = 1;
68 	long LL[2];
69 	union { double f; ULong L[2]; } u;
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 			goto fmt_test;
95 			}
96 		printf("\nInput: %s", ibuf);
97 		i = strtord(ibuf, &se, r, &u.f);
98 		if (r == 1) {
99 			if ((u.f != strtod(ibuf, &se1) || se1 != se))
100 				printf("***strtod and strtord disagree!!\n");
101 			i1 = strtopd(ibuf, &se, &f1);
102 			if (i != i1 || u.f != f1 || se != se1)
103 				printf("***strtord and strtopd disagree!!\n");
104 			}
105 		printf("strtod consumes %d bytes and returns %d with f = %.17g = #%lx %lx\n",
106 				(int)(se-ibuf), i, u.f, U u.L[_0], U u.L[_1]);
107  fmt_test:
108 		se = g_dfmt(obuf, &u.f, ndig, sizeof(obuf));
109 		printf("g_dfmt(%d) gives %d bytes: \"%s\"\n\n",
110 			ndig, (int)(se-obuf), se ? obuf : "<null>");
111 		if (*s == '#')
112 			continue;
113 		printf("strtoId returns %d,", strtoId(ibuf, &se, fI, &fI[1]));
114 		printf(" consuming %d bytes.\n", (int)(se-ibuf));
115 		if (fI[0] == fI[1]) {
116 			if (fI[0] == u.f)
117 				printf("fI[0] == fI[1] == strtod\n");
118 			else
119 				printf("fI[0] == fI[1] = #%lx %lx = %.17g\n",
120 					U ((ULong*)fI)[_0], U ((ULong*)fI)[_1],
121 					fI[0]);
122 			}
123 		else {
124 			printf("fI[0] = #%lx %lx = %.17g\n",
125 				U ((ULong*)fI)[_0], U ((ULong*)fI)[_1], fI[0]);
126 			printf("fI[1] = #%lx %lx = %.17g\n",
127 				U ((ULong*)&fI[1])[_0], U ((ULong*)&fI[1])[_1],
128 				fI[1]);
129 			if (fI[0] == u.f)
130 				printf("fI[0] == strtod\n");
131 			else if (fI[1] == u.f)
132 				printf("fI[1] == strtod\n");
133 			else
134 				printf("**** Both differ from strtod ****\n");
135 			}
136 		printf("\n");
137 		}
138 	return 0;
139 	}
140