Skip to content

Instantly share code, notes, and snippets.

@cschneid
Created April 1, 2015 19:02
Show Gist options
  • Save cschneid/aaafaac0b88910201613 to your computer and use it in GitHub Desktop.
Save cschneid/aaafaac0b88910201613 to your computer and use it in GitHub Desktop.
module Data.List.HelpersTest where
import Test.Tasty
import Test.Tasty.SmallCheck as SC
import Test.Tasty.QuickCheck as QC
import Test.Tasty.HUnit
import Data.List (nub)
import Data.List.Helpers
tests :: TestTree
tests = testGroup "Data.List.Helpers" [ selectTests
, separateTests
]
selectTests = testGroup "select" [selectQCProps, selectUnitTests]
separateTests = testGroup "separate" [separateQCProps, separateUnitTests]
selectQCProps = testGroup "Properties"
[
QC.testProperty "length of input == length of output" $
\list -> length (select (list :: [Int])) == length list
, QC.testProperty "with a unique input list, all outputs are unique" $
\list ->
let uniqList = nub (list :: [Int])
in nub (select uniqList) == select uniqList
, QC.testProperty "each item is picked in order" $
\list -> map fst (select (list :: [Int])) == list
]
selectUnitTests = testGroup "Unit tests" []
separateQCProps = testGroup "Properties"
[
QC.testProperty "length of input == length of output" $
\list -> length (separate (list :: [Int])) == length list
, QC.testProperty "merging the list back together equals original list" $
\list -> let merge (xs,y,zs) = xs ++ [y] ++ zs
mergedLists = map merge (separate list)
in all (== (list :: [Int])) mergedLists
]
separateUnitTests = testGroup "Unit tests" []
@cschneid
Copy link
Author

cschneid commented Apr 1, 2015

module Data.List.Helpers where

-- 13:56 Cale: > select [1,2,3,4]
-- 13:56 lambdabot: [(1,[2,3,4]),(2,[1,3,4]),(3,[1,2,4]),(4,[1,2,3])]
select [] = []
select (x:xs) = (x,xs) : [(y,x:ys) | (y,ys) <- select xs]

-- 13:56 Cale: > separate [1,2,3,4]
-- 13:56 lambdabot: [([],1,[2,3,4]),([1],2,[3,4]),([1,2],3,[4]),([1,2,3],4,[])]
separate [] = []
separate (x:xs) = ([],x,xs) : [(x:us,v,vs) | (us,v,vs) <- separate xs]

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