1 1.1 christos /**************************************************************** 2 1.1 christos 3 1.1 christos The author of this software is David M. Gay. 4 1.1 christos 5 1.1 christos Copyright (C) 1998-2001 by Lucent Technologies 6 1.1 christos All Rights Reserved 7 1.1 christos 8 1.1 christos Permission to use, copy, modify, and distribute this software and 9 1.1 christos its documentation for any purpose and without fee is hereby 10 1.1 christos granted, provided that the above copyright notice appear in all 11 1.1 christos copies and that both that the copyright notice and this 12 1.1 christos permission notice and warranty disclaimer appear in supporting 13 1.1 christos documentation, and that the name of Lucent or any of its entities 14 1.1 christos not be used in advertising or publicity pertaining to 15 1.1 christos distribution of the software without specific, written prior 16 1.1 christos permission. 17 1.1 christos 18 1.1 christos LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 1.1 christos INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 1.1 christos IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 1.1 christos SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 1.1 christos WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 1.1 christos IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 1.1 christos ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 1.1 christos THIS SOFTWARE. 26 1.1 christos 27 1.1 christos ****************************************************************/ 28 1.1 christos 29 1.1 christos /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 1.1 christos * with " at " changed at "@" and " dot " changed to "."). */ 31 1.1 christos 32 1.1 christos /* Test program for g_ffmt, strtof, strtoIf, strtopf, and strtorf. 33 1.1 christos * 34 1.1 christos * Inputs (on stdin): 35 1.1 christos * r rounding_mode 36 1.1 christos * n ndig 37 1.1 christos * number 38 1.1 christos * #hex 39 1.1 christos * 40 1.1 christos * rounding_mode values: 41 1.1 christos * 0 = toward zero 42 1.1 christos * 1 = nearest 43 1.1 christos * 2 = toward +Infinity 44 1.1 christos * 3 = toward -Infinity 45 1.1 christos * 46 1.1 christos * where number is a decimal floating-point number, 47 1.1 christos * hex is a string of <= 8 Hex digits for the internal representation 48 1.1 christos * of the number, and ndig is a parameters to g_ffmt. 49 1.1 christos */ 50 1.1 christos 51 1.1 christos #include "gdtoa.h" 52 1.1 christos #include <stdio.h> 53 1.1 christos #include <stdlib.h> 54 1.1 christos 55 1.1 christos extern int getround ANSI((int,char*)); 56 1.1 christos 57 1.1 christos static char ibuf[2048], obuf[1024]; 58 1.1 christos 59 1.1 christos #define U (unsigned long) 60 1.1 christos 61 1.1 christos int 62 1.1 christos main(Void) 63 1.1 christos { 64 1.1 christos char *s, *se, *se1; 65 1.1 christos int dItry, i, i1, ndig = 0, r = 1; 66 1.1 christos float f1, fI[2]; 67 1.1 christos union { float f; ULong L[1]; } u; 68 1.1 christos 69 1.1 christos while( (s = fgets(ibuf, sizeof(ibuf), stdin)) !=0) { 70 1.1 christos while(*s <= ' ') 71 1.1 christos if (!*s++) 72 1.1 christos continue; 73 1.1 christos dItry = 0; 74 1.1 christos switch(*s) { 75 1.1 christos case 'r': 76 1.1 christos r = getround(r, s); 77 1.1 christos continue; 78 1.1 christos case 'n': 79 1.1 christos i = s[1]; 80 1.1 christos if (i <= ' ' || (i >= '0' && i <= '9')) { 81 1.1 christos ndig = atoi(s+1); 82 1.1 christos continue; 83 1.1 christos } 84 1.1 christos break; /* nan? */ 85 1.1 christos case '#': 86 1.1 christos /* sscanf(s+1, "%lx", &u.L[0]); */ 87 1.1 christos u.L[0] = (ULong)strtoul(s+1, &se, 16); 88 1.1 christos printf("\nInput: %s", ibuf); 89 1.1 christos printf(" --> f = #%lx\n", U u.L[0]); 90 1.1 christos goto fmt_test; 91 1.1 christos } 92 1.1 christos dItry = 1; 93 1.1 christos printf("\nInput: %s", ibuf); 94 1.1 christos i = strtorf(ibuf, &se, r, &u.f); 95 1.1 christos if (r == 1) { 96 1.1 christos if (u.f != (i1 = strtopf(ibuf, &se1, &f1), f1) 97 1.1 christos || se != se1 || i != i1) { 98 1.1 christos printf("***strtopf and strtorf disagree!!\n"); 99 1.1 christos if (u.f != f1) 100 1.1 christos printf("\tf1 = %g\n", (double)f1); 101 1.1 christos if (i != i1) 102 1.1 christos printf("\ti = %d but i1 = %d\n", i, i1); 103 1.1 christos if (se != se1) 104 1.1 christos printf("se - se1 = %d\n", (int)(se-se1)); 105 1.1 christos } 106 1.1 christos if (u.f != strtof(ibuf, &se1) || se != se1) 107 1.1 christos printf("***strtof and strtorf disagree!\n"); 108 1.1 christos } 109 1.1 christos printf("strtof consumes %d bytes and returns %.8g = #%lx\n", 110 1.1 christos (int)(se-ibuf), u.f, U u.L[0]); 111 1.1 christos fmt_test: 112 1.1 christos se = g_ffmt(obuf, &u.f, ndig, sizeof(obuf)); 113 1.1 christos printf("g_ffmt(%d) gives %d bytes: \"%s\"\n\n", 114 1.1 christos ndig, (int)(se-obuf), se ? obuf : "<null>"); 115 1.1 christos if (!dItry) 116 1.1 christos continue; 117 1.1 christos printf("strtoIf returns %d,", strtoIf(ibuf, &se, fI, &fI[1])); 118 1.1 christos printf(" consuming %d bytes.\n", (int)(se-ibuf)); 119 1.1 christos if (fI[0] == fI[1]) { 120 1.1 christos if (fI[0] == u.f) 121 1.1 christos printf("fI[0] == fI[1] == strtof\n"); 122 1.1 christos else 123 1.1 christos printf("fI[0] == fI[1] = #%lx = %.8g\n", 124 1.1 christos U *(ULong*)fI, fI[0]); 125 1.1 christos } 126 1.1 christos else { 127 1.1 christos printf("fI[0] = #%lx = %.8g\nfI[1] = #%lx = %.8g\n", 128 1.1 christos U *(ULong*)fI, fI[0], 129 1.1 christos U *(ULong*)&fI[1], fI[1]); 130 1.1 christos if (fI[0] == u.f) 131 1.1 christos printf("fI[0] == strtof\n"); 132 1.1 christos else if (fI[1] == u.f) 133 1.1 christos printf("fI[1] == strtof\n"); 134 1.1 christos else 135 1.1 christos printf("**** Both differ from strtof ****\n"); 136 1.1 christos } 137 1.1 christos printf("\n"); 138 1.1 christos } 139 1.1 christos return 0; 140 1.1 christos } 141