Home | History | Annotate | Line # | Download | only in test
ddtest.c revision 1.1
      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_ddfmt, strtoIdd, strtopdd, and strtordd.
     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  *		#hex0 hex1 hex2 hex3
     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  * hex0 is a string of <= 8 Hex digits for the most significant
     48  1.1  christos  * word of the number, hex1 is a similar string for the next
     49  1.1  christos  * word, etc., and ndig is a parameters to g_ddfmt.
     50  1.1  christos  */
     51  1.1  christos 
     52  1.1  christos #include "gdtoaimp.h"
     53  1.1  christos #include <stdio.h>
     54  1.1  christos #include <stdlib.h>
     55  1.1  christos 
     56  1.1  christos  extern int getround ANSI((int,char*));
     57  1.1  christos 
     58  1.1  christos  static char ibuf[2048], obuf[1024];
     59  1.1  christos 
     60  1.1  christos #define U (unsigned long)
     61  1.1  christos 
     62  1.1  christos  static void
     63  1.1  christos #ifdef KR_headers
     64  1.1  christos dprint(what, d) char *what; double d;
     65  1.1  christos #else
     66  1.1  christos dprint(char *what, double d)
     67  1.1  christos #endif
     68  1.1  christos {
     69  1.1  christos 	char buf[32];
     70  1.1  christos 	union { double d; ULong L[2]; } u;
     71  1.1  christos 
     72  1.1  christos 	u.d = d;
     73  1.1  christos 	g_dfmt(buf,&d,0,sizeof(buf));
     74  1.1  christos 	printf("%s = %s = #%lx %lx\n", what, buf, U u.L[_0], U u.L[_1]);
     75  1.1  christos 	}
     76  1.1  christos 
     77  1.1  christos  int
     78  1.1  christos main(Void)
     79  1.1  christos {
     80  1.1  christos 	char *s, *s1, *se, *se1;
     81  1.1  christos 	int dItry, i, j, r = 1, ndig = 0;
     82  1.1  christos 	double ddI[4];
     83  1.1  christos 	long LL[4];
     84  1.1  christos 	union { double dd[2]; ULong L[4]; } u;
     85  1.1  christos 
     86  1.1  christos 	while( (s = fgets(ibuf, sizeof(ibuf), stdin)) !=0) {
     87  1.1  christos 		while(*s <= ' ')
     88  1.1  christos 			if (!*s++)
     89  1.1  christos 				continue;
     90  1.1  christos 		dItry = 0;
     91  1.1  christos 		switch(*s) {
     92  1.1  christos 		  case 'r':
     93  1.1  christos 			r = getround(r, s);
     94  1.1  christos 			continue;
     95  1.1  christos 		  case 'n':
     96  1.1  christos 			i = s[1];
     97  1.1  christos 			if (i <= ' ' || (i >= '0' && i <= '9')) {
     98  1.1  christos 				ndig = atoi(s+1);
     99  1.1  christos 				continue;
    100  1.1  christos 				}
    101  1.1  christos 			break; /* nan? */
    102  1.1  christos 		  case '#':
    103  1.1  christos 			LL[0] = u.L[_0];
    104  1.1  christos 			LL[1] = u.L[_1];
    105  1.1  christos 			LL[2] = u.L[2+_0];
    106  1.1  christos 			LL[3] = u.L[2+_1];
    107  1.1  christos 			sscanf(s+1, "%lx %lx %lx %lx", &LL[0], &LL[1],
    108  1.1  christos 				&LL[2], &LL[3]);
    109  1.1  christos 			u.L[_0] = LL[0];
    110  1.1  christos 			u.L[_1] = LL[1];
    111  1.1  christos 			u.L[2+_0] = LL[2];
    112  1.1  christos 			u.L[2+_1] = LL[3];
    113  1.1  christos 			printf("\nInput: %s", ibuf);
    114  1.1  christos 			printf(" --> f = #%lx %lx %lx %lx\n",
    115  1.1  christos 				LL[0],LL[1],LL[2],LL[3]);
    116  1.1  christos 			goto fmt_test;
    117  1.1  christos 			}
    118  1.1  christos 		printf("\nInput: %s", ibuf);
    119  1.1  christos 		for(s1 = s; *s1 > ' '; s1++){};
    120  1.1  christos 		while(*s1 <= ' ' && *s1) s1++;
    121  1.1  christos 		if (!*s1) {
    122  1.1  christos 			dItry = 1;
    123  1.1  christos 			i = strtordd(ibuf, &se, r, u.dd);
    124  1.1  christos 			if (r == 1) {
    125  1.1  christos 				j = strtopdd(ibuf, &se1, ddI);
    126  1.1  christos 				if (i != j || u.dd[0] != ddI[0]
    127  1.1  christos 				 || u.dd[1] != ddI[1] || se != se1)
    128  1.1  christos 					printf("***strtopdd and strtordd disagree!!\n:");
    129  1.1  christos 				}
    130  1.1  christos 			printf("strtopdd consumes %d bytes and returns %d\n",
    131  1.1  christos 				(int)(se-ibuf), i);
    132  1.1  christos 			}
    133  1.1  christos 		else {
    134  1.1  christos 			u.dd[0] = strtod(s, &se);
    135  1.1  christos 			u.dd[1] = strtod(se, &se);
    136  1.1  christos 			}
    137  1.1  christos  fmt_test:
    138  1.1  christos 		dprint("dd[0]", u.dd[0]);
    139  1.1  christos 		dprint("dd[1]", u.dd[1]);
    140  1.1  christos 		se = g_ddfmt(obuf, u.dd, ndig, sizeof(obuf));
    141  1.1  christos 		printf("g_ddfmt(%d) gives %d bytes: \"%s\"\n\n",
    142  1.1  christos 			ndig, (int)(se-obuf), se ? obuf : "<null>");
    143  1.1  christos 		if (!dItry)
    144  1.1  christos 			continue;
    145  1.1  christos 		printf("strtoIdd returns %d,", strtoIdd(ibuf, &se, ddI,&ddI[2]));
    146  1.1  christos 		printf(" consuming %d bytes.\n", (int)(se-ibuf));
    147  1.1  christos 		if (ddI[0] == ddI[2] && ddI[1] == ddI[3]) {
    148  1.1  christos 			if (ddI[0] == u.dd[0] && ddI[1] == u.dd[1])
    149  1.1  christos 				printf("ddI[0] == ddI[1] == strtopdd\n");
    150  1.1  christos 			else
    151  1.1  christos 				printf("ddI[0] == ddI[1] = #%lx %lx + %lx %lx\n= %.17g + %17.g\n",
    152  1.1  christos 					U ((ULong*)ddI)[_0],
    153  1.1  christos 					U ((ULong*)ddI)[_1],
    154  1.1  christos 					U ((ULong*)ddI)[2+_0],
    155  1.1  christos 					U ((ULong*)ddI)[2+_1],
    156  1.1  christos 					ddI[0], ddI[1]);
    157  1.1  christos 			}
    158  1.1  christos 		else {
    159  1.1  christos 			printf("ddI[0] = #%lx %lx + %lx %lx\n= %.17g + %.17g\n",
    160  1.1  christos 				U ((ULong*)ddI)[_0], U ((ULong*)ddI)[_1],
    161  1.1  christos 				U ((ULong*)ddI)[2+_0], U ((ULong*)ddI)[2+_1],
    162  1.1  christos 				ddI[0], ddI[1]);
    163  1.1  christos 			printf("ddI[1] = #%lx %lx + %lx %lx\n= %.17g + %.17g\n",
    164  1.1  christos 				U ((ULong*)ddI)[4+_0], U ((ULong*)ddI)[4+_1],
    165  1.1  christos 				U ((ULong*)ddI)[6+_0], U ((ULong*)ddI)[6+_1],
    166  1.1  christos 				ddI[2], ddI[3]);
    167  1.1  christos 			if (ddI[0] == u.dd[0] && ddI[1] == u.dd[1])
    168  1.1  christos 				printf("ddI[0] == strtod\n");
    169  1.1  christos 			else if (ddI[2] == u.dd[0] && ddI[3] == u.dd[1])
    170  1.1  christos 				printf("ddI[1] == strtod\n");
    171  1.1  christos 			else
    172  1.1  christos 				printf("**** Both differ from strtopdd ****\n");
    173  1.1  christos 			}
    174  1.1  christos 		printf("\n");
    175  1.1  christos 		}
    176  1.1  christos 	return 0;
    177  1.1  christos 	}
    178