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