Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/tests/sfio/tscanf.c
1810 views
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 <[email protected]> *
18
* *
19
***********************************************************************/
20
#include "sftest.h"
21
22
#if _hdr_values
23
#include <values.h>
24
#endif
25
26
#if _hdr_math
27
#include <math.h>
28
#endif
29
30
#if _hdr_float
31
#include <float.h>
32
#endif
33
34
#if defined(MAXDOUBLE)
35
#define MAXD MAXDOUBLE
36
#endif
37
#if !defined(MAXD) && defined(DBL_MAX)
38
#define MAXD DBL_MAX
39
#endif
40
#if !defined(MAXD)
41
#define MAXD (double)(~((unsigned long)0))
42
#endif
43
44
typedef struct Fmt_s
45
{
46
Sffmt_t fmt;
47
Void_t** args[2];
48
int arg;
49
} Fmt_t;
50
51
static int
52
#if __STD_C
53
extf(Sfio_t* sp, Void_t* vp, Sffmt_t* dp)
54
#else
55
extf(sp, vp, dp)
56
Sfio_t* sp;
57
Void_t* vp;
58
Sffmt_t* dp;
59
#endif
60
{
61
register Fmt_t* fmt = (Fmt_t*)dp;
62
63
dp->flags |= SFFMT_VALUE;
64
*((Void_t**)vp) = fmt->args[fmt->arg++];
65
return 0;
66
}
67
68
tmain()
69
{
70
char str[8], c[4], cl[8];
71
int i, j, k, n;
72
unsigned long a1, a2, a3;
73
float f;
74
double d;
75
char* s;
76
Void_t* vp;
77
Sfio_t* sf;
78
Fmt_t fmt;
79
80
str[0] = 'x'; str[1] = 0;
81
n = sfsscanf("123","%[a-z]%d",str,&i);
82
if(n != 0)
83
terror("Bad %%[ scanning1");
84
n = sfsscanf("123","%#[a-z]%d",str,&i);
85
if(n != 2 || str[0] != 0 || i != 123)
86
terror("Bad %%[ scanning2");
87
88
str[0] = str[1] = str[2] = str[3] =
89
str[4] = str[5] = str[6] = str[7] = 'x';
90
c[0] = c[1] = c[2] = c[3] = 'x';
91
cl[0] = cl[1] = cl[2] = cl[3] =
92
cl[4] = cl[5] = cl[6] = cl[7] = 'x';
93
94
i = -1;
95
if(sfsscanf("123456789","%.*.*d",4,10,&i) != 1)
96
terror("Bad %%d scanning");
97
if(i != 1234)
98
terror("Expected 1234, got %d", i);
99
100
i = -1;
101
if(sfsscanf("0","%i",&i) != 1 || i != 0)
102
terror("Bad %%i scanning1");
103
i = -1;
104
if(sfsscanf("0x","%1i%c",&i,c) != 2 || i != 0 || c[0] != 'x')
105
terror("Bad %%i scanning2");
106
i = -1;
107
if(sfsscanf("0x1","%i",&i) != 1 || i != 1)
108
terror("Bad %%i scanning3");
109
i = -1;
110
if(sfsscanf("07","%i",&i) != 1 || i != 7)
111
terror("Bad %%i scanning4");
112
i = -1;
113
if(sfsscanf("08","%i%i",&i,&j) != 2 || i != 0 || j != 8)
114
terror("Bad %%i scanning5");
115
116
sfsscanf("1234567890","%4I*s%2I*c%4I*[0-9]",4,str,2,c,6,cl);
117
118
if(strcmp(str,"123") != 0)
119
terror("Bad s");
120
if(str[4] != 'x')
121
terror("str overwritten");
122
123
if(strncmp(c,"56",2) != 0)
124
terror("Bad c");
125
if(c[2] != 'x')
126
terror("c overwritten");
127
128
if(strcmp(cl,"7890") != 0)
129
terror("Bad class");
130
if(cl[5] != 'x')
131
terror("cl overwritten");
132
133
if(sfsscanf("123 ab","%*d") != 0)
134
terror("Bad return value");
135
136
if(sfsscanf("123abcA","%[0-9]%[a-z]%[0-9]",str,c,cl) != 2 ||
137
strcmp(str,"123") != 0 || strcmp(c,"abc") != 0)
138
terror("Bad character class scanning");
139
140
if(sfsscanf("123 456 ","%d %d%n",&i,&j,&n) != 2)
141
terror("Bad integer scanning");
142
if(i != 123 || j != 456 || n != 7)
143
terror("Bad return values");
144
145
if(sfsscanf("1 2","%d %d%n",&i,&j,&n) != 2)
146
terror("Bad scanning2");
147
if(i != 1 || j != 2 || n != 3)
148
terror("Bad return values 2");
149
150
if(sfsscanf("1234 1","%2d %d%n",&i,&j,&n) != 2)
151
terror("Bad scanning3");
152
if(i != 12 || j != 34 || n != 4)
153
terror("Bad return values 3");
154
155
if(sfsscanf("011234 1","%3i%1d%1d%n",&i,&j,&k,&n) != 3)
156
terror("Bad scanning4");
157
if(i != 9 || j != 2 || k != 3 || n != 5)
158
terror("Bad return values 4");
159
160
if(sfsscanf("4 6","%f %lf",&f, &d) != 2)
161
terror("Bad scanning5");
162
if(f != 4 || d != 6)
163
terror("Bad return values f=%f d=%f", f, d);
164
165
s = ".1234 .1234";
166
if(sfsscanf(s,"%f %lf",&f, &d) != 2)
167
terror("Bad scanning6");
168
169
if(f <= .1233 || f >= .1235 || d <= .1233 || d >= .1235)
170
terror("Bad return values: f=%.4f d=%.4lf",f,d);
171
172
/* test for scanning max double value */
173
s = sfprints("%.14le",MAXD);
174
if(!s || s[0] < '0' || s[0] > '9')
175
terror("sfprints failed");
176
for(i = 0; s[i]; ++i)
177
if(s[i] == 'e')
178
break;
179
if(s[i-1] > '0' && s[i-1] <= '9')
180
s[i-1] -= 1;
181
sfsscanf(s,"%le",&d);
182
if(d > MAXD || d < MAXD/2)
183
terror("sfscanf of MAXDOUBLE failed");
184
185
if(!(sf = sftmp(8*1024)) )
186
terror("Opening temp file");
187
188
for(k = 2; k <= 64; ++k)
189
{ sfseek(sf,(Sfoff_t)0,0);
190
for(i = 0; i < 1000; ++i)
191
sfprintf(sf,"%#..*d\n",k,i);
192
sfseek(sf,(Sfoff_t)0,0);
193
for(i = 0; i < 1000; ++i)
194
{ if(sfscanf(sf,"%i",&j) != 1)
195
terror("Scanf failed");
196
if(i != j)
197
terror("Wrong scanned value");
198
}
199
}
200
201
/* test %p */
202
s = sfprints("%p", sf);
203
sfsscanf(s, "%p", &vp);
204
if(vp != (Void_t*)sf)
205
terror("Wrong pointer scan");
206
207
if(sfsscanf("2#1001","%i",&i) != 1 || i != 9)
208
terror("Bad %%i scanning");
209
if(sfsscanf("2#1001","%#i%c",&i,c) != 2 || i != 2 || c[0] != '#')
210
terror("Bad %%#i scanning");
211
212
n = -1;
213
if(sfsscanf("12345","%d%n",&k,&n) != 1 || k != 12345 || n != 5)
214
terror("Bad scanning results");
215
n = -1;
216
if(sfsscanf("12345","%d %n",&k,&n) != 1 || k != 12345 || n != 5)
217
terror("Bad scanning results");
218
n = -1;
219
if(sfsscanf("12345 ","%d%n",&k,&n) != 1 || k != 12345 || n != 5)
220
terror("Bad scanning results");
221
n = -1;
222
if(sfsscanf("12345 ","%d %n",&k,&n) != 1 || k != 12345 || n != 6)
223
terror("Bad scanning results");
224
225
n = sfsscanf("zis.zis.gawpwo", "%..36lu.%..36lu.%..36lu", &a1, &a2, &a3);
226
s = sfprints("%d %lu %lu %lu", n, a1, a2, a3);
227
if (!s)
228
terror("sfprints failed");
229
if (strcmp(s, "3 46036 46036 985781544"))
230
terror("Base 36 scan failed");
231
232
if(sfsscanf("NaNS", "%g", &f) != 1)
233
terror("Scanning NaN failed");
234
235
fmt.fmt.version = SFIO_VERSION;
236
fmt.fmt.extf = extf;
237
fmt.fmt.eventf = 0;
238
fmt.fmt.form = "%d %g";
239
fmt.arg = 0;
240
fmt.args[0] = (Void_t*)&n; n = 0;
241
fmt.args[1] = (Void_t*)&f; f = 0;
242
i = sfsscanf("123 3.1415", "%!", &fmt.fmt);
243
if(i != 2 || n != 123 || f <= 3.1414 || f >= 3.1416)
244
terror("%%! failed i=%d n=%d d=%g", i, n, f);
245
246
k = 0;
247
if(sfsscanf("%1", "%%%d", &k) != 1 || k != 1)
248
terror("%%%% failed");
249
k = 0;
250
if(sfsscanf(" %1", "%%%d", &k) != 1 || k != 1)
251
terror("%%%% does not skip leading space");
252
k = 0;
253
if(sfsscanf("%1", "%*[%]%d", &k) != 1 || k != 1)
254
terror("%%*%% failed");
255
k = 0;
256
if(sfsscanf(" %1", "%*[%]%d", &k) == 1 && k == 1)
257
terror("%%*%% skips leading space");
258
259
texit(0);
260
}
261
262