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