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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
|
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
-- Copyright (C) 2011 John Millikin <jmillikin@gmail.com>
--
-- See license.txt for details
module Main
( tests
, main
) where
import Control.Monad.IO.Class (liftIO)
import qualified Data.ByteString
import Data.ByteString.Char8 ()
import Data.ByteString.Unsafe (unsafePackCStringLen)
import Foreign (nullPtr)
import qualified GHC.IO.Exception as GHC
import System.IO
import Test.Chell
import Data.Knob
main :: IO ()
main = Test.Chell.defaultMain tests
tests :: [Suite]
tests = [suite_File, suite_Duplex]
suite_File :: Suite
suite_File = suite "file" $ concatMap suiteTests
[ suite "read"
[ test_ReadFromStart
, test_ReadFromOffset
, test_ReadToEOF
, test_ReadPastEOF
]
, suite "write"
[ test_WriteFromStart
, test_WriteFromOffset
, test_WritePastEOF
, test_WriteAppended
]
, suite "seek"
[ test_SeekAbsolute
, test_SeekRelative
, test_SeekFromEnd
, test_SeekBeyondMaxInt
]
, suite "setSize"
[ test_SetSize_Read
, test_SetSize_Write
, test_SetSize_ReadWrite
, test_SetSize_Append
]
]
++ [ test_Ready
, test_Close
, test_SetContents
, test_WithFileHandle
]
test_ReadFromStart :: Test
test_ReadFromStart = assertions "from-start" $ do
k <- newKnob "abcde"
h <- newFileHandle k "foo.txt" ReadMode
bytes <- liftIO $ Data.ByteString.hGet h 3
$expect (equal bytes "abc")
off <- liftIO $ hTell h
$expect (equal off 3)
test_ReadFromOffset :: Test
test_ReadFromOffset = assertions "from-offset" $ do
k <- newKnob "abcde"
h <- newFileHandle k "foo.txt" ReadMode
liftIO $ hSeek h AbsoluteSeek 1
bytes <- liftIO $ Data.ByteString.hGet h 3
$expect (equal bytes "bcd")
off <- liftIO $ hTell h
$expect (equal off 4)
test_ReadToEOF :: Test
test_ReadToEOF = assertions "to-eof" $ do
k <- newKnob "abcde"
h <- newFileHandle k "foo.txt" ReadMode
bytes <- liftIO $ Data.ByteString.hGet h 10
$expect (equal bytes "abcde")
off <- liftIO $ hTell h
$expect (equal off 5)
test_ReadPastEOF :: Test
test_ReadPastEOF = assertions "past-eof" $ do
k <- newKnob "abcde"
h <- newFileHandle k "foo.txt" ReadMode
liftIO $ hSeek h AbsoluteSeek 10
bytes <- liftIO $ Data.ByteString.hGet h 10
$expect (equal bytes "")
off <- liftIO $ hTell h
$expect (equal off 10)
test_WriteFromStart :: Test
test_WriteFromStart = assertions "from-start" $ do
k <- newKnob ""
h <- newFileHandle k "foo.txt" WriteMode
liftIO $ hSetBuffering h NoBuffering
liftIO $ Data.ByteString.hPut h "abcde"
bytes <- Data.Knob.getContents k
$expect (equal bytes "abcde")
test_WriteFromOffset :: Test
test_WriteFromOffset = assertions "from-offset" $ do
k <- newKnob ""
h <- newFileHandle k "foo.txt" WriteMode
liftIO $ hSetBuffering h NoBuffering
liftIO $ Data.ByteString.hPut h "abcde"
liftIO $ hSeek h AbsoluteSeek 2
liftIO $ Data.ByteString.hPut h "abcde"
bytes <- Data.Knob.getContents k
$expect (equal bytes "ababcde")
test_WritePastEOF :: Test
test_WritePastEOF = assertions "past-eof" $ do
k <- newKnob ""
h <- newFileHandle k "foo.txt" WriteMode
liftIO $ hSetBuffering h NoBuffering
liftIO $ hSeek h AbsoluteSeek 2
liftIO $ Data.ByteString.hPut h "abcde"
bytes <- Data.Knob.getContents k
$expect (equal bytes "\0\0abcde")
test_WriteAppended :: Test
test_WriteAppended = assertions "appended" $ do
k <- newKnob "foo"
h <- newFileHandle k "foo.txt" AppendMode
liftIO $ hSetBuffering h NoBuffering
liftIO $ Data.ByteString.hPut h "bar"
bytes <- Data.Knob.getContents k
$expect (equal bytes "foobar")
test_SeekAbsolute :: Test
test_SeekAbsolute = assertions "absolute" $ do
k <- newKnob ""
h <- newFileHandle k "foo.txt" ReadMode
before <- liftIO $ hTell h
liftIO $ hSeek h AbsoluteSeek 2
after <- liftIO $ hTell h
$expect (equal before 0)
$expect (equal after 2)
test_SeekRelative :: Test
test_SeekRelative = assertions "relative" $ do
k <- newKnob ""
h <- newFileHandle k "foo.txt" ReadMode
before <- liftIO $ hTell h
liftIO $ hSeek h RelativeSeek 2
after1 <- liftIO $ hTell h
liftIO $ hSeek h RelativeSeek 2
after2 <- liftIO $ hTell h
$expect (equal before 0)
$expect (equal after1 2)
$expect (equal after2 4)
test_SeekFromEnd :: Test
test_SeekFromEnd = assertions "from-end" $ do
k <- newKnob "abcde"
h <- newFileHandle k "foo.txt" ReadMode
before <- liftIO $ hTell h
liftIO $ hSeek h SeekFromEnd (- 2)
after <- liftIO $ hTell h
$expect (equal before 0)
$expect (equal after 3)
test_SeekBeyondMaxInt :: Test
test_SeekBeyondMaxInt = assertions "beyond-max-int" $ do
k <- newKnob "abcde"
h <- newFileHandle k "foo.txt" ReadMode
let intPlusOne = toInteger (maxBound :: Int) + 1
$expect $ throwsEq
(GHC.IOError (Just h) GHC.InvalidArgument "hSeek" "offset > (maxBound :: Int)" Nothing (Just "foo.txt"))
(hSeek h AbsoluteSeek intPlusOne)
$expect $ throwsEq
(GHC.IOError (Just h) GHC.InvalidArgument "hSeek" "offset > (maxBound :: Int)" Nothing (Just "foo.txt"))
(hSeek h RelativeSeek intPlusOne)
-- testing this with real contents is difficult/impossible on a
-- 64-bit system, so use an unsafe function to corrupt the knob's
-- internal buffer first.
hugeBytes <- liftIO (unsafePackCStringLen (nullPtr, maxBound))
liftIO $ hSeek h AbsoluteSeek (intPlusOne - 1)
setContents k hugeBytes
$expect $ throwsEq
(GHC.IOError (Just h) GHC.InvalidArgument "hSeek" "offset > (maxBound :: Int)" Nothing (Just "foo.txt"))
(hSeek h SeekFromEnd 2)
test_Ready :: Test
test_Ready = assertions "ready" $ do
k <- newKnob "abcde"
h <- newFileHandle k "foo.txt" ReadMode
ready <- liftIO $ hReady h
$expect ready
_ <- liftIO $ Data.ByteString.hGet h 10
$expect $ throwsEq
(GHC.IOError (Just h) GHC.EOF "hWaitForInput" "" Nothing (Just "foo.txt"))
(hReady h)
test_Close :: Test
test_Close = assertions "close" $ do
k <- newKnob "abcde"
h <- newFileHandle k "foo.txt" ReadMode
liftIO $ hClose h
$expect $ throwsEq
(GHC.IOError (Just h) GHC.IllegalOperation "hGetBuf" "handle is closed" Nothing (Just "foo.txt"))
(Data.ByteString.hGet h 1)
$expect $ throwsEq
(GHC.IOError (Just h) GHC.IllegalOperation "hWaitForInput" "handle is closed" Nothing (Just "foo.txt"))
(hReady h)
test_SetSize_Read :: Test
test_SetSize_Read = assertions "ReadMode" $ do
k <- newKnob "abcde"
h <- newFileHandle k "foo.txt" ReadMode
let intPlusOne = toInteger (maxBound :: Int) + 1
$expect $ throwsEq
(GHC.IOError (Just h) GHC.InvalidArgument "hSetFileSize" "size > (maxBound :: Int)" Nothing (Just "foo.txt"))
(hSetFileSize h intPlusOne)
$expect $ throwsEq
(GHC.IOError (Just h) GHC.IllegalOperation "hSetFileSize" "handle in ReadMode" Nothing (Just "foo.txt"))
(hSetFileSize h 2)
test_SetSize_Write :: Test
test_SetSize_Write = assertions "WriteMode" $ do
k <- newKnob "abcde"
h <- newFileHandle k "foo.txt" WriteMode
let intPlusOne = toInteger (maxBound :: Int) + 1
$expect $ throwsEq
(GHC.IOError (Just h) GHC.InvalidArgument "hSetFileSize" "size > (maxBound :: Int)" Nothing (Just "foo.txt"))
(hSetFileSize h intPlusOne)
-- Resets contents to all NULL, regardless of offset
liftIO $ hSeek h AbsoluteSeek 2
liftIO $ hSetFileSize h 4
bytes <- Data.Knob.getContents k
$expect (equal bytes "\0\0\0\0")
test_SetSize_ReadWrite :: Test
test_SetSize_ReadWrite = assertions "ReadWriteMode" $ do
k <- newKnob "abcde"
h <- newFileHandle k "foo.txt" ReadWriteMode
let intPlusOne = toInteger (maxBound :: Int) + 1
$expect $ throwsEq
(GHC.IOError (Just h) GHC.InvalidArgument "hSetFileSize" "size > (maxBound :: Int)" Nothing (Just "foo.txt"))
(hSetFileSize h intPlusOne)
-- Truncates contents, regardless of offset
do
liftIO $ hSeek h AbsoluteSeek 2
liftIO $ hSetFileSize h 4
bytes <- Data.Knob.getContents k
$expect (equal bytes "abcd")
do
liftIO $ hSetFileSize h 6
bytes <- Data.Knob.getContents k
$expect (equal bytes "abcd\0\0")
test_SetSize_Append :: Test
test_SetSize_Append = assertions "AppendMode" $ do
k <- newKnob "abcde"
h <- newFileHandle k "foo.txt" AppendMode
let intPlusOne = toInteger (maxBound :: Int) + 1
$expect $ throwsEq
(GHC.IOError (Just h) GHC.InvalidArgument "hSetFileSize" "size > (maxBound :: Int)" Nothing (Just "foo.txt"))
(hSetFileSize h intPlusOne)
do
liftIO $ hSetFileSize h 4
bytes <- Data.Knob.getContents k
$expect (equal bytes "abcd")
do
liftIO $ hSetFileSize h 6
bytes <- Data.Knob.getContents k
$expect (equal bytes "abcd\0\0")
test_SetContents :: Test
test_SetContents = assertions "setContents" $ do
k <- newKnob "abcde"
before <- Data.Knob.getContents k
setContents k "foo"
after <- Data.Knob.getContents k
$expect (equal before "abcde")
$expect (equal after "foo")
test_WithFileHandle :: Test
test_WithFileHandle = assertions "withFileHandle" $ do
k <- newKnob ""
h <- withFileHandle k "test.txt" WriteMode $ \h -> do
Data.ByteString.hPut h "abcde"
return h
bytes <- Data.Knob.getContents k
$expect (equal bytes "abcde")
closed <- liftIO $ hIsClosed h
$expect closed
suite_Duplex :: Suite
suite_Duplex = suite "duplex" []
|