tio.nim 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. import std/[assertions, syncio]
  5. block: # readChars
  6. let file = buildDir / "D20201118T205105.txt"
  7. let s = "he\0l\0lo"
  8. writeFile(file, s)
  9. defer: removeFile(file)
  10. let f = open(file)
  11. defer: close(f)
  12. let n = f.getFileInfo.blockSize
  13. var buf = newString(n)
  14. template fn =
  15. let n2 = f.readChars(buf)
  16. doAssert n2 == s.len
  17. doAssert buf[0..<n2] == s
  18. fn()
  19. setFilePos(f, 0)
  20. fn()
  21. block:
  22. setFilePos(f, 0)
  23. var s2: string
  24. let nSmall = 2
  25. for ai in buf.mitems: ai = '\0'
  26. var n2s: seq[int]
  27. while true:
  28. let n2 = f.readChars(toOpenArray(buf, 0, nSmall-1))
  29. # xxx: maybe we could support: toOpenArray(buf, 0..nSmall)
  30. n2s.add n2
  31. s2.add buf[0..<n2]
  32. if n2 == 0:
  33. break
  34. doAssert n2s == @[2,2,2,1,0]
  35. doAssert s2 == s
  36. import std/strutils
  37. block: # bug #21273
  38. let FILE = buildDir / "D20220119T134305.txt"
  39. let hex = "313632313920313632343720313632353920313632363020313632393020323035363520323037323120323131353020323239393820323331303520323332313020323332343820323332363820"
  40. writeFile FILE, parseHexStr(hex)
  41. doAssert readFile(FILE).toHex == hex
  42. let f = open(FILE)
  43. var s = newString(80)
  44. while f.readLine(s):
  45. doAssert s.toHex == hex