on master help sign in
Rename internal-prelude test into prelude test suite.
af62054pranaysashank
streamly.cabal−13
@@ -551,19 +551,6 @@ library
551-- Test suites
551-- Test suites
552-------------------------------------------------------------------------------
552-------------------------------------------------------------------------------
553
553
554−test-suite internal-prelude-test
555− import: test-options
556− type: exitcode-stdio-1.0
557− main-is: Streamly/Test/Internal/Prelude.hs
558− js-sources: jsbits/clock.js
559− hs-source-dirs: test
560− build-depends:
561− streamly
562− , base >= 4.9 && < 5
563− , QuickCheck >= 2.13 && < 2.15
564− , hspec >= 2.0 && < 3
565− default-language: Haskell2010
566−
567test-suite pure-streams-base
554test-suite pure-streams-base
568 import: test-options
555 import: test-options
569 type: exitcode-stdio-1.0
556 type: exitcode-stdio-1.0
test/Streamly/Test/Internal/Prelude.hsdeleted−131
@@ -1,131 +0,0 @@
1−-- |
2−-- Module : Main
3−-- Copyright : (c) 2019 Composewell Technologies
4−--
5−-- License : BSD-3-Clause
6−-- Maintainer : streamly@composewell.com
7−-- Stability : experimental
8−-- Portability : GHC
9−--
10−module Main (main) where
11−
12−import Control.Concurrent (threadDelay)
13−import Control.Monad (when)
14−import Data.Function ((&))
15−import Data.IORef (newIORef, writeIORef, readIORef)
16−import Data.Maybe (fromJust, isJust)
17−
18−import Test.Hspec as H
19−import Test.Hspec.QuickCheck (prop)
20−import Test.QuickCheck (Property, choose)
21−import Test.QuickCheck.Monadic (monadicIO, pick)
22−
23−import qualified Streamly.Prelude as S
24−import qualified Streamly.Internal.Data.Fold as FL
25−import qualified Streamly.Internal.Data.Unfold as UF
26−import qualified Streamly.Internal.Data.Stream.IsStream as IS
27−
28−import Streamly.Internal.Data.Time.Clock (Clock(Monotonic), getTime)
29−import Streamly.Internal.Data.Time.Units
30− (AbsTime, NanoSecond64(..), toRelTime64, diffAbsTime64)
31−import Data.Int (Int64)
32−
33−max_length :: Int
34−max_length = 1000
35−
36−tenPow8 :: Int64
37−tenPow8 = 10^(8 :: Int)
38−
39−tenPow7 :: Int64
40−tenPow7 = 10^(7 :: Int)
41−
42−takeDropTime :: NanoSecond64
43−takeDropTime = NanoSecond64 $ 5 * tenPow8
44−
45−checkTakeDropTime :: (Maybe AbsTime, Maybe AbsTime) -> IO Bool
46−checkTakeDropTime (mt0, mt1) = do
47− let graceTime = NanoSecond64 $ 8 * tenPow7
48− case mt0 of
49− Nothing -> return True
50− Just t0 ->
51− case mt1 of
52− Nothing -> return True
53− Just t1 -> do
54− let tMax = toRelTime64 (takeDropTime + graceTime)
55− let tMin = toRelTime64 (takeDropTime - graceTime)
56− let t = diffAbsTime64 t1 t0
57− let r = t >= tMin && t <= tMax
58− when (not r) $ putStrLn $
59− "t = " ++ show t ++
60− " tMin = " ++ show tMin ++
61− " tMax = " ++ show tMax
62− return r
63−
64−testTakeByTime :: IO Bool
65−testTakeByTime = do
66− r <-
67− S.fold ((,) <$> FL.head <*> FL.last)
68− $ IS.takeByTime takeDropTime
69− $ S.repeatM (threadDelay 1000 >> getTime Monotonic)
70− checkTakeDropTime r
71−
72−testDropByTime :: IO Bool
73−testDropByTime = do
74− t0 <- getTime Monotonic
75− mt1 <-
76− S.head
77− $ IS.dropByTime takeDropTime
78− $ S.repeatM (threadDelay 1000 >> getTime Monotonic)
79− checkTakeDropTime (Just t0, mt1)
80−
81−unfold :: Property
82−unfold = monadicIO $ do
83− a <- pick $ choose (0, max_length `div` 2)
84− b <- pick $ choose (0, max_length)
85− let unf = UF.enumerateFromToIntegral b
86− ls <- S.toList $ S.unfold unf a
87− return $ ls == [a..b]
88−
89−unfold0 :: Property
90−unfold0 = monadicIO $ do
91− a <- pick $ choose (0, max_length `div` 2)
92− b <- pick $ choose (0, max_length)
93− let unf = UF.supply (UF.enumerateFromToIntegral b) a
94− ls <- S.toList $ IS.unfold0 unf
95− return $ ls == [a..b]
96−
97−testFromCallback :: IO Int
98−testFromCallback = do
99− ref <- newIORef Nothing
100− let stream = S.map Just (IS.fromCallback (setCallback ref))
101− `S.parallel` runCallback ref
102− S.sum $ S.map fromJust $ S.takeWhile isJust stream
103−
104− where
105−
106− setCallback ref cb = do
107− writeIORef ref (Just cb)
108−
109− runCallback ref = S.yieldM $ do
110− cb <-
111− S.repeatM (readIORef ref)
112− & IS.delayPost 0.1
113− & S.mapMaybe id
114− & S.head
115−
116− S.fromList [1..100]
117− & IS.delayPost 0.001
118− & S.mapM_ (fromJust cb)
119− threadDelay 100000
120− return Nothing
121−
122−main :: IO ()
123−main =
124− hspec $ do
125− describe "Filtering" $ do
126− it "takeByTime" (testTakeByTime `shouldReturn` True)
127− it "dropByTime" (testDropByTime `shouldReturn` True)
128− describe "From Generators" $ do
129− prop "unfold" unfold
130− prop "unfold0" unfold0
131− it "fromCallback" $ testFromCallback `shouldReturn` (50*101)
test/Streamly/Test/Prelude.hs+127−16
@@ -9,38 +9,126 @@
9
9
10module Streamly.Test.Prelude (main) where
10module Streamly.Test.Prelude (main) where
11
11
12+import Control.Concurrent (threadDelay)
12import Control.Exception (Exception, try)
13import Control.Exception (Exception, try)
14+import Control.Monad (when)
13import Control.Monad.Catch (throwM)
15import Control.Monad.Catch (throwM)
14import Control.Monad.Error.Class (throwError, MonadError)
16import Control.Monad.Error.Class (throwError, MonadError)
15import Control.Monad.Trans.Except (runExceptT, ExceptT)
17import Control.Monad.Trans.Except (runExceptT, ExceptT)
18+import Data.Function ((&))
19+import Data.IORef (newIORef, readIORef, writeIORef)
20+import Data.Int (Int64)
16import Data.List (sort)
21import Data.List (sort)
22+import Data.Maybe (fromJust, isJust)
17#if !(MIN_VERSION_base(4,11,0))
23#if !(MIN_VERSION_base(4,11,0))
18import Data.Semigroup (Semigroup(..))
24import Data.Semigroup (Semigroup(..))
19−import Data.IORef
20#endif
25#endif
21−
22import Test.Hspec as H
26import Test.Hspec as H
23−
27+import Test.Hspec.QuickCheck (prop)
28+import Test.QuickCheck (Property, choose)
29+import Test.QuickCheck.Monadic (monadicIO, pick)
30+
24−import Streamly.Prelude
31+import Streamly.Prelude (SerialT, IsStream)
25− ( SerialT, IsStream)
32+import Streamly.Internal.Data.Time.Units
33+ (AbsTime, NanoSecond64(..), toRelTime64, diffAbsTime64)
34+import Streamly.Internal.Data.Time.Clock (Clock(Monotonic), getTime)
35+
36+import qualified Streamly.Internal.Data.Fold as FL
37+import qualified Streamly.Internal.Data.Unfold as UF
38+import qualified Streamly.Internal.Data.Stream.IsStream as IS
26import qualified Streamly.Prelude as S
39import qualified Streamly.Prelude as S
27
40
28toListSerial :: SerialT IO a -> IO [a]
41toListSerial :: SerialT IO a -> IO [a]
29toListSerial = S.toList . S.serially
42toListSerial = S.toList . S.serially
30
43
31−main :: IO ()
44+max_length :: Int
32−main = hspec $ H.parallel $ do
45+max_length = 1000
46+
47+tenPow8 :: Int64
48+tenPow8 = 10^(8 :: Int)
49+
50+tenPow7 :: Int64
51+tenPow7 = 10^(7 :: Int)
52+
53+takeDropTime :: NanoSecond64
54+takeDropTime = NanoSecond64 $ 5 * tenPow8
55+
56+checkTakeDropTime :: (Maybe AbsTime, Maybe AbsTime) -> IO Bool
57+checkTakeDropTime (mt0, mt1) = do
58+ let graceTime = NanoSecond64 $ 8 * tenPow7
59+ case mt0 of
60+ Nothing -> return True
61+ Just t0 ->
62+ case mt1 of
63+ Nothing -> return True
64+ Just t1 -> do
65+ let tMax = toRelTime64 (takeDropTime + graceTime)
66+ let tMin = toRelTime64 (takeDropTime - graceTime)
67+ let t = diffAbsTime64 t1 t0
68+ let r = t >= tMin && t <= tMax
69+ when (not r) $ putStrLn $
70+ "t = " ++ show t ++
71+ " tMin = " ++ show tMin ++
72+ " tMax = " ++ show tMax
73+ return r
74+
75+testTakeByTime :: IO Bool
76+testTakeByTime = do
77+ r <-
78+ S.fold ((,) <$> FL.head <*> FL.last)
79+ $ IS.takeByTime takeDropTime
80+ $ S.repeatM (threadDelay 1000 >> getTime Monotonic)
81+ checkTakeDropTime r
82+
83+testDropByTime :: IO Bool
84+testDropByTime = do
85+ t0 <- getTime Monotonic
86+ mt1 <-
87+ S.head
88+ $ IS.dropByTime takeDropTime
89+ $ S.repeatM (threadDelay 1000 >> getTime Monotonic)
90+ checkTakeDropTime (Just t0, mt1)
91+
92+unfold :: Property
93+unfold = monadicIO $ do
94+ a <- pick $ choose (0, max_length `div` 2)
95+ b <- pick $ choose (0, max_length)
96+ let unf = UF.enumerateFromToIntegral b
97+ ls <- S.toList $ S.unfold unf a
98+ return $ ls == [a..b]
99+
100+unfold0 :: Property
101+unfold0 = monadicIO $ do
102+ a <- pick $ choose (0, max_length `div` 2)
103+ b <- pick $ choose (0, max_length)
104+ let unf = UF.supply (UF.enumerateFromToIntegral b) a
105+ ls <- S.toList $ IS.unfold0 unf
106+ return $ ls == [a..b]
107+
108+testFromCallback :: IO Int
109+testFromCallback = do
110+ ref <- newIORef Nothing
111+ let stream = S.map Just (IS.fromCallback (setCallback ref))
112+ `S.parallel` runCallback ref
113+ S.sum $ S.map fromJust $ S.takeWhile isJust stream
33
114
34− describe "Miscellaneous combined examples" mixedOps
115+ where
35− describe "Miscellaneous combined examples aheadly" mixedOpsAheadly
36− describe "Simple MonadError and MonadThrow" simpleMonadError
37
116
38− {-
117+ setCallback ref cb = do
39− describe "Composed MonadError serially" $ composeWithMonadError serially
118+ writeIORef ref (Just cb)
40− describe "Composed MonadError wSerially" $ composeWithMonadError wSerially
119+
41− describe "Composed MonadError asyncly" $ composeWithMonadError asyncly
120+ runCallback ref = S.yieldM $ do
42− describe "Composed MonadError wAsyncly" $ composeWithMonadError wAsyncly
121+ cb <-
43− -}
122+ S.repeatM (readIORef ref)
123+ & IS.delayPost 0.1
124+ & S.mapMaybe id
125+ & S.head
126+
127+ S.fromList [1..100]
128+ & IS.delayPost 0.001
129+ & S.mapM_ (fromJust cb)
130+ threadDelay 100000
131+ return Nothing
44
132
45-- XXX need to test that we have promptly cleaned up everything after the error
133-- XXX need to test that we have promptly cleaned up everything after the error
46-- XXX We can also check the output that we are expected to get before the
134-- XXX We can also check the output that we are expected to get before the
@@ -138,3 +226,26 @@ mixedOpsAheadly =
138 return (x11 + y11 + z11)
226 return (x11 + y11 + z11)
139 return (x1 + y1 + z1)
227 return (x1 + y1 + z1)
140 return (x + y + z)
228 return (x + y + z)
229+
230+main :: IO ()
231+main = hspec $ H.parallel $ do
232+ describe "Filtering" $ do
233+ it "takeByTime" (testTakeByTime `shouldReturn` True)
234+ it "dropByTime" (testDropByTime `shouldReturn` True)
235+
236+ describe "From Generators" $ do
237+ prop "unfold" unfold
238+ prop "unfold0" unfold0
239+
240+ it "fromCallback" $ testFromCallback `shouldReturn` (50*101)
241+
242+ describe "Miscellaneous combined examples" mixedOps
243+ describe "Miscellaneous combined examples aheadly" mixedOpsAheadly
244+ describe "Simple MonadError and MonadThrow" simpleMonadError
245+
246+ {-
247+ describe "Composed MonadError serially" $ composeWithMonadError serially
248+ describe "Composed MonadError wSerially" $ composeWithMonadError wSerially
249+ describe "Composed MonadError asyncly" $ composeWithMonadError asyncly
250+ describe "Composed MonadError wAsyncly" $ composeWithMonadError wAsyncly
251+ -}