1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| module TestCribbage where
import Test.QuickCheck
import Cribbage
import Control.Monad
newtype CribbageHand = CH Hand
deriving (Show)
instance Arbitrary Suit where
arbitrary = elements [Clubs, Diamond, Heart, Spade]
coarbitrary = undefined
instance Arbitrary Rank where
arbitrary = liftM toEnum $ choose (0, 12)
coarbitrary = undefined
differents n = differents' n arbitrary
differents' n g = aux_diff n []
where
aux_diff 0 _ = return []
aux_diff n seen = do
x <- untilM (not . (`elem` seen)) g
xs <- aux_diff (n-1) (x:seen)
return (x:xs)
untilM p act = do
result <- act
if p result then return result else untilM p act
instance Arbitrary CribbageHand where
arbitrary = liftM CH $ differents 4
coarbitrary = undefined
prop_Trivial (CH hand) starter isCrib =
count hand starter isCrib >= 0
prop_Flush randSuit starter =
forAll (differents 4) $ \ranks ->
let hand = map (\r->(r,randSuit)) ranks
score = countFlush hand starter False
in score >= 4 && score <= 5
prop_Jocker (CH hand) starter =
let jacks = filter ((== Jack) . rank) hand
starterSuit = liftM suit starter
in not (null jacks)
&& maybe False (\s -> any ((== s).suit) jacks) starterSuit
==> countJack hand starter == 1 |
Partager