files.lua 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. local file = os.tmpname()
  2. local otherfile = os.tmpname()
  3. -- assert(os.setlocale('C', 'all'))
  4. io.input(io.stdin); io.output(io.stdout);
  5. os.remove(file)
  6. assert(loadfile(file) == nil)
  7. assert(io.open(file) == nil)
  8. io.output(file)
  9. assert(io.output() ~= io.stdout)
  10. assert(io.output():seek() == 0)
  11. assert(io.write("alo alo"))
  12. assert(io.output():seek() == string.len("alo alo"))
  13. assert(io.output():seek("cur", -3) == string.len("alo alo")-3)
  14. assert(io.write("joao"))
  15. assert(io.output():seek("end") == string.len("alo joao"))
  16. assert(io.output():seek("set") == 0)
  17. assert(io.write('"álo"', "{a}\n", "second line\n", "third line \n"))
  18. assert(io.write('çfourth_line'))
  19. io.output():close()
  20. io.output(io.stdout)
  21. -- collectgarbage() -- file should be closed by GC
  22. assert(io.input() == io.stdin and rawequal(io.output(), io.stdout))
  23. print('+')
  24. -- test GC for files
  25. -- collectgarbage()
  26. -- for i=1,120 do
  27. -- for i=1,5 do
  28. -- io.input(file)
  29. -- assert(io.open(file, 'r'))
  30. -- io.lines(file)
  31. -- end
  32. -- collectgarbage()
  33. -- end
  34. assert(os.rename(file, otherfile))
  35. assert(os.rename(file, otherfile) == nil)
  36. io.output(io.open(otherfile, "a"))
  37. assert(io.write("\n\n\t\t 3450\n"));
  38. io.close()
  39. -- test line generators
  40. assert(os.rename(otherfile, file))
  41. io.output(otherfile)
  42. local f = io.lines(file)
  43. while f() do end;
  44. assert(not pcall(f)) -- read lines after EOF
  45. assert(not pcall(f)) -- read lines after EOF
  46. -- copy from file to otherfile
  47. for l in io.lines(file) do io.write(l, "\n") end
  48. io.close()
  49. -- copy from otherfile back to file
  50. local f = assert(io.open(otherfile))
  51. assert(io.type(f) == "file")
  52. io.output(file)
  53. assert(io.output():read() == nil)
  54. for l in f:lines() do io.write(l, "\n") end
  55. assert(f:close()); io.close()
  56. assert(not pcall(io.close, f)) -- error trying to close again
  57. assert(tostring(f) == "file (closed)")
  58. assert(io.type(f) == "closed file")
  59. io.input(file)
  60. local of =io.open(otherfile)
  61. f = of:lines()
  62. for l in io.lines() do
  63. assert(l == f())
  64. end
  65. of:close()
  66. assert(os.remove(otherfile))
  67. assert(io.close(io.input()))
  68. io.input(file)
  69. do -- test error returns
  70. local a,b,c = io.input():write("xuxu")
  71. assert(not a and type(b) == "string" and type(c) == "number")
  72. end
  73. assert(io.read(0) == "") -- not eof
  74. assert(io.read(5, '*l') == '"álo"')
  75. assert(io.read(0) == "")
  76. assert(io.read() == "second line")
  77. local x = io.input():seek()
  78. assert(io.read() == "third line ")
  79. assert(io.input():seek("set", x))
  80. assert(io.read('*l') == "third line ")
  81. assert(io.read(1) == "ç")
  82. assert(io.read(string.len"fourth_line") == "fourth_line")
  83. assert(io.input():seek("cur", -string.len"fourth_line"))
  84. assert(io.read() == "fourth_line")
  85. assert(io.read() == "") -- empty line
  86. assert(io.read('*n') == 3450)
  87. assert(io.read(1) == '\n')
  88. assert(io.read(0) == nil) -- end of file
  89. assert(io.read(1) == nil) -- end of file
  90. assert(({io.read(1)})[2] == nil)
  91. assert(io.read() == nil) -- end of file
  92. assert(({io.read()})[2] == nil)
  93. assert(io.read('*n') == nil) -- end of file
  94. assert(({io.read('*n')})[2] == nil)
  95. assert(io.read('*a') == '') -- end of file (OK for `*a')
  96. assert(io.read('*a') == '') -- end of file (OK for `*a')
  97. collectgarbage()
  98. print('+')
  99. io.close(io.input())
  100. assert(not pcall(io.read))
  101. assert(os.remove(file))
  102. local t = '0123456789'
  103. for i=1,12 do t = t..t; end
  104. assert(string.len(t) == 10*2^12)
  105. io.output(file)
  106. io.write("alo\n")
  107. io.close()
  108. assert(not pcall(io.write))
  109. local f = io.open(file, "a")
  110. io.output(f)
  111. collectgarbage()
  112. assert(io.write(' ' .. t .. ' '))
  113. assert(io.write(';', 'end of file\n'))
  114. f:flush(); io.flush()
  115. f:close()
  116. print('+')
  117. io.input(file)
  118. assert(io.read() == "alo")
  119. assert(io.read(1) == ' ')
  120. assert(io.read(string.len(t)) == t)
  121. assert(io.read(1) == ' ')
  122. assert(io.read(0))
  123. assert(io.read('*a') == ';end of file\n')
  124. assert(io.read(0) == nil)
  125. assert(io.close(io.input()))
  126. assert(os.remove(file))
  127. print('+')
  128. local x1 = "string\n\n\\com \"\"''coisas [[estranhas]] ]]'"
  129. io.output(file)
  130. assert(io.write(string.format("x2 = %q\n-- comment without ending EOS", x1)))
  131. io.close()
  132. assert(loadfile(file))()
  133. assert(x1 == x2)
  134. print('+')
  135. assert(os.remove(file))
  136. assert(os.remove(file) == nil)
  137. assert(os.remove(otherfile) == nil)
  138. io.output(file)
  139. assert(io.write("qualquer coisa\n"))
  140. assert(io.write("mais qualquer coisa"))
  141. io.close()
  142. io.output(assert(io.open(otherfile, 'wb')))
  143. assert(io.write("outra coisa\0\1\3\0\0\0\0\255\0"))
  144. io.close()
  145. local filehandle = assert(io.open(file, 'r'))
  146. local otherfilehandle = assert(io.open(otherfile, 'rb'))
  147. assert(filehandle ~= otherfilehandle)
  148. assert(type(filehandle) == "userdata")
  149. assert(filehandle:read('*l') == "qualquer coisa")
  150. io.input(otherfilehandle)
  151. assert(io.read(string.len"outra coisa") == "outra coisa")
  152. assert(filehandle:read('*l') == "mais qualquer coisa")
  153. filehandle:close();
  154. assert(type(filehandle) == "userdata")
  155. io.input(otherfilehandle)
  156. assert(io.read(4) == "\0\1\3\0")
  157. assert(io.read(3) == "\0\0\0")
  158. assert(io.read(0) == "") -- 255 is not eof
  159. assert(io.read(1) == "\255")
  160. assert(io.read('*a') == "\0")
  161. assert(not io.read(0))
  162. assert(otherfilehandle == io.input())
  163. otherfilehandle:close()
  164. assert(os.remove(file))
  165. assert(os.remove(otherfile))
  166. collectgarbage()
  167. io.output(file)
  168. io.write[[
  169. 123.4 -56e-2 not a number
  170. second line
  171. third line
  172. and the rest of the file
  173. ]]
  174. io.close()
  175. io.input(file)
  176. local _,a,b,c,d,e,h,__ = io.read(1, '*n', '*n', '*l', '*l', '*l', '*a', 10)
  177. assert(io.close(io.input()))
  178. assert(_ == ' ' and __ == nil)
  179. assert(type(a) == 'number' and a==123.4 and b==-56e-2)
  180. assert(d=='second line' and e=='third line')
  181. assert(h==[[
  182. and the rest of the file
  183. ]])
  184. assert(os.remove(file))
  185. collectgarbage()
  186. -- testing buffers
  187. do
  188. local f = assert(io.open(file, "w"))
  189. local fr = assert(io.open(file, "r"))
  190. assert(f:setvbuf("full", 2000))
  191. f:write("x")
  192. assert(fr:read("*all") == "") -- full buffer; output not written yet
  193. f:close()
  194. fr:seek("set")
  195. assert(fr:read("*all") == "x") -- `close' flushes it
  196. f = assert(io.open(file, "w"))
  197. assert(f:setvbuf("no"))
  198. f:write("x")
  199. fr:seek("set")
  200. assert(fr:read("*all") == "x") -- no buffer; output is ready
  201. f:close()
  202. fr:close()
  203. -- f = assert(io.open(file, "a"))
  204. -- assert(f:setvbuf("line"))
  205. -- f:write("x")
  206. -- fr:seek("set", 1)
  207. -- assert(fr:read("*all") == "") -- line buffer; no output without `\n'
  208. -- f:write("a\n")
  209. -- fr:seek("set", 1)
  210. -- assert(fr:read("*all") == "xa\n") -- now we have a whole line
  211. -- f:close(); fr:close()
  212. end
  213. -- testing large files (> BUFSIZ)
  214. io.output(file)
  215. for i=1,5001 do io.write('0123456789123') end
  216. io.write('\n12346')
  217. io.close()
  218. io.input(file)
  219. local x = io.read('*a')
  220. io.input():seek('set', 0)
  221. local y = io.read(30001)..io.read(1005)..io.read(0)..io.read(1)..io.read(100003)
  222. assert(x == y and string.len(x) == 5001*13 + 6)
  223. io.input():seek('set', 0)
  224. y = io.read() -- huge line
  225. assert(x == y..'\n'..io.read())
  226. assert(io.read() == nil)
  227. io.close(io.input())
  228. assert(os.remove(file))
  229. x = nil; y = nil
  230. x, y = pcall(io.popen, "ls")
  231. if x then
  232. assert(y:read("*a"))
  233. assert(y:close())
  234. else
  235. (Message or print)('\a\n >>> popen not available<<<\n\a')
  236. end
  237. print'+'
  238. local t = os.time()
  239. -- T = os.date("*t", t)
  240. -- loadstring(os.date([[assert(T.year==%Y and T.month==%m and T.day==%d and
  241. -- T.hour==%H and T.min==%M and T.sec==%S and
  242. -- T.wday==%w+1 and T.yday==%j and type(T.isdst) == 'boolean')]], t))()
  243. --
  244. -- assert(os.time(T) == t)
  245. --
  246. T = os.date("!*t", t)
  247. -- loadstring(os.date([[!assert(T.year==%Y and T.month==%m and T.day==%d and
  248. -- T.hour==%H and T.min==%M and T.sec==%S and
  249. -- T.wday==%w+1 and T.yday==%j and type(T.isdst) == 'boolean')]], t))()
  250. do
  251. local T = os.date("*t")
  252. local t = os.time(T)
  253. assert(type(T.isdst) == 'boolean')
  254. T.isdst = nil
  255. local t1 = os.time(T)
  256. assert(t == t1) -- if isdst is absent uses correct default
  257. end
  258. t = os.time(T)
  259. T.year = T.year-1;
  260. local t1 = os.time(T)
  261. -- allow for leap years
  262. assert(math.abs(os.difftime(t,t1)/(24*3600) - 365) < 2)
  263. t = os.time()
  264. t1 = os.time(os.date("*t"))
  265. assert(os.difftime(t1,t) <= 2)
  266. local t1 = os.time{year=2000, month=10, day=1, hour=23, min=12, sec=17}
  267. local t2 = os.time{year=2000, month=10, day=1, hour=23, min=10, sec=19}
  268. assert(os.difftime(t1,t2) == 60*2-2)
  269. io.output(io.stdout)
  270. local d = os.date('%d')
  271. local m = os.date('%m')
  272. local a = os.date('%Y')
  273. local ds = os.date('%w') + 1
  274. local h = os.date('%H')
  275. local min = os.date('%M')
  276. local s = os.date('%S')
  277. io.write(string.format('test done on %2.2d/%2.2d/%d', d, m, a))
  278. io.write(string.format(', at %2.2d:%2.2d:%2.2d\n', h, min, s))
  279. io.write(string.format('%s\n', _VERSION))