files.lua 23 KB

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