safe-lazy-io-0.1: examples/testhgetcontents.hs
import Prelude hiding (catch)
import System.IO
import System.IO.Unsafe.GetContents
import GHC.Handle
import Control.Exception
test name k = do
putStrLn $ " " ++ name
putStrLn . (replicate 4 ' '++) =<<
(fmap show (evaluate =<< k)
`catch` (\e -> return $ "error: " ++ show (e :: SomeException)))
tests hGetC = do
test "Length OK" $ do
h <- openFile "testhgetcontents.hs" ReadMode
xs <- hGetC h
return $ length xs
test "Length but closed" $ do
h <- openFile "testhgetcontents.hs" ReadMode
xs <- hGetC h
hClose h
return $ length xs
test "Lengths on Double get" $ do
h <- openFile "testhgetcontents.hs" ReadMode
xs <- hGetC h
ys <- hGetC h
let l1 = length xs
l1 `seq` return (l1, length ys)
test "Lengths on dupped get" $ do
h <- openFile "testhgetcontents.hs" ReadMode
duph <- hDuplicate h
xs <- hGetC h
ys <- hGetC duph
let l1 = length xs
l1 `seq` return (l1, length ys)
main = do
putStrLn "using hGetContents"
tests hGetContents
putStrLn "using unsafeHGetContents"
tests unsafeHGetContents