author | Claus Gittinger <cg@exept.de> |
Wed, 24 Oct 2012 19:12:01 +0200 | |
changeset 697 | efb5015d621b |
parent 653 | df2012001461 |
child 730 | 43f9555a83e5 |
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. |
|
210 |
||
211 |
sz := 'testFile' asFilename fileSize. |
|
212 |
||
213 |
20 timesRepeat:[ |
|
651 | 214 |
[ |
215 |
self readFileExpecting:sz. |
|
216 |
verbose ifTrue:[ 'done' printCR ]. |
|
217 |
] fork. |
|
0 | 218 |
]. |
219 |
||
220 |
" |
|
119 | 221 |
self new testRead2 |
0 | 222 |
" |
223 |
||
651 | 224 |
"Modified: / 10-01-2012 / 19:28:45 / cg" |
0 | 225 |
! |
226 |
||
75 | 227 |
testRead3 |
0 | 228 |
"read a file - check count read with files size. |
229 |
Do this and interrupt the reading thread heavily" |
|
230 |
||
231 |
|sz p count nLoop| |
|
232 |
||
233 |
nLoop := 1000. |
|
234 |
||
235 |
self createTestFile. |
|
236 |
||
237 |
sz := 'testFile' asFilename fileSize. |
|
238 |
||
239 |
p := [ |
|
75 | 240 |
nLoop timesRepeat:[ |
241 |
self readFileExpecting:sz. |
|
242 |
]. |
|
30 | 243 |
] forkAt:7. |
244 |
||
245 |
count := 0. |
|
246 |
[p isDead] whileFalse:[ |
|
75 | 247 |
Delay waitForMilliseconds:5. |
248 |
p interruptWith:[count := count + 1]. |
|
30 | 249 |
]. |
250 |
('read file <1p> times; interrupted <2p> times' |
|
75 | 251 |
expandMacrosWith:nLoop with:count) printCR |
30 | 252 |
|
253 |
" |
|
119 | 254 |
self new testRead3 |
30 | 255 |
" |
256 |
||
257 |
"Modified: / 12.8.1998 / 13:42:13 / cg" |
|
258 |
! |
|
259 |
||
75 | 260 |
testRewrite1 |
261 |
"basic open for writing (with truncation)." |
|
262 |
||
263 |
|file s sz| |
|
264 |
||
265 |
file := Filename newTemporary. |
|
266 |
s := file writeStream. |
|
267 |
s binary. |
|
268 |
s nextPutAll:(1 to:200). |
|
269 |
s close. |
|
270 |
||
271 |
sz := file fileSize. |
|
272 |
sz ~= 200 ifTrue:[self error:'size mismatch']. |
|
273 |
||
274 |
s := file writeStream. |
|
275 |
s binary. |
|
276 |
s nextPutAll:(101 to:200). |
|
277 |
s close. |
|
278 |
||
279 |
sz := file fileSize. |
|
280 |
sz ~= 100 ifTrue:[self error:'size mismatch']. |
|
281 |
||
282 |
(file binaryContentsOfEntireFile ~= (101 to:200) asByteArray) ifTrue:[ |
|
283 |
self error:'contents mismatch' |
|
284 |
]. |
|
285 |
||
286 |
file delete. |
|
287 |
||
288 |
" |
|
120 | 289 |
self new testRewrite1 |
75 | 290 |
" |
291 |
! |
|
292 |
||
293 |
testRewrite2 |
|
294 |
"basic open for re-writing (without truncation)." |
|
295 |
||
296 |
|file s sz| |
|
297 |
||
298 |
file := Filename newTemporary. |
|
299 |
s := file writeStream. |
|
300 |
s binary. |
|
301 |
s nextPutAll:(1 to:200). |
|
302 |
s close. |
|
303 |
||
304 |
sz := file fileSize. |
|
305 |
sz ~= 200 ifTrue:[self error:'size mismatch']. |
|
306 |
||
307 |
s := file readWriteStream. |
|
308 |
s binary. |
|
309 |
s nextPutAll:(101 to:200). |
|
310 |
s close. |
|
311 |
||
312 |
sz := file fileSize. |
|
313 |
sz ~= 200 ifTrue:[self error:'size mismatch']. |
|
314 |
||
315 |
(file binaryContentsOfEntireFile ~= |
|
316 |
((101 to:200) asByteArray , (101 to:200) asByteArray)) ifTrue:[ |
|
317 |
self error:'contents mismatch' |
|
318 |
]. |
|
319 |
||
320 |
file delete. |
|
321 |
||
322 |
" |
|
120 | 323 |
self new testRewrite2 |
75 | 324 |
" |
76 | 325 |
! |
326 |
||
327 |
testWrite1 |
|
653 | 328 |
"basic writing in various block sizes" |
76 | 329 |
|
120 | 330 |
self doTestWrite:1024. |
331 |
self doTestWrite:2048. |
|
332 |
self doTestWrite:4096. |
|
333 |
self doTestWrite:8192. |
|
334 |
self doTestWrite:8192*2. |
|
335 |
self doTestWrite:8192*4. |
|
336 |
self doTestWrite:8192*8. |
|
337 |
self doTestWrite:8192*16. |
|
338 |
self doTestWrite:8192*32. |
|
339 |
self doTestWrite:8192*64. |
|
340 |
self doTestWrite:8192*128. |
|
76 | 341 |
|
342 |
" |
|
120 | 343 |
self new testWrite1 |
76 | 344 |
" |
653 | 345 |
|
346 |
"Modified (comment): / 18-01-2012 / 12:02:34 / cg" |
|
76 | 347 |
! |
348 |
||
349 |
testWrite2 |
|
350 |
"basic writing" |
|
351 |
||
352 |
10 timesRepeat:[ |
|
353 |
self testWrite1. |
|
354 |
]. |
|
355 |
||
356 |
" |
|
120 | 357 |
self new testWrite2 |
76 | 358 |
" |
359 |
! |
|
360 |
||
361 |
testWrite3 |
|
653 | 362 |
"writing with many interrupts" |
76 | 363 |
|
364 |
|interruptor| |
|
365 |
||
366 |
interruptor := [ |
|
367 |
[true] whileTrue:[ |
|
368 |
Delay waitForMilliseconds:10. |
|
369 |
]. |
|
370 |
] forkAt:9. |
|
371 |
||
372 |
[ |
|
373 |
50 timesRepeat:[ |
|
374 |
self testWrite1. |
|
375 |
]. |
|
376 |
] valueNowOrOnUnwindDo:[ |
|
377 |
interruptor terminate |
|
378 |
]. |
|
379 |
||
380 |
" |
|
120 | 381 |
self new testWrite3 |
76 | 382 |
" |
653 | 383 |
|
384 |
"Modified (comment): / 18-01-2012 / 12:02:21 / cg" |
|
0 | 385 |
! ! |
386 |
||
387 |
!FileStreamTest class methodsFor:'documentation'! |
|
388 |
||
389 |
version |
|
390 |
^ '$Header$' |
|
391 |
! ! |