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 55 extern int getround ANSI((int,char*)); 56 57 static char ibuf[2048], obuf[1024]; 58 59 #define U (unsigned long) 60 61 int 62 main(Void) 63 { 64 char *s, *se, *se1; 65 int dItry, i, i1, ndig = 0, r = 1; 66 float f1, fI[2]; 67 union { float f; ULong L[1]; } u; 68 69 while( (s = fgets(ibuf, sizeof(ibuf), stdin)) !=0) { 70 while(*s <= ' ') 71 if (!*s++) 72 continue; 73 dItry = 0; 74 switch(*s) { 75 case 'r': 76 r = getround(r, s); 77 continue; 78 case 'n': 79 i = s[1]; 80 if (i <= ' ' || (i >= '0' && i <= '9')) { 81 ndig = atoi(s+1); 82 continue; 83 } 84 break; /* nan? */ 85 case '#': 86 /* sscanf(s+1, "%lx", &u.L[0]); */ 87 u.L[0] = (ULong)strtoul(s+1, &se, 16); 88 printf("\nInput: %s", ibuf); 89 printf(" --> f = #%lx\n", U u.L[0]); 90 goto fmt_test; 91 } 92 dItry = 1; 93 printf("\nInput: %s", ibuf); 94 i = strtorf(ibuf, &se, r, &u.f); 95 if (r == 1) { 96 if (u.f != (i1 = strtopf(ibuf, &se1, &f1), f1) 97 || se != se1 || i != i1) { 98 printf("***strtopf and strtorf disagree!!\n"); 99 if (u.f != f1) 100 printf("\tf1 = %g\n", (double)f1); 101 if (i != i1) 102 printf("\ti = %d but i1 = %d\n", i, i1); 103 if (se != se1) 104 printf("se - se1 = %d\n", (int)(se-se1)); 105 } 106 if (u.f != strtof(ibuf, &se1) || se != se1) 107 printf("***strtof and strtorf disagree!\n"); 108 } 109 printf("strtof consumes %d bytes and returns %.8g = #%lx\n", 110 (int)(se-ibuf), u.f, U u.L[0]); 111 fmt_test: 112 se = g_ffmt(obuf, &u.f, ndig, sizeof(obuf)); 113 printf("g_ffmt(%d) gives %d bytes: \"%s\"\n\n", 114 ndig, (int)(se-obuf), se ? obuf : "<null>"); 115 if (!dItry) 116 continue; 117 printf("strtoIf returns %d,", strtoIf(ibuf, &se, fI, &fI[1])); 118 printf(" consuming %d bytes.\n", (int)(se-ibuf)); 119 if (fI[0] == fI[1]) { 120 if (fI[0] == u.f) 121 printf("fI[0] == fI[1] == strtof\n"); 122 else 123 printf("fI[0] == fI[1] = #%lx = %.8g\n", 124 U *(ULong*)fI, fI[0]); 125 } 126 else { 127 printf("fI[0] = #%lx = %.8g\nfI[1] = #%lx = %.8g\n", 128 U *(ULong*)fI, fI[0], 129 U *(ULong*)&fI[1], fI[1]); 130 if (fI[0] == u.f) 131 printf("fI[0] == strtof\n"); 132 else if (fI[1] == u.f) 133 printf("fI[1] == strtof\n"); 134 else 135 printf("**** Both differ from strtof ****\n"); 136 } 137 printf("\n"); 138 } 139 return 0; 140 } 141