on master help sign in
Modify the argument order of testUnfold-ish helpers & add some docs
commita2d35d9a3aebc5988fff14654b082de9a759fbf8 authoradithyaov <adithya@composewell.com> dateFri 23 Oct 2020, 10:50 (6 years ago) parente786c09
1 file changed+26−22
test/Streamly/Test/Data/Unfold.hs+26−22
test/Streamly/Test/Data/Unfold.hs+26−22
@@ -31,9 +31,12 @@ import Test.QuickCheck.Function
3131-- Helper functions
3232-------------------------------------------------------------------------------
3333
34+-- | @testUnfoldM unf seed initial final xs@, runs an unfold under state monad
35+-- using @initial@ as the initial state. @final@ is the expected state after
36+-- running it and @xs@ is the list of elements the stream must produce.
3437testUnfoldM ::
35− (Eq s, Eq b) => Unfold (State s) a b -> s -> s -> a -> [b] -> Bool
36−testUnfoldM unf si sf seed lst = evalState action si
38+ (Eq s, Eq b) => Unfold (State s) a b -> a -> s -> s -> [b] -> Bool
39+testUnfoldM unf seed si sf lst = evalState action si
3740
3841 where
3942
@@ -42,9 +45,10 @@ testUnfoldM unf si sf seed lst = evalState action si
4245 y <- get
4346 return $ x == lst && y == sf
4447
45−testUnfoldMD :: Unfold (State Int) a Int -> Int -> Int -> a -> [Int] -> Bool
48+testUnfoldMD :: Unfold (State Int) a Int -> a -> Int -> Int -> [Int] -> Bool
4649testUnfoldMD = testUnfoldM
4750
51+-- | This is similar to 'testUnfoldM' but without the state monad.
4852testUnfold :: Eq b => Unfold Identity a b -> a -> [b] -> Bool
4953testUnfold unf seed lst = runIdentity action
5054
@@ -64,7 +68,7 @@ testUnfoldD = testUnfold
6468lmapM :: Bool
6569lmapM =
6670 let unf = UF.lmapM (\x -> modify (+ 1) >> return x) (UF.singleton id)
67− in testUnfoldMD unf 0 1 1 [1]
71+ in testUnfoldMD unf 1 0 1 [1]
6872
6973supply :: Bool
7074supply =
@@ -119,28 +123,28 @@ fromStreamK =
119123nilM :: Bool
120124nilM =
121125 let unf = UF.nilM put
122− in testUnfoldMD unf 0 1 1 []
126+ in testUnfoldMD unf 1 0 1 []
123127
124128consM :: Bool
125129consM =
126130 let cns = UF.consM (\a -> modify (+ a) >> get)
127131 unf = cns $ cns $ UF.nilM $ \a -> modify (+ a)
128− in testUnfoldMD unf 0 3 1 [1, 2]
132+ in testUnfoldMD unf 1 0 3 [1, 2]
129133
130134effect :: Bool
131135effect =
132136 let unf = UF.effect (modify (+ 1) >> get)
133− in testUnfoldMD unf 0 1 undefined [1]
137+ in testUnfoldMD unf undefined 0 1 [1]
134138
135139singletonM :: Bool
136140singletonM =
137141 let unf = UF.singletonM (\a -> modify (+ a) >> get)
138− in testUnfoldMD unf 0 1 1 [1]
142+ in testUnfoldMD unf 1 0 1 [1]
139143
140144const :: Bool
141145const =
142146 let unf = UF.take 10 $ UF.const (modify (+ 1) >> get)
143− in testUnfoldMD unf 0 10 (0 :: Int) [1 .. 10]
147+ in testUnfoldMD unf (0 :: Int) 0 10 [1 .. 10]
144148
145149unfoldrM :: Property
146150unfoldrM =
@@ -152,14 +156,14 @@ unfoldrM =
152156 unf = UF.take 100 $ UF.unfoldrM genM
153157 ll = length list
154158 fs = if ll < 100 then ll + 1 else 100
155− in testUnfoldMD unf 0 fs 1 list
159+ in testUnfoldMD unf 1 0 fs list
156160
157161fromListM :: Property
158162fromListM =
159163 property
160164 $ \n ->
161165 let lst = Prelude.map (\x -> modify (+ 1) >> return x) n
162− in testUnfoldMD UF.fromListM 0 (length n) lst n
166+ in testUnfoldMD UF.fromListM lst 0 (length n) n
163167
164168replicateM :: Property
165169replicateM =
@@ -167,11 +171,11 @@ replicateM =
167171 $ \i ->
168172 let ns = max 0 i
169173 seed = modify (+ 1) >> get
170− in testUnfoldMD (UF.replicateM i) 0 ns seed [1 .. i]
174+ in testUnfoldMD (UF.replicateM i) seed 0 ns [1 .. i]
171175
172176repeatM :: Bool
173177repeatM =
174− testUnfoldMD (UF.take 10 UF.repeatM) 0 10 (modify (+ 1) >> get) [1 .. 10]
178+ testUnfoldMD (UF.take 10 UF.repeatM) (modify (+ 1) >> get) 0 10 [1 .. 10]
175179
176180iterateM :: Property
177181iterateM =
@@ -181,7 +185,7 @@ iterateM =
181185 nextM x = modify (+ 1) >> return (nextA x)
182186 list = Prelude.take 100 $ List.iterate nextA 1
183187 unf = UF.take 100 $ UF.iterateM nextM
184− in testUnfoldMD unf 0 110 (modify (+ 10) >> return 1) list
188+ in testUnfoldMD unf (modify (+ 10) >> return 1) 0 110 list
185189
186190fromIndicesM :: Property
187191fromIndicesM =
@@ -191,7 +195,7 @@ fromIndicesM =
191195 indFM x = modify (+ 1) >> return (indFA x)
192196 list = Prelude.take 100 $ Prelude.map indFA [1 ..]
193197 unf = UF.take 100 $ UF.fromIndicesM indFM
194− in testUnfoldMD unf 0 (length list) 1 list
198+ in testUnfoldMD unf 1 0 (length list) list
195199
196200enumerateFromStepNum :: Property
197201enumerateFromStepNum =
@@ -238,7 +242,7 @@ mapM =
238242 fM x = modify (+ 1) >> return (fA x)
239243 unf = UF.mapM fM UF.fromList
240244 mList = Prelude.map fA list
241− in testUnfoldMD unf 0 (length list) list mList
245+ in testUnfoldMD unf list 0 (length list) mList
242246
243247mapMWithInput :: Property
244248mapMWithInput =
@@ -248,7 +252,7 @@ mapMWithInput =
248252 fM x y = modify (+ 1) >> return (fA x y)
249253 unf = UF.mapMWithInput fM UF.fromList
250254 mList = Prelude.map (fA list) list
251− in testUnfoldMD unf 0 (length list) list mList
255+ in testUnfoldMD unf list 0 (length list) mList
252256
253257take :: Property
254258take =
@@ -270,7 +274,7 @@ takeWhileM =
270274 unf = UF.takeWhileM fM UF.fromList
271275 fL = Prelude.takeWhile (apply f) n
272276 fS = Prelude.length fL
273− in testUnfoldMD unf 0 fS n fL
277+ in testUnfoldMD unf n 0 fS fL
274278
275279filterM :: Property
276280filterM =
@@ -283,7 +287,7 @@ filterM =
283287 unf = UF.filterM fM UF.fromList
284288 fL = Prelude.filter (apply f) n
285289 fS = Prelude.length fL
286− in testUnfoldMD unf 0 fS n fL
290+ in testUnfoldMD unf n 0 fS fL
287291
288292drop :: Property
289293drop =
@@ -304,7 +308,7 @@ dropWhileM =
304308 unf = UF.dropWhileM fM UF.fromList
305309 fL = Prelude.dropWhile (apply f) n
306310 fS = Prelude.length n - Prelude.length fL
307− in testUnfoldMD unf 0 fS n fL
311+ in testUnfoldMD unf n 0 fS fL
308312
309313-------------------------------------------------------------------------------
310314-- Stream combination
@@ -320,7 +324,7 @@ zipWithM =
320324 fM a b = modify (+ 1) >> return (fA a b)
321325 unf = UF.zipWithM fM unf1 unf2
322326 lst = Prelude.zipWith fA [1 .. 10] [1 .. 20]
323− in testUnfoldMD unf 0 10 (1, 1) lst
327+ in testUnfoldMD unf (1, 1) 0 10 lst
324328
325329concat :: Bool
326330concat =
@@ -352,7 +356,7 @@ concatMapM =
352356 unfOut = UF.enumerateFromToIntegral 10
353357 unf = UF.concatMapM unfInF unfOut
354358 list = List.concatMap listInF [1 .. 10]
355− in testUnfoldMD unf 0 110 1 list
359+ in testUnfoldMD unf 1 0 110 list
356360
357361-------------------------------------------------------------------------------
358362-- Test groups