diff options
Diffstat (limited to 'zig-lua/lua-5.4.7/testes/calls.lua')
| -rw-r--r-- | zig-lua/lua-5.4.7/testes/calls.lua | 497 |
1 files changed, 497 insertions, 0 deletions
diff --git a/zig-lua/lua-5.4.7/testes/calls.lua b/zig-lua/lua-5.4.7/testes/calls.lua new file mode 100644 index 0000000..a193858 --- /dev/null +++ b/zig-lua/lua-5.4.7/testes/calls.lua | |||
| @@ -0,0 +1,497 @@ | |||
| 1 | -- $Id: testes/calls.lua $ | ||
| 2 | -- See Copyright Notice in file all.lua | ||
| 3 | |||
| 4 | print("testing functions and calls") | ||
| 5 | |||
| 6 | local debug = require "debug" | ||
| 7 | |||
| 8 | -- get the opportunity to test 'type' too ;) | ||
| 9 | |||
| 10 | assert(type(1<2) == 'boolean') | ||
| 11 | assert(type(true) == 'boolean' and type(false) == 'boolean') | ||
| 12 | assert(type(nil) == 'nil' | ||
| 13 | and type(-3) == 'number' | ||
| 14 | and type'x' == 'string' | ||
| 15 | and type{} == 'table' | ||
| 16 | and type(type) == 'function') | ||
| 17 | |||
| 18 | assert(type(assert) == type(print)) | ||
| 19 | local function f (x) return a:x (x) end | ||
| 20 | assert(type(f) == 'function') | ||
| 21 | assert(not pcall(type)) | ||
| 22 | |||
| 23 | |||
| 24 | -- testing local-function recursion | ||
| 25 | fact = false | ||
| 26 | do | ||
| 27 | local res = 1 | ||
| 28 | local function fact (n) | ||
| 29 | if n==0 then return res | ||
| 30 | else return n*fact(n-1) | ||
| 31 | end | ||
| 32 | end | ||
| 33 | assert(fact(5) == 120) | ||
| 34 | end | ||
| 35 | assert(fact == false) | ||
| 36 | fact = nil | ||
| 37 | |||
| 38 | -- testing declarations | ||
| 39 | local a = {i = 10} | ||
| 40 | local self = 20 | ||
| 41 | function a:x (x) return x+self.i end | ||
| 42 | function a.y (x) return x+self end | ||
| 43 | |||
| 44 | assert(a:x(1)+10 == a.y(1)) | ||
| 45 | |||
| 46 | a.t = {i=-100} | ||
| 47 | a["t"].x = function (self, a,b) return self.i+a+b end | ||
| 48 | |||
| 49 | assert(a.t:x(2,3) == -95) | ||
| 50 | |||
| 51 | do | ||
| 52 | local a = {x=0} | ||
| 53 | function a:add (x) self.x, a.y = self.x+x, 20; return self end | ||
| 54 | assert(a:add(10):add(20):add(30).x == 60 and a.y == 20) | ||
| 55 | end | ||
| 56 | |||
| 57 | local a = {b={c={}}} | ||
| 58 | |||
| 59 | function a.b.c.f1 (x) return x+1 end | ||
| 60 | function a.b.c:f2 (x,y) self[x] = y end | ||
| 61 | assert(a.b.c.f1(4) == 5) | ||
| 62 | a.b.c:f2('k', 12); assert(a.b.c.k == 12) | ||
| 63 | |||
| 64 | print('+') | ||
| 65 | |||
| 66 | t = nil -- 'declare' t | ||
| 67 | function f(a,b,c) local d = 'a'; t={a,b,c,d} end | ||
| 68 | |||
| 69 | f( -- this line change must be valid | ||
| 70 | 1,2) | ||
| 71 | assert(t[1] == 1 and t[2] == 2 and t[3] == nil and t[4] == 'a') | ||
| 72 | f(1,2, -- this one too | ||
| 73 | 3,4) | ||
| 74 | assert(t[1] == 1 and t[2] == 2 and t[3] == 3 and t[4] == 'a') | ||
| 75 | |||
| 76 | t = nil -- delete 't' | ||
| 77 | |||
| 78 | function fat(x) | ||
| 79 | if x <= 1 then return 1 | ||
| 80 | else return x*load("return fat(" .. x-1 .. ")", "")() | ||
| 81 | end | ||
| 82 | end | ||
| 83 | |||
| 84 | assert(load "load 'assert(fat(6)==720)' () ")() | ||
| 85 | a = load('return fat(5), 3') | ||
| 86 | local a,b = a() | ||
| 87 | assert(a == 120 and b == 3) | ||
| 88 | fat = nil | ||
| 89 | print('+') | ||
| 90 | |||
| 91 | local function err_on_n (n) | ||
| 92 | if n==0 then error(); exit(1); | ||
| 93 | else err_on_n (n-1); exit(1); | ||
| 94 | end | ||
| 95 | end | ||
| 96 | |||
| 97 | do | ||
| 98 | local function dummy (n) | ||
| 99 | if n > 0 then | ||
| 100 | assert(not pcall(err_on_n, n)) | ||
| 101 | dummy(n-1) | ||
| 102 | end | ||
| 103 | end | ||
| 104 | |||
| 105 | dummy(10) | ||
| 106 | end | ||
| 107 | |||
| 108 | _G.deep = nil -- "declaration" (used by 'all.lua') | ||
| 109 | |||
| 110 | function deep (n) | ||
| 111 | if n>0 then deep(n-1) end | ||
| 112 | end | ||
| 113 | deep(10) | ||
| 114 | deep(180) | ||
| 115 | |||
| 116 | |||
| 117 | print"testing tail calls" | ||
| 118 | |||
| 119 | function deep (n) if n>0 then return deep(n-1) else return 101 end end | ||
| 120 | assert(deep(30000) == 101) | ||
| 121 | a = {} | ||
| 122 | function a:deep (n) if n>0 then return self:deep(n-1) else return 101 end end | ||
| 123 | assert(a:deep(30000) == 101) | ||
| 124 | |||
| 125 | do -- tail calls x varargs | ||
| 126 | local function foo (x, ...) local a = {...}; return x, a[1], a[2] end | ||
| 127 | |||
| 128 | local function foo1 (x) return foo(10, x, x + 1) end | ||
| 129 | |||
| 130 | local a, b, c = foo1(-2) | ||
| 131 | assert(a == 10 and b == -2 and c == -1) | ||
| 132 | |||
| 133 | -- tail calls x metamethods | ||
| 134 | local t = setmetatable({}, {__call = foo}) | ||
| 135 | local function foo2 (x) return t(10, x) end | ||
| 136 | a, b, c = foo2(100) | ||
| 137 | assert(a == t and b == 10 and c == 100) | ||
| 138 | |||
| 139 | a, b = (function () return foo() end)() | ||
| 140 | assert(a == nil and b == nil) | ||
| 141 | |||
| 142 | local X, Y, A | ||
| 143 | local function foo (x, y, ...) X = x; Y = y; A = {...} end | ||
| 144 | local function foo1 (...) return foo(...) end | ||
| 145 | |||
| 146 | local a, b, c = foo1() | ||
| 147 | assert(X == nil and Y == nil and #A == 0) | ||
| 148 | |||
| 149 | a, b, c = foo1(10) | ||
| 150 | assert(X == 10 and Y == nil and #A == 0) | ||
| 151 | |||
| 152 | a, b, c = foo1(10, 20) | ||
| 153 | assert(X == 10 and Y == 20 and #A == 0) | ||
| 154 | |||
| 155 | a, b, c = foo1(10, 20, 30) | ||
| 156 | assert(X == 10 and Y == 20 and #A == 1 and A[1] == 30) | ||
| 157 | end | ||
| 158 | |||
| 159 | |||
| 160 | do -- C-stack overflow while handling C-stack overflow | ||
| 161 | local function loop () | ||
| 162 | assert(pcall(loop)) | ||
| 163 | end | ||
| 164 | |||
| 165 | local err, msg = xpcall(loop, loop) | ||
| 166 | assert(not err and string.find(msg, "error")) | ||
| 167 | end | ||
| 168 | |||
| 169 | |||
| 170 | |||
| 171 | do -- tail calls x chain of __call | ||
| 172 | local n = 10000 -- depth | ||
| 173 | |||
| 174 | local function foo () | ||
| 175 | if n == 0 then return 1023 | ||
| 176 | else n = n - 1; return foo() | ||
| 177 | end | ||
| 178 | end | ||
| 179 | |||
| 180 | -- build a chain of __call metamethods ending in function 'foo' | ||
| 181 | for i = 1, 100 do | ||
| 182 | foo = setmetatable({}, {__call = foo}) | ||
| 183 | end | ||
| 184 | |||
| 185 | -- call the first one as a tail call in a new coroutine | ||
| 186 | -- (to ensure stack is not preallocated) | ||
| 187 | assert(coroutine.wrap(function() return foo() end)() == 1023) | ||
| 188 | end | ||
| 189 | |||
| 190 | print('+') | ||
| 191 | |||
| 192 | |||
| 193 | do -- testing chains of '__call' | ||
| 194 | local N = 20 | ||
| 195 | local u = table.pack | ||
| 196 | for i = 1, N do | ||
| 197 | u = setmetatable({i}, {__call = u}) | ||
| 198 | end | ||
| 199 | |||
| 200 | local Res = u("a", "b", "c") | ||
| 201 | |||
| 202 | assert(Res.n == N + 3) | ||
| 203 | for i = 1, N do | ||
| 204 | assert(Res[i][1] == i) | ||
| 205 | end | ||
| 206 | assert(Res[N + 1] == "a" and Res[N + 2] == "b" and Res[N + 3] == "c") | ||
| 207 | end | ||
| 208 | |||
| 209 | |||
| 210 | a = nil | ||
| 211 | (function (x) a=x end)(23) | ||
| 212 | assert(a == 23 and (function (x) return x*2 end)(20) == 40) | ||
| 213 | |||
| 214 | |||
| 215 | -- testing closures | ||
| 216 | |||
| 217 | -- fixed-point operator | ||
| 218 | local Z = function (le) | ||
| 219 | local function a (f) | ||
| 220 | return le(function (x) return f(f)(x) end) | ||
| 221 | end | ||
| 222 | return a(a) | ||
| 223 | end | ||
| 224 | |||
| 225 | |||
| 226 | -- non-recursive factorial | ||
| 227 | |||
| 228 | local F = function (f) | ||
| 229 | return function (n) | ||
| 230 | if n == 0 then return 1 | ||
| 231 | else return n*f(n-1) end | ||
| 232 | end | ||
| 233 | end | ||
| 234 | |||
| 235 | local fat = Z(F) | ||
| 236 | |||
| 237 | assert(fat(0) == 1 and fat(4) == 24 and Z(F)(5)==5*Z(F)(4)) | ||
| 238 | |||
| 239 | local function g (z) | ||
| 240 | local function f (a,b,c,d) | ||
| 241 | return function (x,y) return a+b+c+d+a+x+y+z end | ||
| 242 | end | ||
| 243 | return f(z,z+1,z+2,z+3) | ||
| 244 | end | ||
| 245 | |||
| 246 | local f = g(10) | ||
| 247 | assert(f(9, 16) == 10+11+12+13+10+9+16+10) | ||
| 248 | |||
| 249 | print('+') | ||
| 250 | |||
| 251 | -- testing multiple returns | ||
| 252 | |||
| 253 | local function unlpack (t, i) | ||
| 254 | i = i or 1 | ||
| 255 | if (i <= #t) then | ||
| 256 | return t[i], unlpack(t, i+1) | ||
| 257 | end | ||
| 258 | end | ||
| 259 | |||
| 260 | local function equaltab (t1, t2) | ||
| 261 | assert(#t1 == #t2) | ||
| 262 | for i = 1, #t1 do | ||
| 263 | assert(t1[i] == t2[i]) | ||
| 264 | end | ||
| 265 | end | ||
| 266 | |||
| 267 | local pack = function (...) return (table.pack(...)) end | ||
| 268 | |||
| 269 | local function f() return 1,2,30,4 end | ||
| 270 | local function ret2 (a,b) return a,b end | ||
| 271 | |||
| 272 | local a,b,c,d = unlpack{1,2,3} | ||
| 273 | assert(a==1 and b==2 and c==3 and d==nil) | ||
| 274 | a = {1,2,3,4,false,10,'alo',false,assert} | ||
| 275 | equaltab(pack(unlpack(a)), a) | ||
| 276 | equaltab(pack(unlpack(a), -1), {1,-1}) | ||
| 277 | a,b,c,d = ret2(f()), ret2(f()) | ||
| 278 | assert(a==1 and b==1 and c==2 and d==nil) | ||
| 279 | a,b,c,d = unlpack(pack(ret2(f()), ret2(f()))) | ||
| 280 | assert(a==1 and b==1 and c==2 and d==nil) | ||
| 281 | a,b,c,d = unlpack(pack(ret2(f()), (ret2(f())))) | ||
| 282 | assert(a==1 and b==1 and c==nil and d==nil) | ||
| 283 | |||
| 284 | a = ret2{ unlpack{1,2,3}, unlpack{3,2,1}, unlpack{"a", "b"}} | ||
| 285 | assert(a[1] == 1 and a[2] == 3 and a[3] == "a" and a[4] == "b") | ||
| 286 | |||
| 287 | |||
| 288 | -- testing calls with 'incorrect' arguments | ||
| 289 | rawget({}, "x", 1) | ||
| 290 | rawset({}, "x", 1, 2) | ||
| 291 | assert(math.sin(1,2) == math.sin(1)) | ||
| 292 | table.sort({10,9,8,4,19,23,0,0}, function (a,b) return a<b end, "extra arg") | ||
| 293 | |||
| 294 | |||
| 295 | -- test for generic load | ||
| 296 | local x = "-- a comment\0\0\0\n x = 10 + \n23; \ | ||
| 297 | local a = function () x = 'hi' end; \ | ||
| 298 | return '\0'" | ||
| 299 | local function read1 (x) | ||
| 300 | local i = 0 | ||
| 301 | return function () | ||
| 302 | collectgarbage() | ||
| 303 | i=i+1 | ||
| 304 | return string.sub(x, i, i) | ||
| 305 | end | ||
| 306 | end | ||
| 307 | |||
| 308 | local function cannotload (msg, a,b) | ||
| 309 | assert(not a and string.find(b, msg)) | ||
| 310 | end | ||
| 311 | |||
| 312 | a = assert(load(read1(x), "modname", "t", _G)) | ||
| 313 | assert(a() == "\0" and _G.x == 33) | ||
| 314 | assert(debug.getinfo(a).source == "modname") | ||
| 315 | -- cannot read text in binary mode | ||
| 316 | cannotload("attempt to load a text chunk", load(read1(x), "modname", "b", {})) | ||
| 317 | cannotload("attempt to load a text chunk", load(x, "modname", "b")) | ||
| 318 | |||
| 319 | a = assert(load(function () return nil end)) | ||
| 320 | a() -- empty chunk | ||
| 321 | |||
| 322 | assert(not load(function () return true end)) | ||
| 323 | |||
| 324 | |||
| 325 | -- small bug | ||
| 326 | local t = {nil, "return ", "3"} | ||
| 327 | f, msg = load(function () return table.remove(t, 1) end) | ||
| 328 | assert(f() == nil) -- should read the empty chunk | ||
| 329 | |||
| 330 | -- another small bug (in 5.2.1) | ||
| 331 | f = load(string.dump(function () return 1 end), nil, "b", {}) | ||
| 332 | assert(type(f) == "function" and f() == 1) | ||
| 333 | |||
| 334 | |||
| 335 | do -- another bug (in 5.4.0) | ||
| 336 | -- loading a binary long string interrupted by GC cycles | ||
| 337 | local f = string.dump(function () | ||
| 338 | return '01234567890123456789012345678901234567890123456789' | ||
| 339 | end) | ||
| 340 | f = load(read1(f)) | ||
| 341 | assert(f() == '01234567890123456789012345678901234567890123456789') | ||
| 342 | end | ||
| 343 | |||
| 344 | |||
| 345 | x = string.dump(load("x = 1; return x")) | ||
| 346 | a = assert(load(read1(x), nil, "b")) | ||
| 347 | assert(a() == 1 and _G.x == 1) | ||
| 348 | cannotload("attempt to load a binary chunk", load(read1(x), nil, "t")) | ||
| 349 | cannotload("attempt to load a binary chunk", load(x, nil, "t")) | ||
| 350 | _G.x = nil | ||
| 351 | |||
| 352 | assert(not pcall(string.dump, print)) -- no dump of C functions | ||
| 353 | |||
| 354 | cannotload("unexpected symbol", load(read1("*a = 123"))) | ||
| 355 | cannotload("unexpected symbol", load("*a = 123")) | ||
| 356 | cannotload("hhi", load(function () error("hhi") end)) | ||
| 357 | |||
| 358 | -- any value is valid for _ENV | ||
| 359 | assert(load("return _ENV", nil, nil, 123)() == 123) | ||
| 360 | |||
| 361 | |||
| 362 | -- load when _ENV is not first upvalue | ||
| 363 | local x; XX = 123 | ||
| 364 | local function h () | ||
| 365 | local y=x -- use 'x', so that it becomes 1st upvalue | ||
| 366 | return XX -- global name | ||
| 367 | end | ||
| 368 | local d = string.dump(h) | ||
| 369 | x = load(d, "", "b") | ||
| 370 | assert(debug.getupvalue(x, 2) == '_ENV') | ||
| 371 | debug.setupvalue(x, 2, _G) | ||
| 372 | assert(x() == 123) | ||
| 373 | |||
| 374 | assert(assert(load("return XX + ...", nil, nil, {XX = 13}))(4) == 17) | ||
| 375 | XX = nil | ||
| 376 | |||
| 377 | -- test generic load with nested functions | ||
| 378 | x = [[ | ||
| 379 | return function (x) | ||
| 380 | return function (y) | ||
| 381 | return function (z) | ||
| 382 | return x+y+z | ||
| 383 | end | ||
| 384 | end | ||
| 385 | end | ||
| 386 | ]] | ||
| 387 | a = assert(load(read1(x), "read", "t")) | ||
| 388 | assert(a()(2)(3)(10) == 15) | ||
| 389 | |||
| 390 | -- repeat the test loading a binary chunk | ||
| 391 | x = string.dump(a) | ||
| 392 | a = assert(load(read1(x), "read", "b")) | ||
| 393 | assert(a()(2)(3)(10) == 15) | ||
| 394 | |||
| 395 | |||
| 396 | -- test for dump/undump with upvalues | ||
| 397 | local a, b = 20, 30 | ||
| 398 | x = load(string.dump(function (x) | ||
| 399 | if x == "set" then a = 10+b; b = b+1 else | ||
| 400 | return a | ||
| 401 | end | ||
| 402 | end), "", "b", nil) | ||
| 403 | assert(x() == nil) | ||
| 404 | assert(debug.setupvalue(x, 1, "hi") == "a") | ||
| 405 | assert(x() == "hi") | ||
| 406 | assert(debug.setupvalue(x, 2, 13) == "b") | ||
| 407 | assert(not debug.setupvalue(x, 3, 10)) -- only 2 upvalues | ||
| 408 | x("set") | ||
| 409 | assert(x() == 23) | ||
| 410 | x("set") | ||
| 411 | assert(x() == 24) | ||
| 412 | |||
| 413 | -- test for dump/undump with many upvalues | ||
| 414 | do | ||
| 415 | local nup = 200 -- maximum number of local variables | ||
| 416 | local prog = {"local a1"} | ||
| 417 | for i = 2, nup do prog[#prog + 1] = ", a" .. i end | ||
| 418 | prog[#prog + 1] = " = 1" | ||
| 419 | for i = 2, nup do prog[#prog + 1] = ", " .. i end | ||
| 420 | local sum = 1 | ||
| 421 | prog[#prog + 1] = "; return function () return a1" | ||
| 422 | for i = 2, nup do prog[#prog + 1] = " + a" .. i; sum = sum + i end | ||
| 423 | prog[#prog + 1] = " end" | ||
| 424 | prog = table.concat(prog) | ||
| 425 | local f = assert(load(prog))() | ||
| 426 | assert(f() == sum) | ||
| 427 | |||
| 428 | f = load(string.dump(f)) -- main chunk now has many upvalues | ||
| 429 | local a = 10 | ||
| 430 | local h = function () return a end | ||
| 431 | for i = 1, nup do | ||
| 432 | debug.upvaluejoin(f, i, h, 1) | ||
| 433 | end | ||
| 434 | assert(f() == 10 * nup) | ||
| 435 | end | ||
| 436 | |||
| 437 | -- test for long method names | ||
| 438 | do | ||
| 439 | local t = {x = 1} | ||
| 440 | function t:_012345678901234567890123456789012345678901234567890123456789 () | ||
| 441 | return self.x | ||
| 442 | end | ||
| 443 | assert(t:_012345678901234567890123456789012345678901234567890123456789() == 1) | ||
| 444 | end | ||
| 445 | |||
| 446 | |||
| 447 | -- test for bug in parameter adjustment | ||
| 448 | assert((function () return nil end)(4) == nil) | ||
| 449 | assert((function () local a; return a end)(4) == nil) | ||
| 450 | assert((function (a) return a end)() == nil) | ||
| 451 | |||
| 452 | |||
| 453 | print("testing binary chunks") | ||
| 454 | do | ||
| 455 | local header = string.pack("c4BBc6BBB", | ||
| 456 | "\27Lua", -- signature | ||
| 457 | 0x54, -- version 5.4 (0x54) | ||
| 458 | 0, -- format | ||
| 459 | "\x19\x93\r\n\x1a\n", -- data | ||
| 460 | 4, -- size of instruction | ||
| 461 | string.packsize("j"), -- sizeof(lua integer) | ||
| 462 | string.packsize("n") -- sizeof(lua number) | ||
| 463 | ) | ||
| 464 | local c = string.dump(function () | ||
| 465 | local a = 1; local b = 3; | ||
| 466 | local f = function () return a + b + _ENV.c; end -- upvalues | ||
| 467 | local s1 = "a constant" | ||
| 468 | local s2 = "another constant" | ||
| 469 | return a + b * 3 | ||
| 470 | end) | ||
| 471 | |||
| 472 | assert(assert(load(c))() == 10) | ||
| 473 | |||
| 474 | -- check header | ||
| 475 | assert(string.sub(c, 1, #header) == header) | ||
| 476 | -- check LUAC_INT and LUAC_NUM | ||
| 477 | local ci, cn = string.unpack("jn", c, #header + 1) | ||
| 478 | assert(ci == 0x5678 and cn == 370.5) | ||
| 479 | |||
| 480 | -- corrupted header | ||
| 481 | for i = 1, #header do | ||
| 482 | local s = string.sub(c, 1, i - 1) .. | ||
| 483 | string.char(string.byte(string.sub(c, i, i)) + 1) .. | ||
| 484 | string.sub(c, i + 1, -1) | ||
| 485 | assert(#s == #c) | ||
| 486 | assert(not load(s)) | ||
| 487 | end | ||
| 488 | |||
| 489 | -- loading truncated binary chunks | ||
| 490 | for i = 1, #c - 1 do | ||
| 491 | local st, msg = load(string.sub(c, 1, i)) | ||
| 492 | assert(not st and string.find(msg, "truncated")) | ||
| 493 | end | ||
| 494 | end | ||
| 495 | |||
| 496 | print('OK') | ||
| 497 | return deep | ||
