on master help sign in
Add tests to lastN
5aa8e44adithyaov
src/Streamly/Internal/Data/Stream/StreamD.hs+3−3
@@ -4028,11 +4028,11 @@ lastN n = Fold step initial done
4028 step (Tuple3' rb rh i) a = do
4028 step (Tuple3' rb rh i) a = do
4029 rh1 <- liftIO $ RB.unsafeInsert rb rh a
4029 rh1 <- liftIO $ RB.unsafeInsert rb rh a
4030 return $ Tuple3' rb rh1 (i + 1)
4030 return $ Tuple3' rb rh1 (i + 1)
4031− initial = fmap (\(a, b) -> Tuple3' a b 0) $ liftIO $ RB.new n
4031+ initial = fmap (\(a, b) -> Tuple3' a b 0) $ liftIO $ RB.new n
4032 done (Tuple3' rb rh i) = do
4032 done (Tuple3' rb rh i) = do
4033 arr <- liftIO $ A.newArray n
4033 arr <- liftIO $ A.newArray n
4034 foldFunc i rh snoc' arr rb
4034 foldFunc i rh snoc' arr rb
4035 snoc' b a = liftIO $ A.unsafeSnoc b a
4035 snoc' b a = liftIO $ A.unsafeSnoc b a
4036 foldFunc i
4036 foldFunc i
4037− | i < n = RB.unsafeFoldRingM
4037+ | i < n = RB.unsafeFoldRingM
4038− | otherwise = RB.unsafeFoldRingFullM
4038+ | otherwise = RB.unsafeFoldRingFullM
src/Streamly/Internal/Memory/Array/Types.hs+1−1
@@ -286,7 +286,7 @@ withNewArray count f = do
286--
286--
287-- Internal routine for when the array is being created. Appends one item at
287-- Internal routine for when the array is being created. Appends one item at
288-- the end of the array. Useful when sequentially writing a stream to the
288-- the end of the array. Useful when sequentially writing a stream to the
289−-- array. DOES NOT CHECK THE ARRAY BOUNDS.
289+-- array.
290{-# INLINE unsafeSnoc #-}
290{-# INLINE unsafeSnoc #-}
291unsafeSnoc :: forall a. Storable a => Array a -> a -> IO (Array a)
291unsafeSnoc :: forall a. Storable a => Array a -> a -> IO (Array a)
292unsafeSnoc arr@Array{..} x = do
292unsafeSnoc arr@Array{..} x = do
test/Streamly/Test/Array.hs+31−13
@@ -132,26 +132,44 @@ testArraysOf =
132 $ IP.arraysOf 240
132 $ IP.arraysOf 240
133 $ S.fromList list
133 $ S.fromList list
134 assert (xs == list)
134 assert (xs == list)
135+
136+lastN :: Int -> [a] -> [a]
137+lastN n l = drop (length l - n) l
138+
139+testLastN :: Property
140+testLastN =
141+ forAll (choose (0, maxArrLen)) $ \len ->
142+ forAll (choose (0, len)) $ \n ->
143+ forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
144+ monadicIO $ do
145+ xs <- fmap A.toList
146+ $ S.fold (A.lastN n)
147+ $ S.fromList list
148+ assert (xs == lastN n list)
135#endif
149#endif
136
150
137main :: IO ()
151main :: IO ()
138main =
152main =
139 hspec $
153 hspec $
140 H.parallel $
154 H.parallel $
141− modifyMaxSuccess (const maxTestCount) $
155+ modifyMaxSuccess (const maxTestCount) $ do
142− describe "Construction" $ do
156+ describe "Construction" $ do
143− prop "length . writeN n === n" testLength
157+ prop "length . writeN n === n" testLength
144− prop "length . fromStreamN n === n" testLengthFromStreamN
158+ prop "length . fromStreamN n === n" testLengthFromStreamN
145− prop "read . writeN === id " testFoldNUnfold
159+ prop "read . writeN === id " testFoldNUnfold
146− prop "toStream . writeN === id" testFoldNToStream
160+ prop "toStream . writeN === id" testFoldNToStream
147− prop "toStreamRev . writeN === reverse" testFoldNToStreamRev
161+ prop "toStreamRev . writeN === reverse" testFoldNToStreamRev
148− prop "read . fromStreamN === id" testFromStreamNUnfold
162+ prop "read . fromStreamN === id" testFromStreamNUnfold
149− prop "toStream . fromStreamN === id" testFromStreamNToStream
163+ prop "toStream . fromStreamN === id" testFromStreamNToStream
150#ifndef TEST_SMALL_ARRAY
164#ifndef TEST_SMALL_ARRAY
151− prop "length . fromStream === n" testLengthFromStream
165+ prop "length . fromStream === n" testLengthFromStream
152− prop "toStream . fromStream === id" testFromStreamToStream
166+ prop "toStream . fromStream === id" testFromStreamToStream
153− prop "read . write === id" testFoldUnfold
167+ prop "read . write === id" testFoldUnfold
168+#endif
169+#ifdef TEST_ARRAY
170+ prop "arraysOf concats to original" testArraysOf
154#endif
171#endif
155#ifdef TEST_ARRAY
172#ifdef TEST_ARRAY
156− prop "arraysOf concats to original" testArraysOf
173+ describe "Fold" $ do
174+ prop "lastN" $ testLastN
157#endif
175#endif