on master help sign in
Refactor applicativeOps.
commitd78cc5388d26e6f5a747b3af7bfa56a6ae0723b1 authorPranay Sashank <pranaysashank@composewell.com> dateWed 29 Jul 2020, 07:02 (6 years ago) parent0f77322
7 files changed+45−35
test/Streamly/Test/Prelude.hs+32−20 test/Streamly/Test/Prelude/Ahead.hs+2−2 test/Streamly/Test/Prelude/Async.hs+2−2 test/Streamly/Test/Prelude/Parallel.hs+1−2 test/Streamly/Test/Prelude/Serial.hs+4−3 test/Streamly/Test/Prelude/WAsync.hs+2−4 test/Streamly/Test/Prelude/WSerial.hs+2−2
test/Streamly/Test/Prelude.hs+32−20
@@ -237,38 +237,50 @@ constructWithFromIndicesM op len =
237237applicativeOps
238238 :: Applicative (t IO)
239239 => ([Int] -> t IO Int)
240+ -> String
240241 -> ([(Int, Int)] -> [(Int, Int)] -> Bool)
241242 -> (t IO (Int, Int) -> SerialT IO (Int, Int))
242− -> ([Int], [Int])
243− -> Property
244−applicativeOps constr eq t (a, b) = withMaxSuccess maxTestCount $
245− monadicIO $ do
246− stream <- run ((S.toList . t) ((,) <$> constr a <*> constr b))
247− let list = (,) <$> a <*> b
248− listEquals eq stream list
249−
250− stream1 <- run ((S.toList . t) (liftA2 (,) (constr a) (constr b)))
251− listEquals eq stream1 list
243+ -> Spec
244+applicativeOps constr desc eq t = do
245+ prop (desc <> " <*>") $
246+ transformFromList2
247+ constr
248+ eq
249+ (\a b -> (,) <$> a <*> b)
250+ (\a b -> t ((,) <$> a <*> b))
251+ prop (desc <> " liftA2") $
252+ transformFromList2 constr eq (liftA2 (,)) (\a b -> t $ liftA2 (,) a b)
252253
253254-- XXX we can combine this with applicativeOps by making the type sufficiently
254255-- polymorphic.
255256applicativeOps1
256257 :: Applicative (t IO)
257258 => ([Int] -> t IO Int)
259+ -> String
258260 -> ([Int] -> [Int] -> Bool)
259261 -> (t IO Int -> SerialT IO Int)
260− -> ([Int], [Int])
261− -> Property
262−applicativeOps1 constr eq t (a, b) = withMaxSuccess maxTestCount $
262+ -> Spec
263+applicativeOps1 constr desc eq t = do
264+ prop (desc <> " *>") $
265+ transformFromList2 constr eq (*>) (\a b -> t (a *> b))
266+ prop (desc <> " <*") $
267+ transformFromList2 constr eq (<*) (\a b -> t (a <* b))
268+
269+transformFromList2
270+ :: (Eq c, Show c)
271+ => ([a] -> t IO a)
272+ -> ([c] -> [c] -> Bool)
273+ -> ([a] -> [a] -> [c])
274+ -> (t IO a -> t IO a -> SerialT IO c)
275+ -> ([a], [a])
276+ -> Property
277+transformFromList2 constr eq listOp op (a, b) =
278+ withMaxSuccess maxTestCount $
263279 monadicIO $ do
264− stream <- run ((S.toList . t) (constr a *> constr b))
265− let list = a *> b
280+ stream <- run (S.toList $ op (constr a) (constr b))
281+ let list = listOp a b
266282 listEquals eq stream list
267283
268− stream1 <- run ((S.toList . t) (constr a <* constr b))
269− let list1 = a <* b
270− listEquals eq stream1 list1
271−
272284-------------------------------------------------------------------------------
273285-- Elimination operations
274286-------------------------------------------------------------------------------
@@ -432,7 +444,7 @@ eliminationOpsWord8 constr desc t = do
432444-------------------------------------------------------------------------------
433445
434446functorOps
435− :: Functor (t IO)
447+ :: (Functor (t IO), Semigroup (t IO Int))
436448 => ([Int] -> t IO Int)
437449 -> String
438450 -> ([Int] -> [Int] -> Bool)
test/Streamly/Test/Prelude/Ahead.hs+2−2
@@ -44,8 +44,8 @@ main = hspec
4444 aheadOps $ semigroupOps "aheadly" (==)
4545
4646 describe "Applicative operations" $ do
47− aheadOps $ prop "aheadly applicative" . applicativeOps S.fromFoldable (==)
48− aheadOps $ prop "aheadly applicative folded" . applicativeOps folded (==)
47+ aheadOps $ applicativeOps S.fromFoldable "aheadly applicative" (==)
48+ aheadOps $ applicativeOps folded "aheadly applicative folded" (==)
4949
5050 -- XXX add tests for indexed/indexedR
5151 describe "Zip operations" $ do
test/Streamly/Test/Prelude/Async.hs+2−2
@@ -44,8 +44,8 @@ main = hspec
4444 asyncOps $ semigroupOps "asyncly" sortEq
4545
4646 describe "Applicative operations" $ do
47− asyncOps $ prop "asyncly applicative" . applicativeOps S.fromFoldable sortEq
48− asyncOps $ prop "asyncly applicative folded" . applicativeOps folded sortEq
47+ asyncOps $ applicativeOps S.fromFoldable "asyncly applicative" sortEq
48+ asyncOps $ applicativeOps folded "asyncly applicative folded" sortEq
4949
5050 -- XXX add tests for indexed/indexedR
5151 describe "Zip operations" $ do
test/Streamly/Test/Prelude/Parallel.hs+1−2
@@ -48,8 +48,7 @@ main = hspec
4848 parallelOps $ semigroupOps "parallely" sortEq
4949
5050 describe "Applicative operations" $ do
51− parallelOps $
52− prop "parallely applicative folded" . applicativeOps folded sortEq
51+ parallelOps $ applicativeOps folded "parallely applicative folded" sortEq
5352
5453 -- XXX add tests for indexed/indexedR
5554 describe "Zip operations" $ do
test/Streamly/Test/Prelude/Serial.hs+4−3
@@ -184,9 +184,10 @@ main = hspec
184184 -- The tests using sorted equality are weaker tests
185185 -- We need to have stronger unit tests for all those
186186 -- XXX applicative with three arguments
187− serialOps $ prop "serially applicative" . applicativeOps S.fromFoldable (==)
188− serialOps $ prop "serially applicative folded" . applicativeOps folded (==)
189− serialOps $ prop "serially applicative discard" . applicativeOps1 S.fromFoldable (==)
187+ serialOps $ applicativeOps S.fromFoldable "serially applicative" (==)
188+ serialOps $ applicativeOps folded "serially applicative folded" (==)
189+ serialOps $
190+ applicativeOps1 S.fromFoldable "serially applicative discard" (==)
190191
191192 -- XXX add tests for indexed/indexedR
192193 describe "Zip operations" $ do
test/Streamly/Test/Prelude/WAsync.hs+2−4
@@ -45,10 +45,8 @@ main = hspec
4545 wAsyncOps $ semigroupOps "wAsyncly" sortEq
4646
4747 describe "Applicative operations" $ do
48− wAsyncOps $
49− prop "wAsyncly applicative" . applicativeOps S.fromFoldable sortEq
50− wAsyncOps $
51− prop "wAsyncly applicative folded" . applicativeOps folded sortEq
48+ wAsyncOps $ applicativeOps S.fromFoldable "wAsyncly applicative" sortEq
49+ wAsyncOps $ applicativeOps folded "wAsyncly applicative folded" sortEq
5250
5351 -- XXX add tests for indexed/indexedR
5452 describe "Zip operations" $
test/Streamly/Test/Prelude/WSerial.hs+2−2
@@ -45,8 +45,8 @@ main = hspec
4545 wSerialOps $ semigroupOps "wSerially" (==)
4646
4747 describe "Applicative operations" $ do
48− wSerialOps $ prop "wSerially applicative" . applicativeOps S.fromFoldable sortEq
49− wSerialOps $ prop "wSerially applicative folded" . applicativeOps folded sortEq
48+ wSerialOps $ applicativeOps S.fromFoldable "wSerially applicative" sortEq
49+ wSerialOps $ applicativeOps folded "wSerially applicative folded" sortEq
5050
5151 -- XXX add tests for indexed/indexedR
5252 describe "Zip operations" $ do