interplane.Get Omega
Read in your language

lua

[Ported from github.com/lua/lua] Cast in 'rl_readline_name'

Brought by drakestapleton

branch master Branches Tags History

lua / testes / closure.lua

279 lines · 5784 bytes Raw Blame History
1-- $Id: testes/closure.lua $
2-- See Copyright Notice in file lua.h
3
4global <const> *
5
6print "testing closures"
7
8do -- bug in 5.4.7
9 _ENV[true] = 10
10 local function aux () return _ENV[1 < 2] end
11 assert(aux() == 10)
12 _ENV[true] = nil
13end
14
15
16local A,B = 0,{g=10}
17local function f(x)
18 local a = {}
19 for i=1,1000 do
20 local y = 0
21 do
22 a[i] = function () B.g = B.g+1; y = y+x; return y+A end
23 end
24 end
25 local dummy = function () return a[A] end
26 collectgarbage()
27 A = 1; assert(dummy() == a[1]); A = 0;
28 assert(a[1]() == x)
29 assert(a[3]() == x)
30 collectgarbage()
31 assert(B.g == 12)
32 return a
33end
34
35local a = f(10)
36-- force a GC in this level
37local x = {[1] = {}} -- to detect a GC
38setmetatable(x, {__mode = 'kv'})
39while x[1] do -- repeat until GC
40 local a = A..A..A..A -- create garbage
41 A = A+1
42end
43assert(a[1]() == 20+A)
44assert(a[1]() == 30+A)
45assert(a[2]() == 10+A)
46collectgarbage()
47assert(a[2]() == 20+A)
48assert(a[2]() == 30+A)
49assert(a[3]() == 20+A)
50assert(a[8]() == 10+A)
51assert(getmetatable(x).__mode == 'kv')
52assert(B.g == 19)
53
54
55-- testing equality
56a = {}
57
58for i = 1, 5 do a[i] = function (x) return i + a + _ENV end end
59assert(a[3] ~= a[4] and a[4] ~= a[5])
60
61do
62 local a = function (x) return math.sin(_ENV[x]) end
63 local function f()
64 return a
65 end
66 assert(f() == f())
67end
68
69
70-- testing closures with 'for' control variable
71a = {}
72for i=1,10 do
73 a[i] = function () return i end
74 if i == 3 then break end
75end
76assert(a[4] == undef)
77assert(a[2]() == 2)
78assert(a[3]() == 3)
79
80a = {}
81local t = {"a", "b"}
82for i = 1, #t do
83 local k = t[i]
84 a[i] = {set = function(x) k=x end,
85 get = function () return i, k end}
86 if i == 2 then break end
87end
88a[1].set(10)
89local r,s = a[2].get()
90assert(r == 2 and s == 'b')
91r,s = a[1].get()
92assert(r == 1 and s == 10)
93a[2].set('a')
94r,s = a[2].get()
95assert(r == 2 and s == "a")
96
97
98-- testing closures with 'for' control variable x break
99local f
100for i=1,3 do
101 f = function () return i end
102 break
103end
104assert(f() == 1)
105
106for k = 1, #t do
107 local v = t[k]
108 f = function () return k, v end
109 break
110end
111assert(({f()})[1] == 1)
112assert(({f()})[2] == "a")
113
114
115-- testing closure x break x return x errors
116
117local b
118function f(x)
119 local first = 1
120 while 1 do
121 if x == 3 and not first then return end
122 local a = 'xuxu'
123 b = function (op, y)
124 if op == 'set' then
125 a = x+y
126 else
127 return a
128 end
129 end
130 if x == 1 then do break end
131 elseif x == 2 then return
132 else if x ~= 3 then error() end
133 end
134 first = nil
135 end
136end
137
138for i=1,3 do
139 f(i)
140 assert(b('get') == 'xuxu')
141 b('set', 10); assert(b('get') == 10+i)
142 b = nil
143end
144
145pcall(f, 4);
146assert(b('get') == 'xuxu')
147b('set', 10); assert(b('get') == 14)
148
149
150local y, w
151-- testing multi-level closure
152function f(x)
153 return function (y)
154 return function (z) return w+x+y+z end
155 end
156end
157
158y = f(10)
159w = 1.345
160assert(y(20)(30) == 60+w)
161
162
163-- testing closures x break
164do
165 local X, Y
166 local a = math.sin(0)
167
168 while a do
169 local b = 10
170 X = function () return b end -- closure with upvalue
171 if a then break end
172 end
173
174 do
175 local b = 20
176 Y = function () return b end -- closure with upvalue
177 end
178
179 -- upvalues must be different
180 assert(X() == 10 and Y() == 20)
181end
182
183
184-- testing closures x repeat-until
185
186local a = {}
187local i = 1
188repeat
189 local x = i
190 a[i] = function () i = x+1; return x end
191until i > 10 or a[i]() ~= x
192assert(i == 11 and a[1]() == 1 and a[3]() == 3 and i == 4)
193
194
195-- testing closures created in 'then' and 'else' parts of 'if's
196a = {}
197for i = 1, 10 do
198 if i % 3 == 0 then
199 local y = 0
200 a[i] = function (x) local t = y; y = x; return t end
201 elseif i % 3 == 1 then
202 goto L1
203 error'not here'
204 ::L1::
205 local y = 1
206 a[i] = function (x) local t = y; y = x; return t end
207 elseif i % 3 == 2 then
208 local t
209 goto l4
210 ::l4a:: a[i] = t; goto l4b
211 error("should never be here!")
212 ::l4::
213 local y = 2
214 t = function (x) local t = y; y = x; return t end
215 goto l4a
216 error("should never be here!")
217 ::l4b::
218 end
219end
220
221for i = 1, 10 do
222 assert(a[i](i * 10) == i % 3 and a[i]() == i * 10)
223end
224
225print'+'
226
227
228-- test for correctly closing upvalues in tail calls of vararg functions
229local function t ()
230 local function c(a,b) assert(a=="test" and b=="OK") end
231 local function v(f, ...) c("test", f() ~= 1 and "FAILED" or "OK") end
232 local x = 1
233 return v(function() return x end)
234end
235t()
236
237
238-- test for debug manipulation of upvalues
239local debug = require'debug'
240
241local foo1, foo2, foo3
242do
243 local a , b, c = 3, 5, 7
244 foo1 = function () return a+b end;
245 foo2 = function () return b+a end;
246 do
247 local a = 10
248 foo3 = function () return a+b end;
249 end
250end
251
252assert(debug.upvalueid(foo1, 1))
253assert(debug.upvalueid(foo1, 2))
254assert(not debug.upvalueid(foo1, 3))
255assert(debug.upvalueid(foo1, 1) == debug.upvalueid(foo2, 2))
256assert(debug.upvalueid(foo1, 2) == debug.upvalueid(foo2, 1))
257assert(debug.upvalueid(foo3, 1))
258assert(debug.upvalueid(foo1, 1) ~= debug.upvalueid(foo3, 1))
259assert(debug.upvalueid(foo1, 2) == debug.upvalueid(foo3, 2))
260
261assert(debug.upvalueid(string.gmatch("x", "x"), 1) ~= nil)
262
263assert(foo1() == 3 + 5 and foo2() == 5 + 3)
264debug.upvaluejoin(foo1, 2, foo2, 2)
265assert(foo1() == 3 + 3 and foo2() == 5 + 3)
266assert(foo3() == 10 + 5)
267debug.upvaluejoin(foo3, 2, foo2, 1)
268assert(foo3() == 10 + 5)
269debug.upvaluejoin(foo3, 2, foo2, 2)
270assert(foo3() == 10 + 3)
271
272assert(not pcall(debug.upvaluejoin, foo1, 3, foo2, 1))
273assert(not pcall(debug.upvaluejoin, foo1, 1, foo2, 3))
274assert(not pcall(debug.upvaluejoin, foo1, 0, foo2, 1))
275assert(not pcall(debug.upvaluejoin, print, 1, foo2, 1))
276assert(not pcall(debug.upvaluejoin, {}, 1, foo2, 1))
277assert(not pcall(debug.upvaluejoin, foo1, 1, print, 1))
278
279print'OK'