lua
[Ported from github.com/lua/lua] Cast in 'rl_readline_name'
Brought by drakestapleton
| 1 | -- $Id: testes/closure.lua $ |
| 2 | -- See Copyright Notice in file lua.h |
| 3 | |
| 4 | global <const> * |
| 5 | |
| 6 | print "testing closures" |
| 7 | |
| 8 | do -- 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 |
| 13 | end |
| 14 | |
| 15 | |
| 16 | local A,B = 0,{g=10} |
| 17 | local 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 |
| 33 | end |
| 34 | |
| 35 | local a = f(10) |
| 36 | -- force a GC in this level |
| 37 | local x = {[1] = {}} -- to detect a GC |
| 38 | setmetatable(x, {__mode = 'kv'}) |
| 39 | while x[1] do -- repeat until GC |
| 40 | local a = A..A..A..A -- create garbage |
| 41 | A = A+1 |
| 42 | end |
| 43 | assert(a[1]() == 20+A) |
| 44 | assert(a[1]() == 30+A) |
| 45 | assert(a[2]() == 10+A) |
| 46 | collectgarbage() |
| 47 | assert(a[2]() == 20+A) |
| 48 | assert(a[2]() == 30+A) |
| 49 | assert(a[3]() == 20+A) |
| 50 | assert(a[8]() == 10+A) |
| 51 | assert(getmetatable(x).__mode == 'kv') |
| 52 | assert(B.g == 19) |
| 53 | |
| 54 | |
| 55 | -- testing equality |
| 56 | a = {} |
| 57 | |
| 58 | for i = 1, 5 do a[i] = function (x) return i + a + _ENV end end |
| 59 | assert(a[3] ~= a[4] and a[4] ~= a[5]) |
| 60 | |
| 61 | do |
| 62 | local a = function (x) return math.sin(_ENV[x]) end |
| 63 | local function f() |
| 64 | return a |
| 65 | end |
| 66 | assert(f() == f()) |
| 67 | end |
| 68 | |
| 69 | |
| 70 | -- testing closures with 'for' control variable |
| 71 | a = {} |
| 72 | for i=1,10 do |
| 73 | a[i] = function () return i end |
| 74 | if i == 3 then break end |
| 75 | end |
| 76 | assert(a[4] == undef) |
| 77 | assert(a[2]() == 2) |
| 78 | assert(a[3]() == 3) |
| 79 | |
| 80 | a = {} |
| 81 | local t = {"a", "b"} |
| 82 | for 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 |
| 87 | end |
| 88 | a[1].set(10) |
| 89 | local r,s = a[2].get() |
| 90 | assert(r == 2 and s == 'b') |
| 91 | r,s = a[1].get() |
| 92 | assert(r == 1 and s == 10) |
| 93 | a[2].set('a') |
| 94 | r,s = a[2].get() |
| 95 | assert(r == 2 and s == "a") |
| 96 | |
| 97 | |
| 98 | -- testing closures with 'for' control variable x break |
| 99 | local f |
| 100 | for i=1,3 do |
| 101 | f = function () return i end |
| 102 | break |
| 103 | end |
| 104 | assert(f() == 1) |
| 105 | |
| 106 | for k = 1, #t do |
| 107 | local v = t[k] |
| 108 | f = function () return k, v end |
| 109 | break |
| 110 | end |
| 111 | assert(({f()})[1] == 1) |
| 112 | assert(({f()})[2] == "a") |
| 113 | |
| 114 | |
| 115 | -- testing closure x break x return x errors |
| 116 | |
| 117 | local b |
| 118 | function 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 |
| 136 | end |
| 137 | |
| 138 | for i=1,3 do |
| 139 | f(i) |
| 140 | assert(b('get') == 'xuxu') |
| 141 | b('set', 10); assert(b('get') == 10+i) |
| 142 | b = nil |
| 143 | end |
| 144 | |
| 145 | pcall(f, 4); |
| 146 | assert(b('get') == 'xuxu') |
| 147 | b('set', 10); assert(b('get') == 14) |
| 148 | |
| 149 | |
| 150 | local y, w |
| 151 | -- testing multi-level closure |
| 152 | function f(x) |
| 153 | return function (y) |
| 154 | return function (z) return w+x+y+z end |
| 155 | end |
| 156 | end |
| 157 | |
| 158 | y = f(10) |
| 159 | w = 1.345 |
| 160 | assert(y(20)(30) == 60+w) |
| 161 | |
| 162 | |
| 163 | -- testing closures x break |
| 164 | do |
| 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) |
| 181 | end |
| 182 | |
| 183 | |
| 184 | -- testing closures x repeat-until |
| 185 | |
| 186 | local a = {} |
| 187 | local i = 1 |
| 188 | repeat |
| 189 | local x = i |
| 190 | a[i] = function () i = x+1; return x end |
| 191 | until i > 10 or a[i]() ~= x |
| 192 | assert(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 |
| 196 | a = {} |
| 197 | for 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 |
| 219 | end |
| 220 | |
| 221 | for i = 1, 10 do |
| 222 | assert(a[i](i * 10) == i % 3 and a[i]() == i * 10) |
| 223 | end |
| 224 | |
| 225 | print'+' |
| 226 | |
| 227 | |
| 228 | -- test for correctly closing upvalues in tail calls of vararg functions |
| 229 | local 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) |
| 234 | end |
| 235 | t() |
| 236 | |
| 237 | |
| 238 | -- test for debug manipulation of upvalues |
| 239 | local debug = require'debug' |
| 240 | |
| 241 | local foo1, foo2, foo3 |
| 242 | do |
| 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 |
| 250 | end |
| 251 | |
| 252 | assert(debug.upvalueid(foo1, 1)) |
| 253 | assert(debug.upvalueid(foo1, 2)) |
| 254 | assert(not debug.upvalueid(foo1, 3)) |
| 255 | assert(debug.upvalueid(foo1, 1) == debug.upvalueid(foo2, 2)) |
| 256 | assert(debug.upvalueid(foo1, 2) == debug.upvalueid(foo2, 1)) |
| 257 | assert(debug.upvalueid(foo3, 1)) |
| 258 | assert(debug.upvalueid(foo1, 1) ~= debug.upvalueid(foo3, 1)) |
| 259 | assert(debug.upvalueid(foo1, 2) == debug.upvalueid(foo3, 2)) |
| 260 | |
| 261 | assert(debug.upvalueid(string.gmatch("x", "x"), 1) ~= nil) |
| 262 | |
| 263 | assert(foo1() == 3 + 5 and foo2() == 5 + 3) |
| 264 | debug.upvaluejoin(foo1, 2, foo2, 2) |
| 265 | assert(foo1() == 3 + 3 and foo2() == 5 + 3) |
| 266 | assert(foo3() == 10 + 5) |
| 267 | debug.upvaluejoin(foo3, 2, foo2, 1) |
| 268 | assert(foo3() == 10 + 5) |
| 269 | debug.upvaluejoin(foo3, 2, foo2, 2) |
| 270 | assert(foo3() == 10 + 3) |
| 271 | |
| 272 | assert(not pcall(debug.upvaluejoin, foo1, 3, foo2, 1)) |
| 273 | assert(not pcall(debug.upvaluejoin, foo1, 1, foo2, 3)) |
| 274 | assert(not pcall(debug.upvaluejoin, foo1, 0, foo2, 1)) |
| 275 | assert(not pcall(debug.upvaluejoin, print, 1, foo2, 1)) |
| 276 | assert(not pcall(debug.upvaluejoin, {}, 1, foo2, 1)) |
| 277 | assert(not pcall(debug.upvaluejoin, foo1, 1, print, 1)) |
| 278 | |
| 279 | print'OK' |