more sort tests
authorStefan Vogel <sv@exept.de>
Wed, 11 Apr 2012 21:52:30 +0200
changeset 655 43841f85365a
parent 654 fbf6fc76b0fa
child 656 470e994fc84b
more sort tests
RegressionTests__CollectionTests.st
--- a/RegressionTests__CollectionTests.st	Wed Mar 07 13:19:22 2012 +0100
+++ b/RegressionTests__CollectionTests.st	Wed Apr 11 21:52:30 2012 +0200
@@ -43,6 +43,21 @@
 
 !CollectionTests methodsFor:'helpers'!
 
+checkSorted:aSortedCollection with:sortBlock against:anOriginalCollection
+    |first|
+
+    self assert:aSortedCollection size = anOriginalCollection size.
+
+    "is it sorted?"
+    first := true.
+    aSortedCollection inject:nil into:[:last :each|
+            first ifTrue:[first := false] 
+                  ifFalse:[self assert:(sortBlock value:last value:each)]. each].
+
+    "same elements as in original?"
+    self assert:(aSortedCollection asBag = anOriginalCollection asBag).
+!
+
 collectedDoArgsOf:aCollection
     |collectedDoArgs|
 
@@ -440,6 +455,42 @@
     "
      self new testOrderedCollection04_SortNilsInCollection
     "
+!
+
+testOrderedCollection05_SortRandomCollection
+     |data sorted rg coData checkBlock|
+    
+     checkBlock := [:a :b| a <= b].   
+     rg := Random new.
+
+     #(1 4 11 2000 20011 200000) do:[:eachSize|
+         data := Array new:eachSize.
+         1 to:data size do:[:i |
+            data at:i put:(rg nextIntegerBetween:1 and:100).
+         ].
+         sorted := data copy sort.
+         self checkSorted:sorted with:checkBlock against:data.
+         sorted := sorted sort.
+         self checkSorted:sorted with:checkBlock against:data.
+         sorted := sorted reverse sort.
+         self checkSorted:sorted with:checkBlock against:data.
+
+         coData := data copy.   
+         sorted := data copy sortWith:coData.
+         self checkSorted:sorted with:checkBlock against:data.
+         self checkSorted:coData with:checkBlock against:data.
+
+         sorted := data copy sort:[:policy :a :b| a < b].
+         self checkSorted:sorted with:checkBlock against:data.
+
+         sorted := data copy sort:[:a :b| a < b].
+         self checkSorted:sorted with:checkBlock against:data.
+
+         coData := data copy.   
+         sorted := data copy sort:[:a :b| a < b] with:coData.
+         self checkSorted:sorted with:checkBlock against:data.
+         self checkSorted:coData with:checkBlock against:data.
+    ].
 ! !
 
 !CollectionTests methodsFor:'tests-orderedDictionary'!