author | Stefan Vogel <sv@exept.de> |
Sat, 14 Apr 2012 20:01:29 +0200 | |
changeset 657 | 4f04a8bf19c5 |
parent 656 | 470e994fc84b |
child 663 | 437a6c228d34 |
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 |
]. |
657
4f04a8bf19c5
added: #testOrderedCollection07_HeapSortRandomCollection
Stefan Vogel <sv@exept.de>
parents:
656
diff
changeset
|
549 |
! |
4f04a8bf19c5
added: #testOrderedCollection07_HeapSortRandomCollection
Stefan Vogel <sv@exept.de>
parents:
656
diff
changeset
|
550 |
|
4f04a8bf19c5
added: #testOrderedCollection07_HeapSortRandomCollection
Stefan Vogel <sv@exept.de>
parents:
656
diff
changeset
|
551 |
testOrderedCollection07_HeapSortRandomCollection |
4f04a8bf19c5
added: #testOrderedCollection07_HeapSortRandomCollection
Stefan Vogel <sv@exept.de>
parents:
656
diff
changeset
|
552 |
|data sorted rg checkBlock| |
4f04a8bf19c5
added: #testOrderedCollection07_HeapSortRandomCollection
Stefan Vogel <sv@exept.de>
parents:
656
diff
changeset
|
553 |
|
4f04a8bf19c5
added: #testOrderedCollection07_HeapSortRandomCollection
Stefan Vogel <sv@exept.de>
parents:
656
diff
changeset
|
554 |
checkBlock := [:a :b| a <= b]. |
4f04a8bf19c5
added: #testOrderedCollection07_HeapSortRandomCollection
Stefan Vogel <sv@exept.de>
parents:
656
diff
changeset
|
555 |
rg := Random new. |
4f04a8bf19c5
added: #testOrderedCollection07_HeapSortRandomCollection
Stefan Vogel <sv@exept.de>
parents:
656
diff
changeset
|
556 |
|
4f04a8bf19c5
added: #testOrderedCollection07_HeapSortRandomCollection
Stefan Vogel <sv@exept.de>
parents:
656
diff
changeset
|
557 |
#(1 4 11 2000 20011 200000) do:[:eachSize| |
4f04a8bf19c5
added: #testOrderedCollection07_HeapSortRandomCollection
Stefan Vogel <sv@exept.de>
parents:
656
diff
changeset
|
558 |
data := Array new:eachSize. |
4f04a8bf19c5
added: #testOrderedCollection07_HeapSortRandomCollection
Stefan Vogel <sv@exept.de>
parents:
656
diff
changeset
|
559 |
1 to:data size do:[:i | |
4f04a8bf19c5
added: #testOrderedCollection07_HeapSortRandomCollection
Stefan Vogel <sv@exept.de>
parents:
656
diff
changeset
|
560 |
data at:i put:(rg nextIntegerBetween:1 and:100). |
4f04a8bf19c5
added: #testOrderedCollection07_HeapSortRandomCollection
Stefan Vogel <sv@exept.de>
parents:
656
diff
changeset
|
561 |
]. |
4f04a8bf19c5
added: #testOrderedCollection07_HeapSortRandomCollection
Stefan Vogel <sv@exept.de>
parents:
656
diff
changeset
|
562 |
sorted := data copy heapSort. |
4f04a8bf19c5
added: #testOrderedCollection07_HeapSortRandomCollection
Stefan Vogel <sv@exept.de>
parents:
656
diff
changeset
|
563 |
self checkSorted:sorted with:checkBlock against:data. |
4f04a8bf19c5
added: #testOrderedCollection07_HeapSortRandomCollection
Stefan Vogel <sv@exept.de>
parents:
656
diff
changeset
|
564 |
sorted := sorted heapSort. |
4f04a8bf19c5
added: #testOrderedCollection07_HeapSortRandomCollection
Stefan Vogel <sv@exept.de>
parents:
656
diff
changeset
|
565 |
self checkSorted:sorted with:checkBlock against:data. |
4f04a8bf19c5
added: #testOrderedCollection07_HeapSortRandomCollection
Stefan Vogel <sv@exept.de>
parents:
656
diff
changeset
|
566 |
sorted := sorted reverse heapSort. |
4f04a8bf19c5
added: #testOrderedCollection07_HeapSortRandomCollection
Stefan Vogel <sv@exept.de>
parents:
656
diff
changeset
|
567 |
self checkSorted:sorted with:checkBlock against:data. |
4f04a8bf19c5
added: #testOrderedCollection07_HeapSortRandomCollection
Stefan Vogel <sv@exept.de>
parents:
656
diff
changeset
|
568 |
|
4f04a8bf19c5
added: #testOrderedCollection07_HeapSortRandomCollection
Stefan Vogel <sv@exept.de>
parents:
656
diff
changeset
|
569 |
sorted := data copy heapSort:[:a :b| a < b]. |
4f04a8bf19c5
added: #testOrderedCollection07_HeapSortRandomCollection
Stefan Vogel <sv@exept.de>
parents:
656
diff
changeset
|
570 |
self checkSorted:sorted with:checkBlock against:data. |
4f04a8bf19c5
added: #testOrderedCollection07_HeapSortRandomCollection
Stefan Vogel <sv@exept.de>
parents:
656
diff
changeset
|
571 |
]. |
301 | 572 |
! ! |
573 |
||
574 |
!CollectionTests methodsFor:'tests-orderedDictionary'! |
|
575 |
||
576 |
testOrderedDictionary01 |
|
577 |
|d ret| |
|
578 |
||
579 |
d := OrderedDictionary new. |
|
580 |
||
581 |
self assert: ( d size == 0 ). |
|
582 |
self assert: ( d isEmpty ). |
|
583 |
self assert: ( d notEmpty not ). |
|
584 |
||
585 |
d at:'one' put:1. |
|
586 |
d at:'two' put:2. |
|
587 |
d at:'three' put:3. |
|
588 |
||
589 |
self assert: ( d size == 3 ). |
|
590 |
self assert: ( d isEmpty not). |
|
591 |
self assert: ( d notEmpty ). |
|
592 |
||
585 | 593 |
self assert:((ret := self collectedDoArgsOf:d) sameContentsAs: #(1 2 3)). |
301 | 594 |
|
595 |
self assert: ( (d atIndex:1) == 1 ). |
|
596 |
self assert: ( (d atIndex:2) == 2 ). |
|
597 |
self assert: ( (d atIndex:3) == 3 ). |
|
598 |
||
599 |
self assert: ( (d first) = ('one'->1) ). |
|
600 |
self assert: ( (d last) = ('three'->3) ). |
|
601 |
||
585 | 602 |
self assert:((ret := d collect:[:each | each]) sameContentsAs: #(1 2 3)). |
603 |
self assert:((ret := d select:[:each | true]) sameContentsAs: (OrderedDictionary withAssociations:(Array with:('one'->1) with:('two'->2) with:('three'->3)))). |
|
301 | 604 |
|
605 |
self assert: ( d includes:1 ). |
|
606 |
self assert: ( d includes:2 ). |
|
607 |
self assert: ( d includes:3 ). |
|
608 |
||
609 |
self assert: ( d includesKey:'one' ). |
|
610 |
self assert: ( d includesKey:'two' ). |
|
611 |
self assert: ( d includesKey:'three' ). |
|
612 |
||
613 |
self assert: ((ret := d removeFirst) = 1). |
|
614 |
self assert: ( d size == 2 ). |
|
615 |
self assert: ((ret := d removeLast) = 3). |
|
616 |
self assert: ( d size == 1 ). |
|
617 |
self assert: ((ret := d removeLast) = 2). |
|
618 |
self assert: ( d size == 0 ). |
|
619 |
self assert: ( d isEmpty ). |
|
620 |
self assert: ( d notEmpty not ). |
|
621 |
||
622 |
" |
|
623 |
self basicNew testOrderedDictionary01 |
|
624 |
" |
|
585 | 625 |
|
626 |
"Modified: / 04-07-2011 / 19:32:34 / cg" |
|
301 | 627 |
! ! |
628 |
||
629 |
!CollectionTests methodsFor:'tests-orderedSet'! |
|
162 | 630 |
|
156 | 631 |
testOrderedSet01 |
632 |
|s v1 v2 v3| |
|
633 |
||
634 |
s := OrderedSet new. |
|
635 |
s add:(v1 := 'one'). |
|
636 |
s add:(v2 := 'two'). |
|
637 |
s add:(v3 := 'three'). |
|
638 |
||
639 |
self assert: ( s size == 3 ). |
|
640 |
self assert: ( (s at:1) == v1 ). |
|
641 |
self assert: ( (s at:2) == v2 ). |
|
642 |
self assert: ( (s at:3) == v3 ). |
|
643 |
||
644 |
self assert: ( s includes:v1 ). |
|
645 |
self assert: ( s includes:v2 ). |
|
646 |
self assert: ( s includes:v3 ). |
|
647 |
||
648 |
self assert: ( s includes:v1 copy). |
|
649 |
self assert: ( s includes:v2 copy). |
|
650 |
self assert: ( s includes:v3 copy). |
|
651 |
||
652 |
"already in - adding again should not change things" |
|
653 |
s add:v1. |
|
654 |
self assert: ( s size == 3 ). |
|
655 |
||
656 |
self assert: ( (s at:1) == v1 ). |
|
657 |
self assert: ( (s at:2) == v2 ). |
|
658 |
self assert: ( (s at:3) == v3 ). |
|
659 |
||
660 |
"set i.e. not identitySet - adding copy should not change things" |
|
661 |
s add:v1 copy. |
|
662 |
self assert: ( s size == 3 ). |
|
663 |
||
664 |
self assert: ( (s at:1) == v1 ). |
|
665 |
self assert: ( (s at:2) == v2 ). |
|
666 |
self assert: ( (s at:3) == v3 ). |
|
667 |
||
668 |
s remove:v1. |
|
669 |
||
670 |
self assert: ( s size == 2 ). |
|
671 |
self assert: ( (s at:1) == v2 ). |
|
672 |
self assert: ( (s at:2) == v3 ). |
|
673 |
||
674 |
self assert: ( s includes:v1 ) not. |
|
675 |
self assert: ( s includes:v2 ). |
|
676 |
self assert: ( s includes:v3 ). |
|
677 |
||
678 |
self assert: ( s includes:v1 copy) not. |
|
679 |
self assert: ( s includes:v2 copy). |
|
680 |
self assert: ( s includes:v3 copy). |
|
681 |
||
682 |
s add:v1. |
|
683 |
||
684 |
self assert: ( s size == 3 ). |
|
685 |
self assert: ( (s at:1) == v2 ). |
|
686 |
self assert: ( (s at:2) == v3 ). |
|
687 |
self assert: ( (s at:3) == v1 ). |
|
688 |
||
689 |
self assert: ( s includes:v1 ). |
|
690 |
self assert: ( s includes:v2 ). |
|
691 |
self assert: ( s includes:v3 ). |
|
692 |
||
693 |
self assert: ( s includes:v1 copy). |
|
694 |
self assert: ( s includes:v2 copy). |
|
695 |
self assert: ( s includes:v3 copy). |
|
696 |
||
697 |
s removeLast. |
|
698 |
||
699 |
self assert: ( s size == 2 ). |
|
700 |
self assert: ( (s at:1) == v2 ). |
|
701 |
self assert: ( (s at:2) == v3 ). |
|
702 |
||
703 |
self assert: ( s includes:v1 ) not. |
|
704 |
self assert: ( s includes:v2 ). |
|
705 |
self assert: ( s includes:v3 ). |
|
706 |
||
707 |
self assert: ( s includes:v1 copy) not. |
|
708 |
self assert: ( s includes:v2 copy). |
|
709 |
self assert: ( s includes:v3 copy). |
|
710 |
||
711 |
" |
|
712 |
self basicNew testOrderedSet01 |
|
713 |
" |
|
301 | 714 |
! |
715 |
||
716 |
testOrderedSet02 |
|
717 |
|col| |
|
718 |
||
719 |
col := OrderedSet new. |
|
720 |
||
721 |
col add:'a'. |
|
722 |
col add:'b'. |
|
723 |
col add:'c'. |
|
724 |
self assert:( col size == 3 ). |
|
725 |
self assert:( Array streamContents:[:s | |
|
726 |
col do:[:each | s nextPut:each] |
|
727 |
] |
|
728 |
) = #('a' 'b' 'c' ). |
|
729 |
||
730 |
" |
|
731 |
self new testOrderedSet02 |
|
732 |
" |
|
733 |
! |
|
734 |
||
735 |
testOrderedSet03 |
|
736 |
|col| |
|
737 |
||
738 |
col := OrderedSet new. |
|
739 |
||
740 |
col add:'a'. |
|
741 |
col add:'b'. |
|
742 |
col add:'c'. |
|
743 |
col add:'a'. |
|
744 |
col add:'b'. |
|
745 |
self assert:( col size == 3 ). |
|
746 |
self assert:( Array streamContents:[:s | |
|
747 |
col do:[:each | s nextPut:each] |
|
748 |
] |
|
749 |
) = #('a' 'b' 'c' ). |
|
750 |
||
751 |
" |
|
752 |
self new testOrderedSet03 |
|
753 |
" |
|
754 |
! |
|
755 |
||
756 |
testOrderedSet04 |
|
757 |
|col| |
|
758 |
||
759 |
col := OrderedSet new. |
|
760 |
||
761 |
col add:'a'. |
|
762 |
col addFirst:'b'. |
|
763 |
col addFirst:'c'. |
|
764 |
self assert:( col size == 3 ). |
|
765 |
self assert:( Array streamContents:[:s | |
|
766 |
col do:[:each | s nextPut:each] |
|
767 |
] |
|
768 |
) = #('c' 'b' 'a' ). |
|
769 |
||
770 |
" |
|
771 |
self new testOrderedSet04 |
|
772 |
" |
|
773 |
! |
|
774 |
||
775 |
testOrderedSet05 |
|
776 |
|col| |
|
777 |
||
778 |
col := OrderedSet new. |
|
779 |
||
780 |
col add:'a'. |
|
781 |
col addFirst:'b'. |
|
782 |
col addFirst:'c'. |
|
783 |
col add:'a'. |
|
784 |
col add:'b'. |
|
785 |
col add:'c'. |
|
786 |
self assert:( col size == 3 ). |
|
787 |
self assert:( Array streamContents:[:s | |
|
788 |
col do:[:each | s nextPut:each] |
|
789 |
] |
|
790 |
) = #('c' 'b' 'a' ). |
|
791 |
||
792 |
" |
|
793 |
self new testOrderedSet05 |
|
794 |
" |
|
264 | 795 |
! ! |
796 |
||
297 | 797 |
!CollectionTests methodsFor:'tests-replace'! |
798 |
||
799 |
testEmptyReplace |
|
800 |
| array1 array2 | |
|
801 |
||
802 |
array1 := Array with: 1 with: 2 with: 3 with: 4. |
|
803 |
array2 := Array with: 5 with: 6 with: 7. |
|
804 |
||
805 |
array1 replaceFrom:1 to:0 with:array2 startingAt:1. |
|
806 |
||
807 |
self should: [ array1 = #(1 2 3 4) ]. |
|
808 |
||
809 |
" |
|
810 |
self new testEmptyReplace |
|
811 |
" |
|
538 | 812 |
! |
813 |
||
814 |
testTransliterating |
|
815 |
self assert:( 'abcdefghijkl1234567890' copyTransliterating:'b-g' to:'B-G' ) = 'aBCDEFGhijkl1234567890'. |
|
816 |
self assert:( 'abcdefghij-kl1234567890' copyTransliterating:'b\-g' to:'B+G' ) = 'aBcdefGhij+kl1234567890'. |
|
817 |
self assert:( 'abcdefghijkl1234567890' copyTransliterating:'69' to:'96' ) = 'abcdefghijkl1234597860' . |
|
818 |
self assert:( 'abcdefghijkl1234567890' copyTransliterating:'a' to:'b' ) = 'bbcdefghijkl1234567890' . |
|
819 |
self assert:( 'abcdefghijkl1234567890' copyTransliterating:'aeiou' to:'AEIOU' ) = 'AbcdEfghIjkl1234567890'. |
|
820 |
self assert:( 'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'QERTYUIOPX' ) = 'abcdefghijklERTYUIOPXQ' . |
|
821 |
||
822 |
self assert:( 'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'A' complement:false squashDuplicates:false ) |
|
823 |
= 'abcdefghijklAAAAAAAAAA'. |
|
824 |
||
825 |
self assert:( 'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'A' complement:false squashDuplicates:true ) |
|
826 |
= 'abcdefghijklA'. |
|
827 |
||
828 |
self assert:( 'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'*' complement:false squashDuplicates:false ) |
|
829 |
= 'abcdefghijkl**********'. |
|
830 |
||
831 |
self assert:( 'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'*' complement:true squashDuplicates:false ) |
|
832 |
= '************1234567890'. |
|
833 |
||
834 |
self assert:( 'abcdefghijkl1234567890' copyTransliterating:'a-zA-Z' to:' ' complement:true squashDuplicates:false ) |
|
835 |
= 'abcdefghijkl '. |
|
836 |
||
837 |
self assert:( 'abcdefghijkl1234567890' copyTransliterating:'a-zA-Z' to:' ' complement:false squashDuplicates:false ) |
|
838 |
= ' 1234567890'. |
|
839 |
||
840 |
self assert:( 'abcdefghijkl1234567890abcdefghijkl' copyTransliterating:'a-zA-Z' to:'' complement:false squashDuplicates:false ) |
|
841 |
= '1234567890'. |
|
842 |
||
843 |
self assert:( 'abcdefghijkl1234567890abcdefghijkl' copyTransliterating:'a-zA-Z' to:'' complement:true squashDuplicates:false ) |
|
844 |
= 'abcdefghijklabcdefghijkl'. |
|
297 | 845 |
! ! |
846 |
||
639 | 847 |
!CollectionTests methodsFor:'tests-searching'! |
848 |
||
849 |
test_min_max |
|
850 |
|a| |
|
851 |
||
852 |
#( |
|
853 |
asArray |
|
854 |
asFloatArray |
|
855 |
asDoubleArray |
|
856 |
asSignedIntegerArray |
|
857 |
) do:[:converter | |
|
640 | 858 |
|empty| |
859 |
||
860 |
empty := #() perform:converter. |
|
861 |
self should:[ empty min ] raise:Error. |
|
862 |
self should:[ empty max ] raise:Error. |
|
863 |
self should:[ empty minMax ] raise:Error. |
|
864 |
||
639 | 865 |
#( |
640 | 866 |
(0 0) |
867 |
(1 1) |
|
868 |
(-1 -1) |
|
869 |
(0 1) |
|
870 |
(1 2) |
|
871 |
(-1 -0) |
|
872 |
(-1 1) |
|
639 | 873 |
(1 10000) |
874 |
(1 10001) |
|
875 |
(2 10000) |
|
876 |
(2 10001) |
|
877 |
(-1000 1000) |
|
878 |
(-1000 999) |
|
879 |
) pairsDo:[:min :max | |
|
880 |
a := (min to:max) perform:converter. |
|
881 |
self assert:(a min) = min. |
|
882 |
self assert:(a max) = max. |
|
883 |
self assert:(a minMax) = { min. max }. |
|
884 |
||
885 |
a := a reverse. |
|
886 |
self assert:(a min) = min. |
|
887 |
self assert:(a max) = max. |
|
888 |
self assert:(a minMax) = { min. max }. |
|
889 |
||
890 |
a := a randomShuffle. |
|
891 |
self assert:(a min) = min. |
|
892 |
self assert:(a max) = max. |
|
893 |
self assert:(a minMax) = { min. max }. |
|
894 |
]. |
|
895 |
]. |
|
896 |
||
897 |
"Created: / 07-10-2011 / 13:11:36 / cg" |
|
898 |
! ! |
|
899 |
||
264 | 900 |
!CollectionTests methodsFor:'tests-sorting'! |
212 | 901 |
|
902 |
testSort01 |
|
903 |
| col errs last | |
|
904 |
||
905 |
col := SortedCollection new addAll: (100 to: 1 by: -1); yourself. |
|
906 |
last := nil. |
|
907 |
errs := col inject: 0 into: |
|
908 |
[ :totIn :n | |totOut| |
|
909 |
||
910 |
totOut := totIn. |
|
911 |
last notNil ifTrue: [ last > n ifTrue: [ totOut := totOut + 1 ] ]. |
|
912 |
last := n. |
|
913 |
totOut ]. |
|
914 |
self should: [ errs = 0 ] |
|
915 |
||
916 |
" |
|
917 |
self basicNew testSort01 |
|
918 |
" |
|
919 |
! |
|
920 |
||
921 |
testSortStability01 |
|
922 |
"Verify that the sort is stable" |
|
923 |
||
924 |
| testData col lastTestData errs | |
|
925 |
||
926 |
testData := OrderedCollection new. |
|
656
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
927 |
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
|
928 |
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
|
929 |
testData addAll: ((111 to: 1 by: -1) collect: [ :n | SortTestData new value: n ]). |
212 | 930 |
1 to: testData size do: [ :i | (testData at: i) order: i ]. |
931 |
||
656
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
932 |
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
|
933 |
col addAll: testData. |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
934 |
|
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
935 |
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
|
936 |
|
212 | 937 |
lastTestData := nil. |
938 |
||
939 |
errs := col inject: 0 into: |
|
940 |
[ :totIn :aSortTestData | |totOut| |
|
941 |
totOut := totIn. |
|
942 |
lastTestData notNil ifTrue: |
|
943 |
[ lastTestData value = aSortTestData value ifTrue: |
|
944 |
[ lastTestData order > aSortTestData order ifTrue: [ totOut := totOut + 1 ] ] ]. |
|
945 |
lastTestData := aSortTestData. |
|
946 |
totOut ]. |
|
947 |
self should: [ errs = 0 ] |
|
948 |
||
949 |
" |
|
950 |
self basicNew testSortStability01 |
|
951 |
" |
|
952 |
! |
|
953 |
||
954 |
testSortStability02 |
|
656
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
955 |
"Verify that the sort is stable" |
212 | 956 |
|
656
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
957 |
| testData col lastTestData errs | |
212 | 958 |
|
656
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
959 |
testData := OrderedCollection new. |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
960 |
(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
|
961 |
1 to: testData size do: [ :i | (testData at: i) order: i ]. |
212 | 962 |
|
656
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
963 |
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
|
964 |
col addAll: testData. |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
965 |
|
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
966 |
self checkSorted:col with:[:v1 :v2 | v1 value <= v2 value ] against:testData. |
212 | 967 |
|
656
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
968 |
lastTestData := nil. |
212 | 969 |
|
656
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
970 |
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
|
971 |
[ :totIn :aSortTestData | |totOut| |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
972 |
totOut := totIn. |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
973 |
lastTestData notNil ifTrue: |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
974 |
[ 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
|
975 |
[ 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
|
976 |
lastTestData := aSortTestData. |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
977 |
totOut ]. |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
978 |
self should: [ errs = 0 ] |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
979 |
|
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
980 |
" |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
981 |
self basicNew testSortStability02 |
470e994fc84b
Fix stability tests (they didn't work after being fixed).
Stefan Vogel <sv@exept.de>
parents:
655
diff
changeset
|
982 |
" |
212 | 983 |
! ! |
984 |
||
293 | 985 |
!CollectionTests methodsFor:'tests-squeak'! |
986 |
||
987 |
testIfEmptyifNotEmpty |
|
988 |
self assert: (empty ifEmpty: [true] ifNotEmpty: [false]). |
|
989 |
self assert: (nonEmpty ifEmpty: [false] ifNotEmpty: [true]). |
|
990 |
self assert: (nonEmpty ifEmpty: [false] ifNotEmpty: [:s | s first = #x]) |
|
991 |
! |
|
992 |
||
993 |
testIfEmptyifNotEmptyDo |
|
994 |
self assert: (empty ifEmpty: [true] ifNotEmptyDo: [:s | false]). |
|
995 |
self assert: (nonEmpty ifEmpty: [false] ifNotEmptyDo: [:s | s first = #x]) |
|
996 |
! |
|
997 |
||
998 |
testIfNotEmpty |
|
999 |
empty ifNotEmpty: [self assert: false]. |
|
1000 |
self assert: (nonEmpty ifNotEmpty: [self]) == self. |
|
1001 |
self assert: (nonEmpty ifNotEmpty: [:s | s first]) = #x |
|
1002 |
! |
|
1003 |
||
1004 |
testIfNotEmptyDo |
|
1005 |
empty ifNotEmptyDo: [:s | self assert: false]. |
|
1006 |
self assert: (nonEmpty ifNotEmptyDo: [:s | s first]) = #x |
|
1007 |
! |
|
1008 |
||
1009 |
testIfNotEmptyDoifNotEmpty |
|
1010 |
self assert: (empty ifNotEmptyDo: [:s | false] ifEmpty: [true]). |
|
1011 |
self assert: (nonEmpty ifNotEmptyDo: [:s | s first = #x] ifEmpty: [false]) |
|
1012 |
! |
|
1013 |
||
1014 |
testIfNotEmptyifEmpty |
|
1015 |
self assert: (empty ifEmpty: [true] ifNotEmpty: [false]). |
|
1016 |
self assert: (nonEmpty ifEmpty: [false] ifNotEmpty: [true]). |
|
1017 |
self assert: (nonEmpty ifEmpty: [false] ifNotEmpty: [:s | s first = #x]) |
|
1018 |
! ! |
|
1019 |
||
212 | 1020 |
!CollectionTests::SortTestData methodsFor:'accessing'! |
1021 |
||
1022 |
value |
|
1023 |
"Answer the value of the receiver's ''value'' instance variable." |
|
1024 |
||
1025 |
^value |
|
1026 |
! |
|
1027 |
||
1028 |
value: anObject |
|
1029 |
"Set the value of the receiver's ''value'' instance variable to the argument, anObject." |
|
1030 |
||
1031 |
value := anObject |
|
1032 |
! ! |
|
1033 |
||
1034 |
!CollectionTests::SortTestData methodsFor:'order'! |
|
1035 |
||
1036 |
order |
|
1037 |
"Answer the value of the receiver's ''order'' instance variable." |
|
1038 |
||
1039 |
^order |
|
1040 |
! |
|
1041 |
||
1042 |
order: anObject |
|
1043 |
"Set the value of the receiver's ''order'' instance variable to the argument, anObject." |
|
1044 |
||
1045 |
order := anObject |
|
1046 |
! ! |
|
1047 |
||
1048 |
!CollectionTests::SortTestData methodsFor:'printing & storing'! |
|
1049 |
||
1050 |
printOn: aStream |
|
1051 |
super printOn: aStream. |
|
1052 |
aStream |
|
1053 |
nextPutAll: '(value: '; |
|
1054 |
nextPutAll: value printString; |
|
1055 |
nextPutAll: ' order: '; |
|
1056 |
nextPutAll: order printString; |
|
1057 |
nextPut: $) |
|
115 | 1058 |
! ! |
1059 |
||
1060 |
!CollectionTests class methodsFor:'documentation'! |
|
1061 |
||
1062 |
version |
|
1063 |
^ '$Header$' |
|
640 | 1064 |
! |
1065 |
||
1066 |
version_CVS |
|
1067 |
^ '$Header$' |
|
115 | 1068 |
! ! |