diff options
author | Philipp Janda <siffiejoe@gmx.net> | 2015-01-20 11:35:16 +0100 |
---|---|---|
committer | Philipp Janda <siffiejoe@gmx.net> | 2015-01-20 11:35:16 +0100 |
commit | 866cb79f65c844b3fcfa99d2caa4bf19930dbc6d (patch) | |
tree | 338699b9df9b2c99cec053321790e9a5960c4b3b /tests | |
parent | e52c3c4e7af665acd23e63fd3ae85f2c8ae87e49 (diff) | |
download | lua-compat-5.3-866cb79f65c844b3fcfa99d2caa4bf19930dbc6d.tar.gz lua-compat-5.3-866cb79f65c844b3fcfa99d2caa4bf19930dbc6d.tar.bz2 lua-compat-5.3-866cb79f65c844b3fcfa99d2caa4bf19930dbc6d.zip |
luajit already has yieldable (x)pcall, add tests for code from compat52
Diffstat (limited to 'tests')
-rwxr-xr-x | tests/test.lua | 346 |
1 files changed, 345 insertions, 1 deletions
diff --git a/tests/test.lua b/tests/test.lua index 423fb5b..98259fe 100755 --- a/tests/test.lua +++ b/tests/test.lua | |||
@@ -1,8 +1,9 @@ | |||
1 | #!/usr/bin/env lua | 1 | #!/usr/bin/env lua |
2 | 2 | ||
3 | local F, tproxy, ___ | 3 | local F, tproxy, writefile, noprint, ___ |
4 | do | 4 | do |
5 | local type, unpack = type, table.unpack or unpack | 5 | local type, unpack = type, table.unpack or unpack |
6 | local assert, io = assert, io | ||
6 | function F(...) | 7 | function F(...) |
7 | local args, n = { ... }, select('#', ...) | 8 | local args, n = { ... }, select('#', ...) |
8 | for i = 1, n do | 9 | for i = 1, n do |
@@ -20,6 +21,12 @@ do | |||
20 | __len = function() return #t end, | 21 | __len = function() return #t end, |
21 | }), t | 22 | }), t |
22 | end | 23 | end |
24 | function writefile(name, contents, bin) | ||
25 | local f = assert(io.open(name, bin and "wb" or "w")) | ||
26 | f:write(contents) | ||
27 | f:close() | ||
28 | end | ||
29 | function noprint() end | ||
23 | local sep = ("="):rep(70) | 30 | local sep = ("="):rep(70) |
24 | function ___() | 31 | function ___() |
25 | print(sep) | 32 | print(sep) |
@@ -27,6 +34,7 @@ do | |||
27 | end | 34 | end |
28 | 35 | ||
29 | local V = _VERSION:gsub("^.*(%d+)%.(%d+)$", "%1%2") | 36 | local V = _VERSION:gsub("^.*(%d+)%.(%d+)$", "%1%2") |
37 | if jit then V = "jit" end | ||
30 | 38 | ||
31 | print( "testing Lua API ..." ) | 39 | print( "testing Lua API ..." ) |
32 | package.path = "../?.lua;"..package.path | 40 | package.path = "../?.lua;"..package.path |
@@ -217,6 +225,342 @@ print("math.ult", pcall(math.ult, 1, 2.1)) | |||
217 | ___'' | 225 | ___'' |
218 | 226 | ||
219 | 227 | ||
228 | print("testing Lua API for Lua 5.1 ...") | ||
229 | |||
230 | ___'' | ||
231 | print("debug.getuservalue()", F(debug.getuservalue(false))) | ||
232 | print("debug.setuservalue()", pcall(function() | ||
233 | debug.setuservalue(false, {}) | ||
234 | end)) | ||
235 | print("debug.setmetatable()", F(debug.setmetatable({}, {}))) | ||
236 | |||
237 | |||
238 | ___'' | ||
239 | do | ||
240 | local t = setmetatable({}, { | ||
241 | __pairs = function() return pairs({ a = "a" }) end, | ||
242 | }) | ||
243 | for k,v in pairs(t) do | ||
244 | print("pairs()", k, v) | ||
245 | end | ||
246 | end | ||
247 | |||
248 | |||
249 | ___'' | ||
250 | do | ||
251 | local code = "print('hello world')\n" | ||
252 | local badcode = "print('blub\n" | ||
253 | print("load()", pcall(function() load(true) end)) | ||
254 | print("load()", F(load(badcode))) | ||
255 | print("load()", F(load(code))) | ||
256 | print("load()", F(load(code, "[L]"))) | ||
257 | print("load()", F(load(code, "[L]", "b"))) | ||
258 | print("load()", F(load(code, "[L]", "t"))) | ||
259 | print("load()", F(load(code, "[L]", "bt"))) | ||
260 | local f = load(code, "[L]", "bt", {}) | ||
261 | print("load()", pcall(f)) | ||
262 | f = load(code, "[L]", "bt", { print = noprint }) | ||
263 | print("load()", pcall(f)) | ||
264 | local bytecode = string.dump(f) | ||
265 | print("load()", F(load(bytecode))) | ||
266 | print("load()", F(load(bytecode, "[L]"))) | ||
267 | print("load()", F(load(bytecode, "[L]", "b"))) | ||
268 | print("load()", F(load(bytecode, "[L]", "t"))) | ||
269 | print("load()", F(load(bytecode, "[L]", "bt"))) | ||
270 | f = load(bytecode, "[L]", "bt", {}) | ||
271 | print("load()", pcall(f)) | ||
272 | f = load(bytecode, "[L]", "bt", { print = noprint }) | ||
273 | print("load()", pcall(f)) | ||
274 | local function make_loader(code) | ||
275 | local mid = math.floor( #code/2 ) | ||
276 | local array = { code:sub(1, mid), code:sub(mid+1) } | ||
277 | local i = 0 | ||
278 | return function() | ||
279 | i = i + 1 | ||
280 | return array[i] | ||
281 | end | ||
282 | end | ||
283 | print("load()", F(load(make_loader(badcode)))) | ||
284 | print("load()", F(load(make_loader(code)))) | ||
285 | print("load()", F(load(make_loader(code), "[L]"))) | ||
286 | print("load()", F(load(make_loader(code), "[L]", "b"))) | ||
287 | print("load()", F(load(make_loader(code), "[L]", "t"))) | ||
288 | print("load()", F(load(make_loader(code), "[L]", "bt"))) | ||
289 | f = load(make_loader(code), "[L]", "bt", {}) | ||
290 | print("load()", pcall(f)) | ||
291 | f = load(make_loader(code), "[L]", "bt", { print = noprint }) | ||
292 | print("load()", pcall(f)) | ||
293 | print("load()", F(load(make_loader(bytecode)))) | ||
294 | print("load()", F(load(make_loader(bytecode), "[L]"))) | ||
295 | print("load()", F(load(make_loader(bytecode), "[L]", "b"))) | ||
296 | print("load()", F(load(make_loader(bytecode), "[L]", "t"))) | ||
297 | print("load()", F(load(make_loader(bytecode), "[L]", "bt"))) | ||
298 | f = load(make_loader(bytecode), "[L]", "bt", {}) | ||
299 | print("load()", pcall(f)) | ||
300 | f = load(make_loader(bytecode), "[L]", "bt", { print = noprint }) | ||
301 | print("load()", pcall(f)) | ||
302 | writefile("good.lua", code) | ||
303 | writefile("bad.lua", badcode) | ||
304 | writefile("good.luac", bytecode, true) | ||
305 | print("loadfile()", F(loadfile("bad.lua"))) | ||
306 | print("loadfile()", F(loadfile("good.lua"))) | ||
307 | print("loadfile()", F(loadfile("good.lua", "b"))) | ||
308 | print("loadfile()", F(loadfile("good.lua", "t"))) | ||
309 | print("loadfile()", F(loadfile("good.lua", "bt"))) | ||
310 | f = loadfile("good.lua", "bt", {}) | ||
311 | print("loadfile()", pcall(f)) | ||
312 | f = loadfile("good.lua", "bt", { print = noprint }) | ||
313 | print("loadfile()", pcall(f)) | ||
314 | print("loadfile()", F(loadfile("good.luac"))) | ||
315 | print("loadfile()", F(loadfile("good.luac", "b"))) | ||
316 | print("loadfile()", F(loadfile("good.luac", "t"))) | ||
317 | print("loadfile()", F(loadfile("good.luac", "bt"))) | ||
318 | f = loadfile("good.luac", "bt", {}) | ||
319 | print("loadfile()", pcall(f)) | ||
320 | f = loadfile("good.luac", "bt", { print = noprint }) | ||
321 | print("loadfile()", pcall(f)) | ||
322 | os.remove("good.lua") | ||
323 | os.remove("bad.lua") | ||
324 | os.remove("good.luac") | ||
325 | end | ||
326 | |||
327 | |||
328 | ___'' | ||
329 | do | ||
330 | local function func(throw) | ||
331 | if throw then | ||
332 | error("argh") | ||
333 | else | ||
334 | return 1, 2, 3 | ||
335 | end | ||
336 | end | ||
337 | local function tb(err) return "|"..err.."|" end | ||
338 | print("xpcall()", xpcall(func, debug.traceback, false)) | ||
339 | print("xpcall()", xpcall(func, debug.traceback, true)) | ||
340 | print("xpcall()", xpcall(func, tb, true)) | ||
341 | local function func2(cb) | ||
342 | print("xpcall()", xpcall(cb, debug.traceback, "str")) | ||
343 | end | ||
344 | local function func3(cb) | ||
345 | print("pcall()", pcall(cb, "str")) | ||
346 | end | ||
347 | local function cb(arg) | ||
348 | coroutine.yield(2) | ||
349 | return arg | ||
350 | end | ||
351 | local c = coroutine.wrap(func2) | ||
352 | print("xpcall()", c(cb)) | ||
353 | print("xpcall()", c()) | ||
354 | local c = coroutine.wrap(func3) | ||
355 | print("pcall()", c(cb)) | ||
356 | print("pcall()", c()) | ||
357 | end | ||
358 | |||
359 | |||
360 | ___'' | ||
361 | do | ||
362 | local t = setmetatable({ 1 }, { __len = function() return 5 end }) | ||
363 | print("rawlen()", rawlen(t), rawlen("123")) | ||
364 | end | ||
365 | |||
366 | |||
367 | ___'' | ||
368 | print("os.execute()", os.execute("exit 1")) | ||
369 | io.flush() | ||
370 | print("os.execute()", os.execute("echo 'hello world!'")) | ||
371 | io.flush() | ||
372 | print("os.execute()", os.execute("no_such_file")) | ||
373 | |||
374 | |||
375 | ___'' | ||
376 | do | ||
377 | local t = table.pack("a", nil, "b", nil) | ||
378 | print("table.(un)pack()", t.n, table.unpack(t, 1, t.n)) | ||
379 | end | ||
380 | |||
381 | |||
382 | ___'' | ||
383 | do | ||
384 | print("coroutine.running()", F(coroutine.wrap(function() | ||
385 | return coroutine.running() | ||
386 | end)())) | ||
387 | print("coroutine.running()", F(coroutine.running())) | ||
388 | local main_co, co1, co2 = coroutine.running() | ||
389 | -- coroutine.yield | ||
390 | print("coroutine.yield()", pcall(function() | ||
391 | coroutine.yield(1, 2, 3) | ||
392 | end)) | ||
393 | print("coroutine.yield()", coroutine.wrap(function() | ||
394 | coroutine.yield(1, 2, 3) | ||
395 | end)()) | ||
396 | print("coroutine.resume()", coroutine.resume(main_co, 1, 2, 3)) | ||
397 | co1 = coroutine.create(function(a, b, c) | ||
398 | print("coroutine.resume()", a, b, c) | ||
399 | return a, b, c | ||
400 | end) | ||
401 | print("coroutine.resume()", coroutine.resume(co1, 1, 2, 3)) | ||
402 | co1 = coroutine.create(function() | ||
403 | print("coroutine.status()", "[co1] main is", coroutine.status(main_co)) | ||
404 | print("coroutine.status()", "[co1] co2 is", coroutine.status(co2)) | ||
405 | end) | ||
406 | co2 = coroutine.create(function() | ||
407 | print("coroutine.status()", "[co2] main is", coroutine.status(main_co)) | ||
408 | print("coroutine.status()", "[co2] co2 is", coroutine.status(co2)) | ||
409 | coroutine.yield() | ||
410 | coroutine.resume(co1) | ||
411 | end) | ||
412 | print("coroutine.status()", coroutine.status(main_co)) | ||
413 | print("coroutine.status()", coroutine.status(co2)) | ||
414 | coroutine.resume(co2) | ||
415 | print("coroutine.status()", F(coroutine.status(co2))) | ||
416 | coroutine.resume(co2) | ||
417 | print("coroutine.status()", F(coroutine.status(co2))) | ||
418 | end | ||
419 | |||
420 | |||
421 | ___'' | ||
422 | print("math.log()", math.log(1000)) | ||
423 | print("math.log()", math.log(1000, 10)) | ||
424 | |||
425 | |||
426 | ___'' | ||
427 | do | ||
428 | local path, prefix = "./?.lua;?/init.lua;../?.lua", "package.searchpath()" | ||
429 | print(prefix, package.searchpath("no.such.module", path)) | ||
430 | print(prefix, package.searchpath("no.such.module", "")) | ||
431 | print(prefix, package.searchpath("compat52", path)) | ||
432 | print(prefix, package.searchpath("no:such:module", path, ":", "|")) | ||
433 | end | ||
434 | |||
435 | |||
436 | ___'' | ||
437 | do | ||
438 | local function mod_func() return {} end | ||
439 | local function my_searcher(name) | ||
440 | if name == "my.module" then | ||
441 | print("package.searchers", "my.module found") | ||
442 | return mod_func | ||
443 | end | ||
444 | end | ||
445 | local function my_searcher2(name) | ||
446 | if name == "my.module" then | ||
447 | print("package.searchers", "my.module found 2") | ||
448 | return mod_func | ||
449 | end | ||
450 | end | ||
451 | table.insert(package.searchers, my_searcher) | ||
452 | require("my.module") | ||
453 | package.loaded["my.module"] = nil | ||
454 | local new_s = { my_searcher2 } | ||
455 | for i,f in ipairs(package.searchers) do | ||
456 | new_s[i+1] = f | ||
457 | end | ||
458 | package.searchers = new_s | ||
459 | require("my.module") | ||
460 | end | ||
461 | |||
462 | |||
463 | ___'' | ||
464 | do | ||
465 | print("string.find()", ("abc\0abc\0abc"):find("[^a\0]+")) | ||
466 | print("string.find()", ("abc\0abc\0abc"):find("%w+\0", 5)) | ||
467 | for x in ("abc\0def\0ghi"):gmatch("[^\0]+") do | ||
468 | print("string.gmatch()", x) | ||
469 | end | ||
470 | for x in ("abc\0def\0ghi"):gmatch("%w*\0") do | ||
471 | print("string.gmatch()", #x) | ||
472 | end | ||
473 | print("string.gsub()", ("abc\0def\0ghi"):gsub("[\0]", "X")) | ||
474 | print("string.gsub()", ("abc\0def\0ghi"):gsub("%w*\0", "X")) | ||
475 | print("string.gsub()", ("abc\0def\0ghi"):gsub("%A", "X")) | ||
476 | print("string.match()", ("abc\0abc\0abc"):match("([^\0a]+)")) | ||
477 | print("string.match()", #("abc\0abc\0abc"):match(".*\0")) | ||
478 | print("string.rep()", string.rep("a", 0)) | ||
479 | print("string.rep()", string.rep("b", 1)) | ||
480 | print("string.rep()", string.rep("c", 4)) | ||
481 | print("string.rep()", string.rep("a", 0, "|")) | ||
482 | print("string.rep()", string.rep("b", 1, "|")) | ||
483 | print("string.rep()", string.rep("c", 4, "|")) | ||
484 | local _tostring = tostring | ||
485 | function tostring(v) | ||
486 | if type(v) == "number" then | ||
487 | return "(".._tostring(v)..")" | ||
488 | else | ||
489 | return _tostring(v) | ||
490 | end | ||
491 | end | ||
492 | print("string.format()", string.format("%q", "\"\\\0000\0010\r0\n0\t0\"")) | ||
493 | print("string.format()", string.format("%12.3fx%%sxx%.6s", 3.1, {})) | ||
494 | print("string.format()", string.format("%-3f %%%s %%s", 3.1, true)) | ||
495 | print("string.format()", string.format("% 3.2g %%d %%%s", 3.1, nil)) | ||
496 | print("string.format()", string.format("%+3d %%d %%%%%10.6s", 3, io.stdout)) | ||
497 | print("string.format()", pcall(function() | ||
498 | print("string.format()", string.format("%d %%s", {})) | ||
499 | end)) | ||
500 | tostring = _tostring | ||
501 | end | ||
502 | |||
503 | |||
504 | ___'' | ||
505 | do | ||
506 | print("io.write()", io.type(io.write("hello world\n"))) | ||
507 | local f = assert(io.tmpfile()) | ||
508 | print("file:write()", io.type(f:write("hello world\n"))) | ||
509 | f:close() | ||
510 | end | ||
511 | |||
512 | |||
513 | ___'' | ||
514 | do | ||
515 | writefile("data.txt", "123 18.8 hello world\ni'm here\n") | ||
516 | for a,b in io.lines("test.lua", 2, "*l") do | ||
517 | print("io.lines()", a, b) | ||
518 | break | ||
519 | end | ||
520 | for l in io.lines("test.lua") do | ||
521 | print("io.lines()", l) | ||
522 | break | ||
523 | end | ||
524 | for n1,n2,rest in io.lines("data.txt", "*n", "*n", "*a") do | ||
525 | print("io.lines()", n1, n2, rest) | ||
526 | end | ||
527 | for l in io.lines("data.txt") do | ||
528 | print("io.lines()", l) | ||
529 | end | ||
530 | print("io.lines()", pcall(function() | ||
531 | for l in io.lines("data.txt", "*x") do print(l) end | ||
532 | end)) | ||
533 | print("io.lines()", pcall(function() | ||
534 | for l in io.lines("no_such_file.txt") do print(l) end | ||
535 | end)) | ||
536 | local f = assert(io.open("test.lua", "r")) | ||
537 | for a,b in f:lines(2, "*l") do | ||
538 | print("file:lines()", a, b) | ||
539 | break | ||
540 | end | ||
541 | f:close() | ||
542 | f = assert(io.open("data.txt", "r")) | ||
543 | for n1,n2,rest in f:lines("*n", "*n", "*a") do | ||
544 | print("file:lines()", n1, n2, rest) | ||
545 | end | ||
546 | f:close() | ||
547 | f = assert(io.open("data.txt", "r")) | ||
548 | for l in f:lines() do | ||
549 | print("file:lines()", l) | ||
550 | end | ||
551 | f:close() | ||
552 | print("file:lines()", pcall(function() | ||
553 | for l in f:lines() do print(l) end | ||
554 | end)) | ||
555 | print("file:lines()", pcall(function() | ||
556 | local f = assert(io.open("data.txt", "r")) | ||
557 | for l in f:lines("*l", "*x") do print(l) end | ||
558 | f:close() | ||
559 | end)) | ||
560 | os.remove("data.txt") | ||
561 | end | ||
562 | ___'' | ||
563 | |||
220 | 564 | ||
221 | print("testing C API ...") | 565 | print("testing C API ...") |
222 | local mod = require("testmod") | 566 | local mod = require("testmod") |