events.lua 11 KB

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