Implement readInto and read(n)

This commit is contained in:
Zahary Karadjov 2020-05-01 21:39:08 +03:00
commit bf930b6f1b
No known key found for this signature in database
GPG key ID: C8936F8A3073D609
12 changed files with 636 additions and 220 deletions

View file

@ -1,23 +1,121 @@
{.used.}
import
os, unittest, strutils, stew/ranges/ptr_arith,
../faststreams
os, unittest, strutils, random,
stew/ranges/ptr_arith, testutils,
../faststreams, ../faststreams/textio
suite "input stream":
test "empty input":
setCurrentDir getAppDir()
proc bytes(s: string): seq[byte] =
result = newSeqOfCap[byte](s.len)
for c in s: result.add byte(c)
proc str(bytes: openarray[byte]): string =
result = newStringOfCap(bytes.len)
for b in bytes:
result.add b.char
proc countLines(s: InputStream): Natural =
for s in lines(s):
inc result
const
asciiTableFile = "files" / "ascii_table.txt"
asciiTableContents = slurp(asciiTableFile)
procSuite "input stream":
template emptyInputTests(suiteName, setupCode: untyped) =
suite suiteName & " empty inputs":
setup setupCode
test "input is not readable with read":
check not input.readable
expect Defect:
echo "This read should not complete: ", input.read
test "input is not readable with read(n)":
check not input.readable(10)
expect Defect:
echo "This read should not complete: ", input.read(10)
test "next returns none":
check input.next.isNone
emptyInputTests "unsafeMemoryInput":
var str = ""
var i = unsafeMemoryInput(str)
var input = unsafeMemoryInput(str)
check:
i.readable == false
i.next.isNone
emptyInputTests "fileInput":
var input = memFileInput("files" / "empty_file")
expect Defect:
echo i.read
emptyInputTests "memFileInput":
var input = fileInput("files" / "empty_file")
template asciiTableFileTest(name: string, body: untyped) =
test name & " of ascii table with regular pageSize":
var input {.inject.} = fileInput(asciiTableFile)
try:
body
finally:
close input
test name & " of ascii table with pageSize = 10":
var input {.inject.} = fileInput(asciiTableFile, pageSize = 10)
try:
body
finally:
close input
test name & " of ascii table with pageSize = 1":
var input {.inject.} = fileInput(asciiTableFile, pageSize = 1)
try:
body
finally:
close input
asciiTableFileTest "count lines":
check countLines(input) == 34
asciiTableFileTest "mixed read types":
randomize(10000)
var fileContents = ""
while true:
let r = rand(100)
if r < 20:
let readSize = 1 + rand(10)
var buf = newSeq[byte](readSize)
let bytesRead = input.readIntoEx(buf)
fileContents.add buf.toOpenArray(0, bytesRead - 1).str
if bytesRead < buf.len:
break
elif r < 50:
let readSize = 6 + rand(10)
if input.readable(readSize):
fileContents.add input.read(readSize).str
else:
while input.readable:
fileContents.add input.read.char
break
else:
if input.readable:
fileContents.add input.read.char
# You can uncomment this to get earlier failure in the test:
when false:
require fileContents == asciiTableContents[0 ..< fileContents.len]
check fileContents == asciiTableContents
test "missing file input":
const fileName = "there-is-no-such-faststreams-file-1"
const fileName = "there-is-no-such-faststreams-file"
check not fileExists(fileName)
expect CatchableError: discard fileInput(fileName)