files.lua 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. -- $Id: testes/files.lua $
  2. -- See Copyright Notice in file all.lua
  3. local debug = require "debug"
  4. local maxint = math.maxinteger
  5. assert(type(os.getenv"PATH") == "string")
  6. assert(io.input(io.stdin) == io.stdin)
  7. assert(not pcall(io.input, "non-existent-file"))
  8. assert(io.output(io.stdout) == io.stdout)
  9. local function testerr (msg, f, ...)
  10. local stat, err = pcall(f, ...)
  11. return (not stat and string.find(err, msg, 1, true))
  12. end
  13. local function checkerr (msg, f, ...)
  14. assert(testerr(msg, f, ...))
  15. end
  16. -- cannot close standard files
  17. assert(not io.close(io.stdin) and
  18. not io.stdout:close() and
  19. not io.stderr:close())
  20. -- cannot call close method without an argument (new in 5.3.5)
  21. checkerr("got no value", io.stdin.close)
  22. assert(type(io.input()) == "userdata" and io.type(io.output()) == "file")
  23. assert(type(io.stdin) == "userdata" and io.type(io.stderr) == "file")
  24. assert(not io.type(8))
  25. local a = {}; setmetatable(a, {})
  26. assert(not io.type(a))
  27. assert(getmetatable(io.input()).__name == "FILE*")
  28. local a,b,c = io.open('xuxu_nao_existe')
  29. assert(not a and type(b) == "string" and type(c) == "number")
  30. a,b,c = io.open('/a/b/c/d', 'w')
  31. assert(not a and type(b) == "string" and type(c) == "number")
  32. local file = os.tmpname()
  33. local f, msg = io.open(file, "w")
  34. if not f then
  35. (Message or print)("'os.tmpname' file cannot be open; skipping file tests")
  36. else --{ most tests here need tmpname
  37. f:close()
  38. print('testing i/o')
  39. local otherfile = os.tmpname()
  40. checkerr("invalid mode", io.open, file, "rw")
  41. checkerr("invalid mode", io.open, file, "rb+")
  42. checkerr("invalid mode", io.open, file, "r+bk")
  43. checkerr("invalid mode", io.open, file, "")
  44. checkerr("invalid mode", io.open, file, "+")
  45. checkerr("invalid mode", io.open, file, "b")
  46. assert(io.open(file, "r+b")):close()
  47. assert(io.open(file, "r+")):close()
  48. assert(io.open(file, "rb")):close()
  49. assert(os.setlocale('C', 'all'))
  50. io.input(io.stdin); io.output(io.stdout);
  51. os.remove(file)
  52. assert(not loadfile(file))
  53. checkerr("", dofile, file)
  54. assert(not io.open(file))
  55. io.output(file)
  56. assert(io.output() ~= io.stdout)
  57. if not _port then -- invalid seek
  58. local status, msg, code = io.stdin:seek("set", 1000)
  59. assert(not status and type(msg) == "string" and type(code) == "number")
  60. end
  61. assert(io.output():seek() == 0)
  62. assert(io.write("alo alo"):seek() == string.len("alo alo"))
  63. assert(io.output():seek("cur", -3) == string.len("alo alo")-3)
  64. assert(io.write("joao"))
  65. assert(io.output():seek("end") == string.len("alo joao"))
  66. assert(io.output():seek("set") == 0)
  67. assert(io.write('"álo"', "{a}\n", "second line\n", "third line \n"))
  68. assert(io.write('çfourth_line'))
  69. io.output(io.stdout)
  70. collectgarbage() -- file should be closed by GC
  71. assert(io.input() == io.stdin and rawequal(io.output(), io.stdout))
  72. print('+')
  73. -- test GC for files
  74. collectgarbage()
  75. for i=1,120 do
  76. for i=1,5 do
  77. io.input(file)
  78. assert(io.open(file, 'r'))
  79. io.lines(file)
  80. end
  81. collectgarbage()
  82. end
  83. io.input():close()
  84. io.close()
  85. assert(os.rename(file, otherfile))
  86. assert(not os.rename(file, otherfile))
  87. io.output(io.open(otherfile, "ab"))
  88. assert(io.write("\n\n\t\t ", 3450, "\n"));
  89. io.close()
  90. do
  91. -- closing file by scope
  92. local F = nil
  93. do
  94. local <toclose> f = assert(io.open(file, "w"))
  95. F = f
  96. end
  97. assert(tostring(F) == "file (closed)")
  98. end
  99. assert(os.remove(file))
  100. do
  101. -- test writing/reading numbers
  102. local <toclose> f = assert(io.open(file, "w"))
  103. f:write(maxint, '\n')
  104. f:write(string.format("0X%x\n", maxint))
  105. f:write("0xABCp-3", '\n')
  106. f:write(0, '\n')
  107. f:write(-maxint, '\n')
  108. f:write(string.format("0x%X\n", -maxint))
  109. f:write("-0xABCp-3", '\n')
  110. assert(f:close())
  111. local <toclose> f = assert(io.open(file, "r"))
  112. assert(f:read("n") == maxint)
  113. assert(f:read("n") == maxint)
  114. assert(f:read("n") == 0xABCp-3)
  115. assert(f:read("n") == 0)
  116. assert(f:read("*n") == -maxint) -- test old format (with '*')
  117. assert(f:read("n") == -maxint)
  118. assert(f:read("*n") == -0xABCp-3) -- test old format (with '*')
  119. end
  120. assert(os.remove(file))
  121. -- testing multiple arguments to io.read
  122. do
  123. local <toclose> f = assert(io.open(file, "w"))
  124. f:write[[
  125. a line
  126. another line
  127. 1234
  128. 3.45
  129. one
  130. two
  131. three
  132. ]]
  133. local l1, l2, l3, l4, n1, n2, c, dummy
  134. assert(f:close())
  135. local <toclose> f = assert(io.open(file, "r"))
  136. l1, l2, n1, n2, dummy = f:read("l", "L", "n", "n")
  137. assert(l1 == "a line" and l2 == "another line\n" and
  138. n1 == 1234 and n2 == 3.45 and dummy == nil)
  139. assert(f:close())
  140. local <toclose> f = assert(io.open(file, "r"))
  141. l1, l2, n1, n2, c, l3, l4, dummy = f:read(7, "l", "n", "n", 1, "l", "l")
  142. assert(l1 == "a line\n" and l2 == "another line" and c == '\n' and
  143. n1 == 1234 and n2 == 3.45 and l3 == "one" and l4 == "two"
  144. and dummy == nil)
  145. assert(f:close())
  146. local <toclose> f = assert(io.open(file, "r"))
  147. -- second item failing
  148. l1, n1, n2, dummy = f:read("l", "n", "n", "l")
  149. assert(l1 == "a line" and n1 == nil)
  150. end
  151. assert(os.remove(file))
  152. -- test yielding during 'dofile'
  153. f = assert(io.open(file, "w"))
  154. f:write[[
  155. local x, z = coroutine.yield(10)
  156. local y = coroutine.yield(20)
  157. return x + y * z
  158. ]]
  159. assert(f:close())
  160. f = coroutine.wrap(dofile)
  161. assert(f(file) == 10)
  162. assert(f(100, 101) == 20)
  163. assert(f(200) == 100 + 200 * 101)
  164. assert(os.remove(file))
  165. f = assert(io.open(file, "w"))
  166. -- test number termination
  167. f:write[[
  168. -12.3- -0xffff+ .3|5.E-3X +234e+13E 0xDEADBEEFDEADBEEFx
  169. 0x1.13Ap+3e
  170. ]]
  171. -- very long number
  172. f:write("1234"); for i = 1, 1000 do f:write("0") end; f:write("\n")
  173. -- invalid sequences (must read and discard valid prefixes)
  174. f:write[[
  175. .e+ 0.e; --; 0xX;
  176. ]]
  177. assert(f:close())
  178. f = assert(io.open(file, "r"))
  179. assert(f:read("n") == -12.3); assert(f:read(1) == "-")
  180. assert(f:read("n") == -0xffff); assert(f:read(2) == "+ ")
  181. assert(f:read("n") == 0.3); assert(f:read(1) == "|")
  182. assert(f:read("n") == 5e-3); assert(f:read(1) == "X")
  183. assert(f:read("n") == 234e13); assert(f:read(1) == "E")
  184. assert(f:read("n") == 0Xdeadbeefdeadbeef); assert(f:read(2) == "x\n")
  185. assert(f:read("n") == 0x1.13aP3); assert(f:read(1) == "e")
  186. do -- attempt to read too long number
  187. assert(f:read("n") == nil) -- fails
  188. local s = f:read("L") -- read rest of line
  189. assert(string.find(s, "^00*\n$")) -- lots of 0's left
  190. end
  191. assert(not f:read("n")); assert(f:read(2) == "e+")
  192. assert(not f:read("n")); assert(f:read(1) == ";")
  193. assert(not f:read("n")); assert(f:read(2) == "-;")
  194. assert(not f:read("n")); assert(f:read(1) == "X")
  195. assert(not f:read("n")); assert(f:read(1) == ";")
  196. assert(not f:read("n")); assert(not f:read(0)) -- end of file
  197. assert(f:close())
  198. assert(os.remove(file))
  199. -- test line generators
  200. assert(not pcall(io.lines, "non-existent-file"))
  201. assert(os.rename(otherfile, file))
  202. io.output(otherfile)
  203. local n = 0
  204. local f = io.lines(file)
  205. while f() do n = n + 1 end;
  206. assert(n == 6) -- number of lines in the file
  207. checkerr("file is already closed", f)
  208. checkerr("file is already closed", f)
  209. -- copy from file to otherfile
  210. n = 0
  211. for l in io.lines(file) do io.write(l, "\n"); n = n + 1 end
  212. io.close()
  213. assert(n == 6)
  214. -- copy from otherfile back to file
  215. local f = assert(io.open(otherfile))
  216. assert(io.type(f) == "file")
  217. io.output(file)
  218. assert(not io.output():read())
  219. n = 0
  220. for l in f:lines() do io.write(l, "\n"); n = n + 1 end
  221. assert(tostring(f):sub(1, 5) == "file ")
  222. assert(f:close()); io.close()
  223. assert(n == 6)
  224. checkerr("closed file", io.close, f)
  225. assert(tostring(f) == "file (closed)")
  226. assert(io.type(f) == "closed file")
  227. io.input(file)
  228. f = io.open(otherfile):lines()
  229. n = 0
  230. for l in io.lines() do assert(l == f()); n = n + 1 end
  231. f = nil; collectgarbage()
  232. assert(n == 6)
  233. assert(os.remove(otherfile))
  234. do -- bug in 5.3.1
  235. io.output(otherfile)
  236. io.write(string.rep("a", 300), "\n")
  237. io.close()
  238. local t ={}; for i = 1, 250 do t[i] = 1 end
  239. t = {io.lines(otherfile, table.unpack(t))()}
  240. -- everything ok here
  241. assert(#t == 250 and t[1] == 'a' and t[#t] == 'a')
  242. t[#t + 1] = 1 -- one too many
  243. checkerr("too many arguments", io.lines, otherfile, table.unpack(t))
  244. collectgarbage() -- ensure 'otherfile' is closed
  245. assert(os.remove(otherfile))
  246. end
  247. io.input(file)
  248. do -- test error returns
  249. local a,b,c = io.input():write("xuxu")
  250. assert(not a and type(b) == "string" and type(c) == "number")
  251. end
  252. checkerr("invalid format", io.read, "x")
  253. assert(io.read(0) == "") -- not eof
  254. assert(io.read(5, 'l') == '"álo"')
  255. assert(io.read(0) == "")
  256. assert(io.read() == "second line")
  257. local x = io.input():seek()
  258. assert(io.read() == "third line ")
  259. assert(io.input():seek("set", x))
  260. assert(io.read('L') == "third line \n")
  261. assert(io.read(1) == "ç")
  262. assert(io.read(string.len"fourth_line") == "fourth_line")
  263. assert(io.input():seek("cur", -string.len"fourth_line"))
  264. assert(io.read() == "fourth_line")
  265. assert(io.read() == "") -- empty line
  266. assert(io.read('n') == 3450)
  267. assert(io.read(1) == '\n')
  268. assert(io.read(0) == nil) -- end of file
  269. assert(io.read(1) == nil) -- end of file
  270. assert(io.read(30000) == nil) -- end of file
  271. assert(({io.read(1)})[2] == undef)
  272. assert(io.read() == nil) -- end of file
  273. assert(({io.read()})[2] == undef)
  274. assert(io.read('n') == nil) -- end of file
  275. assert(({io.read('n')})[2] == undef)
  276. assert(io.read('a') == '') -- end of file (OK for 'a')
  277. assert(io.read('a') == '') -- end of file (OK for 'a')
  278. collectgarbage()
  279. print('+')
  280. io.close(io.input())
  281. checkerr(" input file is closed", io.read)
  282. assert(os.remove(file))
  283. local t = '0123456789'
  284. for i=1,10 do t = t..t; end
  285. assert(string.len(t) == 10*2^10)
  286. io.output(file)
  287. io.write("alo"):write("\n")
  288. io.close()
  289. checkerr(" output file is closed", io.write)
  290. local f = io.open(file, "a+b")
  291. io.output(f)
  292. collectgarbage()
  293. assert(io.write(' ' .. t .. ' '))
  294. assert(io.write(';', 'end of file\n'))
  295. f:flush(); io.flush()
  296. f:close()
  297. print('+')
  298. io.input(file)
  299. assert(io.read() == "alo")
  300. assert(io.read(1) == ' ')
  301. assert(io.read(string.len(t)) == t)
  302. assert(io.read(1) == ' ')
  303. assert(io.read(0))
  304. assert(io.read('a') == ';end of file\n')
  305. assert(io.read(0) == nil)
  306. assert(io.close(io.input()))
  307. -- test errors in read/write
  308. do
  309. local function ismsg (m)
  310. -- error message is not a code number
  311. return (type(m) == "string" and tonumber(m) == nil)
  312. end
  313. -- read
  314. local f = io.open(file, "w")
  315. local r, m, c = f:read()
  316. assert(not r and ismsg(m) and type(c) == "number")
  317. assert(f:close())
  318. -- write
  319. f = io.open(file, "r")
  320. r, m, c = f:write("whatever")
  321. assert(not r and ismsg(m) and type(c) == "number")
  322. assert(f:close())
  323. -- lines
  324. f = io.open(file, "w")
  325. r, m = pcall(f:lines())
  326. assert(r == false and ismsg(m))
  327. assert(f:close())
  328. end
  329. assert(os.remove(file))
  330. -- test for L format
  331. io.output(file); io.write"\n\nline\nother":close()
  332. io.input(file)
  333. assert(io.read"L" == "\n")
  334. assert(io.read"L" == "\n")
  335. assert(io.read"L" == "line\n")
  336. assert(io.read"L" == "other")
  337. assert(io.read"L" == nil)
  338. io.input():close()
  339. local f = assert(io.open(file))
  340. local s = ""
  341. for l in f:lines("L") do s = s .. l end
  342. assert(s == "\n\nline\nother")
  343. f:close()
  344. io.input(file)
  345. s = ""
  346. for l in io.lines(nil, "L") do s = s .. l end
  347. assert(s == "\n\nline\nother")
  348. io.input():close()
  349. s = ""
  350. for l in io.lines(file, "L") do s = s .. l end
  351. assert(s == "\n\nline\nother")
  352. s = ""
  353. for l in io.lines(file, "l") do s = s .. l end
  354. assert(s == "lineother")
  355. io.output(file); io.write"a = 10 + 34\na = 2*a\na = -a\n":close()
  356. local t = {}
  357. assert(load(io.lines(file, "L"), nil, nil, t))()
  358. assert(t.a == -((10 + 34) * 2))
  359. do -- testing closing file in line iteration
  360. -- get the to-be-closed variable from a loop
  361. local function gettoclose (lv)
  362. lv = lv + 1
  363. local stvar = 0 -- to-be-closed is 4th state variable in the loop
  364. for i = 1, 1000 do
  365. local n, v = debug.getlocal(lv, i)
  366. if n == "(for state)" then
  367. stvar = stvar + 1
  368. if stvar == 4 then return v end
  369. end
  370. end
  371. end
  372. local f
  373. for l in io.lines(file) do
  374. f = gettoclose(1)
  375. assert(io.type(f) == "file")
  376. break
  377. end
  378. assert(io.type(f) == "closed file")
  379. f = nil
  380. local function foo (name)
  381. for l in io.lines(name) do
  382. f = gettoclose(1)
  383. assert(io.type(f) == "file")
  384. error(f) -- exit loop with an error
  385. end
  386. end
  387. local st, msg = pcall(foo, file)
  388. assert(st == false and io.type(msg) == "closed file")
  389. end
  390. -- test for multipe arguments in 'lines'
  391. io.output(file); io.write"0123456789\n":close()
  392. for a,b in io.lines(file, 1, 1) do
  393. if a == "\n" then assert(b == nil)
  394. else assert(tonumber(a) == tonumber(b) - 1)
  395. end
  396. end
  397. for a,b,c in io.lines(file, 1, 2, "a") do
  398. assert(a == "0" and b == "12" and c == "3456789\n")
  399. end
  400. for a,b,c in io.lines(file, "a", 0, 1) do
  401. if a == "" then break end
  402. assert(a == "0123456789\n" and b == nil and c == nil)
  403. end
  404. collectgarbage() -- to close file in previous iteration
  405. io.output(file); io.write"00\n10\n20\n30\n40\n":close()
  406. for a, b in io.lines(file, "n", "n") do
  407. if a == 40 then assert(b == nil)
  408. else assert(a == b - 10)
  409. end
  410. end
  411. -- test load x lines
  412. io.output(file);
  413. io.write[[
  414. local y
  415. = X
  416. X =
  417. X *
  418. 2 +
  419. X;
  420. X =
  421. X
  422. - y;
  423. ]]:close()
  424. _G.X = 1
  425. assert(not load((io.lines(file))))
  426. collectgarbage() -- to close file in previous iteration
  427. load((io.lines(file, "L")))()
  428. assert(_G.X == 2)
  429. load((io.lines(file, 1)))()
  430. assert(_G.X == 4)
  431. load((io.lines(file, 3)))()
  432. assert(_G.X == 8)
  433. print('+')
  434. local x1 = "string\n\n\\com \"\"''coisas [[estranhas]] ]]'"
  435. io.output(file)
  436. assert(io.write(string.format("x2 = %q\n-- comment without ending EOS", x1)))
  437. io.close()
  438. assert(loadfile(file))()
  439. assert(x1 == x2)
  440. print('+')
  441. assert(os.remove(file))
  442. assert(not os.remove(file))
  443. assert(not os.remove(otherfile))
  444. -- testing loadfile
  445. local function testloadfile (s, expres)
  446. io.output(file)
  447. if s then io.write(s) end
  448. io.close()
  449. local res = assert(loadfile(file))()
  450. assert(os.remove(file))
  451. assert(res == expres)
  452. end
  453. -- loading empty file
  454. testloadfile(nil, nil)
  455. -- loading file with initial comment without end of line
  456. testloadfile("# a non-ending comment", nil)
  457. -- checking Unicode BOM in files
  458. testloadfile("\xEF\xBB\xBF# some comment\nreturn 234", 234)
  459. testloadfile("\xEF\xBB\xBFreturn 239", 239)
  460. testloadfile("\xEF\xBB\xBF", nil) -- empty file with a BOM
  461. -- checking line numbers in files with initial comments
  462. testloadfile("# a comment\nreturn require'debug'.getinfo(1).currentline", 2)
  463. -- loading binary file
  464. io.output(io.open(file, "wb"))
  465. assert(io.write(string.dump(function () return 10, '\0alo\255', 'hi' end)))
  466. io.close()
  467. a, b, c = assert(loadfile(file))()
  468. assert(a == 10 and b == "\0alo\255" and c == "hi")
  469. assert(os.remove(file))
  470. -- bug in 5.2.1
  471. do
  472. io.output(io.open(file, "wb"))
  473. -- save function with no upvalues
  474. assert(io.write(string.dump(function () return 1 end)))
  475. io.close()
  476. f = assert(loadfile(file, "b", {}))
  477. assert(type(f) == "function" and f() == 1)
  478. assert(os.remove(file))
  479. end
  480. -- loading binary file with initial comment
  481. io.output(io.open(file, "wb"))
  482. assert(io.write("#this is a comment for a binary file\0\n",
  483. string.dump(function () return 20, '\0\0\0' end)))
  484. io.close()
  485. a, b, c = assert(loadfile(file))()
  486. assert(a == 20 and b == "\0\0\0" and c == nil)
  487. assert(os.remove(file))
  488. -- 'loadfile' with 'env'
  489. do
  490. local f = io.open(file, 'w')
  491. f:write[[
  492. if (...) then a = 15; return b, c, d
  493. else return _ENV
  494. end
  495. ]]
  496. f:close()
  497. local t = {b = 12, c = "xuxu", d = print}
  498. local f = assert(loadfile(file, 't', t))
  499. local b, c, d = f(1)
  500. assert(t.a == 15 and b == 12 and c == t.c and d == print)
  501. assert(f() == t)
  502. f = assert(loadfile(file, 't', nil))
  503. assert(f() == nil)
  504. f = assert(loadfile(file))
  505. assert(f() == _G)
  506. assert(os.remove(file))
  507. end
  508. -- 'loadfile' x modes
  509. do
  510. io.open(file, 'w'):write("return 10"):close()
  511. local s, m = loadfile(file, 'b')
  512. assert(not s and string.find(m, "a text chunk"))
  513. io.open(file, 'w'):write("\27 return 10"):close()
  514. local s, m = loadfile(file, 't')
  515. assert(not s and string.find(m, "a binary chunk"))
  516. assert(os.remove(file))
  517. end
  518. io.output(file)
  519. assert(io.write("qualquer coisa\n"))
  520. assert(io.write("mais qualquer coisa"))
  521. io.close()
  522. assert(io.output(assert(io.open(otherfile, 'wb')))
  523. :write("outra coisa\0\1\3\0\0\0\0\255\0")
  524. :close())
  525. local filehandle = assert(io.open(file, 'r+'))
  526. local otherfilehandle = assert(io.open(otherfile, 'rb'))
  527. assert(filehandle ~= otherfilehandle)
  528. assert(type(filehandle) == "userdata")
  529. assert(filehandle:read('l') == "qualquer coisa")
  530. io.input(otherfilehandle)
  531. assert(io.read(string.len"outra coisa") == "outra coisa")
  532. assert(filehandle:read('l') == "mais qualquer coisa")
  533. filehandle:close();
  534. assert(type(filehandle) == "userdata")
  535. io.input(otherfilehandle)
  536. assert(io.read(4) == "\0\1\3\0")
  537. assert(io.read(3) == "\0\0\0")
  538. assert(io.read(0) == "") -- 255 is not eof
  539. assert(io.read(1) == "\255")
  540. assert(io.read('a') == "\0")
  541. assert(not io.read(0))
  542. assert(otherfilehandle == io.input())
  543. otherfilehandle:close()
  544. assert(os.remove(file))
  545. assert(os.remove(otherfile))
  546. collectgarbage()
  547. io.output(file)
  548. :write[[
  549. 123.4 -56e-2 not a number
  550. second line
  551. third line
  552. and the rest of the file
  553. ]]
  554. :close()
  555. io.input(file)
  556. local _,a,b,c,d,e,h,__ = io.read(1, 'n', 'n', 'l', 'l', 'l', 'a', 10)
  557. assert(io.close(io.input()))
  558. assert(_ == ' ' and __ == nil)
  559. assert(type(a) == 'number' and a==123.4 and b==-56e-2)
  560. assert(d=='second line' and e=='third line')
  561. assert(h==[[
  562. and the rest of the file
  563. ]])
  564. assert(os.remove(file))
  565. collectgarbage()
  566. -- testing buffers
  567. do
  568. local f = assert(io.open(file, "w"))
  569. local fr = assert(io.open(file, "r"))
  570. assert(f:setvbuf("full", 2000))
  571. f:write("x")
  572. assert(fr:read("all") == "") -- full buffer; output not written yet
  573. f:close()
  574. fr:seek("set")
  575. assert(fr:read("all") == "x") -- `close' flushes it
  576. f = assert(io.open(file), "w")
  577. assert(f:setvbuf("no"))
  578. f:write("x")
  579. fr:seek("set")
  580. assert(fr:read("all") == "x") -- no buffer; output is ready
  581. f:close()
  582. f = assert(io.open(file, "a"))
  583. assert(f:setvbuf("line"))
  584. f:write("x")
  585. fr:seek("set", 1)
  586. assert(fr:read("all") == "") -- line buffer; no output without `\n'
  587. f:write("a\n"):seek("set", 1)
  588. assert(fr:read("all") == "xa\n") -- now we have a whole line
  589. f:close(); fr:close()
  590. assert(os.remove(file))
  591. end
  592. if not _soft then
  593. print("testing large files (> BUFSIZ)")
  594. io.output(file)
  595. for i=1,5001 do io.write('0123456789123') end
  596. io.write('\n12346'):close()
  597. io.input(file)
  598. local x = io.read('a')
  599. io.input():seek('set', 0)
  600. local y = io.read(30001)..io.read(1005)..io.read(0)..
  601. io.read(1)..io.read(100003)
  602. assert(x == y and string.len(x) == 5001*13 + 6)
  603. io.input():seek('set', 0)
  604. y = io.read() -- huge line
  605. assert(x == y..'\n'..io.read())
  606. assert(io.read() == nil)
  607. io.close(io.input())
  608. assert(os.remove(file))
  609. x = nil; y = nil
  610. end
  611. if not _port then
  612. local progname
  613. do -- get name of running executable
  614. local arg = arg or ARG
  615. local i = 0
  616. while arg[i] do i = i - 1 end
  617. progname = '"' .. arg[i + 1] .. '"'
  618. end
  619. print("testing popen/pclose and execute")
  620. local tests = {
  621. -- command, what, code
  622. {"ls > /dev/null", "ok"},
  623. {"not-to-be-found-command", "exit"},
  624. {"exit 3", "exit", 3},
  625. {"exit 129", "exit", 129},
  626. {"kill -s HUP $$", "signal", 1},
  627. {"kill -s KILL $$", "signal", 9},
  628. {"sh -c 'kill -s HUP $$'", "exit"},
  629. {progname .. ' -e " "', "ok"},
  630. {progname .. ' -e "os.exit(0, true)"', "ok"},
  631. {progname .. ' -e "os.exit(20, true)"', "exit", 20},
  632. }
  633. print("\n(some error messages are expected now)")
  634. for _, v in ipairs(tests) do
  635. local x, y, z = io.popen(v[1]):close()
  636. local x1, y1, z1 = os.execute(v[1])
  637. assert(x == x1 and y == y1 and z == z1)
  638. if v[2] == "ok" then
  639. assert(x and y == 'exit' and z == 0)
  640. else
  641. assert(not x and y == v[2]) -- correct status and 'what'
  642. -- correct code if known (but always different from 0)
  643. assert((v[3] == nil and z > 0) or v[3] == z)
  644. end
  645. end
  646. end
  647. -- testing tmpfile
  648. f = io.tmpfile()
  649. assert(io.type(f) == "file")
  650. f:write("alo")
  651. f:seek("set")
  652. assert(f:read"a" == "alo")
  653. end --}
  654. print'+'
  655. print("testing date/time")
  656. assert(os.date("") == "")
  657. assert(os.date("!") == "")
  658. assert(os.date("\0\0") == "\0\0")
  659. assert(os.date("!\0\0") == "\0\0")
  660. local x = string.rep("a", 10000)
  661. assert(os.date(x) == x)
  662. local t = os.time()
  663. D = os.date("*t", t)
  664. assert(os.date(string.rep("%d", 1000), t) ==
  665. string.rep(os.date("%d", t), 1000))
  666. assert(os.date(string.rep("%", 200)) == string.rep("%", 100))
  667. local t = os.time()
  668. D = os.date("*t", t)
  669. load(os.date([[assert(D.year==%Y and D.month==%m and D.day==%d and
  670. D.hour==%H and D.min==%M and D.sec==%S and
  671. D.wday==%w+1 and D.yday==%j)]], t))()
  672. checkerr("invalid conversion specifier", os.date, "%")
  673. checkerr("invalid conversion specifier", os.date, "%9")
  674. checkerr("invalid conversion specifier", os.date, "%")
  675. checkerr("invalid conversion specifier", os.date, "%O")
  676. checkerr("invalid conversion specifier", os.date, "%E")
  677. checkerr("invalid conversion specifier", os.date, "%Ea")
  678. checkerr("not an integer", os.time, {year=1000, month=1, day=1, hour='x'})
  679. checkerr("not an integer", os.time, {year=1000, month=1, day=1, hour=1.5})
  680. checkerr("missing", os.time, {hour = 12}) -- missing date
  681. if not _port then
  682. -- test Posix-specific modifiers
  683. assert(type(os.date("%Ex")) == 'string')
  684. assert(type(os.date("%Oy")) == 'string')
  685. -- test out-of-range dates (at least for Unix)
  686. if maxint >= 2^62 then -- cannot do these tests in Small Lua
  687. -- no arith overflows
  688. checkerr("out-of-bound", os.time, {year = -maxint, month = 1, day = 1})
  689. if string.packsize("i") == 4 then -- 4-byte ints
  690. if testerr("out-of-bound", os.date, "%Y", 2^40) then
  691. -- time_t has 4 bytes and therefore cannot represent year 4000
  692. print(" 4-byte time_t")
  693. checkerr("cannot be represented", os.time, {year=4000, month=1, day=1})
  694. else
  695. -- time_t has 8 bytes; an int year cannot represent a huge time
  696. print(" 8-byte time_t")
  697. checkerr("cannot be represented", os.date, "%Y", 2^60)
  698. -- it should have no problems with year 4000
  699. assert(tonumber(os.time{year=4000, month=1, day=1}))
  700. end
  701. else -- 8-byte ints
  702. -- assume time_t has 8 bytes too
  703. print(" 8-byte time_t")
  704. assert(tonumber(os.date("%Y", 2^60)))
  705. -- but still cannot represent a huge year
  706. checkerr("cannot be represented", os.time, {year=2^60, month=1, day=1})
  707. end
  708. end
  709. end
  710. D = os.date("!*t", t)
  711. load(os.date([[!assert(D.year==%Y and D.month==%m and D.day==%d and
  712. D.hour==%H and D.min==%M and D.sec==%S and
  713. D.wday==%w+1 and D.yday==%j)]], t))()
  714. do
  715. local D = os.date("*t")
  716. local t = os.time(D)
  717. if D.isdst == nil then
  718. print("no daylight saving information")
  719. else
  720. assert(type(D.isdst) == 'boolean')
  721. end
  722. D.isdst = nil
  723. local t1 = os.time(D)
  724. assert(t == t1) -- if isdst is absent uses correct default
  725. end
  726. t = os.time(D)
  727. D.year = D.year-1;
  728. local t1 = os.time(D)
  729. -- allow for leap years
  730. assert(math.abs(os.difftime(t,t1)/(24*3600) - 365) < 2)
  731. -- should not take more than 1 second to execute these two lines
  732. t = os.time()
  733. t1 = os.time(os.date("*t"))
  734. local diff = os.difftime(t1,t)
  735. assert(0 <= diff and diff <= 1)
  736. diff = os.difftime(t,t1)
  737. assert(-1 <= diff and diff <= 0)
  738. local t1 = os.time{year=2000, month=10, day=1, hour=23, min=12}
  739. local t2 = os.time{year=2000, month=10, day=1, hour=23, min=10, sec=19}
  740. assert(os.difftime(t1,t2) == 60*2-19)
  741. -- since 5.3.3, 'os.time' normalizes table fields
  742. t1 = {year = 2005, month = 1, day = 1, hour = 1, min = 0, sec = -3602}
  743. os.time(t1)
  744. assert(t1.day == 31 and t1.month == 12 and t1.year == 2004 and
  745. t1.hour == 23 and t1.min == 59 and t1.sec == 58 and
  746. t1.yday == 366)
  747. io.output(io.stdout)
  748. local t = os.date('%d %m %Y %H %M %S')
  749. local d, m, a, h, min, s = string.match(t,
  750. "(%d+) (%d+) (%d+) (%d+) (%d+) (%d+)")
  751. d = tonumber(d)
  752. m = tonumber(m)
  753. a = tonumber(a)
  754. h = tonumber(h)
  755. min = tonumber(min)
  756. s = tonumber(s)
  757. io.write(string.format('test done on %2.2d/%2.2d/%d', d, m, a))
  758. io.write(string.format(', at %2.2d:%2.2d:%2.2d\n', h, min, s))
  759. io.write(string.format('%s\n', _VERSION))