diff options
author | Timothee Cour <timothee.cour2@gmail.com> | 2021-02-22 11:13:08 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-22 20:13:08 +0100 |
commit | ce7caec4b3beab913d801e6be7f0bed0728c791c (patch) | |
tree | 90925ddba95a130a36dec641e31b3c63ae62cf35 /tests/stdlib/tio.nim | |
parent | a1f41137059fcb5b16e3c95b1e63c4a5ed08a408 (diff) | |
download | Nim-ce7caec4b3beab913d801e6be7f0bed0728c791c.tar.gz |
add io.readChars overload (simpler, less error prone) (#16044)
* add simpler to use readChars overload * use new readChars overload * Update lib/wrappers/openssl.nim Co-authored-by: Andreas Rumpf <rumpf_a@web.de> Co-authored-by: flywind <xzsflywind@gmail.com>
Diffstat (limited to 'tests/stdlib/tio.nim')
-rw-r--r-- | tests/stdlib/tio.nim | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/tests/stdlib/tio.nim b/tests/stdlib/tio.nim new file mode 100644 index 000000000..0da64f9c2 --- /dev/null +++ b/tests/stdlib/tio.nim @@ -0,0 +1,37 @@ +# xxx move to here other tests that belong here; io is a proper module + +import std/os +from stdtest/specialpaths import buildDir + +block: # readChars + let file = buildDir / "D20201118T205105.txt" + let s = "he\0l\0lo" + writeFile(file, s) + defer: removeFile(file) + let f = open(file) + defer: close(f) + let n = f.getFileInfo.blockSize + var buf = newString(n) + template fn = + let n2 = f.readChars(buf) + doAssert n2 == s.len + doAssert buf[0..<n2] == s + fn() + setFilePos(f, 0) + fn() + + block: + setFilePos(f, 0) + var s2: string + let nSmall = 2 + for ai in buf.mitems: ai = '\0' + var n2s: seq[int] + while true: + let n2 = f.readChars(toOpenArray(buf, 0, nSmall-1)) + # xxx: maybe we could support: toOpenArray(buf, 0..nSmall) + n2s.add n2 + s2.add buf[0..<n2] + if n2 == 0: + break + doAssert n2s == @[2,2,2,1,0] + doAssert s2 == s |