lua
[Ported from github.com/lua/lua] Cast in 'rl_readline_name'
Brought by drakestapleton
| 1 | -- $Id: testes/files.lua $ |
| 2 | -- See Copyright Notice in file lua.h |
| 3 | |
| 4 | global <const> * |
| 5 | |
| 6 | local debug = require "debug" |
| 7 | |
| 8 | local maxint = math.maxinteger |
| 9 | |
| 10 | assert(type(os.getenv"PATH") == "string") |
| 11 | |
| 12 | assert(io.input(io.stdin) == io.stdin) |
| 13 | assert(not pcall(io.input, "non-existent-file")) |
| 14 | assert(io.output(io.stdout) == io.stdout) |
| 15 | |
| 16 | |
| 17 | local function testerr (msg, f, ...) |
| 18 | local stat, err = pcall(f, ...) |
| 19 | return (not stat and string.find(err, msg, 1, true)) |
| 20 | end |
| 21 | |
| 22 | |
| 23 | local function checkerr (msg, f, ...) |
| 24 | assert(testerr(msg, f, ...)) |
| 25 | end |
| 26 | |
| 27 | |
| 28 | -- cannot close standard files |
| 29 | assert(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) |
| 34 | checkerr("got no value", io.stdin.close) |
| 35 | |
| 36 | |
| 37 | assert(type(io.input()) == "userdata" and io.type(io.output()) == "file") |
| 38 | assert(type(io.stdin) == "userdata" and io.type(io.stderr) == "file") |
| 39 | assert(not io.type(8)) |
| 40 | local a = {}; setmetatable(a, {}) |
| 41 | assert(not io.type(a)) |
| 42 | |
| 43 | assert(getmetatable(io.input()).__name == "FILE*") |
| 44 | |
| 45 | local a,b,c = io.open('xuxu_nao_existe') |
| 46 | assert(not a and type(b) == "string" and type(c) == "number") |
| 47 | |
| 48 | a,b,c = io.open('/a/b/c/d', 'w') |
| 49 | assert(not a and type(b) == "string" and type(c) == "number") |
| 50 | |
| 51 | local file = os.tmpname() |
| 52 | local f, msg = io.open(file, "w") |
| 53 | if not f then |
| 54 | (Message or print)("'os.tmpname' file cannot be open; skipping file tests") |
| 55 | |
| 56 | else --{ most tests here need tmpname |
| 57 | f:close() |
| 58 | |
| 59 | print('testing i/o') |
| 60 | |
| 61 | local otherfile = os.tmpname() |
| 62 | |
| 63 | checkerr("invalid mode", io.open, file, "rw") |
| 64 | checkerr("invalid mode", io.open, file, "rb+") |
| 65 | checkerr("invalid mode", io.open, file, "r+bk") |
| 66 | checkerr("invalid mode", io.open, file, "") |
| 67 | checkerr("invalid mode", io.open, file, "+") |
| 68 | checkerr("invalid mode", io.open, file, "b") |
| 69 | assert(io.open(file, "r+b")):close() |
| 70 | assert(io.open(file, "r+")):close() |
| 71 | assert(io.open(file, "rb")):close() |
| 72 | |
| 73 | assert(os.setlocale('C', 'all')) |
| 74 | |
| 75 | io.input(io.stdin); io.output(io.stdout); |
| 76 | |
| 77 | os.remove(file) |
| 78 | assert(not loadfile(file)) |
| 79 | -- Lua code cannot use chunks with fixed buffers |
| 80 | checkerr("invalid mode", load, "", "", "B") |
| 81 | checkerr("", dofile, file) |
| 82 | assert(not io.open(file)) |
| 83 | io.output(file) |
| 84 | assert(io.output() ~= io.stdout) |
| 85 | |
| 86 | if 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") |
| 89 | end |
| 90 | |
| 91 | assert(io.output():seek() == 0) |
| 92 | assert(io.write("alo alo"):seek() == string.len("alo alo")) |
| 93 | assert(io.output():seek("cur", -3) == string.len("alo alo")-3) |
| 94 | assert(io.write("joao")) |
| 95 | assert(io.output():seek("end") == string.len("alo joao")) |
| 96 | |
| 97 | assert(io.output():seek("set") == 0) |
| 98 | |
| 99 | assert(io.write('"alo"', "{a}\n", "second line\n", "third line \n")) |
| 100 | assert(io.write('Xfourth_line')) |
| 101 | io.output(io.stdout) |
| 102 | collectgarbage() -- file should be closed by GC |
| 103 | assert(io.input() == io.stdin and rawequal(io.output(), io.stdout)) |
| 104 | print('+') |
| 105 | |
| 106 | -- test GC for files |
| 107 | collectgarbage() |
| 108 | for 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() |
| 115 | end |
| 116 | |
| 117 | io.input():close() |
| 118 | io.close() |
| 119 | |
| 120 | assert(os.rename(file, otherfile)) |
| 121 | assert(not os.rename(file, otherfile)) |
| 122 | |
| 123 | io.output(io.open(otherfile, "ab")) |
| 124 | assert(io.write("\n\n\t\t ", 3450, "\n")); |
| 125 | io.close() |
| 126 | |
| 127 | |
| 128 | do |
| 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)") |
| 136 | end |
| 137 | assert(os.remove(file)) |
| 138 | |
| 139 | |
| 140 | do |
| 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 '*') |
| 159 | end |
| 160 | assert(os.remove(file)) |
| 161 | |
| 162 | |
| 163 | -- testing multiple arguments to io.read |
| 164 | do |
| 165 | local f <close> = assert(io.open(file, "w")) |
| 166 | f:write[[ |
| 167 | a line |
| 168 | another line |
| 169 | 1234 |
| 170 | 3.45 |
| 171 | one |
| 172 | two |
| 173 | three |
| 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) |
| 192 | end |
| 193 | assert(os.remove(file)) |
| 194 | |
| 195 | |
| 196 | |
| 197 | -- test yielding during 'dofile' |
| 198 | f = assert(io.open(file, "w")) |
| 199 | f:write[[ |
| 200 | local x, z = coroutine.yield(10) |
| 201 | local y = coroutine.yield(20) |
| 202 | return x + y * z |
| 203 | ]] |
| 204 | assert(f:close()) |
| 205 | f = coroutine.wrap(dofile) |
| 206 | assert(f(file) == 10) |
| 207 | assert(f(100, 101) == 20) |
| 208 | assert(f(200) == 100 + 200 * 101) |
| 209 | assert(os.remove(file)) |
| 210 | |
| 211 | |
| 212 | f = assert(io.open(file, "w")) |
| 213 | -- test number termination |
| 214 | f:write[[ |
| 215 | -12.3- -0xffff+ .3|5.E-3X +234e+13E 0xDEADBEEFDEADBEEFx |
| 216 | 0x1.13Ap+3e |
| 217 | ]] |
| 218 | -- very long number |
| 219 | f:write("1234"); for i = 1, 1000 do f:write("0") end; f:write("\n") |
| 220 | -- invalid sequences (must read and discard valid prefixes) |
| 221 | f:write[[ |
| 222 | .e+ 0.e; --; 0xX; |
| 223 | ]] |
| 224 | assert(f:close()) |
| 225 | f = assert(io.open(file, "r")) |
| 226 | assert(f:read("n") == -12.3); assert(f:read(1) == "-") |
| 227 | assert(f:read("n") == -0xffff); assert(f:read(2) == "+ ") |
| 228 | assert(f:read("n") == 0.3); assert(f:read(1) == "|") |
| 229 | assert(f:read("n") == 5e-3); assert(f:read(1) == "X") |
| 230 | assert(f:read("n") == 234e13); assert(f:read(1) == "E") |
| 231 | assert(f:read("n") == 0Xdeadbeefdeadbeef); assert(f:read(2) == "x\n") |
| 232 | assert(f:read("n") == 0x1.13aP3); assert(f:read(1) == "e") |
| 233 | |
| 234 | do -- 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 |
| 238 | end |
| 239 | |
| 240 | assert(not f:read("n")); assert(f:read(2) == "e+") |
| 241 | assert(not f:read("n")); assert(f:read(1) == ";") |
| 242 | assert(not f:read("n")); assert(f:read(2) == "-;") |
| 243 | assert(not f:read("n")); assert(f:read(1) == "X") |
| 244 | assert(not f:read("n")); assert(f:read(1) == ";") |
| 245 | assert(not f:read("n")); assert(not f:read(0)) -- end of file |
| 246 | assert(f:close()) |
| 247 | assert(os.remove(file)) |
| 248 | |
| 249 | |
| 250 | -- test line generators |
| 251 | assert(not pcall(io.lines, "non-existent-file")) |
| 252 | assert(os.rename(otherfile, file)) |
| 253 | io.output(otherfile) |
| 254 | local n = 0 |
| 255 | local f = io.lines(file) |
| 256 | while f() do n = n + 1 end; |
| 257 | assert(n == 6) -- number of lines in the file |
| 258 | checkerr("file is already closed", f) |
| 259 | checkerr("file is already closed", f) |
| 260 | -- copy from file to otherfile |
| 261 | n = 0 |
| 262 | for l in io.lines(file) do io.write(l, "\n"); n = n + 1 end |
| 263 | io.close() |
| 264 | assert(n == 6) |
| 265 | -- copy from otherfile back to file |
| 266 | local f = assert(io.open(otherfile)) |
| 267 | assert(io.type(f) == "file") |
| 268 | io.output(file) |
| 269 | assert(not io.output():read()) |
| 270 | n = 0 |
| 271 | for l in f:lines() do io.write(l, "\n"); n = n + 1 end |
| 272 | assert(tostring(f):sub(1, 5) == "file ") |
| 273 | assert(f:close()); io.close() |
| 274 | assert(n == 6) |
| 275 | checkerr("closed file", io.close, f) |
| 276 | assert(tostring(f) == "file (closed)") |
| 277 | assert(io.type(f) == "closed file") |
| 278 | io.input(file) |
| 279 | f = io.open(otherfile):lines() |
| 280 | n = 0 |
| 281 | for l in io.lines() do assert(l == f()); n = n + 1 end |
| 282 | f = nil; collectgarbage() |
| 283 | assert(n == 6) |
| 284 | assert(os.remove(otherfile)) |
| 285 | |
| 286 | do -- 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)) |
| 298 | end |
| 299 | |
| 300 | io.input(file) |
| 301 | do -- test error returns |
| 302 | local a,b,c = io.input():write("xuxu") |
| 303 | assert(not a and type(b) == "string" and type(c) == "number") |
| 304 | end |
| 305 | checkerr("invalid format", io.read, "x") |
| 306 | assert(io.read(0) == "") -- not eof |
| 307 | assert(io.read(5, 'l') == '"alo"') |
| 308 | assert(io.read(0) == "") |
| 309 | assert(io.read() == "second line") |
| 310 | local x = io.input():seek() |
| 311 | assert(io.read() == "third line ") |
| 312 | assert(io.input():seek("set", x)) |
| 313 | assert(io.read('L') == "third line \n") |
| 314 | assert(io.read(1) == "X") |
| 315 | assert(io.read(string.len"fourth_line") == "fourth_line") |
| 316 | assert(io.input():seek("cur", -string.len"fourth_line")) |
| 317 | assert(io.read() == "fourth_line") |
| 318 | assert(io.read() == "") -- empty line |
| 319 | assert(io.read('n') == 3450) |
| 320 | assert(io.read(1) == '\n') |
| 321 | assert(not io.read(0)) -- end of file |
| 322 | assert(not io.read(1)) -- end of file |
| 323 | assert(not io.read(30000)) -- end of file |
| 324 | assert(({io.read(1)})[2] == undef) |
| 325 | assert(not io.read()) -- end of file |
| 326 | assert(({io.read()})[2] == undef) |
| 327 | assert(not io.read('n')) -- end of file |
| 328 | assert(({io.read('n')})[2] == undef) |
| 329 | assert(io.read('a') == '') -- end of file (OK for 'a') |
| 330 | assert(io.read('a') == '') -- end of file (OK for 'a') |
| 331 | collectgarbage() |
| 332 | print('+') |
| 333 | io.close(io.input()) |
| 334 | checkerr(" input file is closed", io.read) |
| 335 | |
| 336 | assert(os.remove(file)) |
| 337 | |
| 338 | local t = '0123456789' |
| 339 | for i=1,10 do t = t..t; end |
| 340 | assert(string.len(t) == 10*2^10) |
| 341 | |
| 342 | io.output(file) |
| 343 | io.write("alo"):write("\n") |
| 344 | io.close() |
| 345 | checkerr(" output file is closed", io.write) |
| 346 | local f = io.open(file, "a+b") |
| 347 | io.output(f) |
| 348 | collectgarbage() |
| 349 | |
| 350 | assert(io.write(' ' .. t .. ' ')) |
| 351 | assert(io.write(';', 'end of file\n')) |
| 352 | assert(f:flush()); assert(io.flush()) |
| 353 | f:close() |
| 354 | print('+') |
| 355 | |
| 356 | io.input(file) |
| 357 | assert(io.read() == "alo") |
| 358 | assert(io.read(1) == ' ') |
| 359 | assert(io.read(string.len(t)) == t) |
| 360 | assert(io.read(1) == ' ') |
| 361 | assert(io.read(0)) |
| 362 | assert(io.read('a') == ';end of file\n') |
| 363 | assert(not io.read(0)) |
| 364 | assert(io.close(io.input())) |
| 365 | |
| 366 | |
| 367 | -- test errors in read/write |
| 368 | do |
| 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()) |
| 389 | end |
| 390 | |
| 391 | assert(os.remove(file)) |
| 392 | |
| 393 | -- test for L format |
| 394 | io.output(file); io.write"\n\nline\nother":close() |
| 395 | io.input(file) |
| 396 | assert(io.read"L" == "\n") |
| 397 | assert(io.read"L" == "\n") |
| 398 | assert(io.read"L" == "line\n") |
| 399 | assert(io.read"L" == "other") |
| 400 | assert(not io.read"L") |
| 401 | io.input():close() |
| 402 | |
| 403 | local f = assert(io.open(file)) |
| 404 | local s = "" |
| 405 | for l in f:lines("L") do s = s .. l end |
| 406 | assert(s == "\n\nline\nother") |
| 407 | f:close() |
| 408 | |
| 409 | io.input(file) |
| 410 | s = "" |
| 411 | for l in io.lines(nil, "L") do s = s .. l end |
| 412 | assert(s == "\n\nline\nother") |
| 413 | io.input():close() |
| 414 | |
| 415 | s = "" |
| 416 | for l in io.lines(file, "L") do s = s .. l end |
| 417 | assert(s == "\n\nline\nother") |
| 418 | |
| 419 | s = "" |
| 420 | for l in io.lines(file, "l") do s = s .. l end |
| 421 | assert(s == "lineother") |
| 422 | |
| 423 | io.output(file); io.write"a = 10 + 34\na = 2*a\na = -a\n":close() |
| 424 | local t = {} |
| 425 | assert(load(io.lines(file, "L"), nil, nil, t))() |
| 426 | assert(t.a == -((10 + 34) * 2)) |
| 427 | |
| 428 | |
| 429 | do -- 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 | |
| 463 | end |
| 464 | |
| 465 | |
| 466 | do 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()) |
| 480 | end |
| 481 | |
| 482 | |
| 483 | -- test for multiple arguments in 'lines' |
| 484 | io.output(file); io.write"0123456789\n":close() |
| 485 | for 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 |
| 489 | end |
| 490 | |
| 491 | for a,b,c in io.lines(file, 1, 2, "a") do |
| 492 | assert(a == "0" and b == "12" and c == "3456789\n") |
| 493 | end |
| 494 | |
| 495 | for 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) |
| 498 | end |
| 499 | collectgarbage() -- to close file in previous iteration |
| 500 | |
| 501 | io.output(file); io.write"00\n10\n20\n30\n40\n":close() |
| 502 | for 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 |
| 506 | end |
| 507 | |
| 508 | |
| 509 | -- test load x lines |
| 510 | io.output(file); |
| 511 | io.write[[ |
| 512 | local y |
| 513 | = X |
| 514 | X = |
| 515 | X * |
| 516 | 2 + |
| 517 | X; |
| 518 | X = |
| 519 | X |
| 520 | - y; |
| 521 | ]]:close() |
| 522 | _G.X = 1 |
| 523 | assert(not load((io.lines(file)))) |
| 524 | collectgarbage() -- to close file in previous iteration |
| 525 | load((io.lines(file, "L")))() |
| 526 | assert(_G.X == 2) |
| 527 | load((io.lines(file, 1)))() |
| 528 | assert(_G.X == 4) |
| 529 | load((io.lines(file, 3)))() |
| 530 | assert(_G.X == 8) |
| 531 | _G.X = nil |
| 532 | |
| 533 | print('+') |
| 534 | |
| 535 | local x1 = "string\n\n\\com \"\"''coisas [[estranhas]] ]]'" |
| 536 | io.output(file) |
| 537 | assert(io.write(string.format("X2 = %q\n-- comment without ending EOS", x1))) |
| 538 | io.close() |
| 539 | assert(loadfile(file))() |
| 540 | assert(x1 == _G.X2) |
| 541 | _G.X2 = nil |
| 542 | print('+') |
| 543 | assert(os.remove(file)) |
| 544 | assert(not os.remove(file)) |
| 545 | assert(not os.remove(otherfile)) |
| 546 | |
| 547 | -- testing loadfile |
| 548 | local 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) |
| 555 | end |
| 556 | |
| 557 | -- loading empty file |
| 558 | testloadfile(nil, nil) |
| 559 | |
| 560 | -- loading file with initial comment without end of line |
| 561 | testloadfile("# a non-ending comment", nil) |
| 562 | |
| 563 | |
| 564 | -- checking Unicode BOM in files |
| 565 | testloadfile("\xEF\xBB\xBF# some comment\nreturn 234", 234) |
| 566 | testloadfile("\xEF\xBB\xBFreturn 239", 239) |
| 567 | testloadfile("\xEF\xBB\xBF", nil) -- empty file with a BOM |
| 568 | |
| 569 | |
| 570 | -- checking line numbers in files with initial comments |
| 571 | testloadfile("# a comment\nreturn require'debug'.getinfo(1).currentline", 2) |
| 572 | |
| 573 | |
| 574 | -- loading binary file |
| 575 | io.output(io.open(file, "wb")) |
| 576 | assert(io.write(string.dump(function () return 10, '\0alo\255', 'hi' end))) |
| 577 | io.close() |
| 578 | a, b, c = assert(loadfile(file))() |
| 579 | assert(a == 10 and b == "\0alo\255" and c == "hi") |
| 580 | assert(os.remove(file)) |
| 581 | |
| 582 | -- bug in 5.2.1 |
| 583 | do |
| 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)) |
| 591 | end |
| 592 | |
| 593 | -- loading binary file with initial comment |
| 594 | io.output(io.open(file, "wb")) |
| 595 | assert(io.write("#this is a comment for a binary file\0\n", |
| 596 | string.dump(function () return 20, '\0\0\0' end))) |
| 597 | io.close() |
| 598 | a, b, c = assert(loadfile(file))() |
| 599 | assert(a == 20 and b == "\0\0\0" and c == nil) |
| 600 | assert(os.remove(file)) |
| 601 | |
| 602 | |
| 603 | -- 'loadfile' with 'env' |
| 604 | do |
| 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)) |
| 622 | end |
| 623 | |
| 624 | |
| 625 | -- 'loadfile' x modes |
| 626 | do |
| 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)) |
| 634 | end |
| 635 | |
| 636 | |
| 637 | io.output(file) |
| 638 | assert(io.write("qualquer coisa\n")) |
| 639 | assert(io.write("mais qualquer coisa")) |
| 640 | io.close() |
| 641 | assert(io.output(assert(io.open(otherfile, 'wb'))) |
| 642 | :write("outra coisa\0\1\3\0\0\0\0\255\0") |
| 643 | :close()) |
| 644 | |
| 645 | local filehandle = assert(io.open(file, 'r+')) |
| 646 | local otherfilehandle = assert(io.open(otherfile, 'rb')) |
| 647 | assert(filehandle ~= otherfilehandle) |
| 648 | assert(type(filehandle) == "userdata") |
| 649 | assert(filehandle:read('l') == "qualquer coisa") |
| 650 | io.input(otherfilehandle) |
| 651 | assert(io.read(string.len"outra coisa") == "outra coisa") |
| 652 | assert(filehandle:read('l') == "mais qualquer coisa") |
| 653 | filehandle:close(); |
| 654 | assert(type(filehandle) == "userdata") |
| 655 | io.input(otherfilehandle) |
| 656 | assert(io.read(4) == "\0\1\3\0") |
| 657 | assert(io.read(3) == "\0\0\0") |
| 658 | assert(io.read(0) == "") -- 255 is not eof |
| 659 | assert(io.read(1) == "\255") |
| 660 | assert(io.read('a') == "\0") |
| 661 | assert(not io.read(0)) |
| 662 | assert(otherfilehandle == io.input()) |
| 663 | otherfilehandle:close() |
| 664 | assert(os.remove(file)) |
| 665 | assert(os.remove(otherfile)) |
| 666 | collectgarbage() |
| 667 | |
| 668 | io.output(file) |
| 669 | :write[[ |
| 670 | 123.4 -56e-2 not a number |
| 671 | second line |
| 672 | third line |
| 673 | |
| 674 | and the rest of the file |
| 675 | ]] |
| 676 | :close() |
| 677 | io.input(file) |
| 678 | local _,a,b,c,d,e,h,__ = io.read(1, 'n', 'n', 'l', 'l', 'l', 'a', 10) |
| 679 | assert(io.close(io.input())) |
| 680 | assert(_ == ' ' and not __) |
| 681 | assert(type(a) == 'number' and a==123.4 and b==-56e-2) |
| 682 | assert(d=='second line' and e=='third line') |
| 683 | assert(h==[[ |
| 684 | |
| 685 | and the rest of the file |
| 686 | ]]) |
| 687 | assert(os.remove(file)) |
| 688 | collectgarbage() |
| 689 | |
| 690 | -- testing buffers |
| 691 | do |
| 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)) |
| 715 | end |
| 716 | |
| 717 | |
| 718 | if 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()) |
| 746 | end |
| 747 | |
| 748 | |
| 749 | if 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 |
| 767 | end |
| 768 | |
| 769 | if 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)") |
| 820 | end |
| 821 | |
| 822 | |
| 823 | -- testing tmpfile |
| 824 | f = io.tmpfile() |
| 825 | assert(io.type(f) == "file") |
| 826 | f:write("alo") |
| 827 | f:seek("set") |
| 828 | assert(f:read"a" == "alo") |
| 829 | |
| 830 | end --} |
| 831 | |
| 832 | print'+' |
| 833 | |
| 834 | print("testing date/time") |
| 835 | |
| 836 | assert(os.date("") == "") |
| 837 | assert(os.date("!") == "") |
| 838 | assert(os.date("\0\0") == "\0\0") |
| 839 | assert(os.date("!\0\0") == "\0\0") |
| 840 | local x = string.rep("a", 10000) |
| 841 | assert(os.date(x) == x) |
| 842 | local t = os.time() |
| 843 | global D = os.date("*t", t) |
| 844 | assert(os.date(string.rep("%d", 1000), t) == |
| 845 | string.rep(os.date("%d", t), 1000)) |
| 846 | assert(os.date(string.rep("%", 200)) == string.rep("%", 100)) |
| 847 | |
| 848 | local 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 |
| 855 | end |
| 856 | |
| 857 | |
| 858 | do -- testing mktime returning -1 |
| 859 | local t = os.date("*t", -1) |
| 860 | assert(os.time(t) == -1) |
| 861 | end |
| 862 | |
| 863 | |
| 864 | checkDateTable(os.time()) |
| 865 | if 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) |
| 872 | end |
| 873 | |
| 874 | checkerr("invalid conversion specifier", os.date, "%") |
| 875 | checkerr("invalid conversion specifier", os.date, "%9") |
| 876 | checkerr("invalid conversion specifier", os.date, "%") |
| 877 | checkerr("invalid conversion specifier", os.date, "%O") |
| 878 | checkerr("invalid conversion specifier", os.date, "%E") |
| 879 | checkerr("invalid conversion specifier", os.date, "%Ea") |
| 880 | |
| 881 | checkerr("not an integer", os.time, {year=1000, month=1, day=1, hour='x'}) |
| 882 | checkerr("not an integer", os.time, {year=1000, month=1, day=1, hour=1.5}) |
| 883 | |
| 884 | checkerr("missing", os.time, {hour = 12}) -- missing date |
| 885 | |
| 886 | |
| 887 | if 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 |
| 898 | end |
| 899 | |
| 900 | |
| 901 | if 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 |
| 956 | end |
| 957 | |
| 958 | do |
| 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 |
| 969 | end |
| 970 | |
| 971 | local D = os.date("*t") |
| 972 | t = os.time(D) |
| 973 | D.year = D.year-1; |
| 974 | local t1 = os.time(D) |
| 975 | -- allow for leap years |
| 976 | assert(math.abs(os.difftime(t,t1)/(24*3600) - 365) < 2) |
| 977 | |
| 978 | -- should not take more than 1 second to execute these two lines |
| 979 | t = os.time() |
| 980 | t1 = os.time(os.date("*t")) |
| 981 | local diff = os.difftime(t1,t) |
| 982 | assert(0 <= diff and diff <= 1) |
| 983 | diff = os.difftime(t,t1) |
| 984 | assert(-1 <= diff and diff <= 0) |
| 985 | |
| 986 | local t1 = os.time{year=2000, month=10, day=1, hour=23, min=12} |
| 987 | local t2 = os.time{year=2000, month=10, day=1, hour=23, min=10, sec=19} |
| 988 | assert(os.difftime(t1,t2) == 60*2-19) |
| 989 | |
| 990 | -- since 5.3.3, 'os.time' normalizes table fields |
| 991 | t1 = {year = 2005, month = 1, day = 1, hour = 1, min = 0, sec = -3602} |
| 992 | os.time(t1) |
| 993 | assert(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 | |
| 997 | io.output(io.stdout) |
| 998 | local t = os.date('%d %m %Y %H %M %S') |
| 999 | local d, m, a, h, min, s = string.match(t, |
| 1000 | "(%d+) (%d+) (%d+) (%d+) (%d+) (%d+)") |
| 1001 | d = tonumber(d) |
| 1002 | m = tonumber(m) |
| 1003 | a = tonumber(a) |
| 1004 | h = tonumber(h) |
| 1005 | min = tonumber(min) |
| 1006 | s = tonumber(s) |
| 1007 | io.write(string.format('test done on %2.2d/%2.2d/%d', d, m, a)) |
| 1008 | io.write(string.format(', at %2.2d:%2.2d:%2.2d\n', h, min, s)) |
| 1009 | io.write(string.format('%s\n', _VERSION)) |
| 1010 | |
| 1011 |