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