author | Claus Gittinger <cg@exept.de> |
Tue, 06 Mar 2007 16:42:17 +0100 | |
changeset 332 | 917e3efbdbaa |
parent 181 | a56517005229 |
child 651 | 618563277df4 |
permissions | -rw-r--r-- |
69 | 1 |
"{ Package: 'exept:regression' }" |
2 |
||
0 | 3 |
"{ NameSpace: RegressionTests }" |
4 |
||
118 | 5 |
TestCase subclass:#FileStreamTest |
0 | 6 |
instanceVariableNames:'' |
7 |
classVariableNames:'' |
|
8 |
poolDictionaries:'' |
|
181 | 9 |
category:'tests-Regression' |
0 | 10 |
! |
11 |
||
12 |
||
120 | 13 |
!FileStreamTest methodsFor:'helpers'! |
0 | 14 |
|
332
917e3efbdbaa
changed #createBigDirectoryWithManyFiles
Claus Gittinger <cg@exept.de>
parents:
181
diff
changeset
|
15 |
createBigDirectoryWithManyFiles |
917e3efbdbaa
changed #createBigDirectoryWithManyFiles
Claus Gittinger <cg@exept.de>
parents:
181
diff
changeset
|
16 |
"create a directory with 20000 files" |
917e3efbdbaa
changed #createBigDirectoryWithManyFiles
Claus Gittinger <cg@exept.de>
parents:
181
diff
changeset
|
17 |
|
917e3efbdbaa
changed #createBigDirectoryWithManyFiles
Claus Gittinger <cg@exept.de>
parents:
181
diff
changeset
|
18 |
'c:\tmp' asFilename exists ifFalse:[ |
917e3efbdbaa
changed #createBigDirectoryWithManyFiles
Claus Gittinger <cg@exept.de>
parents:
181
diff
changeset
|
19 |
'c:\tmp' asFilename makeDirectory |
917e3efbdbaa
changed #createBigDirectoryWithManyFiles
Claus Gittinger <cg@exept.de>
parents:
181
diff
changeset
|
20 |
]. |
917e3efbdbaa
changed #createBigDirectoryWithManyFiles
Claus Gittinger <cg@exept.de>
parents:
181
diff
changeset
|
21 |
'c:\tmp\BigDirectory' asFilename exists ifFalse:[ |
917e3efbdbaa
changed #createBigDirectoryWithManyFiles
Claus Gittinger <cg@exept.de>
parents:
181
diff
changeset
|
22 |
'c:\tmp\BigDirectory' asFilename makeDirectory |
917e3efbdbaa
changed #createBigDirectoryWithManyFiles
Claus Gittinger <cg@exept.de>
parents:
181
diff
changeset
|
23 |
]. |
917e3efbdbaa
changed #createBigDirectoryWithManyFiles
Claus Gittinger <cg@exept.de>
parents:
181
diff
changeset
|
24 |
|
917e3efbdbaa
changed #createBigDirectoryWithManyFiles
Claus Gittinger <cg@exept.de>
parents:
181
diff
changeset
|
25 |
1 to:20000 do:[:i| |
917e3efbdbaa
changed #createBigDirectoryWithManyFiles
Claus Gittinger <cg@exept.de>
parents:
181
diff
changeset
|
26 |
|f s buffer| |
917e3efbdbaa
changed #createBigDirectoryWithManyFiles
Claus Gittinger <cg@exept.de>
parents:
181
diff
changeset
|
27 |
f :='c:\tmp\BigDirectory' asFilename |
917e3efbdbaa
changed #createBigDirectoryWithManyFiles
Claus Gittinger <cg@exept.de>
parents:
181
diff
changeset
|
28 |
construct:(i printString leftPaddedTo:5 with:$0). |
917e3efbdbaa
changed #createBigDirectoryWithManyFiles
Claus Gittinger <cg@exept.de>
parents:
181
diff
changeset
|
29 |
s := f writeStream binary. |
917e3efbdbaa
changed #createBigDirectoryWithManyFiles
Claus Gittinger <cg@exept.de>
parents:
181
diff
changeset
|
30 |
buffer := ByteArray new:1024. |
917e3efbdbaa
changed #createBigDirectoryWithManyFiles
Claus Gittinger <cg@exept.de>
parents:
181
diff
changeset
|
31 |
32 timesRepeat:[ s nextPutAll:buffer ]. |
917e3efbdbaa
changed #createBigDirectoryWithManyFiles
Claus Gittinger <cg@exept.de>
parents:
181
diff
changeset
|
32 |
s close. |
917e3efbdbaa
changed #createBigDirectoryWithManyFiles
Claus Gittinger <cg@exept.de>
parents:
181
diff
changeset
|
33 |
]. |
917e3efbdbaa
changed #createBigDirectoryWithManyFiles
Claus Gittinger <cg@exept.de>
parents:
181
diff
changeset
|
34 |
|
917e3efbdbaa
changed #createBigDirectoryWithManyFiles
Claus Gittinger <cg@exept.de>
parents:
181
diff
changeset
|
35 |
" |
917e3efbdbaa
changed #createBigDirectoryWithManyFiles
Claus Gittinger <cg@exept.de>
parents:
181
diff
changeset
|
36 |
self new createBigDirectoryWithManyFiles |
917e3efbdbaa
changed #createBigDirectoryWithManyFiles
Claus Gittinger <cg@exept.de>
parents:
181
diff
changeset
|
37 |
" |
917e3efbdbaa
changed #createBigDirectoryWithManyFiles
Claus Gittinger <cg@exept.de>
parents:
181
diff
changeset
|
38 |
|
917e3efbdbaa
changed #createBigDirectoryWithManyFiles
Claus Gittinger <cg@exept.de>
parents:
181
diff
changeset
|
39 |
"Created: / 12.8.1998 / 13:25:25 / cg" |
917e3efbdbaa
changed #createBigDirectoryWithManyFiles
Claus Gittinger <cg@exept.de>
parents:
181
diff
changeset
|
40 |
! |
917e3efbdbaa
changed #createBigDirectoryWithManyFiles
Claus Gittinger <cg@exept.de>
parents:
181
diff
changeset
|
41 |
|
0 | 42 |
createTestFile |
43 |
"create a test file (100k size)" |
|
44 |
||
45 |
|f bytes| |
|
46 |
||
47 |
f := 'testFile' asFilename writeStream binary. |
|
48 |
bytes := ByteArray new:1024. |
|
49 |
1 to:bytes size do:[:i | |
|
119 | 50 |
bytes at:i put:(i \\ 256). |
0 | 51 |
]. |
52 |
||
53 |
100 timesRepeat:[ |
|
119 | 54 |
f nextPutAll:bytes. |
0 | 55 |
]. |
56 |
||
57 |
f close. |
|
58 |
||
59 |
" |
|
119 | 60 |
self new createTestFile |
0 | 61 |
" |
62 |
||
63 |
"Created: / 12.8.1998 / 13:25:25 / cg" |
|
64 |
! |
|
65 |
||
120 | 66 |
doTestWrite:size |
67 |
"basic writing" |
|
68 |
||
69 |
|file s sz buffer byte| |
|
70 |
||
71 |
buffer := ByteArray new:size. |
|
72 |
1 to:buffer size do:[:idx | |
|
73 |
buffer at:idx put:(idx \\ 256) |
|
74 |
]. |
|
75 |
||
76 |
file := Filename newTemporary. |
|
77 |
s := file writeStream. |
|
78 |
||
79 |
s binary. |
|
80 |
s nextPutAll:buffer. |
|
81 |
s close. |
|
82 |
||
83 |
sz := file fileSize. |
|
84 |
sz ~= size ifTrue:[self error:'size mismatch']. |
|
85 |
||
86 |
s := file readStream. |
|
87 |
s binary. |
|
88 |
1 to:size do:[:idx | |
|
89 |
byte := s next. |
|
90 |
byte ~~ (idx \\ 256) ifTrue:[self error:'read data mismatch']. |
|
91 |
]. |
|
92 |
s close. |
|
93 |
||
94 |
(file binaryContentsOfEntireFile ~= buffer) ifTrue:[ |
|
95 |
self error:'read data mismatch2' |
|
96 |
]. |
|
97 |
file delete. |
|
98 |
||
99 |
" |
|
100 |
self testWrite:1024 |
|
101 |
" |
|
102 |
! |
|
103 |
||
0 | 104 |
readFileExpecting:expect |
105 |
"read test file, expect n bytes" |
|
106 |
||
107 |
|f buffer n nRead| |
|
108 |
||
109 |
f := 'testFile' asFilename readStream binary. |
|
110 |
buffer := ByteArray new:128. |
|
111 |
||
112 |
n := 0. |
|
113 |
[f atEnd] whileFalse:[ |
|
1 | 114 |
nRead := f nextBytes:128 into:buffer. |
115 |
n := n + nRead. |
|
0 | 116 |
]. |
117 |
f close. |
|
118 |
expect ~~ n ifTrue:[ |
|
1 | 119 |
self halt:('got <1p>; expected:<2p>' expandMacrosWith:n with:expect) |
0 | 120 |
]. |
121 |
||
122 |
"Created: / 12.8.1998 / 13:29:41 / cg" |
|
123 |
"Modified: / 12.8.1998 / 13:36:40 / cg" |
|
120 | 124 |
! ! |
125 |
||
126 |
!FileStreamTest methodsFor:'tests'! |
|
0 | 127 |
|
75 | 128 |
testAppend1 |
129 |
"basic open for appending." |
|
130 |
||
119 | 131 |
|file s sz actualContents expectedContents| |
75 | 132 |
|
133 |
file := Filename newTemporary. |
|
134 |
s := file writeStream. |
|
135 |
s binary. |
|
136 |
s nextPutAll:(1 to:100). |
|
137 |
s close. |
|
138 |
||
139 |
sz := file fileSize. |
|
119 | 140 |
self should:[ sz = 100 ]. "/ size mismatch |
75 | 141 |
|
142 |
s := file appendingWriteStream. |
|
143 |
s binary. |
|
144 |
s nextPutAll:(101 to:200). |
|
145 |
s close. |
|
146 |
||
147 |
sz := file fileSize. |
|
119 | 148 |
self should:[ sz = 200 ]. "/ size mismatch |
75 | 149 |
|
119 | 150 |
actualContents := file binaryContentsOfEntireFile. |
151 |
expectedContents := (1 to:200) asByteArray. |
|
152 |
self should:[ (actualContents = expectedContents) ]. "/ contents mismatch |
|
75 | 153 |
|
154 |
file delete. |
|
155 |
||
156 |
" |
|
119 | 157 |
self new testAppend1 |
75 | 158 |
" |
159 |
! |
|
160 |
||
161 |
testOpenRead1 |
|
162 |
"basic open for reading." |
|
163 |
||
164 |
|sz| |
|
165 |
||
166 |
self createTestFile. |
|
167 |
||
168 |
sz := 'testFile' asFilename fileSize. |
|
169 |
||
170 |
self readFileExpecting:sz. |
|
171 |
||
172 |
" |
|
119 | 173 |
self new testOpenRead1 |
75 | 174 |
" |
175 |
! |
|
176 |
||
177 |
testRead1 |
|
0 | 178 |
"read a file - check count read with files size." |
179 |
||
180 |
|sz| |
|
181 |
||
182 |
self createTestFile. |
|
183 |
||
184 |
sz := 'testFile' asFilename fileSize. |
|
185 |
||
186 |
self readFileExpecting:sz. |
|
187 |
||
188 |
" |
|
119 | 189 |
self new testRead1 |
0 | 190 |
" |
191 |
||
192 |
"Modified: / 12.8.1998 / 13:29:55 / cg" |
|
193 |
! |
|
194 |
||
75 | 195 |
testRead2 |
0 | 196 |
"read a file - check count read with files size. |
197 |
Do this in 20 threads" |
|
198 |
||
199 |
|sz| |
|
200 |
||
201 |
self createTestFile. |
|
202 |
||
203 |
sz := 'testFile' asFilename fileSize. |
|
204 |
||
205 |
20 timesRepeat:[ |
|
75 | 206 |
[self readFileExpecting:sz. 'done' printCR.] fork. |
0 | 207 |
]. |
208 |
||
209 |
" |
|
119 | 210 |
self new testRead2 |
0 | 211 |
" |
212 |
||
213 |
"Modified: / 12.8.1998 / 13:30:55 / cg" |
|
214 |
! |
|
215 |
||
75 | 216 |
testRead3 |
0 | 217 |
"read a file - check count read with files size. |
218 |
Do this and interrupt the reading thread heavily" |
|
219 |
||
220 |
|sz p count nLoop| |
|
221 |
||
222 |
nLoop := 1000. |
|
223 |
||
224 |
self createTestFile. |
|
225 |
||
226 |
sz := 'testFile' asFilename fileSize. |
|
227 |
||
228 |
p := [ |
|
75 | 229 |
nLoop timesRepeat:[ |
230 |
self readFileExpecting:sz. |
|
231 |
]. |
|
30 | 232 |
] forkAt:7. |
233 |
||
234 |
count := 0. |
|
235 |
[p isDead] whileFalse:[ |
|
75 | 236 |
Delay waitForMilliseconds:5. |
237 |
p interruptWith:[count := count + 1]. |
|
30 | 238 |
]. |
239 |
('read file <1p> times; interrupted <2p> times' |
|
75 | 240 |
expandMacrosWith:nLoop with:count) printCR |
30 | 241 |
|
242 |
" |
|
119 | 243 |
self new testRead3 |
30 | 244 |
" |
245 |
||
246 |
"Modified: / 12.8.1998 / 13:42:13 / cg" |
|
247 |
! |
|
248 |
||
75 | 249 |
testRewrite1 |
250 |
"basic open for writing (with truncation)." |
|
251 |
||
252 |
|file s sz| |
|
253 |
||
254 |
file := Filename newTemporary. |
|
255 |
s := file writeStream. |
|
256 |
s binary. |
|
257 |
s nextPutAll:(1 to:200). |
|
258 |
s close. |
|
259 |
||
260 |
sz := file fileSize. |
|
261 |
sz ~= 200 ifTrue:[self error:'size mismatch']. |
|
262 |
||
263 |
s := file writeStream. |
|
264 |
s binary. |
|
265 |
s nextPutAll:(101 to:200). |
|
266 |
s close. |
|
267 |
||
268 |
sz := file fileSize. |
|
269 |
sz ~= 100 ifTrue:[self error:'size mismatch']. |
|
270 |
||
271 |
(file binaryContentsOfEntireFile ~= (101 to:200) asByteArray) ifTrue:[ |
|
272 |
self error:'contents mismatch' |
|
273 |
]. |
|
274 |
||
275 |
file delete. |
|
276 |
||
277 |
" |
|
120 | 278 |
self new testRewrite1 |
75 | 279 |
" |
280 |
! |
|
281 |
||
282 |
testRewrite2 |
|
283 |
"basic open for re-writing (without truncation)." |
|
284 |
||
285 |
|file s sz| |
|
286 |
||
287 |
file := Filename newTemporary. |
|
288 |
s := file writeStream. |
|
289 |
s binary. |
|
290 |
s nextPutAll:(1 to:200). |
|
291 |
s close. |
|
292 |
||
293 |
sz := file fileSize. |
|
294 |
sz ~= 200 ifTrue:[self error:'size mismatch']. |
|
295 |
||
296 |
s := file readWriteStream. |
|
297 |
s binary. |
|
298 |
s nextPutAll:(101 to:200). |
|
299 |
s close. |
|
300 |
||
301 |
sz := file fileSize. |
|
302 |
sz ~= 200 ifTrue:[self error:'size mismatch']. |
|
303 |
||
304 |
(file binaryContentsOfEntireFile ~= |
|
305 |
((101 to:200) asByteArray , (101 to:200) asByteArray)) ifTrue:[ |
|
306 |
self error:'contents mismatch' |
|
307 |
]. |
|
308 |
||
309 |
file delete. |
|
310 |
||
311 |
" |
|
120 | 312 |
self new testRewrite2 |
75 | 313 |
" |
76 | 314 |
! |
315 |
||
316 |
testWrite1 |
|
317 |
"basic writing" |
|
318 |
||
120 | 319 |
self doTestWrite:1024. |
320 |
self doTestWrite:2048. |
|
321 |
self doTestWrite:4096. |
|
322 |
self doTestWrite:8192. |
|
323 |
self doTestWrite:8192*2. |
|
324 |
self doTestWrite:8192*4. |
|
325 |
self doTestWrite:8192*8. |
|
326 |
self doTestWrite:8192*16. |
|
327 |
self doTestWrite:8192*32. |
|
328 |
self doTestWrite:8192*64. |
|
329 |
self doTestWrite:8192*128. |
|
76 | 330 |
|
331 |
" |
|
120 | 332 |
self new testWrite1 |
76 | 333 |
" |
334 |
! |
|
335 |
||
336 |
testWrite2 |
|
337 |
"basic writing" |
|
338 |
||
339 |
10 timesRepeat:[ |
|
340 |
self testWrite1. |
|
341 |
]. |
|
342 |
||
343 |
" |
|
120 | 344 |
self new testWrite2 |
76 | 345 |
" |
346 |
! |
|
347 |
||
348 |
testWrite3 |
|
349 |
"basic writing" |
|
350 |
||
351 |
|interruptor| |
|
352 |
||
353 |
interruptor := [ |
|
354 |
[true] whileTrue:[ |
|
355 |
Delay waitForMilliseconds:10. |
|
356 |
]. |
|
357 |
] forkAt:9. |
|
358 |
||
359 |
[ |
|
360 |
50 timesRepeat:[ |
|
361 |
self testWrite1. |
|
362 |
]. |
|
363 |
] valueNowOrOnUnwindDo:[ |
|
364 |
interruptor terminate |
|
365 |
]. |
|
366 |
||
367 |
" |
|
120 | 368 |
self new testWrite3 |
76 | 369 |
" |
0 | 370 |
! ! |
371 |
||
372 |
!FileStreamTest class methodsFor:'documentation'! |
|
373 |
||
374 |
version |
|
375 |
^ '$Header$' |
|
376 |
! ! |