| 1 |
-- | This module implements a simplified, pure version of Test.Quickcheck's |
| 2 |
-- quickCheck functionality. |
| 3 |
|
| 4 |
-- Author: Bertram Felgenhauer |
| 5 |
-- License: MIT |
| 6 |
|
| 7 |
{-# LANGUAGE BangPatterns #-} |
| 8 |
{-# LANGUAGE Safe #-} |
| 9 |
{-# LANGUAGE CPP #-} |
| 10 |
|
| 11 |
module Test.QuickCheck.Safe ( |
| 12 |
-- * Checking properties |
| 13 |
quickCheck, quickCheckResult, quickCheckWith, quickCheckWithResult, |
| 14 |
-- * Creating and combining properties |
| 15 |
STestable(), |
| 16 |
(==>), (.||.), (.&&.), (.&.), (===), |
| 17 |
label, shrinking, noShrinking, mapSize, |
| 18 |
forAll, forAllShrink, |
| 19 |
-- * Miscellaneous |
| 20 |
inventQCGen, |
| 21 |
module Test.QuickCheck |
| 22 |
) where |
| 23 |
|
| 24 |
import Test.QuickCheck.Safe.Trusted |
| 25 |
|
| 26 |
import Test.QuickCheck hiding ( |
| 27 |
Testable(..), Property(..), |
| 28 |
(==>), (.||.), (.&&.), (.&.), (===), |
| 29 |
label, shrinking, noShrinking, mapSize, |
| 30 |
forAll, forAllShrink, |
| 31 |
classify, collect, conjoin, counterexample, cover, disjoin, |
| 32 |
expectFailure, once, printTestCase, verbose, within, |
| 33 |
quickCheck, quickCheckResult, quickCheckWith, quickCheckWithResult) |
| 34 |
import Test.QuickCheck.Gen (Gen(..)) |
| 35 |
import Control.Monad |
| 36 |
import qualified Data.Set as S |
| 37 |
import qualified Data.Map as M |
| 38 |
|
| 39 |
-- STestable and SProperty are simplified versions of Testable/Property |
| 40 |
class STestable prop where |
| 41 |
sProperty :: prop -> SProperty |
| 42 |
|
| 43 |
newtype SProperty = MkSProperty{ unSProperty :: Gen SResult } |
| 44 |
|
| 45 |
data SResult |
| 46 |
= SOk -- success |
| 47 |
| SDiscard -- discarded sample |
| 48 |
| SFail{ -- failed sample |
| 49 |
sLabels :: [String], -- text describing counterexample |
| 50 |
sException :: Maybe AnException, -- caught exception, if any |
| 51 |
sSmaller :: [SResult] -- results of shrunk examples |
| 52 |
} |
| 53 |
|
| 54 |
instance STestable SProperty where |
| 55 |
sProperty prop = prop |
| 56 |
|
| 57 |
instance STestable prop => STestable (Gen prop) where |
| 58 |
sProperty gen = MkSProperty $ gen >>= unSProperty . sProperty |
| 59 |
|
| 60 |
-- instance STestable Discard where |
| 61 |
-- sProperty _ = MkSProperty . return $ SDiscard |
| 62 |
|
| 63 |
instance STestable Bool where |
| 64 |
sProperty b = MkSProperty . return $ case pureEvaluate b of |
| 65 |
Right True -> SOk |
| 66 |
Right _ -> SFail{ sLabels = [], sException = Nothing, sSmaller = [] } |
| 67 |
Left e -> SFail{ sLabels = [], sException = Just e, sSmaller = [] } |
| 68 |
|
| 69 |
instance (Arbitrary a, Show a, STestable prop) => STestable (a -> prop) where |
| 70 |
sProperty = forAllShrink arbitrary shrink |
| 71 |
|
| 72 |
-- | Implication. Cf. 'Test.QuickCheck.==>'. |
| 73 |
(==>) :: STestable prop => Bool -> prop -> SProperty |
| 74 |
t ==> p = case pureEvaluate t of |
| 75 |
Right True -> sProperty $ p |
| 76 |
Right _ -> MkSProperty . return $ SDiscard |
| 77 |
Left e -> MkSProperty . return $ |
| 78 |
SFail{ sLabels = [], sException = Just e, sSmaller = [] } |
| 79 |
|
| 80 |
-- | Equality test. Cf. 'Test.QuickCheck.==='. |
| 81 |
(===) :: (Eq a, Show a) => a -> a -> SProperty |
| 82 |
a === b = label (show a ++ " /= " ++ show b) $ sProperty (a == b) |
| 83 |
|
| 84 |
-- | Conjunction. Cf. 'Test.QuickCheck..&&.'. |
| 85 |
(.&&.) :: (STestable prop2, STestable prop1) => prop1 -> prop2 -> SProperty |
| 86 |
prop1 .&&. prop2 = MkSProperty $ do |
| 87 |
res1 <- unSProperty $ label "LHS" $ prop1 |
| 88 |
case res1 of |
| 89 |
SOk -> unSProperty $ label "RHS" $ prop2 |
| 90 |
_ -> return res1 |
| 91 |
|
| 92 |
-- | Disjunction. Cf. 'Test.QuickCheck..||.'. |
| 93 |
(.||.) :: (STestable prop2, STestable prop1) => prop1 -> prop2 -> SProperty |
| 94 |
prop1 .||. prop2 = MkSProperty $ do |
| 95 |
res1 <- unSProperty . sProperty $ prop1 |
| 96 |
res2 <- unSProperty . sProperty $ prop2 |
| 97 |
let merge res1@SFail{ sSmaller = shr1 } res2@SFail{ sSmaller = shr2 } = |
| 98 |
SFail{ |
| 99 |
sLabels = sLabels res1 ++ sLabels res2, |
| 100 |
sException = sException res1 `mplus` sException res2, |
| 101 |
sSmaller = map (`merge` res2) shr1 ++ map (res1 `merge`) shr2 |
| 102 |
} |
| 103 |
merge res1 SFail{} = res1 |
| 104 |
merge SFail{} res2 = res2 |
| 105 |
return $ res1 `merge` res2 |
| 106 |
|
| 107 |
-- | Nondeterministic conjunction. Cf. 'Test.QuickCheck.&.'. |
| 108 |
(.&.) :: (STestable prop2, STestable prop1) => prop1 -> prop2 -> SProperty |
| 109 |
prop1 .&. prop2 = MkSProperty $ do |
| 110 |
c <- choose (0, 1) |
| 111 |
case c :: Int of |
| 112 |
0 -> unSProperty $ label "LHS" prop1 |
| 113 |
1 -> unSProperty $ label "RHS" prop2 |
| 114 |
|
| 115 |
-- | Label tests. Cf. 'Test.QuickCheck.label'. |
| 116 |
label :: STestable prop => String -> prop -> SProperty |
| 117 |
label lab = MkSProperty . fmap (labelSResult lab) . unSProperty . sProperty |
| 118 |
|
| 119 |
labelSResult :: String -> SResult -> SResult |
| 120 |
labelSResult lab = mapSResultLabels (lab :) |
| 121 |
|
| 122 |
mapSResultLabels :: ([String] -> [String]) -> SResult -> SResult |
| 123 |
mapSResultLabels f res@SFail{} = res{ |
| 124 |
sLabels = f (sLabels res), |
| 125 |
sSmaller = map (mapSResultLabels f) (sSmaller res) |
| 126 |
} |
| 127 |
mapSResultLabels _ res = res |
| 128 |
|
| 129 |
-- | Shrink counterexamples. Cf. 'Test.QuickCheck.shrinking'. |
| 130 |
shrinking :: STestable prop => (a -> [a]) -> a -> (a -> prop) -> SProperty |
| 131 |
shrinking shr x f = MkSProperty $ MkGen $ \seed size -> do |
| 132 |
let unfold x = case unGen (unSProperty . sProperty $ f x) seed size of |
| 133 |
res@SFail{ sSmaller = ps } -> |
| 134 |
res{ sSmaller = map unfold (shr x) ++ sSmaller res } |
| 135 |
res -> res |
| 136 |
unfold x |
| 137 |
|
| 138 |
-- | Suppress shrinking of counterexamples. Cf. 'Test.QuickCheck.noShrinking'. |
| 139 |
noShrinking :: STestable prop => prop -> SProperty |
| 140 |
noShrinking prop = MkSProperty $ do |
| 141 |
res <- unSProperty . sProperty $ prop |
| 142 |
return $ case res of |
| 143 |
SFail{} -> res{ sSmaller = [] } |
| 144 |
_ -> res |
| 145 |
|
| 146 |
-- | Universal quantification with shrinking. |
| 147 |
-- Cf. 'Test.QuickCheck.forAllShrink'. |
| 148 |
forAllShrink :: (Show a, STestable prop) => |
| 149 |
Gen a -> (a -> [a]) -> (a -> prop) -> SProperty |
| 150 |
forAllShrink gen shr f = MkSProperty $ do |
| 151 |
x <- gen |
| 152 |
unSProperty . label (show x) $ shrinking shr x f |
| 153 |
|
| 154 |
-- | Universal quantification. Cf. 'Test.QuickCheck.forAll'. |
| 155 |
forAll :: (Show a, STestable prop) => Gen a -> (a -> prop) -> SProperty |
| 156 |
forAll gen = forAllShrink gen (const []) |
| 157 |
|
| 158 |
-- | Adjust testcase sizes. Cf. 'Test.QuickCheck.mapSize'. |
| 159 |
mapSize :: STestable prop => (Int -> Int) -> prop -> SProperty |
| 160 |
mapSize f = MkSProperty . scale f . unSProperty . sProperty where |
| 161 |
scale f a = sized (\n -> resize (f n) a) |
| 162 |
|
| 163 |
-- Other combinators that may be considered: |
| 164 |
|
| 165 |
-- classify :: STestable prop => Bool -> String -> prop -> SProperty |
| 166 |
-- collect :: (Show a, STestable prop) => a -> prop -> SProperty |
| 167 |
-- conjoin :: STestable prop => [prop] -> SProperty |
| 168 |
-- counterexample :: STestable prop => String -> prop -> SProperty |
| 169 |
-- cover :: STestable prop => Bool -> Int -> String -> prop -> SProperty |
| 170 |
-- disjoin :: STestable prop => [prop] -> SProperty |
| 171 |
-- expectFailure :: STestable prop => prop -> SProperty |
| 172 |
-- once :: STestable prop => prop -> SProperty |
| 173 |
-- printTestCase :: STestable prop => String -> prop -> SProperty |
| 174 |
-- verbose :: STestable prop => prop -> SProperty |
| 175 |
-- within :: STestable prop => Int -> prop -> SProperty |
| 176 |
|
| 177 |
-- | Cf. 'Test.QuickCheck.quickCheckWithResult'. Note that in contrast to |
| 178 |
-- QuickCheck's function, this one takes an additional 'QCGen' argument. |
| 179 |
quickCheckWithResult :: STestable prop => Args -> QCGen -> prop -> Result |
| 180 |
quickCheckWithResult args seed prop = unGen (runTests 0 0 sizes) seed' 0 where |
| 181 |
runTests :: Int -> Int -> [Int] -> Gen Result |
| 182 |
runTests pass disc (size : sizes) |
| 183 |
| pass >= maxSuccess args = |
| 184 |
return Success{ |
| 185 |
numTests = pass, |
| 186 |
#if MIN_VERSION_QuickCheck(2,12,0) |
| 187 |
numDiscarded = disc, |
| 188 |
labels = M.empty, |
| 189 |
classes = M.empty, |
| 190 |
tables = M.empty, |
| 191 |
#else |
| 192 |
labels = [], |
| 193 |
#endif |
| 194 |
output = "+++ OK, passed " ++ show pass ++ " tests.\n" |
| 195 |
} |
| 196 |
| disc > (maxDiscardRatio args - 1) * maxSuccess args = |
| 197 |
return GaveUp{ |
| 198 |
numTests = pass, |
| 199 |
#if MIN_VERSION_QuickCheck(2,12,0) |
| 200 |
numDiscarded = disc, |
| 201 |
labels = M.empty, |
| 202 |
classes = M.empty, |
| 203 |
tables = M.empty, |
| 204 |
#else |
| 205 |
labels = [], |
| 206 |
#endif |
| 207 |
output = "*** Gave up! Passed only " ++ show pass ++ " tests.\n" |
| 208 |
} |
| 209 |
| otherwise = do |
| 210 |
(seed, _) <- MkGen (,) |
| 211 |
res <- resize size (unSProperty . sProperty $ prop) |
| 212 |
case res of |
| 213 |
SOk -> runTests (pass + 1) disc sizes |
| 214 |
SDiscard -> runTests pass (disc + 1) sizes |
| 215 |
SFail{} -> return $ deflate pass disc 0 0 0 seed size res |
| 216 |
|
| 217 |
deflate :: Int -> Int -> Int -> Int -> Int -> QCGen -> Int -> SResult -> Result |
| 218 |
deflate pass disc !shr !shrT !shrF seed size res@SFail{ sSmaller = [] } = |
| 219 |
Failure{ |
| 220 |
numTests = pass, |
| 221 |
numShrinks = shr, |
| 222 |
numShrinkTries = shrT, |
| 223 |
numShrinkFinal = shrF, |
| 224 |
usedSeed = seed, |
| 225 |
usedSize = size, |
| 226 |
reason = reason, |
| 227 |
theException = sException res, |
| 228 |
#if MIN_VERSION_QuickCheck(2,10,0) |
| 229 |
failingTestCase = sLabels res, |
| 230 |
#endif |
| 231 |
#if !MIN_VERSION_QuickCheck(2,12,0) |
| 232 |
labels = map (\x -> (x, 0)) (sLabels res), |
| 233 |
#else |
| 234 |
numDiscarded = disc, |
| 235 |
failingLabels = sLabels res, |
| 236 |
failingClasses = S.empty, |
| 237 |
#endif |
| 238 |
output = "*** Failed! " ++ reason ++ |
| 239 |
" (after " ++ count (pass + 1) "test" ++ |
| 240 |
(if shr > 0 then " and " ++ count shr "shrink" else "") ++ |
| 241 |
"):\n" ++ unlines (sLabels res) |
| 242 |
} |
| 243 |
where |
| 244 |
count i w = show i ++ " " ++ w ++ ['s' | i /= 1] |
| 245 |
reason = maybe "Falsifiable" (\e -> "Exception: '" ++ show e ++ "'") $ |
| 246 |
sException res |
| 247 |
deflate pass disc shr shrT shrF seed size res@SFail{ sSmaller = res' : rs } = |
| 248 |
case res' of |
| 249 |
SFail{} -> deflate pass disc (shr + 1) (shrT + shrF) 0 seed size res' |
| 250 |
_ -> deflate pass disc shr shrT (shrF + 1) seed size res{ sSmaller = rs } |
| 251 |
|
| 252 |
sizes :: [Int] |
| 253 |
sizes = cycle [0..maxSize args] |
| 254 |
|
| 255 |
seed' :: QCGen |
| 256 |
seed' = maybe seed fst (replay args) |
| 257 |
|
| 258 |
-- | Cf. 'Test.QuickCheck.quickCheckResult'. Note that in contrast to |
| 259 |
-- QuickCheck's function, this one takes an additional 'QCGen' argument. |
| 260 |
quickCheckResult :: STestable prop => QCGen -> prop -> Result |
| 261 |
quickCheckResult = quickCheckWithResult stdArgs |
| 262 |
|
| 263 |
-- | Cf. 'Test.QuickCheck.quickCheckWith'. Note that in contrast to |
| 264 |
-- QuickCheck's function, this one takes an additional 'QCGen' argument. |
| 265 |
quickCheckWith :: STestable prop => Args -> QCGen -> prop -> String |
| 266 |
quickCheckWith args seed = output . quickCheckWithResult args seed |
| 267 |
|
| 268 |
-- | Cf. 'Test.QuickCheck.quickCheck'. Note that in contrast to QuickCheck's |
| 269 |
-- function, this one takes an additional 'QCGen' argument. |
| 270 |
-- |
| 271 |
-- >>> putStr $ quickCheck (inventQCGen ()) (\x -> length (x :: [()]) < 10) |
| 272 |
-- *** Failed! Falsifiable (after 18 tests and 3 shrinks): |
| 273 |
-- [(),(),(),(),(),(),(),(),(),(),(),(),(),(),()] |
| 274 |
quickCheck :: STestable prop => QCGen -> prop -> String |
| 275 |
quickCheck = quickCheckWith stdArgs |
| 276 |
|