Home | History | Annotate | Line # | Download | only in test
      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_xfmt, strtoIx, strtopx, and strtorx.
     33  *
     34  * Inputs (on stdin):
     35  *		r rounding_mode
     36  *		n ndig
     37  *		number
     38  *		#hex0 hex1 hex2 hex3 hex4
     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 <= 4 Hex digits for the most significant
     48  * half-word of the number, hex1 is a similar string for the next
     49  * half-word, etc., and ndig is a parameters to g_xfmt.
     50  */
     51 
     52 #include "gdtoa.h"
     53 #include <stdio.h>
     54 #include <stdlib.h>
     55 #include <string.h>
     56 
     57  extern int getround ANSI((int,char*));
     58 
     59  static char ibuf[2048], obuf[2048];
     60 
     61 #undef _0
     62 #undef _1
     63 
     64 /* one or the other of IEEE_BIG_ENDIAN or IEEE_LITTLE_ENDIAN should be #defined */
     65 
     66 #ifdef IEEE_BIG_ENDIAN
     67 #define _0 0
     68 #define _1 1
     69 #define _2 2
     70 #define _3 3
     71 #define _4 4
     72 #endif
     73 #ifdef IEEE_LITTLE_ENDIAN
     74 #define _0 4
     75 #define _1 3
     76 #define _2 2
     77 #define _3 1
     78 #define _4 0
     79 #endif
     80 
     81  int
     82 main(Void)
     83 {
     84 	char *s, *se, *se1;
     85 	int i, dItry, ndig = 0, r = 1;
     86 	union { long double d; UShort bits[5]; } u, v[2];
     87 
     88 	while((s = fgets(ibuf, sizeof(ibuf), stdin))) {
     89 		while(*s <= ' ')
     90 			if (!*s++)
     91 				continue;
     92 		dItry = 0;
     93 		switch(*s) {
     94 		  case 'r':
     95 			r = getround(r, s);
     96 			continue;
     97 		  case 'n':
     98 			i = s[1];
     99 			if (i <= ' ' || (i >= '0' && i <= '9')) {
    100 				ndig = atoi(s+1);
    101 				continue;
    102 				}
    103 			break; /* nan? */
    104 		  case '#':
    105 			sscanf(s+1, "%hx %hx %hx %hx %hx", &u.bits[_0],
    106 				&u.bits[_1], &u.bits[_2], &u.bits[_3],
    107 				&u.bits[_4]);
    108 			printf("\nInput: %s", ibuf);
    109 			printf(" --> f = #%x %x %x %x %x\n", u.bits[_0],
    110 				u.bits[_1], u.bits[_2], u.bits[_3], u.bits[_4]);
    111 			goto fmt_test;
    112 			}
    113 		dItry = 1;
    114 		printf("\nInput: %s", ibuf);
    115 		i = strtorx(ibuf, &se, r, u.bits);
    116 		if (r == 1 && (i != strtopx(ibuf, &se1, v[0].bits) || se1 != se
    117 		 || memcmp(u.bits, v[0].bits, 10)))
    118 			printf("***strtox and strtorx disagree!!\n:");
    119 		printf("\nstrtox consumes %d bytes and returns %d\n",
    120 				(int)(se-ibuf), i);
    121 		printf("with bits = #%x %x %x %x %x\n",
    122 			u.bits[_0], u.bits[_1], u.bits[_2],
    123 			u.bits[_3], u.bits[_4]);
    124 		if (sizeof(long double) == 12)
    125 			printf("printf(\"%%.21Lg\") gives %.21Lg\n", u.d);
    126  fmt_test:
    127 		se = g_xfmt(obuf, u.bits, ndig, sizeof(obuf));
    128 		printf("g_xfmt(%d) gives %d bytes: \"%s\"\n\n",
    129 			ndig, (int)(se-obuf), se ? obuf : "<null>");
    130 		if (!dItry)
    131 			continue;
    132 		printf("strtoIx returns %d,",
    133 			strtoIx(ibuf, &se, v[0].bits, v[1].bits));
    134 		printf(" consuming %d bytes.\n", (int)(se-ibuf));
    135 		if (!memcmp(v[0].bits, v[1].bits, 10)) {
    136 			if (!memcmp(u.bits, v[0].bits, 10))
    137 				printf("fI[0] == fI[1] == strtox\n");
    138 			else {
    139 				printf("fI[0] == fI[1] = #%x %x %x %x %x\n",
    140 					v[0].bits[_0], v[0].bits[_1],
    141 					v[0].bits[_2], v[0].bits[_3],
    142 					v[0].bits[_4]);
    143 				if (sizeof(long double) == 12)
    144 				    printf("= %.21Lg\n", v[0].d);
    145 				}
    146 			}
    147 		else {
    148 			printf("fI[0] = #%x %x %x %x %x\n",
    149 					v[0].bits[_0], v[0].bits[_1],
    150 					v[0].bits[_2], v[0].bits[_3],
    151 					v[0].bits[_4]);
    152 			if (sizeof(long double) == 12)
    153 				printf("= %.21Lg\n", v[0].d);
    154 			printf("fI[1] = #%x %x %x %x %x\n",
    155 					v[1].bits[_0], v[1].bits[_1],
    156 					v[1].bits[_2], v[1].bits[_3],
    157 					v[1].bits[_4]);
    158 			if (sizeof(long double) == 12)
    159 				printf("= %.21Lg\n", v[1].d);
    160 			if (!memcmp(v[0].bits, u.bits, 10))
    161 				printf("fI[0] == strtox\n");
    162 			else if (!memcmp(v[1].bits, u.bits, 10))
    163 				printf("fI[1] == strtox\n");
    164 			else
    165 				printf("**** Both differ from strtod ****\n");
    166 			}
    167 		printf("\n");
    168 		}
    169 	return 0;
    170 	}
    171