tio.nim 890 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. # xxx move to here other tests that belong here; io is a proper module
  2. import std/os
  3. from stdtest/specialpaths import buildDir
  4. block: # readChars
  5. let file = buildDir / "D20201118T205105.txt"
  6. let s = "he\0l\0lo"
  7. writeFile(file, s)
  8. defer: removeFile(file)
  9. let f = open(file)
  10. defer: close(f)
  11. let n = f.getFileInfo.blockSize
  12. var buf = newString(n)
  13. template fn =
  14. let n2 = f.readChars(buf)
  15. doAssert n2 == s.len
  16. doAssert buf[0..<n2] == s
  17. fn()
  18. setFilePos(f, 0)
  19. fn()
  20. block:
  21. setFilePos(f, 0)
  22. var s2: string
  23. let nSmall = 2
  24. for ai in buf.mitems: ai = '\0'
  25. var n2s: seq[int]
  26. while true:
  27. let n2 = f.readChars(toOpenArray(buf, 0, nSmall-1))
  28. # xxx: maybe we could support: toOpenArray(buf, 0..nSmall)
  29. n2s.add n2
  30. s2.add buf[0..<n2]
  31. if n2 == 0:
  32. break
  33. doAssert n2s == @[2,2,2,1,0]
  34. doAssert s2 == s