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" $ \(SortedNNS (n,ns)) ->
21 combinOfRank n (toInteger $ length ns) (rankOfCombin n ns) == ns
22 , testProperty "rankOfSequence n (sequenceOfRank n k r) == r" $ \(NKR (n,k,r)) ->
23 rankOfSequence n (sequenceOfRank n k r) == r
24 , testProperty "sequenceOfRank n (length ns) (rankOfSequence n ns) == ns" $ \(NNS (n,ns)) ->
25 sequenceOfRank n (toInteger $ length ns) (rankOfSequence n ns) == ns
29 newtype NNS = NNS (Integer, [Integer])
31 instance Arbitrary NNS where
33 n <- arbitrarySizedNatural
35 ns <- List.take (fromInteger k) . nubList <$> infiniteListOf (choose (1,n))
39 newtype SortedNNS = SortedNNS (Integer, [Integer])
41 instance Arbitrary SortedNNS where
43 n <- arbitrarySizedNatural
45 ns <- List.sort . List.take (fromInteger k) . nubList <$> infiniteListOf (choose (1,n))
46 return $ SortedNNS (n,ns)
49 newtype NKR = NKR (Integer, Integer, Integer)
51 instance Arbitrary NKR where
53 n <- arbitrarySizedNatural
55 r <- choose (0, (n`nCk`k) - 1)
58 -- | Like 'nub', but O(n * log n).
59 nubList :: Ord a => [a] -> [a]
60 nubList = go Set.empty where
62 go s (x:xs) | x`Set.member`s = go s xs
63 | otherwise = x:go (Set.insert x s) xs