calls.lua 9.4 KB

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