Add applicative and alternative tests

This commit is contained in:
Harendra Kumar 2017-06-26 03:15:27 +05:30
parent b9c1c92dc0
commit 594185f18d
2 changed files with 47 additions and 0 deletions

30
test/alternative.hs Normal file
View File

@ -0,0 +1,30 @@
import Control.Concurrent (threadDelay, myThreadId)
import Control.Monad.IO.Class (liftIO)
import System.Random (randomIO)
import System.IO
import Control.Applicative
import Strands
main = wait $ threads 3 $ do
liftIO $ hSetBuffering stdout LineBuffering
mainThread <- liftIO myThreadId
liftIO $ putStrLn $ "Main thread: " ++ show mainThread
eventA <|> eventB
eventA = do
x <- sample (randomIO :: IO Int) 1000000
evThread <- liftIO myThreadId
liftIO $ putStrLn $ "X Event thread: " ++ show evThread
liftIO $ putStrLn $ "x = " ++ (show x)
return x
eventB = do
y <- sample (randomIO :: IO Int) 1000000
-- liftIO $ threadDelay 10000000
evThread <- liftIO myThreadId
liftIO $ putStrLn $ "Y Event thread: " ++ show evThread
liftIO $ putStrLn $ "y = " ++ (show y)
return y

17
test/applicative.hs Normal file
View File

@ -0,0 +1,17 @@
{-# LANGUAGE FlexibleContexts #-}
import Control.Applicative ((<$>), (<*>), (<|>))
import Control.Monad.IO.Class (liftIO)
import System.IO
import Strands
main = do
hSetBuffering stdout LineBuffering
xs <- gather $ threads 5 $ do
x <- (,) <$> (event 1 <|> event 2) <*> (event 3 <|> event 4)
--x <- (,) <$> choose [1,2] <*> choose [3,4]
liftIO $ putStrLn $ show x
return x
putStrLn $ show xs
where
event n = async (do putStrLn ("event" ++ show n); return n :: IO Int)