115
|
1 |
"{ Package: 'exept:regression' }"
|
|
2 |
|
157
|
3 |
"{ NameSpace: RegressionTests }"
|
|
4 |
|
115
|
5 |
TestCase subclass:#CollectionTests
|
293
|
6 |
instanceVariableNames:'empty nonEmpty'
|
115
|
7 |
classVariableNames:''
|
|
8 |
poolDictionaries:''
|
181
|
9 |
category:'tests-Regression'
|
115
|
10 |
!
|
|
11 |
|
212
|
12 |
Object subclass:#SortTestData
|
|
13 |
instanceVariableNames:'value order'
|
|
14 |
classVariableNames:''
|
|
15 |
poolDictionaries:''
|
|
16 |
privateIn:CollectionTests
|
|
17 |
!
|
|
18 |
|
115
|
19 |
!CollectionTests class methodsFor:'documentation'!
|
|
20 |
|
|
21 |
documentation
|
|
22 |
"
|
|
23 |
documentation to be added.
|
|
24 |
|
|
25 |
[author:]
|
|
26 |
Claus Gittinger (cg@alan)
|
|
27 |
|
|
28 |
[see also:]
|
|
29 |
|
|
30 |
[instance variables:]
|
|
31 |
|
|
32 |
[class variables:]
|
|
33 |
"
|
|
34 |
!
|
|
35 |
|
|
36 |
history
|
|
37 |
"Created: / 8.11.2001 / 08:17:09 / cg"
|
|
38 |
! !
|
|
39 |
|
301
|
40 |
!CollectionTests methodsFor:'helpers'!
|
|
41 |
|
|
42 |
collectedDoArgsOf:aCollection
|
|
43 |
|collectedDoArgs|
|
|
44 |
|
|
45 |
collectedDoArgs := OrderedCollection new.
|
|
46 |
aCollection do:[:each | collectedDoArgs add:each].
|
|
47 |
^ collectedDoArgs
|
|
48 |
! !
|
|
49 |
|
293
|
50 |
!CollectionTests methodsFor:'initialize-release'!
|
|
51 |
|
|
52 |
setUp
|
295
|
53 |
empty := Set new.
|
|
54 |
nonEmpty := OrderedCollection with: #x.
|
293
|
55 |
! !
|
|
56 |
|
264
|
57 |
!CollectionTests methodsFor:'tests-dictionary'!
|
115
|
58 |
|
|
59 |
testDictionary01
|
301
|
60 |
|d v1 v2 v3 ret|
|
115
|
61 |
|
|
62 |
d := Dictionary new.
|
|
63 |
d at:1 put:(v1 := 'one').
|
|
64 |
d at:2 put:(v2 := 'two').
|
|
65 |
d at:3 put:(v3 := 'three').
|
|
66 |
|
|
67 |
self assert: ( d size == 3 ).
|
301
|
68 |
|
|
69 |
self assert:(ret := self collectedDoArgsOf:d) = #('one' 'two' 'three').
|
|
70 |
self assert:(ret := d collect:[:each | each]) = #('one' 'two' 'three') asBag.
|
|
71 |
self assert:(ret := d select:[:each | true]) = (Dictionary withAssociations:(Array with:(1->'one') with:(2->'two') with:(3->'three'))).
|
|
72 |
|
115
|
73 |
self assert: ( (d at:1) == v1 ).
|
|
74 |
self assert: ( (d at:2) == v2 ).
|
|
75 |
self assert: ( (d at:3) == v3 ).
|
|
76 |
|
|
77 |
d at:3 put:(v3 := 'drei').
|
|
78 |
|
|
79 |
self assert: ( d size == 3 ).
|
|
80 |
|
|
81 |
self assert: ( (d at:1) == v1 ).
|
|
82 |
self assert: ( (d at:2) == v2 ).
|
|
83 |
self assert: ( (d at:3) == v3 ).
|
|
84 |
|
|
85 |
self assert: ( (d keyAtValue:v1) = 1 ).
|
|
86 |
|
|
87 |
"
|
|
88 |
self basicNew testDictionary01
|
|
89 |
"
|
264
|
90 |
! !
|
|
91 |
|
|
92 |
!CollectionTests methodsFor:'tests-general'!
|
|
93 |
|
|
94 |
doTestGeneralCollectionOperationsIn:aClass
|
|
95 |
|coll|
|
|
96 |
|
|
97 |
coll := aClass new.
|
|
98 |
|
|
99 |
self assert: ( coll size == 0 ).
|
|
100 |
self assert: ( coll isEmpty ).
|
|
101 |
self assert: ( coll notEmpty not ).
|
|
102 |
self assert: ( coll isEmptyOrNil ).
|
|
103 |
self assert: ( coll notEmptyOrNil not ).
|
|
104 |
|
|
105 |
self should: [ coll first ] raise:Error.
|
|
106 |
self should: [ coll last ] raise:Error.
|
|
107 |
self should: [ (coll at:1) ] raise:Error.
|
|
108 |
|
|
109 |
self assert: ( (coll includes:0) not ).
|
|
110 |
|
|
111 |
"
|
|
112 |
self basicNew testGeneralOperations
|
|
113 |
"
|
156
|
114 |
!
|
|
115 |
|
264
|
116 |
testGeneralOperations
|
|
117 |
|classes|
|
|
118 |
|
|
119 |
classes := OrderedCollection new.
|
|
120 |
classes
|
|
121 |
add:RunArray;
|
|
122 |
add:Bag;
|
|
123 |
add:Set;
|
|
124 |
add:IdentitySet;
|
|
125 |
add:Dictionary;
|
|
126 |
add:IdentityDictionary;
|
|
127 |
add:OrderedCollection;
|
|
128 |
add:SortedCollection;
|
|
129 |
add:Queue;
|
|
130 |
add:Stack;
|
|
131 |
add:SortedCollection;
|
|
132 |
add:OrderedDictionary;
|
|
133 |
add:OrderedSet.
|
|
134 |
|
|
135 |
classes do:[:eachClass |
|
|
136 |
self doTestGeneralCollectionOperationsIn:eachClass
|
|
137 |
].
|
|
138 |
|
|
139 |
"
|
|
140 |
self basicNew testGeneralOperations
|
|
141 |
"
|
|
142 |
! !
|
|
143 |
|
|
144 |
!CollectionTests methodsFor:'tests-interval'!
|
|
145 |
|
162
|
146 |
testInterval01
|
|
147 |
|i|
|
|
148 |
|
|
149 |
i := 1 to:10.
|
|
150 |
|
|
151 |
self assert: ( i size == 10 ).
|
|
152 |
|
|
153 |
self assert: ( i includes:1 ).
|
|
154 |
self assert: ( i includes:10 ).
|
|
155 |
|
|
156 |
self assert: ( i min == 1 ).
|
|
157 |
self assert: ( i max == 10 ).
|
|
158 |
|
|
159 |
"cannot add/remove"
|
|
160 |
self should:[i add:99] raise:Error.
|
|
161 |
self should:[i remove:10] raise:Error.
|
|
162 |
|
|
163 |
"
|
|
164 |
self basicNew testInterval01
|
|
165 |
"
|
264
|
166 |
! !
|
|
167 |
|
301
|
168 |
!CollectionTests methodsFor:'tests-orderedCollection'!
|
|
169 |
|
|
170 |
testOrderedCollection01_Sort1
|
|
171 |
|col col2|
|
|
172 |
|
|
173 |
col := #( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ) copy.
|
|
174 |
col sort.
|
|
175 |
self assert:( col = (1 to:20) ).
|
|
176 |
|
|
177 |
col := #( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ) copy reverse.
|
|
178 |
col sort.
|
|
179 |
self assert:( col = (1 to:20) ).
|
|
180 |
|
|
181 |
col := #( 10 2 13 4 15 6 17 8 19 11 1 12 3 14 5 16 7 18 9 20 ) copy.
|
|
182 |
col sort.
|
|
183 |
self assert:( col = (1 to:20) ).
|
|
184 |
|
|
185 |
col := ( 1 to: 10000 ) asOrderedCollection.
|
|
186 |
col sort.
|
|
187 |
self assert:( col = (1 to:10000) ).
|
|
188 |
|
|
189 |
col := ( 1 to: 10000 ) asOrderedCollection.
|
|
190 |
col2 := ( 1 to: 10000 ) asOrderedCollection.
|
|
191 |
col sortWith:col2.
|
|
192 |
self assert:( col = (1 to:10000) ).
|
|
193 |
self assert:( col2 = (1 to:10000) ).
|
|
194 |
|
|
195 |
col := ( 10000 to: 1 by:-1) asOrderedCollection.
|
|
196 |
col2 := ( 10000 to: 1 by:-1) asOrderedCollection.
|
|
197 |
col sortWith:col2.
|
|
198 |
self assert:( col = (1 to:10000) ).
|
|
199 |
self assert:( col2 = (1 to:10000) ).
|
|
200 |
|
|
201 |
"
|
|
202 |
self new testOrderedCollection01_Sort1
|
|
203 |
"
|
|
204 |
!
|
|
205 |
|
|
206 |
testOrderedCollection02_SortBigCollection
|
|
207 |
| size col sortBlock|
|
|
208 |
|
|
209 |
size := 12500.
|
|
210 |
col := OrderedCollection new.
|
|
211 |
1 to:size do:[:el|
|
|
212 |
col add:nil.
|
|
213 |
].
|
|
214 |
sortBlock := [:entry1 :entry2 |
|
|
215 |
((entry1 isNil) or:[entry2 isNil]) ifTrue:[
|
|
216 |
true
|
|
217 |
] ifFalse:[
|
|
218 |
false
|
|
219 |
]
|
|
220 |
].
|
|
221 |
self shouldnt:[col sort:sortBlock] raise:RecursionInterruptSignal.
|
|
222 |
|
|
223 |
"
|
|
224 |
self new testOrderedCollection02_SortBigCollection
|
|
225 |
"
|
|
226 |
!
|
|
227 |
|
|
228 |
testOrderedCollection03_SortBigCollection2
|
|
229 |
| size col|
|
|
230 |
|
|
231 |
size := 12500.
|
|
232 |
col := OrderedCollection new.
|
|
233 |
1 to:size do:[:el|
|
|
234 |
col add:'abc'.
|
|
235 |
].
|
|
236 |
self shouldnt:[col sort] raise:RecursionInterruptSignal.
|
|
237 |
|
|
238 |
"
|
|
239 |
self new testOrderedCollection03_SortBigCollection2
|
|
240 |
"
|
|
241 |
!
|
|
242 |
|
|
243 |
testOrderedCollection04_SortNilsInCollection
|
|
244 |
| size col sortBlock|
|
|
245 |
|
|
246 |
size := 125.
|
|
247 |
col := OrderedCollection new.
|
|
248 |
1 to:size do:[:el|
|
|
249 |
col add:nil.
|
|
250 |
].
|
|
251 |
sortBlock := [:entry1 :entry2 |
|
|
252 |
((entry1 isNil) or:[entry2 isNil]) ifTrue:[
|
|
253 |
true
|
|
254 |
] ifFalse:[
|
|
255 |
false
|
|
256 |
]
|
|
257 |
].
|
|
258 |
"/ col sort:sortBlock.
|
|
259 |
self shouldnt:[col sort:sortBlock] raise:RecursionInterruptSignal.
|
|
260 |
|
|
261 |
"
|
|
262 |
self new testOrderedCollection04_SortNilsInCollection
|
|
263 |
"
|
|
264 |
! !
|
|
265 |
|
|
266 |
!CollectionTests methodsFor:'tests-orderedDictionary'!
|
|
267 |
|
|
268 |
testOrderedDictionary01
|
|
269 |
|d ret|
|
|
270 |
|
|
271 |
d := OrderedDictionary new.
|
|
272 |
|
|
273 |
self assert: ( d size == 0 ).
|
|
274 |
self assert: ( d isEmpty ).
|
|
275 |
self assert: ( d notEmpty not ).
|
|
276 |
|
|
277 |
d at:'one' put:1.
|
|
278 |
d at:'two' put:2.
|
|
279 |
d at:'three' put:3.
|
|
280 |
|
|
281 |
self assert: ( d size == 3 ).
|
|
282 |
self assert: ( d isEmpty not).
|
|
283 |
self assert: ( d notEmpty ).
|
|
284 |
|
|
285 |
self assert:(ret := self collectedDoArgsOf:d) = #(1 2 3).
|
|
286 |
|
|
287 |
self assert: ( (d atIndex:1) == 1 ).
|
|
288 |
self assert: ( (d atIndex:2) == 2 ).
|
|
289 |
self assert: ( (d atIndex:3) == 3 ).
|
|
290 |
|
|
291 |
self assert: ( (d first) = ('one'->1) ).
|
|
292 |
self assert: ( (d last) = ('three'->3) ).
|
|
293 |
|
|
294 |
self assert:(ret := d collect:[:each | each]) = #(1 2 3).
|
|
295 |
self assert:(ret := d select:[:each | true]) = (OrderedDictionary withAssociations:(Array with:('one'->1) with:('two'->2) with:('three'->3))).
|
|
296 |
|
|
297 |
self assert: ( d includes:1 ).
|
|
298 |
self assert: ( d includes:2 ).
|
|
299 |
self assert: ( d includes:3 ).
|
|
300 |
|
|
301 |
self assert: ( d includesKey:'one' ).
|
|
302 |
self assert: ( d includesKey:'two' ).
|
|
303 |
self assert: ( d includesKey:'three' ).
|
|
304 |
|
|
305 |
self assert: ((ret := d removeFirst) = 1).
|
|
306 |
self assert: ( d size == 2 ).
|
|
307 |
self assert: ((ret := d removeLast) = 3).
|
|
308 |
self assert: ( d size == 1 ).
|
|
309 |
self assert: ((ret := d removeLast) = 2).
|
|
310 |
self assert: ( d size == 0 ).
|
|
311 |
self assert: ( d isEmpty ).
|
|
312 |
self assert: ( d notEmpty not ).
|
|
313 |
|
|
314 |
"
|
|
315 |
self basicNew testOrderedDictionary01
|
|
316 |
"
|
|
317 |
! !
|
|
318 |
|
|
319 |
!CollectionTests methodsFor:'tests-orderedSet'!
|
162
|
320 |
|
156
|
321 |
testOrderedSet01
|
|
322 |
|s v1 v2 v3|
|
|
323 |
|
|
324 |
s := OrderedSet new.
|
|
325 |
s add:(v1 := 'one').
|
|
326 |
s add:(v2 := 'two').
|
|
327 |
s add:(v3 := 'three').
|
|
328 |
|
|
329 |
self assert: ( s size == 3 ).
|
|
330 |
self assert: ( (s at:1) == v1 ).
|
|
331 |
self assert: ( (s at:2) == v2 ).
|
|
332 |
self assert: ( (s at:3) == v3 ).
|
|
333 |
|
|
334 |
self assert: ( s includes:v1 ).
|
|
335 |
self assert: ( s includes:v2 ).
|
|
336 |
self assert: ( s includes:v3 ).
|
|
337 |
|
|
338 |
self assert: ( s includes:v1 copy).
|
|
339 |
self assert: ( s includes:v2 copy).
|
|
340 |
self assert: ( s includes:v3 copy).
|
|
341 |
|
|
342 |
"already in - adding again should not change things"
|
|
343 |
s add:v1.
|
|
344 |
self assert: ( s size == 3 ).
|
|
345 |
|
|
346 |
self assert: ( (s at:1) == v1 ).
|
|
347 |
self assert: ( (s at:2) == v2 ).
|
|
348 |
self assert: ( (s at:3) == v3 ).
|
|
349 |
|
|
350 |
"set i.e. not identitySet - adding copy should not change things"
|
|
351 |
s add:v1 copy.
|
|
352 |
self assert: ( s size == 3 ).
|
|
353 |
|
|
354 |
self assert: ( (s at:1) == v1 ).
|
|
355 |
self assert: ( (s at:2) == v2 ).
|
|
356 |
self assert: ( (s at:3) == v3 ).
|
|
357 |
|
|
358 |
s remove:v1.
|
|
359 |
|
|
360 |
self assert: ( s size == 2 ).
|
|
361 |
self assert: ( (s at:1) == v2 ).
|
|
362 |
self assert: ( (s at:2) == v3 ).
|
|
363 |
|
|
364 |
self assert: ( s includes:v1 ) not.
|
|
365 |
self assert: ( s includes:v2 ).
|
|
366 |
self assert: ( s includes:v3 ).
|
|
367 |
|
|
368 |
self assert: ( s includes:v1 copy) not.
|
|
369 |
self assert: ( s includes:v2 copy).
|
|
370 |
self assert: ( s includes:v3 copy).
|
|
371 |
|
|
372 |
s add:v1.
|
|
373 |
|
|
374 |
self assert: ( s size == 3 ).
|
|
375 |
self assert: ( (s at:1) == v2 ).
|
|
376 |
self assert: ( (s at:2) == v3 ).
|
|
377 |
self assert: ( (s at:3) == v1 ).
|
|
378 |
|
|
379 |
self assert: ( s includes:v1 ).
|
|
380 |
self assert: ( s includes:v2 ).
|
|
381 |
self assert: ( s includes:v3 ).
|
|
382 |
|
|
383 |
self assert: ( s includes:v1 copy).
|
|
384 |
self assert: ( s includes:v2 copy).
|
|
385 |
self assert: ( s includes:v3 copy).
|
|
386 |
|
|
387 |
s removeLast.
|
|
388 |
|
|
389 |
self assert: ( s size == 2 ).
|
|
390 |
self assert: ( (s at:1) == v2 ).
|
|
391 |
self assert: ( (s at:2) == v3 ).
|
|
392 |
|
|
393 |
self assert: ( s includes:v1 ) not.
|
|
394 |
self assert: ( s includes:v2 ).
|
|
395 |
self assert: ( s includes:v3 ).
|
|
396 |
|
|
397 |
self assert: ( s includes:v1 copy) not.
|
|
398 |
self assert: ( s includes:v2 copy).
|
|
399 |
self assert: ( s includes:v3 copy).
|
|
400 |
|
|
401 |
"
|
|
402 |
self basicNew testOrderedSet01
|
|
403 |
"
|
301
|
404 |
!
|
|
405 |
|
|
406 |
testOrderedSet02
|
|
407 |
|col|
|
|
408 |
|
|
409 |
col := OrderedSet new.
|
|
410 |
|
|
411 |
col add:'a'.
|
|
412 |
col add:'b'.
|
|
413 |
col add:'c'.
|
|
414 |
self assert:( col size == 3 ).
|
|
415 |
self assert:( Array streamContents:[:s |
|
|
416 |
col do:[:each | s nextPut:each]
|
|
417 |
]
|
|
418 |
) = #('a' 'b' 'c' ).
|
|
419 |
|
|
420 |
"
|
|
421 |
self new testOrderedSet02
|
|
422 |
"
|
|
423 |
!
|
|
424 |
|
|
425 |
testOrderedSet03
|
|
426 |
|col|
|
|
427 |
|
|
428 |
col := OrderedSet new.
|
|
429 |
|
|
430 |
col add:'a'.
|
|
431 |
col add:'b'.
|
|
432 |
col add:'c'.
|
|
433 |
col add:'a'.
|
|
434 |
col add:'b'.
|
|
435 |
self assert:( col size == 3 ).
|
|
436 |
self assert:( Array streamContents:[:s |
|
|
437 |
col do:[:each | s nextPut:each]
|
|
438 |
]
|
|
439 |
) = #('a' 'b' 'c' ).
|
|
440 |
|
|
441 |
"
|
|
442 |
self new testOrderedSet03
|
|
443 |
"
|
|
444 |
!
|
|
445 |
|
|
446 |
testOrderedSet04
|
|
447 |
|col|
|
|
448 |
|
|
449 |
col := OrderedSet new.
|
|
450 |
|
|
451 |
col add:'a'.
|
|
452 |
col addFirst:'b'.
|
|
453 |
col addFirst:'c'.
|
|
454 |
self assert:( col size == 3 ).
|
|
455 |
self assert:( Array streamContents:[:s |
|
|
456 |
col do:[:each | s nextPut:each]
|
|
457 |
]
|
|
458 |
) = #('c' 'b' 'a' ).
|
|
459 |
|
|
460 |
"
|
|
461 |
self new testOrderedSet04
|
|
462 |
"
|
|
463 |
!
|
|
464 |
|
|
465 |
testOrderedSet05
|
|
466 |
|col|
|
|
467 |
|
|
468 |
col := OrderedSet new.
|
|
469 |
|
|
470 |
col add:'a'.
|
|
471 |
col addFirst:'b'.
|
|
472 |
col addFirst:'c'.
|
|
473 |
col add:'a'.
|
|
474 |
col add:'b'.
|
|
475 |
col add:'c'.
|
|
476 |
self assert:( col size == 3 ).
|
|
477 |
self assert:( Array streamContents:[:s |
|
|
478 |
col do:[:each | s nextPut:each]
|
|
479 |
]
|
|
480 |
) = #('c' 'b' 'a' ).
|
|
481 |
|
|
482 |
"
|
|
483 |
self new testOrderedSet05
|
|
484 |
"
|
264
|
485 |
! !
|
|
486 |
|
297
|
487 |
!CollectionTests methodsFor:'tests-replace'!
|
|
488 |
|
|
489 |
testEmptyReplace
|
|
490 |
| array1 array2 |
|
|
491 |
|
|
492 |
array1 := Array with: 1 with: 2 with: 3 with: 4.
|
|
493 |
array2 := Array with: 5 with: 6 with: 7.
|
|
494 |
|
|
495 |
array1 replaceFrom:1 to:0 with:array2 startingAt:1.
|
|
496 |
|
|
497 |
self should: [ array1 = #(1 2 3 4) ].
|
|
498 |
|
|
499 |
"
|
|
500 |
self new testEmptyReplace
|
|
501 |
"
|
538
|
502 |
!
|
|
503 |
|
|
504 |
testTransliterating
|
|
505 |
self assert:( 'abcdefghijkl1234567890' copyTransliterating:'b-g' to:'B-G' ) = 'aBCDEFGhijkl1234567890'.
|
|
506 |
self assert:( 'abcdefghij-kl1234567890' copyTransliterating:'b\-g' to:'B+G' ) = 'aBcdefGhij+kl1234567890'.
|
|
507 |
self assert:( 'abcdefghijkl1234567890' copyTransliterating:'69' to:'96' ) = 'abcdefghijkl1234597860' .
|
|
508 |
self assert:( 'abcdefghijkl1234567890' copyTransliterating:'a' to:'b' ) = 'bbcdefghijkl1234567890' .
|
|
509 |
self assert:( 'abcdefghijkl1234567890' copyTransliterating:'aeiou' to:'AEIOU' ) = 'AbcdEfghIjkl1234567890'.
|
|
510 |
self assert:( 'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'QERTYUIOPX' ) = 'abcdefghijklERTYUIOPXQ' .
|
|
511 |
|
|
512 |
self assert:( 'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'A' complement:false squashDuplicates:false )
|
|
513 |
= 'abcdefghijklAAAAAAAAAA'.
|
|
514 |
|
|
515 |
self assert:( 'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'A' complement:false squashDuplicates:true )
|
|
516 |
= 'abcdefghijklA'.
|
|
517 |
|
|
518 |
self assert:( 'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'*' complement:false squashDuplicates:false )
|
|
519 |
= 'abcdefghijkl**********'.
|
|
520 |
|
|
521 |
self assert:( 'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'*' complement:true squashDuplicates:false )
|
|
522 |
= '************1234567890'.
|
|
523 |
|
|
524 |
self assert:( 'abcdefghijkl1234567890' copyTransliterating:'a-zA-Z' to:' ' complement:true squashDuplicates:false )
|
|
525 |
= 'abcdefghijkl '.
|
|
526 |
|
|
527 |
self assert:( 'abcdefghijkl1234567890' copyTransliterating:'a-zA-Z' to:' ' complement:false squashDuplicates:false )
|
|
528 |
= ' 1234567890'.
|
|
529 |
|
|
530 |
self assert:( 'abcdefghijkl1234567890abcdefghijkl' copyTransliterating:'a-zA-Z' to:'' complement:false squashDuplicates:false )
|
|
531 |
= '1234567890'.
|
|
532 |
|
|
533 |
self assert:( 'abcdefghijkl1234567890abcdefghijkl' copyTransliterating:'a-zA-Z' to:'' complement:true squashDuplicates:false )
|
|
534 |
= 'abcdefghijklabcdefghijkl'.
|
297
|
535 |
! !
|
|
536 |
|
264
|
537 |
!CollectionTests methodsFor:'tests-sorting'!
|
212
|
538 |
|
|
539 |
testSort01
|
|
540 |
| col errs last |
|
|
541 |
|
|
542 |
col := SortedCollection new addAll: (100 to: 1 by: -1); yourself.
|
|
543 |
last := nil.
|
|
544 |
errs := col inject: 0 into:
|
|
545 |
[ :totIn :n | |totOut|
|
|
546 |
|
|
547 |
totOut := totIn.
|
|
548 |
last notNil ifTrue: [ last > n ifTrue: [ totOut := totOut + 1 ] ].
|
|
549 |
last := n.
|
|
550 |
totOut ].
|
|
551 |
self should: [ errs = 0 ]
|
|
552 |
|
|
553 |
"
|
|
554 |
self basicNew testSort01
|
|
555 |
"
|
|
556 |
!
|
|
557 |
|
|
558 |
testSortStability01
|
|
559 |
"Verify that the sort is stable"
|
|
560 |
|
|
561 |
| testData col lastTestData errs |
|
|
562 |
|
|
563 |
testData := OrderedCollection new.
|
|
564 |
testData addAll: ((100 to: 1 by: -1) collect: [ :n | SortTestData new value: n ]).
|
|
565 |
testData addAll: ((100 to: 1 by: -1) collect: [ :n | SortTestData new value: n ]).
|
|
566 |
testData addAll: ((100 to: 1 by: -1) collect: [ :n | SortTestData new value: n ]).
|
|
567 |
1 to: testData size do: [ :i | (testData at: i) order: i ].
|
|
568 |
|
|
569 |
col := (SortedCollection new sortBlock: [ :v1 :v2 | v1 value <= v2 value ]) addAll: testData.
|
|
570 |
lastTestData := nil.
|
|
571 |
|
|
572 |
errs := col inject: 0 into:
|
|
573 |
[ :totIn :aSortTestData | |totOut|
|
|
574 |
totOut := totIn.
|
|
575 |
lastTestData notNil ifTrue:
|
|
576 |
[ lastTestData value = aSortTestData value ifTrue:
|
|
577 |
[ lastTestData order > aSortTestData order ifTrue: [ totOut := totOut + 1 ] ] ].
|
|
578 |
lastTestData := aSortTestData.
|
|
579 |
totOut ].
|
|
580 |
self should: [ errs = 0 ]
|
|
581 |
|
|
582 |
"
|
|
583 |
self basicNew testSortStability01
|
|
584 |
"
|
|
585 |
!
|
|
586 |
|
|
587 |
testSortStability02
|
|
588 |
"Verify that the sort is stable"
|
|
589 |
|
|
590 |
| testData col lastTestData errs |
|
|
591 |
|
|
592 |
testData := OrderedCollection new.
|
|
593 |
(25 to: 1 by: -1) do: [ :i | 1 to: 100 do: [ :j | testData add: (SortTestData new value: i ) ] ].
|
|
594 |
1 to: testData size do: [ :i | (testData at: i) order: i ].
|
|
595 |
|
|
596 |
col := (SortedCollection new sortBlock: [ :v1 :v2 | v1 value <= v2 value ]) addAll: testData.
|
|
597 |
lastTestData := nil.
|
|
598 |
|
|
599 |
errs := col inject: 0 into:
|
|
600 |
[ :totIn :aSortTestData | |totOut|
|
|
601 |
totOut := totIn.
|
|
602 |
lastTestData notNil ifTrue:
|
|
603 |
[ lastTestData value = aSortTestData value ifTrue:
|
|
604 |
[ lastTestData order > aSortTestData order ifTrue: [ totOut := totOut + 1 ] ] ].
|
|
605 |
lastTestData := aSortTestData.
|
|
606 |
totOut ].
|
|
607 |
self should: [ errs = 0 ]
|
|
608 |
|
|
609 |
"
|
|
610 |
self basicNew testSortStability02
|
|
611 |
"
|
|
612 |
! !
|
|
613 |
|
293
|
614 |
!CollectionTests methodsFor:'tests-squeak'!
|
|
615 |
|
|
616 |
testIfEmptyifNotEmpty
|
|
617 |
self assert: (empty ifEmpty: [true] ifNotEmpty: [false]).
|
|
618 |
self assert: (nonEmpty ifEmpty: [false] ifNotEmpty: [true]).
|
|
619 |
self assert: (nonEmpty ifEmpty: [false] ifNotEmpty: [:s | s first = #x])
|
|
620 |
!
|
|
621 |
|
|
622 |
testIfEmptyifNotEmptyDo
|
|
623 |
self assert: (empty ifEmpty: [true] ifNotEmptyDo: [:s | false]).
|
|
624 |
self assert: (nonEmpty ifEmpty: [false] ifNotEmptyDo: [:s | s first = #x])
|
|
625 |
!
|
|
626 |
|
|
627 |
testIfNotEmpty
|
|
628 |
empty ifNotEmpty: [self assert: false].
|
|
629 |
self assert: (nonEmpty ifNotEmpty: [self]) == self.
|
|
630 |
self assert: (nonEmpty ifNotEmpty: [:s | s first]) = #x
|
|
631 |
!
|
|
632 |
|
|
633 |
testIfNotEmptyDo
|
|
634 |
empty ifNotEmptyDo: [:s | self assert: false].
|
|
635 |
self assert: (nonEmpty ifNotEmptyDo: [:s | s first]) = #x
|
|
636 |
!
|
|
637 |
|
|
638 |
testIfNotEmptyDoifNotEmpty
|
|
639 |
self assert: (empty ifNotEmptyDo: [:s | false] ifEmpty: [true]).
|
|
640 |
self assert: (nonEmpty ifNotEmptyDo: [:s | s first = #x] ifEmpty: [false])
|
|
641 |
!
|
|
642 |
|
|
643 |
testIfNotEmptyifEmpty
|
|
644 |
self assert: (empty ifEmpty: [true] ifNotEmpty: [false]).
|
|
645 |
self assert: (nonEmpty ifEmpty: [false] ifNotEmpty: [true]).
|
|
646 |
self assert: (nonEmpty ifEmpty: [false] ifNotEmpty: [:s | s first = #x])
|
|
647 |
! !
|
|
648 |
|
212
|
649 |
!CollectionTests::SortTestData methodsFor:'accessing'!
|
|
650 |
|
|
651 |
value
|
|
652 |
"Answer the value of the receiver's ''value'' instance variable."
|
|
653 |
|
|
654 |
^value
|
|
655 |
!
|
|
656 |
|
|
657 |
value: anObject
|
|
658 |
"Set the value of the receiver's ''value'' instance variable to the argument, anObject."
|
|
659 |
|
|
660 |
value := anObject
|
|
661 |
! !
|
|
662 |
|
|
663 |
!CollectionTests::SortTestData methodsFor:'order'!
|
|
664 |
|
|
665 |
order
|
|
666 |
"Answer the value of the receiver's ''order'' instance variable."
|
|
667 |
|
|
668 |
^order
|
|
669 |
!
|
|
670 |
|
|
671 |
order: anObject
|
|
672 |
"Set the value of the receiver's ''order'' instance variable to the argument, anObject."
|
|
673 |
|
|
674 |
order := anObject
|
|
675 |
! !
|
|
676 |
|
|
677 |
!CollectionTests::SortTestData methodsFor:'printing & storing'!
|
|
678 |
|
|
679 |
printOn: aStream
|
|
680 |
super printOn: aStream.
|
|
681 |
aStream
|
|
682 |
nextPutAll: '(value: ';
|
|
683 |
nextPutAll: value printString;
|
|
684 |
nextPutAll: ' order: ';
|
|
685 |
nextPutAll: order printString;
|
|
686 |
nextPut: $)
|
115
|
687 |
! !
|
|
688 |
|
|
689 |
!CollectionTests class methodsFor:'documentation'!
|
|
690 |
|
|
691 |
version
|
|
692 |
^ '$Header$'
|
|
693 |
! !
|