on master help sign in
Add test for foldl
commitc0f0b54f37f106115b5f5898860b04938e376041 authorHarendra Kumar <harendra.kumar@gmail.com> dateSat 10 Mar 2018, 17:21 (9 years ago) parent7daee5c
1 file changed+31−27
test/Main.hs+31−27
test/Main.hs+31−27
@@ -257,7 +257,7 @@ main = hspec $ do
257257 -- TBD combine all binds and all compose in one example
258258 describe "Miscellaneous combined examples" mixedOps
259259
260− describe "Transformation" $ transformOps
260+ describe "Stream Operations" $ streamOperations
261261 ((<>) :: StreamT IO Int -> StreamT IO Int -> StreamT IO Int)
262262 describe "Serial zipping" $
263263 zipOps A.zipWith A.zipWithM zipping
@@ -599,38 +599,42 @@ mixedOps = do
599599 return (x1 + y1 + z1)
600600 return (x + y + z)
601601
602−testListOp
603− :: (Streaming t, Monad (t IO))
604− => (t IO Int -> t IO Int -> t IO Int)
605− -> (t IO Int -> t IO Int)
606− -> ([Int] -> [Int])
607− -> Expectation
608−testListOp f streamOp listOp =
609− let list = [1..10]
610− stream = foldMapWith f return list
611− in (A.toList $ streamOp stream) `shouldReturn` listOp list
612−
613−transformOps
602+streamOperations
614603 :: (Streaming t, Monad (t IO))
615604 => (t IO Int -> t IO Int -> t IO Int) -> Spec
616−transformOps f = do
617− it "take all" $ testOp (A.take 10) (take 10)
618− it "take none" $ testOp (A.take 0) (take 0)
619− it "take 5" $ testOp (A.take 5) (take 5)
605+streamOperations f = do
606+
607+ -- Filtering
608+ it "take all" $ transform (A.take 10) (take 10)
609+ it "take none" $ transform (A.take 0) (take 0)
610+ it "take 5" $ transform (A.take 5) (take 5)
620611
621− it "drop all" $ testOp (A.drop 10) (drop 10)
622− it "drop none" $ testOp (A.drop 0) (drop 0)
623− it "drop 5" $ testOp (A.drop 5) (drop 5)
612+ it "drop all" $ transform (A.drop 10) (drop 10)
613+ it "drop none" $ transform (A.drop 0) (drop 0)
614+ it "drop 5" $ transform (A.drop 5) (drop 5)
624615
625− it "dropWhile true" $ testOp (A.dropWhile (const True))
616+ it "dropWhile true" $ transform (A.dropWhile (const True))
626617 (dropWhile (const True))
627− it "dropWhile false" $ testOp (A.dropWhile (const False))
618+ it "dropWhile false" $ transform (A.dropWhile (const False))
628619 (dropWhile (const False))
629− it "dropWhile < 5" $ testOp (A.dropWhile (< 5)) (dropWhile (< 5))
620+ it "dropWhile < 5" $ transform (A.dropWhile (< 5)) (dropWhile (< 5))
630621
631− it "filter all out" $ testOp (A.filter (> 10)) (filter (> 10))
632− it "filter all in" $ testOp (A.filter (<= 10)) (filter (<= 10))
633− it "filter even" $ testOp (A.filter even) (filter even)
622+ it "filter all out" $ transform (A.filter (> 10)) (filter (> 10))
623+ it "filter all in" $ transform (A.filter (<= 10)) (filter (<= 10))
624+ it "filter even" $ transform (A.filter even) (filter even)
625+
626+ -- Elimination
627+ it "foldl" $ elimination (A.foldl (+) 0 id) (foldl (+) 0)
634628
635629 where
636− testOp = testListOp f
630+
631+ -- XXX run on empty stream as well
632+ transform streamOp listOp =
633+ let list = [1..10]
634+ stream = foldMapWith f return list
635+ in (A.toList $ streamOp stream) `shouldReturn` listOp list
636+
637+ elimination streamOp listOp =
638+ let list = [1..10]
639+ stream = foldMapWith f return list
640+ in (streamOp stream) `shouldReturn` listOp list