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