Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

commented Aug 21, 2014

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
You can’t perform that action at this time.