interplane.Get Omega
Read in your language

lua

[Ported from github.com/lua/lua] Cast in 'rl_readline_name'

Brought by drakestapleton

branch master Branches Tags History

lua / testes / files.lua

1011 lines · 27904 bytes Raw Blame History
1-- $Id: testes/files.lua $
2-- See Copyright Notice in file lua.h
3
4global <const> *
5
6local debug = require "debug"
7
8local maxint = math.maxinteger
9
10assert(type(os.getenv"PATH") == "string")
11
12assert(io.input(io.stdin) == io.stdin)
13assert(not pcall(io.input, "non-existent-file"))
14assert(io.output(io.stdout) == io.stdout)
15
16
17local function testerr (msg, f, ...)
18 local stat, err = pcall(f, ...)
19 return (not stat and string.find(err, msg, 1, true))
20end
21
22
23local function checkerr (msg, f, ...)
24 assert(testerr(msg, f, ...))
25end
26
27
28-- cannot close standard files
29assert(not io.close(io.stdin) and
30 not io.stdout:close() and
31 not io.stderr:close())
32
33-- cannot call close method without an argument (new in 5.3.5)
34checkerr("got no value", io.stdin.close)
35
36
37assert(type(io.input()) == "userdata" and io.type(io.output()) == "file")
38assert(type(io.stdin) == "userdata" and io.type(io.stderr) == "file")
39assert(not io.type(8))
40local a = {}; setmetatable(a, {})
41assert(not io.type(a))
42
43assert(getmetatable(io.input()).__name == "FILE*")
44
45local a,b,c = io.open('xuxu_nao_existe')
46assert(not a and type(b) == "string" and type(c) == "number")
47
48a,b,c = io.open('/a/b/c/d', 'w')
49assert(not a and type(b) == "string" and type(c) == "number")
50
51local file = os.tmpname()
52local f, msg = io.open(file, "w")
53if not f then
54 (Message or print)("'os.tmpname' file cannot be open; skipping file tests")
55
56else --{ most tests here need tmpname
57f:close()
58
59print('testing i/o')
60
61local otherfile = os.tmpname()
62
63checkerr("invalid mode", io.open, file, "rw")
64checkerr("invalid mode", io.open, file, "rb+")
65checkerr("invalid mode", io.open, file, "r+bk")
66checkerr("invalid mode", io.open, file, "")
67checkerr("invalid mode", io.open, file, "+")
68checkerr("invalid mode", io.open, file, "b")
69assert(io.open(file, "r+b")):close()
70assert(io.open(file, "r+")):close()
71assert(io.open(file, "rb")):close()
72
73assert(os.setlocale('C', 'all'))
74
75io.input(io.stdin); io.output(io.stdout);
76
77os.remove(file)
78assert(not loadfile(file))
79-- Lua code cannot use chunks with fixed buffers
80checkerr("invalid mode", load, "", "", "B")
81checkerr("", dofile, file)
82assert(not io.open(file))
83io.output(file)
84assert(io.output() ~= io.stdout)
85
86if not _port then -- invalid seek
87 local status, msg, code = io.stdin:seek("set", 1000)
88 assert(not status and type(msg) == "string" and type(code) == "number")
89end
90
91assert(io.output():seek() == 0)
92assert(io.write("alo alo"):seek() == string.len("alo alo"))
93assert(io.output():seek("cur", -3) == string.len("alo alo")-3)
94assert(io.write("joao"))
95assert(io.output():seek("end") == string.len("alo joao"))
96
97assert(io.output():seek("set") == 0)
98
99assert(io.write('"alo"', "{a}\n", "second line\n", "third line \n"))
100assert(io.write('Xfourth_line'))
101io.output(io.stdout)
102collectgarbage() -- file should be closed by GC
103assert(io.input() == io.stdin and rawequal(io.output(), io.stdout))
104print('+')
105
106-- test GC for files
107collectgarbage()
108for i=1,120 do
109 for i=1,5 do
110 io.input(file)
111 assert(io.open(file, 'r'))
112 io.lines(file)
113 end
114 collectgarbage()
115end
116
117io.input():close()
118io.close()
119
120assert(os.rename(file, otherfile))
121assert(not os.rename(file, otherfile))
122
123io.output(io.open(otherfile, "ab"))
124assert(io.write("\n\n\t\t ", 3450, "\n"));
125io.close()
126
127
128do
129 -- closing file by scope
130 local F = nil
131 do
132 local f <close> = assert(io.open(file, "w"))
133 F = f
134 end
135 assert(tostring(F) == "file (closed)")
136end
137assert(os.remove(file))
138
139
140do
141 -- test writing/reading numbers
142 local f <close> = assert(io.open(file, "w"))
143 f:write(maxint, '\n')
144 f:write(string.format("0X%x\n", maxint))
145 f:write("0xABCp-3", '\n')
146 f:write(0, '\n')
147 f:write(-maxint, '\n')
148 f:write(string.format("0x%X\n", -maxint))
149 f:write("-0xABCp-3", '\n')
150 assert(f:close())
151 local f <close> = assert(io.open(file, "r"))
152 assert(f:read("n") == maxint)
153 assert(f:read("n") == maxint)
154 assert(f:read("n") == 0xABCp-3)
155 assert(f:read("n") == 0)
156 assert(f:read("*n") == -maxint) -- test old format (with '*')
157 assert(f:read("n") == -maxint)
158 assert(f:read("*n") == -0xABCp-3) -- test old format (with '*')
159end
160assert(os.remove(file))
161
162
163-- testing multiple arguments to io.read
164do
165 local f <close> = assert(io.open(file, "w"))
166 f:write[[
167a line
168another line
1691234
1703.45
171one
172two
173three
174]]
175 local l1, l2, l3, l4, n1, n2, c, dummy
176 assert(f:close())
177 local f <close> = assert(io.open(file, "r"))
178 l1, l2, n1, n2, dummy = f:read("l", "L", "n", "n")
179 assert(l1 == "a line" and l2 == "another line\n" and
180 n1 == 1234 and n2 == 3.45 and dummy == nil)
181 assert(f:close())
182 local f <close> = assert(io.open(file, "r"))
183 l1, l2, n1, n2, c, l3, l4, dummy = f:read(7, "l", "n", "n", 1, "l", "l")
184 assert(l1 == "a line\n" and l2 == "another line" and c == '\n' and
185 n1 == 1234 and n2 == 3.45 and l3 == "one" and l4 == "two"
186 and dummy == nil)
187 assert(f:close())
188 local f <close> = assert(io.open(file, "r"))
189 -- second item failing
190 l1, n1, n2, dummy = f:read("l", "n", "n", "l")
191 assert(l1 == "a line" and not n1)
192end
193assert(os.remove(file))
194
195
196
197-- test yielding during 'dofile'
198f = assert(io.open(file, "w"))
199f:write[[
200local x, z = coroutine.yield(10)
201local y = coroutine.yield(20)
202return x + y * z
203]]
204assert(f:close())
205f = coroutine.wrap(dofile)
206assert(f(file) == 10)
207assert(f(100, 101) == 20)
208assert(f(200) == 100 + 200 * 101)
209assert(os.remove(file))
210
211
212f = assert(io.open(file, "w"))
213-- test number termination
214f:write[[
215-12.3- -0xffff+ .3|5.E-3X +234e+13E 0xDEADBEEFDEADBEEFx
2160x1.13Ap+3e
217]]
218-- very long number
219f:write("1234"); for i = 1, 1000 do f:write("0") end; f:write("\n")
220-- invalid sequences (must read and discard valid prefixes)
221f:write[[
222.e+ 0.e; --; 0xX;
223]]
224assert(f:close())
225f = assert(io.open(file, "r"))
226assert(f:read("n") == -12.3); assert(f:read(1) == "-")
227assert(f:read("n") == -0xffff); assert(f:read(2) == "+ ")
228assert(f:read("n") == 0.3); assert(f:read(1) == "|")
229assert(f:read("n") == 5e-3); assert(f:read(1) == "X")
230assert(f:read("n") == 234e13); assert(f:read(1) == "E")
231assert(f:read("n") == 0Xdeadbeefdeadbeef); assert(f:read(2) == "x\n")
232assert(f:read("n") == 0x1.13aP3); assert(f:read(1) == "e")
233
234do -- attempt to read too long number
235 assert(not f:read("n")) -- fails
236 local s = f:read("L") -- read rest of line
237 assert(string.find(s, "^00*\n$")) -- lots of 0's left
238end
239
240assert(not f:read("n")); assert(f:read(2) == "e+")
241assert(not f:read("n")); assert(f:read(1) == ";")
242assert(not f:read("n")); assert(f:read(2) == "-;")
243assert(not f:read("n")); assert(f:read(1) == "X")
244assert(not f:read("n")); assert(f:read(1) == ";")
245assert(not f:read("n")); assert(not f:read(0)) -- end of file
246assert(f:close())
247assert(os.remove(file))
248
249
250-- test line generators
251assert(not pcall(io.lines, "non-existent-file"))
252assert(os.rename(otherfile, file))
253io.output(otherfile)
254local n = 0
255local f = io.lines(file)
256while f() do n = n + 1 end;
257assert(n == 6) -- number of lines in the file
258checkerr("file is already closed", f)
259checkerr("file is already closed", f)
260-- copy from file to otherfile
261n = 0
262for l in io.lines(file) do io.write(l, "\n"); n = n + 1 end
263io.close()
264assert(n == 6)
265-- copy from otherfile back to file
266local f = assert(io.open(otherfile))
267assert(io.type(f) == "file")
268io.output(file)
269assert(not io.output():read())
270n = 0
271for l in f:lines() do io.write(l, "\n"); n = n + 1 end
272assert(tostring(f):sub(1, 5) == "file ")
273assert(f:close()); io.close()
274assert(n == 6)
275checkerr("closed file", io.close, f)
276assert(tostring(f) == "file (closed)")
277assert(io.type(f) == "closed file")
278io.input(file)
279f = io.open(otherfile):lines()
280n = 0
281for l in io.lines() do assert(l == f()); n = n + 1 end
282f = nil; collectgarbage()
283assert(n == 6)
284assert(os.remove(otherfile))
285
286do -- bug in 5.3.1
287 io.output(otherfile)
288 io.write(string.rep("a", 300), "\n")
289 io.close()
290 local t ={}; for i = 1, 250 do t[i] = 1 end
291 t = {io.lines(otherfile, table.unpack(t))()}
292 -- everything ok here
293 assert(#t == 250 and t[1] == 'a' and t[#t] == 'a')
294 t[#t + 1] = 1 -- one too many
295 checkerr("too many arguments", io.lines, otherfile, table.unpack(t))
296 collectgarbage() -- ensure 'otherfile' is closed
297 assert(os.remove(otherfile))
298end
299
300io.input(file)
301do -- test error returns
302 local a,b,c = io.input():write("xuxu")
303 assert(not a and type(b) == "string" and type(c) == "number")
304end
305checkerr("invalid format", io.read, "x")
306assert(io.read(0) == "") -- not eof
307assert(io.read(5, 'l') == '"alo"')
308assert(io.read(0) == "")
309assert(io.read() == "second line")
310local x = io.input():seek()
311assert(io.read() == "third line ")
312assert(io.input():seek("set", x))
313assert(io.read('L') == "third line \n")
314assert(io.read(1) == "X")
315assert(io.read(string.len"fourth_line") == "fourth_line")
316assert(io.input():seek("cur", -string.len"fourth_line"))
317assert(io.read() == "fourth_line")
318assert(io.read() == "") -- empty line
319assert(io.read('n') == 3450)
320assert(io.read(1) == '\n')
321assert(not io.read(0)) -- end of file
322assert(not io.read(1)) -- end of file
323assert(not io.read(30000)) -- end of file
324assert(({io.read(1)})[2] == undef)
325assert(not io.read()) -- end of file
326assert(({io.read()})[2] == undef)
327assert(not io.read('n')) -- end of file
328assert(({io.read('n')})[2] == undef)
329assert(io.read('a') == '') -- end of file (OK for 'a')
330assert(io.read('a') == '') -- end of file (OK for 'a')
331collectgarbage()
332print('+')
333io.close(io.input())
334checkerr(" input file is closed", io.read)
335
336assert(os.remove(file))
337
338local t = '0123456789'
339for i=1,10 do t = t..t; end
340assert(string.len(t) == 10*2^10)
341
342io.output(file)
343io.write("alo"):write("\n")
344io.close()
345checkerr(" output file is closed", io.write)
346local f = io.open(file, "a+b")
347io.output(f)
348collectgarbage()
349
350assert(io.write(' ' .. t .. ' '))
351assert(io.write(';', 'end of file\n'))
352assert(f:flush()); assert(io.flush())
353f:close()
354print('+')
355
356io.input(file)
357assert(io.read() == "alo")
358assert(io.read(1) == ' ')
359assert(io.read(string.len(t)) == t)
360assert(io.read(1) == ' ')
361assert(io.read(0))
362assert(io.read('a') == ';end of file\n')
363assert(not io.read(0))
364assert(io.close(io.input()))
365
366
367-- test errors in read/write
368do
369 local function ismsg (m)
370 -- error message is not a code number
371 return (type(m) == "string" and not tonumber(m))
372 end
373
374 -- read
375 local f = io.open(file, "w")
376 local r, m, c = f:read()
377 assert(not r and ismsg(m) and type(c) == "number")
378 assert(f:close())
379 -- write
380 f = io.open(file, "r")
381 r, m, c = f:write("whatever")
382 assert(not r and ismsg(m) and type(c) == "number")
383 assert(f:close())
384 -- lines
385 f = io.open(file, "w")
386 r, m = pcall(f:lines())
387 assert(r == false and ismsg(m))
388 assert(f:close())
389end
390
391assert(os.remove(file))
392
393-- test for L format
394io.output(file); io.write"\n\nline\nother":close()
395io.input(file)
396assert(io.read"L" == "\n")
397assert(io.read"L" == "\n")
398assert(io.read"L" == "line\n")
399assert(io.read"L" == "other")
400assert(not io.read"L")
401io.input():close()
402
403local f = assert(io.open(file))
404local s = ""
405for l in f:lines("L") do s = s .. l end
406assert(s == "\n\nline\nother")
407f:close()
408
409io.input(file)
410s = ""
411for l in io.lines(nil, "L") do s = s .. l end
412assert(s == "\n\nline\nother")
413io.input():close()
414
415s = ""
416for l in io.lines(file, "L") do s = s .. l end
417assert(s == "\n\nline\nother")
418
419s = ""
420for l in io.lines(file, "l") do s = s .. l end
421assert(s == "lineother")
422
423io.output(file); io.write"a = 10 + 34\na = 2*a\na = -a\n":close()
424local t = {}
425assert(load(io.lines(file, "L"), nil, nil, t))()
426assert(t.a == -((10 + 34) * 2))
427
428
429do -- testing closing file in line iteration
430
431 -- get the to-be-closed variable from a loop
432 local function gettoclose (lv)
433 lv = lv + 1
434 local stvar = 0 -- to-be-closed is 3th state variable in the loop
435 for i = 1, 1000 do
436 local n, v = debug.getlocal(lv, i)
437 if n == "(for state)" then
438 stvar = stvar + 1
439 if stvar == 3 then return v end
440 end
441 end
442 end
443
444 local f
445 for l in io.lines(file) do
446 f = gettoclose(1)
447 assert(io.type(f) == "file")
448 break
449 end
450 assert(io.type(f) == "closed file")
451
452 f = nil
453 local function foo (name)
454 for l in io.lines(name) do
455 f = gettoclose(1)
456 assert(io.type(f) == "file")
457 error(f) -- exit loop with an error
458 end
459 end
460 local st, msg = pcall(foo, file)
461 assert(st == false and io.type(msg) == "closed file")
462
463end
464
465
466do print("testing flush")
467 local f = io.output("/dev/null")
468 assert(f:write("abcd")) -- write to buffer
469 assert(f:flush()) -- write to device
470 assert(f:write("abcd")) -- write to buffer
471 assert(io.flush()) -- write to device
472 assert(f:close())
473
474 local f = io.output("/dev/full")
475 assert(f:write("abcd")) -- write to buffer
476 assert(not f:flush()) -- cannot write to device
477 assert(f:write("abcd")) -- write to buffer
478 assert(not io.flush()) -- cannot write to device
479 assert(f:close())
480end
481
482
483-- test for multiple arguments in 'lines'
484io.output(file); io.write"0123456789\n":close()
485for a,b in io.lines(file, 1, 1) do
486 if a == "\n" then assert(not b)
487 else assert(tonumber(a) == tonumber(b) - 1)
488 end
489end
490
491for a,b,c in io.lines(file, 1, 2, "a") do
492 assert(a == "0" and b == "12" and c == "3456789\n")
493end
494
495for a,b,c in io.lines(file, "a", 0, 1) do
496 if a == "" then break end
497 assert(a == "0123456789\n" and not b and not c)
498end
499collectgarbage() -- to close file in previous iteration
500
501io.output(file); io.write"00\n10\n20\n30\n40\n":close()
502for a, b in io.lines(file, "n", "n") do
503 if a == 40 then assert(not b)
504 else assert(a == b - 10)
505 end
506end
507
508
509-- test load x lines
510io.output(file);
511io.write[[
512local y
513= X
514X =
515X *
5162 +
517X;
518X =
519X
520- y;
521]]:close()
522_G.X = 1
523assert(not load((io.lines(file))))
524collectgarbage() -- to close file in previous iteration
525load((io.lines(file, "L")))()
526assert(_G.X == 2)
527load((io.lines(file, 1)))()
528assert(_G.X == 4)
529load((io.lines(file, 3)))()
530assert(_G.X == 8)
531_G.X = nil
532
533print('+')
534
535local x1 = "string\n\n\\com \"\"''coisas [[estranhas]] ]]'"
536io.output(file)
537assert(io.write(string.format("X2 = %q\n-- comment without ending EOS", x1)))
538io.close()
539assert(loadfile(file))()
540assert(x1 == _G.X2)
541_G.X2 = nil
542print('+')
543assert(os.remove(file))
544assert(not os.remove(file))
545assert(not os.remove(otherfile))
546
547-- testing loadfile
548local function testloadfile (s, expres)
549 io.output(file)
550 if s then io.write(s) end
551 io.close()
552 local res = assert(loadfile(file))()
553 assert(os.remove(file))
554 assert(res == expres)
555end
556
557-- loading empty file
558testloadfile(nil, nil)
559
560-- loading file with initial comment without end of line
561testloadfile("# a non-ending comment", nil)
562
563
564-- checking Unicode BOM in files
565testloadfile("\xEF\xBB\xBF# some comment\nreturn 234", 234)
566testloadfile("\xEF\xBB\xBFreturn 239", 239)
567testloadfile("\xEF\xBB\xBF", nil) -- empty file with a BOM
568
569
570-- checking line numbers in files with initial comments
571testloadfile("# a comment\nreturn require'debug'.getinfo(1).currentline", 2)
572
573
574-- loading binary file
575io.output(io.open(file, "wb"))
576assert(io.write(string.dump(function () return 10, '\0alo\255', 'hi' end)))
577io.close()
578a, b, c = assert(loadfile(file))()
579assert(a == 10 and b == "\0alo\255" and c == "hi")
580assert(os.remove(file))
581
582-- bug in 5.2.1
583do
584 io.output(io.open(file, "wb"))
585 -- save function with no upvalues
586 assert(io.write(string.dump(function () return 1 end)))
587 io.close()
588 f = assert(loadfile(file, "b", {}))
589 assert(type(f) == "function" and f() == 1)
590 assert(os.remove(file))
591end
592
593-- loading binary file with initial comment
594io.output(io.open(file, "wb"))
595assert(io.write("#this is a comment for a binary file\0\n",
596 string.dump(function () return 20, '\0\0\0' end)))
597io.close()
598a, b, c = assert(loadfile(file))()
599assert(a == 20 and b == "\0\0\0" and c == nil)
600assert(os.remove(file))
601
602
603-- 'loadfile' with 'env'
604do
605 local f = io.open(file, 'w')
606 f:write[[
607 if (...) then a = 15; return b, c, d
608 else return _ENV
609 end
610 ]]
611 f:close()
612 local t = {b = 12, c = "xuxu", d = print}
613 local f = assert(loadfile(file, 't', t))
614 local b, c, d = f(1)
615 assert(t.a == 15 and b == 12 and c == t.c and d == print)
616 assert(f() == t)
617 f = assert(loadfile(file, 't', nil))
618 assert(f() == nil)
619 f = assert(loadfile(file))
620 assert(f() == _G)
621 assert(os.remove(file))
622end
623
624
625-- 'loadfile' x modes
626do
627 io.open(file, 'w'):write("return 10"):close()
628 local s, m = loadfile(file, 'b')
629 assert(not s and string.find(m, "a text chunk"))
630 io.open(file, 'w'):write("\27 return 10"):close()
631 local s, m = loadfile(file, 't')
632 assert(not s and string.find(m, "a binary chunk"))
633 assert(os.remove(file))
634end
635
636
637io.output(file)
638assert(io.write("qualquer coisa\n"))
639assert(io.write("mais qualquer coisa"))
640io.close()
641assert(io.output(assert(io.open(otherfile, 'wb')))
642 :write("outra coisa\0\1\3\0\0\0\0\255\0")
643 :close())
644
645local filehandle = assert(io.open(file, 'r+'))
646local otherfilehandle = assert(io.open(otherfile, 'rb'))
647assert(filehandle ~= otherfilehandle)
648assert(type(filehandle) == "userdata")
649assert(filehandle:read('l') == "qualquer coisa")
650io.input(otherfilehandle)
651assert(io.read(string.len"outra coisa") == "outra coisa")
652assert(filehandle:read('l') == "mais qualquer coisa")
653filehandle:close();
654assert(type(filehandle) == "userdata")
655io.input(otherfilehandle)
656assert(io.read(4) == "\0\1\3\0")
657assert(io.read(3) == "\0\0\0")
658assert(io.read(0) == "") -- 255 is not eof
659assert(io.read(1) == "\255")
660assert(io.read('a') == "\0")
661assert(not io.read(0))
662assert(otherfilehandle == io.input())
663otherfilehandle:close()
664assert(os.remove(file))
665assert(os.remove(otherfile))
666collectgarbage()
667
668io.output(file)
669 :write[[
670 123.4 -56e-2 not a number
671second line
672third line
673
674and the rest of the file
675]]
676 :close()
677io.input(file)
678local _,a,b,c,d,e,h,__ = io.read(1, 'n', 'n', 'l', 'l', 'l', 'a', 10)
679assert(io.close(io.input()))
680assert(_ == ' ' and not __)
681assert(type(a) == 'number' and a==123.4 and b==-56e-2)
682assert(d=='second line' and e=='third line')
683assert(h==[[
684
685and the rest of the file
686]])
687assert(os.remove(file))
688collectgarbage()
689
690-- testing buffers
691do
692 local f = assert(io.open(file, "w"))
693 local fr = assert(io.open(file, "r"))
694 assert(f:setvbuf("full", 2000))
695 f:write("x")
696 assert(fr:read("all") == "") -- full buffer; output not written yet
697 f:close()
698 fr:seek("set")
699 assert(fr:read("all") == "x") -- `close' flushes it
700 f = assert(io.open(file), "w")
701 assert(f:setvbuf("no"))
702 f:write("x")
703 fr:seek("set")
704 assert(fr:read("all") == "x") -- no buffer; output is ready
705 f:close()
706 f = assert(io.open(file, "a"))
707 assert(f:setvbuf("line"))
708 f:write("x")
709 fr:seek("set", 1)
710 assert(fr:read("all") == "") -- line buffer; no output without `\n'
711 f:write("a\n"):seek("set", 1)
712 assert(fr:read("all") == "xa\n") -- now we have a whole line
713 f:close(); fr:close()
714 assert(os.remove(file))
715end
716
717
718if T and T.nonblock and not _port then
719 print("testing failed write")
720
721 -- unable to write anything to /dev/full
722 local f = io.open("/dev/full", "w")
723 assert(f:setvbuf("no"))
724 local _, _, err, count = f:write("abcd")
725 assert(err > 0 and count == 0)
726 assert(f:close())
727
728 -- receiver will read a "few" bytes (enough to empty a large buffer)
729 local receiver = [[
730 lua -e 'assert(io.stdin:setvbuf("no")); assert(#io.read(1e4) == 1e4)' ]]
731
732 local f = io.popen(receiver, "w")
733 assert(f:setvbuf("no"))
734 T.nonblock(f)
735
736 -- able to write a few bytes
737 assert(f:write(string.rep("a", 1e2)))
738
739 -- Unable to write more bytes than the pipe buffer supports.
740 -- (In Linux, the pipe buffer size is 64K (2^16). Posix requires at
741 -- least 512 bytes.)
742 local _, _, err, count = f:write("abcd", string.rep("a", 2^17))
743 assert(err > 0 and count >= 512 and count < 2^17)
744
745 assert(f:close())
746end
747
748
749if not _soft then
750 print("testing large files (> BUFSIZ)")
751 io.output(file)
752 for i=1,5001 do io.write('0123456789123') end
753 io.write('\n12346'):close()
754 io.input(file)
755 local x = io.read('a')
756 io.input():seek('set', 0)
757 local y = io.read(30001)..io.read(1005)..io.read(0)..
758 io.read(1)..io.read(100003)
759 assert(x == y and string.len(x) == 5001*13 + 6)
760 io.input():seek('set', 0)
761 y = io.read() -- huge line
762 assert(x == y..'\n'..io.read())
763 assert(not io.read())
764 io.close(io.input())
765 assert(os.remove(file))
766 x = nil; y = nil
767end
768
769if not _port then
770 local progname
771 do -- get name of running executable
772 local arg = arg or ARG
773 local i = 0
774 while arg[i] do i = i - 1 end
775 progname = '"' .. arg[i + 1] .. '"'
776 end
777 print("testing popen/pclose and execute")
778 -- invalid mode for popen
779 checkerr("invalid mode", io.popen, "cat", "")
780 checkerr("invalid mode", io.popen, "cat", "r+")
781 checkerr("invalid mode", io.popen, "cat", "rw")
782 do -- basic tests for popen
783 local file = os.tmpname()
784 local f = assert(io.popen("cat - > " .. file, "w"))
785 f:write("a line")
786 assert(f:close())
787 local f = assert(io.popen("cat - < " .. file, "r"))
788 assert(f:read("a") == "a line")
789 assert(f:close())
790 assert(os.remove(file))
791 end
792
793 local tests = {
794 -- command, what, code
795 {"ls > /dev/null", "ok"},
796 {"not-to-be-found-command", "exit"},
797 {"exit 3", "exit", 3},
798 {"exit 129", "exit", 129},
799 {"kill -s HUP $$", "signal", 1},
800 {"kill -s KILL $$", "signal", 9},
801 {"sh -c 'kill -s HUP $$'", "exit"},
802 {progname .. ' -e " "', "ok"},
803 {progname .. ' -e "os.exit(0, true)"', "ok"},
804 {progname .. ' -e "os.exit(20, true)"', "exit", 20},
805 }
806 print("\n(some error messages are expected now)")
807 for _, v in ipairs(tests) do
808 local x, y, z = io.popen(v[1]):close()
809 local x1, y1, z1 = os.execute(v[1])
810 assert(x == x1 and y == y1 and z == z1)
811 if v[2] == "ok" then
812 assert(x and y == 'exit' and z == 0)
813 else
814 assert(not x and y == v[2]) -- correct status and 'what'
815 -- correct code if known (but always different from 0)
816 assert((v[3] == nil and z > 0) or v[3] == z)
817 end
818 end
819 print("(done)")
820end
821
822
823-- testing tmpfile
824f = io.tmpfile()
825assert(io.type(f) == "file")
826f:write("alo")
827f:seek("set")
828assert(f:read"a" == "alo")
829
830end --}
831
832print'+'
833
834print("testing date/time")
835
836assert(os.date("") == "")
837assert(os.date("!") == "")
838assert(os.date("\0\0") == "\0\0")
839assert(os.date("!\0\0") == "\0\0")
840local x = string.rep("a", 10000)
841assert(os.date(x) == x)
842local t = os.time()
843global D = os.date("*t", t)
844assert(os.date(string.rep("%d", 1000), t) ==
845 string.rep(os.date("%d", t), 1000))
846assert(os.date(string.rep("%", 200)) == string.rep("%", 100))
847
848local function checkDateTable (t)
849 D = os.date("*t", t)
850 assert(os.time(D) == t)
851 load(os.date([[assert(D.year==%Y and D.month==%m and D.day==%d and
852 D.hour==%H and D.min==%M and D.sec==%S and
853 D.wday==%w+1 and D.yday==%j)]], t))()
854 _G.D = nil
855end
856
857
858do -- testing mktime returning -1
859 local t = os.date("*t", -1)
860 assert(os.time(t) == -1)
861end
862
863
864checkDateTable(os.time())
865if not _port then
866 -- assume that time_t can represent these values
867 checkDateTable(0)
868 checkDateTable(1)
869 checkDateTable(1000)
870 checkDateTable(0x7fffffff)
871 checkDateTable(0x80000000)
872end
873
874checkerr("invalid conversion specifier", os.date, "%")
875checkerr("invalid conversion specifier", os.date, "%9")
876checkerr("invalid conversion specifier", os.date, "%")
877checkerr("invalid conversion specifier", os.date, "%O")
878checkerr("invalid conversion specifier", os.date, "%E")
879checkerr("invalid conversion specifier", os.date, "%Ea")
880
881checkerr("not an integer", os.time, {year=1000, month=1, day=1, hour='x'})
882checkerr("not an integer", os.time, {year=1000, month=1, day=1, hour=1.5})
883
884checkerr("missing", os.time, {hour = 12}) -- missing date
885
886
887if string.packsize("i") == 4 then -- 4-byte ints
888 checkerr("field 'year' is out-of-bound", os.time,
889 {year = -(1 << 31) + 1899, month = 1, day = 1})
890
891 checkerr("field 'year' is out-of-bound", os.time,
892 {year = -(1 << 31), month = 1, day = 1})
893
894 if math.maxinteger > 2^31 then -- larger lua_integer?
895 checkerr("field 'year' is out-of-bound", os.time,
896 {year = (1 << 31) + 1900, month = 1, day = 1})
897 end
898end
899
900
901if not _port then
902 -- test Posix-specific modifiers
903 assert(type(os.date("%Ex")) == 'string')
904 assert(type(os.date("%Oy")) == 'string')
905
906 -- test large dates (assume at least 4-byte ints and time_t)
907 local t0 = os.time{year = 1970, month = 1, day = 0}
908 local t1 = os.time{year = 1970, month = 1, day = 0, sec = (1 << 31) - 1}
909 assert(t1 - t0 == (1 << 31) - 1)
910 t0 = os.time{year = 1970, month = 1, day = 1}
911 t1 = os.time{year = 1970, month = 1, day = 1, sec = -(1 << 31)}
912 assert(t1 - t0 == -(1 << 31))
913
914 -- test out-of-range dates (at least for Unix)
915 if maxint >= 2^62 then -- cannot do these tests in Small Lua
916 -- no arith overflows
917 checkerr("out-of-bound", os.time, {year = -maxint, month = 1, day = 1})
918 if string.packsize("i") == 4 then -- 4-byte ints
919 if testerr("out-of-bound", os.date, "%Y", 2^40) then
920 -- time_t has 4 bytes and therefore cannot represent year 4000
921 print(" 4-byte time_t")
922 checkerr("cannot be represented", os.time, {year=4000, month=1, day=1})
923 else
924 -- time_t has 8 bytes; an int year cannot represent a huge time
925 print(" 8-byte time_t")
926 checkerr("cannot be represented", os.date, "%Y", 2^60)
927
928 -- this is the maximum year
929 assert(tonumber(os.time
930 {year=(1 << 31) + 1899, month=12, day=31, hour=23, min=59, sec=59}))
931
932 -- this is too much
933 checkerr("represented", os.time,
934 {year=(1 << 31) + 1899, month=12, day=31, hour=23, min=59, sec=60})
935 end
936
937 -- internal 'int' fields cannot hold these values
938 checkerr("field 'day' is out-of-bound", os.time,
939 {year = 0, month = 1, day = 2^32})
940
941 checkerr("field 'month' is out-of-bound", os.time,
942 {year = 0, month = -((1 << 31) + 1), day = 1})
943
944 checkerr("field 'year' is out-of-bound", os.time,
945 {year = (1 << 31) + 1900, month = 1, day = 1})
946
947 else -- 8-byte ints
948 -- assume time_t has 8 bytes too
949 print(" 8-byte time_t")
950 assert(tonumber(os.date("%Y", 2^60)))
951
952 -- but still cannot represent a huge year
953 checkerr("cannot be represented", os.time, {year=2^60, month=1, day=1})
954 end
955 end
956end
957
958do
959 local D = os.date("*t")
960 local t = os.time(D)
961 if D.isdst == nil then
962 print("no daylight saving information")
963 else
964 assert(type(D.isdst) == 'boolean')
965 end
966 D.isdst = nil
967 local t1 = os.time(D)
968 assert(t == t1) -- if isdst is absent uses correct default
969end
970
971local D = os.date("*t")
972t = os.time(D)
973D.year = D.year-1;
974local t1 = os.time(D)
975-- allow for leap years
976assert(math.abs(os.difftime(t,t1)/(24*3600) - 365) < 2)
977
978-- should not take more than 1 second to execute these two lines
979t = os.time()
980t1 = os.time(os.date("*t"))
981local diff = os.difftime(t1,t)
982assert(0 <= diff and diff <= 1)
983diff = os.difftime(t,t1)
984assert(-1 <= diff and diff <= 0)
985
986local t1 = os.time{year=2000, month=10, day=1, hour=23, min=12}
987local t2 = os.time{year=2000, month=10, day=1, hour=23, min=10, sec=19}
988assert(os.difftime(t1,t2) == 60*2-19)
989
990-- since 5.3.3, 'os.time' normalizes table fields
991t1 = {year = 2005, month = 1, day = 1, hour = 1, min = 0, sec = -3602}
992os.time(t1)
993assert(t1.day == 31 and t1.month == 12 and t1.year == 2004 and
994 t1.hour == 23 and t1.min == 59 and t1.sec == 58 and
995 t1.yday == 366)
996
997io.output(io.stdout)
998local t = os.date('%d %m %Y %H %M %S')
999local d, m, a, h, min, s = string.match(t,
1000 "(%d+) (%d+) (%d+) (%d+) (%d+) (%d+)")
1001d = tonumber(d)
1002m = tonumber(m)
1003a = tonumber(a)
1004h = tonumber(h)
1005min = tonumber(min)
1006s = tonumber(s)
1007io.write(string.format('test done on %2.2d/%2.2d/%d', d, m, a))
1008io.write(string.format(', at %2.2d:%2.2d:%2.2d\n', h, min, s))
1009io.write(string.format('%s\n', _VERSION))
1010
1011