on master help sign in
Refactor, cleanup, hlint - fold and handle tests
commitc3b506b68b477294940bdda8c83010b9e033c7ea authorHarendra Kumar <harendra@composewell.com> dateMon 4 Jan 2021, 01:03 (6 years ago) parent0bd1586
6 files changed+89−82
.hlint.ignore−1 bin/targets.sh+1−1 hie.yaml+2 test/Streamly/Test/Common/Array.hs+2−2 test/Streamly/Test/Data/Fold.hs+45−42 test/Streamly/Test/FileSystem/Handle.hs+39−36
.hlint.ignore−1
@@ -26,7 +26,6 @@ test/Streamly/Test/Data/Array.hs
2626test/Streamly/Test/Data/Array/Prim.hs
2727test/Streamly/Test/Data/Array/Prim/Pinned.hs
2828test/Streamly/Test/Data/Array/Storable/Foreign.hs
29−test/Streamly/Test/Data/Fold.hs
3029test/Streamly/Test/Data/Parser.hs
3130test/Streamly/Test/Data/Parser/ParserD.hs
3231test/Streamly/Test/Data/SmallArray.hs
bin/targets.sh+1−1
@@ -105,6 +105,6 @@ GROUP_TARGETS="\
105105INDIVIDUAL_TARGETS="\
106106 Data.Unfold \
107107 Unicode.Stream \
108− `bench_only FileSystem.Handle` \
108+ FileSystem.Handle \
109109 `test_only FileSystem.Event` \
110110 `test_only version-bounds`"
hie.yaml+2
@@ -80,6 +80,8 @@ cradle:
8080 component: "test:Unicode.Stream"
8181 - path: "./test/Streamly/Test/FileSystem/Event.hs"
8282 component: "test:FileSystem.Event"
83+ - path: "./test/Streamly/Test/FileSystem/Handle.hs"
84+ component: "test:FileSystem.Handle"
8385 - path: "./test/version-bounds.hs"
8486 component: "test:version-bounds"
8587
test/Streamly/Test/Common/Array.hs+2−2
@@ -219,13 +219,13 @@ main =
219219 prop "toStreamRev . writeN === reverse" testFoldNToStreamRev
220220 prop "read . fromStreamN === id" testFromStreamNUnfold
221221 prop "toStream . fromStreamN === id" testFromStreamNToStream
222− prop "First N elements of a list" testFromListN
222+ prop "fromListN" testFromListN
223223
224224#ifndef TEST_SMALL_ARRAY
225225 prop "length . fromStream === n" testLengthFromStream
226226 prop "toStream . fromStream === id" testFromStreamToStream
227227 prop "read . write === id" testFoldUnfold
228− prop "From a list" testFromList
228+ prop "fromList" testFromList
229229#endif
230230
231231#if defined(TEST_ARRAY) ||\
test/Streamly/Test/Data/Fold.hs+45−42
@@ -1,8 +1,7 @@
11module Main (main) where
22
3−import Prelude hiding
4− (maximum, minimum, elem, notElem, null, product, sum, head, last)
53import Data.Semigroup (Sum(..), getSum)
4+import Streamly.Test.Common (checkListEqual)
65import Test.QuickCheck
76 ( Gen
87 , Property
@@ -15,15 +14,15 @@ import Test.QuickCheck
1514 , vectorOf
1615 , withMaxSuccess
1716 )
18−import Prelude hiding
19− (maximum, minimum, elem, notElem, null, product, sum, head, last)
20−import Streamly.Test.Common (checkListEqual)
2117import Test.QuickCheck.Monadic (monadicIO, assert, run)
2218
19+import qualified Prelude
2320import qualified Streamly.Internal.Data.Fold as F
2421import qualified Streamly.Prelude as S
2522import qualified Streamly.Data.Fold as FL
2623
24+import Prelude hiding
25+ (maximum, minimum, elem, notElem, null, product, sum, head, last)
2726import Test.Hspec as H
2827import Test.Hspec.QuickCheck
2928
@@ -70,10 +69,11 @@ length :: [Int] -> Expectation
7069length ls = S.fold FL.length (S.fromList ls) `shouldReturn` Prelude.length ls
7170
7271sum :: [Int] -> Expectation
73−sum ls = S.fold FL.sum (S.fromList ls) `shouldReturn` foldl (+) 0 ls
72+sum ls = S.fold FL.sum (S.fromList ls) `shouldReturn` Prelude.sum ls
7473
7574product :: [Int] -> Expectation
76−product ls = S.fold FL.product (S.fromList ls) `shouldReturn` foldl (*) 1 ls
75+product ls =
76+ S.fold FL.product (S.fromList ls) `shouldReturn` Prelude.product ls
7777
7878lesser :: (a -> a -> Ordering) -> a -> a -> a
7979lesser f x y = if f x y == LT then x else y
@@ -90,8 +90,7 @@ foldMaybe f acc ls =
9090maximumBy :: (Ord a, Show a) => a -> (a -> a -> Ordering) -> [a] -> Expectation
9191maximumBy genmin f ls =
9292 S.fold (FL.maximumBy f) (S.fromList ls)
93− `shouldReturn`
94− foldMaybe (greater f) genmin ls
93+ `shouldReturn` foldMaybe (greater f) genmin ls
9594
9695maximum :: (Show a, Ord a) => a -> [a] -> Expectation
9796maximum genmin ls =
@@ -129,16 +128,16 @@ mapMaybe ls =
129128 in S.fold f (S.fromList ls) `shouldReturn` filter even ls
130129
131130nth :: Int -> [a] -> Maybe a
132−nth idx (x:xs) = if idx == 0
133− then Just x
134− else if idx < 0
135− then Nothing
136− else nth (idx - 1) xs
131+nth idx (x : xs)
132+ | idx == 0 = Just x
133+ | idx < 0 = Nothing
134+ | otherwise = nth (idx - 1) xs
137135nth _ [] = Nothing
138136
139137index :: Int -> [String] -> Expectation
140−index idx ls = let x = S.fold (FL.index idx) (S.fromList ls)
141− in x `shouldReturn` (nth idx ls)
138+index idx ls =
139+ let x = S.fold (FL.index idx) (S.fromList ls)
140+ in x `shouldReturn` nth idx ls
142141
143142find :: (Show a, Eq a) => (a -> Bool) -> [a] -> Expectation
144143find f ls = do
@@ -152,7 +151,7 @@ find f ls = do
152151 in fld `shouldReturn` False
153152
154153neg :: (a -> Bool) -> a -> Bool
155−neg f x = if f x == True then False else True
154+neg f x = not (f x)
156155
157156findIndex :: (a -> Bool) -> [a] -> Expectation
158157findIndex f ls = do
@@ -161,16 +160,16 @@ findIndex f ls = do
161160 Nothing ->
162161 let fld = S.fold (FL.all $ neg f) (S.fromList ls)
163162 in fld `shouldReturn` True
164− Just idx -> if idx == 0
165− then
166− S.fold (FL.all f) (S.fromList []) `shouldReturn` True
167− else
168− S.fold (FL.all f) (S.fromList $ (take idx ls))
169− `shouldReturn`
170− False
163+ Just idx ->
164+ if idx == 0
165+ then
166+ S.fold (FL.all f) (S.fromList []) `shouldReturn` True
167+ else
168+ S.fold (FL.all f) (S.fromList $ take idx ls)
169+ `shouldReturn` False
171170
172171predicate :: Int -> Bool
173−predicate x = if x * x < 100 then True else False
172+predicate x = x * x < 100
174173
175174elemIndex :: Int -> [Int] -> Expectation
176175elemIndex elm ls = do
@@ -180,13 +179,17 @@ elemIndex elm ls = do
180179 let fld = S.fold (FL.any (== elm)) (S.fromList ls)
181180 in fld `shouldReturn` False
182181 Just idx ->
183− let fld = S.fold (FL.any (== elm)) (S.fromList $ (take idx ls))
182+ let fld = S.fold (FL.any (== elm)) (S.fromList $ take idx ls)
184183 in fld `shouldReturn` False
185184
186185null :: [Int] -> Expectation
187−null ls = S.fold FL.null (S.fromList ls) `shouldReturn` case ls of
188− [] -> True
189− _ -> False
186+null ls =
187+ S.fold FL.null (S.fromList ls)
188+ `shouldReturn`
189+ case ls of
190+ [] -> True
191+ _ -> False
192+
190193elem :: Int -> [Int] -> Expectation
191194elem elm ls = do
192195 y <- S.fold (FL.elem elm) (S.fromList ls)
@@ -201,7 +204,7 @@ notElem elm ls = do
201204
202205all :: (a -> Bool) -> [a] -> Expectation
203206all f ls =
204− S.fold (FL.all f) (S.fromList ls) `shouldReturn` Prelude.and (map f ls)
207+ S.fold (FL.all f) (S.fromList ls) `shouldReturn` Prelude.all f ls
205208
206209any :: (a -> Bool) -> [a] -> Expectation
207210any f ls = S.fold (FL.any f) (S.fromList ls) `shouldReturn` Prelude.any f ls
@@ -257,7 +260,7 @@ mean =
257260
258261 action ls = do
259262 v1 <- run $ S.fold FL.mean (S.fromList ls)
260− let v2 = (foldl (+) 0 ls) / fromIntegral (Prelude.length ls)
263+ let v2 = Prelude.sum ls / fromIntegral (Prelude.length ls)
261264 assert (abs (v1 - v2) < 0.0001)
262265
263266stdDev :: Property
@@ -269,8 +272,8 @@ stdDev =
269272
270273 action ls = do
271274 v1 <- run $ S.fold FL.stdDev (S.fromList ls)
272− let avg = (foldl (+) 0 ls) / fromIntegral (Prelude.length ls)
273− se = (foldl (+) 0 (map (\x -> (x - avg) * (x - avg)) ls))
275+ let avg = Prelude.sum ls / fromIntegral (Prelude.length ls)
276+ se = Prelude.sum (fmap (\x -> (x - avg) * (x - avg)) ls)
274277 sd = sqrt $ se / fromIntegral (Prelude.length ls)
275278 assert (abs (v1 - sd) < 0.0001 )
276279
@@ -283,8 +286,8 @@ variance =
283286
284287 action ls = do
285288 v1 <- run $ S.fold FL.variance (S.fromList ls)
286− let avg = (foldl (+) 0 ls) / fromIntegral (Prelude.length ls)
287− se = (foldl (+) 0 (map (\x -> (x - avg) * (x - avg)) ls))
289+ let avg = Prelude.sum ls / fromIntegral (Prelude.length ls)
290+ se = Prelude.sum (fmap (\x -> (x - avg) * (x - avg)) ls)
288291 vr = se / fromIntegral (Prelude.length ls)
289292 assert (abs (v1 - vr) < 0.01 )
290293
@@ -297,7 +300,7 @@ mconcat =
297300
298301 action ls = do
299302 v1 <- run $ S.fold FL.mconcat (S.map Sum $ S.fromList ls)
300− let v2 = (foldl (+) 0 ls)
303+ let v2 = Prelude.sum ls
301304 assert (getSum v1 == v2)
302305
303306foldMap :: Property
@@ -309,7 +312,7 @@ foldMap =
309312
310313 action ls = do
311314 v1 <- run $ S.fold (FL.foldMap Sum) $ S.fromList ls
312− let v2 = (foldl (+) 0 ls)
315+ let v2 = Prelude.sum ls
313316 assert (getSum v1 == v2)
314317
315318foldMapM :: Property
@@ -321,7 +324,7 @@ foldMapM =
321324
322325 action ls = do
323326 v1 <- run $ S.fold (FL.foldMapM (return . Sum)) $ S.fromList ls
324− let v2 = (foldl (+) 0 ls)
327+ let v2 = Prelude.sum ls
325328 assert (getSum v1 == v2)
326329
327330lookup :: Property
@@ -363,7 +366,7 @@ teeWithLength =
363366
364367 action ls = do
365368 v1 <- run $ S.fold (FL.tee FL.sum FL.length) $ S.fromList ls
366− let v2 = foldl (+) 0 ls
369+ let v2 = Prelude.sum ls
367370 v3 = Prelude.length ls
368371 assert (v1 == (v2, v3))
369372
@@ -376,7 +379,7 @@ teeWithMax =
376379
377380 action ls = do
378381 v1 <- run $ S.fold (FL.tee FL.sum FL.maximum) $ S.fromList ls
379− let v2 = foldl (+) 0 ls
382+ let v2 = Prelude.sum ls
380383 v3 = foldMaybe (greater compare) intMin ls
381384 assert (v1 == (v2, v3))
382385
@@ -389,7 +392,7 @@ distribute =
389392
390393 action ls = do
391394 v1 <- run $ S.fold (FL.distribute [FL.sum, FL.length]) $ S.fromList ls
392− let v2 = foldl (+) 0 ls
395+ let v2 = Prelude.sum ls
393396 v3 = Prelude.length ls
394397 assert (v1 == [v2, v3])
395398
@@ -418,7 +421,7 @@ main :: IO ()
418421main = hspec $
419422 describe "Fold s" $ do
420423 prop "RollingHashFirstN" rollingHashFirstN
421− prop "Index" $ index
424+ prop "Index" index
422425 prop "Head" head
423426 prop "Last" last
424427 prop "Length" Main.length
test/Streamly/Test/FileSystem/Handle.hs+39−36
@@ -26,15 +26,15 @@ import System.IO.Temp (withSystemTempDirectory)
2626import Test.QuickCheck (Property, forAll, Gen, vectorOf, choose)
2727import Test.QuickCheck.Monadic (monadicIO, assert, run)
2828
29−import Streamly.FileSystem.Handle as FH
29+import qualified Streamly.Data.Fold as Fold
30+import qualified Streamly.FileSystem.Handle as Handle
31+import qualified Streamly.Internal.Data.Stream.IsStream as Stream
32+import qualified Streamly.Internal.Data.Array.Storable.Foreign as Array
33+import qualified Streamly.Internal.Unicode.Stream as Unicode
34+
3035import Test.Hspec as H
3136import Test.Hspec.QuickCheck
3237
33−import qualified Streamly.Data.Fold as FL
34−import qualified Streamly.Internal.Data.Stream.IsStream as S
35−import qualified Streamly.Internal.Data.Array.Storable.Foreign as A
36−import qualified Streamly.Internal.Unicode.Stream as U
37−
3838allocOverhead :: Int
3939allocOverhead = 2 * sizeOf (undefined :: Int)
4040
@@ -51,8 +51,9 @@ maxTestCount = 10
5151chooseWord8 :: (Word8, Word8) -> Gen Word8
5252chooseWord8 = choose
5353
54−utf8ToString :: A.Array Word8 -> String
55−utf8ToString = runIdentity . S.toList . U.decodeUtf8' . A.toStream
54+utf8ToString :: Array.Array Word8 -> String
55+utf8ToString =
56+ runIdentity . Stream.toList . Unicode.decodeUtf8' . Array.toStream
5657
5758testData :: String
5859testData = "This is the test data for FileSystem.Handle ??`!@#$%^&*~~))`]"
@@ -60,7 +61,7 @@ testData = "This is the test data for FileSystem.Handle ??`!@#$%^&*~~))`]"
6061testDataLarge :: String
6162testDataLarge = concat $ replicate 6000 testData
6263
63−executor :: (Handle -> (SerialT IO Char)) -> IO (SerialT IO Char)
64+executor :: (Handle -> SerialT IO Char) -> IO (SerialT IO Char)
6465executor f =
6566 withSystemTempDirectory "fs_handle" $ \fp -> do
6667 let fpath = fp </> "tmp_read.txt"
@@ -70,38 +71,40 @@ executor f =
7071
7172readFromHandle :: IO (SerialT IO Char)
7273readFromHandle =
73− let f = U.decodeUtf8 . S.unfold FH.read
74+ let f = Unicode.decodeUtf8 . Stream.unfold Handle.read
7475 in executor f
7576
7677readWithBufferFromHandle :: IO (SerialT IO Char)
7778readWithBufferFromHandle =
7879 let f1 = (\h -> (1024, h))
79− f2 = U.decodeUtf8 . S.unfold FH.readWithBufferOf . f1
80+ f2 = Unicode.decodeUtf8 . Stream.unfold Handle.readWithBufferOf . f1
8081 in executor f2
8182
8283readChunksFromHandle :: IO (SerialT IO Char)
8384readChunksFromHandle =
84− let f = U.decodeUtf8 . S.concatMap (A.toStream) . S.unfold FH.readChunks
85+ let f = Unicode.decodeUtf8
86+ . Stream.concatMap Array.toStream
87+ . Stream.unfold Handle.readChunks
8588 in executor f
8689
8790readChunksWithBuffer :: IO (SerialT IO Char)
8891readChunksWithBuffer =
8992 let f1 = (\h -> (1024, h))
9093 f2 =
91− U.decodeUtf8
92− . S.concatMap (A.toStream)
93− . S.unfold FH.readChunksWithBufferOf
94+ Unicode.decodeUtf8
95+ . Stream.concatMap Array.toStream
96+ . Stream.unfold Handle.readChunksWithBufferOf
9497 . f1
9598 in executor f2
9699
97100testRead :: (IsStream t) => IO (t IO Char) -> Property
98101testRead fn = monadicIO $ do
99− let v2 = (S.fromList testDataLarge)
100− v1 <- run $ fn
101− res <- run $ S.eqBy (==) v1 v2
102− assert (res)
102+ let v2 = Stream.fromList testDataLarge
103+ v1 <- run fn
104+ res <- run $ Stream.eqBy (==) v1 v2
105+ assert res
103106
104−testWrite :: (Handle -> FL.Fold IO Word8 ()) -> Property
107+testWrite :: (Handle -> Fold.Fold IO Word8 ()) -> Property
105108testWrite hfold =
106109 forAll (choose (0, maxArrLen)) $ \len ->
107110 forAll (vectorOf len $ chooseWord8 (0, 255)) $ \list0 ->
@@ -117,17 +120,17 @@ testWrite hfold =
117120 writeFile fpathWrite ""
118121 h <- openFile fpathWrite ReadWriteMode
119122 hSeek h AbsoluteSeek 0
120− S.fold (hfold h) $ S.fromList list
123+ Stream.fold (hfold h) $ Stream.fromList list
121124 hFlush h
122125 hSeek h AbsoluteSeek 0
123− ls <- S.toList $ S.unfold FH.read h
126+ ls <- Stream.toList $ Stream.unfold Handle.read h
124127 hClose h
125128 return (ls == list)
126129
127130testWriteWithChunk :: Property
128131testWriteWithChunk =
129132 monadicIO $ do
130− res <- run $ go
133+ res <- run go
131134 assert res
132135
133136 where
@@ -141,12 +144,12 @@ testWriteWithChunk =
141144 hr <- openFile fpathRead ReadMode
142145 hw <- openFile fpathWrite ReadWriteMode
143146 hSeek hw AbsoluteSeek 0
144− S.fold (FH.writeChunks hw)
145− $ S.unfold FH.readChunksWithBufferOf (1024, hr)
147+ Stream.fold (Handle.writeChunks hw)
148+ $ Stream.unfold Handle.readChunksWithBufferOf (1024, hr)
146149 hFlush hw
147150 hSeek hw AbsoluteSeek 0
148− ls <- S.toList $ S.unfold FH.read hw
149− let arr = A.fromList ls
151+ ls <- Stream.toList $ Stream.unfold Handle.read hw
152+ let arr = Array.fromList ls
150153 return (testDataLarge == utf8ToString arr)
151154
152155main :: IO ()
@@ -154,12 +157,12 @@ main =
154157 hspec $
155158 H.parallel $
156159 modifyMaxSuccess (const maxTestCount) $ do
157− describe "Read" $ do
158− prop "testRead" $ testRead readFromHandle
159− prop "testReadWithBuffer" $ testRead readWithBufferFromHandle
160− prop "testReadChunks" $ testRead readChunksFromHandle
161− prop "testReadChunksWithBuffer" $ testRead readChunksWithBuffer
162− describe "Write" $ do
163− prop "testWrite" $ testWrite FH.write
164− prop "testWriteWithBufferOf" $ testWrite $ FH.writeWithBufferOf 1024
165− prop "testWriteWithChunk" $ testWriteWithChunk
160+ describe "Read From Handle" $ do
161+ prop "read" $ testRead readFromHandle
162+ prop "readWithBufferOf" $ testRead readWithBufferFromHandle
163+ prop "readChunks" $ testRead readChunksFromHandle
164+ prop "readChunksWithBufferOf" $ testRead readChunksWithBuffer
165+ describe "Write To Handle" $ do
166+ prop "write" $ testWrite Handle.write
167+ prop "writeWithBufferOf" $ testWrite $ Handle.writeWithBufferOf 1024
168+ prop "writeChunks" testWriteWithChunk