on master help sign in
Refactor and reformat readChunksFromToWith
commit932f7b26f0f049e07d9434609931c962fce88a77 authorHarendra Kumar <harendra@composewell.com> dateThu 9 Sep 2021, 09:56 (5 years ago) parentdebc6a9
3 files changed+45−43
src/Streamly/FileSystem/Handle.hs−1 src/Streamly/Internal/FileSystem/Handle.hs+11−11 test/Streamly/Test/FileSystem/Handle.hs+34−31
src/Streamly/FileSystem/Handle.hs−1
@@ -79,7 +79,6 @@ module Streamly.FileSystem.Handle
7979 , readWithBufferOf
8080 , readChunks
8181 , readChunksWithBufferOf
82− , readChunksFromToWith
8382
8483 -- ** Writing
8584 -- | 'TextEncoding', 'NewLineMode', and 'Buffering' options of the
src/Streamly/Internal/FileSystem/Handle.hs+11−11
@@ -98,6 +98,7 @@ module Streamly.Internal.FileSystem.Handle
9898 )
9999where
100100
101+import Control.Exception (assert)
101102import Control.Monad.IO.Class (MonadIO(..))
102103import Data.Word (Word8)
103104import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
@@ -252,6 +253,8 @@ readChunksWithBufferOf = Unfold step return
252253-- the array lengths and trim the last array to correct size.
253254-- 2. Simply implement it from scratch like readChunksWithBufferOf.
254255--
256+-- XXX Change this to readChunksWithFromTo (bufferSize, from, to, h)?
257+--
255258-- | The input to the unfold is @(from, to, bufferSize, handle)@. It starts
256259-- reading from the offset `from` in the file and reads up to the offset `to`.
257260--
@@ -263,26 +266,23 @@ readChunksFromToWith = Unfold step inject
263266
264267 where
265268
266− inject (from, to, buffSize, h) = do
269+ inject (from, to, bufSize, h) = do
267270 liftIO $ hSeek h AbsoluteSeek $ fromIntegral from
268− return (to - from + 1, buffSize, h)
271+ -- XXX Use a strict Tuple?
272+ return (to - from + 1, bufSize, h)
269273
270274 {-# INLINE_LATE step #-}
271− step (remaining, buffSize, h) =
275+ step (remaining, bufSize, h) =
272276 if remaining <= 0
273277 then return D.Stop
274− else
275− do
276− arr <- getChunk (min buffSize remaining) h
278+ else do
279+ arr <- getChunk (min bufSize remaining) h
277280 return $
278281 case A.length arr of
279282 0 -> D.Stop
280283 len ->
281− if (remaining - len) >= 0
282− then
283− D.Yield arr (remaining - len, buffSize, h)
284− else
285− error "Bug: getChunk returned array length more than expected"
284+ assert (len <= remaining)
285+ $ D.Yield arr (remaining - len, bufSize, h)
286286
287287-- XXX read 'Array a' instead of Word8
288288--
test/Streamly/Test/FileSystem/Handle.hs+34−31
@@ -27,7 +27,7 @@ import Test.QuickCheck (Property, forAll, Gen, vectorOf, choose)
2727import Test.QuickCheck.Monadic (monadicIO, assert, run)
2828
2929import qualified Streamly.Data.Fold as Fold
30−import qualified Streamly.FileSystem.Handle as Handle
30+import qualified Streamly.Internal.FileSystem.Handle as Handle
3131import qualified Streamly.Internal.Data.Stream.IsStream as Stream
3232import qualified Streamly.Internal.Data.Array.Foreign as Array
3333import qualified Streamly.Internal.Unicode.Stream as Unicode
@@ -155,52 +155,48 @@ testWriteWithChunk =
155155 let arr = Array.fromList ls
156156 return (testDataLarge == utf8ToString arr)
157157
158+testReadChunksFromToWith :: Int -> Int -> Int -> [[Word8]] -> Property
159+testReadChunksFromToWith from to buffSize res = monadicIO $ run go
158160
159−testReadChunksFromToWith :: Int -> Int -> Int -> String -> Property
160−testReadChunksFromToWith from to buffSize expRes =
161− monadicIO $ do
162− res <- run go
163− assert res
164−
165− where
161+ where
166162
167− go =
168− withSystemTempDirectory "fs_handle" $ \fp -> do
169− let fpathRead = fp </> "tmp_read.txt"
170− writeFile fpathRead testBinData
171− h <- openFile fpathRead ReadMode
172− ls <- Stream.toList $
173− Stream.unfold
174− Handle.readChunksFromToWith
175− (from, to, buffSize, h)
176− return ( expRes == show ls)
163+ go =
164+ withSystemTempDirectory "fs_handle" $ \fp -> do
165+ let fpathRead = fp </> "tmp_read.txt"
166+ writeFile fpathRead testBinData
167+ h <- openFile fpathRead ReadMode
168+ ls <-
169+ Stream.toList
170+ $ Stream.unfold
171+ Handle.readChunksFromToWith (from, to, buffSize, h)
172+ return (res `shouldBe` fmap Array.toList ls)
177173
178174-- Test for first byte
179175testReadChunksFromToWithFirstByte :: Property
180−testReadChunksFromToWithFirstByte = testReadChunksFromToWith 0 0 15 "[[48]]"
176+testReadChunksFromToWithFirstByte = testReadChunksFromToWith 0 0 15 [[48]]
181177
182178-- Test for second byte
183179testReadChunksFromToWithSecondByte :: Property
184−testReadChunksFromToWithSecondByte = testReadChunksFromToWith 1 1 15 "[[49]]"
180+testReadChunksFromToWithSecondByte = testReadChunksFromToWith 1 1 15 [[49]]
185181
186182-- Test for second to 10th bytes
187183testReadChunksFromToWithSecondToTenthBytes :: Property
188184testReadChunksFromToWithSecondToTenthBytes =
189− testReadChunksFromToWith 1 10 15 "[[49,50,51,52,53,54,55,56,57,48]]"
185+ testReadChunksFromToWith 1 10 15 [[49,50,51,52,53,54,55,56,57,48]]
190186
191187-- Test for offset of buffer size
192188testReadChunksFromToWithBuffSizeOffset :: Property
193189testReadChunksFromToWithBuffSizeOffset =
194− testReadChunksFromToWith 15 25 15 "[[53,54,55,56,57,48,49,50,51,52,53]]"
190+ testReadChunksFromToWith 15 25 15 [[53,54,55,56,57,48,49,50,51,52,53]]
195191
196192-- Test with multi buffer size
197193testReadChunksFromToWithMultiBuff :: Property
198194testReadChunksFromToWithMultiBuff =
199195 testReadChunksFromToWith
200− 5 22 5 "[[53,54,55,56,57],[48,49,50,51,52],[53,54,55,56,57],[48,49,50]]"
196+ 5 22 5 [[53,54,55,56,57],[48,49,50,51,52],[53,54,55,56,57],[48,49,50]]
201197
202198testReadChunksFromToWithRangeInvalid :: Property
203−testReadChunksFromToWithRangeInvalid = testReadChunksFromToWith 15 5 15 "[]"
199+testReadChunksFromToWithRangeInvalid = testReadChunksFromToWith 15 5 15 []
204200
205201moduleName :: String
206202moduleName = "FileSystem.Handle"
@@ -216,14 +212,21 @@ main =
216212 prop "readWithBufferOf" $ testRead readWithBufferFromHandle
217213 prop "readChunks" $ testRead readChunksFromHandle
218214 prop "readChunksWithBufferOf" $ testRead readChunksWithBuffer
219− prop "readChunksFromToWithFirstByte" testReadChunksFromToWithFirstByte
220− prop "readChunksFromToWithSecondByte" testReadChunksFromToWithSecondByte
221− prop "readChunksFromToWithSecondToTenthBytes" testReadChunksFromToWithSecondToTenthBytes
222− prop "readChunksFromToWithBuffSizeOffset" testReadChunksFromToWithBuffSizeOffset
223− prop "readChunksFromToWithMultiBuff" testReadChunksFromToWithMultiBuff
224− prop "readChunksFromToWithRangeInvalid" testReadChunksFromToWithRangeInvalid
215+ prop "readChunksFromToWith (0,0,n)"
216+ testReadChunksFromToWithFirstByte
217+ prop "readChunksFromToWith (1,1,n)"
218+ testReadChunksFromToWithSecondByte
219+ prop "readChunksFromToWith (1,10,n)"
220+ testReadChunksFromToWithSecondToTenthBytes
221+ prop "readChunksFromToWith (n,<2n,n)"
222+ testReadChunksFromToWithBuffSizeOffset
223+ prop "readChunksFromToWith (n,>2n,n)"
224+ testReadChunksFromToWithMultiBuff
225+ prop "readChunksFromToWith (n,<n,n)"
226+ testReadChunksFromToWithRangeInvalid
225227 describe "Write To Handle" $ do
226228 prop "write" $ testWrite Handle.write
227− prop "writeWithBufferOf" $ testWrite $ Handle.writeWithBufferOf 1024
229+ prop "writeWithBufferOf"
230+ $ testWrite $ Handle.writeWithBufferOf 1024
228231 -- XXX This test needs a lot of stack when built with -O0
229232 prop "writeChunks" testWriteWithChunk