Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use binary shrinking for integral. #413

Merged
merged 4 commits into from
Feb 5, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions hedgehog/src/Hedgehog/Internal/Gen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -799,9 +799,40 @@ golden x =
-- > 2058
-- > 2060
--
integral :: (MonadGen m, Integral a) => Range a -> m a
integral :: forall m a. (MonadGen m, Integral a) => Range a -> m a
integral range =
shrink (Shrink.towards $ Range.origin range) (integral_ range)
-- https://github.com/hedgehogqa/haskell-hedgehog/pull/413/files
let
HuwCampbell marked this conversation as resolved.
Show resolved Hide resolved
tryOriginFirst :: Tree.TreeT (MaybeT (GenBase m)) a -> Tree.TreeT (MaybeT (GenBase m)) a
tryOriginFirst tree =
Tree.TreeT $ do
let origin_ = Range.origin range
Tree.NodeT x xs <- Tree.runTreeT tree
pure $
if x == origin_ then
Tree.NodeT x xs
else
Tree.NodeT x (pure origin_ : xs)

binarySearchTree :: a -> Tree.TreeT (MaybeT (GenBase m)) a -> Tree.TreeT (MaybeT (GenBase m)) a
binarySearchTree bottom tree =
Tree.TreeT $ do
Tree.NodeT x xs <- Tree.runTreeT tree
let
level =
Shrink.towards bottom x
zipped =
zipWith (\b -> binarySearchTree b . pure) level (drop 1 level)

pure $
Tree.NodeT x $
xs <> zipped

withGenT' :: (GenT (GenBase m) a -> GenT (GenBase m) b) -> m a -> m b
withGenT' = withGenT

in
withGenT' (mapGenT (tryOriginFirst . binarySearchTree (Range.origin range))) (integral_ range)

-- | Generates a random integral number in the [inclusive,inclusive] range.
--
Expand Down