Skip to content

Commit 7cb5442

Browse files
Also bench and improve chooseBackwards
1 parent c0f96f9 commit 7cb5442

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

bench/Constrained/Bench.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import Constrained.API
1313
import Constrained.Generation
1414
import Constrained.Examples.Set
1515
import Constrained.Examples.Map
16+
import Constrained.Examples.Basic
1617

1718
import Control.DeepSeq
1819
import Criterion
@@ -37,6 +38,7 @@ benchmarks =
3738
, benchSpec 10 30 "maybeJustSetSpec" maybeJustSetSpec
3839
, benchSpec 10 40 "eitherKeys" eitherKeys
3940
, benchSimplifySpec "eitherKeys" eitherKeys
41+
, benchSpec 10 40 "chooseBackwards" chooseBackwards'
4042
]
4143

4244
roseTreeMaybe :: Specification (Tree (Maybe (Int, Int)))

src/Constrained/Spec/List.hs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,8 @@ instance HasSpec a => HasSpec [a] where
227227
Just szHint -> do
228228
sz <- genFromSizeSpec (leqSpec szHint)
229229
listOfUntilLenT (genFromSpecT elemS) (fromIntegral sz) (const True)
230-
pureGen $ shuffle (must ++ lst)
230+
must' <- pureGen $ shuffle must
231+
pureGen $ randomInterleaving must' lst
231232
genFromTypeSpec (ListSpec msz must szSpec elemS NoFold) = do
232233
sz0 <- genFromSizeSpec (szSpec <> geqSpec (sizeOf must) <> maybe TrueSpec (leqSpec . max 0) msz)
233234
let sz = fromIntegral (sz0 - sizeOf must)
@@ -236,7 +237,8 @@ instance HasSpec a => HasSpec [a] where
236237
(genFromSpecT elemS)
237238
sz
238239
((`conformsToSpec` szSpec) . (+ sizeOf must) . fromIntegral)
239-
pureGen $ shuffle (must ++ lst)
240+
must' <- pureGen $ shuffle must
241+
pureGen $ randomInterleaving must' lst
240242
genFromTypeSpec (ListSpec msz must szSpec elemS (FoldSpec f foldS)) = do
241243
let szSpec' = szSpec <> maybe TrueSpec (leqSpec . max 0) msz
242244
genFromFold must szSpec' elemS f foldS
@@ -266,6 +268,25 @@ instance HasSpec a => HasSpec [a] where
266268
<> satisfies (sizeOf_ x) size
267269
<> maybe TruePred (flip genHint x) msz
268270

271+
randomInterleaving :: [a] -> [a] -> Gen [a]
272+
randomInterleaving xs ys = go xs ys (length ys)
273+
where
274+
go [] ys _ = pure ys
275+
go xs [] _ = pure xs
276+
go xs ys l = do
277+
i <- choose (0, l)
278+
go' i xs ys (l - i)
279+
280+
go' _ xs [] _ = pure xs
281+
go' _ [] ys _ = pure ys
282+
go' 0 (x:xs) ys l = (x:) <$> go xs ys l
283+
go' i xs (y:ys) l = (y:) <$> go' (i-1) xs ys l
284+
285+
genSplit :: [a] -> Gen ([a], [a])
286+
genSplit as = do
287+
s <- choose (0, length as)
288+
return $ splitAt s as
289+
269290
instance HasSpec a => HasGenHint [a] where
270291
type Hint [a] = Integer
271292
giveHint szHint = typeSpec $ ListSpec (Just szHint) [] mempty mempty NoFold

0 commit comments

Comments
 (0)