aboutsummaryrefslogtreecommitdiff
path: root/testes/events.lua
diff options
context:
space:
mode:
Diffstat (limited to 'testes/events.lua')
-rw-r--r--testes/events.lua476
1 files changed, 476 insertions, 0 deletions
diff --git a/testes/events.lua b/testes/events.lua
new file mode 100644
index 00000000..cf064d3d
--- /dev/null
+++ b/testes/events.lua
@@ -0,0 +1,476 @@
1-- $Id: events.lua,v 1.52 2018/03/12 13:51:02 roberto Exp $
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")
141
142-- Some tests are done inside small anonymous functions to ensure
143-- that constants go to constant table even in debug compilation,
144-- when the constant table is very small.
145assert(b+5 == b)
146assert(cap[0] == "add" and cap[1] == b and cap[2] == 5 and cap[3]==undef)
147assert(b+'5' == b)
148assert(cap[0] == "add" and cap[1] == b and cap[2] == '5' and cap[3]==undef)
149assert(5+b == 5)
150assert(cap[0] == "add" and cap[1] == 5 and cap[2] == b and cap[3]==undef)
151assert('5'+b == '5')
152assert(cap[0] == "add" and cap[1] == '5' and cap[2] == b and cap[3]==undef)
153b=b-3; assert(getmetatable(b) == t)
154assert(cap[0] == "sub" and cap[1] == b and cap[2] == 3 and cap[3]==undef)
155assert(5-a == 5)
156assert(cap[0] == "sub" and cap[1] == 5 and cap[2] == a and cap[3]==undef)
157assert('5'-a == '5')
158assert(cap[0] == "sub" and cap[1] == '5' and cap[2] == a and cap[3]==undef)
159assert(a*a == a)
160assert(cap[0] == "mul" and cap[1] == a and cap[2] == a and cap[3]==undef)
161assert(a/0 == a)
162assert(cap[0] == "div" and cap[1] == a and cap[2] == 0 and cap[3]==undef)
163assert(a%2 == a)
164assert(cap[0] == "mod" and cap[1] == a and cap[2] == 2 and cap[3]==undef)
165assert(a // (1/0) == a)
166assert(cap[0] == "idiv" and cap[1] == a and cap[2] == 1/0 and cap[3]==undef)
167;(function () assert(a & "hi" == a) end)()
168assert(cap[0] == "band" and cap[1] == a and cap[2] == "hi" and cap[3]==undef)
169;(function () assert(10 & a == 10) end)()
170assert(cap[0] == "band" and cap[1] == 10 and cap[2] == a and cap[3]==undef)
171;(function () assert(a | 10 == a) end)()
172assert(cap[0] == "bor" and cap[1] == a and cap[2] == 10 and cap[3]==undef)
173assert(a | "hi" == a)
174assert(cap[0] == "bor" and cap[1] == a and cap[2] == "hi" and cap[3]==undef)
175assert("hi" ~ a == "hi")
176assert(cap[0] == "bxor" and cap[1] == "hi" and cap[2] == a and cap[3]==undef)
177;(function () assert(10 ~ a == 10) end)()
178assert(cap[0] == "bxor" and cap[1] == 10 and cap[2] == a and cap[3]==undef)
179assert(-a == a)
180assert(cap[0] == "unm" and cap[1] == a)
181assert(a^4 == a)
182assert(cap[0] == "pow" and cap[1] == a and cap[2] == 4 and cap[3]==undef)
183assert(a^'4' == a)
184assert(cap[0] == "pow" and cap[1] == a and cap[2] == '4' and cap[3]==undef)
185assert(4^a == 4)
186assert(cap[0] == "pow" and cap[1] == 4 and cap[2] == a and cap[3]==undef)
187assert('4'^a == '4')
188assert(cap[0] == "pow" and cap[1] == '4' and cap[2] == a and cap[3]==undef)
189assert(#a == a)
190assert(cap[0] == "len" and cap[1] == a)
191assert(~a == a)
192assert(cap[0] == "bnot" and cap[1] == a)
193assert(a << 3 == a)
194assert(cap[0] == "shl" and cap[1] == a and cap[2] == 3)
195assert(1.5 >> a == 1.5)
196assert(cap[0] == "shr" and cap[1] == 1.5 and cap[2] == a)
197
198
199-- test for rawlen
200t = setmetatable({1,2,3}, {__len = function () return 10 end})
201assert(#t == 10 and rawlen(t) == 3)
202assert(rawlen"abc" == 3)
203assert(not pcall(rawlen, io.stdin))
204assert(not pcall(rawlen, 34))
205assert(not pcall(rawlen))
206
207-- rawlen for long strings
208assert(rawlen(string.rep('a', 1000)) == 1000)
209
210
211t = {}
212t.__lt = function (a,b,c)
213 collectgarbage()
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
220function Op(x) return setmetatable({x=x}, t) end
221
222local function test ()
223 assert(not(Op(1)<Op(1)) and (Op(1)<Op(2)) and not(Op(2)<Op(1)))
224 assert(not(1 < Op(1)) and (Op(1) < 2) and not(2 < Op(1)))
225 assert(not(Op('a')<Op('a')) and (Op('a')<Op('b')) and not(Op('b')<Op('a')))
226 assert(not('a' < Op('a')) and (Op('a') < 'b') and not(Op('b') < Op('a')))
227 assert((Op(1)<=Op(1)) and (Op(1)<=Op(2)) and not(Op(2)<=Op(1)))
228 assert((Op('a')<=Op('a')) and (Op('a')<=Op('b')) and not(Op('b')<=Op('a')))
229 assert(not(Op(1)>Op(1)) and not(Op(1)>Op(2)) and (Op(2)>Op(1)))
230 assert(not(Op('a')>Op('a')) and not(Op('a')>Op('b')) and (Op('b')>Op('a')))
231 assert((Op(1)>=Op(1)) and not(Op(1)>=Op(2)) and (Op(2)>=Op(1)))
232 assert((1 >= Op(1)) and not(1 >= Op(2)) and (Op(2) >= 1))
233 assert((Op('a')>=Op('a')) and not(Op('a')>=Op('b')) and (Op('b')>=Op('a')))
234 assert(('a' >= Op('a')) and not(Op('a') >= 'b') and (Op('b') >= Op('a')))
235end
236
237test()
238
239t.__le = function (a,b,c)
240 assert(c == nil)
241 if type(a) == 'table' then a = a.x end
242 if type(b) == 'table' then b = b.x end
243 return a<=b, "dummy"
244end
245
246test() -- retest comparisons, now using both `lt' and `le'
247
248
249-- test `partial order'
250
251local function rawSet(x)
252 local y = {}
253 for _,k in pairs(x) do y[k] = 1 end
254 return y
255end
256
257local function Set(x)
258 return setmetatable(rawSet(x), t)
259end
260
261t.__lt = function (a,b)
262 for k in pairs(a) do
263 if not b[k] then return false end
264 b[k] = undef
265 end
266 return next(b) ~= nil
267end
268
269t.__le = nil
270
271assert(Set{1,2,3} < Set{1,2,3,4})
272assert(not(Set{1,2,3,4} < Set{1,2,3,4}))
273assert((Set{1,2,3,4} <= Set{1,2,3,4}))
274assert((Set{1,2,3,4} >= Set{1,2,3,4}))
275assert((Set{1,3} <= Set{3,5})) -- wrong!! model needs a `le' method ;-)
276
277t.__le = function (a,b)
278 for k in pairs(a) do
279 if not b[k] then return false end
280 end
281 return true
282end
283
284assert(not (Set{1,3} <= Set{3,5})) -- now its OK!
285assert(not(Set{1,3} <= Set{3,5}))
286assert(not(Set{1,3} >= Set{3,5}))
287
288t.__eq = function (a,b)
289 for k in pairs(a) do
290 if not b[k] then return false end
291 b[k] = undef
292 end
293 return next(b) == nil
294end
295
296local s = Set{1,3,5}
297assert(s == Set{3,5,1})
298assert(not rawequal(s, Set{3,5,1}))
299assert(rawequal(s, s))
300assert(Set{1,3,5,1} == rawSet{3,5,1})
301assert(rawSet{1,3,5,1} == Set{3,5,1})
302assert(Set{1,3,5} ~= Set{3,5,1,6})
303
304-- '__eq' is not used for table accesses
305t[Set{1,3,5}] = 1
306assert(t[Set{1,3,5}] == undef)
307
308
309if not T then
310 (Message or print)('\n >>> testC not active: skipping tests for \z
311userdata <<<\n')
312else
313 local u1 = T.newuserdata(0, 1)
314 local u2 = T.newuserdata(0, 1)
315 local u3 = T.newuserdata(0, 1)
316 assert(u1 ~= u2 and u1 ~= u3)
317 debug.setuservalue(u1, 1);
318 debug.setuservalue(u2, 2);
319 debug.setuservalue(u3, 1);
320 debug.setmetatable(u1, {__eq = function (a, b)
321 return debug.getuservalue(a) == debug.getuservalue(b)
322 end})
323 debug.setmetatable(u2, {__eq = function (a, b)
324 return true
325 end})
326 assert(u1 == u3 and u3 == u1 and u1 ~= u2)
327 assert(u2 == u1 and u2 == u3 and u3 == u2)
328 assert(u2 ~= {}) -- different types cannot be equal
329
330 local mirror = {}
331 debug.setmetatable(u3, {__index = mirror, __newindex = mirror})
332 for i = 1, 10 do u3[i] = i end
333 for i = 1, 10 do assert(u3[i] == i) end
334end
335
336
337t.__concat = function (a,b,c)
338 assert(c == nil)
339 if type(a) == 'table' then a = a.val end
340 if type(b) == 'table' then b = b.val end
341 if A then return a..b
342 else
343 return setmetatable({val=a..b}, t)
344 end
345end
346
347c = {val="c"}; setmetatable(c, t)
348d = {val="d"}; setmetatable(d, t)
349
350A = true
351assert(c..d == 'cd')
352assert(0 .."a".."b"..c..d.."e".."f"..(5+3).."g" == "0abcdef8g")
353
354A = false
355assert((c..d..c..d).val == 'cdcd')
356x = c..d
357assert(getmetatable(x) == t and x.val == 'cd')
358x = 0 .."a".."b"..c..d.."e".."f".."g"
359assert(x.val == "0abcdefg")
360
361
362-- concat metamethod x numbers (bug in 5.1.1)
363c = {}
364local x
365setmetatable(c, {__concat = function (a,b)
366 assert(type(a) == "number" and b == c or type(b) == "number" and a == c)
367 return c
368end})
369assert(c..5 == c and 5 .. c == c)
370assert(4 .. c .. 5 == c and 4 .. 5 .. 6 .. 7 .. c == c)
371
372
373-- test comparison compatibilities
374local t1, t2, c, d
375t1 = {}; c = {}; setmetatable(c, t1)
376d = {}
377t1.__eq = function () return true end
378t1.__lt = function () return true end
379setmetatable(d, t1)
380assert(c == d and c < d and not(d <= c))
381t2 = {}
382t2.__eq = t1.__eq
383t2.__lt = t1.__lt
384setmetatable(d, t2)
385assert(c == d and c < d and not(d <= c))
386
387
388
389-- test for several levels of calls
390local i
391local tt = {
392 __call = function (t, ...)
393 i = i+1
394 if t.f then return t.f(...)
395 else return {...}
396 end
397 end
398}
399
400local a = setmetatable({}, tt)
401local b = setmetatable({f=a}, tt)
402local c = setmetatable({f=b}, tt)
403
404i = 0
405x = c(3,4,5)
406assert(i == 3 and x[1] == 3 and x[3] == 5)
407
408
409assert(_G.X == 20)
410
411print'+'
412
413local _g = _G
414_ENV = setmetatable({}, {__index=function (_,k) return _g[k] end})
415
416
417a = {}
418rawset(a, "x", 1, 2, 3)
419assert(a.x == 1 and rawget(a, "x", 3) == 1)
420
421print '+'
422
423-- testing metatables for basic types
424mt = {__index = function (a,b) return a+b end,
425 __len = function (x) return math.floor(x) end}
426debug.setmetatable(10, mt)
427assert(getmetatable(-2) == mt)
428assert((10)[3] == 13)
429assert((10)["3"] == 13)
430assert(#3.45 == 3)
431debug.setmetatable(23, nil)
432assert(getmetatable(-2) == nil)
433
434debug.setmetatable(true, mt)
435assert(getmetatable(false) == mt)
436mt.__index = function (a,b) return a or b end
437assert((true)[false] == true)
438assert((false)[false] == false)
439debug.setmetatable(false, nil)
440assert(getmetatable(true) == nil)
441
442debug.setmetatable(nil, mt)
443assert(getmetatable(nil) == mt)
444mt.__add = function (a,b) return (a or 1) + (b or 2) end
445assert(10 + nil == 12)
446assert(nil + 23 == 24)
447assert(nil + nil == 3)
448debug.setmetatable(nil, nil)
449assert(getmetatable(nil) == nil)
450
451debug.setmetatable(nil, {})
452
453
454-- loops in delegation
455a = {}; setmetatable(a, a); a.__index = a; a.__newindex = a
456assert(not pcall(function (a,b) return a[b] end, a, 10))
457assert(not pcall(function (a,b,c) a[b] = c end, a, 10, true))
458
459-- bug in 5.1
460T, K, V = nil
461grandparent = {}
462grandparent.__newindex = function(t,k,v) T=t; K=k; V=v end
463
464parent = {}
465parent.__newindex = parent
466setmetatable(parent, grandparent)
467
468child = setmetatable({}, parent)
469child.foo = 10 --> CRASH (on some machines)
470assert(T == parent and K == "foo" and V == 10)
471
472print 'OK'
473
474return 12
475
476