Skip to content

Commit 4662eb4

Browse files
committed
Add tests for unfoldr{N,NM}
1 parent 8861057 commit 4662eb4

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

tests/Tests/Vector.hs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,8 @@ testPolymorphicFunctions _ = $(testProperties [
120120
'prop_createT,
121121
{- 'prop_replicateM, 'prop_generateM, 'prop_create, -}
122122

123-
-- Unfolding (FIXME)
124-
{- 'prop_unfoldr, prop_unfoldrN, -}
125-
'prop_unfoldr, 'prop_unfoldrM,
123+
-- Unfolding
124+
'prop_unfoldr, 'prop_unfoldrN, 'prop_unfoldrM, 'prop_unfoldrNM,
126125
'prop_constructN, 'prop_constructrN,
127126

128127
-- Enumeration? (FIXME?)
@@ -433,21 +432,27 @@ testPolymorphicFunctions _ = $(testProperties [
433432

434433
-- Because the vectors are strict, we need to be totally sure that the unfold eventually terminates. This
435434
-- is achieved by injecting our own bit of state into the unfold - the maximum number of unfolds allowed.
436-
limitUnfolds f (theirs, ours) | ours >= 0
437-
, Just (out, theirs') <- f theirs = Just (out, (theirs', ours - 1))
438-
| otherwise = Nothing
435+
limitUnfolds f (theirs, ours)
436+
| ours > 0
437+
, Just (out, theirs') <- f theirs = Just (out, (theirs', ours - 1))
438+
| otherwise = Nothing
439439
limitUnfoldsM f (theirs, ours)
440-
| ours >= 0 = do r <- f theirs
440+
| ours > 0 = do r <- f theirs
441441
return $ (\(a,b) -> (a,(b,ours - 1))) `fmap` r
442442
| otherwise = return Nothing
443443

444444

445445
prop_unfoldr :: P (Int -> (Int -> Maybe (a,Int)) -> Int -> v a)
446446
= (\n f a -> V.unfoldr (limitUnfolds f) (a, n))
447447
`eq` (\n f a -> unfoldr (limitUnfolds f) (a, n))
448+
prop_unfoldrN :: P (Int -> (Int -> Maybe (a,Int)) -> Int -> v a)
449+
= V.unfoldrN `eq` (\n f a -> unfoldr (limitUnfolds f) (a, n))
448450
prop_unfoldrM :: P (Int -> (Int -> Writer [Int] (Maybe (a,Int))) -> Int -> Writer [Int] (v a))
449451
= (\n f a -> V.unfoldrM (limitUnfoldsM f) (a,n))
450452
`eq` (\n f a -> Util.unfoldrM (limitUnfoldsM f) (a, n))
453+
prop_unfoldrNM :: P (Int -> (Int -> Writer [Int] (Maybe (a,Int))) -> Int -> Writer [Int] (v a))
454+
= V.unfoldrNM `eq` (\n f a -> Util.unfoldrM (limitUnfoldsM f) (a, n))
455+
451456
prop_constructN = \f -> forAll (choose (0,20)) $ \n -> unP prop n f
452457
where
453458
prop :: P (Int -> (v a -> a) -> v a) = V.constructN `eq` constructN []

tests/Utilities.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,3 +342,9 @@ unfoldrM step b0 = do
342342
Nothing -> return []
343343
Just (a,b) -> do as <- unfoldrM step b
344344
return (a : as)
345+
346+
347+
limitUnfolds f (theirs, ours)
348+
| ours >= 0
349+
, Just (out, theirs') <- f theirs = Just (out, (theirs', ours - 1))
350+
| otherwise = Nothing

0 commit comments

Comments
 (0)