Created
August 21, 2014 05:12
-
-
Save rickardlindberg/612174dc97b9d43eb7a7 to your computer and use it in GitHub Desktop.
Testing my own sort function by comparing it to the standard library sort for many random test cases.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.List | |
import Test.QuickCheck | |
mySort :: [Int] -> [Int] | |
mySort list = | |
let swapped = swap list in | |
if swapped == list | |
then list | |
else mySort swapped | |
where | |
swap [] = [] | |
swap [x] = [x] | |
swap (a:b:xs) | |
| a > b = b:(swap (a:xs)) | |
| otherwise = a:(swap (b:xs)) | |
genList :: Gen [Int] | |
genList = arbitrary | |
prop_mySortEqualToStdLibSort = forAllShrink genList shrink $ \xs -> | |
mySort xs === sort xs | |
main = do | |
sample genList | |
quickCheck prop_mySortEqualToStdLibSort |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example output:
$ runhaskell MySortExample.hs
[]
[]
[4,1,2]
[-2,-3]
[3,6,-6,-2,4,6,-2]
[-4,-8,-6,-4,4,2,6,8]
[6,0,3]
[-2,-3,4,-13,11,9,9,-10,0,12,6,-14,8,10]
[]
[-10,-2,-13,1,-13,-14,-4,11,9,11,-18]
[1,-1,16,10,-2,-11]
+++ OK, passed 100 tests.