events.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. -- $Id: testes/events.lua $
  2. -- See Copyright Notice in file all.lua
  3. print('testing metatables')
  4. local debug = require'debug'
  5. X = 20; B = 30
  6. _ENV = setmetatable({}, {__index=_G})
  7. collectgarbage()
  8. X = X+10
  9. assert(X == 30 and _G.X == 20)
  10. B = false
  11. assert(B == false)
  12. _ENV["B"] = undef
  13. assert(B == 30)
  14. assert(getmetatable{} == nil)
  15. assert(getmetatable(4) == nil)
  16. assert(getmetatable(nil) == nil)
  17. a={name = "NAME"}; setmetatable(a, {__metatable = "xuxu",
  18. __tostring=function(x) return x.name end})
  19. assert(getmetatable(a) == "xuxu")
  20. assert(tostring(a) == "NAME")
  21. -- cannot change a protected metatable
  22. assert(pcall(setmetatable, a, {}) == false)
  23. a.name = "gororoba"
  24. assert(tostring(a) == "gororoba")
  25. local a, t = {10,20,30; x="10", y="20"}, {}
  26. assert(setmetatable(a,t) == a)
  27. assert(getmetatable(a) == t)
  28. assert(setmetatable(a,nil) == a)
  29. assert(getmetatable(a) == nil)
  30. assert(setmetatable(a,t) == a)
  31. function f (t, i, e)
  32. assert(not e)
  33. local p = rawget(t, "parent")
  34. return (p and p[i]+3), "dummy return"
  35. end
  36. t.__index = f
  37. a.parent = {z=25, x=12, [4] = 24}
  38. assert(a[1] == 10 and a.z == 28 and a[4] == 27 and a.x == "10")
  39. collectgarbage()
  40. a = setmetatable({}, t)
  41. function f(t, i, v) rawset(t, i, v-3) end
  42. setmetatable(t, t) -- causes a bug in 5.1 !
  43. t.__newindex = f
  44. a[1] = 30; a.x = "101"; a[5] = 200
  45. assert(a[1] == 27 and a.x == 98 and a[5] == 197)
  46. do -- bug in Lua 5.3.2
  47. local mt = {}
  48. mt.__newindex = mt
  49. local t = setmetatable({}, mt)
  50. t[1] = 10 -- will segfault on some machines
  51. assert(mt[1] == 10)
  52. end
  53. local c = {}
  54. a = setmetatable({}, t)
  55. t.__newindex = c
  56. t.__index = c
  57. a[1] = 10; a[2] = 20; a[3] = 90;
  58. for i = 4, 20 do a[i] = i * 10 end
  59. assert(a[1] == 10 and a[2] == 20 and a[3] == 90)
  60. for i = 4, 20 do assert(a[i] == i * 10) end
  61. assert(next(a) == nil)
  62. do
  63. local a;
  64. a = setmetatable({}, {__index = setmetatable({},
  65. {__index = setmetatable({},
  66. {__index = function (_,n) return a[n-3]+4, "lixo" end})})})
  67. a[0] = 20
  68. for i=0,10 do
  69. assert(a[i*3] == 20 + i*4)
  70. end
  71. end
  72. do -- newindex
  73. local foi
  74. local a = {}
  75. for i=1,10 do a[i] = 0; a['a'..i] = 0; end
  76. setmetatable(a, {__newindex = function (t,k,v) foi=true; rawset(t,k,v) end})
  77. foi = false; a[1]=0; assert(not foi)
  78. foi = false; a['a1']=0; assert(not foi)
  79. foi = false; a['a11']=0; assert(foi)
  80. foi = false; a[11]=0; assert(foi)
  81. foi = false; a[1]=undef; assert(not foi)
  82. a[1] = undef
  83. foi = false; a[1]=nil; assert(foi)
  84. end
  85. setmetatable(t, nil)
  86. function f (t, ...) return t, {...} end
  87. t.__call = f
  88. do
  89. local x,y = a(table.unpack{'a', 1})
  90. assert(x==a and y[1]=='a' and y[2]==1 and y[3]==undef)
  91. x,y = a()
  92. assert(x==a and y[1]==undef)
  93. end
  94. local b = setmetatable({}, t)
  95. setmetatable(b,t)
  96. function f(op)
  97. return function (...) cap = {[0] = op, ...} ; return (...) end
  98. end
  99. t.__add = f("add")
  100. t.__sub = f("sub")
  101. t.__mul = f("mul")
  102. t.__div = f("div")
  103. t.__idiv = f("idiv")
  104. t.__mod = f("mod")
  105. t.__unm = f("unm")
  106. t.__pow = f("pow")
  107. t.__len = f("len")
  108. t.__band = f("band")
  109. t.__bor = f("bor")
  110. t.__bxor = f("bxor")
  111. t.__shl = f("shl")
  112. t.__shr = f("shr")
  113. t.__bnot = f("bnot")
  114. -- Some tests are done inside small anonymous functions to ensure
  115. -- that constants go to constant table even in debug compilation,
  116. -- when the constant table is very small.
  117. assert(b+5 == b)
  118. assert(cap[0] == "add" and cap[1] == b and cap[2] == 5 and cap[3]==undef)
  119. assert(5.2 + b == 5.2)
  120. assert(cap[0] == "add" and cap[1] == 5.2 and cap[2] == b and cap[3]==undef)
  121. assert(b+'5' == b)
  122. assert(cap[0] == "add" and cap[1] == b and cap[2] == '5' and cap[3]==undef)
  123. assert(5+b == 5)
  124. assert(cap[0] == "add" and cap[1] == 5 and cap[2] == b and cap[3]==undef)
  125. assert('5'+b == '5')
  126. assert(cap[0] == "add" and cap[1] == '5' and cap[2] == b and cap[3]==undef)
  127. b=b-3; assert(getmetatable(b) == t)
  128. assert(cap[0] == "sub" and cap[1] == b and cap[2] == 3 and cap[3]==undef)
  129. assert(5-a == 5)
  130. assert(cap[0] == "sub" and cap[1] == 5 and cap[2] == a and cap[3]==undef)
  131. assert('5'-a == '5')
  132. assert(cap[0] == "sub" and cap[1] == '5' and cap[2] == a and cap[3]==undef)
  133. assert(a*a == a)
  134. assert(cap[0] == "mul" and cap[1] == a and cap[2] == a and cap[3]==undef)
  135. assert(a/0 == a)
  136. assert(cap[0] == "div" and cap[1] == a and cap[2] == 0 and cap[3]==undef)
  137. assert(a%2 == a)
  138. assert(cap[0] == "mod" and cap[1] == a and cap[2] == 2 and cap[3]==undef)
  139. assert(a // (1/0) == a)
  140. assert(cap[0] == "idiv" and cap[1] == a and cap[2] == 1/0 and cap[3]==undef)
  141. ;(function () assert(a & "hi" == a) end)()
  142. assert(cap[0] == "band" and cap[1] == a and cap[2] == "hi" and cap[3]==undef)
  143. ;(function () assert(10 & a == 10) end)()
  144. assert(cap[0] == "band" and cap[1] == 10 and cap[2] == a and cap[3]==undef)
  145. ;(function () assert(a | 10 == a) end)()
  146. assert(cap[0] == "bor" and cap[1] == a and cap[2] == 10 and cap[3]==undef)
  147. assert(a | "hi" == a)
  148. assert(cap[0] == "bor" and cap[1] == a and cap[2] == "hi" and cap[3]==undef)
  149. assert("hi" ~ a == "hi")
  150. assert(cap[0] == "bxor" and cap[1] == "hi" and cap[2] == a and cap[3]==undef)
  151. ;(function () assert(10 ~ a == 10) end)()
  152. assert(cap[0] == "bxor" and cap[1] == 10 and cap[2] == a and cap[3]==undef)
  153. assert(-a == a)
  154. assert(cap[0] == "unm" and cap[1] == a)
  155. assert(a^4 == a)
  156. assert(cap[0] == "pow" and cap[1] == a and cap[2] == 4 and cap[3]==undef)
  157. assert(a^'4' == a)
  158. assert(cap[0] == "pow" and cap[1] == a and cap[2] == '4' and cap[3]==undef)
  159. assert(4^a == 4)
  160. assert(cap[0] == "pow" and cap[1] == 4 and cap[2] == a and cap[3]==undef)
  161. assert('4'^a == '4')
  162. assert(cap[0] == "pow" and cap[1] == '4' and cap[2] == a and cap[3]==undef)
  163. assert(#a == a)
  164. assert(cap[0] == "len" and cap[1] == a)
  165. assert(~a == a)
  166. assert(cap[0] == "bnot" and cap[1] == a)
  167. assert(a << 3 == a)
  168. assert(cap[0] == "shl" and cap[1] == a and cap[2] == 3)
  169. assert(1.5 >> a == 1.5)
  170. assert(cap[0] == "shr" and cap[1] == 1.5 and cap[2] == a)
  171. -- test for rawlen
  172. t = setmetatable({1,2,3}, {__len = function () return 10 end})
  173. assert(#t == 10 and rawlen(t) == 3)
  174. assert(rawlen"abc" == 3)
  175. assert(not pcall(rawlen, io.stdin))
  176. assert(not pcall(rawlen, 34))
  177. assert(not pcall(rawlen))
  178. -- rawlen for long strings
  179. assert(rawlen(string.rep('a', 1000)) == 1000)
  180. t = {}
  181. t.__lt = function (a,b,c)
  182. collectgarbage()
  183. assert(c == nil)
  184. if type(a) == 'table' then a = a.x end
  185. if type(b) == 'table' then b = b.x end
  186. return a<b, "dummy"
  187. end
  188. t.__le = function (a,b,c)
  189. assert(c == nil)
  190. if type(a) == 'table' then a = a.x end
  191. if type(b) == 'table' then b = b.x end
  192. return a<=b, "dummy"
  193. end
  194. function Op(x) return setmetatable({x=x}, t) end
  195. local function test ()
  196. assert(not(Op(1)<Op(1)) and (Op(1)<Op(2)) and not(Op(2)<Op(1)))
  197. assert(not(1 < Op(1)) and (Op(1) < 2) and not(2 < Op(1)))
  198. assert(not(Op('a')<Op('a')) and (Op('a')<Op('b')) and not(Op('b')<Op('a')))
  199. assert(not('a' < Op('a')) and (Op('a') < 'b') and not(Op('b') < Op('a')))
  200. assert((Op(1)<=Op(1)) and (Op(1)<=Op(2)) and not(Op(2)<=Op(1)))
  201. assert((Op('a')<=Op('a')) and (Op('a')<=Op('b')) and not(Op('b')<=Op('a')))
  202. assert(not(Op(1)>Op(1)) and not(Op(1)>Op(2)) and (Op(2)>Op(1)))
  203. assert(not(Op('a')>Op('a')) and not(Op('a')>Op('b')) and (Op('b')>Op('a')))
  204. assert((Op(1)>=Op(1)) and not(Op(1)>=Op(2)) and (Op(2)>=Op(1)))
  205. assert((1 >= Op(1)) and not(1 >= Op(2)) and (Op(2) >= 1))
  206. assert((Op('a')>=Op('a')) and not(Op('a')>=Op('b')) and (Op('b')>=Op('a')))
  207. assert(('a' >= Op('a')) and not(Op('a') >= 'b') and (Op('b') >= Op('a')))
  208. end
  209. test()
  210. -- test `partial order'
  211. local function rawSet(x)
  212. local y = {}
  213. for _,k in pairs(x) do y[k] = 1 end
  214. return y
  215. end
  216. local function Set(x)
  217. return setmetatable(rawSet(x), t)
  218. end
  219. t.__lt = function (a,b)
  220. for k in pairs(a) do
  221. if not b[k] then return false end
  222. b[k] = undef
  223. end
  224. return next(b) ~= nil
  225. end
  226. t.__le = function (a,b)
  227. for k in pairs(a) do
  228. if not b[k] then return false end
  229. end
  230. return true
  231. end
  232. assert(Set{1,2,3} < Set{1,2,3,4})
  233. assert(not(Set{1,2,3,4} < Set{1,2,3,4}))
  234. assert((Set{1,2,3,4} <= Set{1,2,3,4}))
  235. assert((Set{1,2,3,4} >= Set{1,2,3,4}))
  236. assert(not (Set{1,3} <= Set{3,5}))
  237. assert(not(Set{1,3} <= Set{3,5}))
  238. assert(not(Set{1,3} >= Set{3,5}))
  239. t.__eq = function (a,b)
  240. for k in pairs(a) do
  241. if not b[k] then return false end
  242. b[k] = undef
  243. end
  244. return next(b) == nil
  245. end
  246. local s = Set{1,3,5}
  247. assert(s == Set{3,5,1})
  248. assert(not rawequal(s, Set{3,5,1}))
  249. assert(rawequal(s, s))
  250. assert(Set{1,3,5,1} == rawSet{3,5,1})
  251. assert(rawSet{1,3,5,1} == Set{3,5,1})
  252. assert(Set{1,3,5} ~= Set{3,5,1,6})
  253. -- '__eq' is not used for table accesses
  254. t[Set{1,3,5}] = 1
  255. assert(t[Set{1,3,5}] == undef)
  256. if not T then
  257. (Message or print)('\n >>> testC not active: skipping tests for \z
  258. userdata <<<\n')
  259. else
  260. local u1 = T.newuserdata(0, 1)
  261. local u2 = T.newuserdata(0, 1)
  262. local u3 = T.newuserdata(0, 1)
  263. assert(u1 ~= u2 and u1 ~= u3)
  264. debug.setuservalue(u1, 1);
  265. debug.setuservalue(u2, 2);
  266. debug.setuservalue(u3, 1);
  267. debug.setmetatable(u1, {__eq = function (a, b)
  268. return debug.getuservalue(a) == debug.getuservalue(b)
  269. end})
  270. debug.setmetatable(u2, {__eq = function (a, b)
  271. return true
  272. end})
  273. assert(u1 == u3 and u3 == u1 and u1 ~= u2)
  274. assert(u2 == u1 and u2 == u3 and u3 == u2)
  275. assert(u2 ~= {}) -- different types cannot be equal
  276. local mirror = {}
  277. debug.setmetatable(u3, {__index = mirror, __newindex = mirror})
  278. for i = 1, 10 do u3[i] = i end
  279. for i = 1, 10 do assert(u3[i] == i) end
  280. end
  281. t.__concat = function (a,b,c)
  282. assert(c == nil)
  283. if type(a) == 'table' then a = a.val end
  284. if type(b) == 'table' then b = b.val end
  285. if A then return a..b
  286. else
  287. return setmetatable({val=a..b}, t)
  288. end
  289. end
  290. c = {val="c"}; setmetatable(c, t)
  291. d = {val="d"}; setmetatable(d, t)
  292. A = true
  293. assert(c..d == 'cd')
  294. assert(0 .."a".."b"..c..d.."e".."f"..(5+3).."g" == "0abcdef8g")
  295. A = false
  296. assert((c..d..c..d).val == 'cdcd')
  297. x = c..d
  298. assert(getmetatable(x) == t and x.val == 'cd')
  299. x = 0 .."a".."b"..c..d.."e".."f".."g"
  300. assert(x.val == "0abcdefg")
  301. -- concat metamethod x numbers (bug in 5.1.1)
  302. c = {}
  303. local x
  304. setmetatable(c, {__concat = function (a,b)
  305. assert(type(a) == "number" and b == c or type(b) == "number" and a == c)
  306. return c
  307. end})
  308. assert(c..5 == c and 5 .. c == c)
  309. assert(4 .. c .. 5 == c and 4 .. 5 .. 6 .. 7 .. c == c)
  310. -- test comparison compatibilities
  311. local t1, t2, c, d
  312. t1 = {}; c = {}; setmetatable(c, t1)
  313. d = {}
  314. t1.__eq = function () return true end
  315. t1.__lt = function () return true end
  316. t1.__le = function () return false end
  317. setmetatable(d, t1)
  318. assert(c == d and c < d and not(d <= c))
  319. t2 = {}
  320. t2.__eq = t1.__eq
  321. t2.__lt = t1.__lt
  322. setmetatable(d, t2)
  323. assert(c == d and c < d and not(d <= c))
  324. -- test for several levels of calls
  325. local i
  326. local tt = {
  327. __call = function (t, ...)
  328. i = i+1
  329. if t.f then return t.f(...)
  330. else return {...}
  331. end
  332. end
  333. }
  334. local a = setmetatable({}, tt)
  335. local b = setmetatable({f=a}, tt)
  336. local c = setmetatable({f=b}, tt)
  337. i = 0
  338. x = c(3,4,5)
  339. assert(i == 3 and x[1] == 3 and x[3] == 5)
  340. assert(_G.X == 20)
  341. print'+'
  342. local _g = _G
  343. _ENV = setmetatable({}, {__index=function (_,k) return _g[k] end})
  344. a = {}
  345. rawset(a, "x", 1, 2, 3)
  346. assert(a.x == 1 and rawget(a, "x", 3) == 1)
  347. print '+'
  348. -- testing metatables for basic types
  349. mt = {__index = function (a,b) return a+b end,
  350. __len = function (x) return math.floor(x) end}
  351. debug.setmetatable(10, mt)
  352. assert(getmetatable(-2) == mt)
  353. assert((10)[3] == 13)
  354. assert((10)["3"] == 13)
  355. assert(#3.45 == 3)
  356. debug.setmetatable(23, nil)
  357. assert(getmetatable(-2) == nil)
  358. debug.setmetatable(true, mt)
  359. assert(getmetatable(false) == mt)
  360. mt.__index = function (a,b) return a or b end
  361. assert((true)[false] == true)
  362. assert((false)[false] == false)
  363. debug.setmetatable(false, nil)
  364. assert(getmetatable(true) == nil)
  365. debug.setmetatable(nil, mt)
  366. assert(getmetatable(nil) == mt)
  367. mt.__add = function (a,b) return (a or 1) + (b or 2) end
  368. assert(10 + nil == 12)
  369. assert(nil + 23 == 24)
  370. assert(nil + nil == 3)
  371. debug.setmetatable(nil, nil)
  372. assert(getmetatable(nil) == nil)
  373. debug.setmetatable(nil, {})
  374. -- loops in delegation
  375. a = {}; setmetatable(a, a); a.__index = a; a.__newindex = a
  376. assert(not pcall(function (a,b) return a[b] end, a, 10))
  377. assert(not pcall(function (a,b,c) a[b] = c end, a, 10, true))
  378. -- bug in 5.1
  379. T, K, V = nil
  380. grandparent = {}
  381. grandparent.__newindex = function(t,k,v) T=t; K=k; V=v end
  382. parent = {}
  383. parent.__newindex = parent
  384. setmetatable(parent, grandparent)
  385. child = setmetatable({}, parent)
  386. child.foo = 10 --> CRASH (on some machines)
  387. assert(T == parent and K == "foo" and V == 10)
  388. print 'OK'
  389. return 12