Skip to content

Instantly share code, notes, and snippets.

@rickardlindberg
Created August 21, 2014 05:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rickardlindberg/612174dc97b9d43eb7a7 to your computer and use it in GitHub Desktop.
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.
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
@rickardlindberg
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment