1 |
commit: 45120babd075f9c8d9b4aae04b99f54b9e470f0d |
2 |
Author: David Seifert <soap <AT> gentoo <DOT> org> |
3 |
AuthorDate: Mon Sep 5 14:45:44 2022 +0000 |
4 |
Commit: David Seifert <soap <AT> gentoo <DOT> org> |
5 |
CommitDate: Mon Sep 5 14:45:44 2022 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/lua-patches.git/commit/?id=45120bab |
7 |
|
8 |
Add upstream tests |
9 |
|
10 |
* Only add tests/*.lua for now |
11 |
|
12 |
Signed-off-by: David Seifert <soap <AT> gentoo.org> |
13 |
|
14 |
tests/all.lua | 291 +++++++++++++ |
15 |
tests/api.lua | 1172 ++++++++++++++++++++++++++++++++++++++++++++++++++ |
16 |
tests/attrib.lua | 470 ++++++++++++++++++++ |
17 |
tests/big.lua | 82 ++++ |
18 |
tests/bitwise.lua | 328 ++++++++++++++ |
19 |
tests/calls.lua | 401 +++++++++++++++++ |
20 |
tests/closure.lua | 247 +++++++++++ |
21 |
tests/code.lua | 239 ++++++++++ |
22 |
tests/constructs.lua | 313 ++++++++++++++ |
23 |
tests/coroutine.lua | 874 +++++++++++++++++++++++++++++++++++++ |
24 |
tests/db.lua | 857 ++++++++++++++++++++++++++++++++++++ |
25 |
tests/errors.lua | 537 +++++++++++++++++++++++ |
26 |
tests/events.lua | 456 ++++++++++++++++++++ |
27 |
tests/files.lua | 793 ++++++++++++++++++++++++++++++++++ |
28 |
tests/gc.lua | 624 +++++++++++++++++++++++++++ |
29 |
tests/goto.lua | 232 ++++++++++ |
30 |
tests/literals.lua | 302 +++++++++++++ |
31 |
tests/locals.lua | 162 +++++++ |
32 |
tests/main.lua | 381 ++++++++++++++++ |
33 |
tests/math.lua | 824 +++++++++++++++++++++++++++++++++++ |
34 |
tests/nextvar.lua | 631 +++++++++++++++++++++++++++ |
35 |
tests/pm.lua | 374 ++++++++++++++++ |
36 |
tests/sort.lua | 310 +++++++++++++ |
37 |
tests/strings.lua | 379 ++++++++++++++++ |
38 |
tests/tpack.lua | 322 ++++++++++++++ |
39 |
tests/utf8.lua | 210 +++++++++ |
40 |
tests/vararg.lua | 142 ++++++ |
41 |
tests/verybig.lua | 152 +++++++ |
42 |
28 files changed, 12105 insertions(+) |
43 |
|
44 |
diff --git a/tests/all.lua b/tests/all.lua |
45 |
new file mode 100755 |
46 |
index 0000000..017533c |
47 |
--- /dev/null |
48 |
+++ b/tests/all.lua |
49 |
@@ -0,0 +1,291 @@ |
50 |
+#!../lua |
51 |
+-- $Id: all.lua,v 1.95 2016/11/07 13:11:28 roberto Exp $ |
52 |
+-- See Copyright Notice at the end of this file |
53 |
+ |
54 |
+ |
55 |
+local version = "Lua 5.3" |
56 |
+if _VERSION ~= version then |
57 |
+ io.stderr:write("\nThis test suite is for ", version, ", not for ", _VERSION, |
58 |
+ "\nExiting tests\n") |
59 |
+ return |
60 |
+end |
61 |
+ |
62 |
+ |
63 |
+_G._ARG = arg -- save arg for other tests |
64 |
+ |
65 |
+ |
66 |
+-- next variables control the execution of some tests |
67 |
+-- true means no test (so an undefined variable does not skip a test) |
68 |
+-- defaults are for Linux; test everything. |
69 |
+-- Make true to avoid long or memory consuming tests |
70 |
+_soft = rawget(_G, "_soft") or false |
71 |
+-- Make true to avoid non-portable tests |
72 |
+_port = rawget(_G, "_port") or false |
73 |
+-- Make true to avoid messages about tests not performed |
74 |
+_nomsg = rawget(_G, "_nomsg") or false |
75 |
+ |
76 |
+ |
77 |
+local usertests = rawget(_G, "_U") |
78 |
+ |
79 |
+if usertests then |
80 |
+ -- tests for sissies ;) Avoid problems |
81 |
+ _soft = true |
82 |
+ _port = true |
83 |
+ _nomsg = true |
84 |
+end |
85 |
+ |
86 |
+-- tests should require debug when needed |
87 |
+debug = nil |
88 |
+ |
89 |
+if usertests then |
90 |
+ T = nil -- no "internal" tests for user tests |
91 |
+else |
92 |
+ T = rawget(_G, "T") -- avoid problems with 'strict' module |
93 |
+end |
94 |
+ |
95 |
+math.randomseed(0) |
96 |
+ |
97 |
+--[=[ |
98 |
+ example of a long [comment], |
99 |
+ [[spanning several [lines]]] |
100 |
+ |
101 |
+]=] |
102 |
+ |
103 |
+print("current path:\n****" .. package.path .. "****\n") |
104 |
+ |
105 |
+ |
106 |
+local initclock = os.clock() |
107 |
+local lastclock = initclock |
108 |
+local walltime = os.time() |
109 |
+ |
110 |
+local collectgarbage = collectgarbage |
111 |
+ |
112 |
+do -- ( |
113 |
+ |
114 |
+-- track messages for tests not performed |
115 |
+local msgs = {} |
116 |
+function Message (m) |
117 |
+ if not _nomsg then |
118 |
+ print(m) |
119 |
+ msgs[#msgs+1] = string.sub(m, 3, -3) |
120 |
+ end |
121 |
+end |
122 |
+ |
123 |
+assert(os.setlocale"C") |
124 |
+ |
125 |
+local T,print,format,write,assert,type,unpack,floor = |
126 |
+ T,print,string.format,io.write,assert,type,table.unpack,math.floor |
127 |
+ |
128 |
+-- use K for 1000 and M for 1000000 (not 2^10 -- 2^20) |
129 |
+local function F (m) |
130 |
+ local function round (m) |
131 |
+ m = m + 0.04999 |
132 |
+ return format("%.1f", m) -- keep one decimal digit |
133 |
+ end |
134 |
+ if m < 1000 then return m |
135 |
+ else |
136 |
+ m = m / 1000 |
137 |
+ if m < 1000 then return round(m).."K" |
138 |
+ else |
139 |
+ return round(m/1000).."M" |
140 |
+ end |
141 |
+ end |
142 |
+end |
143 |
+ |
144 |
+local showmem |
145 |
+if not T then |
146 |
+ local max = 0 |
147 |
+ showmem = function () |
148 |
+ local m = collectgarbage("count") * 1024 |
149 |
+ max = (m > max) and m or max |
150 |
+ print(format(" ---- total memory: %s, max memory: %s ----\n", |
151 |
+ F(m), F(max))) |
152 |
+ end |
153 |
+else |
154 |
+ showmem = function () |
155 |
+ T.checkmemory() |
156 |
+ local total, numblocks, maxmem = T.totalmem() |
157 |
+ local count = collectgarbage("count") |
158 |
+ print(format( |
159 |
+ "\n ---- total memory: %s (%.0fK), max use: %s, blocks: %d\n", |
160 |
+ F(total), count, F(maxmem), numblocks)) |
161 |
+ print(format("\t(strings: %d, tables: %d, functions: %d, ".. |
162 |
+ "\n\tudata: %d, threads: %d)", |
163 |
+ T.totalmem"string", T.totalmem"table", T.totalmem"function", |
164 |
+ T.totalmem"userdata", T.totalmem"thread")) |
165 |
+ end |
166 |
+end |
167 |
+ |
168 |
+ |
169 |
+-- |
170 |
+-- redefine dofile to run files through dump/undump |
171 |
+-- |
172 |
+local function report (n) print("\n***** FILE '"..n.."'*****") end |
173 |
+local olddofile = dofile |
174 |
+local dofile = function (n, strip) |
175 |
+ showmem() |
176 |
+ local c = os.clock() |
177 |
+ print(string.format("time: %g (+%g)", c - initclock, c - lastclock)) |
178 |
+ lastclock = c |
179 |
+ report(n) |
180 |
+ local f = assert(loadfile(n)) |
181 |
+ local b = string.dump(f, strip) |
182 |
+ f = assert(load(b)) |
183 |
+ return f() |
184 |
+end |
185 |
+ |
186 |
+dofile('main.lua') |
187 |
+ |
188 |
+do |
189 |
+ local next, setmetatable, stderr = next, setmetatable, io.stderr |
190 |
+ -- track collections |
191 |
+ local mt = {} |
192 |
+ -- each time a table is collected, remark it for finalization |
193 |
+ -- on next cycle |
194 |
+ mt.__gc = function (o) |
195 |
+ stderr:write'.' -- mark progress |
196 |
+ local n = setmetatable(o, mt) -- remark it |
197 |
+ end |
198 |
+ local n = setmetatable({}, mt) -- create object |
199 |
+end |
200 |
+ |
201 |
+report"gc.lua" |
202 |
+local f = assert(loadfile('gc.lua')) |
203 |
+f() |
204 |
+ |
205 |
+dofile('db.lua') |
206 |
+assert(dofile('calls.lua') == deep and deep) |
207 |
+olddofile('strings.lua') |
208 |
+olddofile('literals.lua') |
209 |
+dofile('tpack.lua') |
210 |
+assert(dofile('attrib.lua') == 27) |
211 |
+ |
212 |
+assert(dofile('locals.lua') == 5) |
213 |
+dofile('constructs.lua') |
214 |
+dofile('code.lua', true) |
215 |
+if not _G._soft then |
216 |
+ report('big.lua') |
217 |
+ local f = coroutine.wrap(assert(loadfile('big.lua'))) |
218 |
+ assert(f() == 'b') |
219 |
+ assert(f() == 'a') |
220 |
+end |
221 |
+dofile('nextvar.lua') |
222 |
+dofile('pm.lua') |
223 |
+dofile('utf8.lua') |
224 |
+dofile('api.lua') |
225 |
+assert(dofile('events.lua') == 12) |
226 |
+dofile('vararg.lua') |
227 |
+dofile('closure.lua') |
228 |
+dofile('coroutine.lua') |
229 |
+dofile('goto.lua', true) |
230 |
+dofile('errors.lua') |
231 |
+dofile('math.lua') |
232 |
+dofile('sort.lua', true) |
233 |
+dofile('bitwise.lua') |
234 |
+assert(dofile('verybig.lua', true) == 10); collectgarbage() |
235 |
+dofile('files.lua') |
236 |
+ |
237 |
+if #msgs > 0 then |
238 |
+ print("\ntests not performed:") |
239 |
+ for i=1,#msgs do |
240 |
+ print(msgs[i]) |
241 |
+ end |
242 |
+ print() |
243 |
+end |
244 |
+ |
245 |
+-- no test module should define 'debug' |
246 |
+assert(debug == nil) |
247 |
+ |
248 |
+local debug = require "debug" |
249 |
+ |
250 |
+print(string.format("%d-bit integers, %d-bit floats", |
251 |
+ string.packsize("j") * 8, string.packsize("n") * 8)) |
252 |
+ |
253 |
+debug.sethook(function (a) assert(type(a) == 'string') end, "cr") |
254 |
+ |
255 |
+-- to survive outside block |
256 |
+_G.showmem = showmem |
257 |
+ |
258 |
+end --) |
259 |
+ |
260 |
+local _G, showmem, print, format, clock, time, difftime, assert, open = |
261 |
+ _G, showmem, print, string.format, os.clock, os.time, os.difftime, |
262 |
+ assert, io.open |
263 |
+ |
264 |
+-- file with time of last performed test |
265 |
+local fname = T and "time-debug.txt" or "time.txt" |
266 |
+local lasttime |
267 |
+ |
268 |
+if not usertests then |
269 |
+ -- open file with time of last performed test |
270 |
+ local f = io.open(fname) |
271 |
+ if f then |
272 |
+ lasttime = assert(tonumber(f:read'a')) |
273 |
+ f:close(); |
274 |
+ else -- no such file; assume it is recording time for first time |
275 |
+ lasttime = nil |
276 |
+ end |
277 |
+end |
278 |
+ |
279 |
+-- erase (almost) all globals |
280 |
+print('cleaning all!!!!') |
281 |
+for n in pairs(_G) do |
282 |
+ if not ({___Glob = 1, tostring = 1})[n] then |
283 |
+ _G[n] = nil |
284 |
+ end |
285 |
+end |
286 |
+ |
287 |
+ |
288 |
+collectgarbage() |
289 |
+collectgarbage() |
290 |
+collectgarbage() |
291 |
+collectgarbage() |
292 |
+collectgarbage() |
293 |
+collectgarbage();showmem() |
294 |
+ |
295 |
+local clocktime = clock() - initclock |
296 |
+walltime = difftime(time(), walltime) |
297 |
+ |
298 |
+print(format("\n\ntotal time: %.2fs (wall time: %gs)\n", clocktime, walltime)) |
299 |
+ |
300 |
+if not usertests then |
301 |
+ lasttime = lasttime or clocktime -- if no last time, ignore difference |
302 |
+ -- check whether current test time differs more than 5% from last time |
303 |
+ local diff = (clocktime - lasttime) / lasttime |
304 |
+ local tolerance = 0.05 -- 5% |
305 |
+ if (diff >= tolerance or diff <= -tolerance) then |
306 |
+ print(format("WARNING: time difference from previous test: %+.1f%%", |
307 |
+ diff * 100)) |
308 |
+ end |
309 |
+ assert(open(fname, "w")):write(clocktime):close() |
310 |
+end |
311 |
+ |
312 |
+print("final OK !!!") |
313 |
+ |
314 |
+ |
315 |
+ |
316 |
+--[[ |
317 |
+***************************************************************************** |
318 |
+* Copyright (C) 1994-2016 Lua.org, PUC-Rio. |
319 |
+* |
320 |
+* Permission is hereby granted, free of charge, to any person obtaining |
321 |
+* a copy of this software and associated documentation files (the |
322 |
+* "Software"), to deal in the Software without restriction, including |
323 |
+* without limitation the rights to use, copy, modify, merge, publish, |
324 |
+* distribute, sublicense, and/or sell copies of the Software, and to |
325 |
+* permit persons to whom the Software is furnished to do so, subject to |
326 |
+* the following conditions: |
327 |
+* |
328 |
+* The above copyright notice and this permission notice shall be |
329 |
+* included in all copies or substantial portions of the Software. |
330 |
+* |
331 |
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
332 |
+* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
333 |
+* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
334 |
+* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
335 |
+* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
336 |
+* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
337 |
+* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
338 |
+***************************************************************************** |
339 |
+]] |
340 |
+ |
341 |
|
342 |
diff --git a/tests/api.lua b/tests/api.lua |
343 |
new file mode 100644 |
344 |
index 0000000..c27ee97 |
345 |
--- /dev/null |
346 |
+++ b/tests/api.lua |
347 |
@@ -0,0 +1,1172 @@ |
348 |
+-- $Id: api.lua,v 1.147 2016/11/07 13:06:25 roberto Exp $ |
349 |
+-- See Copyright Notice in file all.lua |
350 |
+ |
351 |
+if T==nil then |
352 |
+ (Message or print)('\n >>> testC not active: skipping API tests <<<\n') |
353 |
+ return |
354 |
+end |
355 |
+ |
356 |
+local debug = require "debug" |
357 |
+ |
358 |
+local pack = table.pack |
359 |
+ |
360 |
+ |
361 |
+function tcheck (t1, t2) |
362 |
+ assert(t1.n == (t2.n or #t2) + 1) |
363 |
+ for i = 2, t1.n do assert(t1[i] == t2[i - 1]) end |
364 |
+end |
365 |
+ |
366 |
+ |
367 |
+local function checkerr (msg, f, ...) |
368 |
+ local stat, err = pcall(f, ...) |
369 |
+ assert(not stat and string.find(err, msg)) |
370 |
+end |
371 |
+ |
372 |
+ |
373 |
+print('testing C API') |
374 |
+ |
375 |
+a = T.testC("pushvalue R; return 1") |
376 |
+assert(a == debug.getregistry()) |
377 |
+ |
378 |
+ |
379 |
+-- absindex |
380 |
+assert(T.testC("settop 10; absindex -1; return 1") == 10) |
381 |
+assert(T.testC("settop 5; absindex -5; return 1") == 1) |
382 |
+assert(T.testC("settop 10; absindex 1; return 1") == 1) |
383 |
+assert(T.testC("settop 10; absindex R; return 1") < -10) |
384 |
+ |
385 |
+-- testing alignment |
386 |
+a = T.d2s(12458954321123.0) |
387 |
+assert(a == string.pack("d", 12458954321123.0)) |
388 |
+assert(T.s2d(a) == 12458954321123.0) |
389 |
+ |
390 |
+a,b,c = T.testC("pushnum 1; pushnum 2; pushnum 3; return 2") |
391 |
+assert(a == 2 and b == 3 and not c) |
392 |
+ |
393 |
+f = T.makeCfunc("pushnum 1; pushnum 2; pushnum 3; return 2") |
394 |
+a,b,c = f() |
395 |
+assert(a == 2 and b == 3 and not c) |
396 |
+ |
397 |
+-- test that all trues are equal |
398 |
+a,b,c = T.testC("pushbool 1; pushbool 2; pushbool 0; return 3") |
399 |
+assert(a == b and a == true and c == false) |
400 |
+a,b,c = T.testC"pushbool 0; pushbool 10; pushnil;\ |
401 |
+ tobool -3; tobool -3; tobool -3; return 3" |
402 |
+assert(a==false and b==true and c==false) |
403 |
+ |
404 |
+ |
405 |
+a,b,c = T.testC("gettop; return 2", 10, 20, 30, 40) |
406 |
+assert(a == 40 and b == 5 and not c) |
407 |
+ |
408 |
+t = pack(T.testC("settop 5; return *", 2, 3)) |
409 |
+tcheck(t, {n=4,2,3}) |
410 |
+ |
411 |
+t = pack(T.testC("settop 0; settop 15; return 10", 3, 1, 23)) |
412 |
+assert(t.n == 10 and t[1] == nil and t[10] == nil) |
413 |
+ |
414 |
+t = pack(T.testC("remove -2; return *", 2, 3, 4)) |
415 |
+tcheck(t, {n=2,2,4}) |
416 |
+ |
417 |
+t = pack(T.testC("insert -1; return *", 2, 3)) |
418 |
+tcheck(t, {n=2,2,3}) |
419 |
+ |
420 |
+t = pack(T.testC("insert 3; return *", 2, 3, 4, 5)) |
421 |
+tcheck(t, {n=4,2,5,3,4}) |
422 |
+ |
423 |
+t = pack(T.testC("replace 2; return *", 2, 3, 4, 5)) |
424 |
+tcheck(t, {n=3,5,3,4}) |
425 |
+ |
426 |
+t = pack(T.testC("replace -2; return *", 2, 3, 4, 5)) |
427 |
+tcheck(t, {n=3,2,3,5}) |
428 |
+ |
429 |
+t = pack(T.testC("remove 3; return *", 2, 3, 4, 5)) |
430 |
+tcheck(t, {n=3,2,4,5}) |
431 |
+ |
432 |
+t = pack(T.testC("copy 3 4; return *", 2, 3, 4, 5)) |
433 |
+tcheck(t, {n=4,2,3,3,5}) |
434 |
+ |
435 |
+t = pack(T.testC("copy -3 -1; return *", 2, 3, 4, 5)) |
436 |
+tcheck(t, {n=4,2,3,4,3}) |
437 |
+ |
438 |
+do -- testing 'rotate' |
439 |
+ local t = {10, 20, 30, 40, 50, 60} |
440 |
+ for i = -6, 6 do |
441 |
+ local s = string.format("rotate 2 %d; return 7", i) |
442 |
+ local t1 = pack(T.testC(s, 10, 20, 30, 40, 50, 60)) |
443 |
+ tcheck(t1, t) |
444 |
+ table.insert(t, 1, table.remove(t)) |
445 |
+ end |
446 |
+ |
447 |
+ t = pack(T.testC("rotate -2 1; return *", 10, 20, 30, 40)) |
448 |
+ tcheck(t, {10, 20, 40, 30}) |
449 |
+ t = pack(T.testC("rotate -2 -1; return *", 10, 20, 30, 40)) |
450 |
+ tcheck(t, {10, 20, 40, 30}) |
451 |
+ |
452 |
+ -- some corner cases |
453 |
+ t = pack(T.testC("rotate -1 0; return *", 10, 20, 30, 40)) |
454 |
+ tcheck(t, {10, 20, 30, 40}) |
455 |
+ t = pack(T.testC("rotate -1 1; return *", 10, 20, 30, 40)) |
456 |
+ tcheck(t, {10, 20, 30, 40}) |
457 |
+ t = pack(T.testC("rotate 5 -1; return *", 10, 20, 30, 40)) |
458 |
+ tcheck(t, {10, 20, 30, 40}) |
459 |
+end |
460 |
+ |
461 |
+-- testing non-function message handlers |
462 |
+do |
463 |
+ local f = T.makeCfunc[[ |
464 |
+ getglobal error |
465 |
+ pushstring bola |
466 |
+ pcall 1 1 1 # call 'error' with given handler |
467 |
+ pushstatus |
468 |
+ return 2 # return error message and status |
469 |
+ ]] |
470 |
+ |
471 |
+ local msg, st = f({}) -- invalid handler |
472 |
+ assert(st == "ERRERR" and string.find(msg, "error handling")) |
473 |
+ local msg, st = f(nil) -- invalid handler |
474 |
+ assert(st == "ERRERR" and string.find(msg, "error handling")) |
475 |
+ |
476 |
+ local a = setmetatable({}, {__call = function (_, x) return x:upper() end}) |
477 |
+ local msg, st = f(a) -- callable handler |
478 |
+ assert(st == "ERRRUN" and msg == "BOLA") |
479 |
+end |
480 |
+ |
481 |
+t = pack(T.testC("insert 3; pushvalue 3; remove 3; pushvalue 2; remove 2; \ |
482 |
+ insert 2; pushvalue 1; remove 1; insert 1; \ |
483 |
+ insert -2; pushvalue -2; remove -3; return *", |
484 |
+ 2, 3, 4, 5, 10, 40, 90)) |
485 |
+tcheck(t, {n=7,2,3,4,5,10,40,90}) |
486 |
+ |
487 |
+t = pack(T.testC("concat 5; return *", "alo", 2, 3, "joao", 12)) |
488 |
+tcheck(t, {n=1,"alo23joao12"}) |
489 |
+ |
490 |
+-- testing MULTRET |
491 |
+t = pack(T.testC("call 2,-1; return *", |
492 |
+ function (a,b) return 1,2,3,4,a,b end, "alo", "joao")) |
493 |
+tcheck(t, {n=6,1,2,3,4,"alo", "joao"}) |
494 |
+ |
495 |
+do -- test returning more results than fit in the caller stack |
496 |
+ local a = {} |
497 |
+ for i=1,1000 do a[i] = true end; a[999] = 10 |
498 |
+ local b = T.testC([[pcall 1 -1 0; pop 1; tostring -1; return 1]], |
499 |
+ table.unpack, a) |
500 |
+ assert(b == "10") |
501 |
+end |
502 |
+ |
503 |
+ |
504 |
+-- testing globals |
505 |
+_G.a = 14; _G.b = "a31" |
506 |
+local a = {T.testC[[ |
507 |
+ getglobal a; |
508 |
+ getglobal b; |
509 |
+ getglobal b; |
510 |
+ setglobal a; |
511 |
+ return * |
512 |
+]]} |
513 |
+assert(a[2] == 14 and a[3] == "a31" and a[4] == nil and _G.a == "a31") |
514 |
+ |
515 |
+ |
516 |
+-- testing arith |
517 |
+assert(T.testC("pushnum 10; pushnum 20; arith /; return 1") == 0.5) |
518 |
+assert(T.testC("pushnum 10; pushnum 20; arith -; return 1") == -10) |
519 |
+assert(T.testC("pushnum 10; pushnum -20; arith *; return 1") == -200) |
520 |
+assert(T.testC("pushnum 10; pushnum 3; arith ^; return 1") == 1000) |
521 |
+assert(T.testC("pushnum 10; pushstring 20; arith /; return 1") == 0.5) |
522 |
+assert(T.testC("pushstring 10; pushnum 20; arith -; return 1") == -10) |
523 |
+assert(T.testC("pushstring 10; pushstring -20; arith *; return 1") == -200) |
524 |
+assert(T.testC("pushstring 10; pushstring 3; arith ^; return 1") == 1000) |
525 |
+assert(T.testC("arith /; return 1", 2, 0) == 10.0/0) |
526 |
+a = T.testC("pushnum 10; pushint 3; arith \\; return 1") |
527 |
+assert(a == 3.0 and math.type(a) == "float") |
528 |
+a = T.testC("pushint 10; pushint 3; arith \\; return 1") |
529 |
+assert(a == 3 and math.type(a) == "integer") |
530 |
+a = assert(T.testC("pushint 10; pushint 3; arith +; return 1")) |
531 |
+assert(a == 13 and math.type(a) == "integer") |
532 |
+a = assert(T.testC("pushnum 10; pushint 3; arith +; return 1")) |
533 |
+assert(a == 13 and math.type(a) == "float") |
534 |
+a,b,c = T.testC([[pushnum 1; |
535 |
+ pushstring 10; arith _; |
536 |
+ pushstring 5; return 3]]) |
537 |
+assert(a == 1 and b == -10 and c == "5") |
538 |
+mt = {__add = function (a,b) return setmetatable({a[1] + b[1]}, mt) end, |
539 |
+ __mod = function (a,b) return setmetatable({a[1] % b[1]}, mt) end, |
540 |
+ __unm = function (a) return setmetatable({a[1]* 2}, mt) end} |
541 |
+a,b,c = setmetatable({4}, mt), |
542 |
+ setmetatable({8}, mt), |
543 |
+ setmetatable({-3}, mt) |
544 |
+x,y,z = T.testC("arith +; return 2", 10, a, b) |
545 |
+assert(x == 10 and y[1] == 12 and z == nil) |
546 |
+assert(T.testC("arith %; return 1", a, c)[1] == 4%-3) |
547 |
+assert(T.testC("arith _; arith +; arith %; return 1", b, a, c)[1] == |
548 |
+ 8 % (4 + (-3)*2)) |
549 |
+ |
550 |
+-- errors in arithmetic |
551 |
+checkerr("divide by zero", T.testC, "arith \\", 10, 0) |
552 |
+checkerr("%%0", T.testC, "arith %", 10, 0) |
553 |
+ |
554 |
+ |
555 |
+-- testing lessthan and lessequal |
556 |
+assert(T.testC("compare LT 2 5, return 1", 3, 2, 2, 4, 2, 2)) |
557 |
+assert(T.testC("compare LE 2 5, return 1", 3, 2, 2, 4, 2, 2)) |
558 |
+assert(not T.testC("compare LT 3 4, return 1", 3, 2, 2, 4, 2, 2)) |
559 |
+assert(T.testC("compare LE 3 4, return 1", 3, 2, 2, 4, 2, 2)) |
560 |
+assert(T.testC("compare LT 5 2, return 1", 4, 2, 2, 3, 2, 2)) |
561 |
+assert(not T.testC("compare LT 2 -3, return 1", "4", "2", "2", "3", "2", "2")) |
562 |
+assert(not T.testC("compare LT -3 2, return 1", "3", "2", "2", "4", "2", "2")) |
563 |
+ |
564 |
+-- non-valid indices produce false |
565 |
+assert(not T.testC("compare LT 1 4, return 1")) |
566 |
+assert(not T.testC("compare LE 9 1, return 1")) |
567 |
+assert(not T.testC("compare EQ 9 9, return 1")) |
568 |
+ |
569 |
+local b = {__lt = function (a,b) return a[1] < b[1] end} |
570 |
+local a1,a3,a4 = setmetatable({1}, b), |
571 |
+ setmetatable({3}, b), |
572 |
+ setmetatable({4}, b) |
573 |
+assert(T.testC("compare LT 2 5, return 1", a3, 2, 2, a4, 2, 2)) |
574 |
+assert(T.testC("compare LE 2 5, return 1", a3, 2, 2, a4, 2, 2)) |
575 |
+assert(T.testC("compare LT 5 -6, return 1", a4, 2, 2, a3, 2, 2)) |
576 |
+a,b = T.testC("compare LT 5 -6, return 2", a1, 2, 2, a3, 2, 20) |
577 |
+assert(a == 20 and b == false) |
578 |
+a,b = T.testC("compare LE 5 -6, return 2", a1, 2, 2, a3, 2, 20) |
579 |
+assert(a == 20 and b == false) |
580 |
+a,b = T.testC("compare LE 5 -6, return 2", a1, 2, 2, a1, 2, 20) |
581 |
+assert(a == 20 and b == true) |
582 |
+ |
583 |
+-- testing length |
584 |
+local t = setmetatable({x = 20}, {__len = function (t) return t.x end}) |
585 |
+a,b,c = T.testC([[ |
586 |
+ len 2; |
587 |
+ Llen 2; |
588 |
+ objsize 2; |
589 |
+ return 3 |
590 |
+]], t) |
591 |
+assert(a == 20 and b == 20 and c == 0) |
592 |
+ |
593 |
+t.x = "234"; t[1] = 20 |
594 |
+a,b,c = T.testC([[ |
595 |
+ len 2; |
596 |
+ Llen 2; |
597 |
+ objsize 2; |
598 |
+ return 3 |
599 |
+]], t) |
600 |
+assert(a == "234" and b == 234 and c == 1) |
601 |
+ |
602 |
+t.x = print; t[1] = 20 |
603 |
+a,c = T.testC([[ |
604 |
+ len 2; |
605 |
+ objsize 2; |
606 |
+ return 2 |
607 |
+]], t) |
608 |
+assert(a == print and c == 1) |
609 |
+ |
610 |
+ |
611 |
+-- testing __concat |
612 |
+ |
613 |
+a = setmetatable({x="u"}, {__concat = function (a,b) return a.x..'.'..b.x end}) |
614 |
+x,y = T.testC([[ |
615 |
+ pushnum 5 |
616 |
+ pushvalue 2; |
617 |
+ pushvalue 2; |
618 |
+ concat 2; |
619 |
+ pushvalue -2; |
620 |
+ return 2; |
621 |
+]], a, a) |
622 |
+assert(x == a..a and y == 5) |
623 |
+ |
624 |
+-- concat with 0 elements |
625 |
+assert(T.testC("concat 0; return 1") == "") |
626 |
+ |
627 |
+-- concat with 1 element |
628 |
+assert(T.testC("concat 1; return 1", "xuxu") == "xuxu") |
629 |
+ |
630 |
+ |
631 |
+ |
632 |
+-- testing lua_is |
633 |
+ |
634 |
+function B(x) return x and 1 or 0 end |
635 |
+ |
636 |
+function count (x, n) |
637 |
+ n = n or 2 |
638 |
+ local prog = [[ |
639 |
+ isnumber %d; |
640 |
+ isstring %d; |
641 |
+ isfunction %d; |
642 |
+ iscfunction %d; |
643 |
+ istable %d; |
644 |
+ isuserdata %d; |
645 |
+ isnil %d; |
646 |
+ isnull %d; |
647 |
+ return 8 |
648 |
+ ]] |
649 |
+ prog = string.format(prog, n, n, n, n, n, n, n, n) |
650 |
+ local a,b,c,d,e,f,g,h = T.testC(prog, x) |
651 |
+ return B(a)+B(b)+B(c)+B(d)+B(e)+B(f)+B(g)+(100*B(h)) |
652 |
+end |
653 |
+ |
654 |
+assert(count(3) == 2) |
655 |
+assert(count('alo') == 1) |
656 |
+assert(count('32') == 2) |
657 |
+assert(count({}) == 1) |
658 |
+assert(count(print) == 2) |
659 |
+assert(count(function () end) == 1) |
660 |
+assert(count(nil) == 1) |
661 |
+assert(count(io.stdin) == 1) |
662 |
+assert(count(nil, 15) == 100) |
663 |
+ |
664 |
+ |
665 |
+-- testing lua_to... |
666 |
+ |
667 |
+function to (s, x, n) |
668 |
+ n = n or 2 |
669 |
+ return T.testC(string.format("%s %d; return 1", s, n), x) |
670 |
+end |
671 |
+ |
672 |
+local hfunc = string.gmatch("", "") -- a "heavy C function" (with upvalues) |
673 |
+assert(debug.getupvalue(hfunc, 1)) |
674 |
+assert(to("tostring", {}) == nil) |
675 |
+assert(to("tostring", "alo") == "alo") |
676 |
+assert(to("tostring", 12) == "12") |
677 |
+assert(to("tostring", 12, 3) == nil) |
678 |
+assert(to("objsize", {}) == 0) |
679 |
+assert(to("objsize", {1,2,3}) == 3) |
680 |
+assert(to("objsize", "alo\0\0a") == 6) |
681 |
+assert(to("objsize", T.newuserdata(0)) == 0) |
682 |
+assert(to("objsize", T.newuserdata(101)) == 101) |
683 |
+assert(to("objsize", 124) == 0) |
684 |
+assert(to("objsize", true) == 0) |
685 |
+assert(to("tonumber", {}) == 0) |
686 |
+assert(to("tonumber", "12") == 12) |
687 |
+assert(to("tonumber", "s2") == 0) |
688 |
+assert(to("tonumber", 1, 20) == 0) |
689 |
+assert(to("topointer", 10) == 0) |
690 |
+assert(to("topointer", true) == 0) |
691 |
+assert(to("topointer", T.pushuserdata(20)) == 20) |
692 |
+assert(to("topointer", io.read) ~= 0) -- light C function |
693 |
+assert(to("topointer", hfunc) ~= 0) -- "heavy" C function |
694 |
+assert(to("topointer", function () end) ~= 0) -- Lua function |
695 |
+assert(to("topointer", io.stdin) ~= 0) -- full userdata |
696 |
+assert(to("func2num", 20) == 0) |
697 |
+assert(to("func2num", T.pushuserdata(10)) == 0) |
698 |
+assert(to("func2num", io.read) ~= 0) -- light C function |
699 |
+assert(to("func2num", hfunc) ~= 0) -- "heavy" C function (with upvalue) |
700 |
+a = to("tocfunction", math.deg) |
701 |
+assert(a(3) == math.deg(3) and a == math.deg) |
702 |
+ |
703 |
+ |
704 |
+print("testing panic function") |
705 |
+do |
706 |
+ -- trivial error |
707 |
+ assert(T.checkpanic("pushstring hi; error") == "hi") |
708 |
+ |
709 |
+ -- using the stack inside panic |
710 |
+ assert(T.checkpanic("pushstring hi; error;", |
711 |
+ [[checkstack 5 XX |
712 |
+ pushstring ' alo' |
713 |
+ pushstring ' mundo' |
714 |
+ concat 3]]) == "hi alo mundo") |
715 |
+ |
716 |
+ -- "argerror" without frames |
717 |
+ assert(T.checkpanic("loadstring 4") == |
718 |
+ "bad argument #4 (string expected, got no value)") |
719 |
+ |
720 |
+ |
721 |
+ -- memory error |
722 |
+ T.totalmem(T.totalmem()+10000) -- set low memory limit (+10k) |
723 |
+ assert(T.checkpanic("newuserdata 20000") == "not enough memory") |
724 |
+ T.totalmem(0) -- restore high limit |
725 |
+ |
726 |
+ -- stack error |
727 |
+ if not _soft then |
728 |
+ local msg = T.checkpanic[[ |
729 |
+ pushstring "function f() f() end" |
730 |
+ loadstring -1; call 0 0 |
731 |
+ getglobal f; call 0 0 |
732 |
+ ]] |
733 |
+ assert(string.find(msg, "stack overflow")) |
734 |
+ end |
735 |
+ |
736 |
+end |
737 |
+ |
738 |
+-- testing deep C stack |
739 |
+if not _soft then |
740 |
+ print("testing stack overflow") |
741 |
+ collectgarbage("stop") |
742 |
+ checkerr("XXXX", T.testC, "checkstack 1000023 XXXX") -- too deep |
743 |
+ -- too deep (with no message) |
744 |
+ checkerr("^stack overflow$", T.testC, "checkstack 1000023 ''") |
745 |
+ local s = string.rep("pushnil;checkstack 1 XX;", 1000000) |
746 |
+ checkerr("overflow", T.testC, s) |
747 |
+ collectgarbage("restart") |
748 |
+ print'+' |
749 |
+end |
750 |
+ |
751 |
+local lim = _soft and 500 or 12000 |
752 |
+local prog = {"checkstack " .. (lim * 2 + 100) .. "msg", "newtable"} |
753 |
+for i = 1,lim do |
754 |
+ prog[#prog + 1] = "pushnum " .. i |
755 |
+ prog[#prog + 1] = "pushnum " .. i * 10 |
756 |
+end |
757 |
+ |
758 |
+prog[#prog + 1] = "rawgeti R 2" -- get global table in registry |
759 |
+prog[#prog + 1] = "insert " .. -(2*lim + 2) |
760 |
+ |
761 |
+for i = 1,lim do |
762 |
+ prog[#prog + 1] = "settable " .. -(2*(lim - i + 1) + 1) |
763 |
+end |
764 |
+ |
765 |
+prog[#prog + 1] = "return 2" |
766 |
+ |
767 |
+prog = table.concat(prog, ";") |
768 |
+local g, t = T.testC(prog) |
769 |
+assert(g == _G) |
770 |
+for i = 1,lim do assert(t[i] == i*10); t[i] = nil end |
771 |
+assert(next(t) == nil) |
772 |
+prog, g, t = nil |
773 |
+ |
774 |
+-- testing errors |
775 |
+ |
776 |
+a = T.testC([[ |
777 |
+ loadstring 2; pcall 0 1 0; |
778 |
+ pushvalue 3; insert -2; pcall 1 1 0; |
779 |
+ pcall 0 0 0; |
780 |
+ return 1 |
781 |
+]], "x=150", function (a) assert(a==nil); return 3 end) |
782 |
+ |
783 |
+assert(type(a) == 'string' and x == 150) |
784 |
+ |
785 |
+function check3(p, ...) |
786 |
+ local arg = {...} |
787 |
+ assert(#arg == 3) |
788 |
+ assert(string.find(arg[3], p)) |
789 |
+end |
790 |
+check3(":1:", T.testC("loadstring 2; return *", "x=")) |
791 |
+check3("%.", T.testC("loadfile 2; return *", ".")) |
792 |
+check3("xxxx", T.testC("loadfile 2; return *", "xxxx")) |
793 |
+ |
794 |
+-- test errors in non protected threads |
795 |
+function checkerrnopro (code, msg) |
796 |
+ local th = coroutine.create(function () end) -- create new thread |
797 |
+ local stt, err = pcall(T.testC, th, code) -- run code there |
798 |
+ assert(not stt and string.find(err, msg)) |
799 |
+end |
800 |
+ |
801 |
+if not _soft then |
802 |
+ checkerrnopro("pushnum 3; call 0 0", "attempt to call") |
803 |
+ print"testing stack overflow in unprotected thread" |
804 |
+ function f () f() end |
805 |
+ checkerrnopro("getglobal 'f'; call 0 0;", "stack overflow") |
806 |
+end |
807 |
+print"+" |
808 |
+ |
809 |
+ |
810 |
+-- testing table access |
811 |
+ |
812 |
+do -- getp/setp |
813 |
+ local a = {} |
814 |
+ T.testC("rawsetp 2 1", a, 20) |
815 |
+ assert(a[T.pushuserdata(1)] == 20) |
816 |
+ assert(T.testC("rawgetp 2 1; return 1", a) == 20) |
817 |
+end |
818 |
+ |
819 |
+a = {x=0, y=12} |
820 |
+x, y = T.testC("gettable 2; pushvalue 4; gettable 2; return 2", |
821 |
+ a, 3, "y", 4, "x") |
822 |
+assert(x == 0 and y == 12) |
823 |
+T.testC("settable -5", a, 3, 4, "x", 15) |
824 |
+assert(a.x == 15) |
825 |
+a[a] = print |
826 |
+x = T.testC("gettable 2; return 1", a) -- table and key are the same object! |
827 |
+assert(x == print) |
828 |
+T.testC("settable 2", a, "x") -- table and key are the same object! |
829 |
+assert(a[a] == "x") |
830 |
+ |
831 |
+b = setmetatable({p = a}, {}) |
832 |
+getmetatable(b).__index = function (t, i) return t.p[i] end |
833 |
+k, x = T.testC("gettable 3, return 2", 4, b, 20, 35, "x") |
834 |
+assert(x == 15 and k == 35) |
835 |
+k = T.testC("getfield 2 y, return 1", b) |
836 |
+assert(k == 12) |
837 |
+getmetatable(b).__index = function (t, i) return a[i] end |
838 |
+getmetatable(b).__newindex = function (t, i,v ) a[i] = v end |
839 |
+y = T.testC("insert 2; gettable -5; return 1", 2, 3, 4, "y", b) |
840 |
+assert(y == 12) |
841 |
+k = T.testC("settable -5, return 1", b, 3, 4, "x", 16) |
842 |
+assert(a.x == 16 and k == 4) |
843 |
+a[b] = 'xuxu' |
844 |
+y = T.testC("gettable 2, return 1", b) |
845 |
+assert(y == 'xuxu') |
846 |
+T.testC("settable 2", b, 19) |
847 |
+assert(a[b] == 19) |
848 |
+ |
849 |
+-- |
850 |
+do -- testing getfield/setfield with long keys |
851 |
+ local t = {_012345678901234567890123456789012345678901234567890123456789 = 32} |
852 |
+ local a = T.testC([[ |
853 |
+ getfield 2 _012345678901234567890123456789012345678901234567890123456789 |
854 |
+ return 1 |
855 |
+ ]], t) |
856 |
+ assert(a == 32) |
857 |
+ local a = T.testC([[ |
858 |
+ pushnum 33 |
859 |
+ setglobal _012345678901234567890123456789012345678901234567890123456789 |
860 |
+ ]]) |
861 |
+ assert(_012345678901234567890123456789012345678901234567890123456789 == 33) |
862 |
+ _012345678901234567890123456789012345678901234567890123456789 = nil |
863 |
+end |
864 |
+ |
865 |
+-- testing next |
866 |
+a = {} |
867 |
+t = pack(T.testC("next; return *", a, nil)) |
868 |
+tcheck(t, {n=1,a}) |
869 |
+a = {a=3} |
870 |
+t = pack(T.testC("next; return *", a, nil)) |
871 |
+tcheck(t, {n=3,a,'a',3}) |
872 |
+t = pack(T.testC("next; pop 1; next; return *", a, nil)) |
873 |
+tcheck(t, {n=1,a}) |
874 |
+ |
875 |
+ |
876 |
+ |
877 |
+-- testing upvalues |
878 |
+ |
879 |
+do |
880 |
+ local A = T.testC[[ pushnum 10; pushnum 20; pushcclosure 2; return 1]] |
881 |
+ t, b, c = A([[pushvalue U0; pushvalue U1; pushvalue U2; return 3]]) |
882 |
+ assert(b == 10 and c == 20 and type(t) == 'table') |
883 |
+ a, b = A([[tostring U3; tonumber U4; return 2]]) |
884 |
+ assert(a == nil and b == 0) |
885 |
+ A([[pushnum 100; pushnum 200; replace U2; replace U1]]) |
886 |
+ b, c = A([[pushvalue U1; pushvalue U2; return 2]]) |
887 |
+ assert(b == 100 and c == 200) |
888 |
+ A([[replace U2; replace U1]], {x=1}, {x=2}) |
889 |
+ b, c = A([[pushvalue U1; pushvalue U2; return 2]]) |
890 |
+ assert(b.x == 1 and c.x == 2) |
891 |
+ T.checkmemory() |
892 |
+end |
893 |
+ |
894 |
+ |
895 |
+-- testing absent upvalues from C-function pointers |
896 |
+assert(T.testC[[isnull U1; return 1]] == true) |
897 |
+assert(T.testC[[isnull U100; return 1]] == true) |
898 |
+assert(T.testC[[pushvalue U1; return 1]] == nil) |
899 |
+ |
900 |
+local f = T.testC[[ pushnum 10; pushnum 20; pushcclosure 2; return 1]] |
901 |
+assert(T.upvalue(f, 1) == 10 and |
902 |
+ T.upvalue(f, 2) == 20 and |
903 |
+ T.upvalue(f, 3) == nil) |
904 |
+T.upvalue(f, 2, "xuxu") |
905 |
+assert(T.upvalue(f, 2) == "xuxu") |
906 |
+ |
907 |
+ |
908 |
+-- large closures |
909 |
+do |
910 |
+ local A = "checkstack 300 msg;" .. |
911 |
+ string.rep("pushnum 10;", 255) .. |
912 |
+ "pushcclosure 255; return 1" |
913 |
+ A = T.testC(A) |
914 |
+ for i=1,255 do |
915 |
+ assert(A(("pushvalue U%d; return 1"):format(i)) == 10) |
916 |
+ end |
917 |
+ assert(A("isnull U256; return 1")) |
918 |
+ assert(not A("isnil U256; return 1")) |
919 |
+end |
920 |
+ |
921 |
+ |
922 |
+ |
923 |
+-- testing get/setuservalue |
924 |
+-- bug in 5.1.2 |
925 |
+checkerr("got number", debug.setuservalue, 3, {}) |
926 |
+checkerr("got nil", debug.setuservalue, nil, {}) |
927 |
+checkerr("got light userdata", debug.setuservalue, T.pushuserdata(1), {}) |
928 |
+ |
929 |
+local b = T.newuserdata(0) |
930 |
+assert(debug.getuservalue(b) == nil) |
931 |
+for _, v in pairs{true, false, 4.56, print, {}, b, "XYZ"} do |
932 |
+ assert(debug.setuservalue(b, v) == b) |
933 |
+ assert(debug.getuservalue(b) == v) |
934 |
+end |
935 |
+ |
936 |
+assert(debug.getuservalue(4) == nil) |
937 |
+ |
938 |
+debug.setuservalue(b, function () return 10 end) |
939 |
+collectgarbage() -- function should not be collected |
940 |
+assert(debug.getuservalue(b)() == 10) |
941 |
+ |
942 |
+debug.setuservalue(b, 134) |
943 |
+collectgarbage() -- number should not be a problem for collector |
944 |
+assert(debug.getuservalue(b) == 134) |
945 |
+ |
946 |
+-- test barrier for uservalues |
947 |
+T.gcstate("atomic") |
948 |
+assert(T.gccolor(b) == "black") |
949 |
+debug.setuservalue(b, {x = 100}) |
950 |
+T.gcstate("pause") -- complete collection |
951 |
+assert(debug.getuservalue(b).x == 100) -- uvalue should be there |
952 |
+ |
953 |
+-- long chain of userdata |
954 |
+for i = 1, 1000 do |
955 |
+ local bb = T.newuserdata(0) |
956 |
+ debug.setuservalue(bb, b) |
957 |
+ b = bb |
958 |
+end |
959 |
+collectgarbage() -- nothing should not be collected |
960 |
+for i = 1, 1000 do |
961 |
+ b = debug.getuservalue(b) |
962 |
+end |
963 |
+assert(debug.getuservalue(b).x == 100) |
964 |
+b = nil |
965 |
+ |
966 |
+ |
967 |
+-- testing locks (refs) |
968 |
+ |
969 |
+-- reuse of references |
970 |
+local i = T.ref{} |
971 |
+T.unref(i) |
972 |
+assert(T.ref{} == i) |
973 |
+ |
974 |
+Arr = {} |
975 |
+Lim = 100 |
976 |
+for i=1,Lim do -- lock many objects |
977 |
+ Arr[i] = T.ref({}) |
978 |
+end |
979 |
+ |
980 |
+assert(T.ref(nil) == -1 and T.getref(-1) == nil) |
981 |
+T.unref(-1); T.unref(-1) |
982 |
+ |
983 |
+for i=1,Lim do -- unlock all them |
984 |
+ T.unref(Arr[i]) |
985 |
+end |
986 |
+ |
987 |
+function printlocks () |
988 |
+ local f = T.makeCfunc("gettable R; return 1") |
989 |
+ local n = f("n") |
990 |
+ print("n", n) |
991 |
+ for i=0,n do |
992 |
+ print(i, f(i)) |
993 |
+ end |
994 |
+end |
995 |
+ |
996 |
+ |
997 |
+for i=1,Lim do -- lock many objects |
998 |
+ Arr[i] = T.ref({}) |
999 |
+end |
1000 |
+ |
1001 |
+for i=1,Lim,2 do -- unlock half of them |
1002 |
+ T.unref(Arr[i]) |
1003 |
+end |
1004 |
+ |
1005 |
+assert(type(T.getref(Arr[2])) == 'table') |
1006 |
+ |
1007 |
+ |
1008 |
+assert(T.getref(-1) == nil) |
1009 |
+ |
1010 |
+ |
1011 |
+a = T.ref({}) |
1012 |
+ |
1013 |
+collectgarbage() |
1014 |
+ |
1015 |
+assert(type(T.getref(a)) == 'table') |
1016 |
+ |
1017 |
+ |
1018 |
+-- colect in cl the `val' of all collected userdata |
1019 |
+tt = {} |
1020 |
+cl = {n=0} |
1021 |
+A = nil; B = nil |
1022 |
+local F |
1023 |
+F = function (x) |
1024 |
+ local udval = T.udataval(x) |
1025 |
+ table.insert(cl, udval) |
1026 |
+ local d = T.newuserdata(100) -- cria lixo |
1027 |
+ d = nil |
1028 |
+ assert(debug.getmetatable(x).__gc == F) |
1029 |
+ assert(load("table.insert({}, {})"))() -- cria mais lixo |
1030 |
+ collectgarbage() -- forca coleta de lixo durante coleta! |
1031 |
+ assert(debug.getmetatable(x).__gc == F) -- coleta anterior nao melou isso? |
1032 |
+ local dummy = {} -- cria lixo durante coleta |
1033 |
+ if A ~= nil then |
1034 |
+ assert(type(A) == "userdata") |
1035 |
+ assert(T.udataval(A) == B) |
1036 |
+ debug.getmetatable(A) -- just acess it |
1037 |
+ end |
1038 |
+ A = x -- ressucita userdata |
1039 |
+ B = udval |
1040 |
+ return 1,2,3 |
1041 |
+end |
1042 |
+tt.__gc = F |
1043 |
+ |
1044 |
+-- test whether udate collection frees memory in the right time |
1045 |
+do |
1046 |
+ collectgarbage(); |
1047 |
+ collectgarbage(); |
1048 |
+ local x = collectgarbage("count"); |
1049 |
+ local a = T.newuserdata(5001) |
1050 |
+ assert(T.testC("objsize 2; return 1", a) == 5001) |
1051 |
+ assert(collectgarbage("count") >= x+4) |
1052 |
+ a = nil |
1053 |
+ collectgarbage(); |
1054 |
+ assert(collectgarbage("count") <= x+1) |
1055 |
+ -- udata without finalizer |
1056 |
+ x = collectgarbage("count") |
1057 |
+ collectgarbage("stop") |
1058 |
+ for i=1,1000 do T.newuserdata(0) end |
1059 |
+ assert(collectgarbage("count") > x+10) |
1060 |
+ collectgarbage() |
1061 |
+ assert(collectgarbage("count") <= x+1) |
1062 |
+ -- udata with finalizer |
1063 |
+ collectgarbage() |
1064 |
+ x = collectgarbage("count") |
1065 |
+ collectgarbage("stop") |
1066 |
+ a = {__gc = function () end} |
1067 |
+ for i=1,1000 do debug.setmetatable(T.newuserdata(0), a) end |
1068 |
+ assert(collectgarbage("count") >= x+10) |
1069 |
+ collectgarbage() -- this collection only calls TM, without freeing memory |
1070 |
+ assert(collectgarbage("count") >= x+10) |
1071 |
+ collectgarbage() -- now frees memory |
1072 |
+ assert(collectgarbage("count") <= x+1) |
1073 |
+ collectgarbage("restart") |
1074 |
+end |
1075 |
+ |
1076 |
+ |
1077 |
+collectgarbage("stop") |
1078 |
+ |
1079 |
+-- create 3 userdatas with tag `tt' |
1080 |
+a = T.newuserdata(0); debug.setmetatable(a, tt); na = T.udataval(a) |
1081 |
+b = T.newuserdata(0); debug.setmetatable(b, tt); nb = T.udataval(b) |
1082 |
+c = T.newuserdata(0); debug.setmetatable(c, tt); nc = T.udataval(c) |
1083 |
+ |
1084 |
+-- create userdata without meta table |
1085 |
+x = T.newuserdata(4) |
1086 |
+y = T.newuserdata(0) |
1087 |
+ |
1088 |
+checkerr("FILE%* expected, got userdata", io.input, a) |
1089 |
+checkerr("FILE%* expected, got userdata", io.input, x) |
1090 |
+ |
1091 |
+assert(debug.getmetatable(x) == nil and debug.getmetatable(y) == nil) |
1092 |
+ |
1093 |
+d=T.ref(a); |
1094 |
+e=T.ref(b); |
1095 |
+f=T.ref(c); |
1096 |
+t = {T.getref(d), T.getref(e), T.getref(f)} |
1097 |
+assert(t[1] == a and t[2] == b and t[3] == c) |
1098 |
+ |
1099 |
+t=nil; a=nil; c=nil; |
1100 |
+T.unref(e); T.unref(f) |
1101 |
+ |
1102 |
+collectgarbage() |
1103 |
+ |
1104 |
+-- check that unref objects have been collected |
1105 |
+assert(#cl == 1 and cl[1] == nc) |
1106 |
+ |
1107 |
+x = T.getref(d) |
1108 |
+assert(type(x) == 'userdata' and debug.getmetatable(x) == tt) |
1109 |
+x =nil |
1110 |
+tt.b = b -- create cycle |
1111 |
+tt=nil -- frees tt for GC |
1112 |
+A = nil |
1113 |
+b = nil |
1114 |
+T.unref(d); |
1115 |
+n5 = T.newuserdata(0) |
1116 |
+debug.setmetatable(n5, {__gc=F}) |
1117 |
+n5 = T.udataval(n5) |
1118 |
+collectgarbage() |
1119 |
+assert(#cl == 4) |
1120 |
+-- check order of collection |
1121 |
+assert(cl[2] == n5 and cl[3] == nb and cl[4] == na) |
1122 |
+ |
1123 |
+collectgarbage"restart" |
1124 |
+ |
1125 |
+ |
1126 |
+a, na = {}, {} |
1127 |
+for i=30,1,-1 do |
1128 |
+ a[i] = T.newuserdata(0) |
1129 |
+ debug.setmetatable(a[i], {__gc=F}) |
1130 |
+ na[i] = T.udataval(a[i]) |
1131 |
+end |
1132 |
+cl = {} |
1133 |
+a = nil; collectgarbage() |
1134 |
+assert(#cl == 30) |
1135 |
+for i=1,30 do assert(cl[i] == na[i]) end |
1136 |
+na = nil |
1137 |
+ |
1138 |
+ |
1139 |
+for i=2,Lim,2 do -- unlock the other half |
1140 |
+ T.unref(Arr[i]) |
1141 |
+end |
1142 |
+ |
1143 |
+x = T.newuserdata(41); debug.setmetatable(x, {__gc=F}) |
1144 |
+assert(T.testC("objsize 2; return 1", x) == 41) |
1145 |
+cl = {} |
1146 |
+a = {[x] = 1} |
1147 |
+x = T.udataval(x) |
1148 |
+collectgarbage() |
1149 |
+-- old `x' cannot be collected (`a' still uses it) |
1150 |
+assert(#cl == 0) |
1151 |
+for n in pairs(a) do a[n] = nil end |
1152 |
+collectgarbage() |
1153 |
+assert(#cl == 1 and cl[1] == x) -- old `x' must be collected |
1154 |
+ |
1155 |
+-- testing lua_equal |
1156 |
+assert(T.testC("compare EQ 2 4; return 1", print, 1, print, 20)) |
1157 |
+assert(T.testC("compare EQ 3 2; return 1", 'alo', "alo")) |
1158 |
+assert(T.testC("compare EQ 2 3; return 1", nil, nil)) |
1159 |
+assert(not T.testC("compare EQ 2 3; return 1", {}, {})) |
1160 |
+assert(not T.testC("compare EQ 2 3; return 1")) |
1161 |
+assert(not T.testC("compare EQ 2 3; return 1", 3)) |
1162 |
+ |
1163 |
+-- testing lua_equal with fallbacks |
1164 |
+do |
1165 |
+ local map = {} |
1166 |
+ local t = {__eq = function (a,b) return map[a] == map[b] end} |
1167 |
+ local function f(x) |
1168 |
+ local u = T.newuserdata(0) |
1169 |
+ debug.setmetatable(u, t) |
1170 |
+ map[u] = x |
1171 |
+ return u |
1172 |
+ end |
1173 |
+ assert(f(10) == f(10)) |
1174 |
+ assert(f(10) ~= f(11)) |
1175 |
+ assert(T.testC("compare EQ 2 3; return 1", f(10), f(10))) |
1176 |
+ assert(not T.testC("compare EQ 2 3; return 1", f(10), f(20))) |
1177 |
+ t.__eq = nil |
1178 |
+ assert(f(10) ~= f(10)) |
1179 |
+end |
1180 |
+ |
1181 |
+print'+' |
1182 |
+ |
1183 |
+ |
1184 |
+ |
1185 |
+-- testing changing hooks during hooks |
1186 |
+_G.t = {} |
1187 |
+T.sethook([[ |
1188 |
+ # set a line hook after 3 count hooks |
1189 |
+ sethook 4 0 ' |
1190 |
+ getglobal t; |
1191 |
+ pushvalue -3; append -2 |
1192 |
+ pushvalue -2; append -2 |
1193 |
+ ']], "c", 3) |
1194 |
+local a = 1 -- counting |
1195 |
+a = 1 -- counting |
1196 |
+a = 1 -- count hook (set line hook) |
1197 |
+a = 1 -- line hook |
1198 |
+a = 1 -- line hook |
1199 |
+debug.sethook() |
1200 |
+t = _G.t |
1201 |
+assert(t[1] == "line") |
1202 |
+line = t[2] |
1203 |
+assert(t[3] == "line" and t[4] == line + 1) |
1204 |
+assert(t[5] == "line" and t[6] == line + 2) |
1205 |
+assert(t[7] == nil) |
1206 |
+ |
1207 |
+ |
1208 |
+------------------------------------------------------------------------- |
1209 |
+do -- testing errors during GC |
1210 |
+ local a = {} |
1211 |
+ for i=1,20 do |
1212 |
+ a[i] = T.newuserdata(i) -- creates several udata |
1213 |
+ end |
1214 |
+ for i=1,20,2 do -- mark half of them to raise errors during GC |
1215 |
+ debug.setmetatable(a[i], {__gc = function (x) error("error inside gc") end}) |
1216 |
+ end |
1217 |
+ for i=2,20,2 do -- mark the other half to count and to create more garbage |
1218 |
+ debug.setmetatable(a[i], {__gc = function (x) load("A=A+1")() end}) |
1219 |
+ end |
1220 |
+ _G.A = 0 |
1221 |
+ a = 0 |
1222 |
+ while 1 do |
1223 |
+ local stat, msg = pcall(collectgarbage) |
1224 |
+ if stat then |
1225 |
+ break -- stop when no more errors |
1226 |
+ else |
1227 |
+ a = a + 1 |
1228 |
+ assert(string.find(msg, "__gc")) |
1229 |
+ end |
1230 |
+ end |
1231 |
+ assert(a == 10) -- number of errors |
1232 |
+ |
1233 |
+ assert(A == 10) -- number of normal collections |
1234 |
+end |
1235 |
+------------------------------------------------------------------------- |
1236 |
+-- test for userdata vals |
1237 |
+do |
1238 |
+ local a = {}; local lim = 30 |
1239 |
+ for i=0,lim do a[i] = T.pushuserdata(i) end |
1240 |
+ for i=0,lim do assert(T.udataval(a[i]) == i) end |
1241 |
+ for i=0,lim do assert(T.pushuserdata(i) == a[i]) end |
1242 |
+ for i=0,lim do a[a[i]] = i end |
1243 |
+ for i=0,lim do a[T.pushuserdata(i)] = i end |
1244 |
+ assert(type(tostring(a[1])) == "string") |
1245 |
+end |
1246 |
+ |
1247 |
+ |
1248 |
+------------------------------------------------------------------------- |
1249 |
+-- testing multiple states |
1250 |
+T.closestate(T.newstate()); |
1251 |
+L1 = T.newstate() |
1252 |
+assert(L1) |
1253 |
+ |
1254 |
+assert(T.doremote(L1, "X='a'; return 'a'") == 'a') |
1255 |
+ |
1256 |
+ |
1257 |
+assert(#pack(T.doremote(L1, "function f () return 'alo', 3 end; f()")) == 0) |
1258 |
+ |
1259 |
+a, b = T.doremote(L1, "return f()") |
1260 |
+assert(a == 'alo' and b == '3') |
1261 |
+ |
1262 |
+T.doremote(L1, "_ERRORMESSAGE = nil") |
1263 |
+-- error: `sin' is not defined |
1264 |
+a, _, b = T.doremote(L1, "return sin(1)") |
1265 |
+assert(a == nil and b == 2) -- 2 == run-time error |
1266 |
+ |
1267 |
+-- error: syntax error |
1268 |
+a, b, c = T.doremote(L1, "return a+") |
1269 |
+assert(a == nil and c == 3 and type(b) == "string") -- 3 == syntax error |
1270 |
+ |
1271 |
+T.loadlib(L1) |
1272 |
+a, b, c = T.doremote(L1, [[ |
1273 |
+ string = require'string' |
1274 |
+ a = require'_G'; assert(a == _G and require("_G") == a) |
1275 |
+ io = require'io'; assert(type(io.read) == "function") |
1276 |
+ assert(require("io") == io) |
1277 |
+ a = require'table'; assert(type(a.insert) == "function") |
1278 |
+ a = require'debug'; assert(type(a.getlocal) == "function") |
1279 |
+ a = require'math'; assert(type(a.sin) == "function") |
1280 |
+ return string.sub('okinama', 1, 2) |
1281 |
+]]) |
1282 |
+assert(a == "ok") |
1283 |
+ |
1284 |
+T.closestate(L1); |
1285 |
+ |
1286 |
+ |
1287 |
+L1 = T.newstate() |
1288 |
+T.loadlib(L1) |
1289 |
+T.doremote(L1, "a = {}") |
1290 |
+T.testC(L1, [[getglobal "a"; pushstring "x"; pushint 1; |
1291 |
+ settable -3]]) |
1292 |
+assert(T.doremote(L1, "return a.x") == "1") |
1293 |
+ |
1294 |
+T.closestate(L1) |
1295 |
+ |
1296 |
+L1 = nil |
1297 |
+ |
1298 |
+print('+') |
1299 |
+ |
1300 |
+------------------------------------------------------------------------- |
1301 |
+-- testing memory limits |
1302 |
+------------------------------------------------------------------------- |
1303 |
+checkerr("block too big", T.newuserdata, math.maxinteger) |
1304 |
+collectgarbage() |
1305 |
+T.totalmem(T.totalmem()+5000) -- set low memory limit (+5k) |
1306 |
+checkerr("not enough memory", load"local a={}; for i=1,100000 do a[i]=i end") |
1307 |
+T.totalmem(0) -- restore high limit |
1308 |
+ |
1309 |
+-- test memory errors; increase memory limit in small steps, so that |
1310 |
+-- we get memory errors in different parts of a given task, up to there |
1311 |
+-- is enough memory to complete the task without errors |
1312 |
+function testamem (s, f) |
1313 |
+ collectgarbage(); collectgarbage() |
1314 |
+ local M = T.totalmem() |
1315 |
+ local oldM = M |
1316 |
+ local a,b = nil |
1317 |
+ while 1 do |
1318 |
+ M = M+7 -- increase memory limit in small steps |
1319 |
+ T.totalmem(M) |
1320 |
+ a, b = pcall(f) |
1321 |
+ T.totalmem(0) -- restore high limit |
1322 |
+ if a and b then break end -- stop when no more errors |
1323 |
+ collectgarbage() |
1324 |
+ if not a and not -- `real' error? |
1325 |
+ (string.find(b, "memory") or string.find(b, "overflow")) then |
1326 |
+ error(b, 0) -- propagate it |
1327 |
+ end |
1328 |
+ end |
1329 |
+ print("\nlimit for " .. s .. ": " .. M-oldM) |
1330 |
+ return b |
1331 |
+end |
1332 |
+ |
1333 |
+ |
1334 |
+-- testing memory errors when creating a new state |
1335 |
+ |
1336 |
+b = testamem("state creation", T.newstate) |
1337 |
+T.closestate(b); -- close new state |
1338 |
+ |
1339 |
+ |
1340 |
+-- testing threads |
1341 |
+ |
1342 |
+-- get main thread from registry (at index LUA_RIDX_MAINTHREAD == 1) |
1343 |
+mt = T.testC("rawgeti R 1; return 1") |
1344 |
+assert(type(mt) == "thread" and coroutine.running() == mt) |
1345 |
+ |
1346 |
+ |
1347 |
+ |
1348 |
+function expand (n,s) |
1349 |
+ if n==0 then return "" end |
1350 |
+ local e = string.rep("=", n) |
1351 |
+ return string.format("T.doonnewstack([%s[ %s;\n collectgarbage(); %s]%s])\n", |
1352 |
+ e, s, expand(n-1,s), e) |
1353 |
+end |
1354 |
+ |
1355 |
+G=0; collectgarbage(); a =collectgarbage("count") |
1356 |
+load(expand(20,"G=G+1"))() |
1357 |
+assert(G==20); collectgarbage(); -- assert(gcinfo() <= a+1) |
1358 |
+ |
1359 |
+testamem("thread creation", function () |
1360 |
+ return T.doonnewstack("x=1") == 0 -- try to create thread |
1361 |
+end) |
1362 |
+ |
1363 |
+ |
1364 |
+-- testing memory x compiler |
1365 |
+ |
1366 |
+testamem("loadstring", function () |
1367 |
+ return load("x=1") -- try to do load a string |
1368 |
+end) |
1369 |
+ |
1370 |
+ |
1371 |
+local testprog = [[ |
1372 |
+local function foo () return end |
1373 |
+local t = {"x"} |
1374 |
+a = "aaa" |
1375 |
+for i = 1, #t do a=a..t[i] end |
1376 |
+return true |
1377 |
+]] |
1378 |
+ |
1379 |
+-- testing memory x dofile |
1380 |
+_G.a = nil |
1381 |
+local t =os.tmpname() |
1382 |
+local f = assert(io.open(t, "w")) |
1383 |
+f:write(testprog) |
1384 |
+f:close() |
1385 |
+testamem("dofile", function () |
1386 |
+ local a = loadfile(t) |
1387 |
+ return a and a() |
1388 |
+end) |
1389 |
+assert(os.remove(t)) |
1390 |
+assert(_G.a == "aaax") |
1391 |
+ |
1392 |
+ |
1393 |
+-- other generic tests |
1394 |
+ |
1395 |
+testamem("string creation", function () |
1396 |
+ local a, b = string.gsub("alo alo", "(a)", function (x) return x..'b' end) |
1397 |
+ return (a == 'ablo ablo') |
1398 |
+end) |
1399 |
+ |
1400 |
+testamem("dump/undump", function () |
1401 |
+ local a = load(testprog) |
1402 |
+ local b = a and string.dump(a) |
1403 |
+ a = b and load(b) |
1404 |
+ return a and a() |
1405 |
+end) |
1406 |
+ |
1407 |
+local t = os.tmpname() |
1408 |
+testamem("file creation", function () |
1409 |
+ local f = assert(io.open(t, 'w')) |
1410 |
+ assert (not io.open"nomenaoexistente") |
1411 |
+ io.close(f); |
1412 |
+ return not loadfile'nomenaoexistente' |
1413 |
+end) |
1414 |
+assert(os.remove(t)) |
1415 |
+ |
1416 |
+testamem("table creation", function () |
1417 |
+ local a, lim = {}, 10 |
1418 |
+ for i=1,lim do a[i] = i; a[i..'a'] = {} end |
1419 |
+ return (type(a[lim..'a']) == 'table' and a[lim] == lim) |
1420 |
+end) |
1421 |
+ |
1422 |
+testamem("constructors", function () |
1423 |
+ local a = {10, 20, 30, 40, 50; a=1, b=2, c=3, d=4, e=5} |
1424 |
+ return (type(a) == 'table' and a.e == 5) |
1425 |
+end) |
1426 |
+ |
1427 |
+local a = 1 |
1428 |
+close = nil |
1429 |
+testamem("closure creation", function () |
1430 |
+ function close (b,c) |
1431 |
+ return function (x) return a+b+c+x end |
1432 |
+ end |
1433 |
+ return (close(2,3)(4) == 10) |
1434 |
+end) |
1435 |
+ |
1436 |
+testamem("coroutines", function () |
1437 |
+ local a = coroutine.wrap(function () |
1438 |
+ coroutine.yield(string.rep("a", 10)) |
1439 |
+ return {} |
1440 |
+ end) |
1441 |
+ assert(string.len(a()) == 10) |
1442 |
+ return a() |
1443 |
+end) |
1444 |
+ |
1445 |
+do -- auxiliary buffer |
1446 |
+ local lim = 100 |
1447 |
+ local a = {}; for i = 1, lim do a[i] = "01234567890123456789" end |
1448 |
+ testamem("auxiliary buffer", function () |
1449 |
+ return (#table.concat(a, ",") == 20*lim + lim - 1) |
1450 |
+ end) |
1451 |
+end |
1452 |
+ |
1453 |
+print'+' |
1454 |
+ |
1455 |
+-- testing some auxlib functions |
1456 |
+local function gsub (a, b, c) |
1457 |
+ a, b = T.testC("gsub 2 3 4; gettop; return 2", a, b, c) |
1458 |
+ assert(b == 5) |
1459 |
+ return a |
1460 |
+end |
1461 |
+ |
1462 |
+assert(gsub("alo.alo.uhuh.", ".", "//") == "alo//alo//uhuh//") |
1463 |
+assert(gsub("alo.alo.uhuh.", "alo", "//") == "//.//.uhuh.") |
1464 |
+assert(gsub("", "alo", "//") == "") |
1465 |
+assert(gsub("...", ".", "/.") == "/././.") |
1466 |
+assert(gsub("...", "...", "") == "") |
1467 |
+ |
1468 |
+ |
1469 |
+-- testing luaL_newmetatable |
1470 |
+local mt_xuxu, res, top = T.testC("newmetatable xuxu; gettop; return 3") |
1471 |
+assert(type(mt_xuxu) == "table" and res and top == 3) |
1472 |
+local d, res, top = T.testC("newmetatable xuxu; gettop; return 3") |
1473 |
+assert(mt_xuxu == d and not res and top == 3) |
1474 |
+d, res, top = T.testC("newmetatable xuxu1; gettop; return 3") |
1475 |
+assert(mt_xuxu ~= d and res and top == 3) |
1476 |
+ |
1477 |
+x = T.newuserdata(0); |
1478 |
+y = T.newuserdata(0); |
1479 |
+T.testC("pushstring xuxu; gettable R; setmetatable 2", x) |
1480 |
+assert(getmetatable(x) == mt_xuxu) |
1481 |
+ |
1482 |
+-- testing luaL_testudata |
1483 |
+-- correct metatable |
1484 |
+local res1, res2, top = T.testC([[testudata -1 xuxu |
1485 |
+ testudata 2 xuxu |
1486 |
+ gettop |
1487 |
+ return 3]], x) |
1488 |
+assert(res1 and res2 and top == 4) |
1489 |
+ |
1490 |
+-- wrong metatable |
1491 |
+res1, res2, top = T.testC([[testudata -1 xuxu1 |
1492 |
+ testudata 2 xuxu1 |
1493 |
+ gettop |
1494 |
+ return 3]], x) |
1495 |
+assert(not res1 and not res2 and top == 4) |
1496 |
+ |
1497 |
+-- non-existent type |
1498 |
+res1, res2, top = T.testC([[testudata -1 xuxu2 |
1499 |
+ testudata 2 xuxu2 |
1500 |
+ gettop |
1501 |
+ return 3]], x) |
1502 |
+assert(not res1 and not res2 and top == 4) |
1503 |
+ |
1504 |
+-- userdata has no metatable |
1505 |
+res1, res2, top = T.testC([[testudata -1 xuxu |
1506 |
+ testudata 2 xuxu |
1507 |
+ gettop |
1508 |
+ return 3]], y) |
1509 |
+assert(not res1 and not res2 and top == 4) |
1510 |
+ |
1511 |
+-- erase metatables |
1512 |
+do |
1513 |
+ local r = debug.getregistry() |
1514 |
+ assert(r.xuxu == mt_xuxu and r.xuxu1 == d) |
1515 |
+ r.xuxu = nil; r.xuxu1 = nil |
1516 |
+end |
1517 |
+ |
1518 |
+print'OK' |
1519 |
+ |
1520 |
|
1521 |
diff --git a/tests/attrib.lua b/tests/attrib.lua |
1522 |
new file mode 100644 |
1523 |
index 0000000..993a96c |
1524 |
--- /dev/null |
1525 |
+++ b/tests/attrib.lua |
1526 |
@@ -0,0 +1,470 @@ |
1527 |
+-- $Id: attrib.lua,v 1.65 2016/11/07 13:11:28 roberto Exp $ |
1528 |
+-- See Copyright Notice in file all.lua |
1529 |
+ |
1530 |
+print "testing require" |
1531 |
+ |
1532 |
+assert(require"string" == string) |
1533 |
+assert(require"math" == math) |
1534 |
+assert(require"table" == table) |
1535 |
+assert(require"io" == io) |
1536 |
+assert(require"os" == os) |
1537 |
+assert(require"coroutine" == coroutine) |
1538 |
+ |
1539 |
+assert(type(package.path) == "string") |
1540 |
+assert(type(package.cpath) == "string") |
1541 |
+assert(type(package.loaded) == "table") |
1542 |
+assert(type(package.preload) == "table") |
1543 |
+ |
1544 |
+assert(type(package.config) == "string") |
1545 |
+print("package config: "..string.gsub(package.config, "\n", "|")) |
1546 |
+ |
1547 |
+do |
1548 |
+ -- create a path with 'max' templates, |
1549 |
+ -- each with 1-10 repetitions of '?' |
1550 |
+ local max = _soft and 100 or 2000 |
1551 |
+ local t = {} |
1552 |
+ for i = 1,max do t[i] = string.rep("?", i%10 + 1) end |
1553 |
+ t[#t + 1] = ";" -- empty template |
1554 |
+ local path = table.concat(t, ";") |
1555 |
+ -- use that path in a search |
1556 |
+ local s, err = package.searchpath("xuxu", path) |
1557 |
+ -- search fails; check that message has an occurence of |
1558 |
+ -- '??????????' with ? replaced by xuxu and at least 'max' lines |
1559 |
+ assert(not s and |
1560 |
+ string.find(err, string.rep("xuxu", 10)) and |
1561 |
+ #string.gsub(err, "[^\n]", "") >= max) |
1562 |
+ -- path with one very long template |
1563 |
+ local path = string.rep("?", max) |
1564 |
+ local s, err = package.searchpath("xuxu", path) |
1565 |
+ assert(not s and string.find(err, string.rep('xuxu', max))) |
1566 |
+end |
1567 |
+ |
1568 |
+do |
1569 |
+ local oldpath = package.path |
1570 |
+ package.path = {} |
1571 |
+ local s, err = pcall(require, "no-such-file") |
1572 |
+ assert(not s and string.find(err, "package.path")) |
1573 |
+ package.path = oldpath |
1574 |
+end |
1575 |
+ |
1576 |
+print('+') |
1577 |
+ |
1578 |
+ |
1579 |
+-- The next tests for 'require' assume some specific directories and |
1580 |
+-- libraries. |
1581 |
+ |
1582 |
+if not _port then --[ |
1583 |
+ |
1584 |
+local dirsep = string.match(package.config, "^([^\n]+)\n") |
1585 |
+ |
1586 |
+-- auxiliary directory with C modules and temporary files |
1587 |
+local DIR = "libs" .. dirsep |
1588 |
+ |
1589 |
+-- prepend DIR to a name and correct directory separators |
1590 |
+local function D (x) |
1591 |
+ x = string.gsub(x, "/", dirsep) |
1592 |
+ return DIR .. x |
1593 |
+end |
1594 |
+ |
1595 |
+-- prepend DIR and pospend proper C lib. extension to a name |
1596 |
+local function DC (x) |
1597 |
+ local ext = (dirsep == '\\') and ".dll" or ".so" |
1598 |
+ return D(x .. ext) |
1599 |
+end |
1600 |
+ |
1601 |
+ |
1602 |
+local function createfiles (files, preextras, posextras) |
1603 |
+ for n,c in pairs(files) do |
1604 |
+ io.output(D(n)) |
1605 |
+ io.write(string.format(preextras, n)) |
1606 |
+ io.write(c) |
1607 |
+ io.write(string.format(posextras, n)) |
1608 |
+ io.close(io.output()) |
1609 |
+ end |
1610 |
+end |
1611 |
+ |
1612 |
+function removefiles (files) |
1613 |
+ for n in pairs(files) do |
1614 |
+ os.remove(D(n)) |
1615 |
+ end |
1616 |
+end |
1617 |
+ |
1618 |
+local files = { |
1619 |
+ ["names.lua"] = "do return {...} end\n", |
1620 |
+ ["err.lua"] = "B = 15; a = a + 1;", |
1621 |
+ ["synerr.lua"] = "B =", |
1622 |
+ ["A.lua"] = "", |
1623 |
+ ["B.lua"] = "assert(...=='B');require 'A'", |
1624 |
+ ["A.lc"] = "", |
1625 |
+ ["A"] = "", |
1626 |
+ ["L"] = "", |
1627 |
+ ["XXxX"] = "", |
1628 |
+ ["C.lua"] = "package.loaded[...] = 25; require'C'", |
1629 |
+} |
1630 |
+ |
1631 |
+AA = nil |
1632 |
+local extras = [[ |
1633 |
+NAME = '%s' |
1634 |
+REQUIRED = ... |
1635 |
+return AA]] |
1636 |
+ |
1637 |
+createfiles(files, "", extras) |
1638 |
+ |
1639 |
+-- testing explicit "dir" separator in 'searchpath' |
1640 |
+assert(package.searchpath("C.lua", D"?", "", "") == D"C.lua") |
1641 |
+assert(package.searchpath("C.lua", D"?", ".", ".") == D"C.lua") |
1642 |
+assert(package.searchpath("--x-", D"?", "-", "X") == D"XXxX") |
1643 |
+assert(package.searchpath("---xX", D"?", "---", "XX") == D"XXxX") |
1644 |
+assert(package.searchpath(D"C.lua", "?", dirsep) == D"C.lua") |
1645 |
+assert(package.searchpath(".\\C.lua", D"?", "\\") == D"./C.lua") |
1646 |
+ |
1647 |
+local oldpath = package.path |
1648 |
+ |
1649 |
+package.path = string.gsub("D/?.lua;D/?.lc;D/?;D/??x?;D/L", "D/", DIR) |
1650 |
+ |
1651 |
+local try = function (p, n, r) |
1652 |
+ NAME = nil |
1653 |
+ local rr = require(p) |
1654 |
+ assert(NAME == n) |
1655 |
+ assert(REQUIRED == p) |
1656 |
+ assert(rr == r) |
1657 |
+end |
1658 |
+ |
1659 |
+a = require"names" |
1660 |
+assert(a[1] == "names" and a[2] == D"names.lua") |
1661 |
+ |
1662 |
+_G.a = nil |
1663 |
+local st, msg = pcall(require, "err") |
1664 |
+assert(not st and string.find(msg, "arithmetic") and B == 15) |
1665 |
+st, msg = pcall(require, "synerr") |
1666 |
+assert(not st and string.find(msg, "error loading module")) |
1667 |
+ |
1668 |
+assert(package.searchpath("C", package.path) == D"C.lua") |
1669 |
+assert(require"C" == 25) |
1670 |
+assert(require"C" == 25) |
1671 |
+AA = nil |
1672 |
+try('B', 'B.lua', true) |
1673 |
+assert(package.loaded.B) |
1674 |
+assert(require"B" == true) |
1675 |
+assert(package.loaded.A) |
1676 |
+assert(require"C" == 25) |
1677 |
+package.loaded.A = nil |
1678 |
+try('B', nil, true) -- should not reload package |
1679 |
+try('A', 'A.lua', true) |
1680 |
+package.loaded.A = nil |
1681 |
+os.remove(D'A.lua') |
1682 |
+AA = {} |
1683 |
+try('A', 'A.lc', AA) -- now must find second option |
1684 |
+assert(package.searchpath("A", package.path) == D"A.lc") |
1685 |
+assert(require("A") == AA) |
1686 |
+AA = false |
1687 |
+try('K', 'L', false) -- default option |
1688 |
+try('K', 'L', false) -- default option (should reload it) |
1689 |
+assert(rawget(_G, "_REQUIREDNAME") == nil) |
1690 |
+ |
1691 |
+AA = "x" |
1692 |
+try("X", "XXxX", AA) |
1693 |
+ |
1694 |
+ |
1695 |
+removefiles(files) |
1696 |
+ |
1697 |
+ |
1698 |
+-- testing require of sub-packages |
1699 |
+ |
1700 |
+local _G = _G |
1701 |
+ |
1702 |
+package.path = string.gsub("D/?.lua;D/?/init.lua", "D/", DIR) |
1703 |
+ |
1704 |
+files = { |
1705 |
+ ["P1/init.lua"] = "AA = 10", |
1706 |
+ ["P1/xuxu.lua"] = "AA = 20", |
1707 |
+} |
1708 |
+ |
1709 |
+createfiles(files, "_ENV = {}\n", "\nreturn _ENV\n") |
1710 |
+AA = 0 |
1711 |
+ |
1712 |
+local m = assert(require"P1") |
1713 |
+assert(AA == 0 and m.AA == 10) |
1714 |
+assert(require"P1" == m) |
1715 |
+assert(require"P1" == m) |
1716 |
+ |
1717 |
+assert(package.searchpath("P1.xuxu", package.path) == D"P1/xuxu.lua") |
1718 |
+m.xuxu = assert(require"P1.xuxu") |
1719 |
+assert(AA == 0 and m.xuxu.AA == 20) |
1720 |
+assert(require"P1.xuxu" == m.xuxu) |
1721 |
+assert(require"P1.xuxu" == m.xuxu) |
1722 |
+assert(require"P1" == m and m.AA == 10) |
1723 |
+ |
1724 |
+ |
1725 |
+removefiles(files) |
1726 |
+ |
1727 |
+ |
1728 |
+package.path = "" |
1729 |
+assert(not pcall(require, "file_does_not_exist")) |
1730 |
+package.path = "??\0?" |
1731 |
+assert(not pcall(require, "file_does_not_exist1")) |
1732 |
+ |
1733 |
+package.path = oldpath |
1734 |
+ |
1735 |
+-- check 'require' error message |
1736 |
+local fname = "file_does_not_exist2" |
1737 |
+local m, err = pcall(require, fname) |
1738 |
+for t in string.gmatch(package.path..";"..package.cpath, "[^;]+") do |
1739 |
+ t = string.gsub(t, "?", fname) |
1740 |
+ assert(string.find(err, t, 1, true)) |
1741 |
+end |
1742 |
+ |
1743 |
+do -- testing 'package.searchers' not being a table |
1744 |
+ local searchers = package.searchers |
1745 |
+ package.searchers = 3 |
1746 |
+ local st, msg = pcall(require, 'a') |
1747 |
+ assert(not st and string.find(msg, "must be a table")) |
1748 |
+ package.searchers = searchers |
1749 |
+end |
1750 |
+ |
1751 |
+local function import(...) |
1752 |
+ local f = {...} |
1753 |
+ return function (m) |
1754 |
+ for i=1, #f do m[f[i]] = _G[f[i]] end |
1755 |
+ end |
1756 |
+end |
1757 |
+ |
1758 |
+-- cannot change environment of a C function |
1759 |
+assert(not pcall(module, 'XUXU')) |
1760 |
+ |
1761 |
+ |
1762 |
+ |
1763 |
+-- testing require of C libraries |
1764 |
+ |
1765 |
+ |
1766 |
+local p = "" -- On Mac OS X, redefine this to "_" |
1767 |
+ |
1768 |
+-- check whether loadlib works in this system |
1769 |
+local st, err, when = package.loadlib(DC"lib1", "*") |
1770 |
+if not st then |
1771 |
+ local f, err, when = package.loadlib("donotexist", p.."xuxu") |
1772 |
+ assert(not f and type(err) == "string" and when == "absent") |
1773 |
+ ;(Message or print)('\n >>> cannot load dynamic library <<<\n') |
1774 |
+ print(err, when) |
1775 |
+else |
1776 |
+ -- tests for loadlib |
1777 |
+ local f = assert(package.loadlib(DC"lib1", p.."onefunction")) |
1778 |
+ local a, b = f(15, 25) |
1779 |
+ assert(a == 25 and b == 15) |
1780 |
+ |
1781 |
+ f = assert(package.loadlib(DC"lib1", p.."anotherfunc")) |
1782 |
+ assert(f(10, 20) == "10%20\n") |
1783 |
+ |
1784 |
+ -- check error messages |
1785 |
+ local f, err, when = package.loadlib(DC"lib1", p.."xuxu") |
1786 |
+ assert(not f and type(err) == "string" and when == "init") |
1787 |
+ f, err, when = package.loadlib("donotexist", p.."xuxu") |
1788 |
+ assert(not f and type(err) == "string" and when == "open") |
1789 |
+ |
1790 |
+ -- symbols from 'lib1' must be visible to other libraries |
1791 |
+ f = assert(package.loadlib(DC"lib11", p.."luaopen_lib11")) |
1792 |
+ assert(f() == "exported") |
1793 |
+ |
1794 |
+ -- test C modules with prefixes in names |
1795 |
+ package.cpath = DC"?" |
1796 |
+ local lib2 = require"lib2-v2" |
1797 |
+ -- check correct access to global environment and correct |
1798 |
+ -- parameters |
1799 |
+ assert(_ENV.x == "lib2-v2" and _ENV.y == DC"lib2-v2") |
1800 |
+ assert(lib2.id("x") == "x") |
1801 |
+ |
1802 |
+ -- test C submodules |
1803 |
+ local fs = require"lib1.sub" |
1804 |
+ assert(_ENV.x == "lib1.sub" and _ENV.y == DC"lib1") |
1805 |
+ assert(fs.id(45) == 45) |
1806 |
+end |
1807 |
+ |
1808 |
+_ENV = _G |
1809 |
+ |
1810 |
+ |
1811 |
+-- testing preload |
1812 |
+ |
1813 |
+do |
1814 |
+ local p = package |
1815 |
+ package = {} |
1816 |
+ p.preload.pl = function (...) |
1817 |
+ local _ENV = {...} |
1818 |
+ function xuxu (x) return x+20 end |
1819 |
+ return _ENV |
1820 |
+ end |
1821 |
+ |
1822 |
+ local pl = require"pl" |
1823 |
+ assert(require"pl" == pl) |
1824 |
+ assert(pl.xuxu(10) == 30) |
1825 |
+ assert(pl[1] == "pl" and pl[2] == nil) |
1826 |
+ |
1827 |
+ package = p |
1828 |
+ assert(type(package.path) == "string") |
1829 |
+end |
1830 |
+ |
1831 |
+print('+') |
1832 |
+ |
1833 |
+end --] |
1834 |
+ |
1835 |
+print("testing assignments, logical operators, and constructors") |
1836 |
+ |
1837 |
+local res, res2 = 27 |
1838 |
+ |
1839 |
+a, b = 1, 2+3 |
1840 |
+assert(a==1 and b==5) |
1841 |
+a={} |
1842 |
+function f() return 10, 11, 12 end |
1843 |
+a.x, b, a[1] = 1, 2, f() |
1844 |
+assert(a.x==1 and b==2 and a[1]==10) |
1845 |
+a[f()], b, a[f()+3] = f(), a, 'x' |
1846 |
+assert(a[10] == 10 and b == a and a[13] == 'x') |
1847 |
+ |
1848 |
+do |
1849 |
+ local f = function (n) local x = {}; for i=1,n do x[i]=i end; |
1850 |
+ return table.unpack(x) end; |
1851 |
+ local a,b,c |
1852 |
+ a,b = 0, f(1) |
1853 |
+ assert(a == 0 and b == 1) |
1854 |
+ A,b = 0, f(1) |
1855 |
+ assert(A == 0 and b == 1) |
1856 |
+ a,b,c = 0,5,f(4) |
1857 |
+ assert(a==0 and b==5 and c==1) |
1858 |
+ a,b,c = 0,5,f(0) |
1859 |
+ assert(a==0 and b==5 and c==nil) |
1860 |
+end |
1861 |
+ |
1862 |
+a, b, c, d = 1 and nil, 1 or nil, (1 and (nil or 1)), 6 |
1863 |
+assert(not a and b and c and d==6) |
1864 |
+ |
1865 |
+d = 20 |
1866 |
+a, b, c, d = f() |
1867 |
+assert(a==10 and b==11 and c==12 and d==nil) |
1868 |
+a,b = f(), 1, 2, 3, f() |
1869 |
+assert(a==10 and b==1) |
1870 |
+ |
1871 |
+assert(a<b == false and a>b == true) |
1872 |
+assert((10 and 2) == 2) |
1873 |
+assert((10 or 2) == 10) |
1874 |
+assert((10 or assert(nil)) == 10) |
1875 |
+assert(not (nil and assert(nil))) |
1876 |
+assert((nil or "alo") == "alo") |
1877 |
+assert((nil and 10) == nil) |
1878 |
+assert((false and 10) == false) |
1879 |
+assert((true or 10) == true) |
1880 |
+assert((false or 10) == 10) |
1881 |
+assert(false ~= nil) |
1882 |
+assert(nil ~= false) |
1883 |
+assert(not nil == true) |
1884 |
+assert(not not nil == false) |
1885 |
+assert(not not 1 == true) |
1886 |
+assert(not not a == true) |
1887 |
+assert(not not (6 or nil) == true) |
1888 |
+assert(not not (nil and 56) == false) |
1889 |
+assert(not not (nil and true) == false) |
1890 |
+assert(not 10 == false) |
1891 |
+assert(not {} == false) |
1892 |
+assert(not 0.5 == false) |
1893 |
+assert(not "x" == false) |
1894 |
+ |
1895 |
+assert({} ~= {}) |
1896 |
+print('+') |
1897 |
+ |
1898 |
+a = {} |
1899 |
+a[true] = 20 |
1900 |
+a[false] = 10 |
1901 |
+assert(a[1<2] == 20 and a[1>2] == 10) |
1902 |
+ |
1903 |
+function f(a) return a end |
1904 |
+ |
1905 |
+local a = {} |
1906 |
+for i=3000,-3000,-1 do a[i + 0.0] = i; end |
1907 |
+a[10e30] = "alo"; a[true] = 10; a[false] = 20 |
1908 |
+assert(a[10e30] == 'alo' and a[not 1] == 20 and a[10<20] == 10) |
1909 |
+for i=3000,-3000,-1 do assert(a[i] == i); end |
1910 |
+a[print] = assert |
1911 |
+a[f] = print |
1912 |
+a[a] = a |
1913 |
+assert(a[a][a][a][a][print] == assert) |
1914 |
+a[print](a[a[f]] == a[print]) |
1915 |
+assert(not pcall(function () local a = {}; a[nil] = 10 end)) |
1916 |
+assert(not pcall(function () local a = {[nil] = 10} end)) |
1917 |
+assert(a[nil] == nil) |
1918 |
+a = nil |
1919 |
+ |
1920 |
+a = {10,9,8,7,6,5,4,3,2; [-3]='a', [f]=print, a='a', b='ab'} |
1921 |
+a, a.x, a.y = a, a[-3] |
1922 |
+assert(a[1]==10 and a[-3]==a.a and a[f]==print and a.x=='a' and not a.y) |
1923 |
+a[1], f(a)[2], b, c = {['alo']=assert}, 10, a[1], a[f], 6, 10, 23, f(a), 2 |
1924 |
+a[1].alo(a[2]==10 and b==10 and c==print) |
1925 |
+ |
1926 |
+ |
1927 |
+-- test of large float/integer indices |
1928 |
+ |
1929 |
+-- compute maximum integer where all bits fit in a float |
1930 |
+local maxint = math.maxinteger |
1931 |
+ |
1932 |
+while maxint - 1.0 == maxint - 0.0 do -- trim (if needed) to fit in a float |
1933 |
+ maxint = maxint // 2 |
1934 |
+end |
1935 |
+ |
1936 |
+maxintF = maxint + 0.0 -- float version |
1937 |
+ |
1938 |
+assert(math.type(maxintF) == "float" and maxintF >= 2.0^14) |
1939 |
+ |
1940 |
+-- floats and integers must index the same places |
1941 |
+a[maxintF] = 10; a[maxintF - 1.0] = 11; |
1942 |
+a[-maxintF] = 12; a[-maxintF + 1.0] = 13; |
1943 |
+ |
1944 |
+assert(a[maxint] == 10 and a[maxint - 1] == 11 and |
1945 |
+ a[-maxint] == 12 and a[-maxint + 1] == 13) |
1946 |
+ |
1947 |
+a[maxint] = 20 |
1948 |
+a[-maxint] = 22 |
1949 |
+ |
1950 |
+assert(a[maxintF] == 20 and a[maxintF - 1.0] == 11 and |
1951 |
+ a[-maxintF] == 22 and a[-maxintF + 1.0] == 13) |
1952 |
+ |
1953 |
+a = nil |
1954 |
+ |
1955 |
+ |
1956 |
+-- test conflicts in multiple assignment |
1957 |
+do |
1958 |
+ local a,i,j,b |
1959 |
+ a = {'a', 'b'}; i=1; j=2; b=a |
1960 |
+ i, a[i], a, j, a[j], a[i+j] = j, i, i, b, j, i |
1961 |
+ assert(i == 2 and b[1] == 1 and a == 1 and j == b and b[2] == 2 and |
1962 |
+ b[3] == 1) |
1963 |
+end |
1964 |
+ |
1965 |
+-- repeat test with upvalues |
1966 |
+do |
1967 |
+ local a,i,j,b |
1968 |
+ a = {'a', 'b'}; i=1; j=2; b=a |
1969 |
+ local function foo () |
1970 |
+ i, a[i], a, j, a[j], a[i+j] = j, i, i, b, j, i |
1971 |
+ end |
1972 |
+ foo() |
1973 |
+ assert(i == 2 and b[1] == 1 and a == 1 and j == b and b[2] == 2 and |
1974 |
+ b[3] == 1) |
1975 |
+ local t = {} |
1976 |
+ (function (a) t[a], a = 10, 20 end)(1); |
1977 |
+ assert(t[1] == 10) |
1978 |
+end |
1979 |
+ |
1980 |
+-- bug in 5.2 beta |
1981 |
+local function foo () |
1982 |
+ local a |
1983 |
+ return function () |
1984 |
+ local b |
1985 |
+ a, b = 3, 14 -- local and upvalue have same index |
1986 |
+ return a, b |
1987 |
+ end |
1988 |
+end |
1989 |
+ |
1990 |
+local a, b = foo()() |
1991 |
+assert(a == 3 and b == 14) |
1992 |
+ |
1993 |
+print('OK') |
1994 |
+ |
1995 |
+return res |
1996 |
+ |
1997 |
|
1998 |
diff --git a/tests/big.lua b/tests/big.lua |
1999 |
new file mode 100644 |
2000 |
index 0000000..1a1fa78 |
2001 |
--- /dev/null |
2002 |
+++ b/tests/big.lua |
2003 |
@@ -0,0 +1,82 @@ |
2004 |
+-- $Id: big.lua,v 1.32 2016/11/07 13:11:28 roberto Exp $ |
2005 |
+-- See Copyright Notice in file all.lua |
2006 |
+ |
2007 |
+if _soft then |
2008 |
+ return 'a' |
2009 |
+end |
2010 |
+ |
2011 |
+print "testing large tables" |
2012 |
+ |
2013 |
+local debug = require"debug" |
2014 |
+ |
2015 |
+local lim = 2^18 + 1000 |
2016 |
+local prog = { "local y = {0" } |
2017 |
+for i = 1, lim do prog[#prog + 1] = i end |
2018 |
+prog[#prog + 1] = "}\n" |
2019 |
+prog[#prog + 1] = "X = y\n" |
2020 |
+prog[#prog + 1] = ("assert(X[%d] == %d)"):format(lim - 1, lim - 2) |
2021 |
+prog[#prog + 1] = "return 0" |
2022 |
+prog = table.concat(prog, ";") |
2023 |
+ |
2024 |
+local env = {string = string, assert = assert} |
2025 |
+local f = assert(load(prog, nil, nil, env)) |
2026 |
+ |
2027 |
+f() |
2028 |
+assert(env.X[lim] == lim - 1 and env.X[lim + 1] == lim) |
2029 |
+for k in pairs(env) do env[k] = nil end |
2030 |
+ |
2031 |
+-- yields during accesses larger than K (in RK) |
2032 |
+setmetatable(env, { |
2033 |
+ __index = function (t, n) coroutine.yield('g'); return _G[n] end, |
2034 |
+ __newindex = function (t, n, v) coroutine.yield('s'); _G[n] = v end, |
2035 |
+}) |
2036 |
+ |
2037 |
+X = nil |
2038 |
+co = coroutine.wrap(f) |
2039 |
+assert(co() == 's') |
2040 |
+assert(co() == 'g') |
2041 |
+assert(co() == 'g') |
2042 |
+assert(co() == 0) |
2043 |
+ |
2044 |
+assert(X[lim] == lim - 1 and X[lim + 1] == lim) |
2045 |
+ |
2046 |
+-- errors in accesses larger than K (in RK) |
2047 |
+getmetatable(env).__index = function () end |
2048 |
+getmetatable(env).__newindex = function () end |
2049 |
+local e, m = pcall(f) |
2050 |
+assert(not e and m:find("global 'X'")) |
2051 |
+ |
2052 |
+-- errors in metamethods |
2053 |
+getmetatable(env).__newindex = function () error("hi") end |
2054 |
+local e, m = xpcall(f, debug.traceback) |
2055 |
+assert(not e and m:find("'__newindex'")) |
2056 |
+ |
2057 |
+f, X = nil |
2058 |
+ |
2059 |
+coroutine.yield'b' |
2060 |
+ |
2061 |
+if 2^32 == 0 then -- (small integers) { |
2062 |
+ |
2063 |
+print "testing string length overflow" |
2064 |
+ |
2065 |
+local repstrings = 192 -- number of strings to be concatenated |
2066 |
+local ssize = math.ceil(2.0^32 / repstrings) + 1 -- size of each string |
2067 |
+ |
2068 |
+assert(repstrings * ssize > 2.0^32) -- it should be larger than maximum size |
2069 |
+ |
2070 |
+local longs = string.rep("\0", ssize) -- create one long string |
2071 |
+ |
2072 |
+-- create function to concatentate 'repstrings' copies of its argument |
2073 |
+local rep = assert(load( |
2074 |
+ "local a = ...; return " .. string.rep("a", repstrings, ".."))) |
2075 |
+ |
2076 |
+local a, b = pcall(rep, longs) -- call that function |
2077 |
+ |
2078 |
+-- it should fail without creating string (result would be too large) |
2079 |
+assert(not a and string.find(b, "overflow")) |
2080 |
+ |
2081 |
+end -- } |
2082 |
+ |
2083 |
+print'OK' |
2084 |
+ |
2085 |
+return 'a' |
2086 |
|
2087 |
diff --git a/tests/bitwise.lua b/tests/bitwise.lua |
2088 |
new file mode 100755 |
2089 |
index 0000000..786679f |
2090 |
--- /dev/null |
2091 |
+++ b/tests/bitwise.lua |
2092 |
@@ -0,0 +1,328 @@ |
2093 |
+-- $Id: bitwise.lua,v 1.26 2016/11/07 13:11:28 roberto Exp $ |
2094 |
+-- See Copyright Notice in file all.lua |
2095 |
+ |
2096 |
+print("testing bitwise operations") |
2097 |
+ |
2098 |
+local numbits = string.packsize('j') * 8 |
2099 |
+ |
2100 |
+assert(~0 == -1) |
2101 |
+ |
2102 |
+assert((1 << (numbits - 1)) == math.mininteger) |
2103 |
+ |
2104 |
+-- basic tests for bitwise operators; |
2105 |
+-- use variables to avoid constant folding |
2106 |
+local a, b, c, d |
2107 |
+a = 0xFFFFFFFFFFFFFFFF |
2108 |
+assert(a == -1 and a & -1 == a and a & 35 == 35) |
2109 |
+a = 0xF0F0F0F0F0F0F0F0 |
2110 |
+assert(a | -1 == -1) |
2111 |
+assert(a ~ a == 0 and a ~ 0 == a and a ~ ~a == -1) |
2112 |
+assert(a >> 4 == ~a) |
2113 |
+a = 0xF0; b = 0xCC; c = 0xAA; d = 0xFD |
2114 |
+assert(a | b ~ c & d == 0xF4) |
2115 |
+ |
2116 |
+a = 0xF0.0; b = 0xCC.0; c = "0xAA.0"; d = "0xFD.0" |
2117 |
+assert(a | b ~ c & d == 0xF4) |
2118 |
+ |
2119 |
+a = 0xF0000000; b = 0xCC000000; |
2120 |
+c = 0xAA000000; d = 0xFD000000 |
2121 |
+assert(a | b ~ c & d == 0xF4000000) |
2122 |
+assert(~~a == a and ~a == -1 ~ a and -d == ~d + 1) |
2123 |
+ |
2124 |
+a = a << 32 |
2125 |
+b = b << 32 |
2126 |
+c = c << 32 |
2127 |
+d = d << 32 |
2128 |
+assert(a | b ~ c & d == 0xF4000000 << 32) |
2129 |
+assert(~~a == a and ~a == -1 ~ a and -d == ~d + 1) |
2130 |
+ |
2131 |
+assert(-1 >> 1 == (1 << (numbits - 1)) - 1 and 1 << 31 == 0x80000000) |
2132 |
+assert(-1 >> (numbits - 1) == 1) |
2133 |
+assert(-1 >> numbits == 0 and |
2134 |
+ -1 >> -numbits == 0 and |
2135 |
+ -1 << numbits == 0 and |
2136 |
+ -1 << -numbits == 0) |
2137 |
+ |
2138 |
+assert((2^30 - 1) << 2^30 == 0) |
2139 |
+assert((2^30 - 1) >> 2^30 == 0) |
2140 |
+ |
2141 |
+assert(1 >> -3 == 1 << 3 and 1000 >> 5 == 1000 << -5) |
2142 |
+ |
2143 |
+ |
2144 |
+-- coercion from strings to integers |
2145 |
+assert("0xffffffffffffffff" | 0 == -1) |
2146 |
+assert("0xfffffffffffffffe" & "-1" == -2) |
2147 |
+assert(" \t-0xfffffffffffffffe\n\t" & "-1" == 2) |
2148 |
+assert(" \n -45 \t " >> " -2 " == -45 * 4) |
2149 |
+ |
2150 |
+-- out of range number |
2151 |
+assert(not pcall(function () return "0xffffffffffffffff.0" | 0 end)) |
2152 |
+ |
2153 |
+-- embedded zeros |
2154 |
+assert(not pcall(function () return "0xffffffffffffffff\0" | 0 end)) |
2155 |
+ |
2156 |
+print'+' |
2157 |
+ |
2158 |
+ |
2159 |
+package.preload.bit32 = function () --{ |
2160 |
+ |
2161 |
+-- no built-in 'bit32' library: implement it using bitwise operators |
2162 |
+ |
2163 |
+local bit = {} |
2164 |
+ |
2165 |
+function bit.bnot (a) |
2166 |
+ return ~a & 0xFFFFFFFF |
2167 |
+end |
2168 |
+ |
2169 |
+ |
2170 |
+-- |
2171 |
+-- in all vararg functions, avoid creating 'arg' table when there are |
2172 |
+-- only 2 (or less) parameters, as 2 parameters is the common case |
2173 |
+-- |
2174 |
+ |
2175 |
+function bit.band (x, y, z, ...) |
2176 |
+ if not z then |
2177 |
+ return ((x or -1) & (y or -1)) & 0xFFFFFFFF |
2178 |
+ else |
2179 |
+ local arg = {...} |
2180 |
+ local res = x & y & z |
2181 |
+ for i = 1, #arg do res = res & arg[i] end |
2182 |
+ return res & 0xFFFFFFFF |
2183 |
+ end |
2184 |
+end |
2185 |
+ |
2186 |
+function bit.bor (x, y, z, ...) |
2187 |
+ if not z then |
2188 |
+ return ((x or 0) | (y or 0)) & 0xFFFFFFFF |
2189 |
+ else |
2190 |
+ local arg = {...} |
2191 |
+ local res = x | y | z |
2192 |
+ for i = 1, #arg do res = res | arg[i] end |
2193 |
+ return res & 0xFFFFFFFF |
2194 |
+ end |
2195 |
+end |
2196 |
+ |
2197 |
+function bit.bxor (x, y, z, ...) |
2198 |
+ if not z then |
2199 |
+ return ((x or 0) ~ (y or 0)) & 0xFFFFFFFF |
2200 |
+ else |
2201 |
+ local arg = {...} |
2202 |
+ local res = x ~ y ~ z |
2203 |
+ for i = 1, #arg do res = res ~ arg[i] end |
2204 |
+ return res & 0xFFFFFFFF |
2205 |
+ end |
2206 |
+end |
2207 |
+ |
2208 |
+function bit.btest (...) |
2209 |
+ return bit.band(...) ~= 0 |
2210 |
+end |
2211 |
+ |
2212 |
+function bit.lshift (a, b) |
2213 |
+ return ((a & 0xFFFFFFFF) << b) & 0xFFFFFFFF |
2214 |
+end |
2215 |
+ |
2216 |
+function bit.rshift (a, b) |
2217 |
+ return ((a & 0xFFFFFFFF) >> b) & 0xFFFFFFFF |
2218 |
+end |
2219 |
+ |
2220 |
+function bit.arshift (a, b) |
2221 |
+ a = a & 0xFFFFFFFF |
2222 |
+ if b <= 0 or (a & 0x80000000) == 0 then |
2223 |
+ return (a >> b) & 0xFFFFFFFF |
2224 |
+ else |
2225 |
+ return ((a >> b) | ~(0xFFFFFFFF >> b)) & 0xFFFFFFFF |
2226 |
+ end |
2227 |
+end |
2228 |
+ |
2229 |
+function bit.lrotate (a ,b) |
2230 |
+ b = b & 31 |
2231 |
+ a = a & 0xFFFFFFFF |
2232 |
+ a = (a << b) | (a >> (32 - b)) |
2233 |
+ return a & 0xFFFFFFFF |
2234 |
+end |
2235 |
+ |
2236 |
+function bit.rrotate (a, b) |
2237 |
+ return bit.lrotate(a, -b) |
2238 |
+end |
2239 |
+ |
2240 |
+local function checkfield (f, w) |
2241 |
+ w = w or 1 |
2242 |
+ assert(f >= 0, "field cannot be negative") |
2243 |
+ assert(w > 0, "width must be positive") |
2244 |
+ assert(f + w <= 32, "trying to access non-existent bits") |
2245 |
+ return f, ~(-1 << w) |
2246 |
+end |
2247 |
+ |
2248 |
+function bit.extract (a, f, w) |
2249 |
+ local f, mask = checkfield(f, w) |
2250 |
+ return (a >> f) & mask |
2251 |
+end |
2252 |
+ |
2253 |
+function bit.replace (a, v, f, w) |
2254 |
+ local f, mask = checkfield(f, w) |
2255 |
+ v = v & mask |
2256 |
+ a = (a & ~(mask << f)) | (v << f) |
2257 |
+ return a & 0xFFFFFFFF |
2258 |
+end |
2259 |
+ |
2260 |
+return bit |
2261 |
+ |
2262 |
+end --} |
2263 |
+ |
2264 |
+ |
2265 |
+print("testing bitwise library") |
2266 |
+ |
2267 |
+local bit32 = require'bit32' |
2268 |
+ |
2269 |
+assert(bit32.band() == bit32.bnot(0)) |
2270 |
+assert(bit32.btest() == true) |
2271 |
+assert(bit32.bor() == 0) |
2272 |
+assert(bit32.bxor() == 0) |
2273 |
+ |
2274 |
+assert(bit32.band() == bit32.band(0xffffffff)) |
2275 |
+assert(bit32.band(1,2) == 0) |
2276 |
+ |
2277 |
+ |
2278 |
+-- out-of-range numbers |
2279 |
+assert(bit32.band(-1) == 0xffffffff) |
2280 |
+assert(bit32.band((1 << 33) - 1) == 0xffffffff) |
2281 |
+assert(bit32.band(-(1 << 33) - 1) == 0xffffffff) |
2282 |
+assert(bit32.band((1 << 33) + 1) == 1) |
2283 |
+assert(bit32.band(-(1 << 33) + 1) == 1) |
2284 |
+assert(bit32.band(-(1 << 40)) == 0) |
2285 |
+assert(bit32.band(1 << 40) == 0) |
2286 |
+assert(bit32.band(-(1 << 40) - 2) == 0xfffffffe) |
2287 |
+assert(bit32.band((1 << 40) - 4) == 0xfffffffc) |
2288 |
+ |
2289 |
+assert(bit32.lrotate(0, -1) == 0) |
2290 |
+assert(bit32.lrotate(0, 7) == 0) |
2291 |
+assert(bit32.lrotate(0x12345678, 0) == 0x12345678) |
2292 |
+assert(bit32.lrotate(0x12345678, 32) == 0x12345678) |
2293 |
+assert(bit32.lrotate(0x12345678, 4) == 0x23456781) |
2294 |
+assert(bit32.rrotate(0x12345678, -4) == 0x23456781) |
2295 |
+assert(bit32.lrotate(0x12345678, -8) == 0x78123456) |
2296 |
+assert(bit32.rrotate(0x12345678, 8) == 0x78123456) |
2297 |
+assert(bit32.lrotate(0xaaaaaaaa, 2) == 0xaaaaaaaa) |
2298 |
+assert(bit32.lrotate(0xaaaaaaaa, -2) == 0xaaaaaaaa) |
2299 |
+for i = -50, 50 do |
2300 |
+ assert(bit32.lrotate(0x89abcdef, i) == bit32.lrotate(0x89abcdef, i%32)) |
2301 |
+end |
2302 |
+ |
2303 |
+assert(bit32.lshift(0x12345678, 4) == 0x23456780) |
2304 |
+assert(bit32.lshift(0x12345678, 8) == 0x34567800) |
2305 |
+assert(bit32.lshift(0x12345678, -4) == 0x01234567) |
2306 |
+assert(bit32.lshift(0x12345678, -8) == 0x00123456) |
2307 |
+assert(bit32.lshift(0x12345678, 32) == 0) |
2308 |
+assert(bit32.lshift(0x12345678, -32) == 0) |
2309 |
+assert(bit32.rshift(0x12345678, 4) == 0x01234567) |
2310 |
+assert(bit32.rshift(0x12345678, 8) == 0x00123456) |
2311 |
+assert(bit32.rshift(0x12345678, 32) == 0) |
2312 |
+assert(bit32.rshift(0x12345678, -32) == 0) |
2313 |
+assert(bit32.arshift(0x12345678, 0) == 0x12345678) |
2314 |
+assert(bit32.arshift(0x12345678, 1) == 0x12345678 // 2) |
2315 |
+assert(bit32.arshift(0x12345678, -1) == 0x12345678 * 2) |
2316 |
+assert(bit32.arshift(-1, 1) == 0xffffffff) |
2317 |
+assert(bit32.arshift(-1, 24) == 0xffffffff) |
2318 |
+assert(bit32.arshift(-1, 32) == 0xffffffff) |
2319 |
+assert(bit32.arshift(-1, -1) == bit32.band(-1 * 2, 0xffffffff)) |
2320 |
+ |
2321 |
+assert(0x12345678 << 4 == 0x123456780) |
2322 |
+assert(0x12345678 << 8 == 0x1234567800) |
2323 |
+assert(0x12345678 << -4 == 0x01234567) |
2324 |
+assert(0x12345678 << -8 == 0x00123456) |
2325 |
+assert(0x12345678 << 32 == 0x1234567800000000) |
2326 |
+assert(0x12345678 << -32 == 0) |
2327 |
+assert(0x12345678 >> 4 == 0x01234567) |
2328 |
+assert(0x12345678 >> 8 == 0x00123456) |
2329 |
+assert(0x12345678 >> 32 == 0) |
2330 |
+assert(0x12345678 >> -32 == 0x1234567800000000) |
2331 |
+ |
2332 |
+print("+") |
2333 |
+-- some special cases |
2334 |
+local c = {0, 1, 2, 3, 10, 0x80000000, 0xaaaaaaaa, 0x55555555, |
2335 |
+ 0xffffffff, 0x7fffffff} |
2336 |
+ |
2337 |
+for _, b in pairs(c) do |
2338 |
+ assert(bit32.band(b) == b) |
2339 |
+ assert(bit32.band(b, b) == b) |
2340 |
+ assert(bit32.band(b, b, b, b) == b) |
2341 |
+ assert(bit32.btest(b, b) == (b ~= 0)) |
2342 |
+ assert(bit32.band(b, b, b) == b) |
2343 |
+ assert(bit32.band(b, b, b, ~b) == 0) |
2344 |
+ assert(bit32.btest(b, b, b) == (b ~= 0)) |
2345 |
+ assert(bit32.band(b, bit32.bnot(b)) == 0) |
2346 |
+ assert(bit32.bor(b, bit32.bnot(b)) == bit32.bnot(0)) |
2347 |
+ assert(bit32.bor(b) == b) |
2348 |
+ assert(bit32.bor(b, b) == b) |
2349 |
+ assert(bit32.bor(b, b, b) == b) |
2350 |
+ assert(bit32.bor(b, b, 0, ~b) == 0xffffffff) |
2351 |
+ assert(bit32.bxor(b) == b) |
2352 |
+ assert(bit32.bxor(b, b) == 0) |
2353 |
+ assert(bit32.bxor(b, b, b) == b) |
2354 |
+ assert(bit32.bxor(b, b, b, b) == 0) |
2355 |
+ assert(bit32.bxor(b, 0) == b) |
2356 |
+ assert(bit32.bnot(b) ~= b) |
2357 |
+ assert(bit32.bnot(bit32.bnot(b)) == b) |
2358 |
+ assert(bit32.bnot(b) == (1 << 32) - 1 - b) |
2359 |
+ assert(bit32.lrotate(b, 32) == b) |
2360 |
+ assert(bit32.rrotate(b, 32) == b) |
2361 |
+ assert(bit32.lshift(bit32.lshift(b, -4), 4) == bit32.band(b, bit32.bnot(0xf))) |
2362 |
+ assert(bit32.rshift(bit32.rshift(b, 4), -4) == bit32.band(b, bit32.bnot(0xf))) |
2363 |
+end |
2364 |
+ |
2365 |
+-- for this test, use at most 24 bits (mantissa of a single float) |
2366 |
+c = {0, 1, 2, 3, 10, 0x800000, 0xaaaaaa, 0x555555, 0xffffff, 0x7fffff} |
2367 |
+for _, b in pairs(c) do |
2368 |
+ for i = -40, 40 do |
2369 |
+ local x = bit32.lshift(b, i) |
2370 |
+ local y = math.floor(math.fmod(b * 2.0^i, 2.0^32)) |
2371 |
+ assert(math.fmod(x - y, 2.0^32) == 0) |
2372 |
+ end |
2373 |
+end |
2374 |
+ |
2375 |
+assert(not pcall(bit32.band, {})) |
2376 |
+assert(not pcall(bit32.bnot, "a")) |
2377 |
+assert(not pcall(bit32.lshift, 45)) |
2378 |
+assert(not pcall(bit32.lshift, 45, print)) |
2379 |
+assert(not pcall(bit32.rshift, 45, print)) |
2380 |
+ |
2381 |
+print("+") |
2382 |
+ |
2383 |
+ |
2384 |
+-- testing extract/replace |
2385 |
+ |
2386 |
+assert(bit32.extract(0x12345678, 0, 4) == 8) |
2387 |
+assert(bit32.extract(0x12345678, 4, 4) == 7) |
2388 |
+assert(bit32.extract(0xa0001111, 28, 4) == 0xa) |
2389 |
+assert(bit32.extract(0xa0001111, 31, 1) == 1) |
2390 |
+assert(bit32.extract(0x50000111, 31, 1) == 0) |
2391 |
+assert(bit32.extract(0xf2345679, 0, 32) == 0xf2345679) |
2392 |
+ |
2393 |
+assert(not pcall(bit32.extract, 0, -1)) |
2394 |
+assert(not pcall(bit32.extract, 0, 32)) |
2395 |
+assert(not pcall(bit32.extract, 0, 0, 33)) |
2396 |
+assert(not pcall(bit32.extract, 0, 31, 2)) |
2397 |
+ |
2398 |
+assert(bit32.replace(0x12345678, 5, 28, 4) == 0x52345678) |
2399 |
+assert(bit32.replace(0x12345678, 0x87654321, 0, 32) == 0x87654321) |
2400 |
+assert(bit32.replace(0, 1, 2) == 2^2) |
2401 |
+assert(bit32.replace(0, -1, 4) == 2^4) |
2402 |
+assert(bit32.replace(-1, 0, 31) == (1 << 31) - 1) |
2403 |
+assert(bit32.replace(-1, 0, 1, 2) == (1 << 32) - 7) |
2404 |
+ |
2405 |
+ |
2406 |
+-- testing conversion of floats |
2407 |
+ |
2408 |
+assert(bit32.bor(3.0) == 3) |
2409 |
+assert(bit32.bor(-4.0) == 0xfffffffc) |
2410 |
+ |
2411 |
+-- large floats and large-enough integers? |
2412 |
+if 2.0^50 < 2.0^50 + 1.0 and 2.0^50 < (-1 >> 1) then |
2413 |
+ assert(bit32.bor(2.0^32 - 5.0) == 0xfffffffb) |
2414 |
+ assert(bit32.bor(-2.0^32 - 6.0) == 0xfffffffa) |
2415 |
+ assert(bit32.bor(2.0^48 - 5.0) == 0xfffffffb) |
2416 |
+ assert(bit32.bor(-2.0^48 - 6.0) == 0xfffffffa) |
2417 |
+end |
2418 |
+ |
2419 |
+print'OK' |
2420 |
+ |
2421 |
|
2422 |
diff --git a/tests/calls.lua b/tests/calls.lua |
2423 |
new file mode 100644 |
2424 |
index 0000000..6d6fb7b |
2425 |
--- /dev/null |
2426 |
+++ b/tests/calls.lua |
2427 |
@@ -0,0 +1,401 @@ |
2428 |
+-- $Id: calls.lua,v 1.60 2016/11/07 13:11:28 roberto Exp $ |
2429 |
+-- See Copyright Notice in file all.lua |
2430 |
+ |
2431 |
+print("testing functions and calls") |
2432 |
+ |
2433 |
+local debug = require "debug" |
2434 |
+ |
2435 |
+-- get the opportunity to test 'type' too ;) |
2436 |
+ |
2437 |
+assert(type(1<2) == 'boolean') |
2438 |
+assert(type(true) == 'boolean' and type(false) == 'boolean') |
2439 |
+assert(type(nil) == 'nil' |
2440 |
+ and type(-3) == 'number' |
2441 |
+ and type'x' == 'string' |
2442 |
+ and type{} == 'table' |
2443 |
+ and type(type) == 'function') |
2444 |
+ |
2445 |
+assert(type(assert) == type(print)) |
2446 |
+function f (x) return a:x (x) end |
2447 |
+assert(type(f) == 'function') |
2448 |
+assert(not pcall(type)) |
2449 |
+ |
2450 |
+ |
2451 |
+do -- test error in 'print' too... |
2452 |
+ local tostring = _ENV.tostring |
2453 |
+ |
2454 |
+ _ENV.tostring = nil |
2455 |
+ local st, msg = pcall(print, 1) |
2456 |
+ assert(st == false and string.find(msg, "attempt to call a nil value")) |
2457 |
+ |
2458 |
+ _ENV.tostring = function () return {} end |
2459 |
+ local st, msg = pcall(print, 1) |
2460 |
+ assert(st == false and string.find(msg, "must return a string")) |
2461 |
+ |
2462 |
+ _ENV.tostring = tostring |
2463 |
+end |
2464 |
+ |
2465 |
+ |
2466 |
+-- testing local-function recursion |
2467 |
+fact = false |
2468 |
+do |
2469 |
+ local res = 1 |
2470 |
+ local function fact (n) |
2471 |
+ if n==0 then return res |
2472 |
+ else return n*fact(n-1) |
2473 |
+ end |
2474 |
+ end |
2475 |
+ assert(fact(5) == 120) |
2476 |
+end |
2477 |
+assert(fact == false) |
2478 |
+ |
2479 |
+-- testing declarations |
2480 |
+a = {i = 10} |
2481 |
+self = 20 |
2482 |
+function a:x (x) return x+self.i end |
2483 |
+function a.y (x) return x+self end |
2484 |
+ |
2485 |
+assert(a:x(1)+10 == a.y(1)) |
2486 |
+ |
2487 |
+a.t = {i=-100} |
2488 |
+a["t"].x = function (self, a,b) return self.i+a+b end |
2489 |
+ |
2490 |
+assert(a.t:x(2,3) == -95) |
2491 |
+ |
2492 |
+do |
2493 |
+ local a = {x=0} |
2494 |
+ function a:add (x) self.x, a.y = self.x+x, 20; return self end |
2495 |
+ assert(a:add(10):add(20):add(30).x == 60 and a.y == 20) |
2496 |
+end |
2497 |
+ |
2498 |
+local a = {b={c={}}} |
2499 |
+ |
2500 |
+function a.b.c.f1 (x) return x+1 end |
2501 |
+function a.b.c:f2 (x,y) self[x] = y end |
2502 |
+assert(a.b.c.f1(4) == 5) |
2503 |
+a.b.c:f2('k', 12); assert(a.b.c.k == 12) |
2504 |
+ |
2505 |
+print('+') |
2506 |
+ |
2507 |
+t = nil -- 'declare' t |
2508 |
+function f(a,b,c) local d = 'a'; t={a,b,c,d} end |
2509 |
+ |
2510 |
+f( -- this line change must be valid |
2511 |
+ 1,2) |
2512 |
+assert(t[1] == 1 and t[2] == 2 and t[3] == nil and t[4] == 'a') |
2513 |
+f(1,2, -- this one too |
2514 |
+ 3,4) |
2515 |
+assert(t[1] == 1 and t[2] == 2 and t[3] == 3 and t[4] == 'a') |
2516 |
+ |
2517 |
+function fat(x) |
2518 |
+ if x <= 1 then return 1 |
2519 |
+ else return x*load("return fat(" .. x-1 .. ")", "")() |
2520 |
+ end |
2521 |
+end |
2522 |
+ |
2523 |
+assert(load "load 'assert(fat(6)==720)' () ")() |
2524 |
+a = load('return fat(5), 3') |
2525 |
+a,b = a() |
2526 |
+assert(a == 120 and b == 3) |
2527 |
+print('+') |
2528 |
+ |
2529 |
+function err_on_n (n) |
2530 |
+ if n==0 then error(); exit(1); |
2531 |
+ else err_on_n (n-1); exit(1); |
2532 |
+ end |
2533 |
+end |
2534 |
+ |
2535 |
+do |
2536 |
+ function dummy (n) |
2537 |
+ if n > 0 then |
2538 |
+ assert(not pcall(err_on_n, n)) |
2539 |
+ dummy(n-1) |
2540 |
+ end |
2541 |
+ end |
2542 |
+end |
2543 |
+ |
2544 |
+dummy(10) |
2545 |
+ |
2546 |
+function deep (n) |
2547 |
+ if n>0 then deep(n-1) end |
2548 |
+end |
2549 |
+deep(10) |
2550 |
+deep(200) |
2551 |
+ |
2552 |
+-- testing tail call |
2553 |
+function deep (n) if n>0 then return deep(n-1) else return 101 end end |
2554 |
+assert(deep(30000) == 101) |
2555 |
+a = {} |
2556 |
+function a:deep (n) if n>0 then return self:deep(n-1) else return 101 end end |
2557 |
+assert(a:deep(30000) == 101) |
2558 |
+ |
2559 |
+print('+') |
2560 |
+ |
2561 |
+ |
2562 |
+a = nil |
2563 |
+(function (x) a=x end)(23) |
2564 |
+assert(a == 23 and (function (x) return x*2 end)(20) == 40) |
2565 |
+ |
2566 |
+ |
2567 |
+-- testing closures |
2568 |
+ |
2569 |
+-- fixed-point operator |
2570 |
+Z = function (le) |
2571 |
+ local function a (f) |
2572 |
+ return le(function (x) return f(f)(x) end) |
2573 |
+ end |
2574 |
+ return a(a) |
2575 |
+ end |
2576 |
+ |
2577 |
+ |
2578 |
+-- non-recursive factorial |
2579 |
+ |
2580 |
+F = function (f) |
2581 |
+ return function (n) |
2582 |
+ if n == 0 then return 1 |
2583 |
+ else return n*f(n-1) end |
2584 |
+ end |
2585 |
+ end |
2586 |
+ |
2587 |
+fat = Z(F) |
2588 |
+ |
2589 |
+assert(fat(0) == 1 and fat(4) == 24 and Z(F)(5)==5*Z(F)(4)) |
2590 |
+ |
2591 |
+local function g (z) |
2592 |
+ local function f (a,b,c,d) |
2593 |
+ return function (x,y) return a+b+c+d+a+x+y+z end |
2594 |
+ end |
2595 |
+ return f(z,z+1,z+2,z+3) |
2596 |
+end |
2597 |
+ |
2598 |
+f = g(10) |
2599 |
+assert(f(9, 16) == 10+11+12+13+10+9+16+10) |
2600 |
+ |
2601 |
+Z, F, f = nil |
2602 |
+print('+') |
2603 |
+ |
2604 |
+-- testing multiple returns |
2605 |
+ |
2606 |
+function unlpack (t, i) |
2607 |
+ i = i or 1 |
2608 |
+ if (i <= #t) then |
2609 |
+ return t[i], unlpack(t, i+1) |
2610 |
+ end |
2611 |
+end |
2612 |
+ |
2613 |
+function equaltab (t1, t2) |
2614 |
+ assert(#t1 == #t2) |
2615 |
+ for i = 1, #t1 do |
2616 |
+ assert(t1[i] == t2[i]) |
2617 |
+ end |
2618 |
+end |
2619 |
+ |
2620 |
+local pack = function (...) return (table.pack(...)) end |
2621 |
+ |
2622 |
+function f() return 1,2,30,4 end |
2623 |
+function ret2 (a,b) return a,b end |
2624 |
+ |
2625 |
+local a,b,c,d = unlpack{1,2,3} |
2626 |
+assert(a==1 and b==2 and c==3 and d==nil) |
2627 |
+a = {1,2,3,4,false,10,'alo',false,assert} |
2628 |
+equaltab(pack(unlpack(a)), a) |
2629 |
+equaltab(pack(unlpack(a), -1), {1,-1}) |
2630 |
+a,b,c,d = ret2(f()), ret2(f()) |
2631 |
+assert(a==1 and b==1 and c==2 and d==nil) |
2632 |
+a,b,c,d = unlpack(pack(ret2(f()), ret2(f()))) |
2633 |
+assert(a==1 and b==1 and c==2 and d==nil) |
2634 |
+a,b,c,d = unlpack(pack(ret2(f()), (ret2(f())))) |
2635 |
+assert(a==1 and b==1 and c==nil and d==nil) |
2636 |
+ |
2637 |
+a = ret2{ unlpack{1,2,3}, unlpack{3,2,1}, unlpack{"a", "b"}} |
2638 |
+assert(a[1] == 1 and a[2] == 3 and a[3] == "a" and a[4] == "b") |
2639 |
+ |
2640 |
+ |
2641 |
+-- testing calls with 'incorrect' arguments |
2642 |
+rawget({}, "x", 1) |
2643 |
+rawset({}, "x", 1, 2) |
2644 |
+assert(math.sin(1,2) == math.sin(1)) |
2645 |
+table.sort({10,9,8,4,19,23,0,0}, function (a,b) return a<b end, "extra arg") |
2646 |
+ |
2647 |
+ |
2648 |
+-- test for generic load |
2649 |
+local x = "-- a comment\0\0\0\n x = 10 + \n23; \ |
2650 |
+ local a = function () x = 'hi' end; \ |
2651 |
+ return '\0'" |
2652 |
+function read1 (x) |
2653 |
+ local i = 0 |
2654 |
+ return function () |
2655 |
+ collectgarbage() |
2656 |
+ i=i+1 |
2657 |
+ return string.sub(x, i, i) |
2658 |
+ end |
2659 |
+end |
2660 |
+ |
2661 |
+function cannotload (msg, a,b) |
2662 |
+ assert(not a and string.find(b, msg)) |
2663 |
+end |
2664 |
+ |
2665 |
+a = assert(load(read1(x), "modname", "t", _G)) |
2666 |
+assert(a() == "\0" and _G.x == 33) |
2667 |
+assert(debug.getinfo(a).source == "modname") |
2668 |
+-- cannot read text in binary mode |
2669 |
+cannotload("attempt to load a text chunk", load(read1(x), "modname", "b", {})) |
2670 |
+cannotload("attempt to load a text chunk", load(x, "modname", "b")) |
2671 |
+ |
2672 |
+a = assert(load(function () return nil end)) |
2673 |
+a() -- empty chunk |
2674 |
+ |
2675 |
+assert(not load(function () return true end)) |
2676 |
+ |
2677 |
+ |
2678 |
+-- small bug |
2679 |
+local t = {nil, "return ", "3"} |
2680 |
+f, msg = load(function () return table.remove(t, 1) end) |
2681 |
+assert(f() == nil) -- should read the empty chunk |
2682 |
+ |
2683 |
+-- another small bug (in 5.2.1) |
2684 |
+f = load(string.dump(function () return 1 end), nil, "b", {}) |
2685 |
+assert(type(f) == "function" and f() == 1) |
2686 |
+ |
2687 |
+ |
2688 |
+x = string.dump(load("x = 1; return x")) |
2689 |
+a = assert(load(read1(x), nil, "b")) |
2690 |
+assert(a() == 1 and _G.x == 1) |
2691 |
+cannotload("attempt to load a binary chunk", load(read1(x), nil, "t")) |
2692 |
+cannotload("attempt to load a binary chunk", load(x, nil, "t")) |
2693 |
+ |
2694 |
+assert(not pcall(string.dump, print)) -- no dump of C functions |
2695 |
+ |
2696 |
+cannotload("unexpected symbol", load(read1("*a = 123"))) |
2697 |
+cannotload("unexpected symbol", load("*a = 123")) |
2698 |
+cannotload("hhi", load(function () error("hhi") end)) |
2699 |
+ |
2700 |
+-- any value is valid for _ENV |
2701 |
+assert(load("return _ENV", nil, nil, 123)() == 123) |
2702 |
+ |
2703 |
+ |
2704 |
+-- load when _ENV is not first upvalue |
2705 |
+local x; XX = 123 |
2706 |
+local function h () |
2707 |
+ local y=x -- use 'x', so that it becomes 1st upvalue |
2708 |
+ return XX -- global name |
2709 |
+end |
2710 |
+local d = string.dump(h) |
2711 |
+x = load(d, "", "b") |
2712 |
+assert(debug.getupvalue(x, 2) == '_ENV') |
2713 |
+debug.setupvalue(x, 2, _G) |
2714 |
+assert(x() == 123) |
2715 |
+ |
2716 |
+assert(assert(load("return XX + ...", nil, nil, {XX = 13}))(4) == 17) |
2717 |
+ |
2718 |
+ |
2719 |
+-- test generic load with nested functions |
2720 |
+x = [[ |
2721 |
+ return function (x) |
2722 |
+ return function (y) |
2723 |
+ return function (z) |
2724 |
+ return x+y+z |
2725 |
+ end |
2726 |
+ end |
2727 |
+ end |
2728 |
+]] |
2729 |
+ |
2730 |
+a = assert(load(read1(x))) |
2731 |
+assert(a()(2)(3)(10) == 15) |
2732 |
+ |
2733 |
+ |
2734 |
+-- test for dump/undump with upvalues |
2735 |
+local a, b = 20, 30 |
2736 |
+x = load(string.dump(function (x) |
2737 |
+ if x == "set" then a = 10+b; b = b+1 else |
2738 |
+ return a |
2739 |
+ end |
2740 |
+end), "", "b", nil) |
2741 |
+assert(x() == nil) |
2742 |
+assert(debug.setupvalue(x, 1, "hi") == "a") |
2743 |
+assert(x() == "hi") |
2744 |
+assert(debug.setupvalue(x, 2, 13) == "b") |
2745 |
+assert(not debug.setupvalue(x, 3, 10)) -- only 2 upvalues |
2746 |
+x("set") |
2747 |
+assert(x() == 23) |
2748 |
+x("set") |
2749 |
+assert(x() == 24) |
2750 |
+ |
2751 |
+-- test for dump/undump with many upvalues |
2752 |
+do |
2753 |
+ local nup = 200 -- maximum number of local variables |
2754 |
+ local prog = {"local a1"} |
2755 |
+ for i = 2, nup do prog[#prog + 1] = ", a" .. i end |
2756 |
+ prog[#prog + 1] = " = 1" |
2757 |
+ for i = 2, nup do prog[#prog + 1] = ", " .. i end |
2758 |
+ local sum = 1 |
2759 |
+ prog[#prog + 1] = "; return function () return a1" |
2760 |
+ for i = 2, nup do prog[#prog + 1] = " + a" .. i; sum = sum + i end |
2761 |
+ prog[#prog + 1] = " end" |
2762 |
+ prog = table.concat(prog) |
2763 |
+ local f = assert(load(prog))() |
2764 |
+ assert(f() == sum) |
2765 |
+ |
2766 |
+ f = load(string.dump(f)) -- main chunk now has many upvalues |
2767 |
+ local a = 10 |
2768 |
+ local h = function () return a end |
2769 |
+ for i = 1, nup do |
2770 |
+ debug.upvaluejoin(f, i, h, 1) |
2771 |
+ end |
2772 |
+ assert(f() == 10 * nup) |
2773 |
+end |
2774 |
+ |
2775 |
+-- test for long method names |
2776 |
+do |
2777 |
+ local t = {x = 1} |
2778 |
+ function t:_012345678901234567890123456789012345678901234567890123456789 () |
2779 |
+ return self.x |
2780 |
+ end |
2781 |
+ assert(t:_012345678901234567890123456789012345678901234567890123456789() == 1) |
2782 |
+end |
2783 |
+ |
2784 |
+ |
2785 |
+-- test for bug in parameter adjustment |
2786 |
+assert((function () return nil end)(4) == nil) |
2787 |
+assert((function () local a; return a end)(4) == nil) |
2788 |
+assert((function (a) return a end)() == nil) |
2789 |
+ |
2790 |
+ |
2791 |
+print("testing binary chunks") |
2792 |
+do |
2793 |
+ local header = string.pack("c4BBc6BBBBBj", |
2794 |
+ "\27Lua", -- signature |
2795 |
+ 5*16 + 3, -- version 5.3 |
2796 |
+ 0, -- format |
2797 |
+ "\x19\x93\r\n\x1a\n", -- data |
2798 |
+ string.packsize("i"), -- sizeof(int) |
2799 |
+ string.packsize("T"), -- sizeof(size_t) |
2800 |
+ 4, -- size of instruction |
2801 |
+ string.packsize("j"), -- sizeof(lua integer) |
2802 |
+ string.packsize("n"), -- sizeof(lua number) |
2803 |
+ 0x5678 -- LUAC_INT |
2804 |
+ -- LUAC_NUM may not have a unique binary representation (padding...) |
2805 |
+ ) |
2806 |
+ local c = string.dump(function () local a = 1; local b = 3; return a+b*3 end) |
2807 |
+ |
2808 |
+ assert(string.sub(c, 1, #header) == header) |
2809 |
+ |
2810 |
+ -- corrupted header |
2811 |
+ for i = 1, #header do |
2812 |
+ local s = string.sub(c, 1, i - 1) .. |
2813 |
+ string.char(string.byte(string.sub(c, i, i)) + 1) .. |
2814 |
+ string.sub(c, i + 1, -1) |
2815 |
+ assert(#s == #c) |
2816 |
+ assert(not load(s)) |
2817 |
+ end |
2818 |
+ |
2819 |
+ -- loading truncated binary chunks |
2820 |
+ for i = 1, #c - 1 do |
2821 |
+ local st, msg = load(string.sub(c, 1, i)) |
2822 |
+ assert(not st and string.find(msg, "truncated")) |
2823 |
+ end |
2824 |
+ assert(assert(load(c))() == 10) |
2825 |
+end |
2826 |
+ |
2827 |
+print('OK') |
2828 |
+return deep |
2829 |
|
2830 |
diff --git a/tests/closure.lua b/tests/closure.lua |
2831 |
new file mode 100644 |
2832 |
index 0000000..15897ae |
2833 |
--- /dev/null |
2834 |
+++ b/tests/closure.lua |
2835 |
@@ -0,0 +1,247 @@ |
2836 |
+-- $Id: closure.lua,v 1.59 2016/11/07 13:11:28 roberto Exp $ |
2837 |
+-- See Copyright Notice in file all.lua |
2838 |
+ |
2839 |
+print "testing closures" |
2840 |
+ |
2841 |
+local A,B = 0,{g=10} |
2842 |
+function f(x) |
2843 |
+ local a = {} |
2844 |
+ for i=1,1000 do |
2845 |
+ local y = 0 |
2846 |
+ do |
2847 |
+ a[i] = function () B.g = B.g+1; y = y+x; return y+A end |
2848 |
+ end |
2849 |
+ end |
2850 |
+ local dummy = function () return a[A] end |
2851 |
+ collectgarbage() |
2852 |
+ A = 1; assert(dummy() == a[1]); A = 0; |
2853 |
+ assert(a[1]() == x) |
2854 |
+ assert(a[3]() == x) |
2855 |
+ collectgarbage() |
2856 |
+ assert(B.g == 12) |
2857 |
+ return a |
2858 |
+end |
2859 |
+ |
2860 |
+local a = f(10) |
2861 |
+-- force a GC in this level |
2862 |
+local x = {[1] = {}} -- to detect a GC |
2863 |
+setmetatable(x, {__mode = 'kv'}) |
2864 |
+while x[1] do -- repeat until GC |
2865 |
+ local a = A..A..A..A -- create garbage |
2866 |
+ A = A+1 |
2867 |
+end |
2868 |
+assert(a[1]() == 20+A) |
2869 |
+assert(a[1]() == 30+A) |
2870 |
+assert(a[2]() == 10+A) |
2871 |
+collectgarbage() |
2872 |
+assert(a[2]() == 20+A) |
2873 |
+assert(a[2]() == 30+A) |
2874 |
+assert(a[3]() == 20+A) |
2875 |
+assert(a[8]() == 10+A) |
2876 |
+assert(getmetatable(x).__mode == 'kv') |
2877 |
+assert(B.g == 19) |
2878 |
+ |
2879 |
+ |
2880 |
+-- testing equality |
2881 |
+a = {} |
2882 |
+for i = 1, 5 do a[i] = function (x) return x + a + _ENV end end |
2883 |
+assert(a[3] == a[4] and a[4] == a[5]) |
2884 |
+ |
2885 |
+for i = 1, 5 do a[i] = function (x) return i + a + _ENV end end |
2886 |
+assert(a[3] ~= a[4] and a[4] ~= a[5]) |
2887 |
+ |
2888 |
+local function f() |
2889 |
+ return function (x) return math.sin(_ENV[x]) end |
2890 |
+end |
2891 |
+assert(f() == f()) |
2892 |
+ |
2893 |
+ |
2894 |
+-- testing closures with 'for' control variable |
2895 |
+a = {} |
2896 |
+for i=1,10 do |
2897 |
+ a[i] = {set = function(x) i=x end, get = function () return i end} |
2898 |
+ if i == 3 then break end |
2899 |
+end |
2900 |
+assert(a[4] == nil) |
2901 |
+a[1].set(10) |
2902 |
+assert(a[2].get() == 2) |
2903 |
+a[2].set('a') |
2904 |
+assert(a[3].get() == 3) |
2905 |
+assert(a[2].get() == 'a') |
2906 |
+ |
2907 |
+a = {} |
2908 |
+local t = {"a", "b"} |
2909 |
+for i = 1, #t do |
2910 |
+ local k = t[i] |
2911 |
+ a[i] = {set = function(x, y) i=x; k=y end, |
2912 |
+ get = function () return i, k end} |
2913 |
+ if i == 2 then break end |
2914 |
+end |
2915 |
+a[1].set(10, 20) |
2916 |
+local r,s = a[2].get() |
2917 |
+assert(r == 2 and s == 'b') |
2918 |
+r,s = a[1].get() |
2919 |
+assert(r == 10 and s == 20) |
2920 |
+a[2].set('a', 'b') |
2921 |
+r,s = a[2].get() |
2922 |
+assert(r == "a" and s == "b") |
2923 |
+ |
2924 |
+ |
2925 |
+-- testing closures with 'for' control variable x break |
2926 |
+for i=1,3 do |
2927 |
+ f = function () return i end |
2928 |
+ break |
2929 |
+end |
2930 |
+assert(f() == 1) |
2931 |
+ |
2932 |
+for k = 1, #t do |
2933 |
+ local v = t[k] |
2934 |
+ f = function () return k, v end |
2935 |
+ break |
2936 |
+end |
2937 |
+assert(({f()})[1] == 1) |
2938 |
+assert(({f()})[2] == "a") |
2939 |
+ |
2940 |
+ |
2941 |
+-- testing closure x break x return x errors |
2942 |
+ |
2943 |
+local b |
2944 |
+function f(x) |
2945 |
+ local first = 1 |
2946 |
+ while 1 do |
2947 |
+ if x == 3 and not first then return end |
2948 |
+ local a = 'xuxu' |
2949 |
+ b = function (op, y) |
2950 |
+ if op == 'set' then |
2951 |
+ a = x+y |
2952 |
+ else |
2953 |
+ return a |
2954 |
+ end |
2955 |
+ end |
2956 |
+ if x == 1 then do break end |
2957 |
+ elseif x == 2 then return |
2958 |
+ else if x ~= 3 then error() end |
2959 |
+ end |
2960 |
+ first = nil |
2961 |
+ end |
2962 |
+end |
2963 |
+ |
2964 |
+for i=1,3 do |
2965 |
+ f(i) |
2966 |
+ assert(b('get') == 'xuxu') |
2967 |
+ b('set', 10); assert(b('get') == 10+i) |
2968 |
+ b = nil |
2969 |
+end |
2970 |
+ |
2971 |
+pcall(f, 4); |
2972 |
+assert(b('get') == 'xuxu') |
2973 |
+b('set', 10); assert(b('get') == 14) |
2974 |
+ |
2975 |
+ |
2976 |
+local w |
2977 |
+-- testing multi-level closure |
2978 |
+function f(x) |
2979 |
+ return function (y) |
2980 |
+ return function (z) return w+x+y+z end |
2981 |
+ end |
2982 |
+end |
2983 |
+ |
2984 |
+y = f(10) |
2985 |
+w = 1.345 |
2986 |
+assert(y(20)(30) == 60+w) |
2987 |
+ |
2988 |
+-- testing closures x repeat-until |
2989 |
+ |
2990 |
+local a = {} |
2991 |
+local i = 1 |
2992 |
+repeat |
2993 |
+ local x = i |
2994 |
+ a[i] = function () i = x+1; return x end |
2995 |
+until i > 10 or a[i]() ~= x |
2996 |
+assert(i == 11 and a[1]() == 1 and a[3]() == 3 and i == 4) |
2997 |
+ |
2998 |
+ |
2999 |
+-- testing closures created in 'then' and 'else' parts of 'if's |
3000 |
+a = {} |
3001 |
+for i = 1, 10 do |
3002 |
+ if i % 3 == 0 then |
3003 |
+ local y = 0 |
3004 |
+ a[i] = function (x) local t = y; y = x; return t end |
3005 |
+ elseif i % 3 == 1 then |
3006 |
+ goto L1 |
3007 |
+ error'not here' |
3008 |
+ ::L1:: |
3009 |
+ local y = 1 |
3010 |
+ a[i] = function (x) local t = y; y = x; return t end |
3011 |
+ elseif i % 3 == 2 then |
3012 |
+ local t |
3013 |
+ goto l4 |
3014 |
+ ::l4a:: a[i] = t; goto l4b |
3015 |
+ error("should never be here!") |
3016 |
+ ::l4:: |
3017 |
+ local y = 2 |
3018 |
+ t = function (x) local t = y; y = x; return t end |
3019 |
+ goto l4a |
3020 |
+ error("should never be here!") |
3021 |
+ ::l4b:: |
3022 |
+ end |
3023 |
+end |
3024 |
+ |
3025 |
+for i = 1, 10 do |
3026 |
+ assert(a[i](i * 10) == i % 3 and a[i]() == i * 10) |
3027 |
+end |
3028 |
+ |
3029 |
+print'+' |
3030 |
+ |
3031 |
+ |
3032 |
+-- test for correctly closing upvalues in tail calls of vararg functions |
3033 |
+local function t () |
3034 |
+ local function c(a,b) assert(a=="test" and b=="OK") end |
3035 |
+ local function v(f, ...) c("test", f() ~= 1 and "FAILED" or "OK") end |
3036 |
+ local x = 1 |
3037 |
+ return v(function() return x end) |
3038 |
+end |
3039 |
+t() |
3040 |
+ |
3041 |
+ |
3042 |
+-- test for debug manipulation of upvalues |
3043 |
+local debug = require'debug' |
3044 |
+ |
3045 |
+do |
3046 |
+ local a , b, c = 3, 5, 7 |
3047 |
+ foo1 = function () return a+b end; |
3048 |
+ foo2 = function () return b+a end; |
3049 |
+ do |
3050 |
+ local a = 10 |
3051 |
+ foo3 = function () return a+b end; |
3052 |
+ end |
3053 |
+end |
3054 |
+ |
3055 |
+assert(debug.upvalueid(foo1, 1)) |
3056 |
+assert(debug.upvalueid(foo1, 2)) |
3057 |
+assert(not pcall(debug.upvalueid, foo1, 3)) |
3058 |
+assert(debug.upvalueid(foo1, 1) == debug.upvalueid(foo2, 2)) |
3059 |
+assert(debug.upvalueid(foo1, 2) == debug.upvalueid(foo2, 1)) |
3060 |
+assert(debug.upvalueid(foo3, 1)) |
3061 |
+assert(debug.upvalueid(foo1, 1) ~= debug.upvalueid(foo3, 1)) |
3062 |
+assert(debug.upvalueid(foo1, 2) == debug.upvalueid(foo3, 2)) |
3063 |
+ |
3064 |
+assert(debug.upvalueid(string.gmatch("x", "x"), 1) ~= nil) |
3065 |
+ |
3066 |
+assert(foo1() == 3 + 5 and foo2() == 5 + 3) |
3067 |
+debug.upvaluejoin(foo1, 2, foo2, 2) |
3068 |
+assert(foo1() == 3 + 3 and foo2() == 5 + 3) |
3069 |
+assert(foo3() == 10 + 5) |
3070 |
+debug.upvaluejoin(foo3, 2, foo2, 1) |
3071 |
+assert(foo3() == 10 + 5) |
3072 |
+debug.upvaluejoin(foo3, 2, foo2, 2) |
3073 |
+assert(foo3() == 10 + 3) |
3074 |
+ |
3075 |
+assert(not pcall(debug.upvaluejoin, foo1, 3, foo2, 1)) |
3076 |
+assert(not pcall(debug.upvaluejoin, foo1, 1, foo2, 3)) |
3077 |
+assert(not pcall(debug.upvaluejoin, foo1, 0, foo2, 1)) |
3078 |
+assert(not pcall(debug.upvaluejoin, print, 1, foo2, 1)) |
3079 |
+assert(not pcall(debug.upvaluejoin, {}, 1, foo2, 1)) |
3080 |
+assert(not pcall(debug.upvaluejoin, foo1, 1, print, 1)) |
3081 |
+ |
3082 |
+print'OK' |
3083 |
|
3084 |
diff --git a/tests/code.lua b/tests/code.lua |
3085 |
new file mode 100644 |
3086 |
index 0000000..0b9d142 |
3087 |
--- /dev/null |
3088 |
+++ b/tests/code.lua |
3089 |
@@ -0,0 +1,239 @@ |
3090 |
+-- $Id: code.lua,v 1.42 2016/11/07 13:04:32 roberto Exp $ |
3091 |
+-- See Copyright Notice in file all.lua |
3092 |
+ |
3093 |
+if T==nil then |
3094 |
+ (Message or print)('\n >>> testC not active: skipping opcode tests <<<\n') |
3095 |
+ return |
3096 |
+end |
3097 |
+print "testing code generation and optimizations" |
3098 |
+ |
3099 |
+ |
3100 |
+-- this code gave an error for the code checker |
3101 |
+do |
3102 |
+ local function f (a) |
3103 |
+ for k,v,w in a do end |
3104 |
+ end |
3105 |
+end |
3106 |
+ |
3107 |
+ |
3108 |
+-- testing reuse in constant table |
3109 |
+local function checkKlist (func, list) |
3110 |
+ local k = T.listk(func) |
3111 |
+ assert(#k == #list) |
3112 |
+ for i = 1, #k do |
3113 |
+ assert(k[i] == list[i] and math.type(k[i]) == math.type(list[i])) |
3114 |
+ end |
3115 |
+end |
3116 |
+ |
3117 |
+local function foo () |
3118 |
+ local a |
3119 |
+ a = 3; |
3120 |
+ a = 0; a = 0.0; a = -7 + 7 |
3121 |
+ a = 3.78/4; a = 3.78/4 |
3122 |
+ a = -3.78/4; a = 3.78/4; a = -3.78/4 |
3123 |
+ a = -3.79/4; a = 0.0; a = -0; |
3124 |
+ a = 3; a = 3.0; a = 3; a = 3.0 |
3125 |
+end |
3126 |
+ |
3127 |
+checkKlist(foo, {3, 0, 0.0, 3.78/4, -3.78/4, -3.79/4, 3.0}) |
3128 |
+ |
3129 |
+ |
3130 |
+-- testing opcodes |
3131 |
+ |
3132 |
+function check (f, ...) |
3133 |
+ local arg = {...} |
3134 |
+ local c = T.listcode(f) |
3135 |
+ for i=1, #arg do |
3136 |
+ -- print(arg[i], c[i]) |
3137 |
+ assert(string.find(c[i], '- '..arg[i]..' *%d')) |
3138 |
+ end |
3139 |
+ assert(c[#arg+2] == nil) |
3140 |
+end |
3141 |
+ |
3142 |
+ |
3143 |
+function checkequal (a, b) |
3144 |
+ a = T.listcode(a) |
3145 |
+ b = T.listcode(b) |
3146 |
+ for i = 1, #a do |
3147 |
+ a[i] = string.gsub(a[i], '%b()', '') -- remove line number |
3148 |
+ b[i] = string.gsub(b[i], '%b()', '') -- remove line number |
3149 |
+ assert(a[i] == b[i]) |
3150 |
+ end |
3151 |
+end |
3152 |
+ |
3153 |
+ |
3154 |
+-- some basic instructions |
3155 |
+check(function () |
3156 |
+ (function () end){f()} |
3157 |
+end, 'CLOSURE', 'NEWTABLE', 'GETTABUP', 'CALL', 'SETLIST', 'CALL', 'RETURN') |
3158 |
+ |
3159 |
+ |
3160 |
+-- sequence of LOADNILs |
3161 |
+check(function () |
3162 |
+ local a,b,c |
3163 |
+ local d; local e; |
3164 |
+ local f,g,h; |
3165 |
+ d = nil; d=nil; b=nil; a=nil; c=nil; |
3166 |
+end, 'LOADNIL', 'RETURN') |
3167 |
+ |
3168 |
+check(function () |
3169 |
+ local a,b,c,d = 1,1,1,1 |
3170 |
+ d=nil;c=nil;b=nil;a=nil |
3171 |
+end, 'LOADK', 'LOADK', 'LOADK', 'LOADK', 'LOADNIL', 'RETURN') |
3172 |
+ |
3173 |
+do |
3174 |
+ local a,b,c,d = 1,1,1,1 |
3175 |
+ d=nil;c=nil;b=nil;a=nil |
3176 |
+ assert(a == nil and b == nil and c == nil and d == nil) |
3177 |
+end |
3178 |
+ |
3179 |
+ |
3180 |
+-- single return |
3181 |
+check (function (a,b,c) return a end, 'RETURN') |
3182 |
+ |
3183 |
+ |
3184 |
+-- infinite loops |
3185 |
+check(function () while true do local a = -1 end end, |
3186 |
+'LOADK', 'JMP', 'RETURN') |
3187 |
+ |
3188 |
+check(function () while 1 do local a = -1 end end, |
3189 |
+'LOADK', 'JMP', 'RETURN') |
3190 |
+ |
3191 |
+check(function () repeat local x = 1 until true end, |
3192 |
+'LOADK', 'RETURN') |
3193 |
+ |
3194 |
+ |
3195 |
+-- concat optimization |
3196 |
+check(function (a,b,c,d) return a..b..c..d end, |
3197 |
+ 'MOVE', 'MOVE', 'MOVE', 'MOVE', 'CONCAT', 'RETURN') |
3198 |
+ |
3199 |
+-- not |
3200 |
+check(function () return not not nil end, 'LOADBOOL', 'RETURN') |
3201 |
+check(function () return not not false end, 'LOADBOOL', 'RETURN') |
3202 |
+check(function () return not not true end, 'LOADBOOL', 'RETURN') |
3203 |
+check(function () return not not 1 end, 'LOADBOOL', 'RETURN') |
3204 |
+ |
3205 |
+-- direct access to locals |
3206 |
+check(function () |
3207 |
+ local a,b,c,d |
3208 |
+ a = b*2 |
3209 |
+ c[2], a[b] = -((a + d/2 - a[b]) ^ a.x), b |
3210 |
+end, |
3211 |
+ 'LOADNIL', |
3212 |
+ 'MUL', |
3213 |
+ 'DIV', 'ADD', 'GETTABLE', 'SUB', 'GETTABLE', 'POW', |
3214 |
+ 'UNM', 'SETTABLE', 'SETTABLE', 'RETURN') |
3215 |
+ |
3216 |
+ |
3217 |
+-- direct access to constants |
3218 |
+check(function () |
3219 |
+ local a,b |
3220 |
+ a.x = 3.2 |
3221 |
+ a.x = b |
3222 |
+ a[b] = 'x' |
3223 |
+end, |
3224 |
+ 'LOADNIL', 'SETTABLE', 'SETTABLE', 'SETTABLE', 'RETURN') |
3225 |
+ |
3226 |
+check(function () |
3227 |
+ local a,b |
3228 |
+ a = 1 - a |
3229 |
+ b = 1/a |
3230 |
+ b = 5-4 |
3231 |
+end, |
3232 |
+ 'LOADNIL', 'SUB', 'DIV', 'LOADK', 'RETURN') |
3233 |
+ |
3234 |
+check(function () |
3235 |
+ local a,b |
3236 |
+ a[true] = false |
3237 |
+end, |
3238 |
+ 'LOADNIL', 'SETTABLE', 'RETURN') |
3239 |
+ |
3240 |
+ |
3241 |
+-- constant folding |
3242 |
+local function checkK (func, val) |
3243 |
+ check(func, 'LOADK', 'RETURN') |
3244 |
+ local k = T.listk(func) |
3245 |
+ assert(#k == 1 and k[1] == val and math.type(k[1]) == math.type(val)) |
3246 |
+ assert(func() == val) |
3247 |
+end |
3248 |
+checkK(function () return 0.0 end, 0.0) |
3249 |
+checkK(function () return 0 end, 0) |
3250 |
+checkK(function () return -0//1 end, 0) |
3251 |
+checkK(function () return 3^-1 end, 1/3) |
3252 |
+checkK(function () return (1 + 1)^(50 + 50) end, 2^100) |
3253 |
+checkK(function () return (-2)^(31 - 2) end, -0x20000000 + 0.0) |
3254 |
+checkK(function () return (-3^0 + 5) // 3.0 end, 1.0) |
3255 |
+checkK(function () return -3 % 5 end, 2) |
3256 |
+checkK(function () return -((2.0^8 + -(-1)) % 8)/2 * 4 - 3 end, -5.0) |
3257 |
+checkK(function () return -((2^8 + -(-1)) % 8)//2 * 4 - 3 end, -7.0) |
3258 |
+checkK(function () return 0xF0.0 | 0xCC.0 ~ 0xAA & 0xFD end, 0xF4) |
3259 |
+checkK(function () return ~(~0xFF0 | 0xFF0) end, 0) |
3260 |
+checkK(function () return ~~-100024.0 end, -100024) |
3261 |
+checkK(function () return ((100 << 6) << -4) >> 2 end, 100) |
3262 |
+ |
3263 |
+ |
3264 |
+-- no foldings |
3265 |
+check(function () return -0.0 end, 'LOADK', 'UNM', 'RETURN') |
3266 |
+check(function () return 3/0 end, 'DIV', 'RETURN') |
3267 |
+check(function () return 0%0 end, 'MOD', 'RETURN') |
3268 |
+check(function () return -4//0 end, 'IDIV', 'RETURN') |
3269 |
+ |
3270 |
+-- bug in constant folding for 5.1 |
3271 |
+check(function () return -nil end, 'LOADNIL', 'UNM', 'RETURN') |
3272 |
+ |
3273 |
+ |
3274 |
+check(function () |
3275 |
+ local a,b,c |
3276 |
+ b[c], a = c, b |
3277 |
+ b[a], a = c, b |
3278 |
+ a, b = c, a |
3279 |
+ a = a |
3280 |
+end, |
3281 |
+ 'LOADNIL', |
3282 |
+ 'MOVE', 'MOVE', 'SETTABLE', |
3283 |
+ 'MOVE', 'MOVE', 'MOVE', 'SETTABLE', |
3284 |
+ 'MOVE', 'MOVE', 'MOVE', |
3285 |
+ -- no code for a = a |
3286 |
+ 'RETURN') |
3287 |
+ |
3288 |
+ |
3289 |
+-- x == nil , x ~= nil |
3290 |
+checkequal(function () if (a==nil) then a=1 end; if a~=nil then a=1 end end, |
3291 |
+ function () if (a==9) then a=1 end; if a~=9 then a=1 end end) |
3292 |
+ |
3293 |
+check(function () if a==nil then a='a' end end, |
3294 |
+'GETTABUP', 'EQ', 'JMP', 'SETTABUP', 'RETURN') |
3295 |
+ |
3296 |
+-- de morgan |
3297 |
+checkequal(function () local a; if not (a or b) then b=a end end, |
3298 |
+ function () local a; if (not a and not b) then b=a end end) |
3299 |
+ |
3300 |
+checkequal(function (l) local a; return 0 <= a and a <= l end, |
3301 |
+ function (l) local a; return not (not(a >= 0) or not(a <= l)) end) |
3302 |
+ |
3303 |
+ |
3304 |
+-- if-goto optimizations |
3305 |
+check(function (a, b, c, d, e) |
3306 |
+ if a == b then goto l1 |
3307 |
+ elseif a == c then goto l2 |
3308 |
+ elseif a == d then goto l2 |
3309 |
+ else if a == e then goto l3 |
3310 |
+ else goto l3 |
3311 |
+ end |
3312 |
+ end |
3313 |
+ ::l1:: ::l2:: ::l3:: ::l4:: |
3314 |
+end, 'EQ', 'JMP', 'EQ', 'JMP', 'EQ', 'JMP', 'EQ', 'JMP', 'JMP', 'RETURN') |
3315 |
+ |
3316 |
+checkequal( |
3317 |
+function (a) while a < 10 do a = a + 1 end end, |
3318 |
+function (a) ::L2:: if not(a < 10) then goto L1 end; a = a + 1; |
3319 |
+ goto L2; ::L1:: end |
3320 |
+) |
3321 |
+ |
3322 |
+checkequal( |
3323 |
+function (a) while a < 10 do a = a + 1 end end, |
3324 |
+function (a) while true do if not(a < 10) then break end; a = a + 1; end end |
3325 |
+) |
3326 |
+ |
3327 |
+print 'OK' |
3328 |
+ |
3329 |
|
3330 |
diff --git a/tests/constructs.lua b/tests/constructs.lua |
3331 |
new file mode 100644 |
3332 |
index 0000000..cebd257 |
3333 |
--- /dev/null |
3334 |
+++ b/tests/constructs.lua |
3335 |
@@ -0,0 +1,313 @@ |
3336 |
+-- $Id: constructs.lua,v 1.41 2016/11/07 13:11:28 roberto Exp $ |
3337 |
+-- See Copyright Notice in file all.lua |
3338 |
+ |
3339 |
+;;print "testing syntax";; |
3340 |
+ |
3341 |
+local debug = require "debug" |
3342 |
+ |
3343 |
+ |
3344 |
+local function checkload (s, msg) |
3345 |
+ assert(string.find(select(2, load(s)), msg)) |
3346 |
+end |
3347 |
+ |
3348 |
+-- testing semicollons |
3349 |
+do ;;; end |
3350 |
+; do ; a = 3; assert(a == 3) end; |
3351 |
+; |
3352 |
+ |
3353 |
+ |
3354 |
+-- invalid operations should not raise errors when not executed |
3355 |
+if false then a = 3 // 0; a = 0 % 0 end |
3356 |
+ |
3357 |
+ |
3358 |
+-- testing priorities |
3359 |
+ |
3360 |
+assert(2^3^2 == 2^(3^2)); |
3361 |
+assert(2^3*4 == (2^3)*4); |
3362 |
+assert(2.0^-2 == 1/4 and -2^- -2 == - - -4); |
3363 |
+assert(not nil and 2 and not(2>3 or 3<2)); |
3364 |
+assert(-3-1-5 == 0+0-9); |
3365 |
+assert(-2^2 == -4 and (-2)^2 == 4 and 2*2-3-1 == 0); |
3366 |
+assert(-3%5 == 2 and -3+5 == 2) |
3367 |
+assert(2*1+3/3 == 3 and 1+2 .. 3*1 == "33"); |
3368 |
+assert(not(2+1 > 3*1) and "a".."b" > "a"); |
3369 |
+ |
3370 |
+assert("7" .. 3 << 1 == 146) |
3371 |
+assert(10 >> 1 .. "9" == 0) |
3372 |
+assert(10 | 1 .. "9" == 27) |
3373 |
+ |
3374 |
+assert(0xF0 | 0xCC ~ 0xAA & 0xFD == 0xF4) |
3375 |
+assert(0xFD & 0xAA ~ 0xCC | 0xF0 == 0xF4) |
3376 |
+assert(0xF0 & 0x0F + 1 == 0x10) |
3377 |
+ |
3378 |
+assert(3^4//2^3//5 == 2) |
3379 |
+ |
3380 |
+assert(-3+4*5//2^3^2//9+4%10/3 == (-3)+(((4*5)//(2^(3^2)))//9)+((4%10)/3)) |
3381 |
+ |
3382 |
+assert(not ((true or false) and nil)) |
3383 |
+assert( true or false and nil) |
3384 |
+ |
3385 |
+-- old bug |
3386 |
+assert((((1 or false) and true) or false) == true) |
3387 |
+assert((((nil and true) or false) and true) == false) |
3388 |
+ |
3389 |
+local a,b = 1,nil; |
3390 |
+assert(-(1 or 2) == -1 and (1 and 2)+(-1.25 or -4) == 0.75); |
3391 |
+x = ((b or a)+1 == 2 and (10 or a)+1 == 11); assert(x); |
3392 |
+x = (((2<3) or 1) == true and (2<3 and 4) == 4); assert(x); |
3393 |
+ |
3394 |
+x,y=1,2; |
3395 |
+assert((x>y) and x or y == 2); |
3396 |
+x,y=2,1; |
3397 |
+assert((x>y) and x or y == 2); |
3398 |
+ |
3399 |
+assert(1234567890 == tonumber('1234567890') and 1234567890+1 == 1234567891) |
3400 |
+ |
3401 |
+ |
3402 |
+-- silly loops |
3403 |
+repeat until 1; repeat until true; |
3404 |
+while false do end; while nil do end; |
3405 |
+ |
3406 |
+do -- test old bug (first name could not be an `upvalue') |
3407 |
+ local a; function f(x) x={a=1}; x={x=1}; x={G=1} end |
3408 |
+end |
3409 |
+ |
3410 |
+function f (i) |
3411 |
+ if type(i) ~= 'number' then return i,'jojo'; end; |
3412 |
+ if i > 0 then return i, f(i-1); end; |
3413 |
+end |
3414 |
+ |
3415 |
+x = {f(3), f(5), f(10);}; |
3416 |
+assert(x[1] == 3 and x[2] == 5 and x[3] == 10 and x[4] == 9 and x[12] == 1); |
3417 |
+assert(x[nil] == nil) |
3418 |
+x = {f'alo', f'xixi', nil}; |
3419 |
+assert(x[1] == 'alo' and x[2] == 'xixi' and x[3] == nil); |
3420 |
+x = {f'alo'..'xixi'}; |
3421 |
+assert(x[1] == 'aloxixi') |
3422 |
+x = {f{}} |
3423 |
+assert(x[2] == 'jojo' and type(x[1]) == 'table') |
3424 |
+ |
3425 |
+ |
3426 |
+local f = function (i) |
3427 |
+ if i < 10 then return 'a'; |
3428 |
+ elseif i < 20 then return 'b'; |
3429 |
+ elseif i < 30 then return 'c'; |
3430 |
+ end; |
3431 |
+end |
3432 |
+ |
3433 |
+assert(f(3) == 'a' and f(12) == 'b' and f(26) == 'c' and f(100) == nil) |
3434 |
+ |
3435 |
+for i=1,1000 do break; end; |
3436 |
+n=100; |
3437 |
+i=3; |
3438 |
+t = {}; |
3439 |
+a=nil |
3440 |
+while not a do |
3441 |
+ a=0; for i=1,n do for i=i,1,-1 do a=a+1; t[i]=1; end; end; |
3442 |
+end |
3443 |
+assert(a == n*(n+1)/2 and i==3); |
3444 |
+assert(t[1] and t[n] and not t[0] and not t[n+1]) |
3445 |
+ |
3446 |
+function f(b) |
3447 |
+ local x = 1; |
3448 |
+ repeat |
3449 |
+ local a; |
3450 |
+ if b==1 then local b=1; x=10; break |
3451 |
+ elseif b==2 then x=20; break; |
3452 |
+ elseif b==3 then x=30; |
3453 |
+ else local a,b,c,d=math.sin(1); x=x+1; |
3454 |
+ end |
3455 |
+ until x>=12; |
3456 |
+ return x; |
3457 |
+end; |
3458 |
+ |
3459 |
+assert(f(1) == 10 and f(2) == 20 and f(3) == 30 and f(4)==12) |
3460 |
+ |
3461 |
+ |
3462 |
+local f = function (i) |
3463 |
+ if i < 10 then return 'a' |
3464 |
+ elseif i < 20 then return 'b' |
3465 |
+ elseif i < 30 then return 'c' |
3466 |
+ else return 8 |
3467 |
+ end |
3468 |
+end |
3469 |
+ |
3470 |
+assert(f(3) == 'a' and f(12) == 'b' and f(26) == 'c' and f(100) == 8) |
3471 |
+ |
3472 |
+local a, b = nil, 23 |
3473 |
+x = {f(100)*2+3 or a, a or b+2} |
3474 |
+assert(x[1] == 19 and x[2] == 25) |
3475 |
+x = {f=2+3 or a, a = b+2} |
3476 |
+assert(x.f == 5 and x.a == 25) |
3477 |
+ |
3478 |
+a={y=1} |
3479 |
+x = {a.y} |
3480 |
+assert(x[1] == 1) |
3481 |
+ |
3482 |
+function f(i) |
3483 |
+ while 1 do |
3484 |
+ if i>0 then i=i-1; |
3485 |
+ else return; end; |
3486 |
+ end; |
3487 |
+end; |
3488 |
+ |
3489 |
+function g(i) |
3490 |
+ while 1 do |
3491 |
+ if i>0 then i=i-1 |
3492 |
+ else return end |
3493 |
+ end |
3494 |
+end |
3495 |
+ |
3496 |
+f(10); g(10); |
3497 |
+ |
3498 |
+do |
3499 |
+ function f () return 1,2,3; end |
3500 |
+ local a, b, c = f(); |
3501 |
+ assert(a==1 and b==2 and c==3) |
3502 |
+ a, b, c = (f()); |
3503 |
+ assert(a==1 and b==nil and c==nil) |
3504 |
+end |
3505 |
+ |
3506 |
+local a,b = 3 and f(); |
3507 |
+assert(a==1 and b==nil) |
3508 |
+ |
3509 |
+function g() f(); return; end; |
3510 |
+assert(g() == nil) |
3511 |
+function g() return nil or f() end |
3512 |
+a,b = g() |
3513 |
+assert(a==1 and b==nil) |
3514 |
+ |
3515 |
+print'+'; |
3516 |
+ |
3517 |
+ |
3518 |
+f = [[ |
3519 |
+return function ( a , b , c , d , e ) |
3520 |
+ local x = a >= b or c or ( d and e ) or nil |
3521 |
+ return x |
3522 |
+end , { a = 1 , b = 2 >= 1 , } or { 1 }; |
3523 |
+]] |
3524 |
+f = string.gsub(f, "%s+", "\n"); -- force a SETLINE between opcodes |
3525 |
+f,a = load(f)(); |
3526 |
+assert(a.a == 1 and a.b) |
3527 |
+ |
3528 |
+function g (a,b,c,d,e) |
3529 |
+ if not (a>=b or c or d and e or nil) then return 0; else return 1; end; |
3530 |
+end |
3531 |
+ |
3532 |
+function h (a,b,c,d,e) |
3533 |
+ while (a>=b or c or (d and e) or nil) do return 1; end; |
3534 |
+ return 0; |
3535 |
+end; |
3536 |
+ |
3537 |
+assert(f(2,1) == true and g(2,1) == 1 and h(2,1) == 1) |
3538 |
+assert(f(1,2,'a') == 'a' and g(1,2,'a') == 1 and h(1,2,'a') == 1) |
3539 |
+assert(f(1,2,'a') |
3540 |
+~= -- force SETLINE before nil |
3541 |
+nil, "") |
3542 |
+assert(f(1,2,'a') == 'a' and g(1,2,'a') == 1 and h(1,2,'a') == 1) |
3543 |
+assert(f(1,2,nil,1,'x') == 'x' and g(1,2,nil,1,'x') == 1 and |
3544 |
+ h(1,2,nil,1,'x') == 1) |
3545 |
+assert(f(1,2,nil,nil,'x') == nil and g(1,2,nil,nil,'x') == 0 and |
3546 |
+ h(1,2,nil,nil,'x') == 0) |
3547 |
+assert(f(1,2,nil,1,nil) == nil and g(1,2,nil,1,nil) == 0 and |
3548 |
+ h(1,2,nil,1,nil) == 0) |
3549 |
+ |
3550 |
+assert(1 and 2<3 == true and 2<3 and 'a'<'b' == true) |
3551 |
+x = 2<3 and not 3; assert(x==false) |
3552 |
+x = 2<1 or (2>1 and 'a'); assert(x=='a') |
3553 |
+ |
3554 |
+ |
3555 |
+do |
3556 |
+ local a; if nil then a=1; else a=2; end; -- this nil comes as PUSHNIL 2 |
3557 |
+ assert(a==2) |
3558 |
+end |
3559 |
+ |
3560 |
+function F(a) |
3561 |
+ assert(debug.getinfo(1, "n").name == 'F') |
3562 |
+ return a,2,3 |
3563 |
+end |
3564 |
+ |
3565 |
+a,b = F(1)~=nil; assert(a == true and b == nil); |
3566 |
+a,b = F(nil)==nil; assert(a == true and b == nil) |
3567 |
+ |
3568 |
+---------------------------------------------------------------- |
3569 |
+------------------------------------------------------------------ |
3570 |
+ |
3571 |
+-- sometimes will be 0, sometimes will not... |
3572 |
+_ENV.GLOB1 = math.floor(os.time()) % 2 |
3573 |
+ |
3574 |
+-- basic expressions with their respective values |
3575 |
+local basiccases = { |
3576 |
+ {"nil", nil}, |
3577 |
+ {"false", false}, |
3578 |
+ {"true", true}, |
3579 |
+ {"10", 10}, |
3580 |
+ {"(0==_ENV.GLOB1)", 0 == _ENV.GLOB1}, |
3581 |
+} |
3582 |
+ |
3583 |
+print('testing short-circuit optimizations (' .. _ENV.GLOB1 .. ')') |
3584 |
+ |
3585 |
+ |
3586 |
+-- operators with their respective values |
3587 |
+local binops = { |
3588 |
+ {" and ", function (a,b) if not a then return a else return b end end}, |
3589 |
+ {" or ", function (a,b) if a then return a else return b end end}, |
3590 |
+} |
3591 |
+ |
3592 |
+local cases = {} |
3593 |
+ |
3594 |
+-- creates all combinations of '(cases[i] op cases[n-i])' plus |
3595 |
+-- 'not(cases[i] op cases[n-i])' (syntax + value) |
3596 |
+local function createcases (n) |
3597 |
+ local res = {} |
3598 |
+ for i = 1, n - 1 do |
3599 |
+ for _, v1 in ipairs(cases[i]) do |
3600 |
+ for _, v2 in ipairs(cases[n - i]) do |
3601 |
+ for _, op in ipairs(binops) do |
3602 |
+ local t = { |
3603 |
+ "(" .. v1[1] .. op[1] .. v2[1] .. ")", |
3604 |
+ op[2](v1[2], v2[2]) |
3605 |
+ } |
3606 |
+ res[#res + 1] = t |
3607 |
+ res[#res + 1] = {"not" .. t[1], not t[2]} |
3608 |
+ end |
3609 |
+ end |
3610 |
+ end |
3611 |
+ end |
3612 |
+ return res |
3613 |
+end |
3614 |
+ |
3615 |
+-- do not do too many combinations for soft tests |
3616 |
+local level = _soft and 3 or 4 |
3617 |
+ |
3618 |
+cases[1] = basiccases |
3619 |
+for i = 2, level do cases[i] = createcases(i) end |
3620 |
+print("+") |
3621 |
+ |
3622 |
+local prog = [[if %s then IX = true end; return %s]] |
3623 |
+ |
3624 |
+local i = 0 |
3625 |
+for n = 1, level do |
3626 |
+ for _, v in pairs(cases[n]) do |
3627 |
+ local s = v[1] |
3628 |
+ local p = load(string.format(prog, s, s), "") |
3629 |
+ IX = false |
3630 |
+ assert(p() == v[2] and IX == not not v[2]) |
3631 |
+ i = i + 1 |
3632 |
+ if i % 60000 == 0 then print('+') end |
3633 |
+ end |
3634 |
+end |
3635 |
+------------------------------------------------------------------ |
3636 |
+ |
3637 |
+-- testing some syntax errors (chosen through 'gcov') |
3638 |
+checkload("for x do", "expected") |
3639 |
+checkload("x:call", "expected") |
3640 |
+ |
3641 |
+if not _soft then |
3642 |
+ -- control structure too long |
3643 |
+ local s = string.rep("a = a + 1\n", 2^18) |
3644 |
+ s = "while true do " .. s .. "end" |
3645 |
+ checkload(s, "too long") |
3646 |
+end |
3647 |
+ |
3648 |
+print'OK' |
3649 |
|
3650 |
diff --git a/tests/coroutine.lua b/tests/coroutine.lua |
3651 |
new file mode 100644 |
3652 |
index 0000000..c8fc857 |
3653 |
--- /dev/null |
3654 |
+++ b/tests/coroutine.lua |
3655 |
@@ -0,0 +1,874 @@ |
3656 |
+-- $Id: coroutine.lua,v 1.42 2016/11/07 13:03:20 roberto Exp $ |
3657 |
+-- See Copyright Notice in file all.lua |
3658 |
+ |
3659 |
+print "testing coroutines" |
3660 |
+ |
3661 |
+local debug = require'debug' |
3662 |
+ |
3663 |
+local f |
3664 |
+ |
3665 |
+local main, ismain = coroutine.running() |
3666 |
+assert(type(main) == "thread" and ismain) |
3667 |
+assert(not coroutine.resume(main)) |
3668 |
+assert(not coroutine.isyieldable()) |
3669 |
+assert(not pcall(coroutine.yield)) |
3670 |
+ |
3671 |
+ |
3672 |
+-- trivial errors |
3673 |
+assert(not pcall(coroutine.resume, 0)) |
3674 |
+assert(not pcall(coroutine.status, 0)) |
3675 |
+ |
3676 |
+ |
3677 |
+-- tests for multiple yield/resume arguments |
3678 |
+ |
3679 |
+local function eqtab (t1, t2) |
3680 |
+ assert(#t1 == #t2) |
3681 |
+ for i = 1, #t1 do |
3682 |
+ local v = t1[i] |
3683 |
+ assert(t2[i] == v) |
3684 |
+ end |
3685 |
+end |
3686 |
+ |
3687 |
+_G.x = nil -- declare x |
3688 |
+function foo (a, ...) |
3689 |
+ local x, y = coroutine.running() |
3690 |
+ assert(x == f and y == false) |
3691 |
+ -- next call should not corrupt coroutine (but must fail, |
3692 |
+ -- as it attempts to resume the running coroutine) |
3693 |
+ assert(coroutine.resume(f) == false) |
3694 |
+ assert(coroutine.status(f) == "running") |
3695 |
+ local arg = {...} |
3696 |
+ assert(coroutine.isyieldable()) |
3697 |
+ for i=1,#arg do |
3698 |
+ _G.x = {coroutine.yield(table.unpack(arg[i]))} |
3699 |
+ end |
3700 |
+ return table.unpack(a) |
3701 |
+end |
3702 |
+ |
3703 |
+f = coroutine.create(foo) |
3704 |
+assert(type(f) == "thread" and coroutine.status(f) == "suspended") |
3705 |
+assert(string.find(tostring(f), "thread")) |
3706 |
+local s,a,b,c,d |
3707 |
+s,a,b,c,d = coroutine.resume(f, {1,2,3}, {}, {1}, {'a', 'b', 'c'}) |
3708 |
+assert(s and a == nil and coroutine.status(f) == "suspended") |
3709 |
+s,a,b,c,d = coroutine.resume(f) |
3710 |
+eqtab(_G.x, {}) |
3711 |
+assert(s and a == 1 and b == nil) |
3712 |
+s,a,b,c,d = coroutine.resume(f, 1, 2, 3) |
3713 |
+eqtab(_G.x, {1, 2, 3}) |
3714 |
+assert(s and a == 'a' and b == 'b' and c == 'c' and d == nil) |
3715 |
+s,a,b,c,d = coroutine.resume(f, "xuxu") |
3716 |
+eqtab(_G.x, {"xuxu"}) |
3717 |
+assert(s and a == 1 and b == 2 and c == 3 and d == nil) |
3718 |
+assert(coroutine.status(f) == "dead") |
3719 |
+s, a = coroutine.resume(f, "xuxu") |
3720 |
+assert(not s and string.find(a, "dead") and coroutine.status(f) == "dead") |
3721 |
+ |
3722 |
+ |
3723 |
+-- yields in tail calls |
3724 |
+local function foo (i) return coroutine.yield(i) end |
3725 |
+f = coroutine.wrap(function () |
3726 |
+ for i=1,10 do |
3727 |
+ assert(foo(i) == _G.x) |
3728 |
+ end |
3729 |
+ return 'a' |
3730 |
+end) |
3731 |
+for i=1,10 do _G.x = i; assert(f(i) == i) end |
3732 |
+_G.x = 'xuxu'; assert(f('xuxu') == 'a') |
3733 |
+ |
3734 |
+-- recursive |
3735 |
+function pf (n, i) |
3736 |
+ coroutine.yield(n) |
3737 |
+ pf(n*i, i+1) |
3738 |
+end |
3739 |
+ |
3740 |
+f = coroutine.wrap(pf) |
3741 |
+local s=1 |
3742 |
+for i=1,10 do |
3743 |
+ assert(f(1, 1) == s) |
3744 |
+ s = s*i |
3745 |
+end |
3746 |
+ |
3747 |
+-- sieve |
3748 |
+function gen (n) |
3749 |
+ return coroutine.wrap(function () |
3750 |
+ for i=2,n do coroutine.yield(i) end |
3751 |
+ end) |
3752 |
+end |
3753 |
+ |
3754 |
+ |
3755 |
+function filter (p, g) |
3756 |
+ return coroutine.wrap(function () |
3757 |
+ while 1 do |
3758 |
+ local n = g() |
3759 |
+ if n == nil then return end |
3760 |
+ if math.fmod(n, p) ~= 0 then coroutine.yield(n) end |
3761 |
+ end |
3762 |
+ end) |
3763 |
+end |
3764 |
+ |
3765 |
+local x = gen(100) |
3766 |
+local a = {} |
3767 |
+while 1 do |
3768 |
+ local n = x() |
3769 |
+ if n == nil then break end |
3770 |
+ table.insert(a, n) |
3771 |
+ x = filter(n, x) |
3772 |
+end |
3773 |
+ |
3774 |
+assert(#a == 25 and a[#a] == 97) |
3775 |
+x, a = nil |
3776 |
+ |
3777 |
+-- yielding across C boundaries |
3778 |
+ |
3779 |
+co = coroutine.wrap(function() |
3780 |
+ assert(not pcall(table.sort,{1,2,3}, coroutine.yield)) |
3781 |
+ assert(coroutine.isyieldable()) |
3782 |
+ coroutine.yield(20) |
3783 |
+ return 30 |
3784 |
+ end) |
3785 |
+ |
3786 |
+assert(co() == 20) |
3787 |
+assert(co() == 30) |
3788 |
+ |
3789 |
+ |
3790 |
+local f = function (s, i) return coroutine.yield(i) end |
3791 |
+ |
3792 |
+local f1 = coroutine.wrap(function () |
3793 |
+ return xpcall(pcall, function (...) return ... end, |
3794 |
+ function () |
3795 |
+ local s = 0 |
3796 |
+ for i in f, nil, 1 do pcall(function () s = s + i end) end |
3797 |
+ error({s}) |
3798 |
+ end) |
3799 |
+ end) |
3800 |
+ |
3801 |
+f1() |
3802 |
+for i = 1, 10 do assert(f1(i) == i) end |
3803 |
+local r1, r2, v = f1(nil) |
3804 |
+assert(r1 and not r2 and v[1] == (10 + 1)*10/2) |
3805 |
+ |
3806 |
+ |
3807 |
+function f (a, b) a = coroutine.yield(a); error{a + b} end |
3808 |
+function g(x) return x[1]*2 end |
3809 |
+ |
3810 |
+co = coroutine.wrap(function () |
3811 |
+ coroutine.yield(xpcall(f, g, 10, 20)) |
3812 |
+ end) |
3813 |
+ |
3814 |
+assert(co() == 10) |
3815 |
+r, msg = co(100) |
3816 |
+assert(not r and msg == 240) |
3817 |
+ |
3818 |
+ |
3819 |
+-- unyieldable C call |
3820 |
+do |
3821 |
+ local function f (c) |
3822 |
+ assert(not coroutine.isyieldable()) |
3823 |
+ return c .. c |
3824 |
+ end |
3825 |
+ |
3826 |
+ local co = coroutine.wrap(function (c) |
3827 |
+ assert(coroutine.isyieldable()) |
3828 |
+ local s = string.gsub("a", ".", f) |
3829 |
+ return s |
3830 |
+ end) |
3831 |
+ assert(co() == "aa") |
3832 |
+end |
3833 |
+ |
3834 |
+ |
3835 |
+-- errors in coroutines |
3836 |
+function foo () |
3837 |
+ assert(debug.getinfo(1).currentline == debug.getinfo(foo).linedefined + 1) |
3838 |
+ assert(debug.getinfo(2).currentline == debug.getinfo(goo).linedefined) |
3839 |
+ coroutine.yield(3) |
3840 |
+ error(foo) |
3841 |
+end |
3842 |
+ |
3843 |
+function goo() foo() end |
3844 |
+x = coroutine.wrap(goo) |
3845 |
+assert(x() == 3) |
3846 |
+local a,b = pcall(x) |
3847 |
+assert(not a and b == foo) |
3848 |
+ |
3849 |
+x = coroutine.create(goo) |
3850 |
+a,b = coroutine.resume(x) |
3851 |
+assert(a and b == 3) |
3852 |
+a,b = coroutine.resume(x) |
3853 |
+assert(not a and b == foo and coroutine.status(x) == "dead") |
3854 |
+a,b = coroutine.resume(x) |
3855 |
+assert(not a and string.find(b, "dead") and coroutine.status(x) == "dead") |
3856 |
+ |
3857 |
+ |
3858 |
+-- co-routines x for loop |
3859 |
+function all (a, n, k) |
3860 |
+ if k == 0 then coroutine.yield(a) |
3861 |
+ else |
3862 |
+ for i=1,n do |
3863 |
+ a[k] = i |
3864 |
+ all(a, n, k-1) |
3865 |
+ end |
3866 |
+ end |
3867 |
+end |
3868 |
+ |
3869 |
+local a = 0 |
3870 |
+for t in coroutine.wrap(function () all({}, 5, 4) end) do |
3871 |
+ a = a+1 |
3872 |
+end |
3873 |
+assert(a == 5^4) |
3874 |
+ |
3875 |
+ |
3876 |
+-- access to locals of collected corroutines |
3877 |
+local C = {}; setmetatable(C, {__mode = "kv"}) |
3878 |
+local x = coroutine.wrap (function () |
3879 |
+ local a = 10 |
3880 |
+ local function f () a = a+10; return a end |
3881 |
+ while true do |
3882 |
+ a = a+1 |
3883 |
+ coroutine.yield(f) |
3884 |
+ end |
3885 |
+ end) |
3886 |
+ |
3887 |
+C[1] = x; |
3888 |
+ |
3889 |
+local f = x() |
3890 |
+assert(f() == 21 and x()() == 32 and x() == f) |
3891 |
+x = nil |
3892 |
+collectgarbage() |
3893 |
+assert(C[1] == nil) |
3894 |
+assert(f() == 43 and f() == 53) |
3895 |
+ |
3896 |
+ |
3897 |
+-- old bug: attempt to resume itself |
3898 |
+ |
3899 |
+function co_func (current_co) |
3900 |
+ assert(coroutine.running() == current_co) |
3901 |
+ assert(coroutine.resume(current_co) == false) |
3902 |
+ coroutine.yield(10, 20) |
3903 |
+ assert(coroutine.resume(current_co) == false) |
3904 |
+ coroutine.yield(23) |
3905 |
+ return 10 |
3906 |
+end |
3907 |
+ |
3908 |
+local co = coroutine.create(co_func) |
3909 |
+local a,b,c = coroutine.resume(co, co) |
3910 |
+assert(a == true and b == 10 and c == 20) |
3911 |
+a,b = coroutine.resume(co, co) |
3912 |
+assert(a == true and b == 23) |
3913 |
+a,b = coroutine.resume(co, co) |
3914 |
+assert(a == true and b == 10) |
3915 |
+assert(coroutine.resume(co, co) == false) |
3916 |
+assert(coroutine.resume(co, co) == false) |
3917 |
+ |
3918 |
+ |
3919 |
+-- other old bug when attempting to resume itself |
3920 |
+-- (trigger C-code assertions) |
3921 |
+do |
3922 |
+ local A = coroutine.running() |
3923 |
+ local B = coroutine.create(function() return coroutine.resume(A) end) |
3924 |
+ local st, res = coroutine.resume(B) |
3925 |
+ assert(st == true and res == false) |
3926 |
+ |
3927 |
+ A = coroutine.wrap(function() return pcall(A, 1) end) |
3928 |
+ st, res = A() |
3929 |
+ assert(not st and string.find(res, "non%-suspended")) |
3930 |
+end |
3931 |
+ |
3932 |
+ |
3933 |
+-- attempt to resume 'normal' coroutine |
3934 |
+local co1, co2 |
3935 |
+co1 = coroutine.create(function () return co2() end) |
3936 |
+co2 = coroutine.wrap(function () |
3937 |
+ assert(coroutine.status(co1) == 'normal') |
3938 |
+ assert(not coroutine.resume(co1)) |
3939 |
+ coroutine.yield(3) |
3940 |
+ end) |
3941 |
+ |
3942 |
+a,b = coroutine.resume(co1) |
3943 |
+assert(a and b == 3) |
3944 |
+assert(coroutine.status(co1) == 'dead') |
3945 |
+ |
3946 |
+-- infinite recursion of coroutines |
3947 |
+a = function(a) coroutine.wrap(a)(a) end |
3948 |
+assert(not pcall(a, a)) |
3949 |
+a = nil |
3950 |
+ |
3951 |
+ |
3952 |
+-- access to locals of erroneous coroutines |
3953 |
+local x = coroutine.create (function () |
3954 |
+ local a = 10 |
3955 |
+ _G.f = function () a=a+1; return a end |
3956 |
+ error('x') |
3957 |
+ end) |
3958 |
+ |
3959 |
+assert(not coroutine.resume(x)) |
3960 |
+-- overwrite previous position of local `a' |
3961 |
+assert(not coroutine.resume(x, 1, 1, 1, 1, 1, 1, 1)) |
3962 |
+assert(_G.f() == 11) |
3963 |
+assert(_G.f() == 12) |
3964 |
+ |
3965 |
+ |
3966 |
+if not T then |
3967 |
+ (Message or print)('\n >>> testC not active: skipping yield/hook tests <<<\n') |
3968 |
+else |
3969 |
+ print "testing yields inside hooks" |
3970 |
+ |
3971 |
+ local turn |
3972 |
+ |
3973 |
+ function fact (t, x) |
3974 |
+ assert(turn == t) |
3975 |
+ if x == 0 then return 1 |
3976 |
+ else return x*fact(t, x-1) |
3977 |
+ end |
3978 |
+ end |
3979 |
+ |
3980 |
+ local A, B = 0, 0 |
3981 |
+ |
3982 |
+ local x = coroutine.create(function () |
3983 |
+ T.sethook("yield 0", "", 2) |
3984 |
+ A = fact("A", 6) |
3985 |
+ end) |
3986 |
+ |
3987 |
+ local y = coroutine.create(function () |
3988 |
+ T.sethook("yield 0", "", 3) |
3989 |
+ B = fact("B", 7) |
3990 |
+ end) |
3991 |
+ |
3992 |
+ while A==0 or B==0 do -- A ~= 0 when 'x' finishes (similar for 'B','y') |
3993 |
+ if A==0 then turn = "A"; assert(T.resume(x)) end |
3994 |
+ if B==0 then turn = "B"; assert(T.resume(y)) end |
3995 |
+ end |
3996 |
+ |
3997 |
+ assert(B // A == 7) -- fact(7) // fact(6) |
3998 |
+ |
3999 |
+ local line = debug.getinfo(1, "l").currentline + 2 -- get line number |
4000 |
+ local function foo () |
4001 |
+ local x = 10 --<< this line is 'line' |
4002 |
+ x = x + 10 |
4003 |
+ _G.XX = x |
4004 |
+ end |
4005 |
+ |
4006 |
+ -- testing yields in line hook |
4007 |
+ local co = coroutine.wrap(function () |
4008 |
+ T.sethook("setglobal X; yield 0", "l", 0); foo(); return 10 end) |
4009 |
+ |
4010 |
+ _G.XX = nil; |
4011 |
+ _G.X = nil; co(); assert(_G.X == line) |
4012 |
+ _G.X = nil; co(); assert(_G.X == line + 1) |
4013 |
+ _G.X = nil; co(); assert(_G.X == line + 2 and _G.XX == nil) |
4014 |
+ _G.X = nil; co(); assert(_G.X == line + 3 and _G.XX == 20) |
4015 |
+ assert(co() == 10) |
4016 |
+ |
4017 |
+ -- testing yields in count hook |
4018 |
+ co = coroutine.wrap(function () |
4019 |
+ T.sethook("yield 0", "", 1); foo(); return 10 end) |
4020 |
+ |
4021 |
+ _G.XX = nil; |
4022 |
+ local c = 0 |
4023 |
+ repeat c = c + 1; local a = co() until a == 10 |
4024 |
+ assert(_G.XX == 20 and c >= 5) |
4025 |
+ |
4026 |
+ co = coroutine.wrap(function () |
4027 |
+ T.sethook("yield 0", "", 2); foo(); return 10 end) |
4028 |
+ |
4029 |
+ _G.XX = nil; |
4030 |
+ local c = 0 |
4031 |
+ repeat c = c + 1; local a = co() until a == 10 |
4032 |
+ assert(_G.XX == 20 and c >= 5) |
4033 |
+ _G.X = nil; _G.XX = nil |
4034 |
+ |
4035 |
+ do |
4036 |
+ -- testing debug library on a coroutine suspended inside a hook |
4037 |
+ -- (bug in 5.2/5.3) |
4038 |
+ c = coroutine.create(function (a, ...) |
4039 |
+ T.sethook("yield 0", "l") -- will yield on next two lines |
4040 |
+ assert(a == 10) |
4041 |
+ return ... |
4042 |
+ end) |
4043 |
+ |
4044 |
+ assert(coroutine.resume(c, 1, 2, 3)) -- start coroutine |
4045 |
+ local n,v = debug.getlocal(c, 0, 1) -- check its local |
4046 |
+ assert(n == "a" and v == 1) |
4047 |
+ n,v = debug.getlocal(c, 0, -1) -- check varargs |
4048 |
+ assert(v == 2) |
4049 |
+ n,v = debug.getlocal(c, 0, -2) |
4050 |
+ assert(v == 3) |
4051 |
+ assert(debug.setlocal(c, 0, 1, 10)) -- test 'setlocal' |
4052 |
+ assert(debug.setlocal(c, 0, -2, 20)) |
4053 |
+ local t = debug.getinfo(c, 0) -- test 'getinfo' |
4054 |
+ assert(t.currentline == t.linedefined + 1) |
4055 |
+ assert(not debug.getinfo(c, 1)) -- no other level |
4056 |
+ assert(coroutine.resume(c)) -- run next line |
4057 |
+ v = {coroutine.resume(c)} -- finish coroutine |
4058 |
+ assert(v[1] == true and v[2] == 2 and v[3] == 20 and v[4] == nil) |
4059 |
+ assert(not coroutine.resume(c)) |
4060 |
+ end |
4061 |
+ |
4062 |
+ do |
4063 |
+ -- testing debug library on last function in a suspended coroutine |
4064 |
+ -- (bug in 5.2/5.3) |
4065 |
+ local c = coroutine.create(function () T.testC("yield 1", 10, 20) end) |
4066 |
+ local a, b = coroutine.resume(c) |
4067 |
+ assert(a and b == 20) |
4068 |
+ assert(debug.getinfo(c, 0).linedefined == -1) |
4069 |
+ a, b = debug.getlocal(c, 0, 2) |
4070 |
+ assert(b == 10) |
4071 |
+ end |
4072 |
+ |
4073 |
+ |
4074 |
+ print "testing coroutine API" |
4075 |
+ |
4076 |
+ -- reusing a thread |
4077 |
+ assert(T.testC([[ |
4078 |
+ newthread # create thread |
4079 |
+ pushvalue 2 # push body |
4080 |
+ pushstring 'a a a' # push argument |
4081 |
+ xmove 0 3 2 # move values to new thread |
4082 |
+ resume -1, 1 # call it first time |
4083 |
+ pushstatus |
4084 |
+ xmove 3 0 0 # move results back to stack |
4085 |
+ setglobal X # result |
4086 |
+ setglobal Y # status |
4087 |
+ pushvalue 2 # push body (to call it again) |
4088 |
+ pushstring 'b b b' |
4089 |
+ xmove 0 3 2 |
4090 |
+ resume -1, 1 # call it again |
4091 |
+ pushstatus |
4092 |
+ xmove 3 0 0 |
4093 |
+ return 1 # return result |
4094 |
+ ]], function (...) return ... end) == 'b b b') |
4095 |
+ |
4096 |
+ assert(X == 'a a a' and Y == 'OK') |
4097 |
+ |
4098 |
+ |
4099 |
+ -- resuming running coroutine |
4100 |
+ C = coroutine.create(function () |
4101 |
+ return T.testC([[ |
4102 |
+ pushnum 10; |
4103 |
+ pushnum 20; |
4104 |
+ resume -3 2; |
4105 |
+ pushstatus |
4106 |
+ gettop; |
4107 |
+ return 3]], C) |
4108 |
+ end) |
4109 |
+ local a, b, c, d = coroutine.resume(C) |
4110 |
+ assert(a == true and string.find(b, "non%-suspended") and |
4111 |
+ c == "ERRRUN" and d == 4) |
4112 |
+ |
4113 |
+ a, b, c, d = T.testC([[ |
4114 |
+ rawgeti R 1 # get main thread |
4115 |
+ pushnum 10; |
4116 |
+ pushnum 20; |
4117 |
+ resume -3 2; |
4118 |
+ pushstatus |
4119 |
+ gettop; |
4120 |
+ return 4]]) |
4121 |
+ assert(a == coroutine.running() and string.find(b, "non%-suspended") and |
4122 |
+ c == "ERRRUN" and d == 4) |
4123 |
+ |
4124 |
+ |
4125 |
+ -- using a main thread as a coroutine |
4126 |
+ local state = T.newstate() |
4127 |
+ T.loadlib(state) |
4128 |
+ |
4129 |
+ assert(T.doremote(state, [[ |
4130 |
+ coroutine = require'coroutine'; |
4131 |
+ X = function (x) coroutine.yield(x, 'BB'); return 'CC' end; |
4132 |
+ return 'ok']])) |
4133 |
+ |
4134 |
+ t = table.pack(T.testC(state, [[ |
4135 |
+ rawgeti R 1 # get main thread |
4136 |
+ pushstring 'XX' |
4137 |
+ getglobal X # get function for body |
4138 |
+ pushstring AA # arg |
4139 |
+ resume 1 1 # 'resume' shadows previous stack! |
4140 |
+ gettop |
4141 |
+ setglobal T # top |
4142 |
+ setglobal B # second yielded value |
4143 |
+ setglobal A # fist yielded value |
4144 |
+ rawgeti R 1 # get main thread |
4145 |
+ pushnum 5 # arg (noise) |
4146 |
+ resume 1 1 # after coroutine ends, previous stack is back |
4147 |
+ pushstatus |
4148 |
+ return * |
4149 |
+ ]])) |
4150 |
+ assert(t.n == 4 and t[2] == 'XX' and t[3] == 'CC' and t[4] == 'OK') |
4151 |
+ assert(T.doremote(state, "return T") == '2') |
4152 |
+ assert(T.doremote(state, "return A") == 'AA') |
4153 |
+ assert(T.doremote(state, "return B") == 'BB') |
4154 |
+ |
4155 |
+ T.closestate(state) |
4156 |
+ |
4157 |
+ print'+' |
4158 |
+ |
4159 |
+end |
4160 |
+ |
4161 |
+ |
4162 |
+-- leaving a pending coroutine open |
4163 |
+_X = coroutine.wrap(function () |
4164 |
+ local a = 10 |
4165 |
+ local x = function () a = a+1 end |
4166 |
+ coroutine.yield() |
4167 |
+ end) |
4168 |
+ |
4169 |
+_X() |
4170 |
+ |
4171 |
+ |
4172 |
+if not _soft then |
4173 |
+ -- bug (stack overflow) |
4174 |
+ local j = 2^9 |
4175 |
+ local lim = 1000000 -- (C stack limit; assume 32-bit machine) |
4176 |
+ local t = {lim - 10, lim - 5, lim - 1, lim, lim + 1} |
4177 |
+ for i = 1, #t do |
4178 |
+ local j = t[i] |
4179 |
+ co = coroutine.create(function() |
4180 |
+ local t = {} |
4181 |
+ for i = 1, j do t[i] = i end |
4182 |
+ return table.unpack(t) |
4183 |
+ end) |
4184 |
+ local r, msg = coroutine.resume(co) |
4185 |
+ assert(not r) |
4186 |
+ end |
4187 |
+ co = nil |
4188 |
+end |
4189 |
+ |
4190 |
+ |
4191 |
+assert(coroutine.running() == main) |
4192 |
+ |
4193 |
+print"+" |
4194 |
+ |
4195 |
+ |
4196 |
+print"testing yields inside metamethods" |
4197 |
+ |
4198 |
+local mt = { |
4199 |
+ __eq = function(a,b) coroutine.yield(nil, "eq"); return a.x == b.x end, |
4200 |
+ __lt = function(a,b) coroutine.yield(nil, "lt"); return a.x < b.x end, |
4201 |
+ __le = function(a,b) coroutine.yield(nil, "le"); return a - b <= 0 end, |
4202 |
+ __add = function(a,b) coroutine.yield(nil, "add"); return a.x + b.x end, |
4203 |
+ __sub = function(a,b) coroutine.yield(nil, "sub"); return a.x - b.x end, |
4204 |
+ __mod = function(a,b) coroutine.yield(nil, "mod"); return a.x % b.x end, |
4205 |
+ __unm = function(a,b) coroutine.yield(nil, "unm"); return -a.x end, |
4206 |
+ __bnot = function(a,b) coroutine.yield(nil, "bnot"); return ~a.x end, |
4207 |
+ __shl = function(a,b) coroutine.yield(nil, "shl"); return a.x << b.x end, |
4208 |
+ __shr = function(a,b) coroutine.yield(nil, "shr"); return a.x >> b.x end, |
4209 |
+ __band = function(a,b) |
4210 |
+ a = type(a) == "table" and a.x or a |
4211 |
+ b = type(b) == "table" and b.x or b |
4212 |
+ coroutine.yield(nil, "band") |
4213 |
+ return a & b |
4214 |
+ end, |
4215 |
+ __bor = function(a,b) coroutine.yield(nil, "bor"); return a.x | b.x end, |
4216 |
+ __bxor = function(a,b) coroutine.yield(nil, "bxor"); return a.x ~ b.x end, |
4217 |
+ |
4218 |
+ __concat = function(a,b) |
4219 |
+ coroutine.yield(nil, "concat"); |
4220 |
+ a = type(a) == "table" and a.x or a |
4221 |
+ b = type(b) == "table" and b.x or b |
4222 |
+ return a .. b |
4223 |
+ end, |
4224 |
+ __index = function (t,k) coroutine.yield(nil, "idx"); return t.k[k] end, |
4225 |
+ __newindex = function (t,k,v) coroutine.yield(nil, "nidx"); t.k[k] = v end, |
4226 |
+} |
4227 |
+ |
4228 |
+ |
4229 |
+local function new (x) |
4230 |
+ return setmetatable({x = x, k = {}}, mt) |
4231 |
+end |
4232 |
+ |
4233 |
+ |
4234 |
+local a = new(10) |
4235 |
+local b = new(12) |
4236 |
+local c = new"hello" |
4237 |
+ |
4238 |
+local function run (f, t) |
4239 |
+ local i = 1 |
4240 |
+ local c = coroutine.wrap(f) |
4241 |
+ while true do |
4242 |
+ local res, stat = c() |
4243 |
+ if res then assert(t[i] == nil); return res, t end |
4244 |
+ assert(stat == t[i]) |
4245 |
+ i = i + 1 |
4246 |
+ end |
4247 |
+end |
4248 |
+ |
4249 |
+ |
4250 |
+assert(run(function () if (a>=b) then return '>=' else return '<' end end, |
4251 |
+ {"le", "sub"}) == "<") |
4252 |
+-- '<=' using '<' |
4253 |
+mt.__le = nil |
4254 |
+assert(run(function () if (a<=b) then return '<=' else return '>' end end, |
4255 |
+ {"lt"}) == "<=") |
4256 |
+assert(run(function () if (a==b) then return '==' else return '~=' end end, |
4257 |
+ {"eq"}) == "~=") |
4258 |
+ |
4259 |
+assert(run(function () return a & b + a end, {"add", "band"}) == 2) |
4260 |
+ |
4261 |
+assert(run(function () return a % b end, {"mod"}) == 10) |
4262 |
+ |
4263 |
+assert(run(function () return ~a & b end, {"bnot", "band"}) == ~10 & 12) |
4264 |
+assert(run(function () return a | b end, {"bor"}) == 10 | 12) |
4265 |
+assert(run(function () return a ~ b end, {"bxor"}) == 10 ~ 12) |
4266 |
+assert(run(function () return a << b end, {"shl"}) == 10 << 12) |
4267 |
+assert(run(function () return a >> b end, {"shr"}) == 10 >> 12) |
4268 |
+ |
4269 |
+assert(run(function () return a..b end, {"concat"}) == "1012") |
4270 |
+ |
4271 |
+assert(run(function() return a .. b .. c .. a end, |
4272 |
+ {"concat", "concat", "concat"}) == "1012hello10") |
4273 |
+ |
4274 |
+assert(run(function() return "a" .. "b" .. a .. "c" .. c .. b .. "x" end, |
4275 |
+ {"concat", "concat", "concat"}) == "ab10chello12x") |
4276 |
+ |
4277 |
+ |
4278 |
+do -- a few more tests for comparsion operators |
4279 |
+ local mt1 = { |
4280 |
+ __le = function (a,b) |
4281 |
+ coroutine.yield(10) |
4282 |
+ return |
4283 |
+ (type(a) == "table" and a.x or a) <= (type(b) == "table" and b.x or b) |
4284 |
+ end, |
4285 |
+ __lt = function (a,b) |
4286 |
+ coroutine.yield(10) |
4287 |
+ return |
4288 |
+ (type(a) == "table" and a.x or a) < (type(b) == "table" and b.x or b) |
4289 |
+ end, |
4290 |
+ } |
4291 |
+ local mt2 = { __lt = mt1.__lt } -- no __le |
4292 |
+ |
4293 |
+ local function run (f) |
4294 |
+ local co = coroutine.wrap(f) |
4295 |
+ local res |
4296 |
+ repeat |
4297 |
+ res = co() |
4298 |
+ until res ~= 10 |
4299 |
+ return res |
4300 |
+ end |
4301 |
+ |
4302 |
+ local function test () |
4303 |
+ local a1 = setmetatable({x=1}, mt1) |
4304 |
+ local a2 = setmetatable({x=2}, mt2) |
4305 |
+ assert(a1 < a2) |
4306 |
+ assert(a1 <= a2) |
4307 |
+ assert(1 < a2) |
4308 |
+ assert(1 <= a2) |
4309 |
+ assert(2 > a1) |
4310 |
+ assert(2 >= a2) |
4311 |
+ return true |
4312 |
+ end |
4313 |
+ |
4314 |
+ run(test) |
4315 |
+ |
4316 |
+end |
4317 |
+ |
4318 |
+assert(run(function () |
4319 |
+ a.BB = print |
4320 |
+ return a.BB |
4321 |
+ end, {"nidx", "idx"}) == print) |
4322 |
+ |
4323 |
+-- getuptable & setuptable |
4324 |
+do local _ENV = _ENV |
4325 |
+ f = function () AAA = BBB + 1; return AAA end |
4326 |
+end |
4327 |
+g = new(10); g.k.BBB = 10; |
4328 |
+debug.setupvalue(f, 1, g) |
4329 |
+assert(run(f, {"idx", "nidx", "idx"}) == 11) |
4330 |
+assert(g.k.AAA == 11) |
4331 |
+ |
4332 |
+print"+" |
4333 |
+ |
4334 |
+print"testing yields inside 'for' iterators" |
4335 |
+ |
4336 |
+local f = function (s, i) |
4337 |
+ if i%2 == 0 then coroutine.yield(nil, "for") end |
4338 |
+ if i < s then return i + 1 end |
4339 |
+ end |
4340 |
+ |
4341 |
+assert(run(function () |
4342 |
+ local s = 0 |
4343 |
+ for i in f, 4, 0 do s = s + i end |
4344 |
+ return s |
4345 |
+ end, {"for", "for", "for"}) == 10) |
4346 |
+ |
4347 |
+ |
4348 |
+ |
4349 |
+-- tests for coroutine API |
4350 |
+if T==nil then |
4351 |
+ (Message or print)('\n >>> testC not active: skipping coroutine API tests <<<\n') |
4352 |
+ return |
4353 |
+end |
4354 |
+ |
4355 |
+print('testing coroutine API') |
4356 |
+ |
4357 |
+local function apico (...) |
4358 |
+ local x = {...} |
4359 |
+ return coroutine.wrap(function () |
4360 |
+ return T.testC(table.unpack(x)) |
4361 |
+ end) |
4362 |
+end |
4363 |
+ |
4364 |
+local a = {apico( |
4365 |
+[[ |
4366 |
+ pushstring errorcode |
4367 |
+ pcallk 1 0 2; |
4368 |
+ invalid command (should not arrive here) |
4369 |
+]], |
4370 |
+[[return *]], |
4371 |
+"stackmark", |
4372 |
+error |
4373 |
+)()} |
4374 |
+assert(#a == 4 and |
4375 |
+ a[3] == "stackmark" and |
4376 |
+ a[4] == "errorcode" and |
4377 |
+ _G.status == "ERRRUN" and |
4378 |
+ _G.ctx == 2) -- 'ctx' to pcallk |
4379 |
+ |
4380 |
+local co = apico( |
4381 |
+ "pushvalue 2; pushnum 10; pcallk 1 2 3; invalid command;", |
4382 |
+ coroutine.yield, |
4383 |
+ "getglobal status; getglobal ctx; pushvalue 2; pushstring a; pcallk 1 0 4; invalid command", |
4384 |
+ "getglobal status; getglobal ctx; return *") |
4385 |
+ |
4386 |
+assert(co() == 10) |
4387 |
+assert(co(20, 30) == 'a') |
4388 |
+a = {co()} |
4389 |
+assert(#a == 10 and |
4390 |
+ a[2] == coroutine.yield and |
4391 |
+ a[5] == 20 and a[6] == 30 and |
4392 |
+ a[7] == "YIELD" and a[8] == 3 and |
4393 |
+ a[9] == "YIELD" and a[10] == 4) |
4394 |
+assert(not pcall(co)) -- coroutine is dead now |
4395 |
+ |
4396 |
+ |
4397 |
+f = T.makeCfunc("pushnum 3; pushnum 5; yield 1;") |
4398 |
+co = coroutine.wrap(function () |
4399 |
+ assert(f() == 23); assert(f() == 23); return 10 |
4400 |
+end) |
4401 |
+assert(co(23,16) == 5) |
4402 |
+assert(co(23,16) == 5) |
4403 |
+assert(co(23,16) == 10) |
4404 |
+ |
4405 |
+ |
4406 |
+-- testing coroutines with C bodies |
4407 |
+f = T.makeCfunc([[ |
4408 |
+ pushnum 102 |
4409 |
+ yieldk 1 U2 |
4410 |
+ cannot be here! |
4411 |
+]], |
4412 |
+[[ # continuation |
4413 |
+ pushvalue U3 # accessing upvalues inside a continuation |
4414 |
+ pushvalue U4 |
4415 |
+ return * |
4416 |
+]], 23, "huu") |
4417 |
+ |
4418 |
+x = coroutine.wrap(f) |
4419 |
+assert(x() == 102) |
4420 |
+eqtab({x()}, {23, "huu"}) |
4421 |
+ |
4422 |
+ |
4423 |
+f = T.makeCfunc[[pushstring 'a'; pushnum 102; yield 2; ]] |
4424 |
+ |
4425 |
+a, b, c, d = T.testC([[newthread; pushvalue 2; xmove 0 3 1; resume 3 0; |
4426 |
+ pushstatus; xmove 3 0 0; resume 3 0; pushstatus; |
4427 |
+ return 4; ]], f) |
4428 |
+ |
4429 |
+assert(a == 'YIELD' and b == 'a' and c == 102 and d == 'OK') |
4430 |
+ |
4431 |
+ |
4432 |
+-- testing chain of suspendable C calls |
4433 |
+ |
4434 |
+local count = 3 -- number of levels |
4435 |
+ |
4436 |
+f = T.makeCfunc([[ |
4437 |
+ remove 1; # remove argument |
4438 |
+ pushvalue U3; # get selection function |
4439 |
+ call 0 1; # call it (result is 'f' or 'yield') |
4440 |
+ pushstring hello # single argument for selected function |
4441 |
+ pushupvalueindex 2; # index of continuation program |
4442 |
+ callk 1 -1 .; # call selected function |
4443 |
+ errorerror # should never arrive here |
4444 |
+]], |
4445 |
+[[ |
4446 |
+ # continuation program |
4447 |
+ pushnum 34 # return value |
4448 |
+ return * # return all results |
4449 |
+]], |
4450 |
+function () -- selection function |
4451 |
+ count = count - 1 |
4452 |
+ if count == 0 then return coroutine.yield |
4453 |
+ else return f |
4454 |
+ end |
4455 |
+end |
4456 |
+) |
4457 |
+ |
4458 |
+co = coroutine.wrap(function () return f(nil) end) |
4459 |
+assert(co() == "hello") -- argument to 'yield' |
4460 |
+a = {co()} |
4461 |
+-- three '34's (one from each pending C call) |
4462 |
+assert(#a == 3 and a[1] == a[2] and a[2] == a[3] and a[3] == 34) |
4463 |
+ |
4464 |
+ |
4465 |
+-- testing yields with continuations |
4466 |
+ |
4467 |
+co = coroutine.wrap(function (...) return |
4468 |
+ T.testC([[ # initial function |
4469 |
+ yieldk 1 2 |
4470 |
+ cannot be here! |
4471 |
+ ]], |
4472 |
+ [[ # 1st continuation |
4473 |
+ yieldk 0 3 |
4474 |
+ cannot be here! |
4475 |
+ ]], |
4476 |
+ [[ # 2nd continuation |
4477 |
+ yieldk 0 4 |
4478 |
+ cannot be here! |
4479 |
+ ]], |
4480 |
+ [[ # 3th continuation |
4481 |
+ pushvalue 6 # function which is last arg. to 'testC' here |
4482 |
+ pushnum 10; pushnum 20; |
4483 |
+ pcall 2 0 0 # call should throw an error and return to next line |
4484 |
+ pop 1 # remove error message |
4485 |
+ pushvalue 6 |
4486 |
+ getglobal status; getglobal ctx |
4487 |
+ pcallk 2 2 5 # call should throw an error and jump to continuation |
4488 |
+ cannot be here! |
4489 |
+ ]], |
4490 |
+ [[ # 4th (and last) continuation |
4491 |
+ return * |
4492 |
+ ]], |
4493 |
+ -- function called by 3th continuation |
4494 |
+ function (a,b) x=a; y=b; error("errmsg") end, |
4495 |
+ ... |
4496 |
+) |
4497 |
+end) |
4498 |
+ |
4499 |
+local a = {co(3,4,6)} |
4500 |
+assert(a[1] == 6 and a[2] == nil) |
4501 |
+a = {co()}; assert(a[1] == nil and _G.status == "YIELD" and _G.ctx == 2) |
4502 |
+a = {co()}; assert(a[1] == nil and _G.status == "YIELD" and _G.ctx == 3) |
4503 |
+a = {co(7,8)}; |
4504 |
+-- original arguments |
4505 |
+assert(type(a[1]) == 'string' and type(a[2]) == 'string' and |
4506 |
+ type(a[3]) == 'string' and type(a[4]) == 'string' and |
4507 |
+ type(a[5]) == 'string' and type(a[6]) == 'function') |
4508 |
+-- arguments left from fist resume |
4509 |
+assert(a[7] == 3 and a[8] == 4) |
4510 |
+-- arguments to last resume |
4511 |
+assert(a[9] == 7 and a[10] == 8) |
4512 |
+-- error message and nothing more |
4513 |
+assert(a[11]:find("errmsg") and #a == 11) |
4514 |
+-- check arguments to pcallk |
4515 |
+assert(x == "YIELD" and y == 4) |
4516 |
+ |
4517 |
+assert(not pcall(co)) -- coroutine should be dead |
4518 |
+ |
4519 |
+ |
4520 |
+-- bug in nCcalls |
4521 |
+local co = coroutine.wrap(function () |
4522 |
+ local a = {pcall(pcall,pcall,pcall,pcall,pcall,pcall,pcall,error,"hi")} |
4523 |
+ return pcall(assert, table.unpack(a)) |
4524 |
+end) |
4525 |
+ |
4526 |
+local a = {co()} |
4527 |
+assert(a[10] == "hi") |
4528 |
+ |
4529 |
+print'OK' |
4530 |
|
4531 |
diff --git a/tests/db.lua b/tests/db.lua |
4532 |
new file mode 100644 |
4533 |
index 0000000..004f57a |
4534 |
--- /dev/null |
4535 |
+++ b/tests/db.lua |
4536 |
@@ -0,0 +1,857 @@ |
4537 |
+-- $Id: db.lua,v 1.79 2016/11/07 13:02:34 roberto Exp $ |
4538 |
+-- See Copyright Notice in file all.lua |
4539 |
+ |
4540 |
+-- testing debug library |
4541 |
+ |
4542 |
+local debug = require "debug" |
4543 |
+ |
4544 |
+local function dostring(s) return assert(load(s))() end |
4545 |
+ |
4546 |
+print"testing debug library and debug information" |
4547 |
+ |
4548 |
+do |
4549 |
+local a=1 |
4550 |
+end |
4551 |
+ |
4552 |
+assert(not debug.gethook()) |
4553 |
+ |
4554 |
+local testline = 19 -- line where 'test' is defined |
4555 |
+function test (s, l, p) -- this must be line 19 |
4556 |
+ collectgarbage() -- avoid gc during trace |
4557 |
+ local function f (event, line) |
4558 |
+ assert(event == 'line') |
4559 |
+ local l = table.remove(l, 1) |
4560 |
+ if p then print(l, line) end |
4561 |
+ assert(l == line, "wrong trace!!") |
4562 |
+ end |
4563 |
+ debug.sethook(f,"l"); load(s)(); debug.sethook() |
4564 |
+ assert(#l == 0) |
4565 |
+end |
4566 |
+ |
4567 |
+ |
4568 |
+do |
4569 |
+ assert(not pcall(debug.getinfo, print, "X")) -- invalid option |
4570 |
+ assert(not debug.getinfo(1000)) -- out of range level |
4571 |
+ assert(not debug.getinfo(-1)) -- out of range level |
4572 |
+ local a = debug.getinfo(print) |
4573 |
+ assert(a.what == "C" and a.short_src == "[C]") |
4574 |
+ a = debug.getinfo(print, "L") |
4575 |
+ assert(a.activelines == nil) |
4576 |
+ local b = debug.getinfo(test, "SfL") |
4577 |
+ assert(b.name == nil and b.what == "Lua" and b.linedefined == testline and |
4578 |
+ b.lastlinedefined == b.linedefined + 10 and |
4579 |
+ b.func == test and not string.find(b.short_src, "%[")) |
4580 |
+ assert(b.activelines[b.linedefined + 1] and |
4581 |
+ b.activelines[b.lastlinedefined]) |
4582 |
+ assert(not b.activelines[b.linedefined] and |
4583 |
+ not b.activelines[b.lastlinedefined + 1]) |
4584 |
+end |
4585 |
+ |
4586 |
+ |
4587 |
+-- test file and string names truncation |
4588 |
+a = "function f () end" |
4589 |
+local function dostring (s, x) return load(s, x)() end |
4590 |
+dostring(a) |
4591 |
+assert(debug.getinfo(f).short_src == string.format('[string "%s"]', a)) |
4592 |
+dostring(a..string.format("; %s\n=1", string.rep('p', 400))) |
4593 |
+assert(string.find(debug.getinfo(f).short_src, '^%[string [^\n]*%.%.%."%]$')) |
4594 |
+dostring(a..string.format("; %s=1", string.rep('p', 400))) |
4595 |
+assert(string.find(debug.getinfo(f).short_src, '^%[string [^\n]*%.%.%."%]$')) |
4596 |
+dostring("\n"..a) |
4597 |
+assert(debug.getinfo(f).short_src == '[string "..."]') |
4598 |
+dostring(a, "") |
4599 |
+assert(debug.getinfo(f).short_src == '[string ""]') |
4600 |
+dostring(a, "@xuxu") |
4601 |
+assert(debug.getinfo(f).short_src == "xuxu") |
4602 |
+dostring(a, "@"..string.rep('p', 1000)..'t') |
4603 |
+assert(string.find(debug.getinfo(f).short_src, "^%.%.%.p*t$")) |
4604 |
+dostring(a, "=xuxu") |
4605 |
+assert(debug.getinfo(f).short_src == "xuxu") |
4606 |
+dostring(a, string.format("=%s", string.rep('x', 500))) |
4607 |
+assert(string.find(debug.getinfo(f).short_src, "^x*$")) |
4608 |
+dostring(a, "=") |
4609 |
+assert(debug.getinfo(f).short_src == "") |
4610 |
+a = nil; f = nil; |
4611 |
+ |
4612 |
+ |
4613 |
+repeat |
4614 |
+ local g = {x = function () |
4615 |
+ local a = debug.getinfo(2) |
4616 |
+ assert(a.name == 'f' and a.namewhat == 'local') |
4617 |
+ a = debug.getinfo(1) |
4618 |
+ assert(a.name == 'x' and a.namewhat == 'field') |
4619 |
+ return 'xixi' |
4620 |
+ end} |
4621 |
+ local f = function () return 1+1 and (not 1 or g.x()) end |
4622 |
+ assert(f() == 'xixi') |
4623 |
+ g = debug.getinfo(f) |
4624 |
+ assert(g.what == "Lua" and g.func == f and g.namewhat == "" and not g.name) |
4625 |
+ |
4626 |
+ function f (x, name) -- local! |
4627 |
+ name = name or 'f' |
4628 |
+ local a = debug.getinfo(1) |
4629 |
+ assert(a.name == name and a.namewhat == 'local') |
4630 |
+ return x |
4631 |
+ end |
4632 |
+ |
4633 |
+ -- breaks in different conditions |
4634 |
+ if 3>4 then break end; f() |
4635 |
+ if 3<4 then a=1 else break end; f() |
4636 |
+ while 1 do local x=10; break end; f() |
4637 |
+ local b = 1 |
4638 |
+ if 3>4 then return math.sin(1) end; f() |
4639 |
+ a = 3<4; f() |
4640 |
+ a = 3<4 or 1; f() |
4641 |
+ repeat local x=20; if 4>3 then f() else break end; f() until 1 |
4642 |
+ g = {} |
4643 |
+ f(g).x = f(2) and f(10)+f(9) |
4644 |
+ assert(g.x == f(19)) |
4645 |
+ function g(x) if not x then return 3 end return (x('a', 'x')) end |
4646 |
+ assert(g(f) == 'a') |
4647 |
+until 1 |
4648 |
+ |
4649 |
+test([[if |
4650 |
+math.sin(1) |
4651 |
+then |
4652 |
+ a=1 |
4653 |
+else |
4654 |
+ a=2 |
4655 |
+end |
4656 |
+]], {2,3,4,7}) |
4657 |
+ |
4658 |
+test([[-- |
4659 |
+if nil then |
4660 |
+ a=1 |
4661 |
+else |
4662 |
+ a=2 |
4663 |
+end |
4664 |
+]], {2,5,6}) |
4665 |
+ |
4666 |
+test([[a=1 |
4667 |
+repeat |
4668 |
+ a=a+1 |
4669 |
+until a==3 |
4670 |
+]], {1,3,4,3,4}) |
4671 |
+ |
4672 |
+test([[ do |
4673 |
+ return |
4674 |
+end |
4675 |
+]], {2}) |
4676 |
+ |
4677 |
+test([[local a |
4678 |
+a=1 |
4679 |
+while a<=3 do |
4680 |
+ a=a+1 |
4681 |
+end |
4682 |
+]], {1,2,3,4,3,4,3,4,3,5}) |
4683 |
+ |
4684 |
+test([[while math.sin(1) do |
4685 |
+ if math.sin(1) |
4686 |
+ then break |
4687 |
+ end |
4688 |
+end |
4689 |
+a=1]], {1,2,3,6}) |
4690 |
+ |
4691 |
+test([[for i=1,3 do |
4692 |
+ a=i |
4693 |
+end |
4694 |
+]], {1,2,1,2,1,2,1,3}) |
4695 |
+ |
4696 |
+test([[for i,v in pairs{'a','b'} do |
4697 |
+ a=tostring(i) .. v |
4698 |
+end |
4699 |
+]], {1,2,1,2,1,3}) |
4700 |
+ |
4701 |
+test([[for i=1,4 do a=1 end]], {1,1,1,1,1}) |
4702 |
+ |
4703 |
+ |
4704 |
+ |
4705 |
+print'+' |
4706 |
+ |
4707 |
+-- invalid levels in [gs]etlocal |
4708 |
+assert(not pcall(debug.getlocal, 20, 1)) |
4709 |
+assert(not pcall(debug.setlocal, -1, 1, 10)) |
4710 |
+ |
4711 |
+ |
4712 |
+-- parameter names |
4713 |
+local function foo (a,b,...) local d, e end |
4714 |
+local co = coroutine.create(foo) |
4715 |
+ |
4716 |
+assert(debug.getlocal(foo, 1) == 'a') |
4717 |
+assert(debug.getlocal(foo, 2) == 'b') |
4718 |
+assert(not debug.getlocal(foo, 3)) |
4719 |
+assert(debug.getlocal(co, foo, 1) == 'a') |
4720 |
+assert(debug.getlocal(co, foo, 2) == 'b') |
4721 |
+assert(not debug.getlocal(co, foo, 3)) |
4722 |
+ |
4723 |
+assert(not debug.getlocal(print, 1)) |
4724 |
+ |
4725 |
+ |
4726 |
+-- varargs |
4727 |
+local function foo (a, ...) |
4728 |
+ local t = table.pack(...) |
4729 |
+ for i = 1, t.n do |
4730 |
+ local n, v = debug.getlocal(1, -i) |
4731 |
+ assert(n == "(*vararg)" and v == t[i]) |
4732 |
+ end |
4733 |
+ assert(not debug.getlocal(1, -(t.n + 1))) |
4734 |
+ assert(not debug.setlocal(1, -(t.n + 1), 30)) |
4735 |
+ if t.n > 0 then |
4736 |
+ (function (x) |
4737 |
+ assert(debug.setlocal(2, -1, x) == "(*vararg)") |
4738 |
+ assert(debug.setlocal(2, -t.n, x) == "(*vararg)") |
4739 |
+ end)(430) |
4740 |
+ assert(... == 430) |
4741 |
+ end |
4742 |
+end |
4743 |
+ |
4744 |
+foo() |
4745 |
+foo(print) |
4746 |
+foo(200, 3, 4) |
4747 |
+local a = {} |
4748 |
+for i = 1, (_soft and 100 or 1000) do a[i] = i end |
4749 |
+foo(table.unpack(a)) |
4750 |
+a = nil |
4751 |
+ |
4752 |
+-- access to vararg in non-vararg function |
4753 |
+local function foo () return debug.getlocal(1, -1) end |
4754 |
+assert(not foo(10)) |
4755 |
+ |
4756 |
+ |
4757 |
+do -- test hook presence in debug info |
4758 |
+ assert(not debug.gethook()) |
4759 |
+ local count = 0 |
4760 |
+ local function f () |
4761 |
+ assert(debug.getinfo(1).namewhat == "hook") |
4762 |
+ local sndline = string.match(debug.traceback(), "\n(.-)\n") |
4763 |
+ assert(string.find(sndline, "hook")) |
4764 |
+ count = count + 1 |
4765 |
+ end |
4766 |
+ debug.sethook(f, "l") |
4767 |
+ local a = 0 |
4768 |
+ _ENV.a = a |
4769 |
+ a = 1 |
4770 |
+ debug.sethook() |
4771 |
+ assert(count == 4) |
4772 |
+end |
4773 |
+ |
4774 |
+ |
4775 |
+a = {}; L = nil |
4776 |
+local glob = 1 |
4777 |
+local oldglob = glob |
4778 |
+debug.sethook(function (e,l) |
4779 |
+ collectgarbage() -- force GC during a hook |
4780 |
+ local f, m, c = debug.gethook() |
4781 |
+ assert(m == 'crl' and c == 0) |
4782 |
+ if e == "line" then |
4783 |
+ if glob ~= oldglob then |
4784 |
+ L = l-1 -- get the first line where "glob" has changed |
4785 |
+ oldglob = glob |
4786 |
+ end |
4787 |
+ elseif e == "call" then |
4788 |
+ local f = debug.getinfo(2, "f").func |
4789 |
+ a[f] = 1 |
4790 |
+ else assert(e == "return") |
4791 |
+ end |
4792 |
+end, "crl") |
4793 |
+ |
4794 |
+ |
4795 |
+function f(a,b) |
4796 |
+ collectgarbage() |
4797 |
+ local _, x = debug.getlocal(1, 1) |
4798 |
+ local _, y = debug.getlocal(1, 2) |
4799 |
+ assert(x == a and y == b) |
4800 |
+ assert(debug.setlocal(2, 3, "pera") == "AA".."AA") |
4801 |
+ assert(debug.setlocal(2, 4, "maçã") == "B") |
4802 |
+ x = debug.getinfo(2) |
4803 |
+ assert(x.func == g and x.what == "Lua" and x.name == 'g' and |
4804 |
+ x.nups == 2 and string.find(x.source, "^@.*db%.lua$")) |
4805 |
+ glob = glob+1 |
4806 |
+ assert(debug.getinfo(1, "l").currentline == L+1) |
4807 |
+ assert(debug.getinfo(1, "l").currentline == L+2) |
4808 |
+end |
4809 |
+ |
4810 |
+function foo() |
4811 |
+ glob = glob+1 |
4812 |
+ assert(debug.getinfo(1, "l").currentline == L+1) |
4813 |
+end; foo() -- set L |
4814 |
+-- check line counting inside strings and empty lines |
4815 |
+ |
4816 |
+_ = 'alo\ |
4817 |
+alo' .. [[ |
4818 |
+ |
4819 |
+]] |
4820 |
+--[[ |
4821 |
+]] |
4822 |
+assert(debug.getinfo(1, "l").currentline == L+11) -- check count of lines |
4823 |
+ |
4824 |
+ |
4825 |
+function g(...) |
4826 |
+ local arg = {...} |
4827 |
+ do local a,b,c; a=math.sin(40); end |
4828 |
+ local feijao |
4829 |
+ local AAAA,B = "xuxu", "mamão" |
4830 |
+ f(AAAA,B) |
4831 |
+ assert(AAAA == "pera" and B == "maçã") |
4832 |
+ do |
4833 |
+ local B = 13 |
4834 |
+ local x,y = debug.getlocal(1,5) |
4835 |
+ assert(x == 'B' and y == 13) |
4836 |
+ end |
4837 |
+end |
4838 |
+ |
4839 |
+g() |
4840 |
+ |
4841 |
+ |
4842 |
+assert(a[f] and a[g] and a[assert] and a[debug.getlocal] and not a[print]) |
4843 |
+ |
4844 |
+ |
4845 |
+-- tests for manipulating non-registered locals (C and Lua temporaries) |
4846 |
+ |
4847 |
+local n, v = debug.getlocal(0, 1) |
4848 |
+assert(v == 0 and n == "(*temporary)") |
4849 |
+local n, v = debug.getlocal(0, 2) |
4850 |
+assert(v == 2 and n == "(*temporary)") |
4851 |
+assert(not debug.getlocal(0, 3)) |
4852 |
+assert(not debug.getlocal(0, 0)) |
4853 |
+ |
4854 |
+function f() |
4855 |
+ assert(select(2, debug.getlocal(2,3)) == 1) |
4856 |
+ assert(not debug.getlocal(2,4)) |
4857 |
+ debug.setlocal(2, 3, 10) |
4858 |
+ return 20 |
4859 |
+end |
4860 |
+ |
4861 |
+function g(a,b) return (a+1) + f() end |
4862 |
+ |
4863 |
+assert(g(0,0) == 30) |
4864 |
+ |
4865 |
+ |
4866 |
+debug.sethook(nil); |
4867 |
+assert(debug.gethook() == nil) |
4868 |
+ |
4869 |
+ |
4870 |
+-- testing access to function arguments |
4871 |
+ |
4872 |
+local function collectlocals (level) |
4873 |
+ local tab = {} |
4874 |
+ for i = 1, math.huge do |
4875 |
+ local n, v = debug.getlocal(level + 1, i) |
4876 |
+ if not (n and string.find(n, "^[a-zA-Z0-9_]+$")) then |
4877 |
+ break -- consider only real variables |
4878 |
+ end |
4879 |
+ tab[n] = v |
4880 |
+ end |
4881 |
+ return tab |
4882 |
+end |
4883 |
+ |
4884 |
+ |
4885 |
+X = nil |
4886 |
+a = {} |
4887 |
+function a:f (a, b, ...) local arg = {...}; local c = 13 end |
4888 |
+debug.sethook(function (e) |
4889 |
+ assert(e == "call") |
4890 |
+ dostring("XX = 12") -- test dostring inside hooks |
4891 |
+ -- testing errors inside hooks |
4892 |
+ assert(not pcall(load("a='joao'+1"))) |
4893 |
+ debug.sethook(function (e, l) |
4894 |
+ assert(debug.getinfo(2, "l").currentline == l) |
4895 |
+ local f,m,c = debug.gethook() |
4896 |
+ assert(e == "line") |
4897 |
+ assert(m == 'l' and c == 0) |
4898 |
+ debug.sethook(nil) -- hook is called only once |
4899 |
+ assert(not X) -- check that |
4900 |
+ X = collectlocals(2) |
4901 |
+ end, "l") |
4902 |
+end, "c") |
4903 |
+ |
4904 |
+a:f(1,2,3,4,5) |
4905 |
+assert(X.self == a and X.a == 1 and X.b == 2 and X.c == nil) |
4906 |
+assert(XX == 12) |
4907 |
+assert(debug.gethook() == nil) |
4908 |
+ |
4909 |
+ |
4910 |
+-- testing access to local variables in return hook (bug in 5.2) |
4911 |
+do |
4912 |
+ local function foo (a, b) |
4913 |
+ do local x,y,z end |
4914 |
+ local c, d = 10, 20 |
4915 |
+ return |
4916 |
+ end |
4917 |
+ |
4918 |
+ local function aux () |
4919 |
+ if debug.getinfo(2).name == "foo" then |
4920 |
+ foo = nil -- to signal that it found 'foo' |
4921 |
+ local tab = {a = 100, b = 200, c = 10, d = 20} |
4922 |
+ for n, v in pairs(collectlocals(2)) do |
4923 |
+ assert(tab[n] == v) |
4924 |
+ tab[n] = nil |
4925 |
+ end |
4926 |
+ assert(next(tab) == nil) -- 'tab' must be empty |
4927 |
+ end |
4928 |
+ end |
4929 |
+ |
4930 |
+ debug.sethook(aux, "r"); foo(100, 200); debug.sethook() |
4931 |
+ assert(foo == nil) |
4932 |
+end |
4933 |
+ |
4934 |
+-- testing upvalue access |
4935 |
+local function getupvalues (f) |
4936 |
+ local t = {} |
4937 |
+ local i = 1 |
4938 |
+ while true do |
4939 |
+ local name, value = debug.getupvalue(f, i) |
4940 |
+ if not name then break end |
4941 |
+ assert(not t[name]) |
4942 |
+ t[name] = value |
4943 |
+ i = i + 1 |
4944 |
+ end |
4945 |
+ return t |
4946 |
+end |
4947 |
+ |
4948 |
+local a,b,c = 1,2,3 |
4949 |
+local function foo1 (a) b = a; return c end |
4950 |
+local function foo2 (x) a = x; return c+b end |
4951 |
+assert(not debug.getupvalue(foo1, 3)) |
4952 |
+assert(not debug.getupvalue(foo1, 0)) |
4953 |
+assert(not debug.setupvalue(foo1, 3, "xuxu")) |
4954 |
+local t = getupvalues(foo1) |
4955 |
+assert(t.a == nil and t.b == 2 and t.c == 3) |
4956 |
+t = getupvalues(foo2) |
4957 |
+assert(t.a == 1 and t.b == 2 and t.c == 3) |
4958 |
+assert(debug.setupvalue(foo1, 1, "xuxu") == "b") |
4959 |
+assert(({debug.getupvalue(foo2, 3)})[2] == "xuxu") |
4960 |
+-- upvalues of C functions are allways "called" "" (the empty string) |
4961 |
+assert(debug.getupvalue(string.gmatch("x", "x"), 1) == "") |
4962 |
+ |
4963 |
+ |
4964 |
+-- testing count hooks |
4965 |
+local a=0 |
4966 |
+debug.sethook(function (e) a=a+1 end, "", 1) |
4967 |
+a=0; for i=1,1000 do end; assert(1000 < a and a < 1012) |
4968 |
+debug.sethook(function (e) a=a+1 end, "", 4) |
4969 |
+a=0; for i=1,1000 do end; assert(250 < a and a < 255) |
4970 |
+local f,m,c = debug.gethook() |
4971 |
+assert(m == "" and c == 4) |
4972 |
+debug.sethook(function (e) a=a+1 end, "", 4000) |
4973 |
+a=0; for i=1,1000 do end; assert(a == 0) |
4974 |
+ |
4975 |
+do |
4976 |
+ debug.sethook(print, "", 2^24 - 1) -- count upperbound |
4977 |
+ local f,m,c = debug.gethook() |
4978 |
+ assert(({debug.gethook()})[3] == 2^24 - 1) |
4979 |
+end |
4980 |
+ |
4981 |
+debug.sethook() |
4982 |
+ |
4983 |
+ |
4984 |
+-- tests for tail calls |
4985 |
+local function f (x) |
4986 |
+ if x then |
4987 |
+ assert(debug.getinfo(1, "S").what == "Lua") |
4988 |
+ assert(debug.getinfo(1, "t").istailcall == true) |
4989 |
+ local tail = debug.getinfo(2) |
4990 |
+ assert(tail.func == g1 and tail.istailcall == true) |
4991 |
+ assert(debug.getinfo(3, "S").what == "main") |
4992 |
+ print"+" |
4993 |
+ end |
4994 |
+end |
4995 |
+ |
4996 |
+function g(x) return f(x) end |
4997 |
+ |
4998 |
+function g1(x) g(x) end |
4999 |
+ |
5000 |
+local function h (x) local f=g1; return f(x) end |
5001 |
+ |
5002 |
+h(true) |
5003 |
+ |
5004 |
+local b = {} |
5005 |
+debug.sethook(function (e) table.insert(b, e) end, "cr") |
5006 |
+h(false) |
5007 |
+debug.sethook() |
5008 |
+local res = {"return", -- first return (from sethook) |
5009 |
+ "call", "tail call", "call", "tail call", |
5010 |
+ "return", "return", |
5011 |
+ "call", -- last call (to sethook) |
5012 |
+} |
5013 |
+for i = 1, #res do assert(res[i] == table.remove(b, 1)) end |
5014 |
+ |
5015 |
+b = 0 |
5016 |
+debug.sethook(function (e) |
5017 |
+ if e == "tail call" then |
5018 |
+ b = b + 1 |
5019 |
+ assert(debug.getinfo(2, "t").istailcall == true) |
5020 |
+ else |
5021 |
+ assert(debug.getinfo(2, "t").istailcall == false) |
5022 |
+ end |
5023 |
+ end, "c") |
5024 |
+h(false) |
5025 |
+debug.sethook() |
5026 |
+assert(b == 2) -- two tail calls |
5027 |
+ |
5028 |
+lim = _soft and 3000 or 30000 |
5029 |
+local function foo (x) |
5030 |
+ if x==0 then |
5031 |
+ assert(debug.getinfo(2).what == "main") |
5032 |
+ local info = debug.getinfo(1) |
5033 |
+ assert(info.istailcall == true and info.func == foo) |
5034 |
+ else return foo(x-1) |
5035 |
+ end |
5036 |
+end |
5037 |
+ |
5038 |
+foo(lim) |
5039 |
+ |
5040 |
+ |
5041 |
+print"+" |
5042 |
+ |
5043 |
+ |
5044 |
+-- testing local function information |
5045 |
+co = load[[ |
5046 |
+ local A = function () |
5047 |
+ return x |
5048 |
+ end |
5049 |
+ return |
5050 |
+]] |
5051 |
+ |
5052 |
+local a = 0 |
5053 |
+-- 'A' should be visible to debugger only after its complete definition |
5054 |
+debug.sethook(function (e, l) |
5055 |
+ if l == 3 then a = a + 1; assert(debug.getlocal(2, 1) == "(*temporary)") |
5056 |
+ elseif l == 4 then a = a + 1; assert(debug.getlocal(2, 1) == "A") |
5057 |
+ end |
5058 |
+end, "l") |
5059 |
+co() -- run local function definition |
5060 |
+debug.sethook() -- turn off hook |
5061 |
+assert(a == 2) -- ensure all two lines where hooked |
5062 |
+ |
5063 |
+-- testing traceback |
5064 |
+ |
5065 |
+assert(debug.traceback(print) == print) |
5066 |
+assert(debug.traceback(print, 4) == print) |
5067 |
+assert(string.find(debug.traceback("hi", 4), "^hi\n")) |
5068 |
+assert(string.find(debug.traceback("hi"), "^hi\n")) |
5069 |
+assert(not string.find(debug.traceback("hi"), "'debug.traceback'")) |
5070 |
+assert(string.find(debug.traceback("hi", 0), "'debug.traceback'")) |
5071 |
+assert(string.find(debug.traceback(), "^stack traceback:\n")) |
5072 |
+ |
5073 |
+do -- C-function names in traceback |
5074 |
+ local st, msg = (function () return pcall end)()(debug.traceback) |
5075 |
+ assert(st == true and string.find(msg, "pcall")) |
5076 |
+end |
5077 |
+ |
5078 |
+ |
5079 |
+-- testing nparams, nups e isvararg |
5080 |
+local t = debug.getinfo(print, "u") |
5081 |
+assert(t.isvararg == true and t.nparams == 0 and t.nups == 0) |
5082 |
+ |
5083 |
+t = debug.getinfo(function (a,b,c) end, "u") |
5084 |
+assert(t.isvararg == false and t.nparams == 3 and t.nups == 0) |
5085 |
+ |
5086 |
+t = debug.getinfo(function (a,b,...) return t[a] end, "u") |
5087 |
+assert(t.isvararg == true and t.nparams == 2 and t.nups == 1) |
5088 |
+ |
5089 |
+t = debug.getinfo(1) -- main |
5090 |
+assert(t.isvararg == true and t.nparams == 0 and t.nups == 1 and |
5091 |
+ debug.getupvalue(t.func, 1) == "_ENV") |
5092 |
+ |
5093 |
+ |
5094 |
+ |
5095 |
+ |
5096 |
+-- testing debugging of coroutines |
5097 |
+ |
5098 |
+local function checktraceback (co, p, level) |
5099 |
+ local tb = debug.traceback(co, nil, level) |
5100 |
+ local i = 0 |
5101 |
+ for l in string.gmatch(tb, "[^\n]+\n?") do |
5102 |
+ assert(i == 0 or string.find(l, p[i])) |
5103 |
+ i = i+1 |
5104 |
+ end |
5105 |
+ assert(p[i] == nil) |
5106 |
+end |
5107 |
+ |
5108 |
+ |
5109 |
+local function f (n) |
5110 |
+ if n > 0 then f(n-1) |
5111 |
+ else coroutine.yield() end |
5112 |
+end |
5113 |
+ |
5114 |
+local co = coroutine.create(f) |
5115 |
+coroutine.resume(co, 3) |
5116 |
+checktraceback(co, {"yield", "db.lua", "db.lua", "db.lua", "db.lua"}) |
5117 |
+checktraceback(co, {"db.lua", "db.lua", "db.lua", "db.lua"}, 1) |
5118 |
+checktraceback(co, {"db.lua", "db.lua", "db.lua"}, 2) |
5119 |
+checktraceback(co, {"db.lua"}, 4) |
5120 |
+checktraceback(co, {}, 40) |
5121 |
+ |
5122 |
+ |
5123 |
+co = coroutine.create(function (x) |
5124 |
+ local a = 1 |
5125 |
+ coroutine.yield(debug.getinfo(1, "l")) |
5126 |
+ coroutine.yield(debug.getinfo(1, "l").currentline) |
5127 |
+ return a |
5128 |
+ end) |
5129 |
+ |
5130 |
+local tr = {} |
5131 |
+local foo = function (e, l) if l then table.insert(tr, l) end end |
5132 |
+debug.sethook(co, foo, "lcr") |
5133 |
+ |
5134 |
+local _, l = coroutine.resume(co, 10) |
5135 |
+local x = debug.getinfo(co, 1, "lfLS") |
5136 |
+assert(x.currentline == l.currentline and x.activelines[x.currentline]) |
5137 |
+assert(type(x.func) == "function") |
5138 |
+for i=x.linedefined + 1, x.lastlinedefined do |
5139 |
+ assert(x.activelines[i]) |
5140 |
+ x.activelines[i] = nil |
5141 |
+end |
5142 |
+assert(next(x.activelines) == nil) -- no 'extra' elements |
5143 |
+assert(not debug.getinfo(co, 2)) |
5144 |
+local a,b = debug.getlocal(co, 1, 1) |
5145 |
+assert(a == "x" and b == 10) |
5146 |
+a,b = debug.getlocal(co, 1, 2) |
5147 |
+assert(a == "a" and b == 1) |
5148 |
+debug.setlocal(co, 1, 2, "hi") |
5149 |
+assert(debug.gethook(co) == foo) |
5150 |
+assert(#tr == 2 and |
5151 |
+ tr[1] == l.currentline-1 and tr[2] == l.currentline) |
5152 |
+ |
5153 |
+a,b,c = pcall(coroutine.resume, co) |
5154 |
+assert(a and b and c == l.currentline+1) |
5155 |
+checktraceback(co, {"yield", "in function <"}) |
5156 |
+ |
5157 |
+a,b = coroutine.resume(co) |
5158 |
+assert(a and b == "hi") |
5159 |
+assert(#tr == 4 and tr[4] == l.currentline+2) |
5160 |
+assert(debug.gethook(co) == foo) |
5161 |
+assert(not debug.gethook()) |
5162 |
+checktraceback(co, {}) |
5163 |
+ |
5164 |
+ |
5165 |
+-- check get/setlocal in coroutines |
5166 |
+co = coroutine.create(function (x) |
5167 |
+ local a, b = coroutine.yield(x) |
5168 |
+ assert(a == 100 and b == nil) |
5169 |
+ return x |
5170 |
+end) |
5171 |
+a, b = coroutine.resume(co, 10) |
5172 |
+assert(a and b == 10) |
5173 |
+a, b = debug.getlocal(co, 1, 1) |
5174 |
+assert(a == "x" and b == 10) |
5175 |
+assert(not debug.getlocal(co, 1, 5)) |
5176 |
+assert(debug.setlocal(co, 1, 1, 30) == "x") |
5177 |
+assert(not debug.setlocal(co, 1, 5, 40)) |
5178 |
+a, b = coroutine.resume(co, 100) |
5179 |
+assert(a and b == 30) |
5180 |
+ |
5181 |
+ |
5182 |
+-- check traceback of suspended (or dead with error) coroutines |
5183 |
+ |
5184 |
+function f(i) if i==0 then error(i) else coroutine.yield(); f(i-1) end end |
5185 |
+ |
5186 |
+co = coroutine.create(function (x) f(x) end) |
5187 |
+a, b = coroutine.resume(co, 3) |
5188 |
+t = {"'coroutine.yield'", "'f'", "in function <"} |
5189 |
+while coroutine.status(co) == "suspended" do |
5190 |
+ checktraceback(co, t) |
5191 |
+ a, b = coroutine.resume(co) |
5192 |
+ table.insert(t, 2, "'f'") -- one more recursive call to 'f' |
5193 |
+end |
5194 |
+t[1] = "'error'" |
5195 |
+checktraceback(co, t) |
5196 |
+ |
5197 |
+ |
5198 |
+-- test acessing line numbers of a coroutine from a resume inside |
5199 |
+-- a C function (this is a known bug in Lua 5.0) |
5200 |
+ |
5201 |
+local function g(x) |
5202 |
+ coroutine.yield(x) |
5203 |
+end |
5204 |
+ |
5205 |
+local function f (i) |
5206 |
+ debug.sethook(function () end, "l") |
5207 |
+ for j=1,1000 do |
5208 |
+ g(i+j) |
5209 |
+ end |
5210 |
+end |
5211 |
+ |
5212 |
+local co = coroutine.wrap(f) |
5213 |
+co(10) |
5214 |
+pcall(co) |
5215 |
+pcall(co) |
5216 |
+ |
5217 |
+ |
5218 |
+assert(type(debug.getregistry()) == "table") |
5219 |
+ |
5220 |
+ |
5221 |
+-- test tagmethod information |
5222 |
+local a = {} |
5223 |
+local function f (t) |
5224 |
+ local info = debug.getinfo(1); |
5225 |
+ assert(info.namewhat == "metamethod") |
5226 |
+ a.op = info.name |
5227 |
+ return info.name |
5228 |
+end |
5229 |
+setmetatable(a, { |
5230 |
+ __index = f; __add = f; __div = f; __mod = f; __concat = f; __pow = f; |
5231 |
+ __mul = f; __idiv = f; __unm = f; __len = f; __sub = f; |
5232 |
+ __shl = f; __shr = f; __bor = f; __bxor = f; |
5233 |
+ __eq = f; __le = f; __lt = f; __unm = f; __len = f; __band = f; |
5234 |
+ __bnot = f; |
5235 |
+}) |
5236 |
+ |
5237 |
+local b = setmetatable({}, getmetatable(a)) |
5238 |
+ |
5239 |
+assert(a[3] == "__index" and a^3 == "__pow" and a..a == "__concat") |
5240 |
+assert(a/3 == "__div" and 3%a == "__mod") |
5241 |
+assert(a+3 == "__add" and 3-a == "__sub" and a*3 == "__mul" and |
5242 |
+ -a == "__unm" and #a == "__len" and a&3 == "__band") |
5243 |
+assert(a|3 == "__bor" and 3~a == "__bxor" and a<<3 == "__shl" and |
5244 |
+ a>>1 == "__shr") |
5245 |
+assert (a==b and a.op == "__eq") |
5246 |
+assert (a>=b and a.op == "__le") |
5247 |
+assert (a>b and a.op == "__lt") |
5248 |
+assert(~a == "__bnot") |
5249 |
+ |
5250 |
+do -- testing for-iterator name |
5251 |
+ local function f() |
5252 |
+ assert(debug.getinfo(1).name == "for iterator") |
5253 |
+ end |
5254 |
+ |
5255 |
+ for i in f do end |
5256 |
+end |
5257 |
+ |
5258 |
+ |
5259 |
+do -- testing debug info for finalizers |
5260 |
+ local name = nil |
5261 |
+ |
5262 |
+ -- create a piece of garbage with a finalizer |
5263 |
+ setmetatable({}, {__gc = function () |
5264 |
+ local t = debug.getinfo(2) -- get callee information |
5265 |
+ assert(t.namewhat == "metamethod") |
5266 |
+ name = t.name |
5267 |
+ end}) |
5268 |
+ |
5269 |
+ -- repeat until previous finalizer runs (setting 'name') |
5270 |
+ repeat local a = {} until name |
5271 |
+ assert(name == "__gc") |
5272 |
+end |
5273 |
+ |
5274 |
+ |
5275 |
+do |
5276 |
+ print("testing traceback sizes") |
5277 |
+ |
5278 |
+ local function countlines (s) |
5279 |
+ return select(2, string.gsub(s, "\n", "")) |
5280 |
+ end |
5281 |
+ |
5282 |
+ local function deep (lvl, n) |
5283 |
+ if lvl == 0 then |
5284 |
+ return (debug.traceback("message", n)) |
5285 |
+ else |
5286 |
+ return (deep(lvl-1, n)) |
5287 |
+ end |
5288 |
+ end |
5289 |
+ |
5290 |
+ local function checkdeep (total, start) |
5291 |
+ local s = deep(total, start) |
5292 |
+ local rest = string.match(s, "^message\nstack traceback:\n(.*)$") |
5293 |
+ local cl = countlines(rest) |
5294 |
+ -- at most 10 lines in first part, 11 in second, plus '...' |
5295 |
+ assert(cl <= 10 + 11 + 1) |
5296 |
+ local brk = string.find(rest, "%.%.%.") |
5297 |
+ if brk then -- does message have '...'? |
5298 |
+ local rest1 = string.sub(rest, 1, brk) |
5299 |
+ local rest2 = string.sub(rest, brk, #rest) |
5300 |
+ assert(countlines(rest1) == 10 and countlines(rest2) == 11) |
5301 |
+ else |
5302 |
+ assert(cl == total - start + 2) |
5303 |
+ end |
5304 |
+ end |
5305 |
+ |
5306 |
+ for d = 1, 51, 10 do |
5307 |
+ for l = 1, d do |
5308 |
+ -- use coroutines to ensure complete control of the stack |
5309 |
+ coroutine.wrap(checkdeep)(d, l) |
5310 |
+ end |
5311 |
+ end |
5312 |
+ |
5313 |
+end |
5314 |
+ |
5315 |
+ |
5316 |
+print("testing debug functions on chunk without debug info") |
5317 |
+prog = [[-- program to be loaded without debug information |
5318 |
+local debug = require'debug' |
5319 |
+local a = 12 -- a local variable |
5320 |
+ |
5321 |
+local n, v = debug.getlocal(1, 1) |
5322 |
+assert(n == "(*temporary)" and v == debug) -- unkown name but known value |
5323 |
+n, v = debug.getlocal(1, 2) |
5324 |
+assert(n == "(*temporary)" and v == 12) -- unkown name but known value |
5325 |
+ |
5326 |
+-- a function with an upvalue |
5327 |
+local f = function () local x; return a end |
5328 |
+n, v = debug.getupvalue(f, 1) |
5329 |
+assert(n == "(*no name)" and v == 12) |
5330 |
+assert(debug.setupvalue(f, 1, 13) == "(*no name)") |
5331 |
+assert(a == 13) |
5332 |
+ |
5333 |
+local t = debug.getinfo(f) |
5334 |
+assert(t.name == nil and t.linedefined > 0 and |
5335 |
+ t.lastlinedefined == t.linedefined and |
5336 |
+ t.short_src == "?") |
5337 |
+assert(debug.getinfo(1).currentline == -1) |
5338 |
+ |
5339 |
+t = debug.getinfo(f, "L").activelines |
5340 |
+assert(next(t) == nil) -- active lines are empty |
5341 |
+ |
5342 |
+-- dump/load a function without debug info |
5343 |
+f = load(string.dump(f)) |
5344 |
+ |
5345 |
+t = debug.getinfo(f) |
5346 |
+assert(t.name == nil and t.linedefined > 0 and |
5347 |
+ t.lastlinedefined == t.linedefined and |
5348 |
+ t.short_src == "?") |
5349 |
+assert(debug.getinfo(1).currentline == -1) |
5350 |
+ |
5351 |
+return a |
5352 |
+]] |
5353 |
+ |
5354 |
+ |
5355 |
+-- load 'prog' without debug info |
5356 |
+local f = assert(load(string.dump(load(prog), true))) |
5357 |
+ |
5358 |
+assert(f() == 13) |
5359 |
+ |
5360 |
+do -- tests for 'source' in binary dumps |
5361 |
+ local prog = [[ |
5362 |
+ return function (x) |
5363 |
+ return function (y) |
5364 |
+ return x + y |
5365 |
+ end |
5366 |
+ end |
5367 |
+ ]] |
5368 |
+ local name = string.rep("x", 1000) |
5369 |
+ local p = assert(load(prog, name)) |
5370 |
+ -- load 'p' as a binary chunk with debug information |
5371 |
+ local c = string.dump(p) |
5372 |
+ assert(#c > 1000 and #c < 2000) -- no repetition of 'source' in dump |
5373 |
+ local f = assert(load(c)) |
5374 |
+ local g = f() |
5375 |
+ local h = g(3) |
5376 |
+ assert(h(5) == 8) |
5377 |
+ assert(debug.getinfo(f).source == name and -- all functions have 'source' |
5378 |
+ debug.getinfo(g).source == name and |
5379 |
+ debug.getinfo(h).source == name) |
5380 |
+ -- again, without debug info |
5381 |
+ local c = string.dump(p, true) |
5382 |
+ assert(#c < 500) -- no 'source' in dump |
5383 |
+ local f = assert(load(c)) |
5384 |
+ local g = f() |
5385 |
+ local h = g(30) |
5386 |
+ assert(h(50) == 80) |
5387 |
+ assert(debug.getinfo(f).source == '=?' and -- no function has 'source' |
5388 |
+ debug.getinfo(g).source == '=?' and |
5389 |
+ debug.getinfo(h).source == '=?') |
5390 |
+end |
5391 |
+ |
5392 |
+print"OK" |
5393 |
+ |
5394 |
|
5395 |
diff --git a/tests/errors.lua b/tests/errors.lua |
5396 |
new file mode 100644 |
5397 |
index 0000000..79d746c |
5398 |
--- /dev/null |
5399 |
+++ b/tests/errors.lua |
5400 |
@@ -0,0 +1,537 @@ |
5401 |
+-- $Id: errors.lua,v 1.94 2016/12/21 19:23:02 roberto Exp $ |
5402 |
+-- See Copyright Notice in file all.lua |
5403 |
+ |
5404 |
+print("testing errors") |
5405 |
+ |
5406 |
+local debug = require"debug" |
5407 |
+ |
5408 |
+-- avoid problems with 'strict' module (which may generate other error messages) |
5409 |
+local mt = getmetatable(_G) or {} |
5410 |
+local oldmm = mt.__index |
5411 |
+mt.__index = nil |
5412 |
+ |
5413 |
+local function checkerr (msg, f, ...) |
5414 |
+ local st, err = pcall(f, ...) |
5415 |
+ assert(not st and string.find(err, msg)) |
5416 |
+end |
5417 |
+ |
5418 |
+ |
5419 |
+local function doit (s) |
5420 |
+ local f, msg = load(s) |
5421 |
+ if f == nil then return msg end |
5422 |
+ local cond, msg = pcall(f) |
5423 |
+ return (not cond) and msg |
5424 |
+end |
5425 |
+ |
5426 |
+ |
5427 |
+local function checkmessage (prog, msg) |
5428 |
+ local m = doit(prog) |
5429 |
+ assert(string.find(m, msg, 1, true)) |
5430 |
+end |
5431 |
+ |
5432 |
+local function checksyntax (prog, extra, token, line) |
5433 |
+ local msg = doit(prog) |
5434 |
+ if not string.find(token, "^<%a") and not string.find(token, "^char%(") |
5435 |
+ then token = "'"..token.."'" end |
5436 |
+ token = string.gsub(token, "(%p)", "%%%1") |
5437 |
+ local pt = string.format([[^%%[string ".*"%%]:%d: .- near %s$]], |
5438 |
+ line, token) |
5439 |
+ assert(string.find(msg, pt)) |
5440 |
+ assert(string.find(msg, msg, 1, true)) |
5441 |
+end |
5442 |
+ |
5443 |
+ |
5444 |
+-- test error message with no extra info |
5445 |
+assert(doit("error('hi', 0)") == 'hi') |
5446 |
+ |
5447 |
+-- test error message with no info |
5448 |
+assert(doit("error()") == nil) |
5449 |
+ |
5450 |
+ |
5451 |
+-- test common errors/errors that crashed in the past |
5452 |
+assert(doit("table.unpack({}, 1, n=2^30)")) |
5453 |
+assert(doit("a=math.sin()")) |
5454 |
+assert(not doit("tostring(1)") and doit("tostring()")) |
5455 |
+assert(doit"tonumber()") |
5456 |
+assert(doit"repeat until 1; a") |
5457 |
+assert(doit"return;;") |
5458 |
+assert(doit"assert(false)") |
5459 |
+assert(doit"assert(nil)") |
5460 |
+assert(doit("function a (... , ...) end")) |
5461 |
+assert(doit("function a (, ...) end")) |
5462 |
+assert(doit("local t={}; t = t[#t] + 1")) |
5463 |
+ |
5464 |
+checksyntax([[ |
5465 |
+ local a = {4 |
5466 |
+ |
5467 |
+]], "'}' expected (to close '{' at line 1)", "<eof>", 3) |
5468 |
+ |
5469 |
+ |
5470 |
+-- tests for better error messages |
5471 |
+ |
5472 |
+checkmessage("a = {} + 1", "arithmetic") |
5473 |
+checkmessage("a = {} | 1", "bitwise operation") |
5474 |
+checkmessage("a = {} < 1", "attempt to compare") |
5475 |
+checkmessage("a = {} <= 1", "attempt to compare") |
5476 |
+ |
5477 |
+checkmessage("a=1; bbbb=2; a=math.sin(3)+bbbb(3)", "global 'bbbb'") |
5478 |
+checkmessage("a={}; do local a=1 end a:bbbb(3)", "method 'bbbb'") |
5479 |
+checkmessage("local a={}; a.bbbb(3)", "field 'bbbb'") |
5480 |
+assert(not string.find(doit"a={13}; local bbbb=1; a[bbbb](3)", "'bbbb'")) |
5481 |
+checkmessage("a={13}; local bbbb=1; a[bbbb](3)", "number") |
5482 |
+checkmessage("a=(1)..{}", "a table value") |
5483 |
+ |
5484 |
+checkmessage("a = #print", "length of a function value") |
5485 |
+checkmessage("a = #3", "length of a number value") |
5486 |
+ |
5487 |
+aaa = nil |
5488 |
+checkmessage("aaa.bbb:ddd(9)", "global 'aaa'") |
5489 |
+checkmessage("local aaa={bbb=1}; aaa.bbb:ddd(9)", "field 'bbb'") |
5490 |
+checkmessage("local aaa={bbb={}}; aaa.bbb:ddd(9)", "method 'ddd'") |
5491 |
+checkmessage("local a,b,c; (function () a = b+1 end)()", "upvalue 'b'") |
5492 |
+assert(not doit"local aaa={bbb={ddd=next}}; aaa.bbb:ddd(nil)") |
5493 |
+ |
5494 |
+-- upvalues being indexed do not go to the stack |
5495 |
+checkmessage("local a,b,cc; (function () a = cc[1] end)()", "upvalue 'cc'") |
5496 |
+checkmessage("local a,b,cc; (function () a.x = 1 end)()", "upvalue 'a'") |
5497 |
+ |
5498 |
+checkmessage("local _ENV = {x={}}; a = a + 1", "global 'a'") |
5499 |
+ |
5500 |
+checkmessage("b=1; local aaa='a'; x=aaa+b", "local 'aaa'") |
5501 |
+checkmessage("aaa={}; x=3/aaa", "global 'aaa'") |
5502 |
+checkmessage("aaa='2'; b=nil;x=aaa*b", "global 'b'") |
5503 |
+checkmessage("aaa={}; x=-aaa", "global 'aaa'") |
5504 |
+ |
5505 |
+-- short circuit |
5506 |
+checkmessage("a=1; local a,bbbb=2,3; a = math.sin(1) and bbbb(3)", |
5507 |
+ "local 'bbbb'") |
5508 |
+checkmessage("a=1; local a,bbbb=2,3; a = bbbb(1) or a(3)", "local 'bbbb'") |
5509 |
+checkmessage("local a,b,c,f = 1,1,1; f((a and b) or c)", "local 'f'") |
5510 |
+checkmessage("local a,b,c = 1,1,1; ((a and b) or c)()", "call a number value") |
5511 |
+assert(not string.find(doit"aaa={}; x=(aaa or aaa)+(aaa and aaa)", "'aaa'")) |
5512 |
+assert(not string.find(doit"aaa={}; (aaa or aaa)()", "'aaa'")) |
5513 |
+ |
5514 |
+checkmessage("print(print < 10)", "function with number") |
5515 |
+checkmessage("print(print < print)", "two function values") |
5516 |
+checkmessage("print('10' < 10)", "string with number") |
5517 |
+checkmessage("print(10 < '23')", "number with string") |
5518 |
+ |
5519 |
+-- float->integer conversions |
5520 |
+checkmessage("local a = 2.0^100; x = a << 2", "local a") |
5521 |
+checkmessage("local a = 1 >> 2.0^100", "has no integer representation") |
5522 |
+checkmessage("local a = '10' << 2.0^100", "has no integer representation") |
5523 |
+checkmessage("local a = 2.0^100 & 1", "has no integer representation") |
5524 |
+checkmessage("local a = 2.0^100 & '1'", "has no integer representation") |
5525 |
+checkmessage("local a = 2.0 | 1e40", "has no integer representation") |
5526 |
+checkmessage("local a = 2e100 ~ 1", "has no integer representation") |
5527 |
+checkmessage("string.sub('a', 2.0^100)", "has no integer representation") |
5528 |
+checkmessage("string.rep('a', 3.3)", "has no integer representation") |
5529 |
+checkmessage("return 6e40 & 7", "has no integer representation") |
5530 |
+checkmessage("return 34 << 7e30", "has no integer representation") |
5531 |
+checkmessage("return ~-3e40", "has no integer representation") |
5532 |
+checkmessage("return ~-3.009", "has no integer representation") |
5533 |
+checkmessage("return 3.009 & 1", "has no integer representation") |
5534 |
+checkmessage("return 34 >> {}", "table value") |
5535 |
+checkmessage("a = 24 // 0", "divide by zero") |
5536 |
+checkmessage("a = 1 % 0", "'n%0'") |
5537 |
+ |
5538 |
+ |
5539 |
+-- passing light userdata instead of full userdata |
5540 |
+_G.D = debug |
5541 |
+checkmessage([[ |
5542 |
+ -- create light udata |
5543 |
+ local x = D.upvalueid(function () return debug end, 1) |
5544 |
+ D.setuservalue(x, {}) |
5545 |
+]], "light userdata") |
5546 |
+_G.D = nil |
5547 |
+ |
5548 |
+do -- named objects (field '__name') |
5549 |
+ checkmessage("math.sin(io.input())", "(number expected, got FILE*)") |
5550 |
+ _G.XX = setmetatable({}, {__name = "My Type"}) |
5551 |
+ assert(string.find(tostring(XX), "^My Type")) |
5552 |
+ checkmessage("io.input(XX)", "(FILE* expected, got My Type)") |
5553 |
+ checkmessage("return XX + 1", "on a My Type value") |
5554 |
+ checkmessage("return ~io.stdin", "on a FILE* value") |
5555 |
+ checkmessage("return XX < XX", "two My Type values") |
5556 |
+ checkmessage("return {} < XX", "table with My Type") |
5557 |
+ checkmessage("return XX < io.stdin", "My Type with FILE*") |
5558 |
+ _G.XX = nil |
5559 |
+end |
5560 |
+ |
5561 |
+-- global functions |
5562 |
+checkmessage("(io.write or print){}", "io.write") |
5563 |
+checkmessage("(collectgarbage or print){}", "collectgarbage") |
5564 |
+ |
5565 |
+-- errors in functions without debug info |
5566 |
+do |
5567 |
+ local f = function (a) return a + 1 end |
5568 |
+ f = assert(load(string.dump(f, true))) |
5569 |
+ assert(f(3) == 4) |
5570 |
+ checkerr("^%?:%-1:", f, {}) |
5571 |
+ |
5572 |
+ -- code with a move to a local var ('OP_MOV A B' with A<B) |
5573 |
+ f = function () local a; a = {}; return a + 2 end |
5574 |
+ -- no debug info (so that 'a' is unknown) |
5575 |
+ f = assert(load(string.dump(f, true))) |
5576 |
+ -- symbolic execution should not get lost |
5577 |
+ checkerr("^%?:%-1:.*table value", f) |
5578 |
+end |
5579 |
+ |
5580 |
+ |
5581 |
+-- tests for field accesses after RK limit |
5582 |
+local t = {} |
5583 |
+for i = 1, 1000 do |
5584 |
+ t[i] = "a = x" .. i |
5585 |
+end |
5586 |
+local s = table.concat(t, "; ") |
5587 |
+t = nil |
5588 |
+checkmessage(s.."; a = bbb + 1", "global 'bbb'") |
5589 |
+checkmessage("local _ENV=_ENV;"..s.."; a = bbb + 1", "global 'bbb'") |
5590 |
+checkmessage(s.."; local t = {}; a = t.bbb + 1", "field 'bbb'") |
5591 |
+checkmessage(s.."; local t = {}; t:bbb()", "method 'bbb'") |
5592 |
+ |
5593 |
+checkmessage([[aaa=9 |
5594 |
+repeat until 3==3 |
5595 |
+local x=math.sin(math.cos(3)) |
5596 |
+if math.sin(1) == x then return math.sin(1) end -- tail call |
5597 |
+local a,b = 1, { |
5598 |
+ {x='a'..'b'..'c', y='b', z=x}, |
5599 |
+ {1,2,3,4,5} or 3+3<=3+3, |
5600 |
+ 3+1>3+1, |
5601 |
+ {d = x and aaa[x or y]}} |
5602 |
+]], "global 'aaa'") |
5603 |
+ |
5604 |
+checkmessage([[ |
5605 |
+local x,y = {},1 |
5606 |
+if math.sin(1) == 0 then return 3 end -- return |
5607 |
+x.a()]], "field 'a'") |
5608 |
+ |
5609 |
+checkmessage([[ |
5610 |
+prefix = nil |
5611 |
+insert = nil |
5612 |
+while 1 do |
5613 |
+ local a |
5614 |
+ if nil then break end |
5615 |
+ insert(prefix, a) |
5616 |
+end]], "global 'insert'") |
5617 |
+ |
5618 |
+checkmessage([[ -- tail call |
5619 |
+ return math.sin("a") |
5620 |
+]], "'sin'") |
5621 |
+ |
5622 |
+checkmessage([[collectgarbage("nooption")]], "invalid option") |
5623 |
+ |
5624 |
+checkmessage([[x = print .. "a"]], "concatenate") |
5625 |
+checkmessage([[x = "a" .. false]], "concatenate") |
5626 |
+checkmessage([[x = {} .. 2]], "concatenate") |
5627 |
+ |
5628 |
+checkmessage("getmetatable(io.stdin).__gc()", "no value") |
5629 |
+ |
5630 |
+checkmessage([[ |
5631 |
+local Var |
5632 |
+local function main() |
5633 |
+ NoSuchName (function() Var=0 end) |
5634 |
+end |
5635 |
+main() |
5636 |
+]], "global 'NoSuchName'") |
5637 |
+print'+' |
5638 |
+ |
5639 |
+a = {}; setmetatable(a, {__index = string}) |
5640 |
+checkmessage("a:sub()", "bad self") |
5641 |
+checkmessage("string.sub('a', {})", "#2") |
5642 |
+checkmessage("('a'):sub{}", "#1") |
5643 |
+ |
5644 |
+checkmessage("table.sort({1,2,3}, table.sort)", "'table.sort'") |
5645 |
+checkmessage("string.gsub('s', 's', setmetatable)", "'setmetatable'") |
5646 |
+ |
5647 |
+-- tests for errors in coroutines |
5648 |
+ |
5649 |
+local function f (n) |
5650 |
+ local c = coroutine.create(f) |
5651 |
+ local a,b = coroutine.resume(c) |
5652 |
+ return b |
5653 |
+end |
5654 |
+assert(string.find(f(), "C stack overflow")) |
5655 |
+ |
5656 |
+checkmessage("coroutine.yield()", "outside a coroutine") |
5657 |
+ |
5658 |
+f = coroutine.wrap(function () table.sort({1,2,3}, coroutine.yield) end) |
5659 |
+checkerr("yield across", f) |
5660 |
+ |
5661 |
+ |
5662 |
+-- testing size of 'source' info; size of buffer for that info is |
5663 |
+-- LUA_IDSIZE, declared as 60 in luaconf. Get one position for '\0'. |
5664 |
+idsize = 60 - 1 |
5665 |
+local function checksize (source) |
5666 |
+ -- syntax error |
5667 |
+ local _, msg = load("x", source) |
5668 |
+ msg = string.match(msg, "^([^:]*):") -- get source (1st part before ':') |
5669 |
+ assert(msg:len() <= idsize) |
5670 |
+end |
5671 |
+ |
5672 |
+for i = 60 - 10, 60 + 10 do -- check border cases around 60 |
5673 |
+ checksize("@" .. string.rep("x", i)) -- file names |
5674 |
+ checksize(string.rep("x", i - 10)) -- string sources |
5675 |
+ checksize("=" .. string.rep("x", i)) -- exact sources |
5676 |
+end |
5677 |
+ |
5678 |
+ |
5679 |
+-- testing line error |
5680 |
+ |
5681 |
+local function lineerror (s, l) |
5682 |
+ local err,msg = pcall(load(s)) |
5683 |
+ local line = string.match(msg, ":(%d+):") |
5684 |
+ assert((line and line+0) == l) |
5685 |
+end |
5686 |
+ |
5687 |
+lineerror("local a\n for i=1,'a' do \n print(i) \n end", 2) |
5688 |
+lineerror("\n local a \n for k,v in 3 \n do \n print(k) \n end", 3) |
5689 |
+lineerror("\n\n for k,v in \n 3 \n do \n print(k) \n end", 4) |
5690 |
+lineerror("function a.x.y ()\na=a+1\nend", 1) |
5691 |
+ |
5692 |
+lineerror("a = \na\n+\n{}", 3) |
5693 |
+lineerror("a = \n3\n+\n(\n4\n/\nprint)", 6) |
5694 |
+lineerror("a = \nprint\n+\n(\n4\n/\n7)", 3) |
5695 |
+ |
5696 |
+lineerror("a\n=\n-\n\nprint\n;", 3) |
5697 |
+ |
5698 |
+lineerror([[ |
5699 |
+a |
5700 |
+( |
5701 |
+23) |
5702 |
+]], 1) |
5703 |
+ |
5704 |
+lineerror([[ |
5705 |
+local a = {x = 13} |
5706 |
+a |
5707 |
+. |
5708 |
+x |
5709 |
+( |
5710 |
+23 |
5711 |
+) |
5712 |
+]], 2) |
5713 |
+ |
5714 |
+lineerror([[ |
5715 |
+local a = {x = 13} |
5716 |
+a |
5717 |
+. |
5718 |
+x |
5719 |
+( |
5720 |
+23 + a |
5721 |
+) |
5722 |
+]], 6) |
5723 |
+ |
5724 |
+local p = [[ |
5725 |
+ function g() f() end |
5726 |
+ function f(x) error('a', X) end |
5727 |
+g() |
5728 |
+]] |
5729 |
+X=3;lineerror((p), 3) |
5730 |
+X=0;lineerror((p), nil) |
5731 |
+X=1;lineerror((p), 2) |
5732 |
+X=2;lineerror((p), 1) |
5733 |
+ |
5734 |
+ |
5735 |
+if not _soft then |
5736 |
+ -- several tests that exaust the Lua stack |
5737 |
+ collectgarbage() |
5738 |
+ print"testing stack overflow" |
5739 |
+ C = 0 |
5740 |
+ local l = debug.getinfo(1, "l").currentline; function y () C=C+1; y() end |
5741 |
+ |
5742 |
+ local function checkstackmessage (m) |
5743 |
+ return (string.find(m, "^.-:%d+: stack overflow")) |
5744 |
+ end |
5745 |
+ -- repeated stack overflows (to check stack recovery) |
5746 |
+ assert(checkstackmessage(doit('y()'))) |
5747 |
+ print('+') |
5748 |
+ assert(checkstackmessage(doit('y()'))) |
5749 |
+ print('+') |
5750 |
+ assert(checkstackmessage(doit('y()'))) |
5751 |
+ print('+') |
5752 |
+ |
5753 |
+ |
5754 |
+ -- error lines in stack overflow |
5755 |
+ C = 0 |
5756 |
+ local l1 |
5757 |
+ local function g(x) |
5758 |
+ l1 = debug.getinfo(x, "l").currentline; y() |
5759 |
+ end |
5760 |
+ local _, stackmsg = xpcall(g, debug.traceback, 1) |
5761 |
+ print('+') |
5762 |
+ local stack = {} |
5763 |
+ for line in string.gmatch(stackmsg, "[^\n]*") do |
5764 |
+ local curr = string.match(line, ":(%d+):") |
5765 |
+ if curr then table.insert(stack, tonumber(curr)) end |
5766 |
+ end |
5767 |
+ local i=1 |
5768 |
+ while stack[i] ~= l1 do |
5769 |
+ assert(stack[i] == l) |
5770 |
+ i = i+1 |
5771 |
+ end |
5772 |
+ assert(i > 15) |
5773 |
+ |
5774 |
+ |
5775 |
+ -- error in error handling |
5776 |
+ local res, msg = xpcall(error, error) |
5777 |
+ assert(not res and type(msg) == 'string') |
5778 |
+ print('+') |
5779 |
+ |
5780 |
+ local function f (x) |
5781 |
+ if x==0 then error('a\n') |
5782 |
+ else |
5783 |
+ local aux = function () return f(x-1) end |
5784 |
+ local a,b = xpcall(aux, aux) |
5785 |
+ return a,b |
5786 |
+ end |
5787 |
+ end |
5788 |
+ f(3) |
5789 |
+ |
5790 |
+ local function loop (x,y,z) return 1 + loop(x, y, z) end |
5791 |
+ |
5792 |
+ local res, msg = xpcall(loop, function (m) |
5793 |
+ assert(string.find(m, "stack overflow")) |
5794 |
+ checkerr("error handling", loop) |
5795 |
+ assert(math.sin(0) == 0) |
5796 |
+ return 15 |
5797 |
+ end) |
5798 |
+ assert(msg == 15) |
5799 |
+ |
5800 |
+ local f = function () |
5801 |
+ for i = 999900, 1000000, 1 do table.unpack({}, 1, i) end |
5802 |
+ end |
5803 |
+ checkerr("too many results", f) |
5804 |
+ |
5805 |
+end |
5806 |
+ |
5807 |
+ |
5808 |
+do |
5809 |
+ -- non string messages |
5810 |
+ local t = {} |
5811 |
+ local res, msg = pcall(function () error(t) end) |
5812 |
+ assert(not res and msg == t) |
5813 |
+ |
5814 |
+ res, msg = pcall(function () error(nil) end) |
5815 |
+ assert(not res and msg == nil) |
5816 |
+ |
5817 |
+ local function f() error{msg='x'} end |
5818 |
+ res, msg = xpcall(f, function (r) return {msg=r.msg..'y'} end) |
5819 |
+ assert(msg.msg == 'xy') |
5820 |
+ |
5821 |
+ -- 'assert' with extra arguments |
5822 |
+ res, msg = pcall(assert, false, "X", t) |
5823 |
+ assert(not res and msg == "X") |
5824 |
+ |
5825 |
+ -- 'assert' with no message |
5826 |
+ res, msg = pcall(function () assert(false) end) |
5827 |
+ local line = string.match(msg, "%w+%.lua:(%d+): assertion failed!$") |
5828 |
+ assert(tonumber(line) == debug.getinfo(1, "l").currentline - 2) |
5829 |
+ |
5830 |
+ -- 'assert' with non-string messages |
5831 |
+ res, msg = pcall(assert, false, t) |
5832 |
+ assert(not res and msg == t) |
5833 |
+ |
5834 |
+ res, msg = pcall(assert, nil, nil) |
5835 |
+ assert(not res and msg == nil) |
5836 |
+ |
5837 |
+ -- 'assert' without arguments |
5838 |
+ res, msg = pcall(assert) |
5839 |
+ assert(not res and string.find(msg, "value expected")) |
5840 |
+end |
5841 |
+ |
5842 |
+-- xpcall with arguments |
5843 |
+a, b, c = xpcall(string.find, error, "alo", "al") |
5844 |
+assert(a and b == 1 and c == 2) |
5845 |
+a, b, c = xpcall(string.find, function (x) return {} end, true, "al") |
5846 |
+assert(not a and type(b) == "table" and c == nil) |
5847 |
+ |
5848 |
+ |
5849 |
+print("testing tokens in error messages") |
5850 |
+checksyntax("syntax error", "", "error", 1) |
5851 |
+checksyntax("1.000", "", "1.000", 1) |
5852 |
+checksyntax("[[a]]", "", "[[a]]", 1) |
5853 |
+checksyntax("'aa'", "", "'aa'", 1) |
5854 |
+checksyntax("while << do end", "", "<<", 1) |
5855 |
+checksyntax("for >> do end", "", ">>", 1) |
5856 |
+ |
5857 |
+-- test invalid non-printable char in a chunk |
5858 |
+checksyntax("a\1a = 1", "", "<\\1>", 1) |
5859 |
+ |
5860 |
+-- test 255 as first char in a chunk |
5861 |
+checksyntax("\255a = 1", "", "<\\255>", 1) |
5862 |
+ |
5863 |
+doit('I = load("a=9+"); a=3') |
5864 |
+assert(a==3 and I == nil) |
5865 |
+print('+') |
5866 |
+ |
5867 |
+lim = 1000 |
5868 |
+if _soft then lim = 100 end |
5869 |
+for i=1,lim do |
5870 |
+ doit('a = ') |
5871 |
+ doit('a = 4+nil') |
5872 |
+end |
5873 |
+ |
5874 |
+ |
5875 |
+-- testing syntax limits |
5876 |
+ |
5877 |
+local maxClevel = 200 -- LUAI_MAXCCALLS (in llimits.h) |
5878 |
+ |
5879 |
+local function testrep (init, rep, close, repc) |
5880 |
+ local s = init .. string.rep(rep, maxClevel - 10) .. close .. |
5881 |
+ string.rep(repc, maxClevel - 10) |
5882 |
+ assert(load(s)) -- 190 levels is OK |
5883 |
+ s = init .. string.rep(rep, maxClevel + 1) |
5884 |
+ checkmessage(s, "too many C levels") |
5885 |
+end |
5886 |
+ |
5887 |
+testrep("local a; a", ",a", "= 1", ",1") -- multiple assignment |
5888 |
+testrep("local a; a=", "{", "0", "}") |
5889 |
+testrep("local a; a=", "(", "2", ")") |
5890 |
+testrep("local a; ", "a(", "2", ")") |
5891 |
+testrep("", "do ", "", " end") |
5892 |
+testrep("", "while a do ", "", " end") |
5893 |
+testrep("local a; ", "if a then else ", "", " end") |
5894 |
+testrep("", "function foo () ", "", " end") |
5895 |
+testrep("local a; a=", "a..", "a", "") |
5896 |
+testrep("local a; a=", "a^", "a", "") |
5897 |
+ |
5898 |
+checkmessage("a = f(x" .. string.rep(",x", 260) .. ")", "too many registers") |
5899 |
+ |
5900 |
+ |
5901 |
+-- testing other limits |
5902 |
+ |
5903 |
+-- upvalues |
5904 |
+local lim = 127 |
5905 |
+local s = "local function fooA ()\n local " |
5906 |
+for j = 1,lim do |
5907 |
+ s = s.."a"..j..", " |
5908 |
+end |
5909 |
+s = s.."b,c\n" |
5910 |
+s = s.."local function fooB ()\n local " |
5911 |
+for j = 1,lim do |
5912 |
+ s = s.."b"..j..", " |
5913 |
+end |
5914 |
+s = s.."b\n" |
5915 |
+s = s.."function fooC () return b+c" |
5916 |
+local c = 1+2 |
5917 |
+for j = 1,lim do |
5918 |
+ s = s.."+a"..j.."+b"..j |
5919 |
+ c = c + 2 |
5920 |
+end |
5921 |
+s = s.."\nend end end" |
5922 |
+local a,b = load(s) |
5923 |
+assert(c > 255 and string.find(b, "too many upvalues") and |
5924 |
+ string.find(b, "line 5")) |
5925 |
+ |
5926 |
+-- local variables |
5927 |
+s = "\nfunction foo ()\n local " |
5928 |
+for j = 1,300 do |
5929 |
+ s = s.."a"..j..", " |
5930 |
+end |
5931 |
+s = s.."b\n" |
5932 |
+local a,b = load(s) |
5933 |
+assert(string.find(b, "line 2") and string.find(b, "too many local variables")) |
5934 |
+ |
5935 |
+mt.__index = oldmm |
5936 |
+ |
5937 |
+print('OK') |
5938 |
|
5939 |
diff --git a/tests/events.lua b/tests/events.lua |
5940 |
new file mode 100644 |
5941 |
index 0000000..9136f99 |
5942 |
--- /dev/null |
5943 |
+++ b/tests/events.lua |
5944 |
@@ -0,0 +1,456 @@ |
5945 |
+-- $Id: events.lua,v 1.45 2016/12/21 19:23:02 roberto Exp $ |
5946 |
+-- See Copyright Notice in file all.lua |
5947 |
+ |
5948 |
+print('testing metatables') |
5949 |
+ |
5950 |
+local debug = require'debug' |
5951 |
+ |
5952 |
+X = 20; B = 30 |
5953 |
+ |
5954 |
+_ENV = setmetatable({}, {__index=_G}) |
5955 |
+ |
5956 |
+collectgarbage() |
5957 |
+ |
5958 |
+X = X+10 |
5959 |
+assert(X == 30 and _G.X == 20) |
5960 |
+B = false |
5961 |
+assert(B == false) |
5962 |
+B = nil |
5963 |
+assert(B == 30) |
5964 |
+ |
5965 |
+assert(getmetatable{} == nil) |
5966 |
+assert(getmetatable(4) == nil) |
5967 |
+assert(getmetatable(nil) == nil) |
5968 |
+a={name = "NAME"}; setmetatable(a, {__metatable = "xuxu", |
5969 |
+ __tostring=function(x) return x.name end}) |
5970 |
+assert(getmetatable(a) == "xuxu") |
5971 |
+assert(tostring(a) == "NAME") |
5972 |
+-- cannot change a protected metatable |
5973 |
+assert(pcall(setmetatable, a, {}) == false) |
5974 |
+a.name = "gororoba" |
5975 |
+assert(tostring(a) == "gororoba") |
5976 |
+ |
5977 |
+local a, t = {10,20,30; x="10", y="20"}, {} |
5978 |
+assert(setmetatable(a,t) == a) |
5979 |
+assert(getmetatable(a) == t) |
5980 |
+assert(setmetatable(a,nil) == a) |
5981 |
+assert(getmetatable(a) == nil) |
5982 |
+assert(setmetatable(a,t) == a) |
5983 |
+ |
5984 |
+ |
5985 |
+function f (t, i, e) |
5986 |
+ assert(not e) |
5987 |
+ local p = rawget(t, "parent") |
5988 |
+ return (p and p[i]+3), "dummy return" |
5989 |
+end |
5990 |
+ |
5991 |
+t.__index = f |
5992 |
+ |
5993 |
+a.parent = {z=25, x=12, [4] = 24} |
5994 |
+assert(a[1] == 10 and a.z == 28 and a[4] == 27 and a.x == "10") |
5995 |
+ |
5996 |
+collectgarbage() |
5997 |
+ |
5998 |
+a = setmetatable({}, t) |
5999 |
+function f(t, i, v) rawset(t, i, v-3) end |
6000 |
+setmetatable(t, t) -- causes a bug in 5.1 ! |
6001 |
+t.__newindex = f |
6002 |
+a[1] = 30; a.x = "101"; a[5] = 200 |
6003 |
+assert(a[1] == 27 and a.x == 98 and a[5] == 197) |
6004 |
+ |
6005 |
+do -- bug in Lua 5.3.2 |
6006 |
+ local mt = {} |
6007 |
+ mt.__newindex = mt |
6008 |
+ local t = setmetatable({}, mt) |
6009 |
+ t[1] = 10 -- will segfault on some machines |
6010 |
+ assert(mt[1] == 10) |
6011 |
+end |
6012 |
+ |
6013 |
+ |
6014 |
+local c = {} |
6015 |
+a = setmetatable({}, t) |
6016 |
+t.__newindex = c |
6017 |
+a[1] = 10; a[2] = 20; a[3] = 90 |
6018 |
+assert(c[1] == 10 and c[2] == 20 and c[3] == 90) |
6019 |
+ |
6020 |
+ |
6021 |
+do |
6022 |
+ local a; |
6023 |
+ a = setmetatable({}, {__index = setmetatable({}, |
6024 |
+ {__index = setmetatable({}, |
6025 |
+ {__index = function (_,n) return a[n-3]+4, "lixo" end})})}) |
6026 |
+ a[0] = 20 |
6027 |
+ for i=0,10 do |
6028 |
+ assert(a[i*3] == 20 + i*4) |
6029 |
+ end |
6030 |
+end |
6031 |
+ |
6032 |
+ |
6033 |
+do -- newindex |
6034 |
+ local foi |
6035 |
+ local a = {} |
6036 |
+ for i=1,10 do a[i] = 0; a['a'..i] = 0; end |
6037 |
+ setmetatable(a, {__newindex = function (t,k,v) foi=true; rawset(t,k,v) end}) |
6038 |
+ foi = false; a[1]=0; assert(not foi) |
6039 |
+ foi = false; a['a1']=0; assert(not foi) |
6040 |
+ foi = false; a['a11']=0; assert(foi) |
6041 |
+ foi = false; a[11]=0; assert(foi) |
6042 |
+ foi = false; a[1]=nil; assert(not foi) |
6043 |
+ foi = false; a[1]=nil; assert(foi) |
6044 |
+end |
6045 |
+ |
6046 |
+ |
6047 |
+setmetatable(t, nil) |
6048 |
+function f (t, ...) return t, {...} end |
6049 |
+t.__call = f |
6050 |
+ |
6051 |
+do |
6052 |
+ local x,y = a(table.unpack{'a', 1}) |
6053 |
+ assert(x==a and y[1]=='a' and y[2]==1 and y[3]==nil) |
6054 |
+ x,y = a() |
6055 |
+ assert(x==a and y[1]==nil) |
6056 |
+end |
6057 |
+ |
6058 |
+ |
6059 |
+local b = setmetatable({}, t) |
6060 |
+setmetatable(b,t) |
6061 |
+ |
6062 |
+function f(op) |
6063 |
+ return function (...) cap = {[0] = op, ...} ; return (...) end |
6064 |
+end |
6065 |
+t.__add = f("add") |
6066 |
+t.__sub = f("sub") |
6067 |
+t.__mul = f("mul") |
6068 |
+t.__div = f("div") |
6069 |
+t.__idiv = f("idiv") |
6070 |
+t.__mod = f("mod") |
6071 |
+t.__unm = f("unm") |
6072 |
+t.__pow = f("pow") |
6073 |
+t.__len = f("len") |
6074 |
+t.__band = f("band") |
6075 |
+t.__bor = f("bor") |
6076 |
+t.__bxor = f("bxor") |
6077 |
+t.__shl = f("shl") |
6078 |
+t.__shr = f("shr") |
6079 |
+t.__bnot = f("bnot") |
6080 |
+ |
6081 |
+assert(b+5 == b) |
6082 |
+assert(cap[0] == "add" and cap[1] == b and cap[2] == 5 and cap[3]==nil) |
6083 |
+assert(b+'5' == b) |
6084 |
+assert(cap[0] == "add" and cap[1] == b and cap[2] == '5' and cap[3]==nil) |
6085 |
+assert(5+b == 5) |
6086 |
+assert(cap[0] == "add" and cap[1] == 5 and cap[2] == b and cap[3]==nil) |
6087 |
+assert('5'+b == '5') |
6088 |
+assert(cap[0] == "add" and cap[1] == '5' and cap[2] == b and cap[3]==nil) |
6089 |
+b=b-3; assert(getmetatable(b) == t) |
6090 |
+assert(5-a == 5) |
6091 |
+assert(cap[0] == "sub" and cap[1] == 5 and cap[2] == a and cap[3]==nil) |
6092 |
+assert('5'-a == '5') |
6093 |
+assert(cap[0] == "sub" and cap[1] == '5' and cap[2] == a and cap[3]==nil) |
6094 |
+assert(a*a == a) |
6095 |
+assert(cap[0] == "mul" and cap[1] == a and cap[2] == a and cap[3]==nil) |
6096 |
+assert(a/0 == a) |
6097 |
+assert(cap[0] == "div" and cap[1] == a and cap[2] == 0 and cap[3]==nil) |
6098 |
+assert(a%2 == a) |
6099 |
+assert(cap[0] == "mod" and cap[1] == a and cap[2] == 2 and cap[3]==nil) |
6100 |
+assert(a // (1/0) == a) |
6101 |
+assert(cap[0] == "idiv" and cap[1] == a and cap[2] == 1/0 and cap[3]==nil) |
6102 |
+assert(a & "hi" == a) |
6103 |
+assert(cap[0] == "band" and cap[1] == a and cap[2] == "hi" and cap[3]==nil) |
6104 |
+assert(a | "hi" == a) |
6105 |
+assert(cap[0] == "bor" and cap[1] == a and cap[2] == "hi" and cap[3]==nil) |
6106 |
+assert("hi" ~ a == "hi") |
6107 |
+assert(cap[0] == "bxor" and cap[1] == "hi" and cap[2] == a and cap[3]==nil) |
6108 |
+assert(-a == a) |
6109 |
+assert(cap[0] == "unm" and cap[1] == a) |
6110 |
+assert(a^4 == a) |
6111 |
+assert(cap[0] == "pow" and cap[1] == a and cap[2] == 4 and cap[3]==nil) |
6112 |
+assert(a^'4' == a) |
6113 |
+assert(cap[0] == "pow" and cap[1] == a and cap[2] == '4' and cap[3]==nil) |
6114 |
+assert(4^a == 4) |
6115 |
+assert(cap[0] == "pow" and cap[1] == 4 and cap[2] == a and cap[3]==nil) |
6116 |
+assert('4'^a == '4') |
6117 |
+assert(cap[0] == "pow" and cap[1] == '4' and cap[2] == a and cap[3]==nil) |
6118 |
+assert(#a == a) |
6119 |
+assert(cap[0] == "len" and cap[1] == a) |
6120 |
+assert(~a == a) |
6121 |
+assert(cap[0] == "bnot" and cap[1] == a) |
6122 |
+assert(a << 3 == a) |
6123 |
+assert(cap[0] == "shl" and cap[1] == a and cap[2] == 3) |
6124 |
+assert(1.5 >> a == 1.5) |
6125 |
+assert(cap[0] == "shr" and cap[1] == 1.5 and cap[2] == a) |
6126 |
+ |
6127 |
+ |
6128 |
+-- test for rawlen |
6129 |
+t = setmetatable({1,2,3}, {__len = function () return 10 end}) |
6130 |
+assert(#t == 10 and rawlen(t) == 3) |
6131 |
+assert(rawlen"abc" == 3) |
6132 |
+assert(not pcall(rawlen, io.stdin)) |
6133 |
+assert(not pcall(rawlen, 34)) |
6134 |
+assert(not pcall(rawlen)) |
6135 |
+ |
6136 |
+-- rawlen for long strings |
6137 |
+assert(rawlen(string.rep('a', 1000)) == 1000) |
6138 |
+ |
6139 |
+ |
6140 |
+t = {} |
6141 |
+t.__lt = function (a,b,c) |
6142 |
+ collectgarbage() |
6143 |
+ assert(c == nil) |
6144 |
+ if type(a) == 'table' then a = a.x end |
6145 |
+ if type(b) == 'table' then b = b.x end |
6146 |
+ return a<b, "dummy" |
6147 |
+end |
6148 |
+ |
6149 |
+function Op(x) return setmetatable({x=x}, t) end |
6150 |
+ |
6151 |
+local function test () |
6152 |
+ assert(not(Op(1)<Op(1)) and (Op(1)<Op(2)) and not(Op(2)<Op(1))) |
6153 |
+ assert(not(1 < Op(1)) and (Op(1) < 2) and not(2 < Op(1))) |
6154 |
+ assert(not(Op('a')<Op('a')) and (Op('a')<Op('b')) and not(Op('b')<Op('a'))) |
6155 |
+ assert(not('a' < Op('a')) and (Op('a') < 'b') and not(Op('b') < Op('a'))) |
6156 |
+ assert((Op(1)<=Op(1)) and (Op(1)<=Op(2)) and not(Op(2)<=Op(1))) |
6157 |
+ assert((Op('a')<=Op('a')) and (Op('a')<=Op('b')) and not(Op('b')<=Op('a'))) |
6158 |
+ assert(not(Op(1)>Op(1)) and not(Op(1)>Op(2)) and (Op(2)>Op(1))) |
6159 |
+ assert(not(Op('a')>Op('a')) and not(Op('a')>Op('b')) and (Op('b')>Op('a'))) |
6160 |
+ assert((Op(1)>=Op(1)) and not(Op(1)>=Op(2)) and (Op(2)>=Op(1))) |
6161 |
+ assert((1 >= Op(1)) and not(1 >= Op(2)) and (Op(2) >= 1)) |
6162 |
+ assert((Op('a')>=Op('a')) and not(Op('a')>=Op('b')) and (Op('b')>=Op('a'))) |
6163 |
+ assert(('a' >= Op('a')) and not(Op('a') >= 'b') and (Op('b') >= Op('a'))) |
6164 |
+end |
6165 |
+ |
6166 |
+test() |
6167 |
+ |
6168 |
+t.__le = function (a,b,c) |
6169 |
+ assert(c == nil) |
6170 |
+ if type(a) == 'table' then a = a.x end |
6171 |
+ if type(b) == 'table' then b = b.x end |
6172 |
+ return a<=b, "dummy" |
6173 |
+end |
6174 |
+ |
6175 |
+test() -- retest comparisons, now using both `lt' and `le' |
6176 |
+ |
6177 |
+ |
6178 |
+-- test `partial order' |
6179 |
+ |
6180 |
+local function rawSet(x) |
6181 |
+ local y = {} |
6182 |
+ for _,k in pairs(x) do y[k] = 1 end |
6183 |
+ return y |
6184 |
+end |
6185 |
+ |
6186 |
+local function Set(x) |
6187 |
+ return setmetatable(rawSet(x), t) |
6188 |
+end |
6189 |
+ |
6190 |
+t.__lt = function (a,b) |
6191 |
+ for k in pairs(a) do |
6192 |
+ if not b[k] then return false end |
6193 |
+ b[k] = nil |
6194 |
+ end |
6195 |
+ return next(b) ~= nil |
6196 |
+end |
6197 |
+ |
6198 |
+t.__le = nil |
6199 |
+ |
6200 |
+assert(Set{1,2,3} < Set{1,2,3,4}) |
6201 |
+assert(not(Set{1,2,3,4} < Set{1,2,3,4})) |
6202 |
+assert((Set{1,2,3,4} <= Set{1,2,3,4})) |
6203 |
+assert((Set{1,2,3,4} >= Set{1,2,3,4})) |
6204 |
+assert((Set{1,3} <= Set{3,5})) -- wrong!! model needs a `le' method ;-) |
6205 |
+ |
6206 |
+t.__le = function (a,b) |
6207 |
+ for k in pairs(a) do |
6208 |
+ if not b[k] then return false end |
6209 |
+ end |
6210 |
+ return true |
6211 |
+end |
6212 |
+ |
6213 |
+assert(not (Set{1,3} <= Set{3,5})) -- now its OK! |
6214 |
+assert(not(Set{1,3} <= Set{3,5})) |
6215 |
+assert(not(Set{1,3} >= Set{3,5})) |
6216 |
+ |
6217 |
+t.__eq = function (a,b) |
6218 |
+ for k in pairs(a) do |
6219 |
+ if not b[k] then return false end |
6220 |
+ b[k] = nil |
6221 |
+ end |
6222 |
+ return next(b) == nil |
6223 |
+end |
6224 |
+ |
6225 |
+local s = Set{1,3,5} |
6226 |
+assert(s == Set{3,5,1}) |
6227 |
+assert(not rawequal(s, Set{3,5,1})) |
6228 |
+assert(rawequal(s, s)) |
6229 |
+assert(Set{1,3,5,1} == rawSet{3,5,1}) |
6230 |
+assert(rawSet{1,3,5,1} == Set{3,5,1}) |
6231 |
+assert(Set{1,3,5} ~= Set{3,5,1,6}) |
6232 |
+ |
6233 |
+-- '__eq' is not used for table accesses |
6234 |
+t[Set{1,3,5}] = 1 |
6235 |
+assert(t[Set{1,3,5}] == nil) |
6236 |
+ |
6237 |
+ |
6238 |
+if not T then |
6239 |
+ (Message or print)('\n >>> testC not active: skipping tests for \z |
6240 |
+userdata equality <<<\n') |
6241 |
+else |
6242 |
+ local u1 = T.newuserdata(0) |
6243 |
+ local u2 = T.newuserdata(0) |
6244 |
+ local u3 = T.newuserdata(0) |
6245 |
+ assert(u1 ~= u2 and u1 ~= u3) |
6246 |
+ debug.setuservalue(u1, 1); |
6247 |
+ debug.setuservalue(u2, 2); |
6248 |
+ debug.setuservalue(u3, 1); |
6249 |
+ debug.setmetatable(u1, {__eq = function (a, b) |
6250 |
+ return debug.getuservalue(a) == debug.getuservalue(b) |
6251 |
+ end}) |
6252 |
+ debug.setmetatable(u2, {__eq = function (a, b) |
6253 |
+ return true |
6254 |
+ end}) |
6255 |
+ assert(u1 == u3 and u3 == u1 and u1 ~= u2) |
6256 |
+ assert(u2 == u1 and u2 == u3 and u3 == u2) |
6257 |
+ assert(u2 ~= {}) -- different types cannot be equal |
6258 |
+end |
6259 |
+ |
6260 |
+ |
6261 |
+t.__concat = function (a,b,c) |
6262 |
+ assert(c == nil) |
6263 |
+ if type(a) == 'table' then a = a.val end |
6264 |
+ if type(b) == 'table' then b = b.val end |
6265 |
+ if A then return a..b |
6266 |
+ else |
6267 |
+ return setmetatable({val=a..b}, t) |
6268 |
+ end |
6269 |
+end |
6270 |
+ |
6271 |
+c = {val="c"}; setmetatable(c, t) |
6272 |
+d = {val="d"}; setmetatable(d, t) |
6273 |
+ |
6274 |
+A = true |
6275 |
+assert(c..d == 'cd') |
6276 |
+assert(0 .."a".."b"..c..d.."e".."f"..(5+3).."g" == "0abcdef8g") |
6277 |
+ |
6278 |
+A = false |
6279 |
+assert((c..d..c..d).val == 'cdcd') |
6280 |
+x = c..d |
6281 |
+assert(getmetatable(x) == t and x.val == 'cd') |
6282 |
+x = 0 .."a".."b"..c..d.."e".."f".."g" |
6283 |
+assert(x.val == "0abcdefg") |
6284 |
+ |
6285 |
+ |
6286 |
+-- concat metamethod x numbers (bug in 5.1.1) |
6287 |
+c = {} |
6288 |
+local x |
6289 |
+setmetatable(c, {__concat = function (a,b) |
6290 |
+ assert(type(a) == "number" and b == c or type(b) == "number" and a == c) |
6291 |
+ return c |
6292 |
+end}) |
6293 |
+assert(c..5 == c and 5 .. c == c) |
6294 |
+assert(4 .. c .. 5 == c and 4 .. 5 .. 6 .. 7 .. c == c) |
6295 |
+ |
6296 |
+ |
6297 |
+-- test comparison compatibilities |
6298 |
+local t1, t2, c, d |
6299 |
+t1 = {}; c = {}; setmetatable(c, t1) |
6300 |
+d = {} |
6301 |
+t1.__eq = function () return true end |
6302 |
+t1.__lt = function () return true end |
6303 |
+setmetatable(d, t1) |
6304 |
+assert(c == d and c < d and not(d <= c)) |
6305 |
+t2 = {} |
6306 |
+t2.__eq = t1.__eq |
6307 |
+t2.__lt = t1.__lt |
6308 |
+setmetatable(d, t2) |
6309 |
+assert(c == d and c < d and not(d <= c)) |
6310 |
+ |
6311 |
+ |
6312 |
+ |
6313 |
+-- test for several levels of calls |
6314 |
+local i |
6315 |
+local tt = { |
6316 |
+ __call = function (t, ...) |
6317 |
+ i = i+1 |
6318 |
+ if t.f then return t.f(...) |
6319 |
+ else return {...} |
6320 |
+ end |
6321 |
+ end |
6322 |
+} |
6323 |
+ |
6324 |
+local a = setmetatable({}, tt) |
6325 |
+local b = setmetatable({f=a}, tt) |
6326 |
+local c = setmetatable({f=b}, tt) |
6327 |
+ |
6328 |
+i = 0 |
6329 |
+x = c(3,4,5) |
6330 |
+assert(i == 3 and x[1] == 3 and x[3] == 5) |
6331 |
+ |
6332 |
+ |
6333 |
+assert(_G.X == 20) |
6334 |
+ |
6335 |
+print'+' |
6336 |
+ |
6337 |
+local _g = _G |
6338 |
+_ENV = setmetatable({}, {__index=function (_,k) return _g[k] end}) |
6339 |
+ |
6340 |
+ |
6341 |
+a = {} |
6342 |
+rawset(a, "x", 1, 2, 3) |
6343 |
+assert(a.x == 1 and rawget(a, "x", 3) == 1) |
6344 |
+ |
6345 |
+print '+' |
6346 |
+ |
6347 |
+-- testing metatables for basic types |
6348 |
+mt = {__index = function (a,b) return a+b end, |
6349 |
+ __len = function (x) return math.floor(x) end} |
6350 |
+debug.setmetatable(10, mt) |
6351 |
+assert(getmetatable(-2) == mt) |
6352 |
+assert((10)[3] == 13) |
6353 |
+assert((10)["3"] == 13) |
6354 |
+assert(#3.45 == 3) |
6355 |
+debug.setmetatable(23, nil) |
6356 |
+assert(getmetatable(-2) == nil) |
6357 |
+ |
6358 |
+debug.setmetatable(true, mt) |
6359 |
+assert(getmetatable(false) == mt) |
6360 |
+mt.__index = function (a,b) return a or b end |
6361 |
+assert((true)[false] == true) |
6362 |
+assert((false)[false] == false) |
6363 |
+debug.setmetatable(false, nil) |
6364 |
+assert(getmetatable(true) == nil) |
6365 |
+ |
6366 |
+debug.setmetatable(nil, mt) |
6367 |
+assert(getmetatable(nil) == mt) |
6368 |
+mt.__add = function (a,b) return (a or 0) + (b or 0) end |
6369 |
+assert(10 + nil == 10) |
6370 |
+assert(nil + 23 == 23) |
6371 |
+assert(nil + nil == 0) |
6372 |
+debug.setmetatable(nil, nil) |
6373 |
+assert(getmetatable(nil) == nil) |
6374 |
+ |
6375 |
+debug.setmetatable(nil, {}) |
6376 |
+ |
6377 |
+ |
6378 |
+-- loops in delegation |
6379 |
+a = {}; setmetatable(a, a); a.__index = a; a.__newindex = a |
6380 |
+assert(not pcall(function (a,b) return a[b] end, a, 10)) |
6381 |
+assert(not pcall(function (a,b,c) a[b] = c end, a, 10, true)) |
6382 |
+ |
6383 |
+-- bug in 5.1 |
6384 |
+T, K, V = nil |
6385 |
+grandparent = {} |
6386 |
+grandparent.__newindex = function(t,k,v) T=t; K=k; V=v end |
6387 |
+ |
6388 |
+parent = {} |
6389 |
+parent.__newindex = parent |
6390 |
+setmetatable(parent, grandparent) |
6391 |
+ |
6392 |
+child = setmetatable({}, parent) |
6393 |
+child.foo = 10 --> CRASH (on some machines) |
6394 |
+assert(T == parent and K == "foo" and V == 10) |
6395 |
+ |
6396 |
+print 'OK' |
6397 |
+ |
6398 |
+return 12 |
6399 |
+ |
6400 |
+ |
6401 |
|
6402 |
diff --git a/tests/files.lua b/tests/files.lua |
6403 |
new file mode 100644 |
6404 |
index 0000000..3cfe12d |
6405 |
--- /dev/null |
6406 |
+++ b/tests/files.lua |
6407 |
@@ -0,0 +1,793 @@ |
6408 |
+-- $Id: files.lua,v 1.95 2016/11/07 13:11:28 roberto Exp $ |
6409 |
+-- See Copyright Notice in file all.lua |
6410 |
+ |
6411 |
+local debug = require "debug" |
6412 |
+ |
6413 |
+local maxint = math.maxinteger |
6414 |
+ |
6415 |
+assert(type(os.getenv"PATH") == "string") |
6416 |
+ |
6417 |
+assert(io.input(io.stdin) == io.stdin) |
6418 |
+assert(not pcall(io.input, "non-existent-file")) |
6419 |
+assert(io.output(io.stdout) == io.stdout) |
6420 |
+ |
6421 |
+ |
6422 |
+local function testerr (msg, f, ...) |
6423 |
+ local stat, err = pcall(f, ...) |
6424 |
+ return (not stat and string.find(err, msg, 1, true)) |
6425 |
+end |
6426 |
+ |
6427 |
+ |
6428 |
+local function checkerr (msg, f, ...) |
6429 |
+ assert(testerr(msg, f, ...)) |
6430 |
+end |
6431 |
+ |
6432 |
+ |
6433 |
+-- cannot close standard files |
6434 |
+assert(not io.close(io.stdin) and |
6435 |
+ not io.stdout:close() and |
6436 |
+ not io.stderr:close()) |
6437 |
+ |
6438 |
+ |
6439 |
+assert(type(io.input()) == "userdata" and io.type(io.output()) == "file") |
6440 |
+assert(type(io.stdin) == "userdata" and io.type(io.stderr) == "file") |
6441 |
+assert(not io.type(8)) |
6442 |
+local a = {}; setmetatable(a, {}) |
6443 |
+assert(not io.type(a)) |
6444 |
+ |
6445 |
+assert(getmetatable(io.input()).__name == "FILE*") |
6446 |
+ |
6447 |
+local a,b,c = io.open('xuxu_nao_existe') |
6448 |
+assert(not a and type(b) == "string" and type(c) == "number") |
6449 |
+ |
6450 |
+a,b,c = io.open('/a/b/c/d', 'w') |
6451 |
+assert(not a and type(b) == "string" and type(c) == "number") |
6452 |
+ |
6453 |
+local file = os.tmpname() |
6454 |
+local f, msg = io.open(file, "w") |
6455 |
+if not f then |
6456 |
+ (Message or print)("'os.tmpname' file cannot be open; skipping file tests") |
6457 |
+ |
6458 |
+else --{ most tests here need tmpname |
6459 |
+f:close() |
6460 |
+ |
6461 |
+print('testing i/o') |
6462 |
+ |
6463 |
+local otherfile = os.tmpname() |
6464 |
+ |
6465 |
+checkerr("invalid mode", io.open, file, "rw") |
6466 |
+checkerr("invalid mode", io.open, file, "rb+") |
6467 |
+checkerr("invalid mode", io.open, file, "r+bk") |
6468 |
+checkerr("invalid mode", io.open, file, "") |
6469 |
+checkerr("invalid mode", io.open, file, "+") |
6470 |
+checkerr("invalid mode", io.open, file, "b") |
6471 |
+assert(io.open(file, "r+b")):close() |
6472 |
+assert(io.open(file, "r+")):close() |
6473 |
+assert(io.open(file, "rb")):close() |
6474 |
+ |
6475 |
+assert(os.setlocale('C', 'all')) |
6476 |
+ |
6477 |
+io.input(io.stdin); io.output(io.stdout); |
6478 |
+ |
6479 |
+os.remove(file) |
6480 |
+assert(not loadfile(file)) |
6481 |
+checkerr("", dofile, file) |
6482 |
+assert(not io.open(file)) |
6483 |
+io.output(file) |
6484 |
+assert(io.output() ~= io.stdout) |
6485 |
+ |
6486 |
+if not _port then -- invalid seek |
6487 |
+ local status, msg, code = io.stdin:seek("set", 1000) |
6488 |
+ assert(not status and type(msg) == "string" and type(code) == "number") |
6489 |
+end |
6490 |
+ |
6491 |
+assert(io.output():seek() == 0) |
6492 |
+assert(io.write("alo alo"):seek() == string.len("alo alo")) |
6493 |
+assert(io.output():seek("cur", -3) == string.len("alo alo")-3) |
6494 |
+assert(io.write("joao")) |
6495 |
+assert(io.output():seek("end") == string.len("alo joao")) |
6496 |
+ |
6497 |
+assert(io.output():seek("set") == 0) |
6498 |
+ |
6499 |
+assert(io.write('"álo"', "{a}\n", "second line\n", "third line \n")) |
6500 |
+assert(io.write('çfourth_line')) |
6501 |
+io.output(io.stdout) |
6502 |
+collectgarbage() -- file should be closed by GC |
6503 |
+assert(io.input() == io.stdin and rawequal(io.output(), io.stdout)) |
6504 |
+print('+') |
6505 |
+ |
6506 |
+-- test GC for files |
6507 |
+collectgarbage() |
6508 |
+for i=1,120 do |
6509 |
+ for i=1,5 do |
6510 |
+ io.input(file) |
6511 |
+ assert(io.open(file, 'r')) |
6512 |
+ io.lines(file) |
6513 |
+ end |
6514 |
+ collectgarbage() |
6515 |
+end |
6516 |
+ |
6517 |
+io.input():close() |
6518 |
+io.close() |
6519 |
+ |
6520 |
+assert(os.rename(file, otherfile)) |
6521 |
+assert(not os.rename(file, otherfile)) |
6522 |
+ |
6523 |
+io.output(io.open(otherfile, "ab")) |
6524 |
+assert(io.write("\n\n\t\t ", 3450, "\n")); |
6525 |
+io.close() |
6526 |
+ |
6527 |
+-- test writing/reading numbers |
6528 |
+f = assert(io.open(file, "w")) |
6529 |
+f:write(maxint, '\n') |
6530 |
+f:write(string.format("0X%x\n", maxint)) |
6531 |
+f:write("0xABCp-3", '\n') |
6532 |
+f:write(0, '\n') |
6533 |
+f:write(-maxint, '\n') |
6534 |
+f:write(string.format("0x%X\n", -maxint)) |
6535 |
+f:write("-0xABCp-3", '\n') |
6536 |
+assert(f:close()) |
6537 |
+f = assert(io.open(file, "r")) |
6538 |
+assert(f:read("n") == maxint) |
6539 |
+assert(f:read("n") == maxint) |
6540 |
+assert(f:read("n") == 0xABCp-3) |
6541 |
+assert(f:read("n") == 0) |
6542 |
+assert(f:read("*n") == -maxint) -- test old format (with '*') |
6543 |
+assert(f:read("n") == -maxint) |
6544 |
+assert(f:read("*n") == -0xABCp-3) -- test old format (with '*') |
6545 |
+assert(f:close()) |
6546 |
+assert(os.remove(file)) |
6547 |
+ |
6548 |
+-- test yielding during 'dofile' |
6549 |
+f = assert(io.open(file, "w")) |
6550 |
+f:write[[ |
6551 |
+local x, z = coroutine.yield(10) |
6552 |
+local y = coroutine.yield(20) |
6553 |
+return x + y * z |
6554 |
+]] |
6555 |
+assert(f:close()) |
6556 |
+f = coroutine.wrap(dofile) |
6557 |
+assert(f(file) == 10) |
6558 |
+print(f(100, 101) == 20) |
6559 |
+assert(f(200) == 100 + 200 * 101) |
6560 |
+assert(os.remove(file)) |
6561 |
+ |
6562 |
+ |
6563 |
+f = assert(io.open(file, "w")) |
6564 |
+-- test number termination |
6565 |
+f:write[[ |
6566 |
+-12.3- -0xffff+ .3|5.E-3X +234e+13E 0xDEADBEEFDEADBEEFx |
6567 |
+0x1.13Ap+3e |
6568 |
+]] |
6569 |
+-- very long number |
6570 |
+f:write("1234"); for i = 1, 1000 do f:write("0") end; f:write("\n") |
6571 |
+-- invalid sequences (must read and discard valid prefixes) |
6572 |
+f:write[[ |
6573 |
+.e+ 0.e; --; 0xX; |
6574 |
+]] |
6575 |
+assert(f:close()) |
6576 |
+f = assert(io.open(file, "r")) |
6577 |
+assert(f:read("n") == -12.3); assert(f:read(1) == "-") |
6578 |
+assert(f:read("n") == -0xffff); assert(f:read(2) == "+ ") |
6579 |
+assert(f:read("n") == 0.3); assert(f:read(1) == "|") |
6580 |
+assert(f:read("n") == 5e-3); assert(f:read(1) == "X") |
6581 |
+assert(f:read("n") == 234e13); assert(f:read(1) == "E") |
6582 |
+assert(f:read("n") == 0Xdeadbeefdeadbeef); assert(f:read(2) == "x\n") |
6583 |
+assert(f:read("n") == 0x1.13aP3); assert(f:read(1) == "e") |
6584 |
+ |
6585 |
+do -- attempt to read too long number |
6586 |
+ assert(f:read("n") == nil) -- fails |
6587 |
+ local s = f:read("L") -- read rest of line |
6588 |
+ assert(string.find(s, "^00*\n$")) -- lots of 0's left |
6589 |
+end |
6590 |
+ |
6591 |
+assert(not f:read("n")); assert(f:read(2) == "e+") |
6592 |
+assert(not f:read("n")); assert(f:read(1) == ";") |
6593 |
+assert(not f:read("n")); assert(f:read(2) == "-;") |
6594 |
+assert(not f:read("n")); assert(f:read(1) == "X") |
6595 |
+assert(not f:read("n")); assert(f:read(1) == ";") |
6596 |
+assert(not f:read("n")); assert(not f:read(0)) -- end of file |
6597 |
+assert(f:close()) |
6598 |
+assert(os.remove(file)) |
6599 |
+ |
6600 |
+ |
6601 |
+-- test line generators |
6602 |
+assert(not pcall(io.lines, "non-existent-file")) |
6603 |
+assert(os.rename(otherfile, file)) |
6604 |
+io.output(otherfile) |
6605 |
+local n = 0 |
6606 |
+local f = io.lines(file) |
6607 |
+while f() do n = n + 1 end; |
6608 |
+assert(n == 6) -- number of lines in the file |
6609 |
+checkerr("file is already closed", f) |
6610 |
+checkerr("file is already closed", f) |
6611 |
+-- copy from file to otherfile |
6612 |
+n = 0 |
6613 |
+for l in io.lines(file) do io.write(l, "\n"); n = n + 1 end |
6614 |
+io.close() |
6615 |
+assert(n == 6) |
6616 |
+-- copy from otherfile back to file |
6617 |
+local f = assert(io.open(otherfile)) |
6618 |
+assert(io.type(f) == "file") |
6619 |
+io.output(file) |
6620 |
+assert(not io.output():read()) |
6621 |
+n = 0 |
6622 |
+for l in f:lines() do io.write(l, "\n"); n = n + 1 end |
6623 |
+assert(tostring(f):sub(1, 5) == "file ") |
6624 |
+assert(f:close()); io.close() |
6625 |
+assert(n == 6) |
6626 |
+checkerr("closed file", io.close, f) |
6627 |
+assert(tostring(f) == "file (closed)") |
6628 |
+assert(io.type(f) == "closed file") |
6629 |
+io.input(file) |
6630 |
+f = io.open(otherfile):lines() |
6631 |
+n = 0 |
6632 |
+for l in io.lines() do assert(l == f()); n = n + 1 end |
6633 |
+f = nil; collectgarbage() |
6634 |
+assert(n == 6) |
6635 |
+assert(os.remove(otherfile)) |
6636 |
+ |
6637 |
+do -- bug in 5.3.1 |
6638 |
+ io.output(otherfile) |
6639 |
+ io.write(string.rep("a", 300), "\n") |
6640 |
+ io.close() |
6641 |
+ local t ={}; for i = 1, 250 do t[i] = 1 end |
6642 |
+ t = {io.lines(otherfile, table.unpack(t))()} |
6643 |
+ -- everything ok here |
6644 |
+ assert(#t == 250 and t[1] == 'a' and t[#t] == 'a') |
6645 |
+ t[#t + 1] = 1 -- one too many |
6646 |
+ checkerr("too many arguments", io.lines, otherfile, table.unpack(t)) |
6647 |
+ collectgarbage() -- ensure 'otherfile' is closed |
6648 |
+ assert(os.remove(otherfile)) |
6649 |
+end |
6650 |
+ |
6651 |
+io.input(file) |
6652 |
+do -- test error returns |
6653 |
+ local a,b,c = io.input():write("xuxu") |
6654 |
+ assert(not a and type(b) == "string" and type(c) == "number") |
6655 |
+end |
6656 |
+checkerr("invalid format", io.read, "x") |
6657 |
+assert(io.read(0) == "") -- not eof |
6658 |
+assert(io.read(5, 'l') == '"álo"') |
6659 |
+assert(io.read(0) == "") |
6660 |
+assert(io.read() == "second line") |
6661 |
+local x = io.input():seek() |
6662 |
+assert(io.read() == "third line ") |
6663 |
+assert(io.input():seek("set", x)) |
6664 |
+assert(io.read('L') == "third line \n") |
6665 |
+assert(io.read(1) == "ç") |
6666 |
+assert(io.read(string.len"fourth_line") == "fourth_line") |
6667 |
+assert(io.input():seek("cur", -string.len"fourth_line")) |
6668 |
+assert(io.read() == "fourth_line") |
6669 |
+assert(io.read() == "") -- empty line |
6670 |
+assert(io.read('n') == 3450) |
6671 |
+assert(io.read(1) == '\n') |
6672 |
+assert(io.read(0) == nil) -- end of file |
6673 |
+assert(io.read(1) == nil) -- end of file |
6674 |
+assert(io.read(30000) == nil) -- end of file |
6675 |
+assert(({io.read(1)})[2] == nil) |
6676 |
+assert(io.read() == nil) -- end of file |
6677 |
+assert(({io.read()})[2] == nil) |
6678 |
+assert(io.read('n') == nil) -- end of file |
6679 |
+assert(({io.read('n')})[2] == nil) |
6680 |
+assert(io.read('a') == '') -- end of file (OK for 'a') |
6681 |
+assert(io.read('a') == '') -- end of file (OK for 'a') |
6682 |
+collectgarbage() |
6683 |
+print('+') |
6684 |
+io.close(io.input()) |
6685 |
+checkerr(" input file is closed", io.read) |
6686 |
+ |
6687 |
+assert(os.remove(file)) |
6688 |
+ |
6689 |
+local t = '0123456789' |
6690 |
+for i=1,10 do t = t..t; end |
6691 |
+assert(string.len(t) == 10*2^10) |
6692 |
+ |
6693 |
+io.output(file) |
6694 |
+io.write("alo"):write("\n") |
6695 |
+io.close() |
6696 |
+checkerr(" output file is closed", io.write) |
6697 |
+local f = io.open(file, "a+b") |
6698 |
+io.output(f) |
6699 |
+collectgarbage() |
6700 |
+ |
6701 |
+assert(io.write(' ' .. t .. ' ')) |
6702 |
+assert(io.write(';', 'end of file\n')) |
6703 |
+f:flush(); io.flush() |
6704 |
+f:close() |
6705 |
+print('+') |
6706 |
+ |
6707 |
+io.input(file) |
6708 |
+assert(io.read() == "alo") |
6709 |
+assert(io.read(1) == ' ') |
6710 |
+assert(io.read(string.len(t)) == t) |
6711 |
+assert(io.read(1) == ' ') |
6712 |
+assert(io.read(0)) |
6713 |
+assert(io.read('a') == ';end of file\n') |
6714 |
+assert(io.read(0) == nil) |
6715 |
+assert(io.close(io.input())) |
6716 |
+ |
6717 |
+ |
6718 |
+-- test errors in read/write |
6719 |
+do |
6720 |
+ local function ismsg (m) |
6721 |
+ -- error message is not a code number |
6722 |
+ return (type(m) == "string" and tonumber(m) == nil) |
6723 |
+ end |
6724 |
+ |
6725 |
+ -- read |
6726 |
+ local f = io.open(file, "w") |
6727 |
+ local r, m, c = f:read() |
6728 |
+ assert(not r and ismsg(m) and type(c) == "number") |
6729 |
+ assert(f:close()) |
6730 |
+ -- write |
6731 |
+ f = io.open(file, "r") |
6732 |
+ r, m, c = f:write("whatever") |
6733 |
+ assert(not r and ismsg(m) and type(c) == "number") |
6734 |
+ assert(f:close()) |
6735 |
+ -- lines |
6736 |
+ f = io.open(file, "w") |
6737 |
+ r, m = pcall(f:lines()) |
6738 |
+ assert(r == false and ismsg(m)) |
6739 |
+ assert(f:close()) |
6740 |
+end |
6741 |
+ |
6742 |
+assert(os.remove(file)) |
6743 |
+ |
6744 |
+-- test for L format |
6745 |
+io.output(file); io.write"\n\nline\nother":close() |
6746 |
+io.input(file) |
6747 |
+assert(io.read"L" == "\n") |
6748 |
+assert(io.read"L" == "\n") |
6749 |
+assert(io.read"L" == "line\n") |
6750 |
+assert(io.read"L" == "other") |
6751 |
+assert(io.read"L" == nil) |
6752 |
+io.input():close() |
6753 |
+ |
6754 |
+local f = assert(io.open(file)) |
6755 |
+local s = "" |
6756 |
+for l in f:lines("L") do s = s .. l end |
6757 |
+assert(s == "\n\nline\nother") |
6758 |
+f:close() |
6759 |
+ |
6760 |
+io.input(file) |
6761 |
+s = "" |
6762 |
+for l in io.lines(nil, "L") do s = s .. l end |
6763 |
+assert(s == "\n\nline\nother") |
6764 |
+io.input():close() |
6765 |
+ |
6766 |
+s = "" |
6767 |
+for l in io.lines(file, "L") do s = s .. l end |
6768 |
+assert(s == "\n\nline\nother") |
6769 |
+ |
6770 |
+s = "" |
6771 |
+for l in io.lines(file, "l") do s = s .. l end |
6772 |
+assert(s == "lineother") |
6773 |
+ |
6774 |
+io.output(file); io.write"a = 10 + 34\na = 2*a\na = -a\n":close() |
6775 |
+local t = {} |
6776 |
+load(io.lines(file, "L"), nil, nil, t)() |
6777 |
+assert(t.a == -((10 + 34) * 2)) |
6778 |
+ |
6779 |
+ |
6780 |
+-- test for multipe arguments in 'lines' |
6781 |
+io.output(file); io.write"0123456789\n":close() |
6782 |
+for a,b in io.lines(file, 1, 1) do |
6783 |
+ if a == "\n" then assert(b == nil) |
6784 |
+ else assert(tonumber(a) == tonumber(b) - 1) |
6785 |
+ end |
6786 |
+end |
6787 |
+ |
6788 |
+for a,b,c in io.lines(file, 1, 2, "a") do |
6789 |
+ assert(a == "0" and b == "12" and c == "3456789\n") |
6790 |
+end |
6791 |
+ |
6792 |
+for a,b,c in io.lines(file, "a", 0, 1) do |
6793 |
+ if a == "" then break end |
6794 |
+ assert(a == "0123456789\n" and b == nil and c == nil) |
6795 |
+end |
6796 |
+collectgarbage() -- to close file in previous iteration |
6797 |
+ |
6798 |
+io.output(file); io.write"00\n10\n20\n30\n40\n":close() |
6799 |
+for a, b in io.lines(file, "n", "n") do |
6800 |
+ if a == 40 then assert(b == nil) |
6801 |
+ else assert(a == b - 10) |
6802 |
+ end |
6803 |
+end |
6804 |
+ |
6805 |
+ |
6806 |
+-- test load x lines |
6807 |
+io.output(file); |
6808 |
+io.write[[ |
6809 |
+local y |
6810 |
+= X |
6811 |
+X = |
6812 |
+X * |
6813 |
+2 + |
6814 |
+X; |
6815 |
+X = |
6816 |
+X |
6817 |
+- y; |
6818 |
+]]:close() |
6819 |
+_G.X = 1 |
6820 |
+assert(not load(io.lines(file))) |
6821 |
+collectgarbage() -- to close file in previous iteration |
6822 |
+load(io.lines(file, "L"))() |
6823 |
+assert(_G.X == 2) |
6824 |
+load(io.lines(file, 1))() |
6825 |
+assert(_G.X == 4) |
6826 |
+load(io.lines(file, 3))() |
6827 |
+assert(_G.X == 8) |
6828 |
+ |
6829 |
+print('+') |
6830 |
+ |
6831 |
+local x1 = "string\n\n\\com \"\"''coisas [[estranhas]] ]]'" |
6832 |
+io.output(file) |
6833 |
+assert(io.write(string.format("x2 = %q\n-- comment without ending EOS", x1))) |
6834 |
+io.close() |
6835 |
+assert(loadfile(file))() |
6836 |
+assert(x1 == x2) |
6837 |
+print('+') |
6838 |
+assert(os.remove(file)) |
6839 |
+assert(not os.remove(file)) |
6840 |
+assert(not os.remove(otherfile)) |
6841 |
+ |
6842 |
+-- testing loadfile |
6843 |
+local function testloadfile (s, expres) |
6844 |
+ io.output(file) |
6845 |
+ if s then io.write(s) end |
6846 |
+ io.close() |
6847 |
+ local res = assert(loadfile(file))() |
6848 |
+ assert(os.remove(file)) |
6849 |
+ assert(res == expres) |
6850 |
+end |
6851 |
+ |
6852 |
+-- loading empty file |
6853 |
+testloadfile(nil, nil) |
6854 |
+ |
6855 |
+-- loading file with initial comment without end of line |
6856 |
+testloadfile("# a non-ending comment", nil) |
6857 |
+ |
6858 |
+ |
6859 |
+-- checking Unicode BOM in files |
6860 |
+testloadfile("\xEF\xBB\xBF# some comment\nreturn 234", 234) |
6861 |
+testloadfile("\xEF\xBB\xBFreturn 239", 239) |
6862 |
+testloadfile("\xEF\xBB\xBF", nil) -- empty file with a BOM |
6863 |
+ |
6864 |
+ |
6865 |
+-- checking line numbers in files with initial comments |
6866 |
+testloadfile("# a comment\nreturn require'debug'.getinfo(1).currentline", 2) |
6867 |
+ |
6868 |
+ |
6869 |
+-- loading binary file |
6870 |
+io.output(io.open(file, "wb")) |
6871 |
+assert(io.write(string.dump(function () return 10, '\0alo\255', 'hi' end))) |
6872 |
+io.close() |
6873 |
+a, b, c = assert(loadfile(file))() |
6874 |
+assert(a == 10 and b == "\0alo\255" and c == "hi") |
6875 |
+assert(os.remove(file)) |
6876 |
+ |
6877 |
+-- bug in 5.2.1 |
6878 |
+do |
6879 |
+ io.output(io.open(file, "wb")) |
6880 |
+ -- save function with no upvalues |
6881 |
+ assert(io.write(string.dump(function () return 1 end))) |
6882 |
+ io.close() |
6883 |
+ f = assert(loadfile(file, "b", {})) |
6884 |
+ assert(type(f) == "function" and f() == 1) |
6885 |
+ assert(os.remove(file)) |
6886 |
+end |
6887 |
+ |
6888 |
+-- loading binary file with initial comment |
6889 |
+io.output(io.open(file, "wb")) |
6890 |
+assert(io.write("#this is a comment for a binary file\0\n", |
6891 |
+ string.dump(function () return 20, '\0\0\0' end))) |
6892 |
+io.close() |
6893 |
+a, b, c = assert(loadfile(file))() |
6894 |
+assert(a == 20 and b == "\0\0\0" and c == nil) |
6895 |
+assert(os.remove(file)) |
6896 |
+ |
6897 |
+ |
6898 |
+-- 'loadfile' with 'env' |
6899 |
+do |
6900 |
+ local f = io.open(file, 'w') |
6901 |
+ f:write[[ |
6902 |
+ if (...) then a = 15; return b, c, d |
6903 |
+ else return _ENV |
6904 |
+ end |
6905 |
+ ]] |
6906 |
+ f:close() |
6907 |
+ local t = {b = 12, c = "xuxu", d = print} |
6908 |
+ local f = assert(loadfile(file, 't', t)) |
6909 |
+ local b, c, d = f(1) |
6910 |
+ assert(t.a == 15 and b == 12 and c == t.c and d == print) |
6911 |
+ assert(f() == t) |
6912 |
+ f = assert(loadfile(file, 't', nil)) |
6913 |
+ assert(f() == nil) |
6914 |
+ f = assert(loadfile(file)) |
6915 |
+ assert(f() == _G) |
6916 |
+ assert(os.remove(file)) |
6917 |
+end |
6918 |
+ |
6919 |
+ |
6920 |
+-- 'loadfile' x modes |
6921 |
+do |
6922 |
+ io.open(file, 'w'):write("return 10"):close() |
6923 |
+ local s, m = loadfile(file, 'b') |
6924 |
+ assert(not s and string.find(m, "a text chunk")) |
6925 |
+ io.open(file, 'w'):write("\27 return 10"):close() |
6926 |
+ local s, m = loadfile(file, 't') |
6927 |
+ assert(not s and string.find(m, "a binary chunk")) |
6928 |
+ assert(os.remove(file)) |
6929 |
+end |
6930 |
+ |
6931 |
+ |
6932 |
+io.output(file) |
6933 |
+assert(io.write("qualquer coisa\n")) |
6934 |
+assert(io.write("mais qualquer coisa")) |
6935 |
+io.close() |
6936 |
+assert(io.output(assert(io.open(otherfile, 'wb'))) |
6937 |
+ :write("outra coisa\0\1\3\0\0\0\0\255\0") |
6938 |
+ :close()) |
6939 |
+ |
6940 |
+local filehandle = assert(io.open(file, 'r+')) |
6941 |
+local otherfilehandle = assert(io.open(otherfile, 'rb')) |
6942 |
+assert(filehandle ~= otherfilehandle) |
6943 |
+assert(type(filehandle) == "userdata") |
6944 |
+assert(filehandle:read('l') == "qualquer coisa") |
6945 |
+io.input(otherfilehandle) |
6946 |
+assert(io.read(string.len"outra coisa") == "outra coisa") |
6947 |
+assert(filehandle:read('l') == "mais qualquer coisa") |
6948 |
+filehandle:close(); |
6949 |
+assert(type(filehandle) == "userdata") |
6950 |
+io.input(otherfilehandle) |
6951 |
+assert(io.read(4) == "\0\1\3\0") |
6952 |
+assert(io.read(3) == "\0\0\0") |
6953 |
+assert(io.read(0) == "") -- 255 is not eof |
6954 |
+assert(io.read(1) == "\255") |
6955 |
+assert(io.read('a') == "\0") |
6956 |
+assert(not io.read(0)) |
6957 |
+assert(otherfilehandle == io.input()) |
6958 |
+otherfilehandle:close() |
6959 |
+assert(os.remove(file)) |
6960 |
+assert(os.remove(otherfile)) |
6961 |
+collectgarbage() |
6962 |
+ |
6963 |
+io.output(file) |
6964 |
+ :write[[ |
6965 |
+ 123.4 -56e-2 not a number |
6966 |
+second line |
6967 |
+third line |
6968 |
+ |
6969 |
+and the rest of the file |
6970 |
+]] |
6971 |
+ :close() |
6972 |
+io.input(file) |
6973 |
+local _,a,b,c,d,e,h,__ = io.read(1, 'n', 'n', 'l', 'l', 'l', 'a', 10) |
6974 |
+assert(io.close(io.input())) |
6975 |
+assert(_ == ' ' and __ == nil) |
6976 |
+assert(type(a) == 'number' and a==123.4 and b==-56e-2) |
6977 |
+assert(d=='second line' and e=='third line') |
6978 |
+assert(h==[[ |
6979 |
+ |
6980 |
+and the rest of the file |
6981 |
+]]) |
6982 |
+assert(os.remove(file)) |
6983 |
+collectgarbage() |
6984 |
+ |
6985 |
+-- testing buffers |
6986 |
+do |
6987 |
+ local f = assert(io.open(file, "w")) |
6988 |
+ local fr = assert(io.open(file, "r")) |
6989 |
+ assert(f:setvbuf("full", 2000)) |
6990 |
+ f:write("x") |
6991 |
+ assert(fr:read("all") == "") -- full buffer; output not written yet |
6992 |
+ f:close() |
6993 |
+ fr:seek("set") |
6994 |
+ assert(fr:read("all") == "x") -- `close' flushes it |
6995 |
+ f = assert(io.open(file), "w") |
6996 |
+ assert(f:setvbuf("no")) |
6997 |
+ f:write("x") |
6998 |
+ fr:seek("set") |
6999 |
+ assert(fr:read("all") == "x") -- no buffer; output is ready |
7000 |
+ f:close() |
7001 |
+ f = assert(io.open(file, "a")) |
7002 |
+ assert(f:setvbuf("line")) |
7003 |
+ f:write("x") |
7004 |
+ fr:seek("set", 1) |
7005 |
+ assert(fr:read("all") == "") -- line buffer; no output without `\n' |
7006 |
+ f:write("a\n"):seek("set", 1) |
7007 |
+ assert(fr:read("all") == "xa\n") -- now we have a whole line |
7008 |
+ f:close(); fr:close() |
7009 |
+ assert(os.remove(file)) |
7010 |
+end |
7011 |
+ |
7012 |
+ |
7013 |
+if not _soft then |
7014 |
+ print("testing large files (> BUFSIZ)") |
7015 |
+ io.output(file) |
7016 |
+ for i=1,5001 do io.write('0123456789123') end |
7017 |
+ io.write('\n12346'):close() |
7018 |
+ io.input(file) |
7019 |
+ local x = io.read('a') |
7020 |
+ io.input():seek('set', 0) |
7021 |
+ local y = io.read(30001)..io.read(1005)..io.read(0).. |
7022 |
+ io.read(1)..io.read(100003) |
7023 |
+ assert(x == y and string.len(x) == 5001*13 + 6) |
7024 |
+ io.input():seek('set', 0) |
7025 |
+ y = io.read() -- huge line |
7026 |
+ assert(x == y..'\n'..io.read()) |
7027 |
+ assert(io.read() == nil) |
7028 |
+ io.close(io.input()) |
7029 |
+ assert(os.remove(file)) |
7030 |
+ x = nil; y = nil |
7031 |
+end |
7032 |
+ |
7033 |
+if not _port then |
7034 |
+ local progname |
7035 |
+ do -- get name of running executable |
7036 |
+ local arg = arg or _ARG |
7037 |
+ local i = 0 |
7038 |
+ while arg[i] do i = i - 1 end |
7039 |
+ progname = '"' .. arg[i + 1] .. '"' |
7040 |
+ end |
7041 |
+ print("testing popen/pclose and execute") |
7042 |
+ local tests = { |
7043 |
+ -- command, what, code |
7044 |
+ {"ls > /dev/null", "ok"}, |
7045 |
+ {"not-to-be-found-command", "exit"}, |
7046 |
+ {"exit 3", "exit", 3}, |
7047 |
+ {"exit 129", "exit", 129}, |
7048 |
+ {"kill -s HUP $$", "signal", 1}, |
7049 |
+ {"kill -s KILL $$", "signal", 9}, |
7050 |
+ {"sh -c 'kill -s HUP $$'", "exit"}, |
7051 |
+ {progname .. ' -e " "', "ok"}, |
7052 |
+ {progname .. ' -e "os.exit(0, true)"', "ok"}, |
7053 |
+ {progname .. ' -e "os.exit(20, true)"', "exit", 20}, |
7054 |
+ } |
7055 |
+ print("\n(some error messages are expected now)") |
7056 |
+ for _, v in ipairs(tests) do |
7057 |
+ local x, y, z = io.popen(v[1]):close() |
7058 |
+ local x1, y1, z1 = os.execute(v[1]) |
7059 |
+ assert(x == x1 and y == y1 and z == z1) |
7060 |
+ if v[2] == "ok" then |
7061 |
+ assert(x and y == 'exit' and z == 0) |
7062 |
+ else |
7063 |
+ assert(not x and y == v[2]) -- correct status and 'what' |
7064 |
+ -- correct code if known (but always different from 0) |
7065 |
+ assert((v[3] == nil and z > 0) or v[3] == z) |
7066 |
+ end |
7067 |
+ end |
7068 |
+end |
7069 |
+ |
7070 |
+ |
7071 |
+-- testing tmpfile |
7072 |
+f = io.tmpfile() |
7073 |
+assert(io.type(f) == "file") |
7074 |
+f:write("alo") |
7075 |
+f:seek("set") |
7076 |
+assert(f:read"a" == "alo") |
7077 |
+ |
7078 |
+end --} |
7079 |
+ |
7080 |
+print'+' |
7081 |
+ |
7082 |
+print("testing date/time") |
7083 |
+ |
7084 |
+assert(os.date("") == "") |
7085 |
+assert(os.date("!") == "") |
7086 |
+assert(os.date("\0\0") == "\0\0") |
7087 |
+assert(os.date("!\0\0") == "\0\0") |
7088 |
+local x = string.rep("a", 10000) |
7089 |
+assert(os.date(x) == x) |
7090 |
+local t = os.time() |
7091 |
+D = os.date("*t", t) |
7092 |
+assert(os.date(string.rep("%d", 1000), t) == |
7093 |
+ string.rep(os.date("%d", t), 1000)) |
7094 |
+assert(os.date(string.rep("%", 200)) == string.rep("%", 100)) |
7095 |
+ |
7096 |
+local t = os.time() |
7097 |
+D = os.date("*t", t) |
7098 |
+load(os.date([[assert(D.year==%Y and D.month==%m and D.day==%d and |
7099 |
+ D.hour==%H and D.min==%M and D.sec==%S and |
7100 |
+ D.wday==%w+1 and D.yday==%j and type(D.isdst) == 'boolean')]], t))() |
7101 |
+ |
7102 |
+checkerr("invalid conversion specifier", os.date, "%") |
7103 |
+checkerr("invalid conversion specifier", os.date, "%9") |
7104 |
+checkerr("invalid conversion specifier", os.date, "%") |
7105 |
+checkerr("invalid conversion specifier", os.date, "%O") |
7106 |
+checkerr("invalid conversion specifier", os.date, "%E") |
7107 |
+checkerr("invalid conversion specifier", os.date, "%Ea") |
7108 |
+ |
7109 |
+checkerr("not an integer", os.time, {year=1000, month=1, day=1, hour='x'}) |
7110 |
+checkerr("not an integer", os.time, {year=1000, month=1, day=1, hour=1.5}) |
7111 |
+ |
7112 |
+checkerr("missing", os.time, {hour = 12}) -- missing date |
7113 |
+ |
7114 |
+if not _port then |
7115 |
+ -- test Posix-specific modifiers |
7116 |
+ assert(type(os.date("%Ex")) == 'string') |
7117 |
+ assert(type(os.date("%Oy")) == 'string') |
7118 |
+ |
7119 |
+ |
7120 |
+ -- test out-of-range dates (at least for Unix) |
7121 |
+ if maxint >= 2^62 then -- cannot do these tests in Small Lua |
7122 |
+ -- no arith overflows |
7123 |
+ checkerr("out-of-bound", os.time, {year = -maxint, month = 1, day = 1}) |
7124 |
+ if string.packsize("i") == 4 then -- 4-byte ints |
7125 |
+ if testerr("out-of-bound", os.date, "%Y", 2^40) then |
7126 |
+ -- time_t has 4 bytes and therefore cannot represent year 4000 |
7127 |
+ print(" 4-byte time_t") |
7128 |
+ checkerr("cannot be represented", os.time, {year=4000, month=1, day=1}) |
7129 |
+ else |
7130 |
+ -- time_t has 8 bytes; an int year cannot represent a huge time |
7131 |
+ print(" 8-byte time_t") |
7132 |
+ checkerr("cannot be represented", os.date, "%Y", 2^60) |
7133 |
+ -- it should have no problems with year 4000 |
7134 |
+ assert(tonumber(os.time{year=4000, month=1, day=1})) |
7135 |
+ end |
7136 |
+ else -- 8-byte ints |
7137 |
+ -- assume time_t has 8 bytes too |
7138 |
+ print(" 8-byte time_t") |
7139 |
+ assert(tonumber(os.date("%Y", 2^60))) |
7140 |
+ -- but still cannot represent a huge year |
7141 |
+ checkerr("cannot be represented", os.time, {year=2^60, month=1, day=1}) |
7142 |
+ end |
7143 |
+ end |
7144 |
+end |
7145 |
+ |
7146 |
+ |
7147 |
+D = os.date("!*t", t) |
7148 |
+load(os.date([[!assert(D.year==%Y and D.month==%m and D.day==%d and |
7149 |
+ D.hour==%H and D.min==%M and D.sec==%S and |
7150 |
+ D.wday==%w+1 and D.yday==%j and type(D.isdst) == 'boolean')]], t))() |
7151 |
+ |
7152 |
+do |
7153 |
+ local D = os.date("*t") |
7154 |
+ local t = os.time(D) |
7155 |
+ assert(type(D.isdst) == 'boolean') |
7156 |
+ D.isdst = nil |
7157 |
+ local t1 = os.time(D) |
7158 |
+ assert(t == t1) -- if isdst is absent uses correct default |
7159 |
+end |
7160 |
+ |
7161 |
+t = os.time(D) |
7162 |
+D.year = D.year-1; |
7163 |
+local t1 = os.time(D) |
7164 |
+-- allow for leap years |
7165 |
+assert(math.abs(os.difftime(t,t1)/(24*3600) - 365) < 2) |
7166 |
+ |
7167 |
+-- should not take more than 1 second to execute these two lines |
7168 |
+t = os.time() |
7169 |
+t1 = os.time(os.date("*t")) |
7170 |
+local diff = os.difftime(t1,t) |
7171 |
+assert(0 <= diff and diff <= 1) |
7172 |
+diff = os.difftime(t,t1) |
7173 |
+assert(-1 <= diff and diff <= 0) |
7174 |
+ |
7175 |
+local t1 = os.time{year=2000, month=10, day=1, hour=23, min=12} |
7176 |
+local t2 = os.time{year=2000, month=10, day=1, hour=23, min=10, sec=19} |
7177 |
+assert(os.difftime(t1,t2) == 60*2-19) |
7178 |
+ |
7179 |
+-- since 5.3.3, 'os.time' normalizes table fields |
7180 |
+t1 = {year = 2005, month = 1, day = 1, hour = 1, min = 0, sec = -3602} |
7181 |
+os.time(t1) |
7182 |
+assert(t1.day == 31 and t1.month == 12 and t1.year == 2004 and |
7183 |
+ t1.hour == 23 and t1.min == 59 and t1.sec == 58 and |
7184 |
+ t1.yday == 366) |
7185 |
+ |
7186 |
+io.output(io.stdout) |
7187 |
+local t = os.date('%d %m %Y %H %M %S') |
7188 |
+local d, m, a, h, min, s = string.match(t, |
7189 |
+ "(%d+) (%d+) (%d+) (%d+) (%d+) (%d+)") |
7190 |
+d = tonumber(d) |
7191 |
+m = tonumber(m) |
7192 |
+a = tonumber(a) |
7193 |
+h = tonumber(h) |
7194 |
+min = tonumber(min) |
7195 |
+s = tonumber(s) |
7196 |
+io.write(string.format('test done on %2.2d/%2.2d/%d', d, m, a)) |
7197 |
+io.write(string.format(', at %2.2d:%2.2d:%2.2d\n', h, min, s)) |
7198 |
+io.write(string.format('%s\n', _VERSION)) |
7199 |
+ |
7200 |
+ |
7201 |
|
7202 |
diff --git a/tests/gc.lua b/tests/gc.lua |
7203 |
new file mode 100644 |
7204 |
index 0000000..93fd6d6 |
7205 |
--- /dev/null |
7206 |
+++ b/tests/gc.lua |
7207 |
@@ -0,0 +1,624 @@ |
7208 |
+-- $Id: gc.lua,v 1.72 2016/11/07 13:11:28 roberto Exp $ |
7209 |
+-- See Copyright Notice in file all.lua |
7210 |
+ |
7211 |
+print('testing garbage collection') |
7212 |
+ |
7213 |
+local debug = require"debug" |
7214 |
+ |
7215 |
+collectgarbage() |
7216 |
+ |
7217 |
+assert(collectgarbage("isrunning")) |
7218 |
+ |
7219 |
+local function gcinfo () return collectgarbage"count" * 1024 end |
7220 |
+ |
7221 |
+ |
7222 |
+-- test weird parameters |
7223 |
+do |
7224 |
+ -- save original parameters |
7225 |
+ local a = collectgarbage("setpause", 200) |
7226 |
+ local b = collectgarbage("setstepmul", 200) |
7227 |
+ local t = {0, 2, 10, 90, 500, 5000, 30000, 0x7ffffffe} |
7228 |
+ for i = 1, #t do |
7229 |
+ local p = t[i] |
7230 |
+ for j = 1, #t do |
7231 |
+ local m = t[j] |
7232 |
+ collectgarbage("setpause", p) |
7233 |
+ collectgarbage("setstepmul", m) |
7234 |
+ collectgarbage("step", 0) |
7235 |
+ collectgarbage("step", 10000) |
7236 |
+ end |
7237 |
+ end |
7238 |
+ -- restore original parameters |
7239 |
+ collectgarbage("setpause", a) |
7240 |
+ collectgarbage("setstepmul", b) |
7241 |
+ collectgarbage() |
7242 |
+end |
7243 |
+ |
7244 |
+ |
7245 |
+_G["while"] = 234 |
7246 |
+ |
7247 |
+limit = 5000 |
7248 |
+ |
7249 |
+ |
7250 |
+local function GC1 () |
7251 |
+ local u |
7252 |
+ local b -- must be declared after 'u' (to be above it in the stack) |
7253 |
+ local finish = false |
7254 |
+ u = setmetatable({}, {__gc = function () finish = true end}) |
7255 |
+ b = {34} |
7256 |
+ repeat u = {} until finish |
7257 |
+ assert(b[1] == 34) -- 'u' was collected, but 'b' was not |
7258 |
+ |
7259 |
+ finish = false; local i = 1 |
7260 |
+ u = setmetatable({}, {__gc = function () finish = true end}) |
7261 |
+ repeat i = i + 1; u = tostring(i) .. tostring(i) until finish |
7262 |
+ assert(b[1] == 34) -- 'u' was collected, but 'b' was not |
7263 |
+ |
7264 |
+ finish = false |
7265 |
+ u = setmetatable({}, {__gc = function () finish = true end}) |
7266 |
+ repeat local i; u = function () return i end until finish |
7267 |
+ assert(b[1] == 34) -- 'u' was collected, but 'b' was not |
7268 |
+end |
7269 |
+ |
7270 |
+local function GC2 () |
7271 |
+ local u |
7272 |
+ local finish = false |
7273 |
+ u = {setmetatable({}, {__gc = function () finish = true end})} |
7274 |
+ b = {34} |
7275 |
+ repeat u = {{}} until finish |
7276 |
+ assert(b[1] == 34) -- 'u' was collected, but 'b' was not |
7277 |
+ |
7278 |
+ finish = false; local i = 1 |
7279 |
+ u = {setmetatable({}, {__gc = function () finish = true end})} |
7280 |
+ repeat i = i + 1; u = {tostring(i) .. tostring(i)} until finish |
7281 |
+ assert(b[1] == 34) -- 'u' was collected, but 'b' was not |
7282 |
+ |
7283 |
+ finish = false |
7284 |
+ u = {setmetatable({}, {__gc = function () finish = true end})} |
7285 |
+ repeat local i; u = {function () return i end} until finish |
7286 |
+ assert(b[1] == 34) -- 'u' was collected, but 'b' was not |
7287 |
+end |
7288 |
+ |
7289 |
+local function GC() GC1(); GC2() end |
7290 |
+ |
7291 |
+ |
7292 |
+contCreate = 0 |
7293 |
+ |
7294 |
+print('tables') |
7295 |
+while contCreate <= limit do |
7296 |
+ local a = {}; a = nil |
7297 |
+ contCreate = contCreate+1 |
7298 |
+end |
7299 |
+ |
7300 |
+a = "a" |
7301 |
+ |
7302 |
+contCreate = 0 |
7303 |
+print('strings') |
7304 |
+while contCreate <= limit do |
7305 |
+ a = contCreate .. "b"; |
7306 |
+ a = string.gsub(a, '(%d%d*)', string.upper) |
7307 |
+ a = "a" |
7308 |
+ contCreate = contCreate+1 |
7309 |
+end |
7310 |
+ |
7311 |
+ |
7312 |
+contCreate = 0 |
7313 |
+ |
7314 |
+a = {} |
7315 |
+ |
7316 |
+print('functions') |
7317 |
+function a:test () |
7318 |
+ while contCreate <= limit do |
7319 |
+ load(string.format("function temp(a) return 'a%d' end", contCreate), "")() |
7320 |
+ assert(temp() == string.format('a%d', contCreate)) |
7321 |
+ contCreate = contCreate+1 |
7322 |
+ end |
7323 |
+end |
7324 |
+ |
7325 |
+a:test() |
7326 |
+ |
7327 |
+-- collection of functions without locals, globals, etc. |
7328 |
+do local f = function () end end |
7329 |
+ |
7330 |
+ |
7331 |
+print("functions with errors") |
7332 |
+prog = [[ |
7333 |
+do |
7334 |
+ a = 10; |
7335 |
+ function foo(x,y) |
7336 |
+ a = sin(a+0.456-0.23e-12); |
7337 |
+ return function (z) return sin(%x+z) end |
7338 |
+ end |
7339 |
+ local x = function (w) a=a+w; end |
7340 |
+end |
7341 |
+]] |
7342 |
+do |
7343 |
+ local step = 1 |
7344 |
+ if _soft then step = 13 end |
7345 |
+ for i=1, string.len(prog), step do |
7346 |
+ for j=i, string.len(prog), step do |
7347 |
+ pcall(load(string.sub(prog, i, j), "")) |
7348 |
+ end |
7349 |
+ end |
7350 |
+end |
7351 |
+ |
7352 |
+foo = nil |
7353 |
+print('long strings') |
7354 |
+x = "01234567890123456789012345678901234567890123456789012345678901234567890123456789" |
7355 |
+assert(string.len(x)==80) |
7356 |
+s = '' |
7357 |
+n = 0 |
7358 |
+k = math.min(300, (math.maxinteger // 80) // 2) |
7359 |
+while n < k do s = s..x; n=n+1; j=tostring(n) end |
7360 |
+assert(string.len(s) == k*80) |
7361 |
+s = string.sub(s, 1, 10000) |
7362 |
+s, i = string.gsub(s, '(%d%d%d%d)', '') |
7363 |
+assert(i==10000 // 4) |
7364 |
+s = nil |
7365 |
+x = nil |
7366 |
+ |
7367 |
+assert(_G["while"] == 234) |
7368 |
+ |
7369 |
+ |
7370 |
+print("steps") |
7371 |
+ |
7372 |
+print("steps (2)") |
7373 |
+ |
7374 |
+local function dosteps (siz) |
7375 |
+ assert(not collectgarbage("isrunning")) |
7376 |
+ collectgarbage() |
7377 |
+ assert(not collectgarbage("isrunning")) |
7378 |
+ local a = {} |
7379 |
+ for i=1,100 do a[i] = {{}}; local b = {} end |
7380 |
+ local x = gcinfo() |
7381 |
+ local i = 0 |
7382 |
+ repeat -- do steps until it completes a collection cycle |
7383 |
+ i = i+1 |
7384 |
+ until collectgarbage("step", siz) |
7385 |
+ assert(gcinfo() < x) |
7386 |
+ return i |
7387 |
+end |
7388 |
+ |
7389 |
+collectgarbage"stop" |
7390 |
+ |
7391 |
+if not _port then |
7392 |
+ -- test the "size" of basic GC steps (whatever they mean...) |
7393 |
+ assert(dosteps(0) > 10) |
7394 |
+ assert(dosteps(10) < dosteps(2)) |
7395 |
+end |
7396 |
+ |
7397 |
+-- collector should do a full collection with so many steps |
7398 |
+assert(dosteps(20000) == 1) |
7399 |
+assert(collectgarbage("step", 20000) == true) |
7400 |
+assert(collectgarbage("step", 20000) == true) |
7401 |
+ |
7402 |
+assert(not collectgarbage("isrunning")) |
7403 |
+collectgarbage"restart" |
7404 |
+assert(collectgarbage("isrunning")) |
7405 |
+ |
7406 |
+ |
7407 |
+if not _port then |
7408 |
+ -- test the pace of the collector |
7409 |
+ collectgarbage(); collectgarbage() |
7410 |
+ local x = gcinfo() |
7411 |
+ collectgarbage"stop" |
7412 |
+ assert(not collectgarbage("isrunning")) |
7413 |
+ repeat |
7414 |
+ local a = {} |
7415 |
+ until gcinfo() > 3 * x |
7416 |
+ collectgarbage"restart" |
7417 |
+ assert(collectgarbage("isrunning")) |
7418 |
+ repeat |
7419 |
+ local a = {} |
7420 |
+ until gcinfo() <= x * 2 |
7421 |
+end |
7422 |
+ |
7423 |
+ |
7424 |
+print("clearing tables") |
7425 |
+lim = 15 |
7426 |
+a = {} |
7427 |
+-- fill a with `collectable' indices |
7428 |
+for i=1,lim do a[{}] = i end |
7429 |
+b = {} |
7430 |
+for k,v in pairs(a) do b[k]=v end |
7431 |
+-- remove all indices and collect them |
7432 |
+for n in pairs(b) do |
7433 |
+ a[n] = nil |
7434 |
+ assert(type(n) == 'table' and next(n) == nil) |
7435 |
+ collectgarbage() |
7436 |
+end |
7437 |
+b = nil |
7438 |
+collectgarbage() |
7439 |
+for n in pairs(a) do error'cannot be here' end |
7440 |
+for i=1,lim do a[i] = i end |
7441 |
+for i=1,lim do assert(a[i] == i) end |
7442 |
+ |
7443 |
+ |
7444 |
+print('weak tables') |
7445 |
+a = {}; setmetatable(a, {__mode = 'k'}); |
7446 |
+-- fill a with some `collectable' indices |
7447 |
+for i=1,lim do a[{}] = i end |
7448 |
+-- and some non-collectable ones |
7449 |
+for i=1,lim do a[i] = i end |
7450 |
+for i=1,lim do local s=string.rep('@', i); a[s] = s..'#' end |
7451 |
+collectgarbage() |
7452 |
+local i = 0 |
7453 |
+for k,v in pairs(a) do assert(k==v or k..'#'==v); i=i+1 end |
7454 |
+assert(i == 2*lim) |
7455 |
+ |
7456 |
+a = {}; setmetatable(a, {__mode = 'v'}); |
7457 |
+a[1] = string.rep('b', 21) |
7458 |
+collectgarbage() |
7459 |
+assert(a[1]) -- strings are *values* |
7460 |
+a[1] = nil |
7461 |
+-- fill a with some `collectable' values (in both parts of the table) |
7462 |
+for i=1,lim do a[i] = {} end |
7463 |
+for i=1,lim do a[i..'x'] = {} end |
7464 |
+-- and some non-collectable ones |
7465 |
+for i=1,lim do local t={}; a[t]=t end |
7466 |
+for i=1,lim do a[i+lim]=i..'x' end |
7467 |
+collectgarbage() |
7468 |
+local i = 0 |
7469 |
+for k,v in pairs(a) do assert(k==v or k-lim..'x' == v); i=i+1 end |
7470 |
+assert(i == 2*lim) |
7471 |
+ |
7472 |
+a = {}; setmetatable(a, {__mode = 'vk'}); |
7473 |
+local x, y, z = {}, {}, {} |
7474 |
+-- keep only some items |
7475 |
+a[1], a[2], a[3] = x, y, z |
7476 |
+a[string.rep('$', 11)] = string.rep('$', 11) |
7477 |
+-- fill a with some `collectable' values |
7478 |
+for i=4,lim do a[i] = {} end |
7479 |
+for i=1,lim do a[{}] = i end |
7480 |
+for i=1,lim do local t={}; a[t]=t end |
7481 |
+collectgarbage() |
7482 |
+assert(next(a) ~= nil) |
7483 |
+local i = 0 |
7484 |
+for k,v in pairs(a) do |
7485 |
+ assert((k == 1 and v == x) or |
7486 |
+ (k == 2 and v == y) or |
7487 |
+ (k == 3 and v == z) or k==v); |
7488 |
+ i = i+1 |
7489 |
+end |
7490 |
+assert(i == 4) |
7491 |
+x,y,z=nil |
7492 |
+collectgarbage() |
7493 |
+assert(next(a) == string.rep('$', 11)) |
7494 |
+ |
7495 |
+ |
7496 |
+-- 'bug' in 5.1 |
7497 |
+a = {} |
7498 |
+local t = {x = 10} |
7499 |
+local C = setmetatable({key = t}, {__mode = 'v'}) |
7500 |
+local C1 = setmetatable({[t] = 1}, {__mode = 'k'}) |
7501 |
+a.x = t -- this should not prevent 't' from being removed from |
7502 |
+ -- weak table 'C' by the time 'a' is finalized |
7503 |
+ |
7504 |
+setmetatable(a, {__gc = function (u) |
7505 |
+ assert(C.key == nil) |
7506 |
+ assert(type(next(C1)) == 'table') |
7507 |
+ end}) |
7508 |
+ |
7509 |
+a, t = nil |
7510 |
+collectgarbage() |
7511 |
+collectgarbage() |
7512 |
+assert(next(C) == nil and next(C1) == nil) |
7513 |
+C, C1 = nil |
7514 |
+ |
7515 |
+ |
7516 |
+-- ephemerons |
7517 |
+local mt = {__mode = 'k'} |
7518 |
+a = {{10},{20},{30},{40}}; setmetatable(a, mt) |
7519 |
+x = nil |
7520 |
+for i = 1, 100 do local n = {}; a[n] = {k = {x}}; x = n end |
7521 |
+GC() |
7522 |
+local n = x |
7523 |
+local i = 0 |
7524 |
+while n do n = a[n].k[1]; i = i + 1 end |
7525 |
+assert(i == 100) |
7526 |
+x = nil |
7527 |
+GC() |
7528 |
+for i = 1, 4 do assert(a[i][1] == i * 10); a[i] = nil end |
7529 |
+assert(next(a) == nil) |
7530 |
+ |
7531 |
+local K = {} |
7532 |
+a[K] = {} |
7533 |
+for i=1,10 do a[K][i] = {}; a[a[K][i]] = setmetatable({}, mt) end |
7534 |
+x = nil |
7535 |
+local k = 1 |
7536 |
+for j = 1,100 do |
7537 |
+ local n = {}; local nk = k%10 + 1 |
7538 |
+ a[a[K][nk]][n] = {x, k = k}; x = n; k = nk |
7539 |
+end |
7540 |
+GC() |
7541 |
+local n = x |
7542 |
+local i = 0 |
7543 |
+while n do local t = a[a[K][k]][n]; n = t[1]; k = t.k; i = i + 1 end |
7544 |
+assert(i == 100) |
7545 |
+K = nil |
7546 |
+GC() |
7547 |
+-- assert(next(a) == nil) |
7548 |
+ |
7549 |
+ |
7550 |
+-- testing errors during GC |
7551 |
+do |
7552 |
+collectgarbage("stop") -- stop collection |
7553 |
+local u = {} |
7554 |
+local s = {}; setmetatable(s, {__mode = 'k'}) |
7555 |
+setmetatable(u, {__gc = function (o) |
7556 |
+ local i = s[o] |
7557 |
+ s[i] = true |
7558 |
+ assert(not s[i - 1]) -- check proper finalization order |
7559 |
+ if i == 8 then error("here") end -- error during GC |
7560 |
+end}) |
7561 |
+ |
7562 |
+for i = 6, 10 do |
7563 |
+ local n = setmetatable({}, getmetatable(u)) |
7564 |
+ s[n] = i |
7565 |
+end |
7566 |
+ |
7567 |
+assert(not pcall(collectgarbage)) |
7568 |
+for i = 8, 10 do assert(s[i]) end |
7569 |
+ |
7570 |
+for i = 1, 5 do |
7571 |
+ local n = setmetatable({}, getmetatable(u)) |
7572 |
+ s[n] = i |
7573 |
+end |
7574 |
+ |
7575 |
+collectgarbage() |
7576 |
+for i = 1, 10 do assert(s[i]) end |
7577 |
+ |
7578 |
+getmetatable(u).__gc = false |
7579 |
+ |
7580 |
+ |
7581 |
+-- __gc errors with non-string messages |
7582 |
+setmetatable({}, {__gc = function () error{} end}) |
7583 |
+local a, b = pcall(collectgarbage) |
7584 |
+assert(not a and type(b) == "string" and string.find(b, "error in __gc")) |
7585 |
+ |
7586 |
+end |
7587 |
+print '+' |
7588 |
+ |
7589 |
+ |
7590 |
+-- testing userdata |
7591 |
+if T==nil then |
7592 |
+ (Message or print)('\n >>> testC not active: skipping userdata GC tests <<<\n') |
7593 |
+ |
7594 |
+else |
7595 |
+ |
7596 |
+ local function newproxy(u) |
7597 |
+ return debug.setmetatable(T.newuserdata(0), debug.getmetatable(u)) |
7598 |
+ end |
7599 |
+ |
7600 |
+ collectgarbage("stop") -- stop collection |
7601 |
+ local u = newproxy(nil) |
7602 |
+ debug.setmetatable(u, {__gc = true}) |
7603 |
+ local s = 0 |
7604 |
+ local a = {[u] = 0}; setmetatable(a, {__mode = 'vk'}) |
7605 |
+ for i=1,10 do a[newproxy(u)] = i end |
7606 |
+ for k in pairs(a) do assert(getmetatable(k) == getmetatable(u)) end |
7607 |
+ local a1 = {}; for k,v in pairs(a) do a1[k] = v end |
7608 |
+ for k,v in pairs(a1) do a[v] = k end |
7609 |
+ for i =1,10 do assert(a[i]) end |
7610 |
+ getmetatable(u).a = a1 |
7611 |
+ getmetatable(u).u = u |
7612 |
+ do |
7613 |
+ local u = u |
7614 |
+ getmetatable(u).__gc = function (o) |
7615 |
+ assert(a[o] == 10-s) |
7616 |
+ assert(a[10-s] == nil) -- udata already removed from weak table |
7617 |
+ assert(getmetatable(o) == getmetatable(u)) |
7618 |
+ assert(getmetatable(o).a[o] == 10-s) |
7619 |
+ s=s+1 |
7620 |
+ end |
7621 |
+ end |
7622 |
+ a1, u = nil |
7623 |
+ assert(next(a) ~= nil) |
7624 |
+ collectgarbage() |
7625 |
+ assert(s==11) |
7626 |
+ collectgarbage() |
7627 |
+ assert(next(a) == nil) -- finalized keys are removed in two cycles |
7628 |
+end |
7629 |
+ |
7630 |
+ |
7631 |
+-- __gc x weak tables |
7632 |
+local u = setmetatable({}, {__gc = true}) |
7633 |
+-- __gc metamethod should be collected before running |
7634 |
+setmetatable(getmetatable(u), {__mode = "v"}) |
7635 |
+getmetatable(u).__gc = function (o) os.exit(1) end -- cannot happen |
7636 |
+u = nil |
7637 |
+collectgarbage() |
7638 |
+ |
7639 |
+local u = setmetatable({}, {__gc = true}) |
7640 |
+local m = getmetatable(u) |
7641 |
+m.x = {[{0}] = 1; [0] = {1}}; setmetatable(m.x, {__mode = "kv"}); |
7642 |
+m.__gc = function (o) |
7643 |
+ assert(next(getmetatable(o).x) == nil) |
7644 |
+ m = 10 |
7645 |
+end |
7646 |
+u, m = nil |
7647 |
+collectgarbage() |
7648 |
+assert(m==10) |
7649 |
+ |
7650 |
+ |
7651 |
+-- errors during collection |
7652 |
+u = setmetatable({}, {__gc = function () error "!!!" end}) |
7653 |
+u = nil |
7654 |
+assert(not pcall(collectgarbage)) |
7655 |
+ |
7656 |
+ |
7657 |
+if not _soft then |
7658 |
+ print("deep structures") |
7659 |
+ local a = {} |
7660 |
+ for i = 1,200000 do |
7661 |
+ a = {next = a} |
7662 |
+ end |
7663 |
+ collectgarbage() |
7664 |
+end |
7665 |
+ |
7666 |
+-- create many threads with self-references and open upvalues |
7667 |
+print("self-referenced threads") |
7668 |
+local thread_id = 0 |
7669 |
+local threads = {} |
7670 |
+ |
7671 |
+local function fn (thread) |
7672 |
+ local x = {} |
7673 |
+ threads[thread_id] = function() |
7674 |
+ thread = x |
7675 |
+ end |
7676 |
+ coroutine.yield() |
7677 |
+end |
7678 |
+ |
7679 |
+while thread_id < 1000 do |
7680 |
+ local thread = coroutine.create(fn) |
7681 |
+ coroutine.resume(thread, thread) |
7682 |
+ thread_id = thread_id + 1 |
7683 |
+end |
7684 |
+ |
7685 |
+ |
7686 |
+-- Create a closure (function inside 'f') with an upvalue ('param') that |
7687 |
+-- points (through a table) to the closure itself and to the thread |
7688 |
+-- ('co' and the initial value of 'param') where closure is running. |
7689 |
+-- Then, assert that table (and therefore everything else) will be |
7690 |
+-- collected. |
7691 |
+do |
7692 |
+ local collected = false -- to detect collection |
7693 |
+ collectgarbage(); collectgarbage("stop") |
7694 |
+ do |
7695 |
+ local function f (param) |
7696 |
+ ;(function () |
7697 |
+ assert(type(f) == 'function' and type(param) == 'thread') |
7698 |
+ param = {param, f} |
7699 |
+ setmetatable(param, {__gc = function () collected = true end}) |
7700 |
+ coroutine.yield(100) |
7701 |
+ end)() |
7702 |
+ end |
7703 |
+ local co = coroutine.create(f) |
7704 |
+ assert(coroutine.resume(co, co)) |
7705 |
+ end |
7706 |
+ -- Now, thread and closure are not reacheable any more; |
7707 |
+ -- two collections are needed to break cycle |
7708 |
+ collectgarbage() |
7709 |
+ assert(not collected) |
7710 |
+ collectgarbage() |
7711 |
+ assert(collected) |
7712 |
+ collectgarbage("restart") |
7713 |
+end |
7714 |
+ |
7715 |
+ |
7716 |
+do |
7717 |
+ collectgarbage() |
7718 |
+ collectgarbage"stop" |
7719 |
+ local x = gcinfo() |
7720 |
+ repeat |
7721 |
+ for i=1,1000 do _ENV.a = {} end |
7722 |
+ collectgarbage("step", 0) -- steps should not unblock the collector |
7723 |
+ until gcinfo() > 2 * x |
7724 |
+ collectgarbage"restart" |
7725 |
+end |
7726 |
+ |
7727 |
+ |
7728 |
+if T then -- tests for weird cases collecting upvalues |
7729 |
+ |
7730 |
+ local function foo () |
7731 |
+ local a = {x = 20} |
7732 |
+ coroutine.yield(function () return a.x end) -- will run collector |
7733 |
+ assert(a.x == 20) -- 'a' is 'ok' |
7734 |
+ a = {x = 30} -- create a new object |
7735 |
+ assert(T.gccolor(a) == "white") -- of course it is new... |
7736 |
+ coroutine.yield(100) -- 'a' is still local to this thread |
7737 |
+ end |
7738 |
+ |
7739 |
+ local t = setmetatable({}, {__mode = "kv"}) |
7740 |
+ collectgarbage(); collectgarbage('stop') |
7741 |
+ -- create coroutine in a weak table, so it will never be marked |
7742 |
+ t.co = coroutine.wrap(foo) |
7743 |
+ local f = t.co() -- create function to access local 'a' |
7744 |
+ T.gcstate("atomic") -- ensure all objects are traversed |
7745 |
+ assert(T.gcstate() == "atomic") |
7746 |
+ assert(t.co() == 100) -- resume coroutine, creating new table for 'a' |
7747 |
+ assert(T.gccolor(t.co) == "white") -- thread was not traversed |
7748 |
+ T.gcstate("pause") -- collect thread, but should mark 'a' before that |
7749 |
+ assert(t.co == nil and f() == 30) -- ensure correct access to 'a' |
7750 |
+ |
7751 |
+ collectgarbage("restart") |
7752 |
+ |
7753 |
+ -- test barrier in sweep phase (advance cleaning of upvalue to white) |
7754 |
+ local u = T.newuserdata(0) -- create a userdata |
7755 |
+ collectgarbage() |
7756 |
+ collectgarbage"stop" |
7757 |
+ T.gcstate"atomic" |
7758 |
+ T.gcstate"sweepallgc" |
7759 |
+ local x = {} |
7760 |
+ assert(T.gccolor(u) == "black") -- upvalue is "old" (black) |
7761 |
+ assert(T.gccolor(x) == "white") -- table is "new" (white) |
7762 |
+ debug.setuservalue(u, x) -- trigger barrier |
7763 |
+ assert(T.gccolor(u) == "white") -- upvalue changed to white |
7764 |
+ collectgarbage"restart" |
7765 |
+ |
7766 |
+ print"+" |
7767 |
+end |
7768 |
+ |
7769 |
+ |
7770 |
+if T then |
7771 |
+ local debug = require "debug" |
7772 |
+ collectgarbage("stop") |
7773 |
+ local x = T.newuserdata(0) |
7774 |
+ local y = T.newuserdata(0) |
7775 |
+ debug.setmetatable(y, {__gc = true}) -- bless the new udata before... |
7776 |
+ debug.setmetatable(x, {__gc = true}) -- ...the old one |
7777 |
+ assert(T.gccolor(y) == "white") |
7778 |
+ T.checkmemory() |
7779 |
+ collectgarbage("restart") |
7780 |
+end |
7781 |
+ |
7782 |
+ |
7783 |
+if T then |
7784 |
+ print("emergency collections") |
7785 |
+ collectgarbage() |
7786 |
+ collectgarbage() |
7787 |
+ T.totalmem(T.totalmem() + 200) |
7788 |
+ for i=1,200 do local a = {} end |
7789 |
+ T.totalmem(0) |
7790 |
+ collectgarbage() |
7791 |
+ local t = T.totalmem("table") |
7792 |
+ local a = {{}, {}, {}} -- create 4 new tables |
7793 |
+ assert(T.totalmem("table") == t + 4) |
7794 |
+ t = T.totalmem("function") |
7795 |
+ a = function () end -- create 1 new closure |
7796 |
+ assert(T.totalmem("function") == t + 1) |
7797 |
+ t = T.totalmem("thread") |
7798 |
+ a = coroutine.create(function () end) -- create 1 new coroutine |
7799 |
+ assert(T.totalmem("thread") == t + 1) |
7800 |
+end |
7801 |
+ |
7802 |
+-- create an object to be collected when state is closed |
7803 |
+do |
7804 |
+ local setmetatable,assert,type,print,getmetatable = |
7805 |
+ setmetatable,assert,type,print,getmetatable |
7806 |
+ local tt = {} |
7807 |
+ tt.__gc = function (o) |
7808 |
+ assert(getmetatable(o) == tt) |
7809 |
+ -- create new objects during GC |
7810 |
+ local a = 'xuxu'..(10+3)..'joao', {} |
7811 |
+ ___Glob = o -- ressurect object! |
7812 |
+ setmetatable({}, tt) -- creates a new one with same metatable |
7813 |
+ print(">>> closing state " .. "<<<\n") |
7814 |
+ end |
7815 |
+ local u = setmetatable({}, tt) |
7816 |
+ ___Glob = {u} -- avoid object being collected before program end |
7817 |
+end |
7818 |
+ |
7819 |
+-- create several objects to raise errors when collected while closing state |
7820 |
+do |
7821 |
+ local mt = {__gc = function (o) return o + 1 end} |
7822 |
+ for i = 1,10 do |
7823 |
+ -- create object and preserve it until the end |
7824 |
+ table.insert(___Glob, setmetatable({}, mt)) |
7825 |
+ end |
7826 |
+end |
7827 |
+ |
7828 |
+-- just to make sure |
7829 |
+assert(collectgarbage'isrunning') |
7830 |
+ |
7831 |
+print('OK') |
7832 |
|
7833 |
diff --git a/tests/goto.lua b/tests/goto.lua |
7834 |
new file mode 100644 |
7835 |
index 0000000..0372aa9 |
7836 |
--- /dev/null |
7837 |
+++ b/tests/goto.lua |
7838 |
@@ -0,0 +1,232 @@ |
7839 |
+-- $Id: goto.lua,v 1.13 2016/11/07 13:11:28 roberto Exp $ |
7840 |
+-- See Copyright Notice in file all.lua |
7841 |
+ |
7842 |
+collectgarbage() |
7843 |
+ |
7844 |
+local function errmsg (code, m) |
7845 |
+ local st, msg = load(code) |
7846 |
+ assert(not st and string.find(msg, m)) |
7847 |
+end |
7848 |
+ |
7849 |
+-- cannot see label inside block |
7850 |
+errmsg([[ goto l1; do ::l1:: end ]], "label 'l1'") |
7851 |
+errmsg([[ do ::l1:: end goto l1; ]], "label 'l1'") |
7852 |
+ |
7853 |
+-- repeated label |
7854 |
+errmsg([[ ::l1:: ::l1:: ]], "label 'l1'") |
7855 |
+ |
7856 |
+ |
7857 |
+-- undefined label |
7858 |
+errmsg([[ goto l1; local aa ::l1:: ::l2:: print(3) ]], "local 'aa'") |
7859 |
+ |
7860 |
+-- jumping over variable definition |
7861 |
+errmsg([[ |
7862 |
+do local bb, cc; goto l1; end |
7863 |
+local aa |
7864 |
+::l1:: print(3) |
7865 |
+]], "local 'aa'") |
7866 |
+ |
7867 |
+-- jumping into a block |
7868 |
+errmsg([[ do ::l1:: end goto l1 ]], "label 'l1'") |
7869 |
+errmsg([[ goto l1 do ::l1:: end ]], "label 'l1'") |
7870 |
+ |
7871 |
+-- cannot continue a repeat-until with variables |
7872 |
+errmsg([[ |
7873 |
+ repeat |
7874 |
+ if x then goto cont end |
7875 |
+ local xuxu = 10 |
7876 |
+ ::cont:: |
7877 |
+ until xuxu < x |
7878 |
+]], "local 'xuxu'") |
7879 |
+ |
7880 |
+-- simple gotos |
7881 |
+local x |
7882 |
+do |
7883 |
+ local y = 12 |
7884 |
+ goto l1 |
7885 |
+ ::l2:: x = x + 1; goto l3 |
7886 |
+ ::l1:: x = y; goto l2 |
7887 |
+end |
7888 |
+::l3:: ::l3_1:: assert(x == 13) |
7889 |
+ |
7890 |
+ |
7891 |
+-- long labels |
7892 |
+do |
7893 |
+ local prog = [[ |
7894 |
+ do |
7895 |
+ local a = 1 |
7896 |
+ goto l%sa; a = a + 1 |
7897 |
+ ::l%sa:: a = a + 10 |
7898 |
+ goto l%sb; a = a + 2 |
7899 |
+ ::l%sb:: a = a + 20 |
7900 |
+ return a |
7901 |
+ end |
7902 |
+ ]] |
7903 |
+ local label = string.rep("0123456789", 40) |
7904 |
+ prog = string.format(prog, label, label, label, label) |
7905 |
+ assert(assert(load(prog))() == 31) |
7906 |
+end |
7907 |
+ |
7908 |
+-- goto to correct label when nested |
7909 |
+do goto l3; ::l3:: end -- does not loop jumping to previous label 'l3' |
7910 |
+ |
7911 |
+-- ok to jump over local dec. to end of block |
7912 |
+do |
7913 |
+ goto l1 |
7914 |
+ local a = 23 |
7915 |
+ x = a |
7916 |
+ ::l1::; |
7917 |
+end |
7918 |
+ |
7919 |
+while true do |
7920 |
+ goto l4 |
7921 |
+ goto l1 -- ok to jump over local dec. to end of block |
7922 |
+ goto l1 -- multiple uses of same label |
7923 |
+ local x = 45 |
7924 |
+ ::l1:: ;;; |
7925 |
+end |
7926 |
+::l4:: assert(x == 13) |
7927 |
+ |
7928 |
+if print then |
7929 |
+ goto l1 -- ok to jump over local dec. to end of block |
7930 |
+ error("should not be here") |
7931 |
+ goto l2 -- ok to jump over local dec. to end of block |
7932 |
+ local x |
7933 |
+ ::l1:: ; ::l2:: ;; |
7934 |
+else end |
7935 |
+ |
7936 |
+-- to repeat a label in a different function is OK |
7937 |
+local function foo () |
7938 |
+ local a = {} |
7939 |
+ goto l3 |
7940 |
+ ::l1:: a[#a + 1] = 1; goto l2; |
7941 |
+ ::l2:: a[#a + 1] = 2; goto l5; |
7942 |
+ ::l3:: |
7943 |
+ ::l3a:: a[#a + 1] = 3; goto l1; |
7944 |
+ ::l4:: a[#a + 1] = 4; goto l6; |
7945 |
+ ::l5:: a[#a + 1] = 5; goto l4; |
7946 |
+ ::l6:: assert(a[1] == 3 and a[2] == 1 and a[3] == 2 and |
7947 |
+ a[4] == 5 and a[5] == 4) |
7948 |
+ if not a[6] then a[6] = true; goto l3a end -- do it twice |
7949 |
+end |
7950 |
+ |
7951 |
+::l6:: foo() |
7952 |
+ |
7953 |
+ |
7954 |
+do -- bug in 5.2 -> 5.3.2 |
7955 |
+ local x |
7956 |
+ ::L1:: |
7957 |
+ local y -- cannot join this SETNIL with previous one |
7958 |
+ assert(y == nil) |
7959 |
+ y = true |
7960 |
+ if x == nil then |
7961 |
+ x = 1 |
7962 |
+ goto L1 |
7963 |
+ else |
7964 |
+ x = x + 1 |
7965 |
+ end |
7966 |
+ assert(x == 2 and y == true) |
7967 |
+end |
7968 |
+ |
7969 |
+-------------------------------------------------------------------------------- |
7970 |
+-- testing closing of upvalues |
7971 |
+ |
7972 |
+local debug = require 'debug' |
7973 |
+ |
7974 |
+local function foo () |
7975 |
+ local t = {} |
7976 |
+ do |
7977 |
+ local i = 1 |
7978 |
+ local a, b, c, d |
7979 |
+ t[1] = function () return a, b, c, d end |
7980 |
+ ::l1:: |
7981 |
+ local b |
7982 |
+ do |
7983 |
+ local c |
7984 |
+ t[#t + 1] = function () return a, b, c, d end -- t[2], t[4], t[6] |
7985 |
+ if i > 2 then goto l2 end |
7986 |
+ do |
7987 |
+ local d |
7988 |
+ t[#t + 1] = function () return a, b, c, d end -- t[3], t[5] |
7989 |
+ i = i + 1 |
7990 |
+ local a |
7991 |
+ goto l1 |
7992 |
+ end |
7993 |
+ end |
7994 |
+ end |
7995 |
+ ::l2:: return t |
7996 |
+end |
7997 |
+ |
7998 |
+local a = foo() |
7999 |
+assert(#a == 6) |
8000 |
+ |
8001 |
+-- all functions share same 'a' |
8002 |
+for i = 2, 6 do |
8003 |
+ assert(debug.upvalueid(a[1], 1) == debug.upvalueid(a[i], 1)) |
8004 |
+end |
8005 |
+ |
8006 |
+-- 'b' and 'c' are shared among some of them |
8007 |
+for i = 2, 6 do |
8008 |
+ -- only a[1] uses external 'b'/'b' |
8009 |
+ assert(debug.upvalueid(a[1], 2) ~= debug.upvalueid(a[i], 2)) |
8010 |
+ assert(debug.upvalueid(a[1], 3) ~= debug.upvalueid(a[i], 3)) |
8011 |
+end |
8012 |
+ |
8013 |
+for i = 3, 5, 2 do |
8014 |
+ -- inner functions share 'b'/'c' with previous ones |
8015 |
+ assert(debug.upvalueid(a[i], 2) == debug.upvalueid(a[i - 1], 2)) |
8016 |
+ assert(debug.upvalueid(a[i], 3) == debug.upvalueid(a[i - 1], 3)) |
8017 |
+ -- but not with next ones |
8018 |
+ assert(debug.upvalueid(a[i], 2) ~= debug.upvalueid(a[i + 1], 2)) |
8019 |
+ assert(debug.upvalueid(a[i], 3) ~= debug.upvalueid(a[i + 1], 3)) |
8020 |
+end |
8021 |
+ |
8022 |
+-- only external 'd' is shared |
8023 |
+for i = 2, 6, 2 do |
8024 |
+ assert(debug.upvalueid(a[1], 4) == debug.upvalueid(a[i], 4)) |
8025 |
+end |
8026 |
+ |
8027 |
+-- internal 'd's are all different |
8028 |
+for i = 3, 5, 2 do |
8029 |
+ for j = 1, 6 do |
8030 |
+ assert((debug.upvalueid(a[i], 4) == debug.upvalueid(a[j], 4)) |
8031 |
+ == (i == j)) |
8032 |
+ end |
8033 |
+end |
8034 |
+ |
8035 |
+-------------------------------------------------------------------------------- |
8036 |
+-- testing if x goto optimizations |
8037 |
+ |
8038 |
+local function testG (a) |
8039 |
+ if a == 1 then |
8040 |
+ goto l1 |
8041 |
+ error("should never be here!") |
8042 |
+ elseif a == 2 then goto l2 |
8043 |
+ elseif a == 3 then goto l3 |
8044 |
+ elseif a == 4 then |
8045 |
+ goto l1 -- go to inside the block |
8046 |
+ error("should never be here!") |
8047 |
+ ::l1:: a = a + 1 -- must go to 'if' end |
8048 |
+ else |
8049 |
+ goto l4 |
8050 |
+ ::l4a:: a = a * 2; goto l4b |
8051 |
+ error("should never be here!") |
8052 |
+ ::l4:: goto l4a |
8053 |
+ error("should never be here!") |
8054 |
+ ::l4b:: |
8055 |
+ end |
8056 |
+ do return a end |
8057 |
+ ::l2:: do return "2" end |
8058 |
+ ::l3:: do return "3" end |
8059 |
+ ::l1:: return "1" |
8060 |
+end |
8061 |
+ |
8062 |
+assert(testG(1) == "1") |
8063 |
+assert(testG(2) == "2") |
8064 |
+assert(testG(3) == "3") |
8065 |
+assert(testG(4) == 5) |
8066 |
+assert(testG(5) == 10) |
8067 |
+-------------------------------------------------------------------------------- |
8068 |
+ |
8069 |
+ |
8070 |
+print'OK' |
8071 |
|
8072 |
diff --git a/tests/literals.lua b/tests/literals.lua |
8073 |
new file mode 100644 |
8074 |
index 0000000..3922b3f |
8075 |
--- /dev/null |
8076 |
+++ b/tests/literals.lua |
8077 |
@@ -0,0 +1,302 @@ |
8078 |
+-- $Id: literals.lua,v 1.36 2016/11/07 13:11:28 roberto Exp $ |
8079 |
+-- See Copyright Notice in file all.lua |
8080 |
+ |
8081 |
+print('testing scanner') |
8082 |
+ |
8083 |
+local debug = require "debug" |
8084 |
+ |
8085 |
+ |
8086 |
+local function dostring (x) return assert(load(x), "")() end |
8087 |
+ |
8088 |
+dostring("x \v\f = \t\r 'a\0a' \v\f\f") |
8089 |
+assert(x == 'a\0a' and string.len(x) == 3) |
8090 |
+ |
8091 |
+-- escape sequences |
8092 |
+assert('\n\"\'\\' == [[ |
8093 |
+ |
8094 |
+"'\]]) |
8095 |
+ |
8096 |
+assert(string.find("\a\b\f\n\r\t\v", "^%c%c%c%c%c%c%c$")) |
8097 |
+ |
8098 |
+-- assume ASCII just for tests: |
8099 |
+assert("\09912" == 'c12') |
8100 |
+assert("\99ab" == 'cab') |
8101 |
+assert("\099" == '\99') |
8102 |
+assert("\099\n" == 'c\10') |
8103 |
+assert('\0\0\0alo' == '\0' .. '\0\0' .. 'alo') |
8104 |
+ |
8105 |
+assert(010 .. 020 .. -030 == "1020-30") |
8106 |
+ |
8107 |
+-- hexadecimal escapes |
8108 |
+assert("\x00\x05\x10\x1f\x3C\xfF\xe8" == "\0\5\16\31\60\255\232") |
8109 |
+ |
8110 |
+local function lexstring (x, y, n) |
8111 |
+ local f = assert(load('return ' .. x .. |
8112 |
+ ', require"debug".getinfo(1).currentline', '')) |
8113 |
+ local s, l = f() |
8114 |
+ assert(s == y and l == n) |
8115 |
+end |
8116 |
+ |
8117 |
+lexstring("'abc\\z \n efg'", "abcefg", 2) |
8118 |
+lexstring("'abc\\z \n\n\n'", "abc", 4) |
8119 |
+lexstring("'\\z \n\t\f\v\n'", "", 3) |
8120 |
+lexstring("[[\nalo\nalo\n\n]]", "alo\nalo\n\n", 5) |
8121 |
+lexstring("[[\nalo\ralo\n\n]]", "alo\nalo\n\n", 5) |
8122 |
+lexstring("[[\nalo\ralo\r\n]]", "alo\nalo\n", 4) |
8123 |
+lexstring("[[\ralo\n\ralo\r\n]]", "alo\nalo\n", 4) |
8124 |
+lexstring("[[alo]\n]alo]]", "alo]\n]alo", 2) |
8125 |
+ |
8126 |
+assert("abc\z |
8127 |
+ def\z |
8128 |
+ ghi\z |
8129 |
+ " == 'abcdefghi') |
8130 |
+ |
8131 |
+ |
8132 |
+-- UTF-8 sequences |
8133 |
+assert("\u{0}\u{00000000}\x00\0" == string.char(0, 0, 0, 0)) |
8134 |
+ |
8135 |
+-- limits for 1-byte sequences |
8136 |
+assert("\u{0}\u{7F}" == "\x00\z\x7F") |
8137 |
+ |
8138 |
+-- limits for 2-byte sequences |
8139 |
+assert("\u{80}\u{7FF}" == "\xC2\x80\z\xDF\xBF") |
8140 |
+ |
8141 |
+-- limits for 3-byte sequences |
8142 |
+assert("\u{800}\u{FFFF}" == "\xE0\xA0\x80\z\xEF\xBF\xBF") |
8143 |
+ |
8144 |
+-- limits for 4-byte sequences |
8145 |
+assert("\u{10000}\u{10FFFF}" == "\xF0\x90\x80\x80\z\xF4\x8F\xBF\xBF") |
8146 |
+ |
8147 |
+ |
8148 |
+-- Error in escape sequences |
8149 |
+local function lexerror (s, err) |
8150 |
+ local st, msg = load('return ' .. s, '') |
8151 |
+ if err ~= '<eof>' then err = err .. "'" end |
8152 |
+ assert(not st and string.find(msg, "near .-" .. err)) |
8153 |
+end |
8154 |
+ |
8155 |
+lexerror([["abc\x"]], [[\x"]]) |
8156 |
+lexerror([["abc\x]], [[\x]]) |
8157 |
+lexerror([["\x]], [[\x]]) |
8158 |
+lexerror([["\x5"]], [[\x5"]]) |
8159 |
+lexerror([["\x5]], [[\x5]]) |
8160 |
+lexerror([["\xr"]], [[\xr]]) |
8161 |
+lexerror([["\xr]], [[\xr]]) |
8162 |
+lexerror([["\x.]], [[\x.]]) |
8163 |
+lexerror([["\x8%"]], [[\x8%%]]) |
8164 |
+lexerror([["\xAG]], [[\xAG]]) |
8165 |
+lexerror([["\g"]], [[\g]]) |
8166 |
+lexerror([["\g]], [[\g]]) |
8167 |
+lexerror([["\."]], [[\%.]]) |
8168 |
+ |
8169 |
+lexerror([["\999"]], [[\999"]]) |
8170 |
+lexerror([["xyz\300"]], [[\300"]]) |
8171 |
+lexerror([[" \256"]], [[\256"]]) |
8172 |
+ |
8173 |
+-- errors in UTF-8 sequences |
8174 |
+lexerror([["abc\u{110000}"]], [[abc\u{110000]]) -- too large |
8175 |
+lexerror([["abc\u11r"]], [[abc\u1]]) -- missing '{' |
8176 |
+lexerror([["abc\u"]], [[abc\u"]]) -- missing '{' |
8177 |
+lexerror([["abc\u{11r"]], [[abc\u{11r]]) -- missing '}' |
8178 |
+lexerror([["abc\u{11"]], [[abc\u{11"]]) -- missing '}' |
8179 |
+lexerror([["abc\u{11]], [[abc\u{11]]) -- missing '}' |
8180 |
+lexerror([["abc\u{r"]], [[abc\u{r]]) -- no digits |
8181 |
+ |
8182 |
+-- unfinished strings |
8183 |
+lexerror("[=[alo]]", "<eof>") |
8184 |
+lexerror("[=[alo]=", "<eof>") |
8185 |
+lexerror("[=[alo]", "<eof>") |
8186 |
+lexerror("'alo", "<eof>") |
8187 |
+lexerror("'alo \\z \n\n", "<eof>") |
8188 |
+lexerror("'alo \\z", "<eof>") |
8189 |
+lexerror([['alo \98]], "<eof>") |
8190 |
+ |
8191 |
+-- valid characters in variable names |
8192 |
+for i = 0, 255 do |
8193 |
+ local s = string.char(i) |
8194 |
+ assert(not string.find(s, "[a-zA-Z_]") == not load(s .. "=1", "")) |
8195 |
+ assert(not string.find(s, "[a-zA-Z_0-9]") == |
8196 |
+ not load("a" .. s .. "1 = 1", "")) |
8197 |
+end |
8198 |
+ |
8199 |
+ |
8200 |
+-- long variable names |
8201 |
+ |
8202 |
+var1 = string.rep('a', 15000) .. '1' |
8203 |
+var2 = string.rep('a', 15000) .. '2' |
8204 |
+prog = string.format([[ |
8205 |
+ %s = 5 |
8206 |
+ %s = %s + 1 |
8207 |
+ return function () return %s - %s end |
8208 |
+]], var1, var2, var1, var1, var2) |
8209 |
+local f = dostring(prog) |
8210 |
+assert(_G[var1] == 5 and _G[var2] == 6 and f() == -1) |
8211 |
+var1, var2, f = nil |
8212 |
+print('+') |
8213 |
+ |
8214 |
+-- escapes -- |
8215 |
+assert("\n\t" == [[ |
8216 |
+ |
8217 |
+ ]]) |
8218 |
+assert([[ |
8219 |
+ |
8220 |
+ $debug]] == "\n $debug") |
8221 |
+assert([[ [ ]] ~= [[ ] ]]) |
8222 |
+-- long strings -- |
8223 |
+b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789" |
8224 |
+assert(string.len(b) == 960) |
8225 |
+prog = [=[ |
8226 |
+print('+') |
8227 |
+ |
8228 |
+a1 = [["this is a 'string' with several 'quotes'"]] |
8229 |
+a2 = "'quotes'" |
8230 |
+ |
8231 |
+assert(string.find(a1, a2) == 34) |
8232 |
+print('+') |
8233 |
+ |
8234 |
+a1 = [==[temp = [[an arbitrary value]]; ]==] |
8235 |
+assert(load(a1))() |
8236 |
+assert(temp == 'an arbitrary value') |
8237 |
+-- long strings -- |
8238 |
+b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789" |
8239 |
+assert(string.len(b) == 960) |
8240 |
+print('+') |
8241 |
+ |
8242 |
+a = [[00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
8243 |
+00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
8244 |
+00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
8245 |
+00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
8246 |
+00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
8247 |
+00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
8248 |
+00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
8249 |
+00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
8250 |
+00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
8251 |
+00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
8252 |
+00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
8253 |
+00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
8254 |
+00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
8255 |
+00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
8256 |
+00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
8257 |
+00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
8258 |
+00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
8259 |
+00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
8260 |
+00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
8261 |
+00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
8262 |
+00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
8263 |
+00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
8264 |
+00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
8265 |
+]] |
8266 |
+assert(string.len(a) == 1863) |
8267 |
+assert(string.sub(a, 1, 40) == string.sub(b, 1, 40)) |
8268 |
+x = 1 |
8269 |
+]=] |
8270 |
+ |
8271 |
+print('+') |
8272 |
+x = nil |
8273 |
+dostring(prog) |
8274 |
+assert(x) |
8275 |
+ |
8276 |
+prog = nil |
8277 |
+a = nil |
8278 |
+b = nil |
8279 |
+ |
8280 |
+ |
8281 |
+-- testing line ends |
8282 |
+prog = [[ |
8283 |
+a = 1 -- a comment |
8284 |
+b = 2 |
8285 |
+ |
8286 |
+ |
8287 |
+x = [=[ |
8288 |
+hi |
8289 |
+]=] |
8290 |
+y = "\ |
8291 |
+hello\r\n\ |
8292 |
+" |
8293 |
+return require"debug".getinfo(1).currentline |
8294 |
+]] |
8295 |
+ |
8296 |
+for _, n in pairs{"\n", "\r", "\n\r", "\r\n"} do |
8297 |
+ local prog, nn = string.gsub(prog, "\n", n) |
8298 |
+ assert(dostring(prog) == nn) |
8299 |
+ assert(_G.x == "hi\n" and _G.y == "\nhello\r\n\n") |
8300 |
+end |
8301 |
+ |
8302 |
+ |
8303 |
+-- testing comments and strings with long brackets |
8304 |
+a = [==[]=]==] |
8305 |
+assert(a == "]=") |
8306 |
+ |
8307 |
+a = [==[[===[[=[]]=][====[]]===]===]==] |
8308 |
+assert(a == "[===[[=[]]=][====[]]===]===") |
8309 |
+ |
8310 |
+a = [====[[===[[=[]]=][====[]]===]===]====] |
8311 |
+assert(a == "[===[[=[]]=][====[]]===]===") |
8312 |
+ |
8313 |
+a = [=[]]]]]]]]]=] |
8314 |
+assert(a == "]]]]]]]]") |
8315 |
+ |
8316 |
+ |
8317 |
+--[===[ |
8318 |
+x y z [==[ blu foo |
8319 |
+]== |
8320 |
+] |
8321 |
+]=]==] |
8322 |
+error error]=]===] |
8323 |
+ |
8324 |
+-- generate all strings of four of these chars |
8325 |
+local x = {"=", "[", "]", "\n"} |
8326 |
+local len = 4 |
8327 |
+local function gen (c, n) |
8328 |
+ if n==0 then coroutine.yield(c) |
8329 |
+ else |
8330 |
+ for _, a in pairs(x) do |
8331 |
+ gen(c..a, n-1) |
8332 |
+ end |
8333 |
+ end |
8334 |
+end |
8335 |
+ |
8336 |
+for s in coroutine.wrap(function () gen("", len) end) do |
8337 |
+ assert(s == load("return [====[\n"..s.."]====]", "")()) |
8338 |
+end |
8339 |
+ |
8340 |
+ |
8341 |
+-- testing decimal point locale |
8342 |
+if os.setlocale("pt_BR") or os.setlocale("ptb") then |
8343 |
+ assert(tonumber("3,4") == 3.4 and tonumber"3.4" == 3.4) |
8344 |
+ assert(tonumber(" -.4 ") == -0.4) |
8345 |
+ assert(tonumber(" +0x.41 ") == 0X0.41) |
8346 |
+ assert(not load("a = (3,4)")) |
8347 |
+ assert(assert(load("return 3.4"))() == 3.4) |
8348 |
+ assert(assert(load("return .4,3"))() == .4) |
8349 |
+ assert(assert(load("return 4."))() == 4.) |
8350 |
+ assert(assert(load("return 4.+.5"))() == 4.5) |
8351 |
+ |
8352 |
+ assert(" 0x.1 " + " 0x,1" + "-0X.1\t" == 0x0.1) |
8353 |
+ |
8354 |
+ assert(tonumber"inf" == nil and tonumber"NAN" == nil) |
8355 |
+ |
8356 |
+ assert(assert(load(string.format("return %q", 4.51)))() == 4.51) |
8357 |
+ |
8358 |
+ local a,b = load("return 4.5.") |
8359 |
+ assert(string.find(b, "'4%.5%.'")) |
8360 |
+ |
8361 |
+ assert(os.setlocale("C")) |
8362 |
+else |
8363 |
+ (Message or print)( |
8364 |
+ '\n >>> pt_BR locale not available: skipping decimal point tests <<<\n') |
8365 |
+end |
8366 |
+ |
8367 |
+ |
8368 |
+-- testing %q x line ends |
8369 |
+local s = "a string with \r and \n and \r\n and \n\r" |
8370 |
+local c = string.format("return %q", s) |
8371 |
+assert(assert(load(c))() == s) |
8372 |
+ |
8373 |
+-- testing errors |
8374 |
+assert(not load"a = 'non-ending string") |
8375 |
+assert(not load"a = 'non-ending string\n'") |
8376 |
+assert(not load"a = '\\345'") |
8377 |
+assert(not load"a = [=x]") |
8378 |
+ |
8379 |
+print('OK') |
8380 |
|
8381 |
diff --git a/tests/locals.lua b/tests/locals.lua |
8382 |
new file mode 100644 |
8383 |
index 0000000..f66f6f7 |
8384 |
--- /dev/null |
8385 |
+++ b/tests/locals.lua |
8386 |
@@ -0,0 +1,162 @@ |
8387 |
+-- $Id: locals.lua,v 1.37 2016/11/07 13:11:28 roberto Exp $ |
8388 |
+-- See Copyright Notice in file all.lua |
8389 |
+ |
8390 |
+print('testing local variables and environments') |
8391 |
+ |
8392 |
+local debug = require"debug" |
8393 |
+ |
8394 |
+ |
8395 |
+-- bug in 5.1: |
8396 |
+ |
8397 |
+local function f(x) x = nil; return x end |
8398 |
+assert(f(10) == nil) |
8399 |
+ |
8400 |
+local function f() local x; return x end |
8401 |
+assert(f(10) == nil) |
8402 |
+ |
8403 |
+local function f(x) x = nil; local y; return x, y end |
8404 |
+assert(f(10) == nil and select(2, f(20)) == nil) |
8405 |
+ |
8406 |
+do |
8407 |
+ local i = 10 |
8408 |
+ do local i = 100; assert(i==100) end |
8409 |
+ do local i = 1000; assert(i==1000) end |
8410 |
+ assert(i == 10) |
8411 |
+ if i ~= 10 then |
8412 |
+ local i = 20 |
8413 |
+ else |
8414 |
+ local i = 30 |
8415 |
+ assert(i == 30) |
8416 |
+ end |
8417 |
+end |
8418 |
+ |
8419 |
+ |
8420 |
+ |
8421 |
+f = nil |
8422 |
+ |
8423 |
+local f |
8424 |
+x = 1 |
8425 |
+ |
8426 |
+a = nil |
8427 |
+load('local a = {}')() |
8428 |
+assert(a == nil) |
8429 |
+ |
8430 |
+function f (a) |
8431 |
+ local _1, _2, _3, _4, _5 |
8432 |
+ local _6, _7, _8, _9, _10 |
8433 |
+ local x = 3 |
8434 |
+ local b = a |
8435 |
+ local c,d = a,b |
8436 |
+ if (d == b) then |
8437 |
+ local x = 'q' |
8438 |
+ x = b |
8439 |
+ assert(x == 2) |
8440 |
+ else |
8441 |
+ assert(nil) |
8442 |
+ end |
8443 |
+ assert(x == 3) |
8444 |
+ local f = 10 |
8445 |
+end |
8446 |
+ |
8447 |
+local b=10 |
8448 |
+local a; repeat local b; a,b=1,2; assert(a+1==b); until a+b==3 |
8449 |
+ |
8450 |
+ |
8451 |
+assert(x == 1) |
8452 |
+ |
8453 |
+f(2) |
8454 |
+assert(type(f) == 'function') |
8455 |
+ |
8456 |
+ |
8457 |
+local function getenv (f) |
8458 |
+ local a,b = debug.getupvalue(f, 1) |
8459 |
+ assert(a == '_ENV') |
8460 |
+ return b |
8461 |
+end |
8462 |
+ |
8463 |
+-- test for global table of loaded chunks |
8464 |
+assert(getenv(load"a=3") == _G) |
8465 |
+local c = {}; local f = load("a = 3", nil, nil, c) |
8466 |
+assert(getenv(f) == c) |
8467 |
+assert(c.a == nil) |
8468 |
+f() |
8469 |
+assert(c.a == 3) |
8470 |
+ |
8471 |
+-- old test for limits for special instructions (now just a generic test) |
8472 |
+do |
8473 |
+ local i = 2 |
8474 |
+ local p = 4 -- p == 2^i |
8475 |
+ repeat |
8476 |
+ for j=-3,3 do |
8477 |
+ assert(load(string.format([[local a=%s; |
8478 |
+ a=a+%s; |
8479 |
+ assert(a ==2^%s)]], j, p-j, i), '')) () |
8480 |
+ assert(load(string.format([[local a=%s; |
8481 |
+ a=a-%s; |
8482 |
+ assert(a==-2^%s)]], -j, p-j, i), '')) () |
8483 |
+ assert(load(string.format([[local a,b=0,%s; |
8484 |
+ a=b-%s; |
8485 |
+ assert(a==-2^%s)]], -j, p-j, i), '')) () |
8486 |
+ end |
8487 |
+ p = 2 * p; i = i + 1 |
8488 |
+ until p <= 0 |
8489 |
+end |
8490 |
+ |
8491 |
+print'+' |
8492 |
+ |
8493 |
+ |
8494 |
+if rawget(_G, "querytab") then |
8495 |
+ -- testing clearing of dead elements from tables |
8496 |
+ collectgarbage("stop") -- stop GC |
8497 |
+ local a = {[{}] = 4, [3] = 0, alo = 1, |
8498 |
+ a1234567890123456789012345678901234567890 = 10} |
8499 |
+ |
8500 |
+ local t = querytab(a) |
8501 |
+ |
8502 |
+ for k,_ in pairs(a) do a[k] = nil end |
8503 |
+ collectgarbage() -- restore GC and collect dead fiels in `a' |
8504 |
+ for i=0,t-1 do |
8505 |
+ local k = querytab(a, i) |
8506 |
+ assert(k == nil or type(k) == 'number' or k == 'alo') |
8507 |
+ end |
8508 |
+end |
8509 |
+ |
8510 |
+ |
8511 |
+-- testing lexical environments |
8512 |
+ |
8513 |
+assert(_ENV == _G) |
8514 |
+ |
8515 |
+do |
8516 |
+local dummy |
8517 |
+local _ENV = (function (...) return ... end)(_G, dummy) -- { |
8518 |
+ |
8519 |
+do local _ENV = {assert=assert}; assert(true) end |
8520 |
+mt = {_G = _G} |
8521 |
+local foo,x |
8522 |
+A = false -- "declare" A |
8523 |
+do local _ENV = mt |
8524 |
+ function foo (x) |
8525 |
+ A = x |
8526 |
+ do local _ENV = _G; A = 1000 end |
8527 |
+ return function (x) return A .. x end |
8528 |
+ end |
8529 |
+end |
8530 |
+assert(getenv(foo) == mt) |
8531 |
+x = foo('hi'); assert(mt.A == 'hi' and A == 1000) |
8532 |
+assert(x('*') == mt.A .. '*') |
8533 |
+ |
8534 |
+do local _ENV = {assert=assert, A=10}; |
8535 |
+ do local _ENV = {assert=assert, A=20}; |
8536 |
+ assert(A==20);x=A |
8537 |
+ end |
8538 |
+ assert(A==10 and x==20) |
8539 |
+end |
8540 |
+assert(x==20) |
8541 |
+ |
8542 |
+ |
8543 |
+print('OK') |
8544 |
+ |
8545 |
+return 5,f |
8546 |
+ |
8547 |
+end -- } |
8548 |
+ |
8549 |
|
8550 |
diff --git a/tests/main.lua b/tests/main.lua |
8551 |
new file mode 100644 |
8552 |
index 0000000..9fc20c1 |
8553 |
--- /dev/null |
8554 |
+++ b/tests/main.lua |
8555 |
@@ -0,0 +1,381 @@ |
8556 |
+# testing special comment on first line |
8557 |
+-- $Id: main.lua,v 1.65 2016/11/07 13:11:28 roberto Exp $ |
8558 |
+-- See Copyright Notice in file all.lua |
8559 |
+ |
8560 |
+-- most (all?) tests here assume a reasonable "Unix-like" shell |
8561 |
+if _port then return end |
8562 |
+ |
8563 |
+-- use only "double quotes" inside shell scripts (better change to |
8564 |
+-- run on Windows) |
8565 |
+ |
8566 |
+ |
8567 |
+print ("testing stand-alone interpreter") |
8568 |
+ |
8569 |
+assert(os.execute()) -- machine has a system command |
8570 |
+ |
8571 |
+local arg = arg or _ARG |
8572 |
+ |
8573 |
+local prog = os.tmpname() |
8574 |
+local otherprog = os.tmpname() |
8575 |
+local out = os.tmpname() |
8576 |
+ |
8577 |
+local progname |
8578 |
+do |
8579 |
+ local i = 0 |
8580 |
+ while arg[i] do i=i-1 end |
8581 |
+ progname = arg[i+1] |
8582 |
+end |
8583 |
+print("progname: "..progname) |
8584 |
+ |
8585 |
+local prepfile = function (s, p) |
8586 |
+ p = p or prog |
8587 |
+ io.output(p) |
8588 |
+ io.write(s) |
8589 |
+ assert(io.close()) |
8590 |
+end |
8591 |
+ |
8592 |
+local function getoutput () |
8593 |
+ io.input(out) |
8594 |
+ local t = io.read("a") |
8595 |
+ io.input():close() |
8596 |
+ assert(os.remove(out)) |
8597 |
+ return t |
8598 |
+end |
8599 |
+ |
8600 |
+local function checkprogout (s) |
8601 |
+ local t = getoutput() |
8602 |
+ for line in string.gmatch(s, ".-\n") do |
8603 |
+ assert(string.find(t, line, 1, true)) |
8604 |
+ end |
8605 |
+end |
8606 |
+ |
8607 |
+local function checkout (s) |
8608 |
+ local t = getoutput() |
8609 |
+ if s ~= t then print(string.format("'%s' - '%s'\n", s, t)) end |
8610 |
+ assert(s == t) |
8611 |
+ return t |
8612 |
+end |
8613 |
+ |
8614 |
+ |
8615 |
+local function RUN (p, ...) |
8616 |
+ p = string.gsub(p, "lua", '"'..progname..'"', 1) |
8617 |
+ local s = string.format(p, ...) |
8618 |
+ assert(os.execute(s)) |
8619 |
+end |
8620 |
+ |
8621 |
+local function NoRun (msg, p, ...) |
8622 |
+ p = string.gsub(p, "lua", '"'..progname..'"', 1) |
8623 |
+ local s = string.format(p, ...) |
8624 |
+ s = string.format("%s 2> %s", s, out) -- will send error to 'out' |
8625 |
+ assert(not os.execute(s)) |
8626 |
+ assert(string.find(getoutput(), msg, 1, true)) -- check error message |
8627 |
+end |
8628 |
+ |
8629 |
+RUN('lua -v') |
8630 |
+ |
8631 |
+print(string.format("(temporary program file used in these tests: %s)", prog)) |
8632 |
+ |
8633 |
+-- running stdin as a file |
8634 |
+prepfile"" |
8635 |
+RUN('lua - < %s > %s', prog, out) |
8636 |
+checkout("") |
8637 |
+ |
8638 |
+prepfile[[ |
8639 |
+ print( |
8640 |
+1, a |
8641 |
+) |
8642 |
+]] |
8643 |
+RUN('lua - < %s > %s', prog, out) |
8644 |
+checkout("1\tnil\n") |
8645 |
+ |
8646 |
+RUN('echo "print(10)\nprint(2)\n" | lua > %s', out) |
8647 |
+checkout("10\n2\n") |
8648 |
+ |
8649 |
+ |
8650 |
+-- test option '-' |
8651 |
+RUN('echo "print(arg[1])" | lua - -h > %s', out) |
8652 |
+checkout("-h\n") |
8653 |
+ |
8654 |
+-- test environment variables used by Lua |
8655 |
+ |
8656 |
+prepfile("print(package.path)") |
8657 |
+ |
8658 |
+-- test LUA_PATH |
8659 |
+RUN('env LUA_INIT= LUA_PATH=x lua %s > %s', prog, out) |
8660 |
+checkout("x\n") |
8661 |
+ |
8662 |
+-- test LUA_PATH_version |
8663 |
+RUN('env LUA_INIT= LUA_PATH_5_3=y LUA_PATH=x lua %s > %s', prog, out) |
8664 |
+checkout("y\n") |
8665 |
+ |
8666 |
+-- test LUA_CPATH |
8667 |
+prepfile("print(package.cpath)") |
8668 |
+RUN('env LUA_INIT= LUA_CPATH=xuxu lua %s > %s', prog, out) |
8669 |
+checkout("xuxu\n") |
8670 |
+ |
8671 |
+-- test LUA_CPATH_version |
8672 |
+RUN('env LUA_INIT= LUA_CPATH_5_3=yacc LUA_CPATH=x lua %s > %s', prog, out) |
8673 |
+checkout("yacc\n") |
8674 |
+ |
8675 |
+-- test LUA_INIT (and its access to 'arg' table) |
8676 |
+prepfile("print(X)") |
8677 |
+RUN('env LUA_INIT="X=tonumber(arg[1])" lua %s 3.2 > %s', prog, out) |
8678 |
+checkout("3.2\n") |
8679 |
+ |
8680 |
+-- test LUA_INIT_version |
8681 |
+prepfile("print(X)") |
8682 |
+RUN('env LUA_INIT_5_3="X=10" LUA_INIT="X=3" lua %s > %s', prog, out) |
8683 |
+checkout("10\n") |
8684 |
+ |
8685 |
+-- test LUA_INIT for files |
8686 |
+prepfile("x = x or 10; print(x); x = x + 1") |
8687 |
+RUN('env LUA_INIT="@%s" lua %s > %s', prog, prog, out) |
8688 |
+checkout("10\n11\n") |
8689 |
+ |
8690 |
+-- test errors in LUA_INIT |
8691 |
+NoRun('LUA_INIT:1: msg', 'env LUA_INIT="error(\'msg\')" lua') |
8692 |
+ |
8693 |
+-- test option '-E' |
8694 |
+local defaultpath, defaultCpath |
8695 |
+ |
8696 |
+do |
8697 |
+ prepfile("print(package.path, package.cpath)") |
8698 |
+ RUN('env LUA_INIT="error(10)" LUA_PATH=xxx LUA_CPATH=xxx lua -E %s > %s', |
8699 |
+ prog, out) |
8700 |
+ local out = getoutput() |
8701 |
+ defaultpath = string.match(out, "^(.-)\t") |
8702 |
+ defaultCpath = string.match(out, "\t(.-)$") |
8703 |
+end |
8704 |
+ |
8705 |
+-- paths did not changed |
8706 |
+assert(not string.find(defaultpath, "xxx") and |
8707 |
+ string.find(defaultpath, "lua") and |
8708 |
+ not string.find(defaultCpath, "xxx") and |
8709 |
+ string.find(defaultCpath, "lua")) |
8710 |
+ |
8711 |
+ |
8712 |
+-- test replacement of ';;' to default path |
8713 |
+local function convert (p) |
8714 |
+ prepfile("print(package.path)") |
8715 |
+ RUN('env LUA_PATH="%s" lua %s > %s', p, prog, out) |
8716 |
+ local expected = getoutput() |
8717 |
+ expected = string.sub(expected, 1, -2) -- cut final end of line |
8718 |
+ assert(string.gsub(p, ";;", ";"..defaultpath..";") == expected) |
8719 |
+end |
8720 |
+ |
8721 |
+convert(";") |
8722 |
+convert(";;") |
8723 |
+convert(";;;") |
8724 |
+convert(";;;;") |
8725 |
+convert(";;;;;") |
8726 |
+convert(";;a;;;bc") |
8727 |
+ |
8728 |
+ |
8729 |
+-- test -l over multiple libraries |
8730 |
+prepfile("print(1); a=2; return {x=15}") |
8731 |
+prepfile(("print(a); print(_G['%s'].x)"):format(prog), otherprog) |
8732 |
+RUN('env LUA_PATH="?;;" lua -l %s -l%s -lstring -l io %s > %s', prog, otherprog, otherprog, out) |
8733 |
+checkout("1\n2\n15\n2\n15\n") |
8734 |
+ |
8735 |
+-- test 'arg' table |
8736 |
+local a = [[ |
8737 |
+ assert(#arg == 3 and arg[1] == 'a' and |
8738 |
+ arg[2] == 'b' and arg[3] == 'c') |
8739 |
+ assert(arg[-1] == '--' and arg[-2] == "-e " and arg[-3] == '%s') |
8740 |
+ assert(arg[4] == nil and arg[-4] == nil) |
8741 |
+ local a, b, c = ... |
8742 |
+ assert(... == 'a' and a == 'a' and b == 'b' and c == 'c') |
8743 |
+]] |
8744 |
+a = string.format(a, progname) |
8745 |
+prepfile(a) |
8746 |
+RUN('lua "-e " -- %s a b c', prog) -- "-e " runs an empty command |
8747 |
+ |
8748 |
+-- test 'arg' availability in libraries |
8749 |
+prepfile"assert(arg)" |
8750 |
+prepfile("assert(arg)", otherprog) |
8751 |
+RUN('env LUA_PATH="?;;" lua -l%s - < %s', prog, otherprog) |
8752 |
+ |
8753 |
+-- test messing up the 'arg' table |
8754 |
+RUN('echo "print(...)" | lua -e "arg[1] = 100" - > %s', out) |
8755 |
+checkout("100\n") |
8756 |
+NoRun("'arg' is not a table", 'echo "" | lua -e "arg = 1" -') |
8757 |
+ |
8758 |
+-- test error in 'print' |
8759 |
+RUN('echo 10 | lua -e "print=nil" -i > /dev/null 2> %s', out) |
8760 |
+assert(string.find(getoutput(), "error calling 'print'")) |
8761 |
+ |
8762 |
+-- test 'debug.debug' |
8763 |
+RUN('echo "io.stderr:write(1000)\ncont" | lua -e "require\'debug\'.debug()" 2> %s', out) |
8764 |
+checkout("lua_debug> 1000lua_debug> ") |
8765 |
+ |
8766 |
+-- test many arguments |
8767 |
+prepfile[[print(({...})[30])]] |
8768 |
+RUN('lua %s %s > %s', prog, string.rep(" a", 30), out) |
8769 |
+checkout("a\n") |
8770 |
+ |
8771 |
+RUN([[lua "-eprint(1)" -ea=3 -e "print(a)" > %s]], out) |
8772 |
+checkout("1\n3\n") |
8773 |
+ |
8774 |
+-- test iteractive mode |
8775 |
+prepfile[[ |
8776 |
+(6*2-6) -- === |
8777 |
+a = |
8778 |
+10 |
8779 |
+print(a) |
8780 |
+a]] |
8781 |
+RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out) |
8782 |
+checkprogout("6\n10\n10\n\n") |
8783 |
+ |
8784 |
+prepfile("a = [[b\nc\nd\ne]]\n=a") |
8785 |
+RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out) |
8786 |
+checkprogout("b\nc\nd\ne\n\n") |
8787 |
+ |
8788 |
+prompt = "alo" |
8789 |
+prepfile[[ -- |
8790 |
+a = 2 |
8791 |
+]] |
8792 |
+RUN([[lua "-e_PROMPT='%s'" -i < %s > %s]], prompt, prog, out) |
8793 |
+local t = getoutput() |
8794 |
+assert(string.find(t, prompt .. ".*" .. prompt .. ".*" .. prompt)) |
8795 |
+ |
8796 |
+-- test for error objects |
8797 |
+prepfile[[ |
8798 |
+debug = require "debug" |
8799 |
+m = {x=0} |
8800 |
+setmetatable(m, {__tostring = function(x) |
8801 |
+ return tostring(debug.getinfo(4).currentline + x.x) |
8802 |
+end}) |
8803 |
+error(m) |
8804 |
+]] |
8805 |
+NoRun(progname .. ": 6\n", [[lua %s]], prog) |
8806 |
+ |
8807 |
+prepfile("error{}") |
8808 |
+NoRun("error object is a table value", [[lua %s]], prog) |
8809 |
+ |
8810 |
+ |
8811 |
+-- chunk broken in many lines |
8812 |
+s = [=[ -- |
8813 |
+function f ( x ) |
8814 |
+ local a = [[ |
8815 |
+xuxu |
8816 |
+]] |
8817 |
+ local b = "\ |
8818 |
+xuxu\n" |
8819 |
+ if x == 11 then return 1 + 12 , 2 + 20 end --[[ test multiple returns ]] |
8820 |
+ return x + 1 |
8821 |
+ --\\ |
8822 |
+end |
8823 |
+return( f( 100 ) ) |
8824 |
+assert( a == b ) |
8825 |
+do return f( 11 ) end ]=] |
8826 |
+s = string.gsub(s, ' ', '\n\n') -- change all spaces for newlines |
8827 |
+prepfile(s) |
8828 |
+RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out) |
8829 |
+checkprogout("101\n13\t22\n\n") |
8830 |
+ |
8831 |
+prepfile[[#comment in 1st line without \n at the end]] |
8832 |
+RUN('lua %s', prog) |
8833 |
+ |
8834 |
+prepfile[[#test line number when file starts with comment line |
8835 |
+debug = require"debug" |
8836 |
+print(debug.getinfo(1).currentline) |
8837 |
+]] |
8838 |
+RUN('lua %s > %s', prog, out) |
8839 |
+checkprogout('3') |
8840 |
+ |
8841 |
+-- close Lua with an open file |
8842 |
+prepfile(string.format([[io.output(%q); io.write('alo')]], out)) |
8843 |
+RUN('lua %s', prog) |
8844 |
+checkout('alo') |
8845 |
+ |
8846 |
+-- bug in 5.2 beta (extra \0 after version line) |
8847 |
+RUN([[lua -v -e"print'hello'" > %s]], out) |
8848 |
+t = getoutput() |
8849 |
+assert(string.find(t, "PUC%-Rio\nhello")) |
8850 |
+ |
8851 |
+ |
8852 |
+-- testing os.exit |
8853 |
+prepfile("os.exit(nil, true)") |
8854 |
+RUN('lua %s', prog) |
8855 |
+prepfile("os.exit(0, true)") |
8856 |
+RUN('lua %s', prog) |
8857 |
+prepfile("os.exit(true, true)") |
8858 |
+RUN('lua %s', prog) |
8859 |
+prepfile("os.exit(1, true)") |
8860 |
+NoRun("", "lua %s", prog) -- no message |
8861 |
+prepfile("os.exit(false, true)") |
8862 |
+NoRun("", "lua %s", prog) -- no message |
8863 |
+ |
8864 |
+-- remove temporary files |
8865 |
+assert(os.remove(prog)) |
8866 |
+assert(os.remove(otherprog)) |
8867 |
+assert(not os.remove(out)) |
8868 |
+ |
8869 |
+-- invalid options |
8870 |
+NoRun("unrecognized option '-h'", "lua -h") |
8871 |
+NoRun("unrecognized option '---'", "lua ---") |
8872 |
+NoRun("unrecognized option '-Ex'", "lua -Ex") |
8873 |
+NoRun("unrecognized option '-vv'", "lua -vv") |
8874 |
+NoRun("unrecognized option '-iv'", "lua -iv") |
8875 |
+NoRun("'-e' needs argument", "lua -e") |
8876 |
+NoRun("syntax error", "lua -e a") |
8877 |
+NoRun("'-l' needs argument", "lua -l") |
8878 |
+ |
8879 |
+ |
8880 |
+if T then -- auxiliary library? |
8881 |
+ print("testing 'not enough memory' to create a state") |
8882 |
+ NoRun("not enough memory", "env MEMLIMIT=100 lua") |
8883 |
+end |
8884 |
+print('+') |
8885 |
+ |
8886 |
+print('testing Ctrl C') |
8887 |
+do |
8888 |
+ -- interrupt a script |
8889 |
+ local function kill (pid) |
8890 |
+ return os.execute(string.format('kill -INT %d 2> /dev/null', pid)) |
8891 |
+ end |
8892 |
+ |
8893 |
+ -- function to run a script in background, returning its output file |
8894 |
+ -- descriptor and its pid |
8895 |
+ local function runback (luaprg) |
8896 |
+ -- shell script to run 'luaprg' in background and echo its pid |
8897 |
+ local shellprg = string.format('%s -e "%s" & echo $!', progname, luaprg) |
8898 |
+ local f = io.popen(shellprg, "r") -- run shell script |
8899 |
+ local pid = f:read() -- get pid for Lua script |
8900 |
+ print("(if test fails now, it may leave a Lua script running in \z |
8901 |
+ background, pid " .. pid .. ")") |
8902 |
+ return f, pid |
8903 |
+ end |
8904 |
+ |
8905 |
+ -- Lua script that runs protected infinite loop and then prints '42' |
8906 |
+ local f, pid = runback[[ |
8907 |
+ pcall(function () print(12); while true do end end); print(42)]] |
8908 |
+ -- wait until script is inside 'pcall' |
8909 |
+ assert(f:read() == "12") |
8910 |
+ kill(pid) -- send INT signal to Lua script |
8911 |
+ -- check that 'pcall' captured the exception and script continued running |
8912 |
+ assert(f:read() == "42") -- expected output |
8913 |
+ assert(f:close()) |
8914 |
+ print("done") |
8915 |
+ |
8916 |
+ -- Lua script in a long unbreakable search |
8917 |
+ local f, pid = runback[[ |
8918 |
+ print(15); string.find(string.rep('a', 100000), '.*b')]] |
8919 |
+ -- wait (so script can reach the loop) |
8920 |
+ assert(f:read() == "15") |
8921 |
+ assert(os.execute("sleep 1")) |
8922 |
+ -- must send at least two INT signals to stop this Lua script |
8923 |
+ local n = 100 |
8924 |
+ for i = 0, 100 do -- keep sending signals |
8925 |
+ if not kill(pid) then -- until it fails |
8926 |
+ n = i -- number of non-failed kills |
8927 |
+ break |
8928 |
+ end |
8929 |
+ end |
8930 |
+ assert(f:close()) |
8931 |
+ assert(n >= 2) |
8932 |
+ print(string.format("done (with %d kills)", n)) |
8933 |
+ |
8934 |
+end |
8935 |
+ |
8936 |
+print("OK") |
8937 |
|
8938 |
diff --git a/tests/math.lua b/tests/math.lua |
8939 |
new file mode 100644 |
8940 |
index 0000000..53ce9b5 |
8941 |
--- /dev/null |
8942 |
+++ b/tests/math.lua |
8943 |
@@ -0,0 +1,824 @@ |
8944 |
+-- $Id: math.lua,v 1.78 2016/11/07 13:11:28 roberto Exp $ |
8945 |
+-- See Copyright Notice in file all.lua |
8946 |
+ |
8947 |
+print("testing numbers and math lib") |
8948 |
+ |
8949 |
+local minint = math.mininteger |
8950 |
+local maxint = math.maxinteger |
8951 |
+ |
8952 |
+local intbits = math.floor(math.log(maxint, 2) + 0.5) + 1 |
8953 |
+assert((1 << intbits) == 0) |
8954 |
+ |
8955 |
+assert(minint == 1 << (intbits - 1)) |
8956 |
+assert(maxint == minint - 1) |
8957 |
+ |
8958 |
+-- number of bits in the mantissa of a floating-point number |
8959 |
+local floatbits = 24 |
8960 |
+do |
8961 |
+ local p = 2.0^floatbits |
8962 |
+ while p < p + 1.0 do |
8963 |
+ p = p * 2.0 |
8964 |
+ floatbits = floatbits + 1 |
8965 |
+ end |
8966 |
+end |
8967 |
+ |
8968 |
+local function isNaN (x) |
8969 |
+ return (x ~= x) |
8970 |
+end |
8971 |
+ |
8972 |
+assert(isNaN(0/0)) |
8973 |
+assert(not isNaN(1/0)) |
8974 |
+ |
8975 |
+ |
8976 |
+do |
8977 |
+ local x = 2.0^floatbits |
8978 |
+ assert(x > x - 1.0 and x == x + 1.0) |
8979 |
+ |
8980 |
+ print(string.format("%d-bit integers, %d-bit (mantissa) floats", |
8981 |
+ intbits, floatbits)) |
8982 |
+end |
8983 |
+ |
8984 |
+assert(math.type(0) == "integer" and math.type(0.0) == "float" |
8985 |
+ and math.type("10") == nil) |
8986 |
+ |
8987 |
+ |
8988 |
+local function checkerror (msg, f, ...) |
8989 |
+ local s, err = pcall(f, ...) |
8990 |
+ assert(not s and string.find(err, msg)) |
8991 |
+end |
8992 |
+ |
8993 |
+local msgf2i = "number.* has no integer representation" |
8994 |
+ |
8995 |
+-- float equality |
8996 |
+function eq (a,b,limit) |
8997 |
+ if not limit then |
8998 |
+ if floatbits >= 50 then limit = 1E-11 |
8999 |
+ else limit = 1E-5 |
9000 |
+ end |
9001 |
+ end |
9002 |
+ -- a == b needed for +inf/-inf |
9003 |
+ return a == b or math.abs(a-b) <= limit |
9004 |
+end |
9005 |
+ |
9006 |
+ |
9007 |
+-- equality with types |
9008 |
+function eqT (a,b) |
9009 |
+ return a == b and math.type(a) == math.type(b) |
9010 |
+end |
9011 |
+ |
9012 |
+ |
9013 |
+-- basic float notation |
9014 |
+assert(0e12 == 0 and .0 == 0 and 0. == 0 and .2e2 == 20 and 2.E-1 == 0.2) |
9015 |
+ |
9016 |
+do |
9017 |
+ local a,b,c = "2", " 3e0 ", " 10 " |
9018 |
+ assert(a+b == 5 and -b == -3 and b+"2" == 5 and "10"-c == 0) |
9019 |
+ assert(type(a) == 'string' and type(b) == 'string' and type(c) == 'string') |
9020 |
+ assert(a == "2" and b == " 3e0 " and c == " 10 " and -c == -" 10 ") |
9021 |
+ assert(c%a == 0 and a^b == 08) |
9022 |
+ a = 0 |
9023 |
+ assert(a == -a and 0 == -0) |
9024 |
+end |
9025 |
+ |
9026 |
+do |
9027 |
+ local x = -1 |
9028 |
+ local mz = 0/x -- minus zero |
9029 |
+ t = {[0] = 10, 20, 30, 40, 50} |
9030 |
+ assert(t[mz] == t[0] and t[-0] == t[0]) |
9031 |
+end |
9032 |
+ |
9033 |
+do -- tests for 'modf' |
9034 |
+ local a,b = math.modf(3.5) |
9035 |
+ assert(a == 3.0 and b == 0.5) |
9036 |
+ a,b = math.modf(-2.5) |
9037 |
+ assert(a == -2.0 and b == -0.5) |
9038 |
+ a,b = math.modf(-3e23) |
9039 |
+ assert(a == -3e23 and b == 0.0) |
9040 |
+ a,b = math.modf(3e35) |
9041 |
+ assert(a == 3e35 and b == 0.0) |
9042 |
+ a,b = math.modf(-1/0) -- -inf |
9043 |
+ assert(a == -1/0 and b == 0.0) |
9044 |
+ a,b = math.modf(1/0) -- inf |
9045 |
+ assert(a == 1/0 and b == 0.0) |
9046 |
+ a,b = math.modf(0/0) -- NaN |
9047 |
+ assert(isNaN(a) and isNaN(b)) |
9048 |
+ a,b = math.modf(3) -- integer argument |
9049 |
+ assert(eqT(a, 3) and eqT(b, 0.0)) |
9050 |
+ a,b = math.modf(minint) |
9051 |
+ assert(eqT(a, minint) and eqT(b, 0.0)) |
9052 |
+end |
9053 |
+ |
9054 |
+assert(math.huge > 10e30) |
9055 |
+assert(-math.huge < -10e30) |
9056 |
+ |
9057 |
+ |
9058 |
+-- integer arithmetic |
9059 |
+assert(minint < minint + 1) |
9060 |
+assert(maxint - 1 < maxint) |
9061 |
+assert(0 - minint == minint) |
9062 |
+assert(minint * minint == 0) |
9063 |
+assert(maxint * maxint * maxint == maxint) |
9064 |
+ |
9065 |
+ |
9066 |
+-- testing floor division and conversions |
9067 |
+ |
9068 |
+for _, i in pairs{-16, -15, -3, -2, -1, 0, 1, 2, 3, 15} do |
9069 |
+ for _, j in pairs{-16, -15, -3, -2, -1, 1, 2, 3, 15} do |
9070 |
+ for _, ti in pairs{0, 0.0} do -- try 'i' as integer and as float |
9071 |
+ for _, tj in pairs{0, 0.0} do -- try 'j' as integer and as float |
9072 |
+ local x = i + ti |
9073 |
+ local y = j + tj |
9074 |
+ assert(i//j == math.floor(i/j)) |
9075 |
+ end |
9076 |
+ end |
9077 |
+ end |
9078 |
+end |
9079 |
+ |
9080 |
+assert(1//0.0 == 1/0) |
9081 |
+assert(-1 // 0.0 == -1/0) |
9082 |
+assert(eqT(3.5 // 1.5, 2.0)) |
9083 |
+assert(eqT(3.5 // -1.5, -3.0)) |
9084 |
+ |
9085 |
+assert(maxint // maxint == 1) |
9086 |
+assert(maxint // 1 == maxint) |
9087 |
+assert((maxint - 1) // maxint == 0) |
9088 |
+assert(maxint // (maxint - 1) == 1) |
9089 |
+assert(minint // minint == 1) |
9090 |
+assert(minint // minint == 1) |
9091 |
+assert((minint + 1) // minint == 0) |
9092 |
+assert(minint // (minint + 1) == 1) |
9093 |
+assert(minint // 1 == minint) |
9094 |
+ |
9095 |
+assert(minint // -1 == -minint) |
9096 |
+assert(minint // -2 == 2^(intbits - 2)) |
9097 |
+assert(maxint // -1 == -maxint) |
9098 |
+ |
9099 |
+ |
9100 |
+-- negative exponents |
9101 |
+do |
9102 |
+ assert(2^-3 == 1 / 2^3) |
9103 |
+ assert(eq((-3)^-3, 1 / (-3)^3)) |
9104 |
+ for i = -3, 3 do -- variables avoid constant folding |
9105 |
+ for j = -3, 3 do |
9106 |
+ -- domain errors (0^(-n)) are not portable |
9107 |
+ if not _port or i ~= 0 or j > 0 then |
9108 |
+ assert(eq(i^j, 1 / i^(-j))) |
9109 |
+ end |
9110 |
+ end |
9111 |
+ end |
9112 |
+end |
9113 |
+ |
9114 |
+-- comparison between floats and integers (border cases) |
9115 |
+if floatbits < intbits then |
9116 |
+ assert(2.0^floatbits == (1 << floatbits)) |
9117 |
+ assert(2.0^floatbits - 1.0 == (1 << floatbits) - 1.0) |
9118 |
+ assert(2.0^floatbits - 1.0 ~= (1 << floatbits)) |
9119 |
+ -- float is rounded, int is not |
9120 |
+ assert(2.0^floatbits + 1.0 ~= (1 << floatbits) + 1) |
9121 |
+else -- floats can express all integers with full accuracy |
9122 |
+ assert(maxint == maxint + 0.0) |
9123 |
+ assert(maxint - 1 == maxint - 1.0) |
9124 |
+ assert(minint + 1 == minint + 1.0) |
9125 |
+ assert(maxint ~= maxint - 1.0) |
9126 |
+end |
9127 |
+assert(maxint + 0.0 == 2.0^(intbits - 1) - 1.0) |
9128 |
+assert(minint + 0.0 == minint) |
9129 |
+assert(minint + 0.0 == -2.0^(intbits - 1)) |
9130 |
+ |
9131 |
+ |
9132 |
+-- order between floats and integers |
9133 |
+assert(1 < 1.1); assert(not (1 < 0.9)) |
9134 |
+assert(1 <= 1.1); assert(not (1 <= 0.9)) |
9135 |
+assert(-1 < -0.9); assert(not (-1 < -1.1)) |
9136 |
+assert(1 <= 1.1); assert(not (-1 <= -1.1)) |
9137 |
+assert(-1 < -0.9); assert(not (-1 < -1.1)) |
9138 |
+assert(-1 <= -0.9); assert(not (-1 <= -1.1)) |
9139 |
+assert(minint <= minint + 0.0) |
9140 |
+assert(minint + 0.0 <= minint) |
9141 |
+assert(not (minint < minint + 0.0)) |
9142 |
+assert(not (minint + 0.0 < minint)) |
9143 |
+assert(maxint < minint * -1.0) |
9144 |
+assert(maxint <= minint * -1.0) |
9145 |
+ |
9146 |
+do |
9147 |
+ local fmaxi1 = 2^(intbits - 1) |
9148 |
+ assert(maxint < fmaxi1) |
9149 |
+ assert(maxint <= fmaxi1) |
9150 |
+ assert(not (fmaxi1 <= maxint)) |
9151 |
+ assert(minint <= -2^(intbits - 1)) |
9152 |
+ assert(-2^(intbits - 1) <= minint) |
9153 |
+end |
9154 |
+ |
9155 |
+if floatbits < intbits then |
9156 |
+ print("testing order (floats cannot represent all integers)") |
9157 |
+ local fmax = 2^floatbits |
9158 |
+ local ifmax = fmax | 0 |
9159 |
+ assert(fmax < ifmax + 1) |
9160 |
+ assert(fmax - 1 < ifmax) |
9161 |
+ assert(-(fmax - 1) > -ifmax) |
9162 |
+ assert(not (fmax <= ifmax - 1)) |
9163 |
+ assert(-fmax > -(ifmax + 1)) |
9164 |
+ assert(not (-fmax >= -(ifmax - 1))) |
9165 |
+ |
9166 |
+ assert(fmax/2 - 0.5 < ifmax//2) |
9167 |
+ assert(-(fmax/2 - 0.5) > -ifmax//2) |
9168 |
+ |
9169 |
+ assert(maxint < 2^intbits) |
9170 |
+ assert(minint > -2^intbits) |
9171 |
+ assert(maxint <= 2^intbits) |
9172 |
+ assert(minint >= -2^intbits) |
9173 |
+else |
9174 |
+ print("testing order (floats can represent all integers)") |
9175 |
+ assert(maxint < maxint + 1.0) |
9176 |
+ assert(maxint < maxint + 0.5) |
9177 |
+ assert(maxint - 1.0 < maxint) |
9178 |
+ assert(maxint - 0.5 < maxint) |
9179 |
+ assert(not (maxint + 0.0 < maxint)) |
9180 |
+ assert(maxint + 0.0 <= maxint) |
9181 |
+ assert(not (maxint < maxint + 0.0)) |
9182 |
+ assert(maxint + 0.0 <= maxint) |
9183 |
+ assert(maxint <= maxint + 0.0) |
9184 |
+ assert(not (maxint + 1.0 <= maxint)) |
9185 |
+ assert(not (maxint + 0.5 <= maxint)) |
9186 |
+ assert(not (maxint <= maxint - 1.0)) |
9187 |
+ assert(not (maxint <= maxint - 0.5)) |
9188 |
+ |
9189 |
+ assert(minint < minint + 1.0) |
9190 |
+ assert(minint < minint + 0.5) |
9191 |
+ assert(minint <= minint + 0.5) |
9192 |
+ assert(minint - 1.0 < minint) |
9193 |
+ assert(minint - 1.0 <= minint) |
9194 |
+ assert(not (minint + 0.0 < minint)) |
9195 |
+ assert(not (minint + 0.5 < minint)) |
9196 |
+ assert(not (minint < minint + 0.0)) |
9197 |
+ assert(minint + 0.0 <= minint) |
9198 |
+ assert(minint <= minint + 0.0) |
9199 |
+ assert(not (minint + 1.0 <= minint)) |
9200 |
+ assert(not (minint + 0.5 <= minint)) |
9201 |
+ assert(not (minint <= minint - 1.0)) |
9202 |
+end |
9203 |
+ |
9204 |
+do |
9205 |
+ local NaN = 0/0 |
9206 |
+ assert(not (NaN < 0)) |
9207 |
+ assert(not (NaN > minint)) |
9208 |
+ assert(not (NaN <= -9)) |
9209 |
+ assert(not (NaN <= maxint)) |
9210 |
+ assert(not (NaN < maxint)) |
9211 |
+ assert(not (minint <= NaN)) |
9212 |
+ assert(not (minint < NaN)) |
9213 |
+end |
9214 |
+ |
9215 |
+ |
9216 |
+-- avoiding errors at compile time |
9217 |
+local function checkcompt (msg, code) |
9218 |
+ checkerror(msg, assert(load(code))) |
9219 |
+end |
9220 |
+checkcompt("divide by zero", "return 2 // 0") |
9221 |
+checkcompt(msgf2i, "return 2.3 >> 0") |
9222 |
+checkcompt(msgf2i, ("return 2.0^%d & 1"):format(intbits - 1)) |
9223 |
+checkcompt("field 'huge'", "return math.huge << 1") |
9224 |
+checkcompt(msgf2i, ("return 1 | 2.0^%d"):format(intbits - 1)) |
9225 |
+checkcompt(msgf2i, "return 2.3 ~ '0.0'") |
9226 |
+ |
9227 |
+ |
9228 |
+-- testing overflow errors when converting from float to integer (runtime) |
9229 |
+local function f2i (x) return x | x end |
9230 |
+checkerror(msgf2i, f2i, math.huge) -- +inf |
9231 |
+checkerror(msgf2i, f2i, -math.huge) -- -inf |
9232 |
+checkerror(msgf2i, f2i, 0/0) -- NaN |
9233 |
+ |
9234 |
+if floatbits < intbits then |
9235 |
+ -- conversion tests when float cannot represent all integers |
9236 |
+ assert(maxint + 1.0 == maxint + 0.0) |
9237 |
+ assert(minint - 1.0 == minint + 0.0) |
9238 |
+ checkerror(msgf2i, f2i, maxint + 0.0) |
9239 |
+ assert(f2i(2.0^(intbits - 2)) == 1 << (intbits - 2)) |
9240 |
+ assert(f2i(-2.0^(intbits - 2)) == -(1 << (intbits - 2))) |
9241 |
+ assert((2.0^(floatbits - 1) + 1.0) // 1 == (1 << (floatbits - 1)) + 1) |
9242 |
+ -- maximum integer representable as a float |
9243 |
+ local mf = maxint - (1 << (floatbits - intbits)) + 1 |
9244 |
+ assert(f2i(mf + 0.0) == mf) -- OK up to here |
9245 |
+ mf = mf + 1 |
9246 |
+ assert(f2i(mf + 0.0) ~= mf) -- no more representable |
9247 |
+else |
9248 |
+ -- conversion tests when float can represent all integers |
9249 |
+ assert(maxint + 1.0 > maxint) |
9250 |
+ assert(minint - 1.0 < minint) |
9251 |
+ assert(f2i(maxint + 0.0) == maxint) |
9252 |
+ checkerror("no integer rep", f2i, maxint + 1.0) |
9253 |
+ checkerror("no integer rep", f2i, minint - 1.0) |
9254 |
+end |
9255 |
+ |
9256 |
+-- 'minint' should be representable as a float no matter the precision |
9257 |
+assert(f2i(minint + 0.0) == minint) |
9258 |
+ |
9259 |
+ |
9260 |
+-- testing numeric strings |
9261 |
+ |
9262 |
+assert("2" + 1 == 3) |
9263 |
+assert("2 " + 1 == 3) |
9264 |
+assert(" -2 " + 1 == -1) |
9265 |
+assert(" -0xa " + 1 == -9) |
9266 |
+ |
9267 |
+ |
9268 |
+-- Literal integer Overflows (new behavior in 5.3.3) |
9269 |
+do |
9270 |
+ -- no overflows |
9271 |
+ assert(eqT(tonumber(tostring(maxint)), maxint)) |
9272 |
+ assert(eqT(tonumber(tostring(minint)), minint)) |
9273 |
+ |
9274 |
+ -- add 1 to last digit as a string (it cannot be 9...) |
9275 |
+ local function incd (n) |
9276 |
+ local s = string.format("%d", n) |
9277 |
+ s = string.gsub(s, "%d$", function (d) |
9278 |
+ assert(d ~= '9') |
9279 |
+ return string.char(string.byte(d) + 1) |
9280 |
+ end) |
9281 |
+ return s |
9282 |
+ end |
9283 |
+ |
9284 |
+ -- 'tonumber' with overflow by 1 |
9285 |
+ assert(eqT(tonumber(incd(maxint)), maxint + 1.0)) |
9286 |
+ assert(eqT(tonumber(incd(minint)), minint - 1.0)) |
9287 |
+ |
9288 |
+ -- large numbers |
9289 |
+ assert(eqT(tonumber("1"..string.rep("0", 30)), 1e30)) |
9290 |
+ assert(eqT(tonumber("-1"..string.rep("0", 30)), -1e30)) |
9291 |
+ |
9292 |
+ -- hexa format still wraps around |
9293 |
+ assert(eqT(tonumber("0x1"..string.rep("0", 30)), 0)) |
9294 |
+ |
9295 |
+ -- lexer in the limits |
9296 |
+ assert(minint == load("return " .. minint)()) |
9297 |
+ assert(eqT(maxint, load("return " .. maxint)())) |
9298 |
+ |
9299 |
+ assert(eqT(10000000000000000000000.0, 10000000000000000000000)) |
9300 |
+ assert(eqT(-10000000000000000000000.0, -10000000000000000000000)) |
9301 |
+end |
9302 |
+ |
9303 |
+ |
9304 |
+-- testing 'tonumber' |
9305 |
+ |
9306 |
+-- 'tonumber' with numbers |
9307 |
+assert(tonumber(3.4) == 3.4) |
9308 |
+assert(eqT(tonumber(3), 3)) |
9309 |
+assert(eqT(tonumber(maxint), maxint) and eqT(tonumber(minint), minint)) |
9310 |
+assert(tonumber(1/0) == 1/0) |
9311 |
+ |
9312 |
+-- 'tonumber' with strings |
9313 |
+assert(tonumber("0") == 0) |
9314 |
+assert(tonumber("") == nil) |
9315 |
+assert(tonumber(" ") == nil) |
9316 |
+assert(tonumber("-") == nil) |
9317 |
+assert(tonumber(" -0x ") == nil) |
9318 |
+assert(tonumber{} == nil) |
9319 |
+assert(tonumber'+0.01' == 1/100 and tonumber'+.01' == 0.01 and |
9320 |
+ tonumber'.01' == 0.01 and tonumber'-1.' == -1 and |
9321 |
+ tonumber'+1.' == 1) |
9322 |
+assert(tonumber'+ 0.01' == nil and tonumber'+.e1' == nil and |
9323 |
+ tonumber'1e' == nil and tonumber'1.0e+' == nil and |
9324 |
+ tonumber'.' == nil) |
9325 |
+assert(tonumber('-012') == -010-2) |
9326 |
+assert(tonumber('-1.2e2') == - - -120) |
9327 |
+ |
9328 |
+assert(tonumber("0xffffffffffff") == (1 << (4*12)) - 1) |
9329 |
+assert(tonumber("0x"..string.rep("f", (intbits//4))) == -1) |
9330 |
+assert(tonumber("-0x"..string.rep("f", (intbits//4))) == 1) |
9331 |
+ |
9332 |
+-- testing 'tonumber' with base |
9333 |
+assert(tonumber(' 001010 ', 2) == 10) |
9334 |
+assert(tonumber(' 001010 ', 10) == 001010) |
9335 |
+assert(tonumber(' -1010 ', 2) == -10) |
9336 |
+assert(tonumber('10', 36) == 36) |
9337 |
+assert(tonumber(' -10 ', 36) == -36) |
9338 |
+assert(tonumber(' +1Z ', 36) == 36 + 35) |
9339 |
+assert(tonumber(' -1z ', 36) == -36 + -35) |
9340 |
+assert(tonumber('-fFfa', 16) == -(10+(16*(15+(16*(15+(16*15))))))) |
9341 |
+assert(tonumber(string.rep('1', (intbits - 2)), 2) + 1 == 2^(intbits - 2)) |
9342 |
+assert(tonumber('ffffFFFF', 16)+1 == (1 << 32)) |
9343 |
+assert(tonumber('0ffffFFFF', 16)+1 == (1 << 32)) |
9344 |
+assert(tonumber('-0ffffffFFFF', 16) - 1 == -(1 << 40)) |
9345 |
+for i = 2,36 do |
9346 |
+ local i2 = i * i |
9347 |
+ local i10 = i2 * i2 * i2 * i2 * i2 -- i^10 |
9348 |
+ assert(tonumber('\t10000000000\t', i) == i10) |
9349 |
+end |
9350 |
+ |
9351 |
+if not _soft then |
9352 |
+ -- tests with very long numerals |
9353 |
+ assert(tonumber("0x"..string.rep("f", 13)..".0") == 2.0^(4*13) - 1) |
9354 |
+ assert(tonumber("0x"..string.rep("f", 150)..".0") == 2.0^(4*150) - 1) |
9355 |
+ assert(tonumber("0x"..string.rep("f", 300)..".0") == 2.0^(4*300) - 1) |
9356 |
+ assert(tonumber("0x"..string.rep("f", 500)..".0") == 2.0^(4*500) - 1) |
9357 |
+ assert(tonumber('0x3.' .. string.rep('0', 1000)) == 3) |
9358 |
+ assert(tonumber('0x' .. string.rep('0', 1000) .. 'a') == 10) |
9359 |
+ assert(tonumber('0x0.' .. string.rep('0', 13).."1") == 2.0^(-4*14)) |
9360 |
+ assert(tonumber('0x0.' .. string.rep('0', 150).."1") == 2.0^(-4*151)) |
9361 |
+ assert(tonumber('0x0.' .. string.rep('0', 300).."1") == 2.0^(-4*301)) |
9362 |
+ assert(tonumber('0x0.' .. string.rep('0', 500).."1") == 2.0^(-4*501)) |
9363 |
+ |
9364 |
+ assert(tonumber('0xe03' .. string.rep('0', 1000) .. 'p-4000') == 3587.0) |
9365 |
+ assert(tonumber('0x.' .. string.rep('0', 1000) .. '74p4004') == 0x7.4) |
9366 |
+end |
9367 |
+ |
9368 |
+-- testing 'tonumber' for invalid formats |
9369 |
+ |
9370 |
+local function f (...) |
9371 |
+ if select('#', ...) == 1 then |
9372 |
+ return (...) |
9373 |
+ else |
9374 |
+ return "***" |
9375 |
+ end |
9376 |
+end |
9377 |
+ |
9378 |
+assert(f(tonumber('fFfa', 15)) == nil) |
9379 |
+assert(f(tonumber('099', 8)) == nil) |
9380 |
+assert(f(tonumber('1\0', 2)) == nil) |
9381 |
+assert(f(tonumber('', 8)) == nil) |
9382 |
+assert(f(tonumber(' ', 9)) == nil) |
9383 |
+assert(f(tonumber(' ', 9)) == nil) |
9384 |
+assert(f(tonumber('0xf', 10)) == nil) |
9385 |
+ |
9386 |
+assert(f(tonumber('inf')) == nil) |
9387 |
+assert(f(tonumber(' INF ')) == nil) |
9388 |
+assert(f(tonumber('Nan')) == nil) |
9389 |
+assert(f(tonumber('nan')) == nil) |
9390 |
+ |
9391 |
+assert(f(tonumber(' ')) == nil) |
9392 |
+assert(f(tonumber('')) == nil) |
9393 |
+assert(f(tonumber('1 a')) == nil) |
9394 |
+assert(f(tonumber('1 a', 2)) == nil) |
9395 |
+assert(f(tonumber('1\0')) == nil) |
9396 |
+assert(f(tonumber('1 \0')) == nil) |
9397 |
+assert(f(tonumber('1\0 ')) == nil) |
9398 |
+assert(f(tonumber('e1')) == nil) |
9399 |
+assert(f(tonumber('e 1')) == nil) |
9400 |
+assert(f(tonumber(' 3.4.5 ')) == nil) |
9401 |
+ |
9402 |
+ |
9403 |
+-- testing 'tonumber' for invalid hexadecimal formats |
9404 |
+ |
9405 |
+assert(tonumber('0x') == nil) |
9406 |
+assert(tonumber('x') == nil) |
9407 |
+assert(tonumber('x3') == nil) |
9408 |
+assert(tonumber('0x3.3.3') == nil) -- two decimal points |
9409 |
+assert(tonumber('00x2') == nil) |
9410 |
+assert(tonumber('0x 2') == nil) |
9411 |
+assert(tonumber('0 x2') == nil) |
9412 |
+assert(tonumber('23x') == nil) |
9413 |
+assert(tonumber('- 0xaa') == nil) |
9414 |
+assert(tonumber('-0xaaP ') == nil) -- no exponent |
9415 |
+assert(tonumber('0x0.51p') == nil) |
9416 |
+assert(tonumber('0x5p+-2') == nil) |
9417 |
+ |
9418 |
+ |
9419 |
+-- testing hexadecimal numerals |
9420 |
+ |
9421 |
+assert(0x10 == 16 and 0xfff == 2^12 - 1 and 0XFB == 251) |
9422 |
+assert(0x0p12 == 0 and 0x.0p-3 == 0) |
9423 |
+assert(0xFFFFFFFF == (1 << 32) - 1) |
9424 |
+assert(tonumber('+0x2') == 2) |
9425 |
+assert(tonumber('-0xaA') == -170) |
9426 |
+assert(tonumber('-0xffFFFfff') == -(1 << 32) + 1) |
9427 |
+ |
9428 |
+-- possible confusion with decimal exponent |
9429 |
+assert(0E+1 == 0 and 0xE+1 == 15 and 0xe-1 == 13) |
9430 |
+ |
9431 |
+ |
9432 |
+-- floating hexas |
9433 |
+ |
9434 |
+assert(tonumber(' 0x2.5 ') == 0x25/16) |
9435 |
+assert(tonumber(' -0x2.5 ') == -0x25/16) |
9436 |
+assert(tonumber(' +0x0.51p+8 ') == 0x51) |
9437 |
+assert(0x.FfffFFFF == 1 - '0x.00000001') |
9438 |
+assert('0xA.a' + 0 == 10 + 10/16) |
9439 |
+assert(0xa.aP4 == 0XAA) |
9440 |
+assert(0x4P-2 == 1) |
9441 |
+assert(0x1.1 == '0x1.' + '+0x.1') |
9442 |
+assert(0Xabcdef.0 == 0x.ABCDEFp+24) |
9443 |
+ |
9444 |
+ |
9445 |
+assert(1.1 == 1.+.1) |
9446 |
+assert(100.0 == 1E2 and .01 == 1e-2) |
9447 |
+assert(1111111111 - 1111111110 == 1000.00e-03) |
9448 |
+assert(1.1 == '1.'+'.1') |
9449 |
+assert(tonumber'1111111111' - tonumber'1111111110' == |
9450 |
+ tonumber" +0.001e+3 \n\t") |
9451 |
+ |
9452 |
+assert(0.1e-30 > 0.9E-31 and 0.9E30 < 0.1e31) |
9453 |
+ |
9454 |
+assert(0.123456 > 0.123455) |
9455 |
+ |
9456 |
+assert(tonumber('+1.23E18') == 1.23*10.0^18) |
9457 |
+ |
9458 |
+-- testing order operators |
9459 |
+assert(not(1<1) and (1<2) and not(2<1)) |
9460 |
+assert(not('a'<'a') and ('a'<'b') and not('b'<'a')) |
9461 |
+assert((1<=1) and (1<=2) and not(2<=1)) |
9462 |
+assert(('a'<='a') and ('a'<='b') and not('b'<='a')) |
9463 |
+assert(not(1>1) and not(1>2) and (2>1)) |
9464 |
+assert(not('a'>'a') and not('a'>'b') and ('b'>'a')) |
9465 |
+assert((1>=1) and not(1>=2) and (2>=1)) |
9466 |
+assert(('a'>='a') and not('a'>='b') and ('b'>='a')) |
9467 |
+assert(1.3 < 1.4 and 1.3 <= 1.4 and not (1.3 < 1.3) and 1.3 <= 1.3) |
9468 |
+ |
9469 |
+-- testing mod operator |
9470 |
+assert(eqT(-4 % 3, 2)) |
9471 |
+assert(eqT(4 % -3, -2)) |
9472 |
+assert(eqT(-4.0 % 3, 2.0)) |
9473 |
+assert(eqT(4 % -3.0, -2.0)) |
9474 |
+assert(math.pi - math.pi % 1 == 3) |
9475 |
+assert(math.pi - math.pi % 0.001 == 3.141) |
9476 |
+ |
9477 |
+assert(eqT(minint % minint, 0)) |
9478 |
+assert(eqT(maxint % maxint, 0)) |
9479 |
+assert((minint + 1) % minint == minint + 1) |
9480 |
+assert((maxint - 1) % maxint == maxint - 1) |
9481 |
+assert(minint % maxint == maxint - 1) |
9482 |
+ |
9483 |
+assert(minint % -1 == 0) |
9484 |
+assert(minint % -2 == 0) |
9485 |
+assert(maxint % -2 == -1) |
9486 |
+ |
9487 |
+-- non-portable tests because Windows C library cannot compute |
9488 |
+-- fmod(1, huge) correctly |
9489 |
+if not _port then |
9490 |
+ local function anan (x) assert(isNaN(x)) end -- assert Not a Number |
9491 |
+ anan(0.0 % 0) |
9492 |
+ anan(1.3 % 0) |
9493 |
+ anan(math.huge % 1) |
9494 |
+ anan(math.huge % 1e30) |
9495 |
+ anan(-math.huge % 1e30) |
9496 |
+ anan(-math.huge % -1e30) |
9497 |
+ assert(1 % math.huge == 1) |
9498 |
+ assert(1e30 % math.huge == 1e30) |
9499 |
+ assert(1e30 % -math.huge == -math.huge) |
9500 |
+ assert(-1 % math.huge == math.huge) |
9501 |
+ assert(-1 % -math.huge == -1) |
9502 |
+end |
9503 |
+ |
9504 |
+ |
9505 |
+-- testing unsigned comparisons |
9506 |
+assert(math.ult(3, 4)) |
9507 |
+assert(not math.ult(4, 4)) |
9508 |
+assert(math.ult(-2, -1)) |
9509 |
+assert(math.ult(2, -1)) |
9510 |
+assert(not math.ult(-2, -2)) |
9511 |
+assert(math.ult(maxint, minint)) |
9512 |
+assert(not math.ult(minint, maxint)) |
9513 |
+ |
9514 |
+ |
9515 |
+assert(eq(math.sin(-9.8)^2 + math.cos(-9.8)^2, 1)) |
9516 |
+assert(eq(math.tan(math.pi/4), 1)) |
9517 |
+assert(eq(math.sin(math.pi/2), 1) and eq(math.cos(math.pi/2), 0)) |
9518 |
+assert(eq(math.atan(1), math.pi/4) and eq(math.acos(0), math.pi/2) and |
9519 |
+ eq(math.asin(1), math.pi/2)) |
9520 |
+assert(eq(math.deg(math.pi/2), 90) and eq(math.rad(90), math.pi/2)) |
9521 |
+assert(math.abs(-10.43) == 10.43) |
9522 |
+assert(eqT(math.abs(minint), minint)) |
9523 |
+assert(eqT(math.abs(maxint), maxint)) |
9524 |
+assert(eqT(math.abs(-maxint), maxint)) |
9525 |
+assert(eq(math.atan(1,0), math.pi/2)) |
9526 |
+assert(math.fmod(10,3) == 1) |
9527 |
+assert(eq(math.sqrt(10)^2, 10)) |
9528 |
+assert(eq(math.log(2, 10), math.log(2)/math.log(10))) |
9529 |
+assert(eq(math.log(2, 2), 1)) |
9530 |
+assert(eq(math.log(9, 3), 2)) |
9531 |
+assert(eq(math.exp(0), 1)) |
9532 |
+assert(eq(math.sin(10), math.sin(10%(2*math.pi)))) |
9533 |
+ |
9534 |
+ |
9535 |
+assert(tonumber(' 1.3e-2 ') == 1.3e-2) |
9536 |
+assert(tonumber(' -1.00000000000001 ') == -1.00000000000001) |
9537 |
+ |
9538 |
+-- testing constant limits |
9539 |
+-- 2^23 = 8388608 |
9540 |
+assert(8388609 + -8388609 == 0) |
9541 |
+assert(8388608 + -8388608 == 0) |
9542 |
+assert(8388607 + -8388607 == 0) |
9543 |
+ |
9544 |
+ |
9545 |
+ |
9546 |
+do -- testing floor & ceil |
9547 |
+ assert(eqT(math.floor(3.4), 3)) |
9548 |
+ assert(eqT(math.ceil(3.4), 4)) |
9549 |
+ assert(eqT(math.floor(-3.4), -4)) |
9550 |
+ assert(eqT(math.ceil(-3.4), -3)) |
9551 |
+ assert(eqT(math.floor(maxint), maxint)) |
9552 |
+ assert(eqT(math.ceil(maxint), maxint)) |
9553 |
+ assert(eqT(math.floor(minint), minint)) |
9554 |
+ assert(eqT(math.floor(minint + 0.0), minint)) |
9555 |
+ assert(eqT(math.ceil(minint), minint)) |
9556 |
+ assert(eqT(math.ceil(minint + 0.0), minint)) |
9557 |
+ assert(math.floor(1e50) == 1e50) |
9558 |
+ assert(math.ceil(1e50) == 1e50) |
9559 |
+ assert(math.floor(-1e50) == -1e50) |
9560 |
+ assert(math.ceil(-1e50) == -1e50) |
9561 |
+ for _, p in pairs{31,32,63,64} do |
9562 |
+ assert(math.floor(2^p) == 2^p) |
9563 |
+ assert(math.floor(2^p + 0.5) == 2^p) |
9564 |
+ assert(math.ceil(2^p) == 2^p) |
9565 |
+ assert(math.ceil(2^p - 0.5) == 2^p) |
9566 |
+ end |
9567 |
+ checkerror("number expected", math.floor, {}) |
9568 |
+ checkerror("number expected", math.ceil, print) |
9569 |
+ assert(eqT(math.tointeger(minint), minint)) |
9570 |
+ assert(eqT(math.tointeger(minint .. ""), minint)) |
9571 |
+ assert(eqT(math.tointeger(maxint), maxint)) |
9572 |
+ assert(eqT(math.tointeger(maxint .. ""), maxint)) |
9573 |
+ assert(eqT(math.tointeger(minint + 0.0), minint)) |
9574 |
+ assert(math.tointeger(0.0 - minint) == nil) |
9575 |
+ assert(math.tointeger(math.pi) == nil) |
9576 |
+ assert(math.tointeger(-math.pi) == nil) |
9577 |
+ assert(math.floor(math.huge) == math.huge) |
9578 |
+ assert(math.ceil(math.huge) == math.huge) |
9579 |
+ assert(math.tointeger(math.huge) == nil) |
9580 |
+ assert(math.floor(-math.huge) == -math.huge) |
9581 |
+ assert(math.ceil(-math.huge) == -math.huge) |
9582 |
+ assert(math.tointeger(-math.huge) == nil) |
9583 |
+ assert(math.tointeger("34.0") == 34) |
9584 |
+ assert(math.tointeger("34.3") == nil) |
9585 |
+ assert(math.tointeger({}) == nil) |
9586 |
+ assert(math.tointeger(0/0) == nil) -- NaN |
9587 |
+end |
9588 |
+ |
9589 |
+ |
9590 |
+-- testing fmod for integers |
9591 |
+for i = -6, 6 do |
9592 |
+ for j = -6, 6 do |
9593 |
+ if j ~= 0 then |
9594 |
+ local mi = math.fmod(i, j) |
9595 |
+ local mf = math.fmod(i + 0.0, j) |
9596 |
+ assert(mi == mf) |
9597 |
+ assert(math.type(mi) == 'integer' and math.type(mf) == 'float') |
9598 |
+ if (i >= 0 and j >= 0) or (i <= 0 and j <= 0) or mi == 0 then |
9599 |
+ assert(eqT(mi, i % j)) |
9600 |
+ end |
9601 |
+ end |
9602 |
+ end |
9603 |
+end |
9604 |
+assert(eqT(math.fmod(minint, minint), 0)) |
9605 |
+assert(eqT(math.fmod(maxint, maxint), 0)) |
9606 |
+assert(eqT(math.fmod(minint + 1, minint), minint + 1)) |
9607 |
+assert(eqT(math.fmod(maxint - 1, maxint), maxint - 1)) |
9608 |
+ |
9609 |
+checkerror("zero", math.fmod, 3, 0) |
9610 |
+ |
9611 |
+ |
9612 |
+do -- testing max/min |
9613 |
+ checkerror("value expected", math.max) |
9614 |
+ checkerror("value expected", math.min) |
9615 |
+ assert(eqT(math.max(3), 3)) |
9616 |
+ assert(eqT(math.max(3, 5, 9, 1), 9)) |
9617 |
+ assert(math.max(maxint, 10e60) == 10e60) |
9618 |
+ assert(eqT(math.max(minint, minint + 1), minint + 1)) |
9619 |
+ assert(eqT(math.min(3), 3)) |
9620 |
+ assert(eqT(math.min(3, 5, 9, 1), 1)) |
9621 |
+ assert(math.min(3.2, 5.9, -9.2, 1.1) == -9.2) |
9622 |
+ assert(math.min(1.9, 1.7, 1.72) == 1.7) |
9623 |
+ assert(math.min(-10e60, minint) == -10e60) |
9624 |
+ assert(eqT(math.min(maxint, maxint - 1), maxint - 1)) |
9625 |
+ assert(eqT(math.min(maxint - 2, maxint, maxint - 1), maxint - 2)) |
9626 |
+end |
9627 |
+-- testing implicit convertions |
9628 |
+ |
9629 |
+local a,b = '10', '20' |
9630 |
+assert(a*b == 200 and a+b == 30 and a-b == -10 and a/b == 0.5 and -b == -20) |
9631 |
+assert(a == '10' and b == '20') |
9632 |
+ |
9633 |
+ |
9634 |
+do |
9635 |
+ print("testing -0 and NaN") |
9636 |
+ local mz, z = -0.0, 0.0 |
9637 |
+ assert(mz == z) |
9638 |
+ assert(1/mz < 0 and 0 < 1/z) |
9639 |
+ local a = {[mz] = 1} |
9640 |
+ assert(a[z] == 1 and a[mz] == 1) |
9641 |
+ a[z] = 2 |
9642 |
+ assert(a[z] == 2 and a[mz] == 2) |
9643 |
+ local inf = math.huge * 2 + 1 |
9644 |
+ mz, z = -1/inf, 1/inf |
9645 |
+ assert(mz == z) |
9646 |
+ assert(1/mz < 0 and 0 < 1/z) |
9647 |
+ local NaN = inf - inf |
9648 |
+ assert(NaN ~= NaN) |
9649 |
+ assert(not (NaN < NaN)) |
9650 |
+ assert(not (NaN <= NaN)) |
9651 |
+ assert(not (NaN > NaN)) |
9652 |
+ assert(not (NaN >= NaN)) |
9653 |
+ assert(not (0 < NaN) and not (NaN < 0)) |
9654 |
+ local NaN1 = 0/0 |
9655 |
+ assert(NaN ~= NaN1 and not (NaN <= NaN1) and not (NaN1 <= NaN)) |
9656 |
+ local a = {} |
9657 |
+ assert(not pcall(rawset, a, NaN, 1)) |
9658 |
+ assert(a[NaN] == nil) |
9659 |
+ a[1] = 1 |
9660 |
+ assert(not pcall(rawset, a, NaN, 1)) |
9661 |
+ assert(a[NaN] == nil) |
9662 |
+ -- strings with same binary representation as 0.0 (might create problems |
9663 |
+ -- for constant manipulation in the pre-compiler) |
9664 |
+ local a1, a2, a3, a4, a5 = 0, 0, "\0\0\0\0\0\0\0\0", 0, "\0\0\0\0\0\0\0\0" |
9665 |
+ assert(a1 == a2 and a2 == a4 and a1 ~= a3) |
9666 |
+ assert(a3 == a5) |
9667 |
+end |
9668 |
+ |
9669 |
+ |
9670 |
+print("testing 'math.random'") |
9671 |
+math.randomseed(0) |
9672 |
+ |
9673 |
+do -- test random for floats |
9674 |
+ local max = -math.huge |
9675 |
+ local min = math.huge |
9676 |
+ for i = 0, 20000 do |
9677 |
+ local t = math.random() |
9678 |
+ assert(0 <= t and t < 1) |
9679 |
+ max = math.max(max, t) |
9680 |
+ min = math.min(min, t) |
9681 |
+ if eq(max, 1, 0.001) and eq(min, 0, 0.001) then |
9682 |
+ goto ok |
9683 |
+ end |
9684 |
+ end |
9685 |
+ -- loop ended without satisfing condition |
9686 |
+ assert(false) |
9687 |
+ ::ok:: |
9688 |
+end |
9689 |
+ |
9690 |
+do |
9691 |
+ local function aux (p, lim) -- test random for small intervals |
9692 |
+ local x1, x2 |
9693 |
+ if #p == 1 then x1 = 1; x2 = p[1] |
9694 |
+ else x1 = p[1]; x2 = p[2] |
9695 |
+ end |
9696 |
+ local mark = {}; local count = 0 -- to check that all values appeared |
9697 |
+ for i = 0, lim or 2000 do |
9698 |
+ local t = math.random(table.unpack(p)) |
9699 |
+ assert(x1 <= t and t <= x2) |
9700 |
+ if not mark[t] then -- new value |
9701 |
+ mark[t] = true |
9702 |
+ count = count + 1 |
9703 |
+ end |
9704 |
+ if count == x2 - x1 + 1 then -- all values appeared; OK |
9705 |
+ goto ok |
9706 |
+ end |
9707 |
+ end |
9708 |
+ -- loop ended without satisfing condition |
9709 |
+ assert(false) |
9710 |
+ ::ok:: |
9711 |
+ end |
9712 |
+ |
9713 |
+ aux({-10,0}) |
9714 |
+ aux({6}) |
9715 |
+ aux({-10, 10}) |
9716 |
+ aux({minint, minint}) |
9717 |
+ aux({maxint, maxint}) |
9718 |
+ aux({minint, minint + 9}) |
9719 |
+ aux({maxint - 3, maxint}) |
9720 |
+end |
9721 |
+ |
9722 |
+do |
9723 |
+ local function aux(p1, p2) -- test random for large intervals |
9724 |
+ local max = minint |
9725 |
+ local min = maxint |
9726 |
+ local n = 200 |
9727 |
+ local mark = {}; local count = 0 -- to count how many different values |
9728 |
+ for _ = 1, n do |
9729 |
+ local t = math.random(p1, p2) |
9730 |
+ max = math.max(max, t) |
9731 |
+ min = math.min(min, t) |
9732 |
+ if not mark[t] then -- new value |
9733 |
+ mark[t] = true |
9734 |
+ count = count + 1 |
9735 |
+ end |
9736 |
+ end |
9737 |
+ -- at least 80% of values are different |
9738 |
+ assert(count >= n * 0.8) |
9739 |
+ -- min and max not too far from formal min and max |
9740 |
+ local diff = (p2 - p1) // 8 |
9741 |
+ assert(min < p1 + diff and max > p2 - diff) |
9742 |
+ end |
9743 |
+ aux(0, maxint) |
9744 |
+ aux(1, maxint) |
9745 |
+ aux(minint, -1) |
9746 |
+ aux(minint // 2, maxint // 2) |
9747 |
+end |
9748 |
+ |
9749 |
+for i=1,100 do |
9750 |
+ assert(math.random(maxint) > 0) |
9751 |
+ assert(math.random(minint, -1) < 0) |
9752 |
+end |
9753 |
+ |
9754 |
+assert(not pcall(math.random, 1, 2, 3)) -- too many arguments |
9755 |
+ |
9756 |
+-- empty interval |
9757 |
+assert(not pcall(math.random, minint + 1, minint)) |
9758 |
+assert(not pcall(math.random, maxint, maxint - 1)) |
9759 |
+assert(not pcall(math.random, maxint, minint)) |
9760 |
+ |
9761 |
+-- interval too large |
9762 |
+assert(not pcall(math.random, minint, 0)) |
9763 |
+assert(not pcall(math.random, -1, maxint)) |
9764 |
+assert(not pcall(math.random, minint // 2, maxint // 2 + 1)) |
9765 |
+ |
9766 |
+ |
9767 |
+print('OK') |
9768 |
|
9769 |
diff --git a/tests/nextvar.lua b/tests/nextvar.lua |
9770 |
new file mode 100644 |
9771 |
index 0000000..ce7312e |
9772 |
--- /dev/null |
9773 |
+++ b/tests/nextvar.lua |
9774 |
@@ -0,0 +1,631 @@ |
9775 |
+-- $Id: nextvar.lua,v 1.79 2016/11/07 13:11:28 roberto Exp $ |
9776 |
+-- See Copyright Notice in file all.lua |
9777 |
+ |
9778 |
+print('testing tables, next, and for') |
9779 |
+ |
9780 |
+local function checkerror (msg, f, ...) |
9781 |
+ local s, err = pcall(f, ...) |
9782 |
+ assert(not s and string.find(err, msg)) |
9783 |
+end |
9784 |
+ |
9785 |
+ |
9786 |
+local a = {} |
9787 |
+ |
9788 |
+-- make sure table has lots of space in hash part |
9789 |
+for i=1,100 do a[i.."+"] = true end |
9790 |
+for i=1,100 do a[i.."+"] = nil end |
9791 |
+-- fill hash part with numeric indices testing size operator |
9792 |
+for i=1,100 do |
9793 |
+ a[i] = true |
9794 |
+ assert(#a == i) |
9795 |
+end |
9796 |
+ |
9797 |
+-- testing ipairs |
9798 |
+local x = 0 |
9799 |
+for k,v in ipairs{10,20,30;x=12} do |
9800 |
+ x = x + 1 |
9801 |
+ assert(k == x and v == x * 10) |
9802 |
+end |
9803 |
+ |
9804 |
+for _ in ipairs{x=12, y=24} do assert(nil) end |
9805 |
+ |
9806 |
+-- test for 'false' x ipair |
9807 |
+x = false |
9808 |
+local i = 0 |
9809 |
+for k,v in ipairs{true,false,true,false} do |
9810 |
+ i = i + 1 |
9811 |
+ x = not x |
9812 |
+ assert(x == v) |
9813 |
+end |
9814 |
+assert(i == 4) |
9815 |
+ |
9816 |
+-- iterator function is always the same |
9817 |
+assert(type(ipairs{}) == 'function' and ipairs{} == ipairs{}) |
9818 |
+ |
9819 |
+ |
9820 |
+if not T then |
9821 |
+ (Message or print) |
9822 |
+ ('\n >>> testC not active: skipping tests for table sizes <<<\n') |
9823 |
+else --[ |
9824 |
+-- testing table sizes |
9825 |
+ |
9826 |
+local function log2 (x) return math.log(x, 2) end |
9827 |
+ |
9828 |
+local function mp2 (n) -- minimum power of 2 >= n |
9829 |
+ local mp = 2^math.ceil(log2(n)) |
9830 |
+ assert(n == 0 or (mp/2 < n and n <= mp)) |
9831 |
+ return mp |
9832 |
+end |
9833 |
+ |
9834 |
+local function fb (n) |
9835 |
+ local r, nn = T.int2fb(n) |
9836 |
+ assert(r < 256) |
9837 |
+ return nn |
9838 |
+end |
9839 |
+ |
9840 |
+-- test fb function |
9841 |
+for a = 1, 10000 do -- all numbers up to 10^4 |
9842 |
+ local n = fb(a) |
9843 |
+ assert(a <= n and n <= a*1.125) |
9844 |
+end |
9845 |
+local a = 1024 -- plus a few up to 2 ^30 |
9846 |
+local lim = 2^30 |
9847 |
+while a < lim do |
9848 |
+ local n = fb(a) |
9849 |
+ assert(a <= n and n <= a*1.125) |
9850 |
+ a = math.ceil(a*1.3) |
9851 |
+end |
9852 |
+ |
9853 |
+ |
9854 |
+local function check (t, na, nh) |
9855 |
+ local a, h = T.querytab(t) |
9856 |
+ if a ~= na or h ~= nh then |
9857 |
+ print(na, nh, a, h) |
9858 |
+ assert(nil) |
9859 |
+ end |
9860 |
+end |
9861 |
+ |
9862 |
+ |
9863 |
+-- testing C library sizes |
9864 |
+do |
9865 |
+ local s = 0 |
9866 |
+ for _ in pairs(math) do s = s + 1 end |
9867 |
+ check(math, 0, mp2(s)) |
9868 |
+end |
9869 |
+ |
9870 |
+ |
9871 |
+-- testing constructor sizes |
9872 |
+local lim = 40 |
9873 |
+local s = 'return {' |
9874 |
+for i=1,lim do |
9875 |
+ s = s..i..',' |
9876 |
+ local s = s |
9877 |
+ for k=0,lim do |
9878 |
+ local t = load(s..'}', '')() |
9879 |
+ assert(#t == i) |
9880 |
+ check(t, fb(i), mp2(k)) |
9881 |
+ s = string.format('%sa%d=%d,', s, k, k) |
9882 |
+ end |
9883 |
+end |
9884 |
+ |
9885 |
+ |
9886 |
+-- tests with unknown number of elements |
9887 |
+local a = {} |
9888 |
+for i=1,lim do a[i] = i end -- build auxiliary table |
9889 |
+for k=0,lim do |
9890 |
+ local a = {table.unpack(a,1,k)} |
9891 |
+ assert(#a == k) |
9892 |
+ check(a, k, 0) |
9893 |
+ a = {1,2,3,table.unpack(a,1,k)} |
9894 |
+ check(a, k+3, 0) |
9895 |
+ assert(#a == k + 3) |
9896 |
+end |
9897 |
+ |
9898 |
+ |
9899 |
+-- testing tables dynamically built |
9900 |
+local lim = 130 |
9901 |
+local a = {}; a[2] = 1; check(a, 0, 1) |
9902 |
+a = {}; a[0] = 1; check(a, 0, 1); a[2] = 1; check(a, 0, 2) |
9903 |
+a = {}; a[0] = 1; a[1] = 1; check(a, 1, 1) |
9904 |
+a = {} |
9905 |
+for i = 1,lim do |
9906 |
+ a[i] = 1 |
9907 |
+ assert(#a == i) |
9908 |
+ check(a, mp2(i), 0) |
9909 |
+end |
9910 |
+ |
9911 |
+a = {} |
9912 |
+for i = 1,lim do |
9913 |
+ a['a'..i] = 1 |
9914 |
+ assert(#a == 0) |
9915 |
+ check(a, 0, mp2(i)) |
9916 |
+end |
9917 |
+ |
9918 |
+a = {} |
9919 |
+for i=1,16 do a[i] = i end |
9920 |
+check(a, 16, 0) |
9921 |
+do |
9922 |
+ for i=1,11 do a[i] = nil end |
9923 |
+ for i=30,50 do a[i] = nil end -- force a rehash (?) |
9924 |
+ check(a, 0, 8) -- only 5 elements in the table |
9925 |
+ a[10] = 1 |
9926 |
+ for i=30,50 do a[i] = nil end -- force a rehash (?) |
9927 |
+ check(a, 0, 8) -- only 6 elements in the table |
9928 |
+ for i=1,14 do a[i] = nil end |
9929 |
+ for i=18,50 do a[i] = nil end -- force a rehash (?) |
9930 |
+ check(a, 0, 4) -- only 2 elements ([15] and [16]) |
9931 |
+end |
9932 |
+ |
9933 |
+-- reverse filling |
9934 |
+for i=1,lim do |
9935 |
+ local a = {} |
9936 |
+ for i=i,1,-1 do a[i] = i end -- fill in reverse |
9937 |
+ check(a, mp2(i), 0) |
9938 |
+end |
9939 |
+ |
9940 |
+-- size tests for vararg |
9941 |
+lim = 35 |
9942 |
+function foo (n, ...) |
9943 |
+ local arg = {...} |
9944 |
+ check(arg, n, 0) |
9945 |
+ assert(select('#', ...) == n) |
9946 |
+ arg[n+1] = true |
9947 |
+ check(arg, mp2(n+1), 0) |
9948 |
+ arg.x = true |
9949 |
+ check(arg, mp2(n+1), 1) |
9950 |
+end |
9951 |
+local a = {} |
9952 |
+for i=1,lim do a[i] = true; foo(i, table.unpack(a)) end |
9953 |
+ |
9954 |
+end --] |
9955 |
+ |
9956 |
+ |
9957 |
+-- test size operation on empty tables |
9958 |
+assert(#{} == 0) |
9959 |
+assert(#{nil} == 0) |
9960 |
+assert(#{nil, nil} == 0) |
9961 |
+assert(#{nil, nil, nil} == 0) |
9962 |
+assert(#{nil, nil, nil, nil} == 0) |
9963 |
+print'+' |
9964 |
+ |
9965 |
+ |
9966 |
+local nofind = {} |
9967 |
+ |
9968 |
+a,b,c = 1,2,3 |
9969 |
+a,b,c = nil |
9970 |
+ |
9971 |
+ |
9972 |
+-- next uses always the same iteraction function |
9973 |
+assert(next{} == next{}) |
9974 |
+ |
9975 |
+local function find (name) |
9976 |
+ local n,v |
9977 |
+ while 1 do |
9978 |
+ n,v = next(_G, n) |
9979 |
+ if not n then return nofind end |
9980 |
+ assert(v ~= nil) |
9981 |
+ if n == name then return v end |
9982 |
+ end |
9983 |
+end |
9984 |
+ |
9985 |
+local function find1 (name) |
9986 |
+ for n,v in pairs(_G) do |
9987 |
+ if n==name then return v end |
9988 |
+ end |
9989 |
+ return nil -- not found |
9990 |
+end |
9991 |
+ |
9992 |
+ |
9993 |
+assert(print==find("print") and print == find1("print")) |
9994 |
+assert(_G["print"]==find("print")) |
9995 |
+assert(assert==find1("assert")) |
9996 |
+assert(nofind==find("return")) |
9997 |
+assert(not find1("return")) |
9998 |
+_G["ret" .. "urn"] = nil |
9999 |
+assert(nofind==find("return")) |
10000 |
+_G["xxx"] = 1 |
10001 |
+assert(xxx==find("xxx")) |
10002 |
+ |
10003 |
+-- invalid key to 'next' |
10004 |
+checkerror("invalid key", next, {10,20}, 3) |
10005 |
+ |
10006 |
+-- both 'pairs' and 'ipairs' need an argument |
10007 |
+checkerror("bad argument", pairs) |
10008 |
+checkerror("bad argument", ipairs) |
10009 |
+ |
10010 |
+print('+') |
10011 |
+ |
10012 |
+a = {} |
10013 |
+for i=0,10000 do |
10014 |
+ if math.fmod(i,10) ~= 0 then |
10015 |
+ a['x'..i] = i |
10016 |
+ end |
10017 |
+end |
10018 |
+ |
10019 |
+n = {n=0} |
10020 |
+for i,v in pairs(a) do |
10021 |
+ n.n = n.n+1 |
10022 |
+ assert(i and v and a[i] == v) |
10023 |
+end |
10024 |
+assert(n.n == 9000) |
10025 |
+a = nil |
10026 |
+ |
10027 |
+do -- clear global table |
10028 |
+ local a = {} |
10029 |
+ for n,v in pairs(_G) do a[n]=v end |
10030 |
+ for n,v in pairs(a) do |
10031 |
+ if not package.loaded[n] and type(v) ~= "function" and |
10032 |
+ not string.find(n, "^[%u_]") then |
10033 |
+ _G[n] = nil |
10034 |
+ end |
10035 |
+ collectgarbage() |
10036 |
+ end |
10037 |
+end |
10038 |
+ |
10039 |
+ |
10040 |
+-- |
10041 |
+ |
10042 |
+local function checknext (a) |
10043 |
+ local b = {} |
10044 |
+ do local k,v = next(a); while k do b[k] = v; k,v = next(a,k) end end |
10045 |
+ for k,v in pairs(b) do assert(a[k] == v) end |
10046 |
+ for k,v in pairs(a) do assert(b[k] == v) end |
10047 |
+end |
10048 |
+ |
10049 |
+checknext{1,x=1,y=2,z=3} |
10050 |
+checknext{1,2,x=1,y=2,z=3} |
10051 |
+checknext{1,2,3,x=1,y=2,z=3} |
10052 |
+checknext{1,2,3,4,x=1,y=2,z=3} |
10053 |
+checknext{1,2,3,4,5,x=1,y=2,z=3} |
10054 |
+ |
10055 |
+assert(#{} == 0) |
10056 |
+assert(#{[-1] = 2} == 0) |
10057 |
+assert(#{1,2,3,nil,nil} == 3) |
10058 |
+for i=0,40 do |
10059 |
+ local a = {} |
10060 |
+ for j=1,i do a[j]=j end |
10061 |
+ assert(#a == i) |
10062 |
+end |
10063 |
+ |
10064 |
+-- 'maxn' is now deprecated, but it is easily defined in Lua |
10065 |
+function table.maxn (t) |
10066 |
+ local max = 0 |
10067 |
+ for k in pairs(t) do |
10068 |
+ max = (type(k) == 'number') and math.max(max, k) or max |
10069 |
+ end |
10070 |
+ return max |
10071 |
+end |
10072 |
+ |
10073 |
+assert(table.maxn{} == 0) |
10074 |
+assert(table.maxn{["1000"] = true} == 0) |
10075 |
+assert(table.maxn{["1000"] = true, [24.5] = 3} == 24.5) |
10076 |
+assert(table.maxn{[1000] = true} == 1000) |
10077 |
+assert(table.maxn{[10] = true, [100*math.pi] = print} == 100*math.pi) |
10078 |
+ |
10079 |
+table.maxn = nil |
10080 |
+ |
10081 |
+-- int overflow |
10082 |
+a = {} |
10083 |
+for i=0,50 do a[2^i] = true end |
10084 |
+assert(a[#a]) |
10085 |
+ |
10086 |
+print('+') |
10087 |
+ |
10088 |
+ |
10089 |
+-- erasing values |
10090 |
+local t = {[{1}] = 1, [{2}] = 2, [string.rep("x ", 4)] = 3, |
10091 |
+ [100.3] = 4, [4] = 5} |
10092 |
+ |
10093 |
+local n = 0 |
10094 |
+for k, v in pairs( t ) do |
10095 |
+ n = n+1 |
10096 |
+ assert(t[k] == v) |
10097 |
+ t[k] = nil |
10098 |
+ collectgarbage() |
10099 |
+ assert(t[k] == nil) |
10100 |
+end |
10101 |
+assert(n == 5) |
10102 |
+ |
10103 |
+ |
10104 |
+local function test (a) |
10105 |
+ assert(not pcall(table.insert, a, 2, 20)); |
10106 |
+ table.insert(a, 10); table.insert(a, 2, 20); |
10107 |
+ table.insert(a, 1, -1); table.insert(a, 40); |
10108 |
+ table.insert(a, #a+1, 50) |
10109 |
+ table.insert(a, 2, -2) |
10110 |
+ assert(not pcall(table.insert, a, 0, 20)); |
10111 |
+ assert(not pcall(table.insert, a, #a + 2, 20)); |
10112 |
+ assert(table.remove(a,1) == -1) |
10113 |
+ assert(table.remove(a,1) == -2) |
10114 |
+ assert(table.remove(a,1) == 10) |
10115 |
+ assert(table.remove(a,1) == 20) |
10116 |
+ assert(table.remove(a,1) == 40) |
10117 |
+ assert(table.remove(a,1) == 50) |
10118 |
+ assert(table.remove(a,1) == nil) |
10119 |
+ assert(table.remove(a) == nil) |
10120 |
+ assert(table.remove(a, #a) == nil) |
10121 |
+end |
10122 |
+ |
10123 |
+a = {n=0, [-7] = "ban"} |
10124 |
+test(a) |
10125 |
+assert(a.n == 0 and a[-7] == "ban") |
10126 |
+ |
10127 |
+a = {[-7] = "ban"}; |
10128 |
+test(a) |
10129 |
+assert(a.n == nil and #a == 0 and a[-7] == "ban") |
10130 |
+ |
10131 |
+a = {[-1] = "ban"} |
10132 |
+test(a) |
10133 |
+assert(#a == 0 and table.remove(a) == nil and a[-1] == "ban") |
10134 |
+ |
10135 |
+a = {[0] = "ban"} |
10136 |
+assert(#a == 0 and table.remove(a) == "ban" and a[0] == nil) |
10137 |
+ |
10138 |
+table.insert(a, 1, 10); table.insert(a, 1, 20); table.insert(a, 1, -1) |
10139 |
+assert(table.remove(a) == 10) |
10140 |
+assert(table.remove(a) == 20) |
10141 |
+assert(table.remove(a) == -1) |
10142 |
+assert(table.remove(a) == nil) |
10143 |
+ |
10144 |
+a = {'c', 'd'} |
10145 |
+table.insert(a, 3, 'a') |
10146 |
+table.insert(a, 'b') |
10147 |
+assert(table.remove(a, 1) == 'c') |
10148 |
+assert(table.remove(a, 1) == 'd') |
10149 |
+assert(table.remove(a, 1) == 'a') |
10150 |
+assert(table.remove(a, 1) == 'b') |
10151 |
+assert(table.remove(a, 1) == nil) |
10152 |
+assert(#a == 0 and a.n == nil) |
10153 |
+ |
10154 |
+a = {10,20,30,40} |
10155 |
+assert(table.remove(a, #a + 1) == nil) |
10156 |
+assert(not pcall(table.remove, a, 0)) |
10157 |
+assert(a[#a] == 40) |
10158 |
+assert(table.remove(a, #a) == 40) |
10159 |
+assert(a[#a] == 30) |
10160 |
+assert(table.remove(a, 2) == 20) |
10161 |
+assert(a[#a] == 30 and #a == 2) |
10162 |
+ |
10163 |
+do -- testing table library with metamethods |
10164 |
+ local function test (proxy, t) |
10165 |
+ for i = 1, 10 do |
10166 |
+ table.insert(proxy, 1, i) |
10167 |
+ end |
10168 |
+ assert(#proxy == 10 and #t == 10) |
10169 |
+ for i = 1, 10 do |
10170 |
+ assert(t[i] == 11 - i) |
10171 |
+ end |
10172 |
+ table.sort(proxy) |
10173 |
+ for i = 1, 10 do |
10174 |
+ assert(t[i] == i and proxy[i] == i) |
10175 |
+ end |
10176 |
+ assert(table.concat(proxy, ",") == "1,2,3,4,5,6,7,8,9,10") |
10177 |
+ for i = 1, 8 do |
10178 |
+ assert(table.remove(proxy, 1) == i) |
10179 |
+ end |
10180 |
+ assert(#proxy == 2 and #t == 2) |
10181 |
+ local a, b, c = table.unpack(proxy) |
10182 |
+ assert(a == 9 and b == 10 and c == nil) |
10183 |
+ end |
10184 |
+ |
10185 |
+ -- all virtual |
10186 |
+ local t = {} |
10187 |
+ local proxy = setmetatable({}, { |
10188 |
+ __len = function () return #t end, |
10189 |
+ __index = t, |
10190 |
+ __newindex = t, |
10191 |
+ }) |
10192 |
+ test(proxy, t) |
10193 |
+ |
10194 |
+ -- only __newindex |
10195 |
+ local count = 0 |
10196 |
+ t = setmetatable({}, { |
10197 |
+ __newindex = function (t,k,v) count = count + 1; rawset(t,k,v) end}) |
10198 |
+ test(t, t) |
10199 |
+ assert(count == 10) -- after first 10, all other sets are not new |
10200 |
+ |
10201 |
+ -- no __newindex |
10202 |
+ t = setmetatable({}, { |
10203 |
+ __index = function (_,k) return k + 1 end, |
10204 |
+ __len = function (_) return 5 end}) |
10205 |
+ assert(table.concat(t, ";") == "2;3;4;5;6") |
10206 |
+ |
10207 |
+end |
10208 |
+ |
10209 |
+ |
10210 |
+if not T then |
10211 |
+ (Message or print) |
10212 |
+ ('\n >>> testC not active: skipping tests for table library on non-tables <<<\n') |
10213 |
+else --[ |
10214 |
+ local debug = require'debug' |
10215 |
+ local tab = {10, 20, 30} |
10216 |
+ local mt = {} |
10217 |
+ local u = T.newuserdata(0) |
10218 |
+ checkerror("table expected", table.insert, u, 40) |
10219 |
+ checkerror("table expected", table.remove, u) |
10220 |
+ debug.setmetatable(u, mt) |
10221 |
+ checkerror("table expected", table.insert, u, 40) |
10222 |
+ checkerror("table expected", table.remove, u) |
10223 |
+ mt.__index = tab |
10224 |
+ checkerror("table expected", table.insert, u, 40) |
10225 |
+ checkerror("table expected", table.remove, u) |
10226 |
+ mt.__newindex = tab |
10227 |
+ checkerror("table expected", table.insert, u, 40) |
10228 |
+ checkerror("table expected", table.remove, u) |
10229 |
+ mt.__len = function () return #tab end |
10230 |
+ table.insert(u, 40) |
10231 |
+ assert(#u == 4 and #tab == 4 and u[4] == 40 and tab[4] == 40) |
10232 |
+ assert(table.remove(u) == 40) |
10233 |
+ table.insert(u, 1, 50) |
10234 |
+ assert(#u == 4 and #tab == 4 and u[4] == 30 and tab[1] == 50) |
10235 |
+ |
10236 |
+ mt.__newindex = nil |
10237 |
+ mt.__len = nil |
10238 |
+ local tab2 = {} |
10239 |
+ local u2 = T.newuserdata(0) |
10240 |
+ debug.setmetatable(u2, {__newindex = function (_, k, v) tab2[k] = v end}) |
10241 |
+ table.move(u, 1, 4, 1, u2) |
10242 |
+ assert(#tab2 == 4 and tab2[1] == tab[1] and tab2[4] == tab[4]) |
10243 |
+ |
10244 |
+end -- ] |
10245 |
+ |
10246 |
+print('+') |
10247 |
+ |
10248 |
+a = {} |
10249 |
+for i=1,1000 do |
10250 |
+ a[i] = i; a[i-1] = nil |
10251 |
+end |
10252 |
+assert(next(a,nil) == 1000 and next(a,1000) == nil) |
10253 |
+ |
10254 |
+assert(next({}) == nil) |
10255 |
+assert(next({}, nil) == nil) |
10256 |
+ |
10257 |
+for a,b in pairs{} do error"not here" end |
10258 |
+for i=1,0 do error'not here' end |
10259 |
+for i=0,1,-1 do error'not here' end |
10260 |
+a = nil; for i=1,1 do assert(not a); a=1 end; assert(a) |
10261 |
+a = nil; for i=1,1,-1 do assert(not a); a=1 end; assert(a) |
10262 |
+ |
10263 |
+do |
10264 |
+ print("testing floats in numeric for") |
10265 |
+ local a |
10266 |
+ -- integer count |
10267 |
+ a = 0; for i=1, 1, 1 do a=a+1 end; assert(a==1) |
10268 |
+ a = 0; for i=10000, 1e4, -1 do a=a+1 end; assert(a==1) |
10269 |
+ a = 0; for i=1, 0.99999, 1 do a=a+1 end; assert(a==0) |
10270 |
+ a = 0; for i=9999, 1e4, -1 do a=a+1 end; assert(a==0) |
10271 |
+ a = 0; for i=1, 0.99999, -1 do a=a+1 end; assert(a==1) |
10272 |
+ |
10273 |
+ -- float count |
10274 |
+ a = 0; for i=0, 0.999999999, 0.1 do a=a+1 end; assert(a==10) |
10275 |
+ a = 0; for i=1.0, 1, 1 do a=a+1 end; assert(a==1) |
10276 |
+ a = 0; for i=-1.5, -1.5, 1 do a=a+1 end; assert(a==1) |
10277 |
+ a = 0; for i=1e6, 1e6, -1 do a=a+1 end; assert(a==1) |
10278 |
+ a = 0; for i=1.0, 0.99999, 1 do a=a+1 end; assert(a==0) |
10279 |
+ a = 0; for i=99999, 1e5, -1.0 do a=a+1 end; assert(a==0) |
10280 |
+ a = 0; for i=1.0, 0.99999, -1 do a=a+1 end; assert(a==1) |
10281 |
+end |
10282 |
+ |
10283 |
+-- conversion |
10284 |
+a = 0; for i="10","1","-2" do a=a+1 end; assert(a==5) |
10285 |
+ |
10286 |
+do -- checking types |
10287 |
+ local c |
10288 |
+ local function checkfloat (i) |
10289 |
+ assert(math.type(i) == "float") |
10290 |
+ c = c + 1 |
10291 |
+ end |
10292 |
+ |
10293 |
+ c = 0; for i = 1.0, 10 do checkfloat(i) end |
10294 |
+ assert(c == 10) |
10295 |
+ |
10296 |
+ c = 0; for i = -1, -10, -1.0 do checkfloat(i) end |
10297 |
+ assert(c == 10) |
10298 |
+ |
10299 |
+ local function checkint (i) |
10300 |
+ assert(math.type(i) == "integer") |
10301 |
+ c = c + 1 |
10302 |
+ end |
10303 |
+ |
10304 |
+ local m = math.maxinteger |
10305 |
+ c = 0; for i = m, m - 10, -1 do checkint(i) end |
10306 |
+ assert(c == 11) |
10307 |
+ |
10308 |
+ c = 0; for i = 1, 10.9 do checkint(i) end |
10309 |
+ assert(c == 10) |
10310 |
+ |
10311 |
+ c = 0; for i = 10, 0.001, -1 do checkint(i) end |
10312 |
+ assert(c == 10) |
10313 |
+ |
10314 |
+ c = 0; for i = 1, "10.8" do checkint(i) end |
10315 |
+ assert(c == 10) |
10316 |
+ |
10317 |
+ c = 0; for i = 9, "3.4", -1 do checkint(i) end |
10318 |
+ assert(c == 6) |
10319 |
+ |
10320 |
+ c = 0; for i = 0, " -3.4 ", -1 do checkint(i) end |
10321 |
+ assert(c == 4) |
10322 |
+ |
10323 |
+ c = 0; for i = 100, "96.3", -2 do checkint(i) end |
10324 |
+ assert(c == 2) |
10325 |
+ |
10326 |
+ c = 0; for i = 1, math.huge do if i > 10 then break end; checkint(i) end |
10327 |
+ assert(c == 10) |
10328 |
+ |
10329 |
+ c = 0; for i = -1, -math.huge, -1 do |
10330 |
+ if i < -10 then break end; checkint(i) |
10331 |
+ end |
10332 |
+ assert(c == 10) |
10333 |
+ |
10334 |
+ |
10335 |
+ for i = math.mininteger, -10e100 do assert(false) end |
10336 |
+ for i = math.maxinteger, 10e100, -1 do assert(false) end |
10337 |
+ |
10338 |
+end |
10339 |
+ |
10340 |
+collectgarbage() |
10341 |
+ |
10342 |
+ |
10343 |
+-- testing generic 'for' |
10344 |
+ |
10345 |
+local function f (n, p) |
10346 |
+ local t = {}; for i=1,p do t[i] = i*10 end |
10347 |
+ return function (_,n) |
10348 |
+ if n > 0 then |
10349 |
+ n = n-1 |
10350 |
+ return n, table.unpack(t) |
10351 |
+ end |
10352 |
+ end, nil, n |
10353 |
+end |
10354 |
+ |
10355 |
+local x = 0 |
10356 |
+for n,a,b,c,d in f(5,3) do |
10357 |
+ x = x+1 |
10358 |
+ assert(a == 10 and b == 20 and c == 30 and d == nil) |
10359 |
+end |
10360 |
+assert(x == 5) |
10361 |
+ |
10362 |
+ |
10363 |
+ |
10364 |
+-- testing __pairs and __ipairs metamethod |
10365 |
+a = {} |
10366 |
+do |
10367 |
+ local x,y,z = pairs(a) |
10368 |
+ assert(type(x) == 'function' and y == a and z == nil) |
10369 |
+end |
10370 |
+ |
10371 |
+local function foo (e,i) |
10372 |
+ assert(e == a) |
10373 |
+ if i <= 10 then return i+1, i+2 end |
10374 |
+end |
10375 |
+ |
10376 |
+local function foo1 (e,i) |
10377 |
+ i = i + 1 |
10378 |
+ assert(e == a) |
10379 |
+ if i <= e.n then return i,a[i] end |
10380 |
+end |
10381 |
+ |
10382 |
+setmetatable(a, {__pairs = function (x) return foo, x, 0 end}) |
10383 |
+ |
10384 |
+local i = 0 |
10385 |
+for k,v in pairs(a) do |
10386 |
+ i = i + 1 |
10387 |
+ assert(k == i and v == k+1) |
10388 |
+end |
10389 |
+ |
10390 |
+a.n = 5 |
10391 |
+a[3] = 30 |
10392 |
+ |
10393 |
+-- testing ipairs with metamethods |
10394 |
+a = {n=10} |
10395 |
+setmetatable(a, { __index = function (t,k) |
10396 |
+ if k <= t.n then return k * 10 end |
10397 |
+ end}) |
10398 |
+i = 0 |
10399 |
+for k,v in ipairs(a) do |
10400 |
+ i = i + 1 |
10401 |
+ assert(k == i and v == i * 10) |
10402 |
+end |
10403 |
+assert(i == a.n) |
10404 |
+ |
10405 |
+print"OK" |
10406 |
|
10407 |
diff --git a/tests/pm.lua b/tests/pm.lua |
10408 |
new file mode 100644 |
10409 |
index 0000000..fde39ff |
10410 |
--- /dev/null |
10411 |
+++ b/tests/pm.lua |
10412 |
@@ -0,0 +1,374 @@ |
10413 |
+-- $Id: pm.lua,v 1.48 2016/11/07 13:11:28 roberto Exp $ |
10414 |
+-- See Copyright Notice in file all.lua |
10415 |
+ |
10416 |
+print('testing pattern matching') |
10417 |
+ |
10418 |
+local function checkerror (msg, f, ...) |
10419 |
+ local s, err = pcall(f, ...) |
10420 |
+ assert(not s and string.find(err, msg)) |
10421 |
+end |
10422 |
+ |
10423 |
+ |
10424 |
+function f(s, p) |
10425 |
+ local i,e = string.find(s, p) |
10426 |
+ if i then return string.sub(s, i, e) end |
10427 |
+end |
10428 |
+ |
10429 |
+a,b = string.find('', '') -- empty patterns are tricky |
10430 |
+assert(a == 1 and b == 0); |
10431 |
+a,b = string.find('alo', '') |
10432 |
+assert(a == 1 and b == 0) |
10433 |
+a,b = string.find('a\0o a\0o a\0o', 'a', 1) -- first position |
10434 |
+assert(a == 1 and b == 1) |
10435 |
+a,b = string.find('a\0o a\0o a\0o', 'a\0o', 2) -- starts in the midle |
10436 |
+assert(a == 5 and b == 7) |
10437 |
+a,b = string.find('a\0o a\0o a\0o', 'a\0o', 9) -- starts in the midle |
10438 |
+assert(a == 9 and b == 11) |
10439 |
+a,b = string.find('a\0a\0a\0a\0\0ab', '\0ab', 2); -- finds at the end |
10440 |
+assert(a == 9 and b == 11); |
10441 |
+a,b = string.find('a\0a\0a\0a\0\0ab', 'b') -- last position |
10442 |
+assert(a == 11 and b == 11) |
10443 |
+assert(string.find('a\0a\0a\0a\0\0ab', 'b\0') == nil) -- check ending |
10444 |
+assert(string.find('', '\0') == nil) |
10445 |
+assert(string.find('alo123alo', '12') == 4) |
10446 |
+assert(string.find('alo123alo', '^12') == nil) |
10447 |
+ |
10448 |
+assert(string.match("aaab", ".*b") == "aaab") |
10449 |
+assert(string.match("aaa", ".*a") == "aaa") |
10450 |
+assert(string.match("b", ".*b") == "b") |
10451 |
+ |
10452 |
+assert(string.match("aaab", ".+b") == "aaab") |
10453 |
+assert(string.match("aaa", ".+a") == "aaa") |
10454 |
+assert(not string.match("b", ".+b")) |
10455 |
+ |
10456 |
+assert(string.match("aaab", ".?b") == "ab") |
10457 |
+assert(string.match("aaa", ".?a") == "aa") |
10458 |
+assert(string.match("b", ".?b") == "b") |
10459 |
+ |
10460 |
+assert(f('aloALO', '%l*') == 'alo') |
10461 |
+assert(f('aLo_ALO', '%a*') == 'aLo') |
10462 |
+ |
10463 |
+assert(f(" \n\r*&\n\r xuxu \n\n", "%g%g%g+") == "xuxu") |
10464 |
+ |
10465 |
+assert(f('aaab', 'a*') == 'aaa'); |
10466 |
+assert(f('aaa', '^.*$') == 'aaa'); |
10467 |
+assert(f('aaa', 'b*') == ''); |
10468 |
+assert(f('aaa', 'ab*a') == 'aa') |
10469 |
+assert(f('aba', 'ab*a') == 'aba') |
10470 |
+assert(f('aaab', 'a+') == 'aaa') |
10471 |
+assert(f('aaa', '^.+$') == 'aaa') |
10472 |
+assert(f('aaa', 'b+') == nil) |
10473 |
+assert(f('aaa', 'ab+a') == nil) |
10474 |
+assert(f('aba', 'ab+a') == 'aba') |
10475 |
+assert(f('a$a', '.$') == 'a') |
10476 |
+assert(f('a$a', '.%$') == 'a$') |
10477 |
+assert(f('a$a', '.$.') == 'a$a') |
10478 |
+assert(f('a$a', '$$') == nil) |
10479 |
+assert(f('a$b', 'a$') == nil) |
10480 |
+assert(f('a$a', '$') == '') |
10481 |
+assert(f('', 'b*') == '') |
10482 |
+assert(f('aaa', 'bb*') == nil) |
10483 |
+assert(f('aaab', 'a-') == '') |
10484 |
+assert(f('aaa', '^.-$') == 'aaa') |
10485 |
+assert(f('aabaaabaaabaaaba', 'b.*b') == 'baaabaaabaaab') |
10486 |
+assert(f('aabaaabaaabaaaba', 'b.-b') == 'baaab') |
10487 |
+assert(f('alo xo', '.o$') == 'xo') |
10488 |
+assert(f(' \n isto é assim', '%S%S*') == 'isto') |
10489 |
+assert(f(' \n isto é assim', '%S*$') == 'assim') |
10490 |
+assert(f(' \n isto é assim', '[a-z]*$') == 'assim') |
10491 |
+assert(f('um caracter ? extra', '[^%sa-z]') == '?') |
10492 |
+assert(f('', 'a?') == '') |
10493 |
+assert(f('á', 'á?') == 'á') |
10494 |
+assert(f('ábl', 'á?b?l?') == 'ábl') |
10495 |
+assert(f(' ábl', 'á?b?l?') == '') |
10496 |
+assert(f('aa', '^aa?a?a') == 'aa') |
10497 |
+assert(f(']]]áb', '[^]]') == 'á') |
10498 |
+assert(f("0alo alo", "%x*") == "0a") |
10499 |
+assert(f("alo alo", "%C+") == "alo alo") |
10500 |
+print('+') |
10501 |
+ |
10502 |
+ |
10503 |
+function f1(s, p) |
10504 |
+ p = string.gsub(p, "%%([0-9])", function (s) |
10505 |
+ return "%" .. (tonumber(s)+1) |
10506 |
+ end) |
10507 |
+ p = string.gsub(p, "^(^?)", "%1()", 1) |
10508 |
+ p = string.gsub(p, "($?)$", "()%1", 1) |
10509 |
+ local t = {string.match(s, p)} |
10510 |
+ return string.sub(s, t[1], t[#t] - 1) |
10511 |
+end |
10512 |
+ |
10513 |
+assert(f1('alo alx 123 b\0o b\0o', '(..*) %1') == "b\0o b\0o") |
10514 |
+assert(f1('axz123= 4= 4 34', '(.+)=(.*)=%2 %1') == '3= 4= 4 3') |
10515 |
+assert(f1('=======', '^(=*)=%1$') == '=======') |
10516 |
+assert(string.match('==========', '^([=]*)=%1$') == nil) |
10517 |
+ |
10518 |
+local function range (i, j) |
10519 |
+ if i <= j then |
10520 |
+ return i, range(i+1, j) |
10521 |
+ end |
10522 |
+end |
10523 |
+ |
10524 |
+local abc = string.char(range(0, 255)); |
10525 |
+ |
10526 |
+assert(string.len(abc) == 256) |
10527 |
+ |
10528 |
+function strset (p) |
10529 |
+ local res = {s=''} |
10530 |
+ string.gsub(abc, p, function (c) res.s = res.s .. c end) |
10531 |
+ return res.s |
10532 |
+end; |
10533 |
+ |
10534 |
+assert(string.len(strset('[\200-\210]')) == 11) |
10535 |
+ |
10536 |
+assert(strset('[a-z]') == "abcdefghijklmnopqrstuvwxyz") |
10537 |
+assert(strset('[a-z%d]') == strset('[%da-uu-z]')) |
10538 |
+assert(strset('[a-]') == "-a") |
10539 |
+assert(strset('[^%W]') == strset('[%w]')) |
10540 |
+assert(strset('[]%%]') == '%]') |
10541 |
+assert(strset('[a%-z]') == '-az') |
10542 |
+assert(strset('[%^%[%-a%]%-b]') == '-[]^ab') |
10543 |
+assert(strset('%Z') == strset('[\1-\255]')) |
10544 |
+assert(strset('.') == strset('[\1-\255%z]')) |
10545 |
+print('+'); |
10546 |
+ |
10547 |
+assert(string.match("alo xyzK", "(%w+)K") == "xyz") |
10548 |
+assert(string.match("254 K", "(%d*)K") == "") |
10549 |
+assert(string.match("alo ", "(%w*)$") == "") |
10550 |
+assert(string.match("alo ", "(%w+)$") == nil) |
10551 |
+assert(string.find("(álo)", "%(á") == 1) |
10552 |
+local a, b, c, d, e = string.match("âlo alo", "^(((.).).* (%w*))$") |
10553 |
+assert(a == 'âlo alo' and b == 'âl' and c == 'â' and d == 'alo' and e == nil) |
10554 |
+a, b, c, d = string.match('0123456789', '(.+(.?)())') |
10555 |
+assert(a == '0123456789' and b == '' and c == 11 and d == nil) |
10556 |
+print('+') |
10557 |
+ |
10558 |
+assert(string.gsub('ülo ülo', 'ü', 'x') == 'xlo xlo') |
10559 |
+assert(string.gsub('alo úlo ', ' +$', '') == 'alo úlo') -- trim |
10560 |
+assert(string.gsub(' alo alo ', '^%s*(.-)%s*$', '%1') == 'alo alo') -- double trim |
10561 |
+assert(string.gsub('alo alo \n 123\n ', '%s+', ' ') == 'alo alo 123 ') |
10562 |
+t = "abç d" |
10563 |
+a, b = string.gsub(t, '(.)', '%1@') |
10564 |
+assert('@'..a == string.gsub(t, '', '@') and b == 5) |
10565 |
+a, b = string.gsub('abçd', '(.)', '%0@', 2) |
10566 |
+assert(a == 'a@b@çd' and b == 2) |
10567 |
+assert(string.gsub('alo alo', '()[al]', '%1') == '12o 56o') |
10568 |
+assert(string.gsub("abc=xyz", "(%w*)(%p)(%w+)", "%3%2%1-%0") == |
10569 |
+ "xyz=abc-abc=xyz") |
10570 |
+assert(string.gsub("abc", "%w", "%1%0") == "aabbcc") |
10571 |
+assert(string.gsub("abc", "%w+", "%0%1") == "abcabc") |
10572 |
+assert(string.gsub('áéí', '$', '\0óú') == 'áéí\0óú') |
10573 |
+assert(string.gsub('', '^', 'r') == 'r') |
10574 |
+assert(string.gsub('', '$', 'r') == 'r') |
10575 |
+print('+') |
10576 |
+ |
10577 |
+ |
10578 |
+do -- new (5.3.3) semantics for empty matches |
10579 |
+ assert(string.gsub("a b cd", " *", "-") == "-a-b-c-d-") |
10580 |
+ |
10581 |
+ local res = "" |
10582 |
+ local sub = "a \nbc\t\td" |
10583 |
+ local i = 1 |
10584 |
+ for p, e in string.gmatch(sub, "()%s*()") do |
10585 |
+ res = res .. string.sub(sub, i, p - 1) .. "-" |
10586 |
+ i = e |
10587 |
+ end |
10588 |
+ assert(res == "-a-b-c-d-") |
10589 |
+end |
10590 |
+ |
10591 |
+ |
10592 |
+assert(string.gsub("um (dois) tres (quatro)", "(%(%w+%))", string.upper) == |
10593 |
+ "um (DOIS) tres (QUATRO)") |
10594 |
+ |
10595 |
+do |
10596 |
+ local function setglobal (n,v) rawset(_G, n, v) end |
10597 |
+ string.gsub("a=roberto,roberto=a", "(%w+)=(%w%w*)", setglobal) |
10598 |
+ assert(_G.a=="roberto" and _G.roberto=="a") |
10599 |
+end |
10600 |
+ |
10601 |
+function f(a,b) return string.gsub(a,'.',b) end |
10602 |
+assert(string.gsub("trocar tudo em |teste|b| é |beleza|al|", "|([^|]*)|([^|]*)|", f) == |
10603 |
+ "trocar tudo em bbbbb é alalalalalal") |
10604 |
+ |
10605 |
+local function dostring (s) return load(s, "")() or "" end |
10606 |
+assert(string.gsub("alo $a='x'$ novamente $return a$", |
10607 |
+ "$([^$]*)%$", |
10608 |
+ dostring) == "alo novamente x") |
10609 |
+ |
10610 |
+x = string.gsub("$x=string.gsub('alo', '.', string.upper)$ assim vai para $return x$", |
10611 |
+ "$([^$]*)%$", dostring) |
10612 |
+assert(x == ' assim vai para ALO') |
10613 |
+ |
10614 |
+t = {} |
10615 |
+s = 'a alo jose joao' |
10616 |
+r = string.gsub(s, '()(%w+)()', function (a,w,b) |
10617 |
+ assert(string.len(w) == b-a); |
10618 |
+ t[a] = b-a; |
10619 |
+ end) |
10620 |
+assert(s == r and t[1] == 1 and t[3] == 3 and t[7] == 4 and t[13] == 4) |
10621 |
+ |
10622 |
+ |
10623 |
+function isbalanced (s) |
10624 |
+ return string.find(string.gsub(s, "%b()", ""), "[()]") == nil |
10625 |
+end |
10626 |
+ |
10627 |
+assert(isbalanced("(9 ((8))(\0) 7) \0\0 a b ()(c)() a")) |
10628 |
+assert(not isbalanced("(9 ((8) 7) a b (\0 c) a")) |
10629 |
+assert(string.gsub("alo 'oi' alo", "%b''", '"') == 'alo " alo') |
10630 |
+ |
10631 |
+ |
10632 |
+local t = {"apple", "orange", "lime"; n=0} |
10633 |
+assert(string.gsub("x and x and x", "x", function () t.n=t.n+1; return t[t.n] end) |
10634 |
+ == "apple and orange and lime") |
10635 |
+ |
10636 |
+t = {n=0} |
10637 |
+string.gsub("first second word", "%w%w*", function (w) t.n=t.n+1; t[t.n] = w end) |
10638 |
+assert(t[1] == "first" and t[2] == "second" and t[3] == "word" and t.n == 3) |
10639 |
+ |
10640 |
+t = {n=0} |
10641 |
+assert(string.gsub("first second word", "%w+", |
10642 |
+ function (w) t.n=t.n+1; t[t.n] = w end, 2) == "first second word") |
10643 |
+assert(t[1] == "first" and t[2] == "second" and t[3] == nil) |
10644 |
+ |
10645 |
+checkerror("invalid replacement value %(a table%)", |
10646 |
+ string.gsub, "alo", ".", {a = {}}) |
10647 |
+checkerror("invalid capture index %%2", string.gsub, "alo", ".", "%2") |
10648 |
+checkerror("invalid capture index %%0", string.gsub, "alo", "(%0)", "a") |
10649 |
+checkerror("invalid capture index %%1", string.gsub, "alo", "(%1)", "a") |
10650 |
+checkerror("invalid use of '%%'", string.gsub, "alo", ".", "%x") |
10651 |
+ |
10652 |
+-- bug since 2.5 (C-stack overflow) |
10653 |
+do |
10654 |
+ local function f (size) |
10655 |
+ local s = string.rep("a", size) |
10656 |
+ local p = string.rep(".?", size) |
10657 |
+ return pcall(string.match, s, p) |
10658 |
+ end |
10659 |
+ local r, m = f(80) |
10660 |
+ assert(r and #m == 80) |
10661 |
+ r, m = f(200000) |
10662 |
+ assert(not r and string.find(m, "too complex")) |
10663 |
+end |
10664 |
+ |
10665 |
+if not _soft then |
10666 |
+ print("big strings") |
10667 |
+ local a = string.rep('a', 300000) |
10668 |
+ assert(string.find(a, '^a*.?$')) |
10669 |
+ assert(not string.find(a, '^a*.?b$')) |
10670 |
+ assert(string.find(a, '^a-.?$')) |
10671 |
+ |
10672 |
+ -- bug in 5.1.2 |
10673 |
+ a = string.rep('a', 10000) .. string.rep('b', 10000) |
10674 |
+ assert(not pcall(string.gsub, a, 'b')) |
10675 |
+end |
10676 |
+ |
10677 |
+-- recursive nest of gsubs |
10678 |
+function rev (s) |
10679 |
+ return string.gsub(s, "(.)(.+)", function (c,s1) return rev(s1)..c end) |
10680 |
+end |
10681 |
+ |
10682 |
+local x = "abcdef" |
10683 |
+assert(rev(rev(x)) == x) |
10684 |
+ |
10685 |
+ |
10686 |
+-- gsub with tables |
10687 |
+assert(string.gsub("alo alo", ".", {}) == "alo alo") |
10688 |
+assert(string.gsub("alo alo", "(.)", {a="AA", l=""}) == "AAo AAo") |
10689 |
+assert(string.gsub("alo alo", "(.).", {a="AA", l="K"}) == "AAo AAo") |
10690 |
+assert(string.gsub("alo alo", "((.)(.?))", {al="AA", o=false}) == "AAo AAo") |
10691 |
+ |
10692 |
+assert(string.gsub("alo alo", "().", {'x','yy','zzz'}) == "xyyzzz alo") |
10693 |
+ |
10694 |
+t = {}; setmetatable(t, {__index = function (t,s) return string.upper(s) end}) |
10695 |
+assert(string.gsub("a alo b hi", "%w%w+", t) == "a ALO b HI") |
10696 |
+ |
10697 |
+ |
10698 |
+-- tests for gmatch |
10699 |
+local a = 0 |
10700 |
+for i in string.gmatch('abcde', '()') do assert(i == a+1); a=i end |
10701 |
+assert(a==6) |
10702 |
+ |
10703 |
+t = {n=0} |
10704 |
+for w in string.gmatch("first second word", "%w+") do |
10705 |
+ t.n=t.n+1; t[t.n] = w |
10706 |
+end |
10707 |
+assert(t[1] == "first" and t[2] == "second" and t[3] == "word") |
10708 |
+ |
10709 |
+t = {3, 6, 9} |
10710 |
+for i in string.gmatch ("xuxx uu ppar r", "()(.)%2") do |
10711 |
+ assert(i == table.remove(t, 1)) |
10712 |
+end |
10713 |
+assert(#t == 0) |
10714 |
+ |
10715 |
+t = {} |
10716 |
+for i,j in string.gmatch("13 14 10 = 11, 15= 16, 22=23", "(%d+)%s*=%s*(%d+)") do |
10717 |
+ t[tonumber(i)] = tonumber(j) |
10718 |
+end |
10719 |
+a = 0 |
10720 |
+for k,v in pairs(t) do assert(k+1 == v+0); a=a+1 end |
10721 |
+assert(a == 3) |
10722 |
+ |
10723 |
+ |
10724 |
+-- tests for `%f' (`frontiers') |
10725 |
+ |
10726 |
+assert(string.gsub("aaa aa a aaa a", "%f[%w]a", "x") == "xaa xa x xaa x") |
10727 |
+assert(string.gsub("[[]] [][] [[[[", "%f[[].", "x") == "x[]] x]x] x[[[") |
10728 |
+assert(string.gsub("01abc45de3", "%f[%d]", ".") == ".01abc.45de.3") |
10729 |
+assert(string.gsub("01abc45 de3x", "%f[%D]%w", ".") == "01.bc45 de3.") |
10730 |
+assert(string.gsub("function", "%f[\1-\255]%w", ".") == ".unction") |
10731 |
+assert(string.gsub("function", "%f[^\1-\255]", ".") == "function.") |
10732 |
+ |
10733 |
+assert(string.find("a", "%f[a]") == 1) |
10734 |
+assert(string.find("a", "%f[^%z]") == 1) |
10735 |
+assert(string.find("a", "%f[^%l]") == 2) |
10736 |
+assert(string.find("aba", "%f[a%z]") == 3) |
10737 |
+assert(string.find("aba", "%f[%z]") == 4) |
10738 |
+assert(not string.find("aba", "%f[%l%z]")) |
10739 |
+assert(not string.find("aba", "%f[^%l%z]")) |
10740 |
+ |
10741 |
+local i, e = string.find(" alo aalo allo", "%f[%S].-%f[%s].-%f[%S]") |
10742 |
+assert(i == 2 and e == 5) |
10743 |
+local k = string.match(" alo aalo allo", "%f[%S](.-%f[%s].-%f[%S])") |
10744 |
+assert(k == 'alo ') |
10745 |
+ |
10746 |
+local a = {1, 5, 9, 14, 17,} |
10747 |
+for k in string.gmatch("alo alo th02 is 1hat", "()%f[%w%d]") do |
10748 |
+ assert(table.remove(a, 1) == k) |
10749 |
+end |
10750 |
+assert(#a == 0) |
10751 |
+ |
10752 |
+ |
10753 |
+-- malformed patterns |
10754 |
+local function malform (p, m) |
10755 |
+ m = m or "malformed" |
10756 |
+ local r, msg = pcall(string.find, "a", p) |
10757 |
+ assert(not r and string.find(msg, m)) |
10758 |
+end |
10759 |
+ |
10760 |
+malform("(.", "unfinished capture") |
10761 |
+malform(".)", "invalid pattern capture") |
10762 |
+malform("[a") |
10763 |
+malform("[]") |
10764 |
+malform("[^]") |
10765 |
+malform("[a%]") |
10766 |
+malform("[a%") |
10767 |
+malform("%b") |
10768 |
+malform("%ba") |
10769 |
+malform("%") |
10770 |
+malform("%f", "missing") |
10771 |
+ |
10772 |
+-- \0 in patterns |
10773 |
+assert(string.match("ab\0\1\2c", "[\0-\2]+") == "\0\1\2") |
10774 |
+assert(string.match("ab\0\1\2c", "[\0-\0]+") == "\0") |
10775 |
+assert(string.find("b$a", "$\0?") == 2) |
10776 |
+assert(string.find("abc\0efg", "%\0") == 4) |
10777 |
+assert(string.match("abc\0efg\0\1e\1g", "%b\0\1") == "\0efg\0\1e\1") |
10778 |
+assert(string.match("abc\0\0\0", "%\0+") == "\0\0\0") |
10779 |
+assert(string.match("abc\0\0\0", "%\0%\0?") == "\0\0") |
10780 |
+ |
10781 |
+-- magic char after \0 |
10782 |
+assert(string.find("abc\0\0","\0.") == 4) |
10783 |
+assert(string.find("abcx\0\0abc\0abc","x\0\0abc\0a.") == 4) |
10784 |
+ |
10785 |
+print('OK') |
10786 |
+ |
10787 |
|
10788 |
diff --git a/tests/sort.lua b/tests/sort.lua |
10789 |
new file mode 100644 |
10790 |
index 0000000..d52feee |
10791 |
--- /dev/null |
10792 |
+++ b/tests/sort.lua |
10793 |
@@ -0,0 +1,310 @@ |
10794 |
+-- $Id: sort.lua,v 1.38 2016/11/07 13:11:28 roberto Exp $ |
10795 |
+-- See Copyright Notice in file all.lua |
10796 |
+ |
10797 |
+print "testing (parts of) table library" |
10798 |
+ |
10799 |
+print "testing unpack" |
10800 |
+ |
10801 |
+local unpack = table.unpack |
10802 |
+ |
10803 |
+local maxI = math.maxinteger |
10804 |
+local minI = math.mininteger |
10805 |
+ |
10806 |
+ |
10807 |
+local function checkerror (msg, f, ...) |
10808 |
+ local s, err = pcall(f, ...) |
10809 |
+ assert(not s and string.find(err, msg)) |
10810 |
+end |
10811 |
+ |
10812 |
+ |
10813 |
+checkerror("wrong number of arguments", table.insert, {}, 2, 3, 4) |
10814 |
+ |
10815 |
+local x,y,z,a,n |
10816 |
+a = {}; lim = _soft and 200 or 2000 |
10817 |
+for i=1, lim do a[i]=i end |
10818 |
+assert(select(lim, unpack(a)) == lim and select('#', unpack(a)) == lim) |
10819 |
+x = unpack(a) |
10820 |
+assert(x == 1) |
10821 |
+x = {unpack(a)} |
10822 |
+assert(#x == lim and x[1] == 1 and x[lim] == lim) |
10823 |
+x = {unpack(a, lim-2)} |
10824 |
+assert(#x == 3 and x[1] == lim-2 and x[3] == lim) |
10825 |
+x = {unpack(a, 10, 6)} |
10826 |
+assert(next(x) == nil) -- no elements |
10827 |
+x = {unpack(a, 11, 10)} |
10828 |
+assert(next(x) == nil) -- no elements |
10829 |
+x,y = unpack(a, 10, 10) |
10830 |
+assert(x == 10 and y == nil) |
10831 |
+x,y,z = unpack(a, 10, 11) |
10832 |
+assert(x == 10 and y == 11 and z == nil) |
10833 |
+a,x = unpack{1} |
10834 |
+assert(a==1 and x==nil) |
10835 |
+a,x = unpack({1,2}, 1, 1) |
10836 |
+assert(a==1 and x==nil) |
10837 |
+ |
10838 |
+do |
10839 |
+ local maxi = (1 << 31) - 1 -- maximum value for an int (usually) |
10840 |
+ local mini = -(1 << 31) -- minimum value for an int (usually) |
10841 |
+ checkerror("too many results", unpack, {}, 0, maxi) |
10842 |
+ checkerror("too many results", unpack, {}, 1, maxi) |
10843 |
+ checkerror("too many results", unpack, {}, 0, maxI) |
10844 |
+ checkerror("too many results", unpack, {}, 1, maxI) |
10845 |
+ checkerror("too many results", unpack, {}, mini, maxi) |
10846 |
+ checkerror("too many results", unpack, {}, -maxi, maxi) |
10847 |
+ checkerror("too many results", unpack, {}, minI, maxI) |
10848 |
+ unpack({}, maxi, 0) |
10849 |
+ unpack({}, maxi, 1) |
10850 |
+ unpack({}, maxI, minI) |
10851 |
+ pcall(unpack, {}, 1, maxi + 1) |
10852 |
+ local a, b = unpack({[maxi] = 20}, maxi, maxi) |
10853 |
+ assert(a == 20 and b == nil) |
10854 |
+ a, b = unpack({[maxi] = 20}, maxi - 1, maxi) |
10855 |
+ assert(a == nil and b == 20) |
10856 |
+ local t = {[maxI - 1] = 12, [maxI] = 23} |
10857 |
+ a, b = unpack(t, maxI - 1, maxI); assert(a == 12 and b == 23) |
10858 |
+ a, b = unpack(t, maxI, maxI); assert(a == 23 and b == nil) |
10859 |
+ a, b = unpack(t, maxI, maxI - 1); assert(a == nil and b == nil) |
10860 |
+ t = {[minI] = 12.3, [minI + 1] = 23.5} |
10861 |
+ a, b = unpack(t, minI, minI + 1); assert(a == 12.3 and b == 23.5) |
10862 |
+ a, b = unpack(t, minI, minI); assert(a == 12.3 and b == nil) |
10863 |
+ a, b = unpack(t, minI + 1, minI); assert(a == nil and b == nil) |
10864 |
+end |
10865 |
+ |
10866 |
+do -- length is not an integer |
10867 |
+ local t = setmetatable({}, {__len = function () return 'abc' end}) |
10868 |
+ assert(#t == 'abc') |
10869 |
+ checkerror("object length is not an integer", table.insert, t, 1) |
10870 |
+end |
10871 |
+ |
10872 |
+print "testing pack" |
10873 |
+ |
10874 |
+a = table.pack() |
10875 |
+assert(a[1] == nil and a.n == 0) |
10876 |
+ |
10877 |
+a = table.pack(table) |
10878 |
+assert(a[1] == table and a.n == 1) |
10879 |
+ |
10880 |
+a = table.pack(nil, nil, nil, nil) |
10881 |
+assert(a[1] == nil and a.n == 4) |
10882 |
+ |
10883 |
+ |
10884 |
+-- testing move |
10885 |
+do |
10886 |
+ |
10887 |
+ checkerror("table expected", table.move, 1, 2, 3, 4) |
10888 |
+ |
10889 |
+ local function eqT (a, b) |
10890 |
+ for k, v in pairs(a) do assert(b[k] == v) end |
10891 |
+ for k, v in pairs(b) do assert(a[k] == v) end |
10892 |
+ end |
10893 |
+ |
10894 |
+ local a = table.move({10,20,30}, 1, 3, 2) -- move forward |
10895 |
+ eqT(a, {10,10,20,30}) |
10896 |
+ |
10897 |
+ -- move forward with overlap of 1 |
10898 |
+ a = table.move({10, 20, 30}, 1, 3, 3) |
10899 |
+ eqT(a, {10, 20, 10, 20, 30}) |
10900 |
+ |
10901 |
+ -- moving to the same table (not being explicit about it) |
10902 |
+ a = {10, 20, 30, 40} |
10903 |
+ table.move(a, 1, 4, 2, a) |
10904 |
+ eqT(a, {10, 10, 20, 30, 40}) |
10905 |
+ |
10906 |
+ a = table.move({10,20,30}, 2, 3, 1) -- move backward |
10907 |
+ eqT(a, {20,30,30}) |
10908 |
+ |
10909 |
+ a = {} -- move to new table |
10910 |
+ assert(table.move({10,20,30}, 1, 3, 1, a) == a) |
10911 |
+ eqT(a, {10,20,30}) |
10912 |
+ |
10913 |
+ a = {} |
10914 |
+ assert(table.move({10,20,30}, 1, 0, 3, a) == a) -- empty move (no move) |
10915 |
+ eqT(a, {}) |
10916 |
+ |
10917 |
+ a = table.move({10,20,30}, 1, 10, 1) -- move to the same place |
10918 |
+ eqT(a, {10,20,30}) |
10919 |
+ |
10920 |
+ -- moving on the fringes |
10921 |
+ a = table.move({[maxI - 2] = 1, [maxI - 1] = 2, [maxI] = 3}, |
10922 |
+ maxI - 2, maxI, -10, {}) |
10923 |
+ eqT(a, {[-10] = 1, [-9] = 2, [-8] = 3}) |
10924 |
+ |
10925 |
+ a = table.move({[minI] = 1, [minI + 1] = 2, [minI + 2] = 3}, |
10926 |
+ minI, minI + 2, -10, {}) |
10927 |
+ eqT(a, {[-10] = 1, [-9] = 2, [-8] = 3}) |
10928 |
+ |
10929 |
+ a = table.move({45}, 1, 1, maxI) |
10930 |
+ eqT(a, {45, [maxI] = 45}) |
10931 |
+ |
10932 |
+ a = table.move({[maxI] = 100}, maxI, maxI, minI) |
10933 |
+ eqT(a, {[minI] = 100, [maxI] = 100}) |
10934 |
+ |
10935 |
+ a = table.move({[minI] = 100}, minI, minI, maxI) |
10936 |
+ eqT(a, {[minI] = 100, [maxI] = 100}) |
10937 |
+ |
10938 |
+ a = setmetatable({}, { |
10939 |
+ __index = function (_,k) return k * 10 end, |
10940 |
+ __newindex = error}) |
10941 |
+ local b = table.move(a, 1, 10, 3, {}) |
10942 |
+ eqT(a, {}) |
10943 |
+ eqT(b, {nil,nil,10,20,30,40,50,60,70,80,90,100}) |
10944 |
+ |
10945 |
+ b = setmetatable({""}, { |
10946 |
+ __index = error, |
10947 |
+ __newindex = function (t,k,v) |
10948 |
+ t[1] = string.format("%s(%d,%d)", t[1], k, v) |
10949 |
+ end}) |
10950 |
+ table.move(a, 10, 13, 3, b) |
10951 |
+ assert(b[1] == "(3,100)(4,110)(5,120)(6,130)") |
10952 |
+ local stat, msg = pcall(table.move, b, 10, 13, 3, b) |
10953 |
+ assert(not stat and msg == b) |
10954 |
+end |
10955 |
+ |
10956 |
+do |
10957 |
+ -- for very long moves, just check initial accesses and interrupt |
10958 |
+ -- move with an error |
10959 |
+ local function checkmove (f, e, t, x, y) |
10960 |
+ local pos1, pos2 |
10961 |
+ local a = setmetatable({}, { |
10962 |
+ __index = function (_,k) pos1 = k end, |
10963 |
+ __newindex = function (_,k) pos2 = k; error() end, }) |
10964 |
+ local st, msg = pcall(table.move, a, f, e, t) |
10965 |
+ assert(not st and not msg and pos1 == x and pos2 == y) |
10966 |
+ end |
10967 |
+ checkmove(1, maxI, 0, 1, 0) |
10968 |
+ checkmove(0, maxI - 1, 1, maxI - 1, maxI) |
10969 |
+ checkmove(minI, -2, -5, -2, maxI - 6) |
10970 |
+ checkmove(minI + 1, -1, -2, -1, maxI - 3) |
10971 |
+ checkmove(minI, -2, 0, minI, 0) -- non overlapping |
10972 |
+ checkmove(minI + 1, -1, 1, minI + 1, 1) -- non overlapping |
10973 |
+end |
10974 |
+ |
10975 |
+checkerror("too many", table.move, {}, 0, maxI, 1) |
10976 |
+checkerror("too many", table.move, {}, -1, maxI - 1, 1) |
10977 |
+checkerror("too many", table.move, {}, minI, -1, 1) |
10978 |
+checkerror("too many", table.move, {}, minI, maxI, 1) |
10979 |
+checkerror("wrap around", table.move, {}, 1, maxI, 2) |
10980 |
+checkerror("wrap around", table.move, {}, 1, 2, maxI) |
10981 |
+checkerror("wrap around", table.move, {}, minI, -2, 2) |
10982 |
+ |
10983 |
+ |
10984 |
+print"testing sort" |
10985 |
+ |
10986 |
+ |
10987 |
+-- strange lengths |
10988 |
+local a = setmetatable({}, {__len = function () return -1 end}) |
10989 |
+assert(#a == -1) |
10990 |
+table.sort(a, error) -- should not compare anything |
10991 |
+a = setmetatable({}, {__len = function () return maxI end}) |
10992 |
+checkerror("too big", table.sort, a) |
10993 |
+ |
10994 |
+-- test checks for invalid order functions |
10995 |
+local function check (t) |
10996 |
+ local function f(a, b) assert(a and b); return true end |
10997 |
+ checkerror("invalid order function", table.sort, t, f) |
10998 |
+end |
10999 |
+ |
11000 |
+check{1,2,3,4} |
11001 |
+check{1,2,3,4,5} |
11002 |
+check{1,2,3,4,5,6} |
11003 |
+ |
11004 |
+ |
11005 |
+function check (a, f) |
11006 |
+ f = f or function (x,y) return x<y end; |
11007 |
+ for n = #a, 2, -1 do |
11008 |
+ assert(not f(a[n], a[n-1])) |
11009 |
+ end |
11010 |
+end |
11011 |
+ |
11012 |
+a = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", |
11013 |
+ "Oct", "Nov", "Dec"} |
11014 |
+ |
11015 |
+table.sort(a) |
11016 |
+check(a) |
11017 |
+ |
11018 |
+function perm (s, n) |
11019 |
+ n = n or #s |
11020 |
+ if n == 1 then |
11021 |
+ local t = {unpack(s)} |
11022 |
+ table.sort(t) |
11023 |
+ check(t) |
11024 |
+ else |
11025 |
+ for i = 1, n do |
11026 |
+ s[i], s[n] = s[n], s[i] |
11027 |
+ perm(s, n - 1) |
11028 |
+ s[i], s[n] = s[n], s[i] |
11029 |
+ end |
11030 |
+ end |
11031 |
+end |
11032 |
+ |
11033 |
+perm{} |
11034 |
+perm{1} |
11035 |
+perm{1,2} |
11036 |
+perm{1,2,3} |
11037 |
+perm{1,2,3,4} |
11038 |
+perm{2,2,3,4} |
11039 |
+perm{1,2,3,4,5} |
11040 |
+perm{1,2,3,3,5} |
11041 |
+perm{1,2,3,4,5,6} |
11042 |
+perm{2,2,3,3,5,6} |
11043 |
+ |
11044 |
+function timesort (a, n, func, msg, pre) |
11045 |
+ local x = os.clock() |
11046 |
+ table.sort(a, func) |
11047 |
+ x = (os.clock() - x) * 1000 |
11048 |
+ pre = pre or "" |
11049 |
+ print(string.format("%ssorting %d %s elements in %.2f msec.", pre, n, msg, x)) |
11050 |
+ check(a, func) |
11051 |
+end |
11052 |
+ |
11053 |
+limit = 50000 |
11054 |
+if _soft then limit = 5000 end |
11055 |
+ |
11056 |
+a = {} |
11057 |
+for i=1,limit do |
11058 |
+ a[i] = math.random() |
11059 |
+end |
11060 |
+ |
11061 |
+timesort(a, limit, nil, "random") |
11062 |
+ |
11063 |
+timesort(a, limit, nil, "sorted", "re-") |
11064 |
+ |
11065 |
+a = {} |
11066 |
+for i=1,limit do |
11067 |
+ a[i] = math.random() |
11068 |
+end |
11069 |
+ |
11070 |
+x = os.clock(); i=0 |
11071 |
+table.sort(a, function(x,y) i=i+1; return y<x end) |
11072 |
+x = (os.clock() - x) * 1000 |
11073 |
+print(string.format("Invert-sorting other %d elements in %.2f msec., with %i comparisons", |
11074 |
+ limit, x, i)) |
11075 |
+check(a, function(x,y) return y<x end) |
11076 |
+ |
11077 |
+ |
11078 |
+table.sort{} -- empty array |
11079 |
+ |
11080 |
+for i=1,limit do a[i] = false end |
11081 |
+timesort(a, limit, function(x,y) return nil end, "equal") |
11082 |
+ |
11083 |
+for i,v in pairs(a) do assert(v == false) end |
11084 |
+ |
11085 |
+A = {"álo", "\0first :-)", "alo", "then this one", "45", "and a new"} |
11086 |
+table.sort(A) |
11087 |
+check(A) |
11088 |
+ |
11089 |
+table.sort(A, function (x, y) |
11090 |
+ load(string.format("A[%q] = ''", x), "")() |
11091 |
+ collectgarbage() |
11092 |
+ return x<y |
11093 |
+ end) |
11094 |
+ |
11095 |
+ |
11096 |
+tt = {__lt = function (a,b) return a.val < b.val end} |
11097 |
+a = {} |
11098 |
+for i=1,10 do a[i] = {val=math.random(100)}; setmetatable(a[i], tt); end |
11099 |
+table.sort(a) |
11100 |
+check(a, tt.__lt) |
11101 |
+check(a) |
11102 |
+ |
11103 |
+print"OK" |
11104 |
|
11105 |
diff --git a/tests/strings.lua b/tests/strings.lua |
11106 |
new file mode 100644 |
11107 |
index 0000000..c37a8e7 |
11108 |
--- /dev/null |
11109 |
+++ b/tests/strings.lua |
11110 |
@@ -0,0 +1,379 @@ |
11111 |
+-- $Id: strings.lua,v 1.87 2016/12/21 19:23:02 roberto Exp $ |
11112 |
+-- See Copyright Notice in file all.lua |
11113 |
+ |
11114 |
+print('testing strings and string library') |
11115 |
+ |
11116 |
+local maxi, mini = math.maxinteger, math.mininteger |
11117 |
+ |
11118 |
+ |
11119 |
+local function checkerror (msg, f, ...) |
11120 |
+ local s, err = pcall(f, ...) |
11121 |
+ assert(not s and string.find(err, msg)) |
11122 |
+end |
11123 |
+ |
11124 |
+ |
11125 |
+-- testing string comparisons |
11126 |
+assert('alo' < 'alo1') |
11127 |
+assert('' < 'a') |
11128 |
+assert('alo\0alo' < 'alo\0b') |
11129 |
+assert('alo\0alo\0\0' > 'alo\0alo\0') |
11130 |
+assert('alo' < 'alo\0') |
11131 |
+assert('alo\0' > 'alo') |
11132 |
+assert('\0' < '\1') |
11133 |
+assert('\0\0' < '\0\1') |
11134 |
+assert('\1\0a\0a' <= '\1\0a\0a') |
11135 |
+assert(not ('\1\0a\0b' <= '\1\0a\0a')) |
11136 |
+assert('\0\0\0' < '\0\0\0\0') |
11137 |
+assert(not('\0\0\0\0' < '\0\0\0')) |
11138 |
+assert('\0\0\0' <= '\0\0\0\0') |
11139 |
+assert(not('\0\0\0\0' <= '\0\0\0')) |
11140 |
+assert('\0\0\0' <= '\0\0\0') |
11141 |
+assert('\0\0\0' >= '\0\0\0') |
11142 |
+assert(not ('\0\0b' < '\0\0a\0')) |
11143 |
+ |
11144 |
+-- testing string.sub |
11145 |
+assert(string.sub("123456789",2,4) == "234") |
11146 |
+assert(string.sub("123456789",7) == "789") |
11147 |
+assert(string.sub("123456789",7,6) == "") |
11148 |
+assert(string.sub("123456789",7,7) == "7") |
11149 |
+assert(string.sub("123456789",0,0) == "") |
11150 |
+assert(string.sub("123456789",-10,10) == "123456789") |
11151 |
+assert(string.sub("123456789",1,9) == "123456789") |
11152 |
+assert(string.sub("123456789",-10,-20) == "") |
11153 |
+assert(string.sub("123456789",-1) == "9") |
11154 |
+assert(string.sub("123456789",-4) == "6789") |
11155 |
+assert(string.sub("123456789",-6, -4) == "456") |
11156 |
+assert(string.sub("123456789", mini, -4) == "123456") |
11157 |
+assert(string.sub("123456789", mini, maxi) == "123456789") |
11158 |
+assert(string.sub("123456789", mini, mini) == "") |
11159 |
+assert(string.sub("\000123456789",3,5) == "234") |
11160 |
+assert(("\000123456789"):sub(8) == "789") |
11161 |
+ |
11162 |
+-- testing string.find |
11163 |
+assert(string.find("123456789", "345") == 3) |
11164 |
+a,b = string.find("123456789", "345") |
11165 |
+assert(string.sub("123456789", a, b) == "345") |
11166 |
+assert(string.find("1234567890123456789", "345", 3) == 3) |
11167 |
+assert(string.find("1234567890123456789", "345", 4) == 13) |
11168 |
+assert(string.find("1234567890123456789", "346", 4) == nil) |
11169 |
+assert(string.find("1234567890123456789", ".45", -9) == 13) |
11170 |
+assert(string.find("abcdefg", "\0", 5, 1) == nil) |
11171 |
+assert(string.find("", "") == 1) |
11172 |
+assert(string.find("", "", 1) == 1) |
11173 |
+assert(not string.find("", "", 2)) |
11174 |
+assert(string.find('', 'aaa', 1) == nil) |
11175 |
+assert(('alo(.)alo'):find('(.)', 1, 1) == 4) |
11176 |
+ |
11177 |
+assert(string.len("") == 0) |
11178 |
+assert(string.len("\0\0\0") == 3) |
11179 |
+assert(string.len("1234567890") == 10) |
11180 |
+ |
11181 |
+assert(#"" == 0) |
11182 |
+assert(#"\0\0\0" == 3) |
11183 |
+assert(#"1234567890" == 10) |
11184 |
+ |
11185 |
+-- testing string.byte/string.char |
11186 |
+assert(string.byte("a") == 97) |
11187 |
+assert(string.byte("\xe4") > 127) |
11188 |
+assert(string.byte(string.char(255)) == 255) |
11189 |
+assert(string.byte(string.char(0)) == 0) |
11190 |
+assert(string.byte("\0") == 0) |
11191 |
+assert(string.byte("\0\0alo\0x", -1) == string.byte('x')) |
11192 |
+assert(string.byte("ba", 2) == 97) |
11193 |
+assert(string.byte("\n\n", 2, -1) == 10) |
11194 |
+assert(string.byte("\n\n", 2, 2) == 10) |
11195 |
+assert(string.byte("") == nil) |
11196 |
+assert(string.byte("hi", -3) == nil) |
11197 |
+assert(string.byte("hi", 3) == nil) |
11198 |
+assert(string.byte("hi", 9, 10) == nil) |
11199 |
+assert(string.byte("hi", 2, 1) == nil) |
11200 |
+assert(string.char() == "") |
11201 |
+assert(string.char(0, 255, 0) == "\0\255\0") |
11202 |
+assert(string.char(0, string.byte("\xe4"), 0) == "\0\xe4\0") |
11203 |
+assert(string.char(string.byte("\xe4l\0óu", 1, -1)) == "\xe4l\0óu") |
11204 |
+assert(string.char(string.byte("\xe4l\0óu", 1, 0)) == "") |
11205 |
+assert(string.char(string.byte("\xe4l\0óu", -10, 100)) == "\xe4l\0óu") |
11206 |
+ |
11207 |
+assert(string.upper("ab\0c") == "AB\0C") |
11208 |
+assert(string.lower("\0ABCc%$") == "\0abcc%$") |
11209 |
+assert(string.rep('teste', 0) == '') |
11210 |
+assert(string.rep('tés\00tê', 2) == 'tés\0têtés\000tê') |
11211 |
+assert(string.rep('', 10) == '') |
11212 |
+ |
11213 |
+if string.packsize("i") == 4 then |
11214 |
+ -- result length would be 2^31 (int overflow) |
11215 |
+ checkerror("too large", string.rep, 'aa', (1 << 30)) |
11216 |
+ checkerror("too large", string.rep, 'a', (1 << 30), ',') |
11217 |
+end |
11218 |
+ |
11219 |
+-- repetitions with separator |
11220 |
+assert(string.rep('teste', 0, 'xuxu') == '') |
11221 |
+assert(string.rep('teste', 1, 'xuxu') == 'teste') |
11222 |
+assert(string.rep('\1\0\1', 2, '\0\0') == '\1\0\1\0\0\1\0\1') |
11223 |
+assert(string.rep('', 10, '.') == string.rep('.', 9)) |
11224 |
+assert(not pcall(string.rep, "aa", maxi // 2 + 10)) |
11225 |
+assert(not pcall(string.rep, "", maxi // 2 + 10, "aa")) |
11226 |
+ |
11227 |
+assert(string.reverse"" == "") |
11228 |
+assert(string.reverse"\0\1\2\3" == "\3\2\1\0") |
11229 |
+assert(string.reverse"\0001234" == "4321\0") |
11230 |
+ |
11231 |
+for i=0,30 do assert(string.len(string.rep('a', i)) == i) end |
11232 |
+ |
11233 |
+assert(type(tostring(nil)) == 'string') |
11234 |
+assert(type(tostring(12)) == 'string') |
11235 |
+assert(string.find(tostring{}, 'table:')) |
11236 |
+assert(string.find(tostring(print), 'function:')) |
11237 |
+assert(#tostring('\0') == 1) |
11238 |
+assert(tostring(true) == "true") |
11239 |
+assert(tostring(false) == "false") |
11240 |
+assert(tostring(-1203) == "-1203") |
11241 |
+assert(tostring(1203.125) == "1203.125") |
11242 |
+assert(tostring(-0.5) == "-0.5") |
11243 |
+assert(tostring(-32767) == "-32767") |
11244 |
+if math.tointeger(2147483647) then -- no overflow? (32 bits) |
11245 |
+ assert(tostring(-2147483647) == "-2147483647") |
11246 |
+end |
11247 |
+if math.tointeger(4611686018427387904) then -- no overflow? (64 bits) |
11248 |
+ assert(tostring(4611686018427387904) == "4611686018427387904") |
11249 |
+ assert(tostring(-4611686018427387904) == "-4611686018427387904") |
11250 |
+end |
11251 |
+ |
11252 |
+if tostring(0.0) == "0.0" then -- "standard" coercion float->string |
11253 |
+ assert('' .. 12 == '12' and 12.0 .. '' == '12.0') |
11254 |
+ assert(tostring(-1203 + 0.0) == "-1203.0") |
11255 |
+else -- compatible coercion |
11256 |
+ assert(tostring(0.0) == "0") |
11257 |
+ assert('' .. 12 == '12' and 12.0 .. '' == '12') |
11258 |
+ assert(tostring(-1203 + 0.0) == "-1203") |
11259 |
+end |
11260 |
+ |
11261 |
+ |
11262 |
+x = '"ílo"\n\\' |
11263 |
+assert(string.format('%q%s', x, x) == '"\\"ílo\\"\\\n\\\\""ílo"\n\\') |
11264 |
+assert(string.format('%q', "\0") == [["\0"]]) |
11265 |
+assert(load(string.format('return %q', x))() == x) |
11266 |
+x = "\0\1\0023\5\0009" |
11267 |
+assert(load(string.format('return %q', x))() == x) |
11268 |
+assert(string.format("\0%c\0%c%x\0", string.byte("\xe4"), string.byte("b"), 140) == |
11269 |
+ "\0\xe4\0b8c\0") |
11270 |
+assert(string.format('') == "") |
11271 |
+assert(string.format("%c",34)..string.format("%c",48)..string.format("%c",90)..string.format("%c",100) == |
11272 |
+ string.format("%c%c%c%c", 34, 48, 90, 100)) |
11273 |
+assert(string.format("%s\0 is not \0%s", 'not be', 'be') == 'not be\0 is not \0be') |
11274 |
+assert(string.format("%%%d %010d", 10, 23) == "%10 0000000023") |
11275 |
+assert(tonumber(string.format("%f", 10.3)) == 10.3) |
11276 |
+x = string.format('"%-50s"', 'a') |
11277 |
+assert(#x == 52) |
11278 |
+assert(string.sub(x, 1, 4) == '"a ') |
11279 |
+ |
11280 |
+assert(string.format("-%.20s.20s", string.rep("%", 2000)) == |
11281 |
+ "-"..string.rep("%", 20)..".20s") |
11282 |
+assert(string.format('"-%20s.20s"', string.rep("%", 2000)) == |
11283 |
+ string.format("%q", "-"..string.rep("%", 2000)..".20s")) |
11284 |
+ |
11285 |
+do |
11286 |
+ local function checkQ (v) |
11287 |
+ local s = string.format("%q", v) |
11288 |
+ local nv = load("return " .. s)() |
11289 |
+ assert(v == nv and math.type(v) == math.type(nv)) |
11290 |
+ end |
11291 |
+ checkQ("\0\0\1\255\u{234}") |
11292 |
+ checkQ(math.maxinteger) |
11293 |
+ checkQ(math.mininteger) |
11294 |
+ checkQ(math.pi) |
11295 |
+ checkQ(0.1) |
11296 |
+ checkQ(true) |
11297 |
+ checkQ(nil) |
11298 |
+ checkQ(false) |
11299 |
+ checkerror("no literal", string.format, "%q", {}) |
11300 |
+end |
11301 |
+ |
11302 |
+assert(string.format("\0%s\0", "\0\0\1") == "\0\0\0\1\0") |
11303 |
+checkerror("contains zeros", string.format, "%10s", "\0") |
11304 |
+ |
11305 |
+-- format x tostring |
11306 |
+assert(string.format("%s %s", nil, true) == "nil true") |
11307 |
+assert(string.format("%s %.4s", false, true) == "false true") |
11308 |
+assert(string.format("%.3s %.3s", false, true) == "fal tru") |
11309 |
+local m = setmetatable({}, {__tostring = function () return "hello" end, |
11310 |
+ __name = "hi"}) |
11311 |
+assert(string.format("%s %.10s", m, m) == "hello hello") |
11312 |
+getmetatable(m).__tostring = nil -- will use '__name' from now on |
11313 |
+assert(string.format("%.4s", m) == "hi: ") |
11314 |
+ |
11315 |
+getmetatable(m).__tostring = function () return {} end |
11316 |
+checkerror("'__tostring' must return a string", tostring, m) |
11317 |
+ |
11318 |
+ |
11319 |
+assert(string.format("%x", 0.0) == "0") |
11320 |
+assert(string.format("%02x", 0.0) == "00") |
11321 |
+assert(string.format("%08X", 0xFFFFFFFF) == "FFFFFFFF") |
11322 |
+assert(string.format("%+08d", 31501) == "+0031501") |
11323 |
+assert(string.format("%+08d", -30927) == "-0030927") |
11324 |
+ |
11325 |
+ |
11326 |
+do -- longest number that can be formatted |
11327 |
+ local i = 1 |
11328 |
+ local j = 10000 |
11329 |
+ while i + 1 < j do -- binary search for maximum finite float |
11330 |
+ local m = (i + j) // 2 |
11331 |
+ if 10^m < math.huge then i = m else j = m end |
11332 |
+ end |
11333 |
+ assert(10^i < math.huge and 10^j == math.huge) |
11334 |
+ local s = string.format('%.99f', -(10^i)) |
11335 |
+ assert(string.len(s) >= i + 101) |
11336 |
+ assert(tonumber(s) == -(10^i)) |
11337 |
+end |
11338 |
+ |
11339 |
+ |
11340 |
+-- testing large numbers for format |
11341 |
+do -- assume at least 32 bits |
11342 |
+ local max, min = 0x7fffffff, -0x80000000 -- "large" for 32 bits |
11343 |
+ assert(string.sub(string.format("%8x", -1), -8) == "ffffffff") |
11344 |
+ assert(string.format("%x", max) == "7fffffff") |
11345 |
+ assert(string.sub(string.format("%x", min), -8) == "80000000") |
11346 |
+ assert(string.format("%d", max) == "2147483647") |
11347 |
+ assert(string.format("%d", min) == "-2147483648") |
11348 |
+ assert(string.format("%u", 0xffffffff) == "4294967295") |
11349 |
+ assert(string.format("%o", 0xABCD) == "125715") |
11350 |
+ |
11351 |
+ max, min = 0x7fffffffffffffff, -0x8000000000000000 |
11352 |
+ if max > 2.0^53 then -- only for 64 bits |
11353 |
+ assert(string.format("%x", (2^52 | 0) - 1) == "fffffffffffff") |
11354 |
+ assert(string.format("0x%8X", 0x8f000003) == "0x8F000003") |
11355 |
+ assert(string.format("%d", 2^53) == "9007199254740992") |
11356 |
+ assert(string.format("%i", -2^53) == "-9007199254740992") |
11357 |
+ assert(string.format("%x", max) == "7fffffffffffffff") |
11358 |
+ assert(string.format("%x", min) == "8000000000000000") |
11359 |
+ assert(string.format("%d", max) == "9223372036854775807") |
11360 |
+ assert(string.format("%d", min) == "-9223372036854775808") |
11361 |
+ assert(string.format("%u", ~(-1 << 64)) == "18446744073709551615") |
11362 |
+ assert(tostring(1234567890123) == '1234567890123') |
11363 |
+ end |
11364 |
+end |
11365 |
+ |
11366 |
+ |
11367 |
+do print("testing 'format %a %A'") |
11368 |
+ local function matchhexa (n) |
11369 |
+ local s = string.format("%a", n) |
11370 |
+ -- result matches ISO C requirements |
11371 |
+ assert(string.find(s, "^%-?0x[1-9a-f]%.?[0-9a-f]*p[-+]?%d+$")) |
11372 |
+ assert(tonumber(s) == n) -- and has full precision |
11373 |
+ s = string.format("%A", n) |
11374 |
+ assert(string.find(s, "^%-?0X[1-9A-F]%.?[0-9A-F]*P[-+]?%d+$")) |
11375 |
+ assert(tonumber(s) == n) |
11376 |
+ end |
11377 |
+ for _, n in ipairs{0.1, -0.1, 1/3, -1/3, 1e30, -1e30, |
11378 |
+ -45/247, 1, -1, 2, -2, 3e-20, -3e-20} do |
11379 |
+ matchhexa(n) |
11380 |
+ end |
11381 |
+ |
11382 |
+ assert(string.find(string.format("%A", 0.0), "^0X0%.?0?P%+?0$")) |
11383 |
+ assert(string.find(string.format("%a", -0.0), "^%-0x0%.?0?p%+?0$")) |
11384 |
+ |
11385 |
+ if not _port then -- test inf, -inf, NaN, and -0.0 |
11386 |
+ assert(string.find(string.format("%a", 1/0), "^inf")) |
11387 |
+ assert(string.find(string.format("%A", -1/0), "^%-INF")) |
11388 |
+ assert(string.find(string.format("%a", 0/0), "^%-?nan")) |
11389 |
+ assert(string.find(string.format("%a", -0.0), "^%-0x0")) |
11390 |
+ end |
11391 |
+ |
11392 |
+ if not pcall(string.format, "%.3a", 0) then |
11393 |
+ (Message or print)("\n >>> modifiers for format '%a' not available <<<\n") |
11394 |
+ else |
11395 |
+ assert(string.find(string.format("%+.2A", 12), "^%+0X%x%.%x0P%+?%d$")) |
11396 |
+ assert(string.find(string.format("%.4A", -12), "^%-0X%x%.%x000P%+?%d$")) |
11397 |
+ end |
11398 |
+end |
11399 |
+ |
11400 |
+ |
11401 |
+-- errors in format |
11402 |
+ |
11403 |
+local function check (fmt, msg) |
11404 |
+ checkerror(msg, string.format, fmt, 10) |
11405 |
+end |
11406 |
+ |
11407 |
+local aux = string.rep('0', 600) |
11408 |
+check("%100.3d", "too long") |
11409 |
+check("%1"..aux..".3d", "too long") |
11410 |
+check("%1.100d", "too long") |
11411 |
+check("%10.1"..aux.."004d", "too long") |
11412 |
+check("%t", "invalid option") |
11413 |
+check("%"..aux.."d", "repeated flags") |
11414 |
+check("%d %d", "no value") |
11415 |
+ |
11416 |
+ |
11417 |
+assert(load("return 1\n--comment without ending EOL")() == 1) |
11418 |
+ |
11419 |
+ |
11420 |
+checkerror("table expected", table.concat, 3) |
11421 |
+assert(table.concat{} == "") |
11422 |
+assert(table.concat({}, 'x') == "") |
11423 |
+assert(table.concat({'\0', '\0\1', '\0\1\2'}, '.\0.') == "\0.\0.\0\1.\0.\0\1\2") |
11424 |
+local a = {}; for i=1,300 do a[i] = "xuxu" end |
11425 |
+assert(table.concat(a, "123").."123" == string.rep("xuxu123", 300)) |
11426 |
+assert(table.concat(a, "b", 20, 20) == "xuxu") |
11427 |
+assert(table.concat(a, "", 20, 21) == "xuxuxuxu") |
11428 |
+assert(table.concat(a, "x", 22, 21) == "") |
11429 |
+assert(table.concat(a, "3", 299) == "xuxu3xuxu") |
11430 |
+assert(table.concat({}, "x", maxi, maxi - 1) == "") |
11431 |
+assert(table.concat({}, "x", mini + 1, mini) == "") |
11432 |
+assert(table.concat({}, "x", maxi, mini) == "") |
11433 |
+assert(table.concat({[maxi] = "alo"}, "x", maxi, maxi) == "alo") |
11434 |
+assert(table.concat({[maxi] = "alo", [maxi - 1] = "y"}, "-", maxi - 1, maxi) |
11435 |
+ == "y-alo") |
11436 |
+ |
11437 |
+assert(not pcall(table.concat, {"a", "b", {}})) |
11438 |
+ |
11439 |
+a = {"a","b","c"} |
11440 |
+assert(table.concat(a, ",", 1, 0) == "") |
11441 |
+assert(table.concat(a, ",", 1, 1) == "a") |
11442 |
+assert(table.concat(a, ",", 1, 2) == "a,b") |
11443 |
+assert(table.concat(a, ",", 2) == "b,c") |
11444 |
+assert(table.concat(a, ",", 3) == "c") |
11445 |
+assert(table.concat(a, ",", 4) == "") |
11446 |
+ |
11447 |
+if not _port then |
11448 |
+ |
11449 |
+ local locales = { "ptb", "pt_BR.iso88591", "ISO-8859-1" } |
11450 |
+ local function trylocale (w) |
11451 |
+ for i = 1, #locales do |
11452 |
+ if os.setlocale(locales[i], w) then |
11453 |
+ print(string.format("'%s' locale set to '%s'", w, locales[i])) |
11454 |
+ return locales[i] |
11455 |
+ end |
11456 |
+ end |
11457 |
+ print(string.format("'%s' locale not found", w)) |
11458 |
+ return false |
11459 |
+ end |
11460 |
+ |
11461 |
+ if trylocale("collate") then |
11462 |
+ assert("alo" < "álo" and "álo" < "amo") |
11463 |
+ end |
11464 |
+ |
11465 |
+ if trylocale("ctype") then |
11466 |
+ assert(string.gsub("áéíóú", "%a", "x") == "xxxxx") |
11467 |
+ assert(string.gsub("áÁéÉ", "%l", "x") == "xÁxÉ") |
11468 |
+ assert(string.gsub("áÁéÉ", "%u", "x") == "áxéx") |
11469 |
+ assert(string.upper"áÁé{xuxu}ção" == "ÁÁÉ{XUXU}ÇÃO") |
11470 |
+ end |
11471 |
+ |
11472 |
+ os.setlocale("C") |
11473 |
+ assert(os.setlocale() == 'C') |
11474 |
+ assert(os.setlocale(nil, "numeric") == 'C') |
11475 |
+ |
11476 |
+end |
11477 |
+ |
11478 |
+ |
11479 |
+-- bug in Lua 5.3.2 |
11480 |
+-- 'gmatch' iterator does not work across coroutines |
11481 |
+do |
11482 |
+ local f = string.gmatch("1 2 3 4 5", "%d+") |
11483 |
+ assert(f() == "1") |
11484 |
+ co = coroutine.wrap(f) |
11485 |
+ assert(co() == "2") |
11486 |
+end |
11487 |
+ |
11488 |
+print('OK') |
11489 |
+ |
11490 |
|
11491 |
diff --git a/tests/tpack.lua b/tests/tpack.lua |
11492 |
new file mode 100644 |
11493 |
index 0000000..96bb4da |
11494 |
--- /dev/null |
11495 |
+++ b/tests/tpack.lua |
11496 |
@@ -0,0 +1,322 @@ |
11497 |
+-- $Id: tpack.lua,v 1.13 2016/11/07 13:11:28 roberto Exp $ |
11498 |
+-- See Copyright Notice in file all.lua |
11499 |
+ |
11500 |
+local pack = string.pack |
11501 |
+local packsize = string.packsize |
11502 |
+local unpack = string.unpack |
11503 |
+ |
11504 |
+print "testing pack/unpack" |
11505 |
+ |
11506 |
+-- maximum size for integers |
11507 |
+local NB = 16 |
11508 |
+ |
11509 |
+local sizeshort = packsize("h") |
11510 |
+local sizeint = packsize("i") |
11511 |
+local sizelong = packsize("l") |
11512 |
+local sizesize_t = packsize("T") |
11513 |
+local sizeLI = packsize("j") |
11514 |
+local sizefloat = packsize("f") |
11515 |
+local sizedouble = packsize("d") |
11516 |
+local sizenumber = packsize("n") |
11517 |
+local little = (pack("i2", 1) == "\1\0") |
11518 |
+local align = packsize("!xXi16") |
11519 |
+ |
11520 |
+assert(1 <= sizeshort and sizeshort <= sizeint and sizeint <= sizelong and |
11521 |
+ sizefloat <= sizedouble) |
11522 |
+ |
11523 |
+print("platform:") |
11524 |
+print(string.format( |
11525 |
+ "\tshort %d, int %d, long %d, size_t %d, float %d, double %d,\n\z |
11526 |
+ \tlua Integer %d, lua Number %d", |
11527 |
+ sizeshort, sizeint, sizelong, sizesize_t, sizefloat, sizedouble, |
11528 |
+ sizeLI, sizenumber)) |
11529 |
+print("\t" .. (little and "little" or "big") .. " endian") |
11530 |
+print("\talignment: " .. align) |
11531 |
+ |
11532 |
+ |
11533 |
+-- check errors in arguments |
11534 |
+function checkerror (msg, f, ...) |
11535 |
+ local status, err = pcall(f, ...) |
11536 |
+ -- print(status, err, msg) |
11537 |
+ assert(not status and string.find(err, msg)) |
11538 |
+end |
11539 |
+ |
11540 |
+-- minimum behavior for integer formats |
11541 |
+assert(unpack("B", pack("B", 0xff)) == 0xff) |
11542 |
+assert(unpack("b", pack("b", 0x7f)) == 0x7f) |
11543 |
+assert(unpack("b", pack("b", -0x80)) == -0x80) |
11544 |
+ |
11545 |
+assert(unpack("H", pack("H", 0xffff)) == 0xffff) |
11546 |
+assert(unpack("h", pack("h", 0x7fff)) == 0x7fff) |
11547 |
+assert(unpack("h", pack("h", -0x8000)) == -0x8000) |
11548 |
+ |
11549 |
+assert(unpack("L", pack("L", 0xffffffff)) == 0xffffffff) |
11550 |
+assert(unpack("l", pack("l", 0x7fffffff)) == 0x7fffffff) |
11551 |
+assert(unpack("l", pack("l", -0x80000000)) == -0x80000000) |
11552 |
+ |
11553 |
+ |
11554 |
+for i = 1, NB do |
11555 |
+ -- small numbers with signal extension ("\xFF...") |
11556 |
+ local s = string.rep("\xff", i) |
11557 |
+ assert(pack("i" .. i, -1) == s) |
11558 |
+ assert(packsize("i" .. i) == #s) |
11559 |
+ assert(unpack("i" .. i, s) == -1) |
11560 |
+ |
11561 |
+ -- small unsigned number ("\0...\xAA") |
11562 |
+ s = "\xAA" .. string.rep("\0", i - 1) |
11563 |
+ assert(pack("<I" .. i, 0xAA) == s) |
11564 |
+ assert(unpack("<I" .. i, s) == 0xAA) |
11565 |
+ assert(pack(">I" .. i, 0xAA) == s:reverse()) |
11566 |
+ assert(unpack(">I" .. i, s:reverse()) == 0xAA) |
11567 |
+end |
11568 |
+ |
11569 |
+do |
11570 |
+ local lnum = 0x13121110090807060504030201 |
11571 |
+ local s = pack("<j", lnum) |
11572 |
+ assert(unpack("<j", s) == lnum) |
11573 |
+ assert(unpack("<i" .. sizeLI + 1, s .. "\0") == lnum) |
11574 |
+ assert(unpack("<i" .. sizeLI + 1, s .. "\0") == lnum) |
11575 |
+ |
11576 |
+ for i = sizeLI + 1, NB do |
11577 |
+ local s = pack("<j", -lnum) |
11578 |
+ assert(unpack("<j", s) == -lnum) |
11579 |
+ -- strings with (correct) extra bytes |
11580 |
+ assert(unpack("<i" .. i, s .. ("\xFF"):rep(i - sizeLI)) == -lnum) |
11581 |
+ assert(unpack(">i" .. i, ("\xFF"):rep(i - sizeLI) .. s:reverse()) == -lnum) |
11582 |
+ assert(unpack("<I" .. i, s .. ("\0"):rep(i - sizeLI)) == -lnum) |
11583 |
+ |
11584 |
+ -- overflows |
11585 |
+ checkerror("does not fit", unpack, "<I" .. i, ("\x00"):rep(i - 1) .. "\1") |
11586 |
+ checkerror("does not fit", unpack, ">i" .. i, "\1" .. ("\x00"):rep(i - 1)) |
11587 |
+ end |
11588 |
+end |
11589 |
+ |
11590 |
+for i = 1, sizeLI do |
11591 |
+ local lstr = "\1\2\3\4\5\6\7\8\9\10\11\12\13" |
11592 |
+ local lnum = 0x13121110090807060504030201 |
11593 |
+ local n = lnum & (~(-1 << (i * 8))) |
11594 |
+ local s = string.sub(lstr, 1, i) |
11595 |
+ assert(pack("<i" .. i, n) == s) |
11596 |
+ assert(pack(">i" .. i, n) == s:reverse()) |
11597 |
+ assert(unpack(">i" .. i, s:reverse()) == n) |
11598 |
+end |
11599 |
+ |
11600 |
+-- sign extension |
11601 |
+do |
11602 |
+ local u = 0xf0 |
11603 |
+ for i = 1, sizeLI - 1 do |
11604 |
+ assert(unpack("<i"..i, "\xf0"..("\xff"):rep(i - 1)) == -16) |
11605 |
+ assert(unpack(">I"..i, "\xf0"..("\xff"):rep(i - 1)) == u) |
11606 |
+ u = u * 256 + 0xff |
11607 |
+ end |
11608 |
+end |
11609 |
+ |
11610 |
+-- mixed endianness |
11611 |
+do |
11612 |
+ assert(pack(">i2 <i2", 10, 20) == "\0\10\20\0") |
11613 |
+ local a, b = unpack("<i2 >i2", "\10\0\0\20") |
11614 |
+ assert(a == 10 and b == 20) |
11615 |
+ assert(pack("=i4", 2001) == pack("i4", 2001)) |
11616 |
+end |
11617 |
+ |
11618 |
+print("testing invalid formats") |
11619 |
+ |
11620 |
+checkerror("out of limits", pack, "i0", 0) |
11621 |
+checkerror("out of limits", pack, "i" .. NB + 1, 0) |
11622 |
+checkerror("out of limits", pack, "!" .. NB + 1, 0) |
11623 |
+checkerror("%(17%) out of limits %[1,16%]", pack, "Xi" .. NB + 1) |
11624 |
+checkerror("invalid format option 'r'", pack, "i3r", 0) |
11625 |
+checkerror("16%-byte integer", unpack, "i16", string.rep('\3', 16)) |
11626 |
+checkerror("not power of 2", pack, "!4i3", 0); |
11627 |
+checkerror("missing size", pack, "c", "") |
11628 |
+checkerror("variable%-length format", packsize, "s") |
11629 |
+checkerror("variable%-length format", packsize, "z") |
11630 |
+ |
11631 |
+-- overflow in option size (error will be in digit after limit) |
11632 |
+checkerror("invalid format", packsize, "c1" .. string.rep("0", 40)) |
11633 |
+ |
11634 |
+if packsize("i") == 4 then |
11635 |
+ -- result would be 2^31 (2^3 repetitions of 2^28 strings) |
11636 |
+ local s = string.rep("c268435456", 2^3) |
11637 |
+ checkerror("too large", packsize, s) |
11638 |
+ -- one less is OK |
11639 |
+ s = string.rep("c268435456", 2^3 - 1) .. "c268435455" |
11640 |
+ assert(packsize(s) == 0x7fffffff) |
11641 |
+end |
11642 |
+ |
11643 |
+-- overflow in packing |
11644 |
+for i = 1, sizeLI - 1 do |
11645 |
+ local umax = (1 << (i * 8)) - 1 |
11646 |
+ local max = umax >> 1 |
11647 |
+ local min = ~max |
11648 |
+ checkerror("overflow", pack, "<I" .. i, -1) |
11649 |
+ checkerror("overflow", pack, "<I" .. i, min) |
11650 |
+ checkerror("overflow", pack, ">I" .. i, umax + 1) |
11651 |
+ |
11652 |
+ checkerror("overflow", pack, ">i" .. i, umax) |
11653 |
+ checkerror("overflow", pack, ">i" .. i, max + 1) |
11654 |
+ checkerror("overflow", pack, "<i" .. i, min - 1) |
11655 |
+ |
11656 |
+ assert(unpack(">i" .. i, pack(">i" .. i, max)) == max) |
11657 |
+ assert(unpack("<i" .. i, pack("<i" .. i, min)) == min) |
11658 |
+ assert(unpack(">I" .. i, pack(">I" .. i, umax)) == umax) |
11659 |
+end |
11660 |
+ |
11661 |
+-- Lua integer size |
11662 |
+assert(unpack(">j", pack(">j", math.maxinteger)) == math.maxinteger) |
11663 |
+assert(unpack("<j", pack("<j", math.mininteger)) == math.mininteger) |
11664 |
+assert(unpack("<J", pack("<j", -1)) == -1) -- maximum unsigned integer |
11665 |
+ |
11666 |
+if little then |
11667 |
+ assert(pack("f", 24) == pack("<f", 24)) |
11668 |
+else |
11669 |
+ assert(pack("f", 24) == pack(">f", 24)) |
11670 |
+end |
11671 |
+ |
11672 |
+print "testing pack/unpack of floating-point numbers" |
11673 |
+ |
11674 |
+for _, n in ipairs{0, -1.1, 1.9, 1/0, -1/0, 1e20, -1e20, 0.1, 2000.7} do |
11675 |
+ assert(unpack("n", pack("n", n)) == n) |
11676 |
+ assert(unpack("<n", pack("<n", n)) == n) |
11677 |
+ assert(unpack(">n", pack(">n", n)) == n) |
11678 |
+ assert(pack("<f", n) == pack(">f", n):reverse()) |
11679 |
+ assert(pack(">d", n) == pack("<d", n):reverse()) |
11680 |
+end |
11681 |
+ |
11682 |
+-- for non-native precisions, test only with "round" numbers |
11683 |
+for _, n in ipairs{0, -1.5, 1/0, -1/0, 1e10, -1e9, 0.5, 2000.25} do |
11684 |
+ assert(unpack("<f", pack("<f", n)) == n) |
11685 |
+ assert(unpack(">f", pack(">f", n)) == n) |
11686 |
+ assert(unpack("<d", pack("<d", n)) == n) |
11687 |
+ assert(unpack(">d", pack(">d", n)) == n) |
11688 |
+end |
11689 |
+ |
11690 |
+print "testing pack/unpack of strings" |
11691 |
+do |
11692 |
+ local s = string.rep("abc", 1000) |
11693 |
+ assert(pack("zB", s, 247) == s .. "\0\xF7") |
11694 |
+ local s1, b = unpack("zB", s .. "\0\xF9") |
11695 |
+ assert(b == 249 and s1 == s) |
11696 |
+ s1 = pack("s", s) |
11697 |
+ assert(unpack("s", s1) == s) |
11698 |
+ |
11699 |
+ checkerror("does not fit", pack, "s1", s) |
11700 |
+ |
11701 |
+ checkerror("contains zeros", pack, "z", "alo\0"); |
11702 |
+ |
11703 |
+ for i = 2, NB do |
11704 |
+ local s1 = pack("s" .. i, s) |
11705 |
+ assert(unpack("s" .. i, s1) == s and #s1 == #s + i) |
11706 |
+ end |
11707 |
+end |
11708 |
+ |
11709 |
+do |
11710 |
+ local x = pack("s", "alo") |
11711 |
+ checkerror("too short", unpack, "s", x:sub(1, -2)) |
11712 |
+ checkerror("too short", unpack, "c5", "abcd") |
11713 |
+ checkerror("out of limits", pack, "s100", "alo") |
11714 |
+end |
11715 |
+ |
11716 |
+do |
11717 |
+ assert(pack("c0", "") == "") |
11718 |
+ assert(packsize("c0") == 0) |
11719 |
+ assert(unpack("c0", "") == "") |
11720 |
+ assert(pack("<! c3", "abc") == "abc") |
11721 |
+ assert(packsize("<! c3") == 3) |
11722 |
+ assert(pack(">!4 c6", "abcdef") == "abcdef") |
11723 |
+ assert(pack("c3", "123") == "123") |
11724 |
+ assert(pack("c0", "") == "") |
11725 |
+ assert(pack("c8", "123456") == "123456\0\0") |
11726 |
+ assert(pack("c88", "") == string.rep("\0", 88)) |
11727 |
+ assert(pack("c188", "ab") == "ab" .. string.rep("\0", 188 - 2)) |
11728 |
+ local a, b, c = unpack("!4 z c3", "abcdefghi\0xyz") |
11729 |
+ assert(a == "abcdefghi" and b == "xyz" and c == 14) |
11730 |
+ checkerror("longer than", pack, "c3", "1234") |
11731 |
+end |
11732 |
+ |
11733 |
+ |
11734 |
+-- testing multiple types and sequence |
11735 |
+do |
11736 |
+ local x = pack("<b h b f d f n i", 1, 2, 3, 4, 5, 6, 7, 8) |
11737 |
+ assert(#x == packsize("<b h b f d f n i")) |
11738 |
+ local a, b, c, d, e, f, g, h = unpack("<b h b f d f n i", x) |
11739 |
+ assert(a == 1 and b == 2 and c == 3 and d == 4 and e == 5 and f == 6 and |
11740 |
+ g == 7 and h == 8) |
11741 |
+end |
11742 |
+ |
11743 |
+print "testing alignment" |
11744 |
+do |
11745 |
+ assert(pack(" < i1 i2 ", 2, 3) == "\2\3\0") -- no alignment by default |
11746 |
+ local x = pack(">!8 b Xh i4 i8 c1 Xi8", -12, 100, 200, "\xEC") |
11747 |
+ assert(#x == packsize(">!8 b Xh i4 i8 c1 Xi8")) |
11748 |
+ assert(x == "\xf4" .. "\0\0\0" .. |
11749 |
+ "\0\0\0\100" .. |
11750 |
+ "\0\0\0\0\0\0\0\xC8" .. |
11751 |
+ "\xEC" .. "\0\0\0\0\0\0\0") |
11752 |
+ local a, b, c, d, pos = unpack(">!8 c1 Xh i4 i8 b Xi8 XI XH", x) |
11753 |
+ assert(a == "\xF4" and b == 100 and c == 200 and d == -20 and (pos - 1) == #x) |
11754 |
+ |
11755 |
+ x = pack(">!4 c3 c4 c2 z i4 c5 c2 Xi4", |
11756 |
+ "abc", "abcd", "xz", "hello", 5, "world", "xy") |
11757 |
+ assert(x == "abcabcdxzhello\0\0\0\0\0\5worldxy\0") |
11758 |
+ local a, b, c, d, e, f, g, pos = unpack(">!4 c3 c4 c2 z i4 c5 c2 Xh Xi4", x) |
11759 |
+ assert(a == "abc" and b == "abcd" and c == "xz" and d == "hello" and |
11760 |
+ e == 5 and f == "world" and g == "xy" and (pos - 1) % 4 == 0) |
11761 |
+ |
11762 |
+ x = pack(" b b Xd b Xb x", 1, 2, 3) |
11763 |
+ assert(packsize(" b b Xd b Xb x") == 4) |
11764 |
+ assert(x == "\1\2\3\0") |
11765 |
+ a, b, c, pos = unpack("bbXdb", x) |
11766 |
+ assert(a == 1 and b == 2 and c == 3 and pos == #x) |
11767 |
+ |
11768 |
+ -- only alignment |
11769 |
+ assert(packsize("!8 xXi8") == 8) |
11770 |
+ local pos = unpack("!8 xXi8", "0123456701234567"); assert(pos == 9) |
11771 |
+ assert(packsize("!8 xXi2") == 2) |
11772 |
+ local pos = unpack("!8 xXi2", "0123456701234567"); assert(pos == 3) |
11773 |
+ assert(packsize("!2 xXi2") == 2) |
11774 |
+ local pos = unpack("!2 xXi2", "0123456701234567"); assert(pos == 3) |
11775 |
+ assert(packsize("!2 xXi8") == 2) |
11776 |
+ local pos = unpack("!2 xXi8", "0123456701234567"); assert(pos == 3) |
11777 |
+ assert(packsize("!16 xXi16") == 16) |
11778 |
+ local pos = unpack("!16 xXi16", "0123456701234567"); assert(pos == 17) |
11779 |
+ |
11780 |
+ checkerror("invalid next option", pack, "X") |
11781 |
+ checkerror("invalid next option", unpack, "XXi", "") |
11782 |
+ checkerror("invalid next option", unpack, "X i", "") |
11783 |
+ checkerror("invalid next option", pack, "Xc1") |
11784 |
+end |
11785 |
+ |
11786 |
+do -- testing initial position |
11787 |
+ local x = pack("i4i4i4i4", 1, 2, 3, 4) |
11788 |
+ for pos = 1, 16, 4 do |
11789 |
+ local i, p = unpack("i4", x, pos) |
11790 |
+ assert(i == pos//4 + 1 and p == pos + 4) |
11791 |
+ end |
11792 |
+ |
11793 |
+ -- with alignment |
11794 |
+ for pos = 0, 12 do -- will always round position to power of 2 |
11795 |
+ local i, p = unpack("!4 i4", x, pos + 1) |
11796 |
+ assert(i == (pos + 3)//4 + 1 and p == i*4 + 1) |
11797 |
+ end |
11798 |
+ |
11799 |
+ -- negative indices |
11800 |
+ local i, p = unpack("!4 i4", x, -4) |
11801 |
+ assert(i == 4 and p == 17) |
11802 |
+ local i, p = unpack("!4 i4", x, -7) |
11803 |
+ assert(i == 4 and p == 17) |
11804 |
+ local i, p = unpack("!4 i4", x, -#x) |
11805 |
+ assert(i == 1 and p == 5) |
11806 |
+ |
11807 |
+ -- limits |
11808 |
+ for i = 1, #x + 1 do |
11809 |
+ assert(unpack("c0", x, i) == "") |
11810 |
+ end |
11811 |
+ checkerror("out of string", unpack, "c0", x, 0) |
11812 |
+ checkerror("out of string", unpack, "c0", x, #x + 2) |
11813 |
+ checkerror("out of string", unpack, "c0", x, -(#x + 1)) |
11814 |
+ |
11815 |
+end |
11816 |
+ |
11817 |
+print "OK" |
11818 |
+ |
11819 |
|
11820 |
diff --git a/tests/utf8.lua b/tests/utf8.lua |
11821 |
new file mode 100644 |
11822 |
index 0000000..ebc190b |
11823 |
--- /dev/null |
11824 |
+++ b/tests/utf8.lua |
11825 |
@@ -0,0 +1,210 @@ |
11826 |
+-- $Id: utf8.lua,v 1.12 2016/11/07 13:11:28 roberto Exp $ |
11827 |
+-- See Copyright Notice in file all.lua |
11828 |
+ |
11829 |
+print "testing UTF-8 library" |
11830 |
+ |
11831 |
+local utf8 = require'utf8' |
11832 |
+ |
11833 |
+ |
11834 |
+local function checkerror (msg, f, ...) |
11835 |
+ local s, err = pcall(f, ...) |
11836 |
+ assert(not s and string.find(err, msg)) |
11837 |
+end |
11838 |
+ |
11839 |
+ |
11840 |
+local function len (s) |
11841 |
+ return #string.gsub(s, "[\x80-\xBF]", "") |
11842 |
+end |
11843 |
+ |
11844 |
+ |
11845 |
+local justone = "^" .. utf8.charpattern .. "$" |
11846 |
+ |
11847 |
+-- 't' is the list of codepoints of 's' |
11848 |
+local function checksyntax (s, t) |
11849 |
+ local ts = {"return '"} |
11850 |
+ for i = 1, #t do ts[i + 1] = string.format("\\u{%x}", t[i]) end |
11851 |
+ ts[#t + 2] = "'" |
11852 |
+ ts = table.concat(ts) |
11853 |
+ assert(assert(load(ts))() == s) |
11854 |
+end |
11855 |
+ |
11856 |
+assert(utf8.offset("alo", 5) == nil) |
11857 |
+assert(utf8.offset("alo", -4) == nil) |
11858 |
+ |
11859 |
+-- 't' is the list of codepoints of 's' |
11860 |
+local function check (s, t) |
11861 |
+ local l = utf8.len(s) |
11862 |
+ assert(#t == l and len(s) == l) |
11863 |
+ assert(utf8.char(table.unpack(t)) == s) |
11864 |
+ |
11865 |
+ assert(utf8.offset(s, 0) == 1) |
11866 |
+ |
11867 |
+ checksyntax(s, t) |
11868 |
+ |
11869 |
+ local t1 = {utf8.codepoint(s, 1, -1)} |
11870 |
+ assert(#t == #t1) |
11871 |
+ for i = 1, #t do assert(t[i] == t1[i]) end |
11872 |
+ |
11873 |
+ for i = 1, l do |
11874 |
+ local pi = utf8.offset(s, i) -- position of i-th char |
11875 |
+ local pi1 = utf8.offset(s, 2, pi) -- position of next char |
11876 |
+ assert(string.find(string.sub(s, pi, pi1 - 1), justone)) |
11877 |
+ assert(utf8.offset(s, -1, pi1) == pi) |
11878 |
+ assert(utf8.offset(s, i - l - 1) == pi) |
11879 |
+ assert(pi1 - pi == #utf8.char(utf8.codepoint(s, pi))) |
11880 |
+ for j = pi, pi1 - 1 do |
11881 |
+ assert(utf8.offset(s, 0, j) == pi) |
11882 |
+ end |
11883 |
+ for j = pi + 1, pi1 - 1 do |
11884 |
+ assert(not utf8.len(s, j)) |
11885 |
+ end |
11886 |
+ assert(utf8.len(s, pi, pi) == 1) |
11887 |
+ assert(utf8.len(s, pi, pi1 - 1) == 1) |
11888 |
+ assert(utf8.len(s, pi) == l - i + 1) |
11889 |
+ assert(utf8.len(s, pi1) == l - i) |
11890 |
+ assert(utf8.len(s, 1, pi) == i) |
11891 |
+ end |
11892 |
+ |
11893 |
+ local i = 0 |
11894 |
+ for p, c in utf8.codes(s) do |
11895 |
+ i = i + 1 |
11896 |
+ assert(c == t[i] and p == utf8.offset(s, i)) |
11897 |
+ assert(utf8.codepoint(s, p) == c) |
11898 |
+ end |
11899 |
+ assert(i == #t) |
11900 |
+ |
11901 |
+ i = 0 |
11902 |
+ for p, c in utf8.codes(s) do |
11903 |
+ i = i + 1 |
11904 |
+ assert(c == t[i] and p == utf8.offset(s, i)) |
11905 |
+ end |
11906 |
+ assert(i == #t) |
11907 |
+ |
11908 |
+ i = 0 |
11909 |
+ for c in string.gmatch(s, utf8.charpattern) do |
11910 |
+ i = i + 1 |
11911 |
+ assert(c == utf8.char(t[i])) |
11912 |
+ end |
11913 |
+ assert(i == #t) |
11914 |
+ |
11915 |
+ for i = 1, l do |
11916 |
+ assert(utf8.offset(s, i) == utf8.offset(s, i - l - 1, #s + 1)) |
11917 |
+ end |
11918 |
+ |
11919 |
+end |
11920 |
+ |
11921 |
+ |
11922 |
+do -- error indication in utf8.len |
11923 |
+ local function check (s, p) |
11924 |
+ local a, b = utf8.len(s) |
11925 |
+ assert(not a and b == p) |
11926 |
+ end |
11927 |
+ check("abc\xE3def", 4) |
11928 |
+ check("æ±å\x80", #("æ±å") + 1) |
11929 |
+ check("\xF4\x9F\xBF", 1) |
11930 |
+ check("\xF4\x9F\xBF\xBF", 1) |
11931 |
+end |
11932 |
+ |
11933 |
+-- error in utf8.codes |
11934 |
+checkerror("invalid UTF%-8 code", |
11935 |
+ function () |
11936 |
+ local s = "ab\xff" |
11937 |
+ for c in utf8.codes(s) do assert(c) end |
11938 |
+ end) |
11939 |
+ |
11940 |
+ |
11941 |
+-- error in initial position for offset |
11942 |
+checkerror("position out of range", utf8.offset, "abc", 1, 5) |
11943 |
+checkerror("position out of range", utf8.offset, "abc", 1, -4) |
11944 |
+checkerror("position out of range", utf8.offset, "", 1, 2) |
11945 |
+checkerror("position out of range", utf8.offset, "", 1, -1) |
11946 |
+checkerror("continuation byte", utf8.offset, "𦧺", 1, 2) |
11947 |
+checkerror("continuation byte", utf8.offset, "𦧺", 1, 2) |
11948 |
+checkerror("continuation byte", utf8.offset, "\x80", 1) |
11949 |
+ |
11950 |
+ |
11951 |
+ |
11952 |
+local s = "hello World" |
11953 |
+local t = {string.byte(s, 1, -1)} |
11954 |
+for i = 1, utf8.len(s) do assert(t[i] == string.byte(s, i)) end |
11955 |
+check(s, t) |
11956 |
+ |
11957 |
+check("æ±å/æ¼¢å", {27721, 23383, 47, 28450, 23383,}) |
11958 |
+ |
11959 |
+do |
11960 |
+ local s = "áéÃ\128" |
11961 |
+ local t = {utf8.codepoint(s,1,#s - 1)} |
11962 |
+ assert(#t == 3 and t[1] == 225 and t[2] == 233 and t[3] == 237) |
11963 |
+ checkerror("invalid UTF%-8 code", utf8.codepoint, s, 1, #s) |
11964 |
+ checkerror("out of range", utf8.codepoint, s, #s + 1) |
11965 |
+ t = {utf8.codepoint(s, 4, 3)} |
11966 |
+ assert(#t == 0) |
11967 |
+ checkerror("out of range", utf8.codepoint, s, -(#s + 1), 1) |
11968 |
+ checkerror("out of range", utf8.codepoint, s, 1, #s + 1) |
11969 |
+end |
11970 |
+ |
11971 |
+assert(utf8.char() == "") |
11972 |
+assert(utf8.char(97, 98, 99) == "abc") |
11973 |
+ |
11974 |
+assert(utf8.codepoint(utf8.char(0x10FFFF)) == 0x10FFFF) |
11975 |
+ |
11976 |
+checkerror("value out of range", utf8.char, 0x10FFFF + 1) |
11977 |
+ |
11978 |
+local function invalid (s) |
11979 |
+ checkerror("invalid UTF%-8 code", utf8.codepoint, s) |
11980 |
+ assert(not utf8.len(s)) |
11981 |
+end |
11982 |
+ |
11983 |
+-- UTF-8 representation for 0x11ffff (value out of valid range) |
11984 |
+invalid("\xF4\x9F\xBF\xBF") |
11985 |
+ |
11986 |
+-- overlong sequences |
11987 |
+invalid("\xC0\x80") -- zero |
11988 |
+invalid("\xC1\xBF") -- 0x7F (should be coded in 1 byte) |
11989 |
+invalid("\xE0\x9F\xBF") -- 0x7FF (should be coded in 2 bytes) |
11990 |
+invalid("\xF0\x8F\xBF\xBF") -- 0xFFFF (should be coded in 3 bytes) |
11991 |
+ |
11992 |
+ |
11993 |
+-- invalid bytes |
11994 |
+invalid("\x80") -- continuation byte |
11995 |
+invalid("\xBF") -- continuation byte |
11996 |
+invalid("\xFE") -- invalid byte |
11997 |
+invalid("\xFF") -- invalid byte |
11998 |
+ |
11999 |
+ |
12000 |
+-- empty string |
12001 |
+check("", {}) |
12002 |
+ |
12003 |
+-- minimum and maximum values for each sequence size |
12004 |
+s = "\0 \x7F\z |
12005 |
+ \xC2\x80 \xDF\xBF\z |
12006 |
+ \xE0\xA0\x80 \xEF\xBF\xBF\z |
12007 |
+ \xF0\x90\x80\x80 \xF4\x8F\xBF\xBF" |
12008 |
+s = string.gsub(s, " ", "") |
12009 |
+check(s, {0,0x7F, 0x80,0x7FF, 0x800,0xFFFF, 0x10000,0x10FFFF}) |
12010 |
+ |
12011 |
+x = "æ¥æ¬èªa-4\0éó" |
12012 |
+check(x, {26085, 26412, 35486, 97, 45, 52, 0, 233, 243}) |
12013 |
+ |
12014 |
+ |
12015 |
+-- Supplementary Characters |
12016 |
+check("𣲷ð ð ±ð¡»ð µ¼ab𠺢", |
12017 |
+ {0x23CB7, 0x2070E, 0x20C53, 0x2107B, 0x20D7C, 0x61, 0x62, 0x20EA2,}) |
12018 |
+ |
12019 |
+check("ð¨³ð©¶ð¦§ºð¨³ð¥«ð¤\xF4\x8F\xBF\xBF", |
12020 |
+ {0x28CCA, 0x29D98, 0x269FA, 0x28CD2, 0x2512B, 0x244D3, 0x10ffff}) |
12021 |
+ |
12022 |
+ |
12023 |
+local i = 0 |
12024 |
+for p, c in string.gmatch(x, "()(" .. utf8.charpattern .. ")") do |
12025 |
+ i = i + 1 |
12026 |
+ assert(utf8.offset(x, i) == p) |
12027 |
+ assert(utf8.len(x, p) == utf8.len(x) - i + 1) |
12028 |
+ assert(utf8.len(c) == 1) |
12029 |
+ for j = 1, #c - 1 do |
12030 |
+ assert(utf8.offset(x, 0, p + j - 1) == p) |
12031 |
+ end |
12032 |
+end |
12033 |
+ |
12034 |
+print'ok' |
12035 |
+ |
12036 |
|
12037 |
diff --git a/tests/vararg.lua b/tests/vararg.lua |
12038 |
new file mode 100644 |
12039 |
index 0000000..d617a98 |
12040 |
--- /dev/null |
12041 |
+++ b/tests/vararg.lua |
12042 |
@@ -0,0 +1,142 @@ |
12043 |
+-- $Id: vararg.lua,v 1.25 2016/11/07 13:11:28 roberto Exp $ |
12044 |
+-- See Copyright Notice in file all.lua |
12045 |
+ |
12046 |
+print('testing vararg') |
12047 |
+ |
12048 |
+function f(a, ...) |
12049 |
+ local arg = {n = select('#', ...), ...} |
12050 |
+ for i=1,arg.n do assert(a[i]==arg[i]) end |
12051 |
+ return arg.n |
12052 |
+end |
12053 |
+ |
12054 |
+function c12 (...) |
12055 |
+ assert(arg == _G.arg) -- no local 'arg' |
12056 |
+ local x = {...}; x.n = #x |
12057 |
+ local res = (x.n==2 and x[1] == 1 and x[2] == 2) |
12058 |
+ if res then res = 55 end |
12059 |
+ return res, 2 |
12060 |
+end |
12061 |
+ |
12062 |
+function vararg (...) return {n = select('#', ...), ...} end |
12063 |
+ |
12064 |
+local call = function (f, args) return f(table.unpack(args, 1, args.n)) end |
12065 |
+ |
12066 |
+assert(f() == 0) |
12067 |
+assert(f({1,2,3}, 1, 2, 3) == 3) |
12068 |
+assert(f({"alo", nil, 45, f, nil}, "alo", nil, 45, f, nil) == 5) |
12069 |
+ |
12070 |
+assert(c12(1,2)==55) |
12071 |
+a,b = assert(call(c12, {1,2})) |
12072 |
+assert(a == 55 and b == 2) |
12073 |
+a = call(c12, {1,2;n=2}) |
12074 |
+assert(a == 55 and b == 2) |
12075 |
+a = call(c12, {1,2;n=1}) |
12076 |
+assert(not a) |
12077 |
+assert(c12(1,2,3) == false) |
12078 |
+local a = vararg(call(next, {_G,nil;n=2})) |
12079 |
+local b,c = next(_G) |
12080 |
+assert(a[1] == b and a[2] == c and a.n == 2) |
12081 |
+a = vararg(call(call, {c12, {1,2}})) |
12082 |
+assert(a.n == 2 and a[1] == 55 and a[2] == 2) |
12083 |
+a = call(print, {'+'}) |
12084 |
+assert(a == nil) |
12085 |
+ |
12086 |
+local t = {1, 10} |
12087 |
+function t:f (...) local arg = {...}; return self[...]+#arg end |
12088 |
+assert(t:f(1,4) == 3 and t:f(2) == 11) |
12089 |
+print('+') |
12090 |
+ |
12091 |
+lim = 20 |
12092 |
+local i, a = 1, {} |
12093 |
+while i <= lim do a[i] = i+0.3; i=i+1 end |
12094 |
+ |
12095 |
+function f(a, b, c, d, ...) |
12096 |
+ local more = {...} |
12097 |
+ assert(a == 1.3 and more[1] == 5.3 and |
12098 |
+ more[lim-4] == lim+0.3 and not more[lim-3]) |
12099 |
+end |
12100 |
+ |
12101 |
+function g(a,b,c) |
12102 |
+ assert(a == 1.3 and b == 2.3 and c == 3.3) |
12103 |
+end |
12104 |
+ |
12105 |
+call(f, a) |
12106 |
+call(g, a) |
12107 |
+ |
12108 |
+a = {} |
12109 |
+i = 1 |
12110 |
+while i <= lim do a[i] = i; i=i+1 end |
12111 |
+assert(call(math.max, a) == lim) |
12112 |
+ |
12113 |
+print("+") |
12114 |
+ |
12115 |
+ |
12116 |
+-- new-style varargs |
12117 |
+ |
12118 |
+function oneless (a, ...) return ... end |
12119 |
+ |
12120 |
+function f (n, a, ...) |
12121 |
+ local b |
12122 |
+ assert(arg == _G.arg) -- no local 'arg' |
12123 |
+ if n == 0 then |
12124 |
+ local b, c, d = ... |
12125 |
+ return a, b, c, d, oneless(oneless(oneless(...))) |
12126 |
+ else |
12127 |
+ n, b, a = n-1, ..., a |
12128 |
+ assert(b == ...) |
12129 |
+ return f(n, a, ...) |
12130 |
+ end |
12131 |
+end |
12132 |
+ |
12133 |
+a,b,c,d,e = assert(f(10,5,4,3,2,1)) |
12134 |
+assert(a==5 and b==4 and c==3 and d==2 and e==1) |
12135 |
+ |
12136 |
+a,b,c,d,e = f(4) |
12137 |
+assert(a==nil and b==nil and c==nil and d==nil and e==nil) |
12138 |
+ |
12139 |
+ |
12140 |
+-- varargs for main chunks |
12141 |
+f = load[[ return {...} ]] |
12142 |
+x = f(2,3) |
12143 |
+assert(x[1] == 2 and x[2] == 3 and x[3] == nil) |
12144 |
+ |
12145 |
+ |
12146 |
+f = load[[ |
12147 |
+ local x = {...} |
12148 |
+ for i=1,select('#', ...) do assert(x[i] == select(i, ...)) end |
12149 |
+ assert(x[select('#', ...)+1] == nil) |
12150 |
+ return true |
12151 |
+]] |
12152 |
+ |
12153 |
+assert(f("a", "b", nil, {}, assert)) |
12154 |
+assert(f()) |
12155 |
+ |
12156 |
+a = {select(3, table.unpack{10,20,30,40})} |
12157 |
+assert(#a == 2 and a[1] == 30 and a[2] == 40) |
12158 |
+a = {select(1)} |
12159 |
+assert(next(a) == nil) |
12160 |
+a = {select(-1, 3, 5, 7)} |
12161 |
+assert(a[1] == 7 and a[2] == nil) |
12162 |
+a = {select(-2, 3, 5, 7)} |
12163 |
+assert(a[1] == 5 and a[2] == 7 and a[3] == nil) |
12164 |
+pcall(select, 10000) |
12165 |
+pcall(select, -10000) |
12166 |
+ |
12167 |
+ |
12168 |
+-- bug in 5.2.2 |
12169 |
+ |
12170 |
+function f(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, |
12171 |
+p11, p12, p13, p14, p15, p16, p17, p18, p19, p20, |
12172 |
+p21, p22, p23, p24, p25, p26, p27, p28, p29, p30, |
12173 |
+p31, p32, p33, p34, p35, p36, p37, p38, p39, p40, |
12174 |
+p41, p42, p43, p44, p45, p46, p48, p49, p50, ...) |
12175 |
+ local a1,a2,a3,a4,a5,a6,a7 |
12176 |
+ local a8,a9,a10,a11,a12,a13,a14 |
12177 |
+end |
12178 |
+ |
12179 |
+-- assertion fail here |
12180 |
+f() |
12181 |
+ |
12182 |
+ |
12183 |
+print('OK') |
12184 |
+ |
12185 |
|
12186 |
diff --git a/tests/verybig.lua b/tests/verybig.lua |
12187 |
new file mode 100644 |
12188 |
index 0000000..1cfd10b |
12189 |
--- /dev/null |
12190 |
+++ b/tests/verybig.lua |
12191 |
@@ -0,0 +1,152 @@ |
12192 |
+-- $Id: verybig.lua,v 1.25 2016/11/07 13:11:28 roberto Exp $ |
12193 |
+-- See Copyright Notice in file all.lua |
12194 |
+ |
12195 |
+print "testing RK" |
12196 |
+ |
12197 |
+-- testing opcodes with RK arguments larger than K limit |
12198 |
+local function foo () |
12199 |
+ local dummy = { |
12200 |
+ -- fill first 256 entries in table of constants |
12201 |
+ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, |
12202 |
+ 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, |
12203 |
+ 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, |
12204 |
+ 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, |
12205 |
+ 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, |
12206 |
+ 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, |
12207 |
+ 97, 98, 99, 100, 101, 102, 103, 104, |
12208 |
+ 105, 106, 107, 108, 109, 110, 111, 112, |
12209 |
+ 113, 114, 115, 116, 117, 118, 119, 120, |
12210 |
+ 121, 122, 123, 124, 125, 126, 127, 128, |
12211 |
+ 129, 130, 131, 132, 133, 134, 135, 136, |
12212 |
+ 137, 138, 139, 140, 141, 142, 143, 144, |
12213 |
+ 145, 146, 147, 148, 149, 150, 151, 152, |
12214 |
+ 153, 154, 155, 156, 157, 158, 159, 160, |
12215 |
+ 161, 162, 163, 164, 165, 166, 167, 168, |
12216 |
+ 169, 170, 171, 172, 173, 174, 175, 176, |
12217 |
+ 177, 178, 179, 180, 181, 182, 183, 184, |
12218 |
+ 185, 186, 187, 188, 189, 190, 191, 192, |
12219 |
+ 193, 194, 195, 196, 197, 198, 199, 200, |
12220 |
+ 201, 202, 203, 204, 205, 206, 207, 208, |
12221 |
+ 209, 210, 211, 212, 213, 214, 215, 216, |
12222 |
+ 217, 218, 219, 220, 221, 222, 223, 224, |
12223 |
+ 225, 226, 227, 228, 229, 230, 231, 232, |
12224 |
+ 233, 234, 235, 236, 237, 238, 239, 240, |
12225 |
+ 241, 242, 243, 244, 245, 246, 247, 248, |
12226 |
+ 249, 250, 251, 252, 253, 254, 255, 256, |
12227 |
+ } |
12228 |
+ assert(24.5 + 0.6 == 25.1) |
12229 |
+ local t = {foo = function (self, x) return x + self.x end, x = 10} |
12230 |
+ t.t = t |
12231 |
+ assert(t:foo(1.5) == 11.5) |
12232 |
+ assert(t.t:foo(0.5) == 10.5) -- bug in 5.2 alpha |
12233 |
+ assert(24.3 == 24.3) |
12234 |
+ assert((function () return t.x end)() == 10) |
12235 |
+end |
12236 |
+ |
12237 |
+ |
12238 |
+foo() |
12239 |
+foo = nil |
12240 |
+ |
12241 |
+if _soft then return 10 end |
12242 |
+ |
12243 |
+print "testing large programs (>64k)" |
12244 |
+ |
12245 |
+-- template to create a very big test file |
12246 |
+prog = [[$ |
12247 |
+ |
12248 |
+local a,b |
12249 |
+ |
12250 |
+b = {$1$ |
12251 |
+ b30009 = 65534, |
12252 |
+ b30010 = 65535, |
12253 |
+ b30011 = 65536, |
12254 |
+ b30012 = 65537, |
12255 |
+ b30013 = 16777214, |
12256 |
+ b30014 = 16777215, |
12257 |
+ b30015 = 16777216, |
12258 |
+ b30016 = 16777217, |
12259 |
+ b30017 = 0x7fffff, |
12260 |
+ b30018 = -0x7fffff, |
12261 |
+ b30019 = 0x1ffffff, |
12262 |
+ b30020 = -0x1ffffd, |
12263 |
+ b30021 = -65534, |
12264 |
+ b30022 = -65535, |
12265 |
+ b30023 = -65536, |
12266 |
+ b30024 = -0xffffff, |
12267 |
+ b30025 = 15012.5, |
12268 |
+ $2$ |
12269 |
+}; |
12270 |
+ |
12271 |
+assert(b.a50008 == 25004 and b["a11"] == -5.5) |
12272 |
+assert(b.a33007 == -16503.5 and b.a50009 == -25004.5) |
12273 |
+assert(b["b"..30024] == -0xffffff) |
12274 |
+ |
12275 |
+function b:xxx (a,b) return a+b end |
12276 |
+assert(b:xxx(10, 12) == 22) -- pushself with non-constant index |
12277 |
+b.xxx = nil |
12278 |
+ |
12279 |
+s = 0; n=0 |
12280 |
+for a,b in pairs(b) do s=s+b; n=n+1 end |
12281 |
+-- with 32-bit floats, exact value of 's' depends on summation order |
12282 |
+assert(81800000.0 < s and s < 81860000 and n == 70001) |
12283 |
+ |
12284 |
+a = nil; b = nil |
12285 |
+print'+' |
12286 |
+ |
12287 |
+function f(x) b=x end |
12288 |
+ |
12289 |
+a = f{$3$} or 10 |
12290 |
+ |
12291 |
+assert(a==10) |
12292 |
+assert(b[1] == "a10" and b[2] == 5 and b[#b-1] == "a50009") |
12293 |
+ |
12294 |
+ |
12295 |
+function xxxx (x) return b[x] end |
12296 |
+ |
12297 |
+assert(xxxx(3) == "a11") |
12298 |
+ |
12299 |
+a = nil; b=nil |
12300 |
+xxxx = nil |
12301 |
+ |
12302 |
+return 10 |
12303 |
+ |
12304 |
+]] |
12305 |
+ |
12306 |
+-- functions to fill in the $n$ |
12307 |
+ |
12308 |
+local function sig (x) |
12309 |
+ return (x % 2 == 0) and '' or '-' |
12310 |
+end |
12311 |
+ |
12312 |
+F = { |
12313 |
+function () -- $1$ |
12314 |
+ for i=10,50009 do |
12315 |
+ io.write('a', i, ' = ', sig(i), 5+((i-10)/2), ',\n') |
12316 |
+ end |
12317 |
+end, |
12318 |
+ |
12319 |
+function () -- $2$ |
12320 |
+ for i=30026,50009 do |
12321 |
+ io.write('b', i, ' = ', sig(i), 15013+((i-30026)/2), ',\n') |
12322 |
+ end |
12323 |
+end, |
12324 |
+ |
12325 |
+function () -- $3$ |
12326 |
+ for i=10,50009 do |
12327 |
+ io.write('"a', i, '", ', sig(i), 5+((i-10)/2), ',\n') |
12328 |
+ end |
12329 |
+end, |
12330 |
+} |
12331 |
+ |
12332 |
+file = os.tmpname() |
12333 |
+io.output(file) |
12334 |
+for s in string.gmatch(prog, "$([^$]+)") do |
12335 |
+ local n = tonumber(s) |
12336 |
+ if not n then io.write(s) else F[n]() end |
12337 |
+end |
12338 |
+io.close() |
12339 |
+result = dofile(file) |
12340 |
+assert(os.remove(file)) |
12341 |
+print'OK' |
12342 |
+return result |
12343 |
+ |