summaryrefslogtreecommitdiff
path: root/tests/test.lua
diff options
context:
space:
mode:
Diffstat (limited to '')
-rwxr-xr-xtests/test.lua789
1 files changed, 789 insertions, 0 deletions
diff --git a/tests/test.lua b/tests/test.lua
new file mode 100755
index 0000000..582f55e
--- /dev/null
+++ b/tests/test.lua
@@ -0,0 +1,789 @@
1#!/usr/bin/env lua
2
3local F, tproxy, writefile, noprint, ___
4do
5 local type, unpack = type, table.unpack or unpack
6 local assert, io = assert, io
7 function F(...)
8 local args, n = { ... }, select('#', ...)
9 for i = 1, n do
10 local t = type(args[i])
11 if t ~= "string" and t ~= "number" and t ~= "boolean" then
12 args[i] = t
13 end
14 end
15 return unpack(args, 1, n)
16 end
17 function tproxy(t)
18 return setmetatable({}, {
19 __index = t,
20 __newindex = t,
21 __len = function() return #t end,
22 }), t
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
30 local sep = ("="):rep(70)
31 function ___()
32 print(sep)
33 end
34end
35
36local V = _VERSION:gsub("^.*(%d+)%.(%d+)$", "%1%2")
37if jit then V = "jit" end
38
39local mode = "global"
40if arg[1] == "module" then
41 mode = "module"
42end
43
44
45package.path = "../?.lua;../?/init.lua"
46package.cpath = "./?-"..V..".so;./?-"..V..".dll;./?.so;./?.dll"
47if mode == "module" then
48 print("testing Lua API using `compat53.module` ...")
49 _ENV = require("compat53.module")
50 if setfenv then setfenv(1, _ENV) end
51else
52 print("testing Lua API using `compat53` ...")
53 require("compat53")
54end
55
56
57___''
58do
59 print("assert", F(pcall(assert, false)))
60 print("assert", F(pcall(assert, false, nil)))
61 print("assert", F(pcall(assert, false, "error msg")))
62 print("assert", F(pcall(assert, nil, {})))
63 print("assert", F(pcall(assert, 1, 2, 3)))
64end
65
66
67___''
68do
69 local t = setmetatable({}, { __index = { 1, false, "three" } })
70 for i,v in ipairs(t) do
71 print("ipairs", i, v)
72 end
73end
74
75
76___''
77do
78 local p, t = tproxy{ "a", "b", "c" }
79 print("table.concat", table.concat(p))
80 print("table.concat", table.concat(p, ",", 2))
81 print("table.concat", table.concat(p, ".", 1, 2))
82 print("table.concat", table.concat(t))
83 print("table.concat", table.concat(t, ",", 2))
84 print("table.concat", table.concat(t, ".", 1, 2))
85end
86
87
88___''
89do
90 local p, t = tproxy{ "a", "b", "c" }
91 table.insert(p, "d")
92 print("table.insert", next(p), t[4])
93 table.insert(p, 1, "z")
94 print("table.insert", next(p), t[1], t[2])
95 table.insert(p, 2, "y")
96 print("table.insert", next(p), t[1], t[2], p[3])
97 t = { "a", "b", "c" }
98 table.insert(t, "d")
99 print("table.insert", t[1], t[2], t[3], t[4])
100 table.insert(t, 1, "z")
101 print("table.insert", t[1], t[2], t[3], t[4], t[5])
102 table.insert(t, 2, "y")
103 print("table.insert", t[1], t[2], t[3], t[4], t[5])
104end
105
106
107___''
108do
109 local ps, s = tproxy{ "a", "b", "c", "d" }
110 local pd, d = tproxy{ "A", "B", "C", "D" }
111 table.move(ps, 1, 4, 1, pd)
112 print("table.move", next(pd), d[1], d[2], d[3], d[4])
113 pd, d = tproxy{ "A", "B", "C", "D" }
114 table.move(ps, 2, 4, 1, pd)
115 print("table.move", next(pd), d[1], d[2], d[3], d[4])
116 pd, d = tproxy{ "A", "B", "C", "D" }
117 table.move(ps, 2, 3, 4, pd)
118 print("table.move", next(pd), d[1], d[2], d[3], d[4], d[5])
119 table.move(ps, 2, 4, 1)
120 print("table.move", next(ps), s[1], s[2], s[3], s[4])
121 ps, s = tproxy{ "a", "b", "c", "d" }
122 table.move(ps, 2, 3, 4)
123 print("table.move", next(ps), s[1], s[2], s[3], s[4], s[5])
124 s = { "a", "b", "c", "d" }
125 d = { "A", "B", "C", "D" }
126 table.move(s, 1, 4, 1, d)
127 print("table.move", d[1], d[2], d[3], d[4])
128 d = { "A", "B", "C", "D" }
129 table.move(s, 2, 4, 1, d)
130 print("table.move", d[1], d[2], d[3], d[4])
131 d = { "A", "B", "C", "D" }
132 table.move(s, 2, 3, 4, d)
133 print("table.move", d[1], d[2], d[3], d[4], d[5])
134 table.move(s, 2, 4, 1)
135 print("table.move", s[1], s[2], s[3], s[4])
136 s = { "a", "b", "c", "d" }
137 table.move(s, 2, 3, 4)
138 print("table.move", s[1], s[2], s[3], s[4], s[5])
139end
140
141
142___''
143do
144 local p, t = tproxy{ "a", "b", "c", "d", "e" }
145 print("table.remove", table.remove(p))
146 print("table.remove", next(p), t[1], t[2], t[3], t[4], t[5])
147 print("table.remove", table.remove(p, 1))
148 print("table.remove", next(p), t[1], t[2], t[3], t[4])
149 print("table.remove", table.remove(p, 2))
150 print("table.remove", next(p), t[1], t[2], t[3])
151 print("table.remove", table.remove(p, 3))
152 print("table.remove", next(p), t[1], t[2], t[3])
153 p, t = tproxy{}
154 print("table.remove", table.remove(p))
155 print("table.remove", next(p), next(t))
156 t = { "a", "b", "c", "d", "e" }
157 print("table.remove", table.remove(t))
158 print("table.remove", t[1], t[2], t[3], t[4], t[5])
159 print("table.remove", table.remove(t, 1))
160 print("table.remove", t[1], t[2], t[3], t[4])
161 print("table.remove", table.remove(t, 2))
162 print("table.remove", t[1], t[2], t[3])
163 print("table.remove", table.remove(t, 3))
164 print("table.remove", t[1], t[2], t[3])
165 t = {}
166 print("table.remove", table.remove(t))
167 print("table.remove", next(t))
168end
169
170___''
171do
172 local p, t = tproxy{ 3, 1, 5, 2, 8, 5, 2, 9, 7, 4 }
173 table.sort(p)
174 print("table.sort", next(p))
175 for i,v in ipairs(t) do
176 print("table.sort", i, v)
177 end
178 table.sort(p)
179 print("table.sort", next(p))
180 for i,v in ipairs(t) do
181 print("table.sort", i, v)
182 end
183 p, t = tproxy{ 9, 8, 7, 6, 5, 4, 3, 2, 1 }
184 table.sort(p)
185 print("table.sort", next(p))
186 for i,v in ipairs(t) do
187 print("table.sort", i, v)
188 end
189 table.sort(p, function(a, b) return a > b end)
190 print("table.sort", next(p))
191 for i,v in ipairs(t) do
192 print("table.sort", i, v)
193 end
194 p, t = tproxy{ 1, 1, 1, 1, 1 }
195 print("table.sort", next(p))
196 for i,v in ipairs(t) do
197 print("table.sort", i, v)
198 end
199 t = { 3, 1, 5, 2, 8, 5, 2, 9, 7, 4 }
200 table.sort(t)
201 for i,v in ipairs(t) do
202 print("table.sort", i, v)
203 end
204 table.sort(t, function(a, b) return a > b end)
205 for i,v in ipairs(t) do
206 print("table.sort", i, v)
207 end
208end
209
210
211___''
212do
213 local p, t = tproxy{ "a", "b", "c" }
214 print("table.unpack", table.unpack(p))
215 print("table.unpack", table.unpack(p, 2))
216 print("table.unpack", table.unpack(p, 1, 2))
217 print("table.unpack", table.unpack(t))
218 print("table.unpack", table.unpack(t, 2))
219 print("table.unpack", table.unpack(t, 1, 2))
220end
221
222
223___''
224print("math.maxinteger", math.maxinteger+1 > math.maxinteger)
225print("math.mininteger", math.mininteger-1 < math.mininteger)
226
227
228___''
229print("math.tointeger", math.tointeger(0))
230print("math.tointeger", math.tointeger(math.pi))
231print("math.tointeger", math.tointeger("hello"))
232print("math.tointeger", math.tointeger(math.maxinteger+2.0))
233print("math.tointeger", math.tointeger(math.mininteger*2.0))
234
235
236___''
237print("math.type", math.type(0))
238print("math.type", math.type(math.pi))
239print("math.type", math.type("hello"))
240
241
242___''
243print("math.ult", math.ult(1, 2), math.ult(2, 1))
244print("math.ult", math.ult(-1, 2), math.ult(2, -1))
245print("math.ult", math.ult(-1, -2), math.ult(-2, -1))
246print("math.ult", pcall(math.ult, "x", 2))
247print("math.ult", pcall(math.ult, 1, 2.1))
248___''
249
250
251if utf8.len then
252 local unpack = table.unpack or unpack
253 local function utf8rt(s)
254 local t = { utf8.codepoint(s, 1, #s) }
255 local ps, cs = {}, {}
256 for p,c in utf8.codes(s) do
257 ps[#ps+1], cs[#cs+1] = p, c
258 end
259 print("utf8.codes", unpack(ps))
260 print("utf8.codes", unpack(cs))
261 print("utf8.codepoint", unpack(t))
262 print("utf8.len", utf8.len(s), #t, #s)
263 print("utf8.char", utf8.char(unpack(t)))
264 end
265 utf8rt("äöüßÄÖÜ")
266 utf8rt("abcdefg")
267 ___''
268 local s = "äöüßÄÖÜ"
269 print("utf8.offset", utf8.offset(s, 1, 1))
270 print("utf8.offset", utf8.offset(s, 2, 1))
271 print("utf8.offset", utf8.offset(s, 3, 1))
272 print("utf8.offset", pcall(utf8.offset, s, 3, 2))
273 print("utf8.offset", utf8.offset(s, 3, 3))
274 print("utf8.offset", utf8.offset(s, -1, 7))
275 print("utf8.offset", utf8.offset(s, -2, 7))
276 print("utf8.offset", utf8.offset(s, -3, 7))
277 print("utf8.offset", utf8.offset(s, -1))
278 ___''
279else
280 print("XXX: utf8 module not available")
281end
282
283
284if string.pack then
285 local format = "bBhHlLjJdc3z"
286 local s = string.pack(format, -128, 255, -32768, 65535, -2147483648, 4294967295, -32768, 65536, 1.25, "abc", "defgh")
287 print("string.unpack", string.unpack(format, s))
288 ___''
289else
290 print("XXX: string packing not available")
291end
292
293
294print("testing Lua API for Lua 5.1 ...")
295
296___''
297print("debug.getuservalue()", F(debug.getuservalue(false)))
298print("debug.setuservalue()", pcall(function()
299 debug.setuservalue(false, {})
300end))
301print("debug.setmetatable()", F(debug.setmetatable({}, {})))
302
303
304___''
305do
306 local t = setmetatable({}, {
307 __pairs = function() return pairs({ a = "a" }) end,
308 })
309 for k,v in pairs(t) do
310 print("pairs()", k, v)
311 end
312end
313
314
315___''
316do
317 local code = "print('hello world')\n"
318 local badcode = "print('blub\n"
319 print("load()", pcall(function() load(true) end))
320 print("load()", F(load(badcode)))
321 print("load()", F(load(code)))
322 print("load()", F(load(code, "[L]")))
323 print("load()", F(load(code, "[L]", "b")))
324 print("load()", F(load(code, "[L]", "t")))
325 print("load()", F(load(code, "[L]", "bt")))
326 local f = load(code, "[L]", "bt", {})
327 print("load()", pcall(f))
328 f = load(code, "[L]", "bt", { print = noprint })
329 print("load()", pcall(f))
330 local bytecode = string.dump(f)
331 print("load()", F(load(bytecode)))
332 print("load()", F(load(bytecode, "[L]")))
333 print("load()", F(load(bytecode, "[L]", "b")))
334 print("load()", F(load(bytecode, "[L]", "t")))
335 print("load()", F(load(bytecode, "[L]", "bt")))
336 f = load(bytecode, "[L]", "bt", {})
337 print("load()", pcall(f))
338 f = load(bytecode, "[L]", "bt", { print = noprint })
339 print("load()", pcall(f))
340 local function make_loader(code)
341 local mid = math.floor( #code/2 )
342 local array = { code:sub(1, mid), code:sub(mid+1) }
343 local i = 0
344 return function()
345 i = i + 1
346 return array[i]
347 end
348 end
349 print("load()", F(load(make_loader(badcode))))
350 print("load()", F(load(make_loader(code))))
351 print("load()", F(load(make_loader(code), "[L]")))
352 print("load()", F(load(make_loader(code), "[L]", "b")))
353 print("load()", F(load(make_loader(code), "[L]", "t")))
354 print("load()", F(load(make_loader(code), "[L]", "bt")))
355 f = load(make_loader(code), "[L]", "bt", {})
356 print("load()", pcall(f))
357 f = load(make_loader(code), "[L]", "bt", { print = noprint })
358 print("load()", pcall(f))
359 print("load()", F(load(make_loader(bytecode))))
360 print("load()", F(load(make_loader(bytecode), "[L]")))
361 print("load()", F(load(make_loader(bytecode), "[L]", "b")))
362 print("load()", F(load(make_loader(bytecode), "[L]", "t")))
363 print("load()", F(load(make_loader(bytecode), "[L]", "bt")))
364 f = load(make_loader(bytecode), "[L]", "bt", {})
365 print("load()", pcall(f))
366 f = load(make_loader(bytecode), "[L]", "bt", { print = noprint })
367 print("load()", pcall(f))
368 writefile("good.lua", code)
369 writefile("bad.lua", badcode)
370 writefile("good.luac", bytecode, true)
371 print("loadfile()", F(loadfile("bad.lua")))
372 print("loadfile()", F(loadfile("good.lua")))
373 print("loadfile()", F(loadfile("good.lua", "b")))
374 print("loadfile()", F(loadfile("good.lua", "t")))
375 print("loadfile()", F(loadfile("good.lua", "bt")))
376 f = loadfile("good.lua", "bt", {})
377 print("loadfile()", pcall(f))
378 f = loadfile("good.lua", "bt", { print = noprint })
379 print("loadfile()", pcall(f))
380 print("loadfile()", F(loadfile("good.luac")))
381 print("loadfile()", F(loadfile("good.luac", "b")))
382 print("loadfile()", F(loadfile("good.luac", "t")))
383 print("loadfile()", F(loadfile("good.luac", "bt")))
384 f = loadfile("good.luac", "bt", {})
385 print("loadfile()", pcall(f))
386 f = loadfile("good.luac", "bt", { print = noprint })
387 print("loadfile()", pcall(f))
388 os.remove("good.lua")
389 os.remove("bad.lua")
390 os.remove("good.luac")
391end
392
393
394___''
395do
396 local function func(throw)
397 if throw then
398 error("argh")
399 else
400 return 1, 2, 3
401 end
402 end
403 local function tb(err) return "|"..err.."|" end
404 print("xpcall()", xpcall(func, debug.traceback, false))
405 print("xpcall()", xpcall(func, debug.traceback, true))
406 print("xpcall()", xpcall(func, tb, true))
407 if mode ~= "module" then
408 local function func2(cb)
409 print("xpcall()", xpcall(cb, debug.traceback, "str"))
410 end
411 local function func3(cb)
412 print("pcall()", pcall(cb, "str"))
413 end
414 local function cb(arg)
415 coroutine.yield(2)
416 return arg
417 end
418 local c = coroutine.wrap(func2)
419 print("xpcall()", c(cb))
420 print("xpcall()", c())
421 local c = coroutine.wrap(func3)
422 print("pcall()", c(cb))
423 print("pcall()", c())
424 end
425end
426
427
428___''
429do
430 local t = setmetatable({ 1 }, { __len = function() return 5 end })
431 print("rawlen()", rawlen(t), rawlen("123"))
432end
433
434
435___''
436print("os.execute()", os.execute("exit 1"))
437io.flush()
438print("os.execute()", os.execute("echo 'hello world!'"))
439io.flush()
440print("os.execute()", os.execute("no_such_file"))
441
442
443___''
444do
445 local t = table.pack("a", nil, "b", nil)
446 print("table.(un)pack()", t.n, table.unpack(t, 1, t.n))
447end
448
449
450___''
451do
452 print("coroutine.running()", F(coroutine.wrap(function()
453 return coroutine.running()
454 end)()))
455 print("coroutine.running()", F(coroutine.running()))
456 local main_co, co1, co2 = coroutine.running()
457 -- coroutine.yield
458 if mode ~= "module" then
459 print("coroutine.yield()", pcall(function()
460 coroutine.yield(1, 2, 3)
461 end))
462 end
463 print("coroutine.yield()", coroutine.wrap(function()
464 coroutine.yield(1, 2, 3)
465 end)())
466 print("coroutine.resume()", coroutine.resume(main_co, 1, 2, 3))
467 co1 = coroutine.create(function(a, b, c)
468 print("coroutine.resume()", a, b, c)
469 return a, b, c
470 end)
471 print("coroutine.resume()", coroutine.resume(co1, 1, 2, 3))
472 co1 = coroutine.create(function()
473 print("coroutine.status()", "[co1] main is", coroutine.status(main_co))
474 print("coroutine.status()", "[co1] co2 is", coroutine.status(co2))
475 end)
476 co2 = coroutine.create(function()
477 print("coroutine.status()", "[co2] main is", coroutine.status(main_co))
478 print("coroutine.status()", "[co2] co2 is", coroutine.status(co2))
479 coroutine.yield()
480 coroutine.resume(co1)
481 end)
482 print("coroutine.status()", coroutine.status(main_co))
483 print("coroutine.status()", coroutine.status(co2))
484 coroutine.resume(co2)
485 print("coroutine.status()", F(coroutine.status(co2)))
486 coroutine.resume(co2)
487 print("coroutine.status()", F(coroutine.status(co2)))
488end
489
490
491___''
492print("math.log()", math.log(1000))
493print("math.log()", math.log(1000, 10))
494
495
496___''
497do
498 local path, prefix = "./?.lua;?/init.lua;../?.lua", "package.searchpath()"
499 print(prefix, package.searchpath("no.such.module", path))
500 print(prefix, package.searchpath("no.such.module", ""))
501 print(prefix, package.searchpath("compat53", path))
502 print(prefix, package.searchpath("no:such:module", path, ":", "|"))
503end
504
505
506___''
507if mode ~= "module" then
508 local function mod_func() return {} end
509 local function my_searcher(name)
510 if name == "my.module" then
511 print("package.searchers", "my.module found")
512 return mod_func
513 end
514 end
515 local function my_searcher2(name)
516 if name == "my.module" then
517 print("package.searchers", "my.module found 2")
518 return mod_func
519 end
520 end
521 table.insert(package.searchers, my_searcher)
522 require("my.module")
523 package.loaded["my.module"] = nil
524 local new_s = { my_searcher2 }
525 for i,f in ipairs(package.searchers) do
526 new_s[i+1] = f
527 end
528 package.searchers = new_s
529 require("my.module")
530end
531
532
533___''
534do
535 print("string.find()", string.find("abc\0abc\0abc", "[^a\0]+"))
536 print("string.find()", string.find("abc\0abc\0abc", "%w+\0", 5))
537 for x in string.gmatch("abc\0def\0ghi", "[^\0]+") do
538 print("string.gmatch()", x)
539 end
540 for x in string.gmatch("abc\0def\0ghi", "%w*\0") do
541 print("string.gmatch()", #x)
542 end
543 print("string.gsub()", string.gsub("abc\0def\0ghi", "[\0]", "X"))
544 print("string.gsub()", string.gsub("abc\0def\0ghi", "%w*\0", "X"))
545 print("string.gsub()", string.gsub("abc\0def\0ghi", "%A", "X"))
546 print("string.match()", string.match("abc\0abc\0abc", "([^\0a]+)"))
547 print("string.match()", #string.match("abc\0abc\0abc", ".*\0"))
548 print("string.rep()", string.rep("a", 0))
549 print("string.rep()", string.rep("b", 1))
550 print("string.rep()", string.rep("c", 4))
551 print("string.rep()", string.rep("a", 0, "|"))
552 print("string.rep()", string.rep("b", 1, "|"))
553 print("string.rep()", string.rep("c", 4, "|"))
554 local _tostring = tostring
555 function tostring(v)
556 if type(v) == "number" then
557 return "(".._tostring(v)..")"
558 else
559 return _tostring(v)
560 end
561 end
562 print("string.format()", string.format("%q", "\"\\\0000\0010\002\r\n0\t0\""))
563 print("string.format()", string.format("%12.3fx%%sxx%.6s", 3.1, {}))
564 print("string.format()", string.format("%-3f %%%s %%s", 3.1, true))
565 print("string.format()", string.format("% 3.2g %%d %%%s", 3.1, nil))
566 print("string.format()", string.format("%+3d %%d %%%%%10.6s", 3, io.stdout))
567 print("string.format()", pcall(function()
568 print("string.format()", string.format("%d %%s", {}))
569 end))
570 tostring = _tostring
571end
572
573
574___''
575do
576 print("io.write()", io.type(io.write("hello world\n")))
577 local f = assert(io.tmpfile())
578 print("file:write()", io.type(f:write("hello world\n")))
579 f:close()
580end
581
582
583___''
584do
585 writefile("data.txt", "123 18.8 hello world\ni'm here\n")
586 io.input("data.txt")
587 print("io.read()", io.read("*n", "*number", "*l", "*a"))
588 io.input("data.txt")
589 print("io.read()", io.read("n", "number", "l", "a"))
590 io.input(io.stdin)
591 if mode ~= "module" then
592 local f = assert(io.open("data.txt", "r"))
593 print("file:read()", f:read("*n", "*number", "*l", "*a"))
594 f:close()
595 f = assert(io.open("data.txt", "r"))
596 print("file:read()", f:read("n", "number", "l", "a"))
597 f:close()
598 end
599 os.remove("data.txt")
600end
601
602
603___''
604do
605 writefile("data.txt", "123 18.8 hello world\ni'm here\n")
606 for a,b in io.lines("test.lua", 2, "*l") do
607 print("io.lines()", a, b)
608 break
609 end
610 for l in io.lines("test.lua") do
611 print("io.lines()", l)
612 break
613 end
614 for n1,n2,rest in io.lines("data.txt", "*n", "n", "*a") do
615 print("io.lines()", n1, n2, rest)
616 end
617 for l in io.lines("data.txt") do
618 print("io.lines()", l)
619 end
620 print("io.lines()", pcall(function()
621 for l in io.lines("data.txt", "*x") do print(l) end
622 end))
623 print("io.lines()", pcall(function()
624 for l in io.lines("no_such_file.txt") do print(l) end
625 end))
626 if mode ~= "module" then
627 local f = assert(io.open("test.lua", "r"))
628 for a,b in f:lines(2, "*l") do
629 print("file:lines()", a, b)
630 break
631 end
632 f:close()
633 f = assert(io.open("data.txt", "r"))
634 for n1,n2,rest in f:lines("*n", "n", "*a") do
635 print("file:lines()", n1, n2, rest)
636 end
637 f:close()
638 f = assert(io.open("data.txt", "r"))
639 for l in f:lines() do
640 print("file:lines()", l)
641 end
642 f:close()
643 print("file:lines()", pcall(function()
644 for l in f:lines() do print(l) end
645 end))
646 print("file:lines()", pcall(function()
647 local f = assert(io.open("data.txt", "r"))
648 for l in f:lines("*l", "*x") do print(l) end
649 f:close()
650 end))
651 end
652 os.remove("data.txt")
653end
654___''
655
656
657print("testing C API ...")
658local mod = require("testmod")
659___''
660print(mod.isinteger(1))
661print(mod.isinteger(0))
662print(mod.isinteger(1234567))
663print(mod.isinteger(12.3))
664print(mod.isinteger(math.huge))
665print(mod.isinteger(math.sqrt(-1)))
666
667
668___''
669print(mod.rotate(1, 1, 2, 3, 4, 5, 6))
670print(mod.rotate(-1, 1, 2, 3, 4, 5, 6))
671print(mod.rotate(4, 1, 2, 3, 4, 5, 6))
672print(mod.rotate(-4, 1, 2, 3, 4, 5, 6))
673
674
675___''
676print(mod.strtonum("+123"))
677print(mod.strtonum(" 123 "))
678print(mod.strtonum("-1.23"))
679print(mod.strtonum(" 123 abc"))
680print(mod.strtonum("jkl"))
681
682
683___''
684local a, b, c = mod.requiref()
685print( type(a), type(b), type(c),
686 a.boolean, b.boolean, c.boolean,
687 type(requiref1), type(requiref2), type(requiref3))
688
689___''
690local proxy, backend = {}, {}
691setmetatable(proxy, { __index = backend, __newindex = backend })
692print(rawget(proxy, 1), rawget(backend, 1))
693print(mod.getseti(proxy, 1))
694print(rawget(proxy, 1), rawget(backend, 1))
695print(mod.getseti(proxy, 1))
696print(rawget(proxy, 1), rawget(backend, 1))
697
698-- tests for Lua 5.1
699___''
700print(mod.tonumber(12))
701print(mod.tonumber("12"))
702print(mod.tonumber("0"))
703print(mod.tonumber(false))
704print(mod.tonumber("error"))
705
706___''
707print(mod.tointeger(12))
708print(mod.tointeger("12"))
709print(mod.tointeger("0"))
710print( "aaa" )
711print(mod.tointeger(math.pi))
712print( "bbb" )
713print(mod.tointeger(false))
714print(mod.tointeger("error"))
715
716___''
717print(mod.len("123"))
718print(mod.len({ 1, 2, 3}))
719print(pcall(mod.len, true))
720local ud, meta = mod.newproxy()
721meta.__len = function() return 5 end
722print(mod.len(ud))
723meta.__len = function() return true end
724print(pcall(mod.len, ud))
725
726___''
727print(mod.copy(true, "string", {}, 1))
728
729___''
730print(mod.rawxetp())
731print(mod.rawxetp("I'm back"))
732
733___''
734print(F(mod.globals()), mod.globals() == _G)
735
736___''
737local t = {}
738print(F(mod.subtable(t)))
739local x, msg = mod.subtable(t)
740print(F(x, msg, x == t.xxx))
741
742___''
743print(F(mod.udata()))
744print(mod.udata("nosuchtype"))
745
746___''
747print(F(mod.uservalue()))
748
749___''
750print(mod.getupvalues())
751
752___''
753print(mod.absindex("hi", true))
754
755___''
756print(mod.arith(2, 1))
757print(mod.arith(3, 5))
758
759___''
760print(mod.compare(1, 1))
761print(mod.compare(2, 1))
762print(mod.compare(1, 2))
763
764___''
765print(mod.tolstring("string"))
766local t = setmetatable({}, {
767 __tostring = function(v) return "mytable" end
768})
769print(mod.tolstring(t))
770local t = setmetatable({}, {
771 __tostring = function(v) return nil end
772})
773print(pcall(mod.tolstring, t))
774local ud, meta = mod.newproxy()
775meta.__name = "XXX"
776print(mod.tolstring(ud):gsub(":.*$", ": yyy"))
777
778___''
779print(mod.pushstring())
780
781___''
782print(mod.buffer())
783
784___''
785print(mod.exec("exit 0"))
786print(mod.exec("exit 1"))
787print(mod.exec("exit 25"))
788___''
789