1 {-# LANGUAGE FlexibleInstances #-}
2 {-# LANGUAGE ScopedTypeVariables #-}
3 {-# OPTIONS_GHC -fno-warn-orphans #-}
4 module QuickCheck where
8 import Test.Tasty.QuickCheck
10 import qualified Data.List as List
11 import qualified Data.Set as Set
15 quickchecks :: TestTree
17 testGroup "QuickCheck"
18 [ testProperty "rankOfCombin n (combinOfRank n k r) == r" $ \(NKR (n,k,r)) ->
19 rankOfCombin n (combinOfRank n k r) == r
20 , testProperty "combinOfRank n (length ns) (rankOfCombin n ns) == ns" $ \(NNS (n,ns)) ->
21 combinOfRank n (toInteger $ length ns) (rankOfCombin n ns) == ns
25 newtype NNS = NNS (Integer, [Integer])
27 instance Arbitrary NNS where
29 n <- arbitrarySizedNatural
31 ns <- List.sort . List.take (fromInteger k) . nubList <$> infiniteListOf (choose (1,n))
35 newtype NKR = NKR (Integer, Integer, Integer)
37 instance Arbitrary NKR where
39 n <- arbitrarySizedNatural
41 r <- choose (0, (n`nCk`k) - 1)
44 -- | Like 'nub', but O(n * log n).
45 nubList :: Ord a => [a] -> [a]
46 nubList = go Set.empty where
48 go s (x:xs) | x`Set.member`s = go s xs
49 | otherwise = x:go (Set.insert x s) xs