summaryrefslogtreecommitdiff
path: root/testsuite/tests/lib/IO/hSeek003.hs
diff options
context:
space:
mode:
Diffstat (limited to 'testsuite/tests/lib/IO/hSeek003.hs')
-rw-r--r--testsuite/tests/lib/IO/hSeek003.hs51
1 files changed, 51 insertions, 0 deletions
diff --git a/testsuite/tests/lib/IO/hSeek003.hs b/testsuite/tests/lib/IO/hSeek003.hs
new file mode 100644
index 0000000000..03400573c4
--- /dev/null
+++ b/testsuite/tests/lib/IO/hSeek003.hs
@@ -0,0 +1,51 @@
+-- !!! file positions (hGetPosn and hSetPosn)
+
+module Main(main) where
+
+import System.IO
+import Control.Monad ( sequence )
+
+testPosns :: Handle -> BufferMode -> IO ()
+testPosns hdl bmo = do
+ hSetBuffering hdl bmo
+ putStrLn ("Testing positioning with buffer mode set to: " ++ show bmo)
+ testPositioning hdl
+
+bmo_ls = [NoBuffering, LineBuffering, BlockBuffering Nothing,
+ BlockBuffering (Just 511),BlockBuffering (Just 3), BlockBuffering (Just 11)]
+
+main = do
+ hdl <- openFile "hSeek003.hs" ReadMode
+ sequence (zipWith testPosns (repeat hdl) bmo_ls)
+ hClose hdl
+
+testPositioning hdl = do
+ hSeek hdl AbsoluteSeek 0 -- go to the beginning of the file again.
+ ps <- getFilePosns 10 hdl
+ hSeek hdl AbsoluteSeek 0
+ putStr "First ten chars: "
+ ls <- hGetChars 10 hdl
+ putStrLn ls
+ -- go to the end
+ hSeek hdl SeekFromEnd 0
+ ls <- sequence (map (\ p -> hSetPosn p >> hGetChar hdl) ps)
+ putStr "First ten chars: "
+ putStrLn ls
+
+ -- position ourselves in the middle.
+ sz <- hFileSize hdl
+ hSeek hdl AbsoluteSeek (sz `div` 2)
+ ls <- sequence (map (\ p -> hSetPosn p >> hGetChar hdl) ps)
+ putStr "First ten chars: "
+ putStrLn ls
+
+hGetChars :: Int -> Handle -> IO String
+hGetChars n h = sequence (replicate n (hGetChar h))
+
+getFilePosns :: Int -> Handle -> IO [HandlePosn]
+getFilePosns 0 h = return []
+getFilePosns x h = do
+ p <- hGetPosn h
+ hGetChar h
+ ps <- getFilePosns (x-1) h
+ return (p:ps)