-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from quickstrom/shrink-tree-improve
Shrink tree improvements
- Loading branch information
Showing
8 changed files
with
90 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
{-# LANGUAGE DeriveTraversable #-} | ||
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
{-# LANGUAGE LambdaCase #-} | ||
{-# LANGUAGE RankNTypes #-} | ||
|
||
module Quickstrom.Run.Shrinking (Prefix (..), shrinkPrefixes, searchSmallestFailingPrefix, drawShrinkForest) where | ||
|
||
import Control.Lens | ||
import Control.Lens.Extras (is) | ||
import Data.String (String) | ||
import Data.Tree | ||
import qualified Pipes | ||
import Quickstrom.Prelude hiding (Prefix) | ||
|
||
newtype Prefix a = Prefix { unPrefix :: [a] } | ||
deriving (Eq, Show, Hashable, Functor, Foldable, Traversable) | ||
|
||
shrinkPrefixes :: [a] -> Forest (Prefix a) | ||
shrinkPrefixes as = go 0 (length as `div` 2) as | ||
where | ||
go minLength n xs | ||
| n < 1 = [] | ||
| otherwise = | ||
let len = length xs - n | ||
in if len <= minLength | ||
then go minLength (n `div` 2) xs | ||
else | ||
let xs' = take len xs | ||
in Node (Prefix xs') (go minLength (len `div` 2) xs') : | ||
go len (n `div` 2) xs | ||
|
||
searchSmallestFailingPrefix :: Monad m => (Prefix a -> m b) -> Prism' b x -> Forest (Prefix a) -> Pipes.Producer b m () | ||
searchSmallestFailingPrefix test failure = go | ||
where | ||
go = \case | ||
[] -> pure () | ||
Node x xs : rest -> do | ||
r <- lift (test x) | ||
Pipes.yield r | ||
if is failure r | ||
then go xs | ||
else go rest | ||
|
||
drawShrinkForest :: Show a => Forest (Prefix a) -> String | ||
drawShrinkForest = drawForest . map (map show) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module Quickstrom.Run.ShrinkingTest where | ||
|
||
import Quickstrom.Prelude | ||
import Quickstrom.Run.Shrinking (shrinkPrefixes, Prefix (unPrefix)) | ||
import Data.Tree | ||
import Test.QuickCheck | ||
|
||
prop_shrinkPrefixes_includes_all_prefixes :: NonEmptyList Int -> Property | ||
prop_shrinkPrefixes_includes_all_prefixes (NonEmpty xs) = | ||
let allPrefixes = sort (map unPrefix (foldMap flatten (shrinkPrefixes xs))) | ||
in allPrefixes === tailSafe (initSafe (inits xs)) | ||
|
||
prop_shrinkPrefixes_is_ordered :: NonEmptyList Int -> Property | ||
prop_shrinkPrefixes_is_ordered (NonEmpty xs) = property $ | ||
let treeMaxLen = foldTree (\p subLengths -> maximum (length (unPrefix p) : subLengths)) | ||
isOrdered forest = | ||
let lengths = map treeMaxLen forest | ||
in lengths == sort lengths && all (isOrdered . subForest) forest | ||
in isOrdered (shrinkPrefixes xs) | ||
|