calls.lua 6.3 KB

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