Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/tests/sfio/treserve.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
tmain()
23
{
24
int i, n, k;
25
Sfoff_t o;
26
char buf[1024], *s;
27
char bigbuf[1024*8];
28
int fd[2];
29
Sfio_t* f;
30
31
if(!(f = sfopen(0, tstfile("sf", 0), "w")) )
32
terror("Opening file to write");
33
if(sfwrite(f,"0123456789",10) != 10 || sfwrite(f,"abcdefgh",8) != 8)
34
terror("Writing data");
35
36
if(!(f = sfopen(f, tstfile("sf", 0), "r")) )
37
terror("Opening file to read");
38
sfsetbuf(f, buf, sizeof(buf));
39
40
if(!(s = (char*)sfreserve(f,10,0)) )
41
terror("sfreserve failed");
42
if(strncmp(s,"0123456789",10) != 0)
43
terror("Did not get correct data");
44
45
if((s = (char*)sfreserve(f,10,0)) )
46
terror("sfreserve should not have succeeded");
47
if(sfvalue(f) != 8)
48
terror("sfreserve should have left the right unread record length");
49
50
if(!(s = (char*)sfreserve(f,4,0)) )
51
terror("sfreserve should return 4 bytes");
52
if(strncmp(s,"abcd",4) != 0)
53
terror("Got wrong data");
54
55
if((s = (char*)sfreserve(f,10,0)) )
56
terror("sfreserve should not have succeeded2");
57
if(sfvalue(f) != 4)
58
terror("sfreserve should have left 4 bytes length");
59
60
if(!(s = (char*)sfreserve(f,0,SF_LASTR)) )
61
terror("sfreserve should have returned last unread record");
62
if(strncmp(s,"efgh",4) != 0)
63
terror("Record has wrong data");
64
65
sfclose(f);
66
67
sfsetbuf(sfstdout,buf,sizeof(buf));
68
sfset(sfstdout,SF_SHARE|SF_PUBLIC,0);
69
if((s = (char*)sfreserve(sfstdout,0,0)) != buf)
70
terror("Wrong buffer");
71
if((n = sfwrite(sfstdout,"foobar",6)) != 6)
72
terror("Write failed");
73
if((char*)sfreserve(sfstdout,0,0) != s+6)
74
terror("Wrong reserved pointer");
75
sfpurge(sfstdout);
76
77
if(sfopen(sfstdout, tstfile("sf", 0),"w") != sfstdout)
78
terror("Opening file");
79
80
sfsetbuf(sfstdout,NIL(char*),0);
81
if(!(s = sfreserve(sfstdout,0,SF_LOCKR)) )
82
terror("Could not lock stdout");
83
if(sfputc(sfstdout,'1') >= 0)
84
terror("stdout wasn't locked");
85
if(sfwrite(sfstdout,s,0) != 0)
86
terror("stdout can't be unlocked");
87
88
sfsetbuf(sfstdout,NIL(char*),sizeof(buf)/2);
89
90
for(i = 0; i < sizeof(buf); ++i)
91
buf[i] = (i%26) + 'a';
92
93
n = 0;
94
for(i = 0; i < 33; ++i)
95
{ if(!(s = sfreserve(sfstdout,sizeof(buf),SF_LOCKR)) )
96
terror("Can't reserve write buffer");
97
98
memcpy(s,buf,sizeof(buf));
99
100
if(sfwrite(sfstdout,s,sizeof(buf)) != sizeof(buf) )
101
terror("Writing to file");
102
else n += sizeof(buf);
103
}
104
105
sfsync(sfstdout);
106
107
if(sfopen(sfstdin, tstfile("sf", 0),"r") != sfstdin)
108
terror("Opening file2");
109
sfsetbuf(sfstdin,NIL(char*),8*sizeof(buf));
110
if(sfsize(sfstdin) != n)
111
terror("Wrong size for file");
112
113
i = 0;
114
for(;;)
115
{ if(!(s = sfreserve(sfstdin,16*sizeof(buf),0)) )
116
break;
117
else i += 16*sizeof(buf);
118
}
119
if(sfvalue(sfstdin) > 0)
120
i += sfvalue(sfstdin);
121
if(i != n)
122
terror("Did not read data");
123
124
if(sfseek(sfstdin,(Sfoff_t)0,0) != 0)
125
terror("sfseek failed0");
126
sfsetbuf(sfstdin,bigbuf,sizeof(bigbuf));
127
i = 0;
128
for(;;)
129
{ if(!(s = sfreserve(sfstdin,16*sizeof(buf),0)) )
130
break;
131
else i += 16*sizeof(buf);
132
}
133
if(sfvalue(sfstdin) > 0)
134
i += sfvalue(sfstdin);
135
if(i != n)
136
terror("Did not read data2");
137
sfsetbuf(sfstdin,NIL(Void_t*),(size_t)SF_UNBOUND);
138
139
if(sfopen(sfstdout, tstfile("sf", 0), "w") != sfstdout)
140
terror("Can't open to write");
141
for(i = 0; i < 32; ++i)
142
{ for(k = 0; k < sizeof(bigbuf); ++k)
143
bigbuf[k] = '0' + (k+i)%10;
144
if(sfwrite(sfstdout,bigbuf,sizeof(bigbuf)) != sizeof(bigbuf))
145
terror("Writing to %s", tstfile("sf", 0));
146
}
147
sfclose(sfstdout);
148
149
if(sfopen(sfstdin, tstfile("sf", 0), "r") != sfstdin)
150
terror("Opening to read");
151
sfsetbuf(sfstdin,NIL(Void_t*),8*1024);
152
if(!(s = sfreserve(sfstdin,16*sizeof(bigbuf),0)) )
153
terror("sfreserve failed");
154
for(i = 0; i < 16; ++i)
155
{ for(k = 0; k < sizeof(bigbuf); ++k)
156
if(*s++ != ('0' + (k+i)%10))
157
terror("Wrong data i=%d k=%d",i,k);
158
}
159
if((o = sfseek(sfstdin,-15*((Sfoff_t)sizeof(bigbuf)),1)) != sizeof(bigbuf))
160
terror("sfseek failed o=%lld", (Sflong_t)o);
161
if(sfread(sfstdin,bigbuf,sizeof(bigbuf)) != sizeof(bigbuf) )
162
terror("sfread failed");
163
s = bigbuf;
164
for(i = 1; i < 2; ++i)
165
{ for(k = 0; k < sizeof(bigbuf); ++k)
166
if(*s++ != ('0' + (k+i)%10))
167
terror("Wrong data2 i=%d k=%d",i,k);
168
}
169
if(!(s = sfreserve(sfstdin,16*sizeof(bigbuf),SF_LOCKR)) )
170
{ sfsetbuf(sfstdin,NIL(Void_t*),16*sizeof(bigbuf));
171
if(!(s = sfreserve(sfstdin,16*sizeof(bigbuf),SF_LOCKR)) )
172
terror("sfreserve failed2");
173
}
174
175
sfread(sfstdin,s,0);
176
#ifdef MAP_TYPE
177
if(sfreserve(sfstdin,0,0) != s)
178
terror("Reserve pointer changed?");
179
#endif
180
for(i = 2; i < 17; ++i)
181
{ for(k = 0; k < sizeof(bigbuf); ++k)
182
if(*s++ != ('0' + (k+i)%10))
183
terror("Wrong data3 i=%d k=%d",i,k);
184
}
185
186
if(sfopen(sfstdout, tstfile("sf", 0),"w") != sfstdout)
187
terror("Opening for write");
188
for(i = 0; i < 100; ++i)
189
bigbuf[i] = 'a';
190
for(i = 0; i < 101; ++i)
191
if(sfwrite(sfstdout,bigbuf,100) != 100)
192
terror("Bad write to file");
193
sfsync(sfstdout);
194
if(sfopen(sfstdin, tstfile("sf", 0),"r") != sfstdin)
195
terror("Opening for read");
196
sfsetbuf(sfstdin,buf,1024);
197
sfset(sfstdin,SF_SHARE,0);
198
for(i = 0; i < 10; ++i)
199
if(!sfreserve(sfstdin,500,0) )
200
terror("Can't reserve from file");
201
for(i = 0; i < 5; ++i)
202
if(sfwrite(sfstdout,bigbuf,100) != 100)
203
terror("Bad write to file2");
204
sfsync(sfstdout);
205
n = 5000;
206
while(sfreserve(sfstdin,500,0))
207
n += 500;
208
if(n+sfvalue(sfstdin) != sfsize(sfstdout))
209
terror("Wrong reserve size from file");
210
211
tcleanup();
212
213
fd[0] = fd[1] = -1;
214
if(pipe(fd) < 0 || fd[0] < 0 || fd[1] < 0)
215
terror("Can't make pipe");
216
if(write(fd[1],"abcdefghijklmnopqrstuvwxyz\n0123456789",37) != 37)
217
terror("Can't write to pipe");
218
close(fd[1]);
219
220
sfclose(sfstdin);
221
if(sfnew(sfstdin,NIL(Void_t*),(size_t)SF_UNBOUND,fd[0],SF_READ) != sfstdin)
222
terror("Can't creat pipe stream");
223
if(!(s = sfgetr(sfstdin,'\n',1) ) ||
224
strcmp(s,"abcdefghijklmnopqrstuvwxyz") != 0)
225
terror("Get wrong string");
226
if((s = sfreserve(sfstdin,16,SF_LOCKR)) )
227
terror("There should not be enough data for this");
228
if(!(s = sfreserve(sfstdin,10,0)) )
229
terror("Fail to reserve remainder of stream");
230
if(strncmp(s,"0123456789",10) != 0)
231
terror("Reserved data was corrupted");
232
233
for(i = 0; i < 18; i += 6)
234
{
235
if(!(f = sftmp(i)) )
236
terror("Can't open tempfile");
237
238
sfset(f,SF_READ,0);
239
for(k = 0; k < 10; ++k)
240
if(sfputc(f,'0'+k) != '0'+k)
241
terror("Write %c to temp file", '0'+k);
242
if(!sfreserve(f,0,-1) )
243
terror("No write buffer?");
244
245
if(i < 10 && (sfset(f,0,0)&SF_STRING) )
246
terror("No file created");
247
248
sfset(f,SF_READ,1);
249
sfseek(f,(Sfoff_t)0,0);
250
if(sfgetc(f) != '0')
251
terror("Getting the 0");
252
if(!(s = sfreserve(f,0,-1)) || sfvalue(f) != 9 ||
253
strncmp(s,"123456789",9) != 0)
254
terror("Read reserved failed");
255
if(sfgetc(f) != '1')
256
terror("Getting the 1");
257
sfclose(f);
258
}
259
260
if(!(f = sftmp(0)) )
261
terror("Can't open file");
262
for(i = 0; i < sizeof(bigbuf); ++i)
263
bigbuf[i] = (i%10) + '0';
264
if(sfwrite(f,bigbuf,1000) != 1000)
265
terror("Writing to file");
266
sfsetbuf(f,buf,100);
267
sfseek(f,(Sfoff_t)0,0);
268
if(!(s = sfreserve(f,SF_UNBOUND,SF_LOCKR)) )
269
terror("sfreserve failed at bottom1");
270
if(sfvalue(f) != 100)
271
terror("Expecting1 100 bytes, get only %d",sfvalue(f));
272
if(strncmp(s,bigbuf,100) != 0)
273
terror("Wrong data at bottom");
274
sfread(f,s,95);
275
if(!(s = sfreserve(f,6,SF_LOCKR)) )
276
terror("sfreserve failed at bottom2");
277
if(sfvalue(f) != 100)
278
terror("Expecting2 100 bytes, get only %d",sfvalue(f));
279
if(strncmp(s,bigbuf+5,100) != 0)
280
terror("Wrong data at bottom2");
281
sfread(f,s,5);
282
for(i = 1; i < 10; ++i)
283
{ if(!(s = sfreserve(f,-96,SF_LOCKR)) )
284
terror("sfreserve failed at bottom loop");
285
if(sfvalue(f) != 100)
286
terror("Expecting3 100 bytes, get only %d",sfvalue(f));
287
if(strncmp(s,bigbuf,100) != 0)
288
terror("Wrong data at bottom loop");
289
sfread(f,s,100);
290
}
291
292
sfseek(f,(Sfoff_t)0,0);
293
for(i = 0; i < 16; ++i)
294
sfwrite(f,bigbuf,sizeof(bigbuf));
295
sfseek(f,(Sfoff_t)0,0);
296
sfset(f,SF_WRITE,0);
297
sfsetbuf(f,NIL(Void_t*),4096);
298
if(!(s = sfreserve(f,SF_UNBOUND,SF_LOCKR)) )
299
terror("sfreserve failed 11");
300
if((n = sfvalue(f)) < 4096)
301
terror("sfvalue is wrong");
302
if(sfread(f,s,n-16) != n-16)
303
terror("sfread failed");
304
if(!(s = sfreserve(f,-7,SF_LOCKR)) )
305
terror("sfreserve failed 12");
306
if(sfvalue(f) < 16 )
307
terror("hmm");
308
309
if(!(f = sfopen(0, "", "sr")) )
310
terror("can't open a read string stream");
311
if(!(s = sfreserve(f, 0, SF_LOCKR)) )
312
terror("can't lock an empty string stream");
313
if(sfread(f,s,0) != 0)
314
terror("can't unlock");
315
if((s = sfreserve(f, 0, 0)) )
316
terror("reserve successful on an empty stream");
317
318
if(!(f = sfopen(0, "", "sw")) )
319
terror("can't open a write string stream");
320
if(!(s = sfreserve(f, 0, SF_LOCKR)) )
321
terror("can't lock an empty string stream");
322
if(sfwrite(f,s,0) != 0)
323
terror("can't unlock");
324
if((s = sfreserve(f, 0, 0)) )
325
terror("reserve successful on an empty stream");
326
327
texit(0);
328
}
329
330