-
Notifications
You must be signed in to change notification settings - Fork 141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Making ByteString.hGetLine behave like System.IO.hGetLine #327
base: master
Are you sure you want to change the base?
Changes from 4 commits
5b169ec
a70a788
537080c
a3d4c5f
6f6a098
3e34f48
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1614,6 +1614,36 @@ prop_read_write_file_D x = unsafePerformIO $ do | |
(const $ do y <- D.readFile f | ||
return (x==y)) | ||
|
||
prop_hgetline_like_s8_hgetline (LinedASCII filetext) (lineEndIn, lineEndOut) = idempotentIOProperty $ do | ||
tid <- myThreadId | ||
let f = "qc-test-"++show tid | ||
let newlineMode = NewlineMode (if lineEndIn then LF else CRLF) (if lineEndOut then LF else CRLF) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a pity that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I raised an issue (nick8325/quickcheck#322) for this, so hopefully There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, it is not worth for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pull request made, in the process I realized I should flip the conditional because QuickCheck attempts to shrink |
||
bracket_ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not much point to use |
||
(writeFile f filetext) | ||
dbramucci marked this conversation as resolved.
Show resolved
Hide resolved
|
||
(removeFile f) | ||
$ do | ||
bsLines <- withFile f ReadMode (\h -> do | ||
hSetNewlineMode h newlineMode | ||
readByLines C.hGetLine h | ||
) | ||
sLines <- withFile f ReadMode (\h -> do | ||
hSetNewlineMode h newlineMode | ||
readByLines System.IO.hGetLine h | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be possible to reduce code duplication here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One way would be to write a readFileByLines filename getLine = withFile filename ReadMode (\h -> do
hSetNewlineMode h newlineMode
readByLines getLine h
) Alternatively, because it is expensive to open files on Windows it would be possible to use just the hPutStr h_ filetext
hFlush h_
let readByLinesFromStart getLine = hSeek h_ AbsoluteSeek 0 *> readByLines getLine h_
hSetNewlineMode h_ newlineMode
bsLines <- readByLinesFromStart C.hGetLine
sLines <- readByLinesFromStart System.IO.hGetLine Then instead of open 3 files per test-case, we open 1 file. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do not particularly care about performance of the test suite, I'd rather keep tests as straightforward and focused as possible. |
||
return $ map C.unpack bsLines === sLines | ||
|
||
where | ||
readByLines getLine h_ = go [] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it better than a more naive implementation? readByLines getLine h_ = do
isEnd <- hIsEOF h_
if isEnd then return [] else (:) <$> getLine h_ <*> readByLines getLine h_ |
||
where | ||
go lines = do | ||
isEnd <- hIsEOF h_ | ||
if isEnd | ||
then return lines | ||
else do | ||
!nextLine <- getLine h_ | ||
go (nextLine : lines) | ||
|
||
|
||
------------------------------------------------------------------------ | ||
|
||
prop_append_file_P x y = unsafePerformIO $ do | ||
|
@@ -1791,7 +1821,8 @@ io_tests = | |
, testProperty "appendFile " prop_append_file_D | ||
|
||
, testProperty "packAddress " prop_packAddress | ||
|
||
|
||
, testProperty "pack.hGetLine=hGetLine" prop_hgetline_like_s8_hgetline | ||
] | ||
|
||
misc_tests = | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
openTempFile
, e. g.,bytestring/tests/LazyHClose.hs
Line 18 in 54133b3