[project @ 1998-12-02 13:17:09 by simonm]
[ghc-hetmet.git] / ghc / interpreter / test / std / iohandle.hs
1 --!!! Testing File I/O operations and errors
2
3 import IO
4
5 testFile    = "test/iohandle.tst"
6 unreadable  = "test/unreadable.tst"
7 unwritable  = "test/unwritable.tst"
8 nonexistent = "test/nonexistent.tst"
9
10 -- Handle free ops
11 a1 = writeFile testFile (show [1..10])
12 a2 = readFile testFile >>= \ s -> putStr s
13 a3 = appendFile testFile (show [11..20])
14 a4 = readFile testFile >>= \ s -> putStr s
15
16 -- Same stuff - but using handle-based operations
17 b1 = openFile testFile WriteMode  >>= \ h ->
18      hPutStr h (show [1..10])
19 b2 = openFile testFile ReadMode   >>= \ h ->
20      hGetContents h               >>= \ s ->
21      putStr s
22 b3 = openFile testFile AppendMode >>= \ h ->
23      hPutStr h (show [11..20])     
24 b4 = openFile testFile ReadMode   >>= \ h ->
25      hGetContents h               >>= \ s ->
26      putStr s
27
28 -- Miscellaneous little functions
29 c1 = openFile testFile WriteMode           >>= \ h ->
30      mapM_ (hPutChar h) (show [1..10])     >>
31      hClose h
32 c2 = openFile testFile ReadMode   >>= \ h ->
33      let loop = 
34            hGetChar h >>= \ c ->
35            putChar c  >>
36            loop
37      in
38      loop  :: IO ()
39 c3 = openFile testFile AppendMode          >>= \ h ->
40      hPutStr h (show [11..20])             >>
41      hClose h
42 c4 = openFile testFile ReadMode   >>= \ h ->
43      let loop = 
44            hGetChar h >>= \ c ->
45            putChar c  >>
46            loop
47      in
48      loop `catch` (\err -> if isEOFError err then return () else fail err)
49 -- If this function raises an uncaught EOF error, then hIsEOF probably
50 -- implements ANSI C feof semantics which is quite different from 
51 -- Haskell 1.3 semantics (but much easier to implement).
52 c5 = openFile testFile ReadMode   >>= \ h ->
53      let loop = 
54            hIsEOF h >>= \ eof ->
55            if eof then return () else
56            hGetChar h >>= \ c ->
57            putChar c  >>
58            loop
59      in
60      loop :: IO ()
61     
62 c6 = openFile testFile ReadMode  >>= \ h ->
63      hFlush h                    >>
64      hGetContents h              >>= \ s ->
65      putStr s
66
67 -- should print first 10 characters of file twice
68 c7 = openFile testFile ReadMode  >>= \ h ->
69      hGetContents h              >>= \ s ->
70      putStr (take 10 s)          >>
71      hClose h                    >>
72      putStr s
73
74
75 -- Deliberately trying to trigger IOErrors:
76
77 -- Note: Linux allows a file to be opened twice
78 d1 = openFile testFile WriteMode  >>= \ h1 ->
79      openFile testFile WriteMode  >>= \ h2 ->
80      let x = [h1,h2] in -- try to make sure both pointers remain live
81      return ()
82
83 d2 = openFile testFile WriteMode  >>= \ h ->
84      hGetContents h               >>= \ s ->
85      putStr s
86
87 d3 = openFile testFile ReadMode  >>= \ h ->
88      hPutStr h (show [5..10])
89
90 -- This should succeed
91 d4 = openFile unreadable WriteMode  >>= \ h ->
92      return ()
93
94 -- This should fail
95 d5 = openFile unreadable ReadMode  >>= \ h ->
96      return ()
97
98 -- This should succeed
99 d6 = openFile unwritable ReadMode  >>= \ h ->
100      return ()
101
102 -- This should fail
103 d7 = openFile unwritable WriteMode  >>= \ h ->
104      return ()
105
106 d8 = openFile testFile ReadMode  >>= \ h ->
107      hClose h                    >>
108      hGetContents h              >>= \ s ->
109      putStr s
110
111 d9 = openFile testFile ReadMode  >>= \ h ->
112      hClose h                    >>
113      hClose h
114
115 -- should fail
116 d10 = openFile testFile ReadMode  >>= \ h ->
117       hGetContents h              >>= \ s1 ->
118       hGetContents h              >>= \ s2 ->
119       putStr s1                   >>
120       putStr s2
121
122
123