events.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. t.__lt = f("lt")
  115. t.__le = f("le")
  116. local function checkcap (t)
  117. assert(#cap + 1 == #t)
  118. for i = 1, #t do
  119. assert(cap[i - 1] == t[i])
  120. assert(math.type(cap[i - 1]) == math.type(t[i]))
  121. end
  122. end
  123. -- Some tests are done inside small anonymous functions to ensure
  124. -- that constants go to constant table even in debug compilation,
  125. -- when the constant table is very small.
  126. assert(b+5 == b); checkcap{"add", b, 5}
  127. assert(5.2 + b == 5.2); checkcap{"add", 5.2, b}
  128. assert(b+'5' == b); checkcap{"add", b, '5'}
  129. assert(5+b == 5); checkcap{"add", 5, b}
  130. assert('5'+b == '5'); checkcap{"add", '5', b}
  131. b=b-3; assert(getmetatable(b) == t); checkcap{"sub", b, 3}
  132. assert(5-a == 5); checkcap{"sub", 5, a}
  133. assert('5'-a == '5'); checkcap{"sub", '5', a}
  134. assert(a*a == a); checkcap{"mul", a, a}
  135. assert(a/0 == a); checkcap{"div", a, 0}
  136. assert(a/0.0 == a); checkcap{"div", a, 0.0}
  137. assert(a%2 == a); checkcap{"mod", a, 2}
  138. assert(a // (1/0) == a); checkcap{"idiv", a, 1/0}
  139. ;(function () assert(a & "hi" == a) end)(); checkcap{"band", a, "hi"}
  140. ;(function () assert(10 & a == 10) end)(); checkcap{"band", 10, a}
  141. ;(function () assert(a | 10 == a) end)(); checkcap{"bor", a, 10}
  142. assert(a | "hi" == a); checkcap{"bor", a, "hi"}
  143. assert("hi" ~ a == "hi"); checkcap{"bxor", "hi", a}
  144. ;(function () assert(10 ~ a == 10) end)(); checkcap{"bxor", 10, a}
  145. assert(-a == a); checkcap{"unm", a, a}
  146. assert(a^4.0 == a); checkcap{"pow", a, 4.0}
  147. assert(a^'4' == a); checkcap{"pow", a, '4'}
  148. assert(4^a == 4); checkcap{"pow", 4, a}
  149. assert('4'^a == '4'); checkcap{"pow", '4', a}
  150. assert(#a == a); checkcap{"len", a, a}
  151. assert(~a == a); checkcap{"bnot", a, a}
  152. assert(a << 3 == a); checkcap{"shl", a, 3}
  153. assert(1.5 >> a == 1.5); checkcap{"shr", 1.5, a}
  154. -- for comparison operators, all results are true
  155. assert(5.0 > a); checkcap{"lt", a, 5.0}
  156. assert(a >= 10); checkcap{"le", 10, a}
  157. assert(a <= -10.0); checkcap{"le", a, -10.0}
  158. assert(a < -10); checkcap{"lt", a, -10}
  159. -- test for rawlen
  160. t = setmetatable({1,2,3}, {__len = function () return 10 end})
  161. assert(#t == 10 and rawlen(t) == 3)
  162. assert(rawlen"abc" == 3)
  163. assert(not pcall(rawlen, io.stdin))
  164. assert(not pcall(rawlen, 34))
  165. assert(not pcall(rawlen))
  166. -- rawlen for long strings
  167. assert(rawlen(string.rep('a', 1000)) == 1000)
  168. t = {}
  169. t.__lt = function (a,b,c)
  170. collectgarbage()
  171. assert(c == nil)
  172. if type(a) == 'table' then a = a.x end
  173. if type(b) == 'table' then b = b.x end
  174. return a<b, "dummy"
  175. end
  176. t.__le = function (a,b,c)
  177. assert(c == nil)
  178. if type(a) == 'table' then a = a.x end
  179. if type(b) == 'table' then b = b.x end
  180. return a<=b, "dummy"
  181. end
  182. t.__eq = function (a,b,c)
  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. function Op(x) return setmetatable({x=x}, t) end
  189. local function test (a, b, c)
  190. assert(not(Op(1)<Op(1)) and (Op(1)<Op(2)) and not(Op(2)<Op(1)))
  191. assert(not(1 < Op(1)) and (Op(1) < 2) and not(2 < Op(1)))
  192. assert(not(Op('a')<Op('a')) and (Op('a')<Op('b')) and not(Op('b')<Op('a')))
  193. assert(not('a' < Op('a')) and (Op('a') < 'b') and not(Op('b') < Op('a')))
  194. assert((Op(1)<=Op(1)) and (Op(1)<=Op(2)) and not(Op(2)<=Op(1)))
  195. assert((Op('a')<=Op('a')) and (Op('a')<=Op('b')) and not(Op('b')<=Op('a')))
  196. assert(not(Op(1)>Op(1)) and not(Op(1)>Op(2)) and (Op(2)>Op(1)))
  197. assert(not(Op('a')>Op('a')) and not(Op('a')>Op('b')) and (Op('b')>Op('a')))
  198. assert((Op(1)>=Op(1)) and not(Op(1)>=Op(2)) and (Op(2)>=Op(1)))
  199. assert((1 >= Op(1)) and not(1 >= Op(2)) and (Op(2) >= 1))
  200. assert((Op('a')>=Op('a')) and not(Op('a')>=Op('b')) and (Op('b')>=Op('a')))
  201. assert(('a' >= Op('a')) and not(Op('a') >= 'b') and (Op('b') >= Op('a')))
  202. assert(Op(1) == Op(1) and Op(1) ~= Op(2))
  203. assert(Op('a') == Op('a') and Op('a') ~= Op('b'))
  204. assert(a == a and a ~= b)
  205. assert(Op(3) == c)
  206. end
  207. test(Op(1), Op(2), Op(3))
  208. -- test `partial order'
  209. local function rawSet(x)
  210. local y = {}
  211. for _,k in pairs(x) do y[k] = 1 end
  212. return y
  213. end
  214. local function Set(x)
  215. return setmetatable(rawSet(x), t)
  216. end
  217. t.__lt = function (a,b)
  218. for k in pairs(a) do
  219. if not b[k] then return false end
  220. b[k] = undef
  221. end
  222. return next(b) ~= nil
  223. end
  224. t.__le = function (a,b)
  225. for k in pairs(a) do
  226. if not b[k] then return false end
  227. end
  228. return true
  229. end
  230. assert(Set{1,2,3} < Set{1,2,3,4})
  231. assert(not(Set{1,2,3,4} < Set{1,2,3,4}))
  232. assert((Set{1,2,3,4} <= Set{1,2,3,4}))
  233. assert((Set{1,2,3,4} >= Set{1,2,3,4}))
  234. assert(not (Set{1,3} <= Set{3,5}))
  235. assert(not(Set{1,3} <= Set{3,5}))
  236. assert(not(Set{1,3} >= Set{3,5}))
  237. t.__eq = function (a,b)
  238. for k in pairs(a) do
  239. if not b[k] then return false end
  240. b[k] = undef
  241. end
  242. return next(b) == nil
  243. end
  244. local s = Set{1,3,5}
  245. assert(s == Set{3,5,1})
  246. assert(not rawequal(s, Set{3,5,1}))
  247. assert(rawequal(s, s))
  248. assert(Set{1,3,5,1} == rawSet{3,5,1})
  249. assert(rawSet{1,3,5,1} == Set{3,5,1})
  250. assert(Set{1,3,5} ~= Set{3,5,1,6})
  251. -- '__eq' is not used for table accesses
  252. t[Set{1,3,5}] = 1
  253. assert(t[Set{1,3,5}] == undef)
  254. do -- test invalidating flags
  255. local mt = {__eq = true}
  256. local a = setmetatable({10}, mt)
  257. local b = setmetatable({10}, mt)
  258. mt.__eq = nil
  259. assert(a ~= b) -- no metamethod
  260. mt.__eq = function (x,y) return x[1] == y[1] end
  261. assert(a == b) -- must use metamethod now
  262. end
  263. if not T then
  264. (Message or print)('\n >>> testC not active: skipping tests for \z
  265. userdata <<<\n')
  266. else
  267. local u1 = T.newuserdata(0, 1)
  268. local u2 = T.newuserdata(0, 1)
  269. local u3 = T.newuserdata(0, 1)
  270. assert(u1 ~= u2 and u1 ~= u3)
  271. debug.setuservalue(u1, 1);
  272. debug.setuservalue(u2, 2);
  273. debug.setuservalue(u3, 1);
  274. debug.setmetatable(u1, {__eq = function (a, b)
  275. return debug.getuservalue(a) == debug.getuservalue(b)
  276. end})
  277. debug.setmetatable(u2, {__eq = function (a, b)
  278. return true
  279. end})
  280. assert(u1 == u3 and u3 == u1 and u1 ~= u2)
  281. assert(u2 == u1 and u2 == u3 and u3 == u2)
  282. assert(u2 ~= {}) -- different types cannot be equal
  283. assert(rawequal(u1, u1) and not rawequal(u1, u3))
  284. local mirror = {}
  285. debug.setmetatable(u3, {__index = mirror, __newindex = mirror})
  286. for i = 1, 10 do u3[i] = i end
  287. for i = 1, 10 do assert(u3[i] == i) end
  288. end
  289. t.__concat = function (a,b,c)
  290. assert(c == nil)
  291. if type(a) == 'table' then a = a.val end
  292. if type(b) == 'table' then b = b.val end
  293. if A then return a..b
  294. else
  295. return setmetatable({val=a..b}, t)
  296. end
  297. end
  298. c = {val="c"}; setmetatable(c, t)
  299. d = {val="d"}; setmetatable(d, t)
  300. A = true
  301. assert(c..d == 'cd')
  302. assert(0 .."a".."b"..c..d.."e".."f"..(5+3).."g" == "0abcdef8g")
  303. A = false
  304. assert((c..d..c..d).val == 'cdcd')
  305. x = c..d
  306. assert(getmetatable(x) == t and x.val == 'cd')
  307. x = 0 .."a".."b"..c..d.."e".."f".."g"
  308. assert(x.val == "0abcdefg")
  309. -- concat metamethod x numbers (bug in 5.1.1)
  310. c = {}
  311. local x
  312. setmetatable(c, {__concat = function (a,b)
  313. assert(type(a) == "number" and b == c or type(b) == "number" and a == c)
  314. return c
  315. end})
  316. assert(c..5 == c and 5 .. c == c)
  317. assert(4 .. c .. 5 == c and 4 .. 5 .. 6 .. 7 .. c == c)
  318. -- test comparison compatibilities
  319. local t1, t2, c, d
  320. t1 = {}; c = {}; setmetatable(c, t1)
  321. d = {}
  322. t1.__eq = function () return true end
  323. t1.__lt = function () return true end
  324. t1.__le = function () return false end
  325. setmetatable(d, t1)
  326. assert(c == d and c < d and not(d <= c))
  327. t2 = {}
  328. t2.__eq = t1.__eq
  329. t2.__lt = t1.__lt
  330. setmetatable(d, t2)
  331. assert(c == d and c < d and not(d <= c))
  332. -- test for several levels of calls
  333. local i
  334. local tt = {
  335. __call = function (t, ...)
  336. i = i+1
  337. if t.f then return t.f(...)
  338. else return {...}
  339. end
  340. end
  341. }
  342. local a = setmetatable({}, tt)
  343. local b = setmetatable({f=a}, tt)
  344. local c = setmetatable({f=b}, tt)
  345. i = 0
  346. x = c(3,4,5)
  347. assert(i == 3 and x[1] == 3 and x[3] == 5)
  348. assert(_G.X == 20)
  349. print'+'
  350. local _g = _G
  351. _ENV = setmetatable({}, {__index=function (_,k) return _g[k] end})
  352. a = {}
  353. rawset(a, "x", 1, 2, 3)
  354. assert(a.x == 1 and rawget(a, "x", 3) == 1)
  355. print '+'
  356. -- testing metatables for basic types
  357. mt = {__index = function (a,b) return a+b end,
  358. __len = function (x) return math.floor(x) end}
  359. debug.setmetatable(10, mt)
  360. assert(getmetatable(-2) == mt)
  361. assert((10)[3] == 13)
  362. assert((10)["3"] == 13)
  363. assert(#3.45 == 3)
  364. debug.setmetatable(23, nil)
  365. assert(getmetatable(-2) == nil)
  366. debug.setmetatable(true, mt)
  367. assert(getmetatable(false) == mt)
  368. mt.__index = function (a,b) return a or b end
  369. assert((true)[false] == true)
  370. assert((false)[false] == false)
  371. debug.setmetatable(false, nil)
  372. assert(getmetatable(true) == nil)
  373. debug.setmetatable(nil, mt)
  374. assert(getmetatable(nil) == mt)
  375. mt.__add = function (a,b) return (a or 1) + (b or 2) end
  376. assert(10 + nil == 12)
  377. assert(nil + 23 == 24)
  378. assert(nil + nil == 3)
  379. debug.setmetatable(nil, nil)
  380. assert(getmetatable(nil) == nil)
  381. debug.setmetatable(nil, {})
  382. -- loops in delegation
  383. a = {}; setmetatable(a, a); a.__index = a; a.__newindex = a
  384. assert(not pcall(function (a,b) return a[b] end, a, 10))
  385. assert(not pcall(function (a,b,c) a[b] = c end, a, 10, true))
  386. -- bug in 5.1
  387. T, K, V = nil
  388. grandparent = {}
  389. grandparent.__newindex = function(t,k,v) T=t; K=k; V=v end
  390. parent = {}
  391. parent.__newindex = parent
  392. setmetatable(parent, grandparent)
  393. child = setmetatable({}, parent)
  394. child.foo = 10 --> CRASH (on some machines)
  395. assert(T == parent and K == "foo" and V == 10)
  396. print 'OK'
  397. return 12