Home | History | Annotate | Line # | Download | only in test
      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) 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 strtod.  */
     33  1.1  christos 
     34  1.1  christos /* On stdin, read triples: d x y:
     35  1.1  christos  *	d = decimal string
     36  1.1  christos  *	x = high-order Hex value expected from strtod
     37  1.1  christos  *	y = low-order Hex value
     38  1.1  christos  * Complain about errors.
     39  1.1  christos  */
     40  1.1  christos 
     41  1.1  christos #include "gdtoa.h"	/* for ULong */
     42  1.1  christos #include <stdio.h>
     43  1.1  christos #include <stdlib.h>
     44  1.1  christos #include <string.h>
     45  1.1  christos 
     46  1.1  christos  static int W0, W1;
     47  1.1  christos  typedef union {
     48  1.1  christos 		double d;
     49  1.1  christos 		ULong L[2];
     50  1.1  christos 		} U;
     51  1.1  christos 
     52  1.1  christos #define UL (unsigned long)
     53  1.1  christos 
     54  1.1  christos  static int
     55  1.1  christos process(char *fname, FILE *f)
     56  1.1  christos {
     57  1.1  christos 	U a, b;
     58  1.1  christos 	char buf[2048];
     59  1.1  christos 	char *s, *s1, *se;
     60  1.1  christos 	int line, n;
     61  1.1  christos 
     62  1.1  christos 	line = n = 0;
     63  1.1  christos 
     64  1.1  christos  top:
     65  1.1  christos 	while(fgets(s = buf, sizeof(buf), f)) {
     66  1.1  christos 		line++;
     67  1.1  christos 		while(*s <= ' ')
     68  1.1  christos 			if (!*s++)
     69  1.1  christos 				goto top; /* break 2 */
     70  1.1  christos 		if (*s == '#')
     71  1.1  christos 			continue;
     72  1.1  christos 		while(*s > ' ')
     73  1.1  christos 			s++;
     74  1.1  christos 		/* if (sscanf(s,"\t%lx\t%lx", &a.L[0], &a.L[1]) != 2) */
     75  1.1  christos 		if ((a.L[0] = (ULong)strtoul(s, &s1,16), s1 <= s)
     76  1.1  christos 		 || (a.L[1] = (ULong)strtoul(s1,&se,16), se <= s1)) {
     77  1.1  christos 			printf("Badly formatted line %d of %s\n",
     78  1.1  christos 				line, fname);
     79  1.1  christos 			n++;
     80  1.1  christos 			continue;
     81  1.1  christos 			}
     82  1.1  christos 		b.d = strtod(buf,0);
     83  1.1  christos 		if (b.L[W0] != a.L[0] || b.L[W1] != a.L[1]) {
     84  1.1  christos 			n++;
     85  1.1  christos 			printf("Line %d of %s: got %lx %lx; expected %lx %lx\n",
     86  1.1  christos 				line, fname, UL b.L[W0], UL b.L[W1], UL a.L[0], UL a.L[1]);
     87  1.1  christos 			}
     88  1.1  christos 		}
     89  1.1  christos 	return n;
     90  1.1  christos 	}
     91  1.1  christos 
     92  1.1  christos  int
     93  1.1  christos main(int argc, char **argv)
     94  1.1  christos {
     95  1.1  christos 	FILE *f;
     96  1.1  christos 	char *prog, *s;
     97  1.1  christos 	int n, rc;
     98  1.1  christos 	U u;
     99  1.1  christos 
    100  1.1  christos 	prog = argv[0];
    101  1.1  christos 	if (argc == 2 && !strcmp(argv[1],"-?")) {
    102  1.1  christos 		fprintf(stderr, "Usage: %s [file [file...]]\n"
    103  1.1  christos 			"\tto read data file(s) of tab-separated triples d x y with\n"
    104  1.1  christos 			"\t\td decimal string\n"
    105  1.1  christos 			"\t\tx = high-order Hex value expected from strtod\n"
    106  1.1  christos 			"\t\ty = low-order Hex value\n"
    107  1.1  christos 			"\tComplain about errors by strtod.\n"
    108  1.1  christos 			"\tIf no files, read triples from stdin.\n",
    109  1.1  christos 			prog);
    110  1.1  christos 		return 0;
    111  1.1  christos 		}
    112  1.1  christos 
    113  1.1  christos 	/* determine endian-ness */
    114  1.1  christos 
    115  1.1  christos 	u.d = 1.;
    116  1.1  christos 	W0 = u.L[0] == 0;
    117  1.1  christos 	W1 = 1 - W0;
    118  1.1  christos 
    119  1.1  christos 	/* test */
    120  1.1  christos 
    121  1.1  christos 	n = rc = 0;
    122  1.1  christos 	if (argc <= 1)
    123  1.1  christos 		n = process("<stdin>", stdin);
    124  1.1  christos 	else
    125  1.1  christos 		while((s = *++argv))
    126  1.1  christos 			if ((f = fopen(s,"r"))) {
    127  1.1  christos 				n += process(s, f);
    128  1.1  christos 				fclose(f);
    129  1.1  christos 				}
    130  1.1  christos 			else {
    131  1.1  christos 				rc = 2;
    132  1.1  christos 				fprintf(stderr, "Cannot open %s\n", s);
    133  1.1  christos 				}
    134  1.1  christos 	printf("%d bad conversions\n", n);
    135  1.1  christos 	if (n)
    136  1.1  christos 		rc |= 1;
    137  1.1  christos 	return rc;
    138  1.1  christos 	}
    139