1 /***********************************************************************
2 * *
3 * This software is part of the ast package *
4 * Copyright (c) 1999-2011 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Eclipse Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
8 * *
9 * A copy of the License is available at *
10 * http://www.eclipse.org/org/documents/epl-v10.html *
11 * (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12 * *
13 * Information and Software Systems Research *
14 * AT&T Research *
15 * Florham Park NJ *
16 * *
17 * Glenn Fowler <gsf@research.att.com> *
18 * *
19 ***********************************************************************/
20 #pragma prototyped
21
22 /*
23 * AT&T Research
24 *
25 * test harness for
26 *
27 * strtod strtold
28 * strntod strntold
29 */
30
31 #if _PACKAGE_ast
32 #include <ast.h>
33 #else
34 #ifndef _ISOC99_SOURCE
35 #define _ISOC99_SOURCE 1
36 #endif
37 #include <stdint.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #endif
41
42 #include <stdio.h>
43 #include <errno.h>
44 #include <locale.h>
45 #include <float.h>
46
47 #ifndef ERANGE
48 #define ERANGE EINVAL
49 #endif
50
51 #ifndef errno
52 extern int errno;
53 #endif
54
55 #ifndef LDBL_DIG
56 #define LDBL_DIG DBL_DIG
57 #endif
58 #ifndef LDBL_MAX_EXP
59 #define LDBL_MAX_EXP DBL_MAX_EXP
60 #endif
61
62 #if !_PACKAGE_ast
63
64 #define _ast_fltmax_t long double
65
66 static char buf[1024];
67
68 #undef strntod
69 #define strntod _tst_strntod
70
71 static double
strntod(const char * s,size_t n,char ** e)72 strntod(const char* s, size_t n, char** e)
73 {
74 double r;
75 char* p;
76
77 if (n >= sizeof(buf))
78 n = sizeof(buf) - 1;
79 memcpy(buf, s, n);
80 buf[n] = 0;
81 r = strtod(buf, &p);
82 if (e)
83 *e = (char*)s + (p - buf);
84 return r;
85 }
86
87 #undef strntold
88 #define strntold _tst_strntold
89
90 static _ast_fltmax_t
strntold(const char * s,size_t n,char ** e)91 strntold(const char* s, size_t n, char** e)
92 {
93 _ast_fltmax_t r;
94 char* p;
95
96 if (n >= sizeof(buf))
97 n = sizeof(buf) - 1;
98 memcpy(buf, s, n);
99 buf[n] = 0;
100 r = strtold(buf, &p);
101 if (e)
102 *e = (char*)s + (p - buf);
103 return r;
104 }
105
106 #endif
107
108 int
main(int argc,char ** argv)109 main(int argc, char** argv)
110 {
111 char* s;
112 char* p;
113 double d;
114 _ast_fltmax_t ld;
115 int sep = 0;
116 int n;
117
118 if (argc <= 1)
119 {
120 printf("%u.%u.%u-%u.%u.%u-%u.%u.%u\n", FLT_DIG, -(FLT_MIN_10_EXP), FLT_MAX_10_EXP, DBL_DIG, -(DBL_MIN_10_EXP), DBL_MAX_10_EXP, LDBL_DIG, -(LDBL_MIN_10_EXP), LDBL_MAX_10_EXP);
121 return 0;
122 }
123 while (s = *++argv)
124 {
125 if (!strncmp(s, "LC_ALL=", 7))
126 {
127 if (!setlocale(LC_ALL, s + 7))
128 {
129 printf("%s failed\n", s);
130 return 0;
131 }
132 continue;
133 }
134 if (sep)
135 printf("\n");
136 else
137 sep = 1;
138
139 errno = 0;
140 d = strtod(s, &p);
141 printf("strtod \"%s\" \"%s\" %.*e %s\n", s, p, DBL_DIG - 1, d, errno == 0 ? "OK" : errno == ERANGE ? "ERANGE" : errno == EINVAL ? "EINVAL" : "ERROR");
142
143 errno = 0;
144 ld = strtold(s, &p);
145 printf("strtold \"%s\" \"%s\" %.*Le %s\n", s, p, LDBL_DIG - 1, ld, errno == 0 ? "OK" : errno == ERANGE ? "ERANGE" : errno == EINVAL ? "EINVAL" : "ERROR");
146
147 n = strlen(s);
148
149 errno = 0;
150 d = strntod(s, n, &p);
151 printf("strntod %2d \"%-.*s\" \"%s\" %.*e %s\n", n, n, s, p, DBL_DIG - 1, d, errno == 0 ? "OK" : errno == ERANGE ? "ERANGE" : errno == EINVAL ? "EINVAL" : "ERROR");
152
153 errno = 0;
154 d = strntod(s, n - 1, &p);
155 printf("strntod %2d \"%-.*s\" \"%s\" %.*e %s\n", n - 1, n - 1, s, p, DBL_DIG - 1, d, errno == 0 ? "OK" : errno == ERANGE ? "ERANGE" : errno == EINVAL ? "EINVAL" : "ERROR");
156
157 errno = 0;
158 ld = strntold(s, n, &p);
159 printf("strntold %2d \"%-.*s\" \"%s\" %.*Le %s\n", n, n, s, p, LDBL_DIG - 1, ld, errno == 0 ? "OK" : errno == ERANGE ? "ERANGE" : errno == EINVAL ? "EINVAL" : "ERROR");
160
161 errno = 0;
162 ld = strntold(s, n - 1, &p);
163 printf("strntold %2d \"%-.*s\" \"%s\" %.*Le %s\n", n - 1, n - 1, s, p, LDBL_DIG - 1, ld, errno == 0 ? "OK" : errno == ERANGE ? "ERANGE" : errno == EINVAL ? "EINVAL" : "ERROR");
164 }
165 return 0;
166 }
167