tio.nim 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. discard """
  2. matrix: "--mm:refc; --mm:orc"
  3. """
  4. # xxx move to here other tests that belong here; io is a proper module
  5. import std/os
  6. from stdtest/specialpaths import buildDir
  7. import std/[assertions, syncio]
  8. block: # readChars
  9. let file = buildDir / "D20201118T205105.txt"
  10. let s = "he\0l\0lo"
  11. writeFile(file, s)
  12. defer: removeFile(file)
  13. let f = open(file)
  14. defer: close(f)
  15. let n = f.getFileInfo.blockSize
  16. var buf = newString(n)
  17. template fn =
  18. let n2 = f.readChars(buf)
  19. doAssert n2 == s.len
  20. doAssert buf[0..<n2] == s
  21. fn()
  22. setFilePos(f, 0)
  23. fn()
  24. block:
  25. setFilePos(f, 0)
  26. var s2: string
  27. let nSmall = 2
  28. for ai in buf.mitems: ai = '\0'
  29. var n2s: seq[int]
  30. while true:
  31. let n2 = f.readChars(toOpenArray(buf, 0, nSmall-1))
  32. # xxx: maybe we could support: toOpenArray(buf, 0..nSmall)
  33. n2s.add n2
  34. s2.add buf[0..<n2]
  35. if n2 == 0:
  36. break
  37. doAssert n2s == @[2,2,2,1,0]
  38. doAssert s2 == s
  39. import std/strutils
  40. block: # bug #21273
  41. let FILE = buildDir / "D20220119T134305.txt"
  42. let hex = "313632313920313632343720313632353920313632363020313632393020323035363520323037323120323131353020323239393820323331303520323332313020323332343820323332363820"
  43. writeFile FILE, parseHexStr(hex)
  44. doAssert readFile(FILE).toHex == hex
  45. let f = open(FILE)
  46. var s = newString(80)
  47. while f.readLine(s):
  48. doAssert s.toHex == hex