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