calls.lua 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. print("testing functions and calls")
  2. local debug = require "debug"
  3. -- get the opportunity to test 'type' too ;)
  4. assert(type(1<2) == 'boolean')
  5. assert(type(true) == 'boolean' and type(false) == 'boolean')
  6. assert(type(nil) == 'nil' and type(-3) == 'number' and type'x' == 'string' and
  7. type{} == 'table' and type(type) == 'function')
  8. assert(type(assert) == type(print))
  9. f = nil
  10. function f (x) return a:x (x) end
  11. assert(type(f) == 'function')
  12. -- testing local-function recursion
  13. fact = false
  14. do
  15. local res = 1
  16. local function fact (n)
  17. if n==0 then return res
  18. else return n*fact(n-1)
  19. end
  20. end
  21. assert(fact(5) == 120)
  22. end
  23. assert(fact == false)
  24. -- testing declarations
  25. a = {i = 10}
  26. self = 20
  27. function a:x (x) return x+self.i end
  28. function a.y (x) return x+self end
  29. assert(a:x(1)+10 == a.y(1))
  30. a.t = {i=-100}
  31. a["t"].x = function (self, a,b) return self.i+a+b end
  32. assert(a.t:x(2,3) == -95)
  33. do
  34. local a = {x=0}
  35. function a:add (x) self.x, a.y = self.x+x, 20; return self end
  36. assert(a:add(10):add(20):add(30).x == 60 and a.y == 20)
  37. end
  38. local a = {b={c={}}}
  39. function a.b.c.f1 (x) return x+1 end
  40. function a.b.c:f2 (x,y) self[x] = y end
  41. assert(a.b.c.f1(4) == 5)
  42. a.b.c:f2('k', 12); assert(a.b.c.k == 12)
  43. print('+')
  44. t = nil -- 'declare' t
  45. function f(a,b,c) local d = 'a'; t={a,b,c,d} end
  46. f( -- this line change must be valid
  47. 1,2)
  48. assert(t[1] == 1 and t[2] == 2 and t[3] == nil and t[4] == 'a')
  49. f(1,2, -- this one too
  50. 3,4)
  51. assert(t[1] == 1 and t[2] == 2 and t[3] == 3 and t[4] == 'a')
  52. function fat(x)
  53. if x <= 1 then return 1
  54. else return x*load("return fat(" .. x-1 .. ")")()
  55. end
  56. end
  57. assert(load "load 'assert(fat(6)==720)' () ")()
  58. a = load('return fat(5), 3')
  59. a,b = a()
  60. assert(a == 120 and b == 3)
  61. print('+')
  62. function err_on_n (n)
  63. if n==0 then error(); exit(1);
  64. else err_on_n (n-1); exit(1);
  65. end
  66. end
  67. do
  68. function dummy (n)
  69. if n > 0 then
  70. assert(not pcall(err_on_n, n))
  71. dummy(n-1)
  72. end
  73. end
  74. end
  75. dummy(10)
  76. function deep (n)
  77. if n>0 then deep(n-1) end
  78. end
  79. deep(10)
  80. deep(200)
  81. -- testing tail call
  82. function deep (n) if n>0 then return deep(n-1) else return 101 end end
  83. assert(deep(30000) == 101)
  84. a = {}
  85. function a:deep (n) if n>0 then return self:deep(n-1) else return 101 end end
  86. assert(a:deep(30000) == 101)
  87. print('+')
  88. a = nil
  89. (function (x) a=x end)(23)
  90. assert(a == 23 and (function (x) return x*2 end)(20) == 40)
  91. -- testing closures
  92. -- fixed-point operator
  93. Z = function (le)
  94. local function a (f)
  95. return le(function (x) return f(f)(x) end)
  96. end
  97. return a(a)
  98. end
  99. -- non-recursive factorial
  100. F = function (f)
  101. return function (n)
  102. if n == 0 then return 1
  103. else return n*f(n-1) end
  104. end
  105. end
  106. fat = Z(F)
  107. assert(fat(0) == 1 and fat(4) == 24 and Z(F)(5)==5*Z(F)(4))
  108. local function g (z)
  109. local function f (a,b,c,d)
  110. return function (x,y) return a+b+c+d+a+x+y+z end
  111. end
  112. return f(z,z+1,z+2,z+3)
  113. end
  114. f = g(10)
  115. assert(f(9, 16) == 10+11+12+13+10+9+16+10)
  116. Z, F, f = nil
  117. print('+')
  118. -- testing multiple returns
  119. function unlpack (t, i)
  120. i = i or 1
  121. if (i <= #t) then
  122. return t[i], unlpack(t, i+1)
  123. end
  124. end
  125. function equaltab (t1, t2)
  126. assert(#t1 == #t2)
  127. for i = 1, #t1 do
  128. assert(t1[i] == t2[i])
  129. end
  130. end
  131. local pack = function (...) return (table.pack(...)) end
  132. function f() return 1,2,30,4 end
  133. function ret2 (a,b) return a,b end
  134. local a,b,c,d = unlpack{1,2,3}
  135. assert(a==1 and b==2 and c==3 and d==nil)
  136. a = {1,2,3,4,false,10,'alo',false,assert}
  137. equaltab(pack(unlpack(a)), a)
  138. equaltab(pack(unlpack(a), -1), {1,-1})
  139. a,b,c,d = ret2(f()), ret2(f())
  140. assert(a==1 and b==1 and c==2 and d==nil)
  141. a,b,c,d = unlpack(pack(ret2(f()), ret2(f())))
  142. assert(a==1 and b==1 and c==2 and d==nil)
  143. a,b,c,d = unlpack(pack(ret2(f()), (ret2(f()))))
  144. assert(a==1 and b==1 and c==nil and d==nil)
  145. a = ret2{ unlpack{1,2,3}, unlpack{3,2,1}, unlpack{"a", "b"}}
  146. assert(a[1] == 1 and a[2] == 3 and a[3] == "a" and a[4] == "b")
  147. -- testing calls with 'incorrect' arguments
  148. rawget({}, "x", 1)
  149. rawset({}, "x", 1, 2)
  150. assert(math.sin(1,2) == math.sin(1))
  151. table.sort({10,9,8,4,19,23,0,0}, function (a,b) return a<b end, "extra arg")
  152. -- test for generic load
  153. local x = "-- a comment\0\0\0\n x = 10 + \n23; \
  154. local a = function () x = 'hi' end; \
  155. return '\0'"
  156. function read1 (x)
  157. local i = 0
  158. return function ()
  159. collectgarbage()
  160. i=i+1
  161. return string.sub(x, i, i)
  162. end
  163. end
  164. function cannotload (msg, a,b)
  165. assert(not a and string.find(b, msg))
  166. end
  167. a = assert(load(read1(x), "modname", "t", _G))
  168. assert(a() == "\0" and _G.x == 33)
  169. assert(debug.getinfo(a).source == "modname")
  170. -- cannot read text in binary mode
  171. cannotload("attempt to load a text chunk", load(read1(x), "modname", "b", {}))
  172. cannotload("attempt to load a text chunk", load(x, "modname", "b"))
  173. a = assert(load(function () return nil end))
  174. a() -- empty chunk
  175. assert(not load(function () return true end))
  176. -- small bug
  177. local t = {nil, "return ", "3"}
  178. f, msg = load(function () return table.remove(t, 1) end)
  179. assert(f() == nil) -- should read the empty chunk
  180. -- another small bug (in 5.2.1)
  181. f = load(string.dump(function () return 1 end), nil, "b", {})
  182. assert(type(f) == "function" and f() == 1)
  183. x = string.dump(load("x = 1; return x"))
  184. a = assert(load(read1(x), nil, "b"))
  185. assert(a() == 1 and _G.x == 1)
  186. cannotload("attempt to load a binary chunk", load(read1(x), nil, "t"))
  187. cannotload("attempt to load a binary chunk", load(x, nil, "t"))
  188. assert(not pcall(string.dump, print)) -- no dump of C functions
  189. cannotload("unexpected symbol", load(read1("*a = 123")))
  190. cannotload("unexpected symbol", load("*a = 123"))
  191. cannotload("hhi", load(function () error("hhi") end))
  192. -- any value is valid for _ENV
  193. assert(load("return _ENV", nil, nil, 123)() == 123)
  194. -- load when _ENV is not first upvalue
  195. local x; XX = 123
  196. local function h ()
  197. local y=x -- use 'x', so that it becomes 1st upvalue
  198. return XX -- global name
  199. end
  200. local d = string.dump(h)
  201. x = load(d, "", "b")
  202. assert(debug.getupvalue(x, 2) == '_ENV')
  203. debug.setupvalue(x, 2, _G)
  204. assert(x() == 123)
  205. assert(assert(load("return XX + ...", nil, nil, {XX = 13}))(4) == 17)
  206. -- test generic load with nested functions
  207. x = [[
  208. return function (x)
  209. return function (y)
  210. return function (z)
  211. return x+y+z
  212. end
  213. end
  214. end
  215. ]]
  216. a = assert(load(read1(x)))
  217. assert(a()(2)(3)(10) == 15)
  218. -- test for dump/undump with upvalues
  219. local a, b = 20, 30
  220. x = load(string.dump(function (x)
  221. if x == "set" then a = 10+b; b = b+1 else
  222. return a
  223. end
  224. end))
  225. assert(x() == nil)
  226. assert(debug.setupvalue(x, 1, "hi") == "a")
  227. assert(x() == "hi")
  228. assert(debug.setupvalue(x, 2, 13) == "b")
  229. assert(not debug.setupvalue(x, 3, 10)) -- only 2 upvalues
  230. x("set")
  231. assert(x() == 23)
  232. x("set")
  233. assert(x() == 24)
  234. -- test for bug in parameter adjustment
  235. assert((function () return nil end)(4) == nil)
  236. assert((function () local a; return a end)(4) == nil)
  237. assert((function (a) return a end)() == nil)
  238. print('OK')
  239. return deep