Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/tests/sfio/tpool.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
static char Serial[128], *S = Serial;
23
#if __STD_C
24
ssize_t writef(Sfio_t* f, const Void_t* buf, size_t n, Sfdisc_t* disc)
25
#else
26
ssize_t writef(f, buf, n, disc)
27
Sfio_t* f;
28
Void_t* buf;
29
size_t n;
30
Sfdisc_t* disc;
31
#endif
32
{
33
memcpy((Void_t*)S,buf,n);
34
S += n;
35
return n;
36
}
37
Sfdisc_t Serialdc = {NIL(Sfread_f), writef, NIL(Sfseek_f), NIL(Sfexcept_f) };
38
39
tmain()
40
{
41
int i, n, on;
42
char *s, *os, *s1, *s2, *s3;
43
char poolbuf[1024];
44
Sfio_t *f, *f1, *f2, *f3, *f4;
45
46
if(!(f1 = sfopen((Sfio_t*)0,tstfile("sf", 0),"w+")) ||
47
!(f2 = sfopen((Sfio_t*)0,tstfile("sf", 1),"w")) ||
48
!(f3 = sfopen((Sfio_t*)0,tstfile("sf", 2),"w")))
49
terror("Opening files");
50
51
if(!(f4 = sfopen((Sfio_t*)0,tstfile("sf", 0),"r+")) )
52
terror("Opening file to read");
53
sfungetc(f1,'a');
54
sfungetc(f4,'b');
55
sfpool(f1,f4,0);
56
sfungetc(f1,'a');
57
sfpool(f1,NIL(Sfio_t*),0);
58
59
sfsetbuf(f2,poolbuf,sizeof(poolbuf));
60
sfsetbuf(f3,poolbuf,sizeof(poolbuf));
61
if(!sfpool(f2,f3,0) )
62
terror("Setting pool");
63
64
os = "1234567890\n";
65
on = strlen(os);
66
for(i = 0; i < 100; ++i)
67
if(sfputr(f1,os,-1) < 0)
68
terror("Writing data");
69
sfseek(f1,(Sfoff_t)0,0);
70
for(i = 0; i < 100; ++i)
71
{ if(!(s = sfgetr(f1,'\n',0)) || (n = sfvalue(f1)) != on)
72
terror("Reading data");
73
if(sfwrite(f2,s,n) != n)
74
terror("Writing1");
75
if(sfwrite(f3,s,n) != n)
76
terror("Writing2");
77
}
78
79
/* see if data matches */
80
if(!(f1 = sfopen(f1, tstfile("sf", 0), "r")) ||
81
!(f2 = sfopen(f2, tstfile("sf", 1), "r")) ||
82
!(f3 = sfopen(f3, tstfile("sf", 2), "r")) )
83
terror("sfopen for file comparison");
84
85
if(sfsize(f1) != sfsize(f2) || sfsize(f2) != sfsize(f3))
86
terror("Files don't match sizes");
87
88
n = (int)sfsize(f1);
89
if(!(s1 = sfreserve(f1,n,0)) ||
90
!(s2 = sfreserve(f2,n,0)) ||
91
!(s3 = sfreserve(f3,n,0)) )
92
terror("Fail reserving data");
93
94
if(memcmp(s1,s2,n) != 0 || memcmp(s2,s3,n) != 0)
95
terror("Data do not match");
96
sfclose(f1);
97
sfclose(f2);
98
sfclose(f3);
99
100
f1 = sfstdout; f2 = sfstderr;
101
sfdisc(sfstdout,&Serialdc);
102
sfdisc(sfstderr,&Serialdc);
103
sfset(sfstdout,SF_LINE,0);
104
sfset(sfstderr,SF_LINE,0);
105
if(sfpool(sfstdout,sfstderr,0) != sfstderr )
106
terror("sfpool1");
107
sfputc(sfstdout,'1');
108
sfputc(sfstderr,'2');
109
sfputc(sfstdout,'3');
110
sfputc(sfstderr,'4');
111
sfsync(sfstderr);
112
if(strcmp(Serial,"1234") != 0)
113
terror("Pool not serializing output");
114
sfdisc(sfstdout,NIL(Sfdisc_t*));
115
sfdisc(sfstderr,NIL(Sfdisc_t*));
116
117
sfclose(sfstdout);
118
if(!(f1 = sfopen((Sfio_t*)0,tstfile("sf", 0),"r")))
119
terror("sfopen");
120
if(!sfpool(f1,sfstderr,0) )
121
terror("sfpool2");
122
123
if(!(f1 = sfopen(NIL(Sfio_t*), tstfile("sf", 0), "w+")) ||
124
!(f2 = sfopen(NIL(Sfio_t*), tstfile("sf", 1), "w+")) ||
125
!(f3 = sfopen(NIL(Sfio_t*), tstfile("sf", 2), "w+")) )
126
terror("sfopen3");
127
if(sfpool(f1,f2,SF_SHARE) != f2)
128
terror("sfpool3 f1");
129
if(sfpool(f3,f2,SF_SHARE) != f2 )
130
terror("sfpool3 f3");
131
if(sfputc(f3,'x') < 0)
132
terror("sfputc to f3");
133
if(sfputc(f2,'y') < 0)
134
terror("sfputc to f2");
135
if(sfputc(f1,'z') < 0)
136
terror("sfputc to f1");
137
if(sfseek(f1,(Sfoff_t)0,0) != 0)
138
terror("sfseek failed on f1");
139
if(!(s = sfreserve(f1,3,SF_LOCKR)) || sfvalue(f1) != 3)
140
terror("sfreserve failed on f1");
141
if(memcmp(s,"xyz",3) != 0)
142
terror("Wrong data");
143
144
if((os = sfreserve(f2,SF_UNBOUND,0)) )
145
terror("sfreserve should have failed on f2");
146
147
if(sfpool(NIL(Sfio_t*),f2,0) != f1)
148
terror("Didn't get right pool head for f2");
149
150
if(sfread(f1,s,3) != 3)
151
terror("Wrong read on f1");
152
153
if(!(f = sfpool(f3,NIL(Sfio_t*),0)) )
154
terror("sfpool to delete f3");
155
if(f != f1 && f != f2)
156
terror("sfpool delete did not return a stream from old pool");
157
158
if(sfpool(f1,NIL(Sfio_t*),0) != f2 )
159
terror("sfpool delete did not return a stream from pool");
160
161
if(sfpool(f1,NIL(Sfio_t*),0) != f1 )
162
terror("sfpool delete of a lone stream did not return self");
163
164
if(!(f1 = sfopen(NIL(Sfio_t*), tstfile("sf", 0), "w+")) ||
165
!(f2 = sfopen(NIL(Sfio_t*), tstfile("sf", 1), "w")) )
166
terror("sfopen4");
167
sfputc(f1,'a');
168
sfputc(f1,'b');
169
sfputc(f1,'c');
170
sfset(f1,SF_WRITE,0); /* off write mode */
171
if(sfpool(f1,f2,SF_SHARE) )
172
terror("sfpool should fail pooling read+write streams");
173
if(sfseek(f1,(Sfoff_t)0,0) != (Sfoff_t)0)
174
terror("sfseek failed");
175
if(!(s = sfreserve(f1,3,SF_LOCKR)) || memcmp(s,"abc",3) != 0)
176
terror("Can't get data from f1");
177
178
texit(0);
179
}
180
181