summaryrefslogtreecommitdiff
path: root/testes/events.lua
diff options
context:
space:
mode:
Diffstat (limited to 'testes/events.lua')
-rw-r--r--testes/events.lua456
1 files changed, 456 insertions, 0 deletions
diff --git a/testes/events.lua b/testes/events.lua
new file mode 100644
index 00000000..9136f99c
--- /dev/null
+++ b/testes/events.lua
@@ -0,0 +1,456 @@
1-- $Id: events.lua,v 1.45 2016/12/21 19:23: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)
18B = nil
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
73a[1] = 10; a[2] = 20; a[3] = 90
74assert(c[1] == 10 and c[2] == 20 and c[3] == 90)
75
76
77do
78 local a;
79 a = setmetatable({}, {__index = setmetatable({},
80 {__index = setmetatable({},
81 {__index = function (_,n) return a[n-3]+4, "lixo" end})})})
82 a[0] = 20
83 for i=0,10 do
84 assert(a[i*3] == 20 + i*4)
85 end
86end
87
88
89do -- newindex
90 local foi
91 local a = {}
92 for i=1,10 do a[i] = 0; a['a'..i] = 0; end
93 setmetatable(a, {__newindex = function (t,k,v) foi=true; rawset(t,k,v) end})
94 foi = false; a[1]=0; assert(not foi)
95 foi = false; a['a1']=0; assert(not foi)
96 foi = false; a['a11']=0; assert(foi)
97 foi = false; a[11]=0; assert(foi)
98 foi = false; a[1]=nil; assert(not foi)
99 foi = false; a[1]=nil; assert(foi)
100end
101
102
103setmetatable(t, nil)
104function f (t, ...) return t, {...} end
105t.__call = f
106
107do
108 local x,y = a(table.unpack{'a', 1})
109 assert(x==a and y[1]=='a' and y[2]==1 and y[3]==nil)
110 x,y = a()
111 assert(x==a and y[1]==nil)
112end
113
114
115local b = setmetatable({}, t)
116setmetatable(b,t)
117
118function f(op)
119 return function (...) cap = {[0] = op, ...} ; return (...) end
120end
121t.__add = f("add")
122t.__sub = f("sub")
123t.__mul = f("mul")
124t.__div = f("div")
125t.__idiv = f("idiv")
126t.__mod = f("mod")
127t.__unm = f("unm")
128t.__pow = f("pow")
129t.__len = f("len")
130t.__band = f("band")
131t.__bor = f("bor")
132t.__bxor = f("bxor")
133t.__shl = f("shl")
134t.__shr = f("shr")
135t.__bnot = f("bnot")
136
137assert(b+5 == b)
138assert(cap[0] == "add" and cap[1] == b and cap[2] == 5 and cap[3]==nil)
139assert(b+'5' == b)
140assert(cap[0] == "add" and cap[1] == b and cap[2] == '5' and cap[3]==nil)
141assert(5+b == 5)
142assert(cap[0] == "add" and cap[1] == 5 and cap[2] == b and cap[3]==nil)
143assert('5'+b == '5')
144assert(cap[0] == "add" and cap[1] == '5' and cap[2] == b and cap[3]==nil)
145b=b-3; assert(getmetatable(b) == t)
146assert(5-a == 5)
147assert(cap[0] == "sub" and cap[1] == 5 and cap[2] == a and cap[3]==nil)
148assert('5'-a == '5')
149assert(cap[0] == "sub" and cap[1] == '5' and cap[2] == a and cap[3]==nil)
150assert(a*a == a)
151assert(cap[0] == "mul" and cap[1] == a and cap[2] == a and cap[3]==nil)
152assert(a/0 == a)
153assert(cap[0] == "div" and cap[1] == a and cap[2] == 0 and cap[3]==nil)
154assert(a%2 == a)
155assert(cap[0] == "mod" and cap[1] == a and cap[2] == 2 and cap[3]==nil)
156assert(a // (1/0) == a)
157assert(cap[0] == "idiv" and cap[1] == a and cap[2] == 1/0 and cap[3]==nil)
158assert(a & "hi" == a)
159assert(cap[0] == "band" and cap[1] == a and cap[2] == "hi" and cap[3]==nil)
160assert(a | "hi" == a)
161assert(cap[0] == "bor" and cap[1] == a and cap[2] == "hi" and cap[3]==nil)
162assert("hi" ~ a == "hi")
163assert(cap[0] == "bxor" and cap[1] == "hi" and cap[2] == a and cap[3]==nil)
164assert(-a == a)
165assert(cap[0] == "unm" and cap[1] == a)
166assert(a^4 == a)
167assert(cap[0] == "pow" and cap[1] == a and cap[2] == 4 and cap[3]==nil)
168assert(a^'4' == a)
169assert(cap[0] == "pow" and cap[1] == a and cap[2] == '4' and cap[3]==nil)
170assert(4^a == 4)
171assert(cap[0] == "pow" and cap[1] == 4 and cap[2] == a and cap[3]==nil)
172assert('4'^a == '4')
173assert(cap[0] == "pow" and cap[1] == '4' and cap[2] == a and cap[3]==nil)
174assert(#a == a)
175assert(cap[0] == "len" and cap[1] == a)
176assert(~a == a)
177assert(cap[0] == "bnot" and cap[1] == a)
178assert(a << 3 == a)
179assert(cap[0] == "shl" and cap[1] == a and cap[2] == 3)
180assert(1.5 >> a == 1.5)
181assert(cap[0] == "shr" and cap[1] == 1.5 and cap[2] == a)
182
183
184-- test for rawlen
185t = setmetatable({1,2,3}, {__len = function () return 10 end})
186assert(#t == 10 and rawlen(t) == 3)
187assert(rawlen"abc" == 3)
188assert(not pcall(rawlen, io.stdin))
189assert(not pcall(rawlen, 34))
190assert(not pcall(rawlen))
191
192-- rawlen for long strings
193assert(rawlen(string.rep('a', 1000)) == 1000)
194
195
196t = {}
197t.__lt = function (a,b,c)
198 collectgarbage()
199 assert(c == nil)
200 if type(a) == 'table' then a = a.x end
201 if type(b) == 'table' then b = b.x end
202 return a<b, "dummy"
203end
204
205function Op(x) return setmetatable({x=x}, t) end
206
207local function test ()
208 assert(not(Op(1)<Op(1)) and (Op(1)<Op(2)) and not(Op(2)<Op(1)))
209 assert(not(1 < Op(1)) and (Op(1) < 2) and not(2 < Op(1)))
210 assert(not(Op('a')<Op('a')) and (Op('a')<Op('b')) and not(Op('b')<Op('a')))
211 assert(not('a' < Op('a')) and (Op('a') < 'b') and not(Op('b') < Op('a')))
212 assert((Op(1)<=Op(1)) and (Op(1)<=Op(2)) and not(Op(2)<=Op(1)))
213 assert((Op('a')<=Op('a')) and (Op('a')<=Op('b')) and not(Op('b')<=Op('a')))
214 assert(not(Op(1)>Op(1)) and not(Op(1)>Op(2)) and (Op(2)>Op(1)))
215 assert(not(Op('a')>Op('a')) and not(Op('a')>Op('b')) and (Op('b')>Op('a')))
216 assert((Op(1)>=Op(1)) and not(Op(1)>=Op(2)) and (Op(2)>=Op(1)))
217 assert((1 >= Op(1)) and not(1 >= Op(2)) and (Op(2) >= 1))
218 assert((Op('a')>=Op('a')) and not(Op('a')>=Op('b')) and (Op('b')>=Op('a')))
219 assert(('a' >= Op('a')) and not(Op('a') >= 'b') and (Op('b') >= Op('a')))
220end
221
222test()
223
224t.__le = function (a,b,c)
225 assert(c == nil)
226 if type(a) == 'table' then a = a.x end
227 if type(b) == 'table' then b = b.x end
228 return a<=b, "dummy"
229end
230
231test() -- retest comparisons, now using both `lt' and `le'
232
233
234-- test `partial order'
235
236local function rawSet(x)
237 local y = {}
238 for _,k in pairs(x) do y[k] = 1 end
239 return y
240end
241
242local function Set(x)
243 return setmetatable(rawSet(x), t)
244end
245
246t.__lt = function (a,b)
247 for k in pairs(a) do
248 if not b[k] then return false end
249 b[k] = nil
250 end
251 return next(b) ~= nil
252end
253
254t.__le = nil
255
256assert(Set{1,2,3} < Set{1,2,3,4})
257assert(not(Set{1,2,3,4} < Set{1,2,3,4}))
258assert((Set{1,2,3,4} <= Set{1,2,3,4}))
259assert((Set{1,2,3,4} >= Set{1,2,3,4}))
260assert((Set{1,3} <= Set{3,5})) -- wrong!! model needs a `le' method ;-)
261
262t.__le = function (a,b)
263 for k in pairs(a) do
264 if not b[k] then return false end
265 end
266 return true
267end
268
269assert(not (Set{1,3} <= Set{3,5})) -- now its OK!
270assert(not(Set{1,3} <= Set{3,5}))
271assert(not(Set{1,3} >= Set{3,5}))
272
273t.__eq = function (a,b)
274 for k in pairs(a) do
275 if not b[k] then return false end
276 b[k] = nil
277 end
278 return next(b) == nil
279end
280
281local s = Set{1,3,5}
282assert(s == Set{3,5,1})
283assert(not rawequal(s, Set{3,5,1}))
284assert(rawequal(s, s))
285assert(Set{1,3,5,1} == rawSet{3,5,1})
286assert(rawSet{1,3,5,1} == Set{3,5,1})
287assert(Set{1,3,5} ~= Set{3,5,1,6})
288
289-- '__eq' is not used for table accesses
290t[Set{1,3,5}] = 1
291assert(t[Set{1,3,5}] == nil)
292
293
294if not T then
295 (Message or print)('\n >>> testC not active: skipping tests for \z
296userdata equality <<<\n')
297else
298 local u1 = T.newuserdata(0)
299 local u2 = T.newuserdata(0)
300 local u3 = T.newuserdata(0)
301 assert(u1 ~= u2 and u1 ~= u3)
302 debug.setuservalue(u1, 1);
303 debug.setuservalue(u2, 2);
304 debug.setuservalue(u3, 1);
305 debug.setmetatable(u1, {__eq = function (a, b)
306 return debug.getuservalue(a) == debug.getuservalue(b)
307 end})
308 debug.setmetatable(u2, {__eq = function (a, b)
309 return true
310 end})
311 assert(u1 == u3 and u3 == u1 and u1 ~= u2)
312 assert(u2 == u1 and u2 == u3 and u3 == u2)
313 assert(u2 ~= {}) -- different types cannot be equal
314end
315
316
317t.__concat = function (a,b,c)
318 assert(c == nil)
319 if type(a) == 'table' then a = a.val end
320 if type(b) == 'table' then b = b.val end
321 if A then return a..b
322 else
323 return setmetatable({val=a..b}, t)
324 end
325end
326
327c = {val="c"}; setmetatable(c, t)
328d = {val="d"}; setmetatable(d, t)
329
330A = true
331assert(c..d == 'cd')
332assert(0 .."a".."b"..c..d.."e".."f"..(5+3).."g" == "0abcdef8g")
333
334A = false
335assert((c..d..c..d).val == 'cdcd')
336x = c..d
337assert(getmetatable(x) == t and x.val == 'cd')
338x = 0 .."a".."b"..c..d.."e".."f".."g"
339assert(x.val == "0abcdefg")
340
341
342-- concat metamethod x numbers (bug in 5.1.1)
343c = {}
344local x
345setmetatable(c, {__concat = function (a,b)
346 assert(type(a) == "number" and b == c or type(b) == "number" and a == c)
347 return c
348end})
349assert(c..5 == c and 5 .. c == c)
350assert(4 .. c .. 5 == c and 4 .. 5 .. 6 .. 7 .. c == c)
351
352
353-- test comparison compatibilities
354local t1, t2, c, d
355t1 = {}; c = {}; setmetatable(c, t1)
356d = {}
357t1.__eq = function () return true end
358t1.__lt = function () return true end
359setmetatable(d, t1)
360assert(c == d and c < d and not(d <= c))
361t2 = {}
362t2.__eq = t1.__eq
363t2.__lt = t1.__lt
364setmetatable(d, t2)
365assert(c == d and c < d and not(d <= c))
366
367
368
369-- test for several levels of calls
370local i
371local tt = {
372 __call = function (t, ...)
373 i = i+1
374 if t.f then return t.f(...)
375 else return {...}
376 end
377 end
378}
379
380local a = setmetatable({}, tt)
381local b = setmetatable({f=a}, tt)
382local c = setmetatable({f=b}, tt)
383
384i = 0
385x = c(3,4,5)
386assert(i == 3 and x[1] == 3 and x[3] == 5)
387
388
389assert(_G.X == 20)
390
391print'+'
392
393local _g = _G
394_ENV = setmetatable({}, {__index=function (_,k) return _g[k] end})
395
396
397a = {}
398rawset(a, "x", 1, 2, 3)
399assert(a.x == 1 and rawget(a, "x", 3) == 1)
400
401print '+'
402
403-- testing metatables for basic types
404mt = {__index = function (a,b) return a+b end,
405 __len = function (x) return math.floor(x) end}
406debug.setmetatable(10, mt)
407assert(getmetatable(-2) == mt)
408assert((10)[3] == 13)
409assert((10)["3"] == 13)
410assert(#3.45 == 3)
411debug.setmetatable(23, nil)
412assert(getmetatable(-2) == nil)
413
414debug.setmetatable(true, mt)
415assert(getmetatable(false) == mt)
416mt.__index = function (a,b) return a or b end
417assert((true)[false] == true)
418assert((false)[false] == false)
419debug.setmetatable(false, nil)
420assert(getmetatable(true) == nil)
421
422debug.setmetatable(nil, mt)
423assert(getmetatable(nil) == mt)
424mt.__add = function (a,b) return (a or 0) + (b or 0) end
425assert(10 + nil == 10)
426assert(nil + 23 == 23)
427assert(nil + nil == 0)
428debug.setmetatable(nil, nil)
429assert(getmetatable(nil) == nil)
430
431debug.setmetatable(nil, {})
432
433
434-- loops in delegation
435a = {}; setmetatable(a, a); a.__index = a; a.__newindex = a
436assert(not pcall(function (a,b) return a[b] end, a, 10))
437assert(not pcall(function (a,b,c) a[b] = c end, a, 10, true))
438
439-- bug in 5.1
440T, K, V = nil
441grandparent = {}
442grandparent.__newindex = function(t,k,v) T=t; K=k; V=v end
443
444parent = {}
445parent.__newindex = parent
446setmetatable(parent, grandparent)
447
448child = setmetatable({}, parent)
449child.foo = 10 --> CRASH (on some machines)
450assert(T == parent and K == "foo" and V == 10)
451
452print 'OK'
453
454return 12
455
456