Skip to content

Commit 7f4750e

Browse files
Speed up heavy use of mapTotalResult
1 parent d66336c commit 7f4750e

4 files changed

Lines changed: 37 additions & 6 deletions

File tree

QuickCheck.cabal

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,14 @@ Test-Suite test-quickcheck-monoids
287287
cpp-options: -DNO_SEMIGROUP_SUPERCLASS
288288
if !impl(ghc >= 8.0)
289289
cpp-options: -DNO_SEMIGROUP_CLASS
290+
291+
Test-Suite test-quickcheck-tabulate-slow
292+
type: exitcode-stdio-1.0
293+
Default-language: Haskell2010
294+
hs-source-dirs: tests
295+
main-is: TabulateSlow.hs
296+
build-depends: base, QuickCheck
297+
if !impl(ghc >= 8.4)
298+
cpp-options: -DNO_SEMIGROUP_SUPERCLASS
299+
if !impl(ghc >= 8.0)
300+
cpp-options: -DNO_SEMIGROUP_CLASS

src/Test/QuickCheck/Property.hs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ instance Testable prop => Testable (Gen prop) where
141141
property mp = MkProperty $ do p <- mp; unProperty (property p)
142142

143143
instance Testable Property where
144-
property (MkProperty mp) = MkProperty (fmap protectProp mp)
144+
property = id
145145

146146
-- | Do I/O inside a property.
147147
{-# DEPRECATED morallyDubiousIOProperty "Use 'ioProperty' instead" #-}
@@ -241,10 +241,13 @@ protectProp (MkProp r) = MkProp (IORose . protectRose . return $ r)
241241

242242
-- | Wrap all the Results in a rose tree in exception handlers.
243243
protectResults :: Rose Result -> Rose Result
244-
protectResults = onRose $ \x rs ->
245-
IORose $ do
246-
y <- protectResult (return x)
247-
return (MkRose y (map protectResults rs))
244+
protectResults = IORose . protect'
245+
where
246+
protect' :: Rose Result -> IO (Rose Result)
247+
protect' (MkRose x rs) = do
248+
y <- protectResult (return x)
249+
return (MkRose y (map protectResults rs))
250+
protect' (IORose m) = m >>= protect'
248251

249252
-- ** Result type
250253

src/Test/QuickCheck/Test.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@ quickCheckResult p = quickCheckWithResult stdArgs p
201201
-- | Tests a property, using test arguments, produces a test result, and prints the results to 'stdout'.
202202
quickCheckWithResult :: Testable prop => Args -> prop -> IO Result
203203
quickCheckWithResult a p =
204-
withState a (\s -> test s (property p))
204+
let MkProperty mp = property p
205+
in withState a (\s -> test s $ MkProperty $ fmap protectProp mp)
205206

206207
-- | Re-run a property with the seed and size that failed in a run of 'quickCheckResult'.
207208
recheck :: Testable prop => Result -> prop -> IO ()

tests/TabulateSlow.hs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{-# LANGUAGE NumericUnderscores #-}
2+
import Test.QuickCheck
3+
import Test.QuickCheck.Monadic
4+
5+
prop_tabulateALot :: Int -> Property
6+
prop_tabulateALot x =
7+
tabulates 1_000
8+
where
9+
tabulates 0 = x === x
10+
tabulates n =
11+
tabulate "World" ["Hello"] $
12+
tabulate "Hello" (["World" | even n] ++ ["There" | odd n]) $
13+
tabulates (n - 1)
14+
15+
main = do
16+
quickCheck $ forAll arbitrary prop_tabulateALot

0 commit comments

Comments
 (0)