Skip to content

Commit 355120c

Browse files
committed
Add tests for iterateM and unfoldrM
test for unfoldrM is not used yet It required generalization of Arbitrary and TestData instances for Writer monad
1 parent 1f14837 commit 355120c

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

tests/Tests/Vector.hs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module Tests.Vector (tests) where
33
import Boilerplater
44
import Utilities as Util
55

6+
import Data.Functor.Identity
67
import qualified Data.Traversable as T (Traversable(..))
78
import Data.Foldable (Foldable(foldMap))
89

@@ -113,7 +114,7 @@ testPolymorphicFunctions _ = $(testProperties [
113114

114115
-- Initialisation (FIXME)
115116
'prop_empty, 'prop_singleton, 'prop_replicate,
116-
'prop_generate, 'prop_iterateN,
117+
'prop_generate, 'prop_iterateN, 'prop_iterateNM,
117118

118119
-- Monadic initialisation (FIXME)
119120
'prop_createT,
@@ -231,7 +232,8 @@ testPolymorphicFunctions _ = $(testProperties [
231232
= (\n _ -> n < 1000) ===> V.generate `eq` Util.generate
232233
prop_iterateN :: P (Int -> (a -> a) -> a -> v a)
233234
= (\n _ _ -> n < 1000) ===> V.iterateN `eq` (\n f -> take n . iterate f)
234-
235+
prop_iterateNM :: P (Int -> (a -> Writer [Int] a) -> a -> Writer [Int] (v a))
236+
= (\n _ _ -> n < 1000) ===> V.iterateNM `eq` Util.iterateNM
235237
prop_createT :: P ((a, v a) -> (a, v a))
236238
prop_createT = (\v -> V.createT (T.mapM V.thaw v)) `eq` id
237239

@@ -433,10 +435,18 @@ testPolymorphicFunctions _ = $(testProperties [
433435
limitUnfolds f (theirs, ours) | ours >= 0
434436
, Just (out, theirs') <- f theirs = Just (out, (theirs', ours - 1))
435437
| otherwise = Nothing
438+
limitUnfoldsM f (theirs, ours)
439+
| ours >= 0 = do r <- f theirs
440+
return $ (\(a,b) -> (a,(b,ours - 1))) `fmap` r
441+
| otherwise = return Nothing
442+
443+
436444
prop_unfoldr :: P (Int -> (Int -> Maybe (a,Int)) -> Int -> v a)
437445
= (\n f a -> V.unfoldr (limitUnfolds f) (a, n))
438446
`eq` (\n f a -> unfoldr (limitUnfolds f) (a, n))
439-
447+
prop_unfoldrM :: P (Int -> (Int -> Writer [Int] (Maybe (a,Int))) -> Int -> Writer [Int] (v a))
448+
= (\n f a -> V.unfoldrM (limitUnfoldsM f) (a,n))
449+
`eq` (\n f a -> Util.unfoldrM (limitUnfoldsM f) (a, n))
440450
prop_constructN = \f -> forAll (choose (0,20)) $ \n -> unP prop n f
441451
where
442452
prop :: P (Int -> (v a -> a) -> v a) = V.constructN `eq` constructN []

tests/Utilities.hs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@ instance Arbitrary a => Arbitrary (S.Bundle v a) where
5252
instance CoArbitrary a => CoArbitrary (S.Bundle v a) where
5353
coarbitrary = coarbitrary . S.toList
5454

55-
instance Arbitrary a => Arbitrary (Writer a ()) where
56-
arbitrary = fmap (writer . ((,) ())) arbitrary
55+
instance (Arbitrary a, Arbitrary b) => Arbitrary (Writer a b) where
56+
arbitrary = do b <- arbitrary
57+
a <- arbitrary
58+
return $ writer (b,a)
5759

5860
instance CoArbitrary a => CoArbitrary (Writer a ()) where
5961
coarbitrary = coarbitrary . runWriter
@@ -148,13 +150,13 @@ instance (Eq a, TestData a) => TestData (Identity a) where
148150
type EqTest (Identity a) = Property
149151
equal = (property .) . on (==) runIdentity
150152

151-
instance (Eq a, TestData a, Monoid a) => TestData (Writer a ()) where
152-
type Model (Writer a ()) = Writer (Model a) ()
153+
instance (Eq a, TestData a, Eq b, TestData b, Monoid a) => TestData (Writer a b) where
154+
type Model (Writer a b) = Writer (Model a) (Model b)
153155
model = mapWriter model
154156
unmodel = mapWriter unmodel
155157

156-
type EqTest (Writer a ()) = Property
157-
equal = (property .) . on (==) execWriter
158+
type EqTest (Writer a b) = Property
159+
equal = (property .) . on (==) runWriter
158160

159161
instance (Eq a, Eq b, TestData a, TestData b) => TestData (a,b) where
160162
type Model (a,b) = (Model a, Model b)
@@ -325,3 +327,18 @@ maxIndex = fst . foldr1 imax . zip [0..]
325327
imax (i,x) (j,y) | x >= y = (i,x)
326328
| otherwise = (j,y)
327329

330+
iterateNM :: Monad m => Int -> (a -> m a) -> a -> m [a]
331+
iterateNM n f x
332+
| n <= 0 = return []
333+
| n == 1 = return [x]
334+
| otherwise = do x' <- f x
335+
xs <- iterateNM (n-1) f x'
336+
return (x : xs)
337+
338+
unfoldrM :: Monad m => (b -> m (Maybe (a,b))) -> b -> m [a]
339+
unfoldrM step b0 = do
340+
r <- step b0
341+
case r of
342+
Nothing -> return []
343+
Just (a,b) -> do as <- unfoldrM step b
344+
return (a : as)

0 commit comments

Comments
 (0)