github.com/assemblaj/gopher-lua@v0.0.0-20221116224352-d57295a0d9e8/_lua5.1-tests/calls.lua (about) 1 print("testing functions and calls") 2 3 -- get the opportunity to test 'type' too ;) 4 5 assert(type(1<2) == 'boolean') 6 assert(type(true) == 'boolean' and type(false) == 'boolean') 7 assert(type(nil) == 'nil' and type(-3) == 'number' and type'x' == 'string' and 8 type{} == 'table' and type(type) == 'function') 9 10 assert(type(assert) == type(print)) 11 f = nil 12 function f (x) return a:x (x) end 13 assert(type(f) == 'function') 14 15 16 -- testing local-function recursion 17 fact = false 18 do 19 local res = 1 20 local function fact (n) 21 if n==0 then return res 22 else return n*fact(n-1) 23 end 24 end 25 assert(fact(5) == 120) 26 end 27 assert(fact == false) 28 29 -- testing declarations 30 a = {i = 10} 31 self = 20 32 function a:x (x) return x+self.i end 33 function a.y (x) return x+self end 34 35 assert(a:x(1)+10 == a.y(1)) 36 37 a.t = {i=-100} 38 a["t"].x = function (self, a,b) return self.i+a+b end 39 40 assert(a.t:x(2,3) == -95) 41 42 do 43 local a = {x=0} 44 function a:add (x) self.x, a.y = self.x+x, 20; return self end 45 assert(a:add(10):add(20):add(30).x == 60 and a.y == 20) 46 end 47 48 local a = {b={c={}}} 49 50 function a.b.c.f1 (x) return x+1 end 51 function a.b.c:f2 (x,y) self[x] = y end 52 assert(a.b.c.f1(4) == 5) 53 a.b.c:f2('k', 12); assert(a.b.c.k == 12) 54 55 print('+') 56 57 t = nil -- 'declare' t 58 function f(a,b,c) local d = 'a'; t={a,b,c,d} end 59 60 f( -- this line change must be valid 61 1,2) 62 assert(t[1] == 1 and t[2] == 2 and t[3] == nil and t[4] == 'a') 63 f(1,2, -- this one too 64 3,4) 65 assert(t[1] == 1 and t[2] == 2 and t[3] == 3 and t[4] == 'a') 66 67 function fat(x) 68 if x <= 1 then return 1 69 else return x*loadstring("return fat(" .. x-1 .. ")")() 70 end 71 end 72 73 assert(loadstring "loadstring 'assert(fat(6)==720)' () ")() 74 a = loadstring('return fat(5), 3') 75 a,b = a() 76 assert(a == 120 and b == 3) 77 print('+') 78 79 function err_on_n (n) 80 if n==0 then error(); exit(1); 81 else err_on_n (n-1); exit(1); 82 end 83 end 84 85 do 86 function dummy (n) 87 if n > 0 then 88 assert(not pcall(err_on_n, n)) 89 dummy(n-1) 90 end 91 end 92 end 93 94 dummy(10) 95 96 function deep (n) 97 if n>0 then deep(n-1) end 98 end 99 deep(10) 100 deep(200) 101 102 -- testing tail call 103 function deep (n) if n>0 then return deep(n-1) else return 101 end end 104 assert(deep(30000) == 101) 105 a = {} 106 function a:deep (n) if n>0 then return self:deep(n-1) else return 101 end end 107 assert(a:deep(30000) == 101) 108 109 print('+') 110 111 112 a = nil 113 (function (x) a=x end)(23) 114 assert(a == 23 and (function (x) return x*2 end)(20) == 40) 115 116 117 local x,y,z,a 118 a = {}; lim = 2000 119 for i=1, lim do a[i]=i end 120 assert(select(lim, unpack(a)) == lim and select('#', unpack(a)) == lim) 121 x = unpack(a) 122 assert(x == 1) 123 x = {unpack(a)} 124 assert(table.getn(x) == lim and x[1] == 1 and x[lim] == lim) 125 x = {unpack(a, lim-2)} 126 assert(table.getn(x) == 3 and x[1] == lim-2 and x[3] == lim) 127 x = {unpack(a, 10, 6)} 128 assert(next(x) == nil) -- no elements 129 x = {unpack(a, 11, 10)} 130 assert(next(x) == nil) -- no elements 131 x,y = unpack(a, 10, 10) 132 assert(x == 10 and y == nil) 133 x,y,z = unpack(a, 10, 11) 134 assert(x == 10 and y == 11 and z == nil) 135 a,x = unpack{1} 136 assert(a==1 and x==nil) 137 a,x = unpack({1,2}, 1, 1) 138 assert(a==1 and x==nil) 139 140 141 -- testing closures 142 143 -- fixed-point operator 144 Y = function (le) 145 local function a (f) 146 return le(function (x) return f(f)(x) end) 147 end 148 return a(a) 149 end 150 151 152 -- non-recursive factorial 153 154 F = function (f) 155 return function (n) 156 if n == 0 then return 1 157 else return n*f(n-1) end 158 end 159 end 160 161 fat = Y(F) 162 163 assert(fat(0) == 1 and fat(4) == 24 and Y(F)(5)==5*Y(F)(4)) 164 165 local function g (z) 166 local function f (a,b,c,d) 167 return function (x,y) return a+b+c+d+a+x+y+z end 168 end 169 return f(z,z+1,z+2,z+3) 170 end 171 172 f = g(10) 173 assert(f(9, 16) == 10+11+12+13+10+9+16+10) 174 175 Y, F, f = nil 176 print('+') 177 178 -- testing multiple returns 179 180 function unlpack (t, i) 181 i = i or 1 182 if (i <= table.getn(t)) then 183 return t[i], unlpack(t, i+1) 184 end 185 end 186 187 function equaltab (t1, t2) 188 assert(table.getn(t1) == table.getn(t2)) 189 for i,v1 in ipairs(t1) do 190 assert(v1 == t2[i]) 191 end 192 end 193 194 local function pack (...) 195 local x = {...} 196 x.n = select('#', ...) 197 return x 198 end 199 200 function f() return 1,2,30,4 end 201 function ret2 (a,b) return a,b end 202 203 local a,b,c,d = unlpack{1,2,3} 204 assert(a==1 and b==2 and c==3 and d==nil) 205 a = {1,2,3,4,false,10,'alo',false,assert} 206 equaltab(pack(unlpack(a)), a) 207 equaltab(pack(unlpack(a), -1), {1,-1}) 208 a,b,c,d = ret2(f()), ret2(f()) 209 assert(a==1 and b==1 and c==2 and d==nil) 210 a,b,c,d = unlpack(pack(ret2(f()), ret2(f()))) 211 assert(a==1 and b==1 and c==2 and d==nil) 212 a,b,c,d = unlpack(pack(ret2(f()), (ret2(f())))) 213 assert(a==1 and b==1 and c==nil and d==nil) 214 215 a = ret2{ unlpack{1,2,3}, unlpack{3,2,1}, unlpack{"a", "b"}} 216 assert(a[1] == 1 and a[2] == 3 and a[3] == "a" and a[4] == "b") 217 218 219 -- testing calls with 'incorrect' arguments 220 rawget({}, "x", 1) 221 rawset({}, "x", 1, 2) 222 assert(math.sin(1,2) == math.sin(1)) 223 table.sort({10,9,8,4,19,23,0,0}, function (a,b) return a<b end, "extra arg") 224 225 226 -- test for generic load 227 x = "-- a comment\n x = 10 + \n23; \ 228 local a = function () x = 'hi' end; \ 229 return ''" 230 local i = 0 231 function read1 (x) 232 return function () 233 collectgarbage() 234 i=i+1 235 return string.sub(x, i, i) 236 end 237 end 238 239 a = assert(load(read1(x), "modname")) 240 assert(a() == "" and _G.x == 33) 241 assert(debug.getinfo(a).source == "modname") 242 243 -- x = string.dump(loadstring("x = 1; return x")) 244 -- i = 0 245 -- a = assert(load(read1(x))) 246 -- assert(a() == 1 and _G.x == 1) 247 248 -- i = 0 249 -- local a, b = load(read1("*a = 123")) 250 -- assert(not a and type(b) == "string" and i == 2) 251 -- 252 -- a, b = load(function () error("hhi") end) 253 -- assert(not a and string.find(b, "hhi")) 254 255 -- test generic load with nested functions 256 i = 0 257 x = [[ 258 return function (x) 259 return function (y) 260 return function (z) 261 return x+y+z 262 end 263 end 264 end 265 ]] 266 267 a = assert(load(read1(x))) 268 assert(a()(2)(3)(10) == 15) 269 270 271 -- test for dump/undump with upvalues 272 -- local a, b = 20, 30 273 -- x = loadstring(string.dump(function (x) 274 -- if x == "set" then a = 10+b; b = b+1 else 275 -- return a 276 -- end 277 -- end)) 278 -- assert(x() == nil) 279 -- assert(debug.setupvalue(x, 1, "hi") == "a") 280 -- assert(x() == "hi") 281 -- assert(debug.setupvalue(x, 2, 13) == "b") 282 -- assert(not debug.setupvalue(x, 3, 10)) -- only 2 upvalues 283 -- x("set") 284 -- assert(x() == 23) 285 -- x("set") 286 -- assert(x() == 24) 287 288 289 -- test for bug in parameter adjustment 290 assert((function () return nil end)(4) == nil) 291 assert((function () local a; return a end)(4) == nil) 292 assert((function (a) return a end)() == nil) 293 294 print('OK') 295 return deep