on master help sign in
Move monoidOps to Prelude from test/Main.hs
commit4d5ab4e0f035484a5279cdb2d3ee2c60b3544c8c authorPranay Sashank <pranaysashank@composewell.com> dateWed 29 Jul 2020, 14:50 (6 years ago) parentf1b0795
10 files changed+89−66
test/Main.hs−66 test/Streamly/Test/Prelude.hs+47 test/Streamly/Test/Prelude/Ahead.hs+3 test/Streamly/Test/Prelude/Async.hs+3 test/Streamly/Test/Prelude/Parallel.hs+3 test/Streamly/Test/Prelude/Serial.hs+21 test/Streamly/Test/Prelude/WAsync.hs+3 test/Streamly/Test/Prelude/WSerial.hs+3 test/Streamly/Test/Prelude/ZipAsync.hs+3 test/Streamly/Test/Prelude/ZipSerial.hs+3
test/Main.hs−66
@@ -35,9 +35,6 @@ toListInterleaved = S.toList . wSerially
3535toListAsync :: AsyncT IO a -> IO [a]
3636toListAsync = S.toList . asyncly
3737
38−toListParallel :: WAsyncT IO a -> IO [a]
39−toListParallel = S.toList . wAsyncly
40−
4138main :: IO ()
4239main = hspec $ do
4340 parallelTests
@@ -189,36 +186,6 @@ testFoldOpsCleanup name f = do
189186
190187parallelTests :: SpecWith ()
191188parallelTests = H.parallel $ do
192− describe "Runners" $ do
193− -- XXX move these to property tests
194− -- XXX use an IORef to store and check the side effects
195− it "simple serially" $
196− (S.drain . serially) (return (0 :: Int)) `shouldReturn` ()
197− it "simple serially with IO" $
198− (S.drain . serially) (S.yieldM $ putStrLn "hello") `shouldReturn` ()
199−
200− describe "Empty" $ -- do
201− it "Monoid - mempty" $
202− toListSerial mempty `shouldReturn` ([] :: [Int])
203− -- it "Alternative - empty" $
204− -- (toListSerial empty) `shouldReturn` ([] :: [Int])
205− -- it "MonadPlus - mzero" $
206− -- (toListSerial mzero) `shouldReturn` ([] :: [Int])
207−
208−
209− ---------------------------------------------------------------------------
210− -- Monoidal Compositions, multiset equality checks
211− ---------------------------------------------------------------------------
212−
213− describe "Serial Composition" $ compose serially mempty id
214− describe "Ahead Composition" $ compose aheadly mempty id
215− describe "WSerial Composition" $ compose wSerially mempty sort
216− describe "Async Composition" $ compose asyncly mempty sort
217− describe "WAsync Composition" $ compose wAsyncly mempty sort
218− describe "Parallel Composition" $ compose parallely mempty sort
219− describe "Semigroup Composition for ZipSerial" $ compose zipSerially mempty id
220− describe "Semigroup Composition for ZipAsync" $ compose zipAsyncly mempty id
221− -- XXX need to check alternative compositions as well
222189
223190 ---------------------------------------------------------------------------
224191 -- TBD Monoidal composition combinations
@@ -903,39 +870,6 @@ parallelCheck t f = do
903870
904871 where event n = S.yieldM (threadDelay (n * 200000)) >> return n
905872
906−compose :: (IsStream t, Semigroup (t IO Int))
907− => (t IO Int -> SerialT IO Int) -> t IO Int -> ([Int] -> [Int]) -> Spec
908−compose t z srt = do
909− -- XXX these should get covered by the property tests
910− it "Compose mempty, mempty" $
911− tl (z <> z) `shouldReturn` ([] :: [Int])
912− it "Compose empty at the beginning" $
913− tl (z <> singleton 1) `shouldReturn` [1]
914− it "Compose empty at the end" $
915− tl (singleton 1 <> z) `shouldReturn` [1]
916− it "Compose two" $
917− srt <$> tl (singleton 0 <> singleton 1)
918− `shouldReturn` [0, 1]
919− it "Compose many" $
920− srt <$> tl (S.forEachWith (<>) [1..100] singleton)
921− `shouldReturn` [1..100]
922−
923− -- These are not covered by the property tests
924− it "Compose three - empty in the middle" $
925− srt <$> tl (singleton 0 <> z <> singleton 1)
926− `shouldReturn` [0, 1]
927− it "Compose left associated" $
928− srt <$> tl (((singleton 0 <> singleton 1) <> singleton 2) <> singleton 3)
929− `shouldReturn` [0, 1, 2, 3]
930− it "Compose right associated" $
931− srt <$> tl (singleton 0 <> (singleton 1 <> (singleton 2 <> singleton 3)))
932− `shouldReturn` [0, 1, 2, 3]
933− it "Compose hierarchical (multiple levels)" $
934− srt <$> tl (((singleton 0 <> singleton 1) <> (singleton 2 <> singleton 3))
935− <> ((singleton 4 <> singleton 5) <> (singleton 6 <> singleton 7)))
936− `shouldReturn` [0..7]
937− where tl = S.toList . t
938−
939873composeAndComposeSimple
940874 :: ( IsStream t1, Semigroup (t1 IO Int)
941875 , IsStream t2, Monoid (t2 IO Int), Monad (t2 IO)
test/Streamly/Test/Prelude.hs+47
@@ -29,6 +29,8 @@ module Streamly.Test.Prelude
2929 , eliminationOps
3030 -- * Functor operations
3131 , functorOps
32+ -- * Monoid operations
33+ , monoidOps
3234 -- * Semigroup operations
3335 , semigroupOps
3436 -- * Transformation operations
@@ -482,6 +484,51 @@ transformFromList constr eq listOp op a =
482484 listEquals eq stream list
483485
484486
487+------------------------------------------------------------------------------
488+-- Monoid operations
489+------------------------------------------------------------------------------
490+
491+monoidOps
492+ :: (IsStream t, Semigroup (t IO Int))
493+ => String
494+ -> t IO Int
495+ -> ([Int] -> [Int] -> Bool)
496+ -> (t IO Int -> SerialT IO Int)
497+ -> Spec
498+monoidOps desc z eq t = do
499+ -- XXX these should get covered by the property tests
500+ prop (desc <> " Compose mempty, mempty") $ spec (z <> z) []
501+ prop (desc <> " Compose empty at the beginning") $ spec (z <> singleton 1) [1]
502+ prop (desc <> " Compose empty at the end") $ spec (singleton 1 <> z) [1]
503+ prop (desc <> " Compose two") $ spec (singleton 0 <> singleton 1) [0, 1]
504+ prop (desc <> " Compose many") $
505+ spec (S.forEachWith (<>) [1 .. 100] singleton) [1 .. 100]
506+
507+ -- These are not covered by the property tests
508+ prop (desc <> " Compose three - empty in the middle") $
509+ spec (singleton 0 <> z <> singleton 1) [0, 1]
510+ prop (desc <> " Compose left associated") $
511+ spec
512+ (((singleton 0 <> singleton 1) <> singleton 2) <> singleton 3)
513+ [0, 1, 2, 3]
514+ prop (desc <> " Compose right associated") $
515+ spec
516+ (singleton 0 <> (singleton 1 <> (singleton 2 <> singleton 3)))
517+ [0, 1, 2, 3]
518+ prop (desc <> " Compose hierarchical (multiple levels)") $
519+ spec
520+ (((singleton 0 <> singleton 1) <> (singleton 2 <> singleton 3)) <>
521+ ((singleton 4 <> singleton 5) <> (singleton 6 <> singleton 7)))
522+ [0 .. 7]
523+
524+ where
525+
526+ tl = S.toList . t
527+ spec s list =
528+ monadicIO $ do
529+ stream <- run $ tl s
530+ listEquals eq stream list
531+
485532-------------------------------------------------------------------------------
486533-- Semigroup operations
487534-------------------------------------------------------------------------------
test/Streamly/Test/Prelude/Ahead.hs+3
@@ -40,6 +40,9 @@ main = hspec
4040 aheadOps $ functorOps S.fromFoldable "aheadly" (==)
4141 aheadOps $ functorOps folded "aheadly folded" (==)
4242
43+ describe "Monoid operations" $ do
44+ aheadOps $ monoidOps "aheadly" mempty (==)
45+
4346 describe "Semigroup operations" $ do
4447 aheadOps $ semigroupOps "aheadly" (==)
4548
test/Streamly/Test/Prelude/Async.hs+3
@@ -40,6 +40,9 @@ main = hspec
4040 asyncOps $ functorOps S.fromFoldable "asyncly" sortEq
4141 asyncOps $ functorOps folded "asyncly folded" sortEq
4242
43+ describe "Monoid operations" $ do
44+ asyncOps $ monoidOps "asyncly" mempty sortEq
45+
4346 describe "Semigroup operations" $ do
4447 asyncOps $ semigroupOps "asyncly" sortEq
4548
test/Streamly/Test/Prelude/Parallel.hs+3
@@ -44,6 +44,9 @@ main = hspec
4444 parallelOps $ functorOps S.fromFoldable "parallely" sortEq
4545 parallelOps $ functorOps folded "parallely folded" sortEq
4646
47+ describe "Monoid operations" $ do
48+ parallelOps $ monoidOps "parallely" mempty sortEq
49+
4750 describe "Semigroup operations" $ do
4851 parallelOps $ semigroupOps "parallely" sortEq
4952
test/Streamly/Test/Prelude/Serial.hs+21
@@ -158,6 +158,23 @@ main = hspec
158158 <> [("rate AvgRate 0.00000001", serially . avgRate 0.00000001)]
159159 <> [("maxBuffer -1", serially . maxBuffer (-1))]
160160#endif
161+ let toListSerial :: SerialT IO a -> IO [a]
162+ toListSerial = S.toList . serially
163+
164+ describe "Runners" $ do
165+ -- XXX use an IORef to store and check the side effects
166+ it "simple serially" $
167+ (S.drain . serially) (return (0 :: Int)) `shouldReturn` ()
168+ it "simple serially with IO" $
169+ (S.drain . serially) (S.yieldM $ putStrLn "hello") `shouldReturn` ()
170+
171+ describe "Empty" $
172+ it "Monoid - mempty" $
173+ toListSerial mempty `shouldReturn` ([] :: [Int])
174+ -- it "Alternative - empty" $
175+ -- (toListSerial empty) `shouldReturn` ([] :: [Int])
176+ -- it "MonadPlus - mzero" $
177+ -- (toListSerial mzero) `shouldReturn` ([] :: [Int])
161178
162179 describe "Construction" $ do
163180 serialOps $ prop "serially replicate" . constructWithReplicate
@@ -177,6 +194,10 @@ main = hspec
177194 describe "Functor operations" $ do
178195 serialOps $ functorOps S.fromFoldable "serially" (==)
179196 serialOps $ functorOps folded "serially folded" (==)
197+
198+ describe "Monoid operations" $ do
199+ serialOps $ monoidOps "serially" mempty (==)
200+
180201 describe "Semigroup operations" $ do
181202 serialOps $ semigroupOps "serially" (==)
182203
test/Streamly/Test/Prelude/WAsync.hs+3
@@ -41,6 +41,9 @@ main = hspec
4141 wAsyncOps $ functorOps S.fromFoldable "wAsyncly" sortEq
4242 wAsyncOps $ functorOps folded "wAsyncly folded" sortEq
4343
44+ describe "Monoid operations" $ do
45+ wAsyncOps $ monoidOps "wAsyncly" mempty sortEq
46+
4447 describe "Semigroup operations" $ do
4548 wAsyncOps $ semigroupOps "wAsyncly" sortEq
4649
test/Streamly/Test/Prelude/WSerial.hs+3
@@ -41,6 +41,9 @@ main = hspec
4141 wSerialOps $ functorOps S.fromFoldable "wSerially" (==)
4242 wSerialOps $ functorOps folded "wSerially folded" (==)
4343
44+ describe "Monoid operations" $ do
45+ wSerialOps $ monoidOps "wSerially" mempty sortEq
46+
4447 describe "Semigroup operations" $ do
4548 wSerialOps $ semigroupOps "wSerially" (==)
4649
test/Streamly/Test/Prelude/ZipAsync.hs+3
@@ -33,6 +33,9 @@ main = hspec
3333 zipAsyncOps $ functorOps S.fromFoldable "zipAsyncly" (==)
3434 zipAsyncOps $ functorOps folded "zipAsyncly folded" (==)
3535
36+ describe "Monoid operations" $ do
37+ zipAsyncOps $ monoidOps "zipAsyncly" mempty (==)
38+
3639 describe "Semigroup operations" $ do
3740 zipAsyncOps $ semigroupOps "zipAsyncly" (==)
3841
test/Streamly/Test/Prelude/ZipSerial.hs+3
@@ -39,6 +39,9 @@ main = hspec
3939 zipSerialOps $ functorOps S.fromFoldable "zipSerially" (==)
4040 zipSerialOps $ functorOps folded "zipSerially folded" (==)
4141
42+ describe "Monoid operations" $ do
43+ zipSerialOps $ monoidOps "zipSerially" mempty (==)
44+
4245 describe "Semigroup operations" $ do
4346 zipSerialOps $ semigroupOps "zipSerially" (==)
4447