-
Notifications
You must be signed in to change notification settings - Fork 0
/
testTrySer.hs
66 lines (49 loc) · 2.08 KB
/
testTrySer.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import GHC.Packing
import qualified Data.Array.IArray as A
import Control.Concurrent
import System.Environment
import System.IO
import qualified Control.Exception as E
import Control.Parallel.Eden.ParPrim
-- this test uses the trySerialize routine. We expect to trigger some
-- exceptions and catch them as appropriate.
catchPackExc :: IO () -> IO ()
catchPackExc io = io `E.catch` (\e -> putStrLn (show (e::PackException)))
main :: IO ()
main=do hSetBuffering stdout NoBuffering
args <- getArgs
putStrLn "Test program for packing/serialization:"
putStrLn "testing exceptions during packing. Use -qQ1k or so..."
let n = if (length args < 2) then 1 else read (args!!1)
size = if null args then 128 else read (head args)::Int
arr :: A.Array Int Int
arr = A.array (0,size-1)
[ (i,i) | i <- [0..size-1] ]
let output = A.amap (2*) arr
putStrLn $ show $ take n $ A.elems output
putStrLn "now packing the array (buffer big enough?)"
catchPackExc $
do packet1 <- trySerialize output
putStrLn (show packet1)
putStrLn "now unpacking (deserialize):"
copy <- deserialize packet1
putStrLn ("unpacked, now evaluate")
putStrLn (show copy)
putStrLn "packing some forbidden types"
t <- myThreadId
putStrLn "next should be unsupported"
catchPackExc (trySerialize t >>= print)
m <- newEmptyMVar :: IO (MVar Int)
putStrLn "next should be cannotpack"
catchPackExc (trySerialize m >>= print)
(c,b) <- createC:: IO (ChanName' Double, Double)
putStrLn "next should hit a blackhole"
catchPackExc (trySerialize b >>= print)
let arr2 = A.listArray (0,n-1) (take n (A.elems arr)) :: A.Array Int Int
putStrLn "this - finally - should work"
putStrLn ( show $ arr2 A.! 0 ) -- forcing it
catchPackExc $
do p2 <- trySerialize arr2
arr3 <- deserialize p2
print arr3
putStrLn "DONE"