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