Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/lua/blob/master/testes/calls.lua # lua [Ported from github.com/lua/lua] Cast in 'rl_readline_name' Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `lua / testes / calls.lua` 603 lines ยท 14874 bytes Raw Blame History 1 -- $Id: testes/calls.lua $ 2 -- See Copyright Notice in file lua.h 3 4 global * 5 6 print("testing functions and calls") 7 8 local debug = require "debug" 9 10 -- get the opportunity to test 'type' too ;) 11 12 assert(type(1<2) == 'boolean') 13 assert(type(true) == 'boolean' and type(false) == 'boolean') 14 assert(type(nil) == 'nil' 15 and type(-3) == 'number' 16 and type'x' == 'string' 17 and type{} == 'table' 18 and type(type) == 'function') 19 20 assert(type(assert) == type(print)) 21 local function f (x) return a:x (x) end 22 assert(type(f) == 'function') 23 assert(not pcall(type)) 24 25 26 -- testing local-function recursion 27 global fact = false 28 do 29 local res = 1 30 local function fact (n) 31 if n==0 then return res 32 else return n*fact(n-1) 33 end 34 end 35 assert(fact(5) == 120) 36 end 37 assert(fact == false) 38 fact = nil 39 40 -- testing declarations 41 local a = {i = 10} 42 local self = 20 43 function a:x (x) return x+self.i end 44 function a.y (x) return x+self end 45 46 assert(a:x(1)+10 == a.y(1)) 47 48 a.t = {i=-100} 49 a["t"].x = function (self, a,b) return self.i+a+b end 50 51 assert(a.t:x(2,3) == -95) 52 53 do 54 local a = {x=0} 55 function a:add (x) self.x, a.y = self.x+x, 20; return self end 56 assert(a:add(10):add(20):add(30).x == 60 and a.y == 20) 57 end 58 59 local a = {b={c={}}} 60 61 function a.b.c.f1 (x) return x+1 end 62 function a.b.c:f2 (x,y) self[x] = y end 63 assert(a.b.c.f1(4) == 5) 64 a.b.c:f2('k', 12); assert(a.b.c.k == 12) 65 66 print('+') 67 68 global t = nil -- 'declare' t 69 function f(a,b,c) local d = 'a'; t={a,b,c,d} end 70 71 f( -- this line change must be valid 72 1,2) 73 assert(t[1] == 1 and t[2] == 2 and t[3] == nil and t[4] == 'a') 74 f(1,2, -- this one too 75 3,4) 76 assert(t[1] == 1 and t[2] == 2 and t[3] == 3 and t[4] == 'a') 77 78 t = nil -- delete 't' 79 80 global function fat(x) 81 if x <= 1 then return 1 82 else return x*load("return fat(" .. x-1 .. ")", "")() 83 end 84 end 85 86 assert(load "load 'assert(fat(6)==720)' () ")() 87 a = load('return fat(5), 3') 88 local a,b = a() 89 assert(a == 120 and b == 3) 90 fat = nil 91 print('+') 92 93 local function err_on_n (n) 94 if n==0 then error(); exit(1); 95 else err_on_n (n-1); exit(1); 96 end 97 end 98 99 do 100 local function dummy (n) 101 if n > 0 then 102 assert(not pcall(err_on_n, n)) 103 dummy(n-1) 104 end 105 end 106 107 dummy(10) 108 end 109 110 _G.deep = nil -- "declaration" (used by 'all.lua') 111 112 global function deep (n) 113 if n>0 then deep(n-1) end 114 end 115 deep(10) 116 deep(180) 117 118 119 print"testing tail calls" 120 121 function deep (n) if n>0 then return deep(n-1) else return 101 end end 122 assert(deep(30000) == 101) 123 a = {} 124 function a:deep (n) if n>0 then return self:deep(n-1) else return 101 end end 125 assert(a:deep(30000) == 101) 126 127 do -- tail calls x varargs 128 local function foo (x, ...) local a = {...}; return x, a[1], a[2] end 129 130 local function foo1 (x) return foo(10, x, x + 1) end 131 132 local a, b, c = foo1(-2) 133 assert(a == 10 and b == -2 and c == -1) 134 135 -- tail calls x metamethods 136 local t = setmetatable({}, {__call = foo}) 137 local function foo2 (x) return t(10, x) end 138 a, b, c = foo2(100) 139 assert(a == t and b == 10 and c == 100) 140 141 a, b = (function () return foo() end)() 142 assert(a == nil and b == nil) 143 144 local X, Y, A 145 local function foo (x, y, ...) X = x; Y = y; A = {...} end 146 local function foo1 (...) return foo(...) end 147 148 local a, b, c = foo1() 149 assert(X == nil and Y == nil and #A == 0) 150 151 a, b, c = foo1(10) 152 assert(X == 10 and Y == nil and #A == 0) 153 154 a, b, c = foo1(10, 20) 155 assert(X == 10 and Y == 20 and #A == 0) 156 157 a, b, c = foo1(10, 20, 30) 158 assert(X == 10 and Y == 20 and #A == 1 and A[1] == 30) 159 end 160 161 162 do -- C-stack overflow while handling C-stack overflow 163 local function loop () 164 assert(pcall(loop)) 165 end 166 167 local err, msg = xpcall(loop, loop) 168 assert(not err and string.find(msg, "error")) 169 end 170 171 172 173 do -- tail calls x chain of __call 174 local n = 10000 -- depth 175 176 local function foo () 177 if n == 0 then return 1023 178 else n = n - 1; return foo() 179 end 180 end 181 182 -- build a chain of __call metamethods ending in function 'foo' 183 for i = 1, 15 do 184 foo = setmetatable({}, {__call = foo}) 185 end 186 187 -- call the first one as a tail call in a new coroutine 188 -- (to ensure stack is not preallocated) 189 assert(coroutine.wrap(function() return foo() end)() == 1023) 190 end 191 192 print('+') 193 194 195 do print"testing chains of '__call'" 196 local N = 15 197 local u = table.pack 198 for i = 1, N do 199 u = setmetatable({i}, {__call = u}) 200 end 201 202 local Res = u("a", "b", "c") 203 204 assert(Res.n == N + 3) 205 for i = 1, N do 206 assert(Res[i][1] == i) 207 end 208 assert(Res[N + 1] == "a" and Res[N + 2] == "b" and Res[N + 3] == "c") 209 210 local function u (...) 211 local n = debug.getinfo(1, 't').extraargs 212 assert(select("#", ...) == n) 213 return n 214 end 215 216 for i = 0, N do 217 assert(u() == i) 218 u = setmetatable({}, {__call = u}) 219 end 220 end 221 222 223 do -- testing chains too long 224 local a = {} 225 for i = 1, 16 do -- one too many 226 a = setmetatable({}, {__call = a}) 227 end 228 local status, msg = pcall(a) 229 assert(not status and string.find(msg, "too long")) 230 231 setmetatable(a, {__call = a}) -- infinite chain 232 local status, msg = pcall(a) 233 assert(not status and string.find(msg, "too long")) 234 235 -- again, with a tail call 236 local status, msg = pcall(function () return a() end) 237 assert(not status and string.find(msg, "too long")) 238 end 239 240 a = nil 241 (function (x) a=x end)(23) 242 assert(a == 23 and (function (x) return x*2 end)(20) == 40) 243 244 245 -- testing closures 246 247 -- fixed-point operator 248 local Z = function (le) 249 local function a (f) 250 return le(function (x) return f(f)(x) end) 251 end 252 return a(a) 253 end 254 255 256 -- non-recursive factorial 257 258 local F = function (f) 259 return function (n) 260 if n == 0 then return 1 261 else return n*f(n-1) end 262 end 263 end 264 265 local fat = Z(F) 266 267 assert(fat(0) == 1 and fat(4) == 24 and Z(F)(5)==5*Z(F)(4)) 268 269 local function g (z) 270 local function f (a,b,c,d) 271 return function (x,y) return a+b+c+d+a+x+y+z end 272 end 273 return f(z,z+1,z+2,z+3) 274 end 275 276 local f = g(10) 277 assert(f(9, 16) == 10+11+12+13+10+9+16+10) 278 279 print('+') 280 281 -- testing multiple returns 282 283 local function unlpack (t, i) 284 i = i or 1 285 if (i <= #t) then 286 return t[i], unlpack(t, i+1) 287 end 288 end 289 290 local function equaltab (t1, t2) 291 assert(#t1 == #t2) 292 for i = 1, #t1 do 293 assert(t1[i] == t2[i]) 294 end 295 end 296 297 local pack = function (...) return (table.pack(...)) end 298 299 local function f() return 1,2,30,4 end 300 local function ret2 (a,b) return a,b end 301 302 local a,b,c,d = unlpack{1,2,3} 303 assert(a==1 and b==2 and c==3 and d==nil) 304 a = {1,2,3,4,false,10,'alo',false,assert} 305 equaltab(pack(unlpack(a)), a) 306 equaltab(pack(unlpack(a), -1), {1,-1}) 307 a,b,c,d = ret2(f()), ret2(f()) 308 assert(a==1 and b==1 and c==2 and d==nil) 309 a,b,c,d = unlpack(pack(ret2(f()), ret2(f()))) 310 assert(a==1 and b==1 and c==2 and d==nil) 311 a,b,c,d = unlpack(pack(ret2(f()), (ret2(f())))) 312 assert(a==1 and b==1 and c==nil and d==nil) 313 314 a = ret2{ unlpack{1,2,3}, unlpack{3,2,1}, unlpack{"a", "b"}} 315 assert(a[1] == 1 and a[2] == 3 and a[3] == "a" and a[4] == "b") 316 317 318 -- testing calls with 'incorrect' arguments 319 rawget({}, "x", 1) 320 rawset({}, "x", 1, 2) 321 assert(math.sin(1,2) == math.sin(1)) 322 table.sort({10,9,8,4,19,23,0,0}, function (a,b) return a 0 and c or nil 390 end 391 end 392 393 local str = "local function foo () end; return 121" 394 assert(assert(load(get(str)))() == 121) 395 396 str = string.dump(load(str)) 397 assert(assert(load(get(str)))() == 121) 398 end 399 400 401 x = string.dump(load("x = 1; return x")) 402 a = assert(load(read1(x), nil, "b")) 403 assert(a() == 1 and _G.x == 1) 404 cannotload("attempt to load a binary chunk", load(read1(x), nil, "t")) 405 cannotload("attempt to load a binary chunk", load(x, nil, "t")) 406 _G.x = nil 407 408 assert(not pcall(string.dump, print)) -- no dump of C functions 409 410 cannotload("unexpected symbol", load(read1("*a = 123"))) 411 cannotload("unexpected symbol", load("*a = 123")) 412 cannotload("hhi", load(function () error("hhi") end)) 413 414 -- any value is valid for _ENV 415 assert(load("return _ENV", nil, nil, 123)() == 123) 416 417 418 -- load when _ENV is not first upvalue 419 global XX; local x 420 XX = 123 421 local function h () 422 local y=x -- use 'x', so that it becomes 1st upvalue 423 return XX -- global name 424 end 425 local d = string.dump(h) 426 x = load(d, "", "b") 427 assert(debug.getupvalue(x, 2) == '_ENV') 428 debug.setupvalue(x, 2, _G) 429 assert(x() == 123) 430 431 assert(assert(load("return XX + ...", nil, nil, {XX = 13}))(4) == 17) 432 XX = nil 433 434 -- test generic load with nested functions 435 x = [[ 436 return function (x) 437 return function (y) 438 return function (z) 439 return x+y+z 440 end 441 end 442 end 443 ]] 444 a = assert(load(read1(x), "read", "t")) 445 assert(a()(2)(3)(10) == 15) 446 447 -- repeat the test loading a binary chunk 448 x = string.dump(a) 449 a = assert(load(read1(x), "read", "b")) 450 assert(a()(2)(3)(10) == 15) 451 452 453 -- test for dump/undump with upvalues 454 local a, b = 20, 30 455 x = load(string.dump(function (x) 456 if x == "set" then a = 10+b; b = b+1 else 457 return a 458 end 459 end), "", "b", nil) 460 assert(x() == nil) 461 assert(debug.setupvalue(x, 1, "hi") == "a") 462 assert(x() == "hi") 463 assert(debug.setupvalue(x, 2, 13) == "b") 464 assert(not debug.setupvalue(x, 3, 10)) -- only 2 upvalues 465 x("set") 466 assert(x() == 23) 467 x("set") 468 assert(x() == 24) 469 470 -- test for dump/undump with many upvalues 471 do 472 local nup = 200 -- maximum number of local variables 473 local prog = {"local a1"} 474 for i = 2, nup do prog[#prog + 1] = ", a" .. i end 475 prog[#prog + 1] = " = 1" 476 for i = 2, nup do prog[#prog + 1] = ", " .. i end 477 local sum = 1 478 prog[#prog + 1] = "; return function () return a1" 479 for i = 2, nup do prog[#prog + 1] = " + a" .. i; sum = sum + i end 480 prog[#prog + 1] = " end" 481 prog = table.concat(prog) 482 local f = assert(load(prog))() 483 assert(f() == sum) 484 485 f = load(string.dump(f)) -- main chunk now has many upvalues 486 local a = 10 487 local h = function () return a end 488 for i = 1, nup do 489 debug.upvaluejoin(f, i, h, 1) 490 end 491 assert(f() == 10 * nup) 492 end 493 494 -- test for long method names 495 do 496 local t = {x = 1} 497 function t:_`012345678901234567890123456789012345678901234567890123456789` () 498 return self.x 499 end 500 assert(t:_`012345678901234567890123456789012345678901234567890123456789`() == 1) 501 end 502 503 504 -- test for bug in parameter adjustment 505 assert((function () return nil end)(4) == nil) 506 assert((function () local a; return a end)(4) == nil) 507 assert((function (a) return a end)() == nil) 508 509 510 print("testing binary chunks") 511 do 512 local headformat = "c4BBc6BiBI4BjBn" 513 local header = { -- header components 514 "\27Lua", -- signature 515 0x55, -- version 5.5 (0x55) 516 0, -- format 517 "\x19\x93\r\n\x1a\n", -- a binary string 518 string.packsize("i"), -- size of an int 519 -0x5678, -- an int 520 4, -- size of an instruction 521 0x12345678, -- an instruction (4 bytes) 522 string.packsize("j"), -- size of a Lua integer 523 -0x5678, -- a Lua integer 524 string.packsize("n"), -- size of a Lua float 525 -370.5, -- a Lua float 526 } 527 528 local c = string.dump(function () 529 local a = 1; local b = 3; 530 local f = function () return a + b + _ENV.c; end -- upvalues 531 local s1 = "a constant" 532 local s2 = "another constant" 533 return a + b * 3 534 end) 535 536 assert(assert(load(c))() == 10) 537 538 -- check header 539 local t = {string.unpack(headformat, c)} 540 for i = 1, #header do 541 assert(t[i] == header[i]) 542 end 543 544 -- Testing corrupted header. 545 -- A single wrong byte in the head invalidates the chunk, 546 -- except for the Lua float check. (If numbers are long double, 547 -- the representation may need padding, and changing that padding 548 -- will not invalidate the chunk.) 549 local headlen = string.packsize(headformat) 550 headlen = headlen - string.packsize("n") -- remove float check 551 for i = 1, headlen do 552 local s = string.sub(c, 1, i - 1) .. 553 string.char((string.byte(string.sub(c, i, i)) + 1) & 0xFF) .. 554 string.sub(c, i + 1, -1) 555 assert(#s == #c and s ~= c) 556 assert(not load(s)) 557 end 558 559 -- loading truncated binary chunks 560 for i = 1, #c - 1 do 561 local st, msg = load(string.sub(c, 1, i)) 562 assert(not st and string.find(msg, "truncated")) 563 end 564 end 565 566 567 do -- check reuse of strings in dumps 568 local str = "|" .. string.rep("X", 50) .. "|" 569 local foo = load(string.format([[ 570 local str = "%s" 571 return { 572 function () return str end, 573 function () return str end, 574 function () return str end 575 } 576 ]], str)) 577 -- count occurrences of 'str' inside the dump 578 local dump = string.dump(foo) 579 local _, count = string.gsub(dump, str, {}) 580 -- there should be only two occurrences: 581 -- one inside the source, other the string itself. 582 assert(count == 2) 583 584 if T then -- check reuse of strings in undump 585 local funcs = load(dump)() 586 assert(string.format("%p", T.listk(funcs[1])[1]) == 587 string.format("%p", T.listk(funcs[3])[1])) 588 end 589 end 590 591 592 do -- test limit of multiple returns (254 values) 593 local code = "return 10" .. string.rep(",10", 253) 594 local res = {assert(load(code))()} 595 assert(#res == 254 and res[254] == 10) 596 597 code = code .. ",10" 598 local status, msg = load(code) 599 assert(not status and string.find(msg, "too many returns")) 600 end 601 602 print('OK') 603 return deep