author | Stefan Vogel <sv@exept.de> |
Thu, 12 Apr 2012 21:11:11 +0200 | |
changeset 656 | 470e994fc84b |
parent 655 | 43841f85365a |
child 657 | 4f04a8bf19c5 |
permissions | -rw-r--r-- |
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 |
||
585 | 21 |
coveredClasses |
22 |
^ Collection withAllSubclasses |
|
23 |
! |
|
24 |
||
115 | 25 |
documentation |
26 |
" |
|
27 |
documentation to be added. |
|
28 |
||
29 |
[author:] |
|
30 |
Claus Gittinger (cg@alan) |
|
31 |
||
32 |
[see also:] |
|
33 |
||
34 |
[instance variables:] |
|
35 |
||
36 |
[class variables:] |
|
37 |
" |
|
38 |
! |
|
39 |
||
40 |
history |
|
41 |
"Created: / 8.11.2001 / 08:17:09 / cg" |
|
42 |
! ! |
|
43 |
||
301 | 44 |
!CollectionTests methodsFor:'helpers'! |
45 |
||
655 | 46 |
checkSorted:aSortedCollection with:sortBlock against:anOriginalCollection |
47 |
|first| |
|
48 |
||
49 |
self assert:aSortedCollection size = anOriginalCollection size. |
|
50 |
||
51 |
"is it sorted?" |
|
52 |
first := true. |
|
53 |
aSortedCollection inject:nil into:[:last :each| |
|
54 |
first ifTrue:[first := false] |
|
55 |
ifFalse:[self assert:(sortBlock value:last value:each)]. each]. |
|
56 |
||
57 |
"same elements as in original?" |
|
58 |
self assert:(aSortedCollection asBag = anOriginalCollection asBag). |
|
59 |
! |
|
60 |
||
301 | 61 |
collectedDoArgsOf:aCollection |
62 |
|collectedDoArgs| |
|
63 |
||
64 |
collectedDoArgs := OrderedCollection new. |
|
65 |
aCollection do:[:each | collectedDoArgs add:each]. |
|
66 |
^ collectedDoArgs |
|
67 |
! ! |
|
68 |
||
293 | 69 |
!CollectionTests methodsFor:'initialize-release'! |
70 |
||
71 |
setUp |
|
295 | 72 |
empty := Set new. |
73 |
nonEmpty := OrderedCollection with: #x. |
|
293 | 74 |
! ! |
75 |
||
264 | 76 |
!CollectionTests methodsFor:'tests-dictionary'! |
115 | 77 |
|
78 |
testDictionary01 |
|
301 | 79 |
|d v1 v2 v3 ret| |
115 | 80 |
|
81 |
d := Dictionary new. |
|
82 |
d at:1 put:(v1 := 'one'). |
|
83 |
d at:2 put:(v2 := 'two'). |
|
84 |
d at:3 put:(v3 := 'three'). |
|
85 |
||
86 |
self assert: ( d size == 3 ). |
|
301 | 87 |
|
585 | 88 |
self assert:(ret := self collectedDoArgsOf:d) asArray = #('one' 'two' 'three'). |
301 | 89 |
self assert:(ret := d collect:[:each | each]) = #('one' 'two' 'three') asBag. |
90 |
self assert:(ret := d select:[:each | true]) = (Dictionary withAssociations:(Array with:(1->'one') with:(2->'two') with:(3->'three'))). |
|
91 |
||
115 | 92 |
self assert: ( (d at:1) == v1 ). |
93 |
self assert: ( (d at:2) == v2 ). |
|
94 |
self assert: ( (d at:3) == v3 ). |
|
95 |
||
96 |
d at:3 put:(v3 := 'drei'). |
|
97 |
||
98 |
self assert: ( d size == 3 ). |
|
99 |
||
100 |
self assert: ( (d at:1) == v1 ). |
|
101 |
self assert: ( (d at:2) == v2 ). |
|
102 |
self assert: ( (d at:3) == v3 ). |
|
103 |
||
104 |
self assert: ( (d keyAtValue:v1) = 1 ). |
|
105 |
||
106 |
" |
|
107 |
self basicNew testDictionary01 |
|
108 |
" |
|
585 | 109 |
|
110 |
"Modified: / 04-07-2011 / 19:28:39 / cg" |
|
264 | 111 |
! ! |
112 |
||
113 |
!CollectionTests methodsFor:'tests-general'! |
|
114 |
||
615 | 115 |
doTestDictionaryLikeAddRemoveOperationsIn:aClass |
116 |
|coll| |
|
117 |
||
118 |
coll := aClass new. |
|
119 |
||
120 |
self assert: ( coll size == 0 ). |
|
121 |
self assert: ( coll isEmpty ). |
|
122 |
self assert: ( coll notEmpty not ). |
|
123 |
self assert: ( coll isEmptyOrNil ). |
|
124 |
self assert: ( coll notEmptyOrNil not ). |
|
125 |
||
126 |
coll at:1 put:'one'. |
|
127 |
||
128 |
self assert: ( coll size == 1 ). |
|
129 |
self assert: ( coll isEmpty not). |
|
130 |
self assert: ( coll notEmpty ). |
|
131 |
self assert: ( coll isEmptyOrNil not ). |
|
132 |
self assert: ( coll notEmptyOrNil ). |
|
133 |
||
134 |
coll at:2 put:'two'. |
|
135 |
||
136 |
self assert: ( coll size == 2 ). |
|
137 |
self assert: ( coll isEmpty not). |
|
138 |
self assert: ( coll notEmpty ). |
|
139 |
self assert: ( coll isEmptyOrNil not ). |
|
140 |
self assert: ( coll notEmptyOrNil ). |
|
141 |
||
142 |
coll removeKey:2. |
|
143 |
self assert: ( coll size == 1 ). |
|
144 |
self assert: ( coll isEmpty not). |
|
145 |
self assert: ( coll notEmpty ). |
|
146 |
self assert: ( coll isEmptyOrNil not ). |
|
147 |
self assert: ( coll notEmptyOrNil ). |
|
148 |
||
149 |
coll removeKey:1. |
|
150 |
self assert: ( coll size == 0 ). |
|
151 |
self assert: ( coll isEmpty ). |
|
152 |
self assert: ( coll notEmpty not ). |
|
153 |
self assert: ( coll isEmptyOrNil ). |
|
154 |
self assert: ( coll notEmptyOrNil not ). |
|
155 |
||
156 |
" |
|
157 |
self basicNew doTestDictionaryLikeAddRemoveOperations |
|
158 |
" |
|
159 |
||
160 |
"Created: / 08-08-2011 / 16:01:14 / cg" |
|
161 |
! |
|
162 |
||
163 |
doTestGeneralAddRemoveOperationsIn:aClass |
|
164 |
|coll| |
|
165 |
||
166 |
coll := aClass new. |
|
167 |
||
168 |
self assert: ( coll size == 0 ). |
|
169 |
self assert: ( coll isEmpty ). |
|
170 |
self assert: ( coll notEmpty not ). |
|
171 |
self assert: ( coll isEmptyOrNil ). |
|
172 |
self assert: ( coll notEmptyOrNil not ). |
|
173 |
||
174 |
coll add:1. |
|
175 |
||
176 |
self assert: ( coll size == 1 ). |
|
177 |
self assert: ( coll isEmpty not). |
|
178 |
self assert: ( coll notEmpty ). |
|
179 |
self assert: ( coll isEmptyOrNil not ). |
|
180 |
self assert: ( coll notEmptyOrNil ). |
|
181 |
||
182 |
coll add:2. |
|
183 |
||
184 |
self assert: ( coll size == 2 ). |
|
185 |
self assert: ( coll isEmpty not). |
|
186 |
self assert: ( coll notEmpty ). |
|
187 |
self assert: ( coll isEmptyOrNil not ). |
|
188 |
self assert: ( coll notEmptyOrNil ). |
|
189 |
||
190 |
coll remove:2. |
|
191 |
self assert: ( coll size == 1 ). |
|
192 |
self assert: ( coll isEmpty not). |
|
193 |
self assert: ( coll notEmpty ). |
|
194 |
self assert: ( coll isEmptyOrNil not ). |
|
195 |
self assert: ( coll notEmptyOrNil ). |
|
196 |
||
197 |
coll remove:1. |
|
198 |
self assert: ( coll size == 0 ). |
|
199 |
self assert: ( coll isEmpty ). |
|
200 |
self assert: ( coll notEmpty not ). |
|
201 |
self assert: ( coll isEmptyOrNil ). |
|
202 |
self assert: ( coll notEmptyOrNil not ). |
|
203 |
||
204 |
" |
|
205 |
self basicNew testGeneralOperations |
|
206 |
" |
|
207 |
||
208 |
"Created: / 08-08-2011 / 16:00:06 / cg" |
|
209 |
! |
|
210 |
||
264 | 211 |
doTestGeneralCollectionOperationsIn:aClass |
212 |
|coll| |
|
213 |
||
214 |
coll := aClass new. |
|
215 |
||
216 |
self assert: ( coll size == 0 ). |
|
217 |
self assert: ( coll isEmpty ). |
|
218 |
self assert: ( coll notEmpty not ). |
|
219 |
self assert: ( coll isEmptyOrNil ). |
|
220 |
self assert: ( coll notEmptyOrNil not ). |
|
221 |
||
222 |
self should: [ coll first ] raise:Error. |
|
223 |
self should: [ coll last ] raise:Error. |
|
224 |
self should: [ (coll at:1) ] raise:Error. |
|
225 |
||
226 |
self assert: ( (coll includes:0) not ). |
|
227 |
||
228 |
" |
|
229 |
self basicNew testGeneralOperations |
|
230 |
" |
|
156 | 231 |
! |
232 |
||
615 | 233 |
testDictionaryLikeAddRemoveOperations |
234 |
|classes| |
|
235 |
||
236 |
classes := OrderedCollection new. |
|
237 |
classes |
|
238 |
add:Dictionary; |
|
239 |
add:IdentityDictionary; |
|
240 |
add:OrderedDictionary; |
|
241 |
add:BTree. |
|
242 |
||
243 |
classes do:[:eachClass | |
|
244 |
self doTestDictionaryLikeAddRemoveOperationsIn:eachClass |
|
245 |
]. |
|
246 |
||
247 |
" |
|
248 |
self basicNew testDictionaryLikeAddRemoveOperations |
|
249 |
" |
|
250 |
||
251 |
"Created: / 08-08-2011 / 15:59:52 / cg" |
|
252 |
! |
|
253 |
||
254 |
testGeneralAddRemoveOperations |
|
255 |
|classes| |
|
256 |
||
257 |
classes := OrderedCollection new. |
|
258 |
classes |
|
259 |
"/ add:RunArray; |
|
260 |
add:Bag; |
|
261 |
add:Set; |
|
262 |
add:IdentitySet; |
|
263 |
"/add:Dictionary; |
|
264 |
"/add:IdentityDictionary; |
|
265 |
add:OrderedCollection; |
|
266 |
add:SortedCollection; |
|
267 |
"/add:Queue; |
|
268 |
"/add:Stack; |
|
269 |
add:SortedCollection; |
|
270 |
"/add:OrderedDictionary; |
|
271 |
add:OrderedSet. |
|
272 |
"/add:BTree. |
|
273 |
||
274 |
classes do:[:eachClass | |
|
275 |
self doTestGeneralAddRemoveOperationsIn:eachClass |
|
276 |
]. |
|
277 |
||
278 |
" |
|
279 |
self basicNew testGeneralAddRemoveOperations |
|
280 |
" |
|
281 |
||
282 |
"Created: / 08-08-2011 / 15:59:02 / cg" |
|
283 |
! |
|
284 |
||
264 | 285 |
testGeneralOperations |
286 |
|classes| |
|
287 |
||
288 |
classes := OrderedCollection new. |
|
289 |
classes |
|
290 |
add:RunArray; |
|
291 |
add:Bag; |
|
292 |
add:Set; |
|
293 |
add:IdentitySet; |
|
294 |
add:Dictionary; |
|
295 |
add:IdentityDictionary; |
|
296 |
add:OrderedCollection; |
|
297 |
add:SortedCollection; |
|
298 |
add:Queue; |
|
299 |
add:Stack; |
|
300 |
add:SortedCollection; |
|
301 |
add:OrderedDictionary; |
|
615 | 302 |
add:OrderedSet; |
303 |
add:BTree. |
|
264 | 304 |
|
305 |
classes do:[:eachClass | |
|
306 |
self doTestGeneralCollectionOperationsIn:eachClass |
|
307 |
]. |
|
308 |
||
309 |
" |
|
310 |
self basicNew testGeneralOperations |
|
311 |
" |
|
615 | 312 |
|
313 |
"Modified: / 08-08-2011 / 15:53:16 / cg" |
|
264 | 314 |
! ! |
315 |
||
316 |
!CollectionTests methodsFor:'tests-interval'! |
|
317 |
||
162 | 318 |
testInterval01 |
644 | 319 |
|i1 i2| |
162 | 320 |
|
644 | 321 |
i1 := 1 to:10. |
162 | 322 |
|
644 | 323 |
self assert: ( i1 size == 10 ). |
162 | 324 |
|
644 | 325 |
self assert: ( i1 includes:1 ). |
326 |
self assert: ( i1 includes:10 ). |
|
162 | 327 |
|
644 | 328 |
self assert: ( i1 min == 1 ). |
329 |
self assert: ( i1 max == 10 ). |
|
162 | 330 |
|
331 |
"cannot add/remove" |
|
644 | 332 |
self should:[i1 add:99] raise:Error. |
333 |
self should:[i1 remove:10] raise:Error. |
|
334 |
||
335 |
i2 := 2 to:20 by:2. |
|
336 |
self assert: ( i2 size == 10 ). |
|
337 |
||
338 |
self assert: ( i2 includes:0 ) not. |
|
339 |
self assert: ( i2 includes:1 ) not. |
|
340 |
self assert: ( i2 includes:2 ). |
|
341 |
self assert: ( i2 includes:3 ) not. |
|
342 |
self assert: ( i2 includes:10 ). |
|
343 |
self assert: ( i2 includes:19 ) not. |
|
344 |
self assert: ( i2 includes:20 ). |
|
345 |
self assert: ( i2 includes:21 ) not. |
|
346 |
self assert: ( i2 includes:22 ) not. |
|
347 |
||
348 |
self assert: ( i2 min == 2 ). |
|
349 |
self assert: ( i2 max == 20 ). |
|
350 |
||
351 |
self assert: ( i2 keys asArray = (1 to:10) asArray ). |
|
162 | 352 |
|
353 |
" |
|
354 |
self basicNew testInterval01 |
|
355 |
" |
|
644 | 356 |
|
357 |
"Modified: / 10-10-2011 / 13:10:43 / cg" |
|
264 | 358 |
! ! |
359 |
||
301 | 360 |
!CollectionTests methodsFor:'tests-orderedCollection'! |
361 |
||
656
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
362 |
testMergeSortStability01 |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
363 |
"Verify that the sort is stable" |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
364 |
|
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
365 |
| testData lastTestData errs col| |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
366 |
|
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
367 |
testData := OrderedCollection new. |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
368 |
testData addAll: ((100 to: 1 by: -1) collect: [ :n | SortTestData new value: n ]). |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
369 |
testData addAll: ((100 to: 1 by: -1) collect: [ :n | SortTestData new value: n ]). |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
370 |
testData addAll: ((100 to: 1 by: -1) collect: [ :n | SortTestData new value: n ]). |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
371 |
testData keysAndValuesDo:[:idx :each| each order: idx ]. |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
372 |
|
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
373 |
col := testData copy. |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
374 |
col mergeSort:[:v1 :v2 | v1 value < v2 value ]. |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
375 |
self checkSorted:col with:[:v1 :v2 | v1 value <= v2 value ] against:testData. |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
376 |
|
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
377 |
lastTestData := nil. |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
378 |
|
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
379 |
errs := col inject: 0 into: |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
380 |
[ :totIn :aSortTestData | |totOut| |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
381 |
totOut := totIn. |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
382 |
lastTestData notNil ifTrue: |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
383 |
[ lastTestData value = aSortTestData value ifTrue: |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
384 |
[ lastTestData order > aSortTestData order ifTrue: [ totOut := totOut + 1 ] ] ]. |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
385 |
lastTestData := aSortTestData. |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
386 |
totOut ]. |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
387 |
self should: [ errs = 0 ] |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
388 |
|
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
389 |
" |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
390 |
self basicNew testMergeSortStability01 |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
391 |
" |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
392 |
! |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
393 |
|
301 | 394 |
testOrderedCollection01_Sort1 |
395 |
|col col2| |
|
396 |
||
397 |
col := #( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ) copy. |
|
398 |
col sort. |
|
585 | 399 |
self assert:( col sameContentsAs: (1 to:20)). |
301 | 400 |
|
401 |
col := #( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ) copy reverse. |
|
402 |
col sort. |
|
585 | 403 |
self assert:( col sameContentsAs: (1 to:20) ). |
301 | 404 |
|
405 |
col := #( 10 2 13 4 15 6 17 8 19 11 1 12 3 14 5 16 7 18 9 20 ) copy. |
|
406 |
col sort. |
|
585 | 407 |
self assert:( col sameContentsAs: (1 to:20) ). |
301 | 408 |
|
409 |
col := ( 1 to: 10000 ) asOrderedCollection. |
|
410 |
col sort. |
|
585 | 411 |
self assert:( col sameContentsAs: (1 to:10000) ). |
301 | 412 |
|
413 |
col := ( 1 to: 10000 ) asOrderedCollection. |
|
414 |
col2 := ( 1 to: 10000 ) asOrderedCollection. |
|
415 |
col sortWith:col2. |
|
585 | 416 |
self assert:( col sameContentsAs: (1 to:10000) ). |
417 |
self assert:( col2 sameContentsAs: (1 to:10000) ). |
|
301 | 418 |
|
419 |
col := ( 10000 to: 1 by:-1) asOrderedCollection. |
|
420 |
col2 := ( 10000 to: 1 by:-1) asOrderedCollection. |
|
421 |
col sortWith:col2. |
|
585 | 422 |
self assert:( col sameContentsAs: (1 to:10000) ). |
423 |
self assert:( col2 sameContentsAs: (1 to:10000) ). |
|
301 | 424 |
|
425 |
" |
|
426 |
self new testOrderedCollection01_Sort1 |
|
427 |
" |
|
585 | 428 |
|
429 |
"Modified: / 04-07-2011 / 19:29:54 / cg" |
|
301 | 430 |
! |
431 |
||
432 |
testOrderedCollection02_SortBigCollection |
|
433 |
| size col sortBlock| |
|
434 |
||
435 |
size := 12500. |
|
436 |
col := OrderedCollection new. |
|
437 |
1 to:size do:[:el| |
|
438 |
col add:nil. |
|
439 |
]. |
|
440 |
sortBlock := [:entry1 :entry2 | |
|
441 |
((entry1 isNil) or:[entry2 isNil]) ifTrue:[ |
|
442 |
true |
|
443 |
] ifFalse:[ |
|
444 |
false |
|
445 |
] |
|
446 |
]. |
|
447 |
self shouldnt:[col sort:sortBlock] raise:RecursionInterruptSignal. |
|
448 |
||
449 |
" |
|
450 |
self new testOrderedCollection02_SortBigCollection |
|
451 |
" |
|
452 |
! |
|
453 |
||
454 |
testOrderedCollection03_SortBigCollection2 |
|
455 |
| size col| |
|
456 |
||
457 |
size := 12500. |
|
458 |
col := OrderedCollection new. |
|
459 |
1 to:size do:[:el| |
|
460 |
col add:'abc'. |
|
461 |
]. |
|
462 |
self shouldnt:[col sort] raise:RecursionInterruptSignal. |
|
463 |
||
464 |
" |
|
465 |
self new testOrderedCollection03_SortBigCollection2 |
|
466 |
" |
|
467 |
! |
|
468 |
||
469 |
testOrderedCollection04_SortNilsInCollection |
|
470 |
| size col sortBlock| |
|
471 |
||
472 |
size := 125. |
|
473 |
col := OrderedCollection new. |
|
474 |
1 to:size do:[:el| |
|
475 |
col add:nil. |
|
476 |
]. |
|
477 |
sortBlock := [:entry1 :entry2 | |
|
478 |
((entry1 isNil) or:[entry2 isNil]) ifTrue:[ |
|
479 |
true |
|
480 |
] ifFalse:[ |
|
481 |
false |
|
482 |
] |
|
483 |
]. |
|
484 |
"/ col sort:sortBlock. |
|
485 |
self shouldnt:[col sort:sortBlock] raise:RecursionInterruptSignal. |
|
486 |
||
487 |
" |
|
488 |
self new testOrderedCollection04_SortNilsInCollection |
|
489 |
" |
|
655 | 490 |
! |
491 |
||
492 |
testOrderedCollection05_SortRandomCollection |
|
493 |
|data sorted rg coData checkBlock| |
|
494 |
||
495 |
checkBlock := [:a :b| a <= b]. |
|
496 |
rg := Random new. |
|
497 |
||
498 |
#(1 4 11 2000 20011 200000) do:[:eachSize| |
|
499 |
data := Array new:eachSize. |
|
500 |
1 to:data size do:[:i | |
|
501 |
data at:i put:(rg nextIntegerBetween:1 and:100). |
|
502 |
]. |
|
503 |
sorted := data copy sort. |
|
504 |
self checkSorted:sorted with:checkBlock against:data. |
|
505 |
sorted := sorted sort. |
|
506 |
self checkSorted:sorted with:checkBlock against:data. |
|
507 |
sorted := sorted reverse sort. |
|
508 |
self checkSorted:sorted with:checkBlock against:data. |
|
509 |
||
510 |
coData := data copy. |
|
511 |
sorted := data copy sortWith:coData. |
|
512 |
self checkSorted:sorted with:checkBlock against:data. |
|
513 |
self checkSorted:coData with:checkBlock against:data. |
|
514 |
||
515 |
sorted := data copy sort:[:policy :a :b| a < b]. |
|
516 |
self checkSorted:sorted with:checkBlock against:data. |
|
517 |
||
518 |
sorted := data copy sort:[:a :b| a < b]. |
|
519 |
self checkSorted:sorted with:checkBlock against:data. |
|
520 |
||
521 |
coData := data copy. |
|
522 |
sorted := data copy sort:[:a :b| a < b] with:coData. |
|
523 |
self checkSorted:sorted with:checkBlock against:data. |
|
524 |
self checkSorted:coData with:checkBlock against:data. |
|
525 |
]. |
|
656
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
526 |
! |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
527 |
|
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
528 |
testOrderedCollection06_MergeSortRandomCollection |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
529 |
|data sorted rg checkBlock| |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
530 |
|
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
531 |
checkBlock := [:a :b| a <= b]. |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
532 |
rg := Random new. |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
533 |
|
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
534 |
#(1 4 11 2000 20011 200000) do:[:eachSize| |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
535 |
data := Array new:eachSize. |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
536 |
1 to:data size do:[:i | |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
537 |
data at:i put:(rg nextIntegerBetween:1 and:100). |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
538 |
]. |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
539 |
sorted := data copy mergeSort. |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
540 |
self checkSorted:sorted with:checkBlock against:data. |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
541 |
sorted := sorted mergeSort. |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
542 |
self checkSorted:sorted with:checkBlock against:data. |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
543 |
sorted := sorted reverse mergeSort. |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
544 |
self checkSorted:sorted with:checkBlock against:data. |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
545 |
|
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
546 |
sorted := data copy mergeSort:[:a :b| a < b]. |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
547 |
self checkSorted:sorted with:checkBlock against:data. |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
548 |
]. |
301 | 549 |
! ! |
550 |
||
551 |
!CollectionTests methodsFor:'tests-orderedDictionary'! |
|
552 |
||
553 |
testOrderedDictionary01 |
|
554 |
|d ret| |
|
555 |
||
556 |
d := OrderedDictionary new. |
|
557 |
||
558 |
self assert: ( d size == 0 ). |
|
559 |
self assert: ( d isEmpty ). |
|
560 |
self assert: ( d notEmpty not ). |
|
561 |
||
562 |
d at:'one' put:1. |
|
563 |
d at:'two' put:2. |
|
564 |
d at:'three' put:3. |
|
565 |
||
566 |
self assert: ( d size == 3 ). |
|
567 |
self assert: ( d isEmpty not). |
|
568 |
self assert: ( d notEmpty ). |
|
569 |
||
585 | 570 |
self assert:((ret := self collectedDoArgsOf:d) sameContentsAs: #(1 2 3)). |
301 | 571 |
|
572 |
self assert: ( (d atIndex:1) == 1 ). |
|
573 |
self assert: ( (d atIndex:2) == 2 ). |
|
574 |
self assert: ( (d atIndex:3) == 3 ). |
|
575 |
||
576 |
self assert: ( (d first) = ('one'->1) ). |
|
577 |
self assert: ( (d last) = ('three'->3) ). |
|
578 |
||
585 | 579 |
self assert:((ret := d collect:[:each | each]) sameContentsAs: #(1 2 3)). |
580 |
self assert:((ret := d select:[:each | true]) sameContentsAs: (OrderedDictionary withAssociations:(Array with:('one'->1) with:('two'->2) with:('three'->3)))). |
|
301 | 581 |
|
582 |
self assert: ( d includes:1 ). |
|
583 |
self assert: ( d includes:2 ). |
|
584 |
self assert: ( d includes:3 ). |
|
585 |
||
586 |
self assert: ( d includesKey:'one' ). |
|
587 |
self assert: ( d includesKey:'two' ). |
|
588 |
self assert: ( d includesKey:'three' ). |
|
589 |
||
590 |
self assert: ((ret := d removeFirst) = 1). |
|
591 |
self assert: ( d size == 2 ). |
|
592 |
self assert: ((ret := d removeLast) = 3). |
|
593 |
self assert: ( d size == 1 ). |
|
594 |
self assert: ((ret := d removeLast) = 2). |
|
595 |
self assert: ( d size == 0 ). |
|
596 |
self assert: ( d isEmpty ). |
|
597 |
self assert: ( d notEmpty not ). |
|
598 |
||
599 |
" |
|
600 |
self basicNew testOrderedDictionary01 |
|
601 |
" |
|
585 | 602 |
|
603 |
"Modified: / 04-07-2011 / 19:32:34 / cg" |
|
301 | 604 |
! ! |
605 |
||
606 |
!CollectionTests methodsFor:'tests-orderedSet'! |
|
162 | 607 |
|
156 | 608 |
testOrderedSet01 |
609 |
|s v1 v2 v3| |
|
610 |
||
611 |
s := OrderedSet new. |
|
612 |
s add:(v1 := 'one'). |
|
613 |
s add:(v2 := 'two'). |
|
614 |
s add:(v3 := 'three'). |
|
615 |
||
616 |
self assert: ( s size == 3 ). |
|
617 |
self assert: ( (s at:1) == v1 ). |
|
618 |
self assert: ( (s at:2) == v2 ). |
|
619 |
self assert: ( (s at:3) == v3 ). |
|
620 |
||
621 |
self assert: ( s includes:v1 ). |
|
622 |
self assert: ( s includes:v2 ). |
|
623 |
self assert: ( s includes:v3 ). |
|
624 |
||
625 |
self assert: ( s includes:v1 copy). |
|
626 |
self assert: ( s includes:v2 copy). |
|
627 |
self assert: ( s includes:v3 copy). |
|
628 |
||
629 |
"already in - adding again should not change things" |
|
630 |
s add:v1. |
|
631 |
self assert: ( s size == 3 ). |
|
632 |
||
633 |
self assert: ( (s at:1) == v1 ). |
|
634 |
self assert: ( (s at:2) == v2 ). |
|
635 |
self assert: ( (s at:3) == v3 ). |
|
636 |
||
637 |
"set i.e. not identitySet - adding copy should not change things" |
|
638 |
s add:v1 copy. |
|
639 |
self assert: ( s size == 3 ). |
|
640 |
||
641 |
self assert: ( (s at:1) == v1 ). |
|
642 |
self assert: ( (s at:2) == v2 ). |
|
643 |
self assert: ( (s at:3) == v3 ). |
|
644 |
||
645 |
s remove:v1. |
|
646 |
||
647 |
self assert: ( s size == 2 ). |
|
648 |
self assert: ( (s at:1) == v2 ). |
|
649 |
self assert: ( (s at:2) == v3 ). |
|
650 |
||
651 |
self assert: ( s includes:v1 ) not. |
|
652 |
self assert: ( s includes:v2 ). |
|
653 |
self assert: ( s includes:v3 ). |
|
654 |
||
655 |
self assert: ( s includes:v1 copy) not. |
|
656 |
self assert: ( s includes:v2 copy). |
|
657 |
self assert: ( s includes:v3 copy). |
|
658 |
||
659 |
s add:v1. |
|
660 |
||
661 |
self assert: ( s size == 3 ). |
|
662 |
self assert: ( (s at:1) == v2 ). |
|
663 |
self assert: ( (s at:2) == v3 ). |
|
664 |
self assert: ( (s at:3) == v1 ). |
|
665 |
||
666 |
self assert: ( s includes:v1 ). |
|
667 |
self assert: ( s includes:v2 ). |
|
668 |
self assert: ( s includes:v3 ). |
|
669 |
||
670 |
self assert: ( s includes:v1 copy). |
|
671 |
self assert: ( s includes:v2 copy). |
|
672 |
self assert: ( s includes:v3 copy). |
|
673 |
||
674 |
s removeLast. |
|
675 |
||
676 |
self assert: ( s size == 2 ). |
|
677 |
self assert: ( (s at:1) == v2 ). |
|
678 |
self assert: ( (s at:2) == v3 ). |
|
679 |
||
680 |
self assert: ( s includes:v1 ) not. |
|
681 |
self assert: ( s includes:v2 ). |
|
682 |
self assert: ( s includes:v3 ). |
|
683 |
||
684 |
self assert: ( s includes:v1 copy) not. |
|
685 |
self assert: ( s includes:v2 copy). |
|
686 |
self assert: ( s includes:v3 copy). |
|
687 |
||
688 |
" |
|
689 |
self basicNew testOrderedSet01 |
|
690 |
" |
|
301 | 691 |
! |
692 |
||
693 |
testOrderedSet02 |
|
694 |
|col| |
|
695 |
||
696 |
col := OrderedSet new. |
|
697 |
||
698 |
col add:'a'. |
|
699 |
col add:'b'. |
|
700 |
col add:'c'. |
|
701 |
self assert:( col size == 3 ). |
|
702 |
self assert:( Array streamContents:[:s | |
|
703 |
col do:[:each | s nextPut:each] |
|
704 |
] |
|
705 |
) = #('a' 'b' 'c' ). |
|
706 |
||
707 |
" |
|
708 |
self new testOrderedSet02 |
|
709 |
" |
|
710 |
! |
|
711 |
||
712 |
testOrderedSet03 |
|
713 |
|col| |
|
714 |
||
715 |
col := OrderedSet new. |
|
716 |
||
717 |
col add:'a'. |
|
718 |
col add:'b'. |
|
719 |
col add:'c'. |
|
720 |
col add:'a'. |
|
721 |
col add:'b'. |
|
722 |
self assert:( col size == 3 ). |
|
723 |
self assert:( Array streamContents:[:s | |
|
724 |
col do:[:each | s nextPut:each] |
|
725 |
] |
|
726 |
) = #('a' 'b' 'c' ). |
|
727 |
||
728 |
" |
|
729 |
self new testOrderedSet03 |
|
730 |
" |
|
731 |
! |
|
732 |
||
733 |
testOrderedSet04 |
|
734 |
|col| |
|
735 |
||
736 |
col := OrderedSet new. |
|
737 |
||
738 |
col add:'a'. |
|
739 |
col addFirst:'b'. |
|
740 |
col addFirst:'c'. |
|
741 |
self assert:( col size == 3 ). |
|
742 |
self assert:( Array streamContents:[:s | |
|
743 |
col do:[:each | s nextPut:each] |
|
744 |
] |
|
745 |
) = #('c' 'b' 'a' ). |
|
746 |
||
747 |
" |
|
748 |
self new testOrderedSet04 |
|
749 |
" |
|
750 |
! |
|
751 |
||
752 |
testOrderedSet05 |
|
753 |
|col| |
|
754 |
||
755 |
col := OrderedSet new. |
|
756 |
||
757 |
col add:'a'. |
|
758 |
col addFirst:'b'. |
|
759 |
col addFirst:'c'. |
|
760 |
col add:'a'. |
|
761 |
col add:'b'. |
|
762 |
col add:'c'. |
|
763 |
self assert:( col size == 3 ). |
|
764 |
self assert:( Array streamContents:[:s | |
|
765 |
col do:[:each | s nextPut:each] |
|
766 |
] |
|
767 |
) = #('c' 'b' 'a' ). |
|
768 |
||
769 |
" |
|
770 |
self new testOrderedSet05 |
|
771 |
" |
|
264 | 772 |
! ! |
773 |
||
297 | 774 |
!CollectionTests methodsFor:'tests-replace'! |
775 |
||
776 |
testEmptyReplace |
|
777 |
| array1 array2 | |
|
778 |
||
779 |
array1 := Array with: 1 with: 2 with: 3 with: 4. |
|
780 |
array2 := Array with: 5 with: 6 with: 7. |
|
781 |
||
782 |
array1 replaceFrom:1 to:0 with:array2 startingAt:1. |
|
783 |
||
784 |
self should: [ array1 = #(1 2 3 4) ]. |
|
785 |
||
786 |
" |
|
787 |
self new testEmptyReplace |
|
788 |
" |
|
538 | 789 |
! |
790 |
||
791 |
testTransliterating |
|
792 |
self assert:( 'abcdefghijkl1234567890' copyTransliterating:'b-g' to:'B-G' ) = 'aBCDEFGhijkl1234567890'. |
|
793 |
self assert:( 'abcdefghij-kl1234567890' copyTransliterating:'b\-g' to:'B+G' ) = 'aBcdefGhij+kl1234567890'. |
|
794 |
self assert:( 'abcdefghijkl1234567890' copyTransliterating:'69' to:'96' ) = 'abcdefghijkl1234597860' . |
|
795 |
self assert:( 'abcdefghijkl1234567890' copyTransliterating:'a' to:'b' ) = 'bbcdefghijkl1234567890' . |
|
796 |
self assert:( 'abcdefghijkl1234567890' copyTransliterating:'aeiou' to:'AEIOU' ) = 'AbcdEfghIjkl1234567890'. |
|
797 |
self assert:( 'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'QERTYUIOPX' ) = 'abcdefghijklERTYUIOPXQ' . |
|
798 |
||
799 |
self assert:( 'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'A' complement:false squashDuplicates:false ) |
|
800 |
= 'abcdefghijklAAAAAAAAAA'. |
|
801 |
||
802 |
self assert:( 'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'A' complement:false squashDuplicates:true ) |
|
803 |
= 'abcdefghijklA'. |
|
804 |
||
805 |
self assert:( 'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'*' complement:false squashDuplicates:false ) |
|
806 |
= 'abcdefghijkl**********'. |
|
807 |
||
808 |
self assert:( 'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'*' complement:true squashDuplicates:false ) |
|
809 |
= '************1234567890'. |
|
810 |
||
811 |
self assert:( 'abcdefghijkl1234567890' copyTransliterating:'a-zA-Z' to:' ' complement:true squashDuplicates:false ) |
|
812 |
= 'abcdefghijkl '. |
|
813 |
||
814 |
self assert:( 'abcdefghijkl1234567890' copyTransliterating:'a-zA-Z' to:' ' complement:false squashDuplicates:false ) |
|
815 |
= ' 1234567890'. |
|
816 |
||
817 |
self assert:( 'abcdefghijkl1234567890abcdefghijkl' copyTransliterating:'a-zA-Z' to:'' complement:false squashDuplicates:false ) |
|
818 |
= '1234567890'. |
|
819 |
||
820 |
self assert:( 'abcdefghijkl1234567890abcdefghijkl' copyTransliterating:'a-zA-Z' to:'' complement:true squashDuplicates:false ) |
|
821 |
= 'abcdefghijklabcdefghijkl'. |
|
297 | 822 |
! ! |
823 |
||
639 | 824 |
!CollectionTests methodsFor:'tests-searching'! |
825 |
||
826 |
test_min_max |
|
827 |
|a| |
|
828 |
||
829 |
#( |
|
830 |
asArray |
|
831 |
asFloatArray |
|
832 |
asDoubleArray |
|
833 |
asSignedIntegerArray |
|
834 |
) do:[:converter | |
|
640 | 835 |
|empty| |
836 |
||
837 |
empty := #() perform:converter. |
|
838 |
self should:[ empty min ] raise:Error. |
|
839 |
self should:[ empty max ] raise:Error. |
|
840 |
self should:[ empty minMax ] raise:Error. |
|
841 |
||
639 | 842 |
#( |
640 | 843 |
(0 0) |
844 |
(1 1) |
|
845 |
(-1 -1) |
|
846 |
(0 1) |
|
847 |
(1 2) |
|
848 |
(-1 -0) |
|
849 |
(-1 1) |
|
639 | 850 |
(1 10000) |
851 |
(1 10001) |
|
852 |
(2 10000) |
|
853 |
(2 10001) |
|
854 |
(-1000 1000) |
|
855 |
(-1000 999) |
|
856 |
) pairsDo:[:min :max | |
|
857 |
a := (min to:max) perform:converter. |
|
858 |
self assert:(a min) = min. |
|
859 |
self assert:(a max) = max. |
|
860 |
self assert:(a minMax) = { min. max }. |
|
861 |
||
862 |
a := a reverse. |
|
863 |
self assert:(a min) = min. |
|
864 |
self assert:(a max) = max. |
|
865 |
self assert:(a minMax) = { min. max }. |
|
866 |
||
867 |
a := a randomShuffle. |
|
868 |
self assert:(a min) = min. |
|
869 |
self assert:(a max) = max. |
|
870 |
self assert:(a minMax) = { min. max }. |
|
871 |
]. |
|
872 |
]. |
|
873 |
||
874 |
"Created: / 07-10-2011 / 13:11:36 / cg" |
|
875 |
! ! |
|
876 |
||
264 | 877 |
!CollectionTests methodsFor:'tests-sorting'! |
212 | 878 |
|
879 |
testSort01 |
|
880 |
| col errs last | |
|
881 |
||
882 |
col := SortedCollection new addAll: (100 to: 1 by: -1); yourself. |
|
883 |
last := nil. |
|
884 |
errs := col inject: 0 into: |
|
885 |
[ :totIn :n | |totOut| |
|
886 |
||
887 |
totOut := totIn. |
|
888 |
last notNil ifTrue: [ last > n ifTrue: [ totOut := totOut + 1 ] ]. |
|
889 |
last := n. |
|
890 |
totOut ]. |
|
891 |
self should: [ errs = 0 ] |
|
892 |
||
893 |
" |
|
894 |
self basicNew testSort01 |
|
895 |
" |
|
896 |
! |
|
897 |
||
898 |
testSortStability01 |
|
899 |
"Verify that the sort is stable" |
|
900 |
||
901 |
| testData col lastTestData errs | |
|
902 |
||
903 |
testData := OrderedCollection new. |
|
656
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
904 |
testData addAll: ((111 to: 1 by: -1) collect: [ :n | SortTestData new value: n ]). |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
905 |
testData addAll: ((111 to: 1 by: -1) collect: [ :n | SortTestData new value: n ]). |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
906 |
testData addAll: ((111 to: 1 by: -1) collect: [ :n | SortTestData new value: n ]). |
212 | 907 |
1 to: testData size do: [ :i | (testData at: i) order: i ]. |
908 |
||
656
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
909 |
col := SortedCollection new sortBlock:[ :v1 :v2 | v1 value < v2 value ]. |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
910 |
col addAll: testData. |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
911 |
|
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
912 |
self checkSorted:col with:[:v1 :v2 | v1 value <= v2 value ] against:testData. |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
913 |
|
212 | 914 |
lastTestData := nil. |
915 |
||
916 |
errs := col inject: 0 into: |
|
917 |
[ :totIn :aSortTestData | |totOut| |
|
918 |
totOut := totIn. |
|
919 |
lastTestData notNil ifTrue: |
|
920 |
[ lastTestData value = aSortTestData value ifTrue: |
|
921 |
[ lastTestData order > aSortTestData order ifTrue: [ totOut := totOut + 1 ] ] ]. |
|
922 |
lastTestData := aSortTestData. |
|
923 |
totOut ]. |
|
924 |
self should: [ errs = 0 ] |
|
925 |
||
926 |
" |
|
927 |
self basicNew testSortStability01 |
|
928 |
" |
|
929 |
! |
|
930 |
||
931 |
testSortStability02 |
|
656
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
932 |
"Verify that the sort is stable" |
212 | 933 |
|
656
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
934 |
| testData col lastTestData errs | |
212 | 935 |
|
656
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
936 |
testData := OrderedCollection new. |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
937 |
(25 to: 1 by: -1) do: [ :i | 1 to: 100 do: [ :j | testData add: (SortTestData new value: i ) ] ]. |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
938 |
1 to: testData size do: [ :i | (testData at: i) order: i ]. |
212 | 939 |
|
656
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
940 |
col := SortedCollection new sortBlock: [ :v1 :v2 | v1 value < v2 value ]. |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
941 |
col addAll: testData. |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
942 |
|
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
943 |
self checkSorted:col with:[:v1 :v2 | v1 value <= v2 value ] against:testData. |
212 | 944 |
|
656
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
945 |
lastTestData := nil. |
212 | 946 |
|
656
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
947 |
errs := col inject: 0 into: |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
948 |
[ :totIn :aSortTestData | |totOut| |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
949 |
totOut := totIn. |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
950 |
lastTestData notNil ifTrue: |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
951 |
[ lastTestData value = aSortTestData value ifTrue: |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
952 |
[ lastTestData order > aSortTestData order ifTrue: [ totOut := totOut + 1 ] ] ]. |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
953 |
lastTestData := aSortTestData. |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
954 |
totOut ]. |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
955 |
self should: [ errs = 0 ] |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
956 |
|
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
957 |
" |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
958 |
self basicNew testSortStability02 |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
959 |
" |
212 | 960 |
! ! |
961 |
||
293 | 962 |
!CollectionTests methodsFor:'tests-squeak'! |
963 |
||
964 |
testIfEmptyifNotEmpty |
|
965 |
self assert: (empty ifEmpty: [true] ifNotEmpty: [false]). |
|
966 |
self assert: (nonEmpty ifEmpty: [false] ifNotEmpty: [true]). |
|
967 |
self assert: (nonEmpty ifEmpty: [false] ifNotEmpty: [:s | s first = #x]) |
|
968 |
! |
|
969 |
||
970 |
testIfEmptyifNotEmptyDo |
|
971 |
self assert: (empty ifEmpty: [true] ifNotEmptyDo: [:s | false]). |
|
972 |
self assert: (nonEmpty ifEmpty: [false] ifNotEmptyDo: [:s | s first = #x]) |
|
973 |
! |
|
974 |
||
975 |
testIfNotEmpty |
|
976 |
empty ifNotEmpty: [self assert: false]. |
|
977 |
self assert: (nonEmpty ifNotEmpty: [self]) == self. |
|
978 |
self assert: (nonEmpty ifNotEmpty: [:s | s first]) = #x |
|
979 |
! |
|
980 |
||
981 |
testIfNotEmptyDo |
|
982 |
empty ifNotEmptyDo: [:s | self assert: false]. |
|
983 |
self assert: (nonEmpty ifNotEmptyDo: [:s | s first]) = #x |
|
984 |
! |
|
985 |
||
986 |
testIfNotEmptyDoifNotEmpty |
|
987 |
self assert: (empty ifNotEmptyDo: [:s | false] ifEmpty: [true]). |
|
988 |
self assert: (nonEmpty ifNotEmptyDo: [:s | s first = #x] ifEmpty: [false]) |
|
989 |
! |
|
990 |
||
991 |
testIfNotEmptyifEmpty |
|
992 |
self assert: (empty ifEmpty: [true] ifNotEmpty: [false]). |
|
993 |
self assert: (nonEmpty ifEmpty: [false] ifNotEmpty: [true]). |
|
994 |
self assert: (nonEmpty ifEmpty: [false] ifNotEmpty: [:s | s first = #x]) |
|
995 |
! ! |
|
996 |
||
212 | 997 |
!CollectionTests::SortTestData methodsFor:'accessing'! |
998 |
||
999 |
value |
|
1000 |
"Answer the value of the receiver's ''value'' instance variable." |
|
1001 |
||
1002 |
^value |
|
1003 |
! |
|
1004 |
||
1005 |
value: anObject |
|
1006 |
"Set the value of the receiver's ''value'' instance variable to the argument, anObject." |
|
1007 |
||
1008 |
value := anObject |
|
1009 |
! ! |
|
1010 |
||
1011 |
!CollectionTests::SortTestData methodsFor:'order'! |
|
1012 |
||
1013 |
order |
|
1014 |
"Answer the value of the receiver's ''order'' instance variable." |
|
1015 |
||
1016 |
^order |
|
1017 |
! |
|
1018 |
||
1019 |
order: anObject |
|
1020 |
"Set the value of the receiver's ''order'' instance variable to the argument, anObject." |
|
1021 |
||
1022 |
order := anObject |
|
1023 |
! ! |
|
1024 |
||
1025 |
!CollectionTests::SortTestData methodsFor:'printing & storing'! |
|
1026 |
||
1027 |
printOn: aStream |
|
1028 |
super printOn: aStream. |
|
1029 |
aStream |
|
1030 |
nextPutAll: '(value: '; |
|
1031 |
nextPutAll: value printString; |
|
1032 |
nextPutAll: ' order: '; |
|
1033 |
nextPutAll: order printString; |
|
1034 |
nextPut: $) |
|
115 | 1035 |
! ! |
1036 |
||
1037 |
!CollectionTests class methodsFor:'documentation'! |
|
1038 |
||
1039 |
version |
|
1040 |
^ '$Header$' |
|
640 | 1041 |
! |
1042 |
||
1043 |
version_CVS |
|
1044 |
^ '$Header$' |
|
115 | 1045 |
! ! |