summaryrefslogtreecommitdiff
path: root/testes/locals.lua
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--testes/locals.lua162
1 files changed, 162 insertions, 0 deletions
diff --git a/testes/locals.lua b/testes/locals.lua
new file mode 100644
index 00000000..f66f6f7a
--- /dev/null
+++ b/testes/locals.lua
@@ -0,0 +1,162 @@
1-- $Id: locals.lua,v 1.37 2016/11/07 13:11:28 roberto Exp $
2-- See Copyright Notice in file all.lua
3
4print('testing local variables and environments')
5
6local debug = require"debug"
7
8
9-- bug in 5.1:
10
11local function f(x) x = nil; return x end
12assert(f(10) == nil)
13
14local function f() local x; return x end
15assert(f(10) == nil)
16
17local function f(x) x = nil; local y; return x, y end
18assert(f(10) == nil and select(2, f(20)) == nil)
19
20do
21 local i = 10
22 do local i = 100; assert(i==100) end
23 do local i = 1000; assert(i==1000) end
24 assert(i == 10)
25 if i ~= 10 then
26 local i = 20
27 else
28 local i = 30
29 assert(i == 30)
30 end
31end
32
33
34
35f = nil
36
37local f
38x = 1
39
40a = nil
41load('local a = {}')()
42assert(a == nil)
43
44function f (a)
45 local _1, _2, _3, _4, _5
46 local _6, _7, _8, _9, _10
47 local x = 3
48 local b = a
49 local c,d = a,b
50 if (d == b) then
51 local x = 'q'
52 x = b
53 assert(x == 2)
54 else
55 assert(nil)
56 end
57 assert(x == 3)
58 local f = 10
59end
60
61local b=10
62local a; repeat local b; a,b=1,2; assert(a+1==b); until a+b==3
63
64
65assert(x == 1)
66
67f(2)
68assert(type(f) == 'function')
69
70
71local function getenv (f)
72 local a,b = debug.getupvalue(f, 1)
73 assert(a == '_ENV')
74 return b
75end
76
77-- test for global table of loaded chunks
78assert(getenv(load"a=3") == _G)
79local c = {}; local f = load("a = 3", nil, nil, c)
80assert(getenv(f) == c)
81assert(c.a == nil)
82f()
83assert(c.a == 3)
84
85-- old test for limits for special instructions (now just a generic test)
86do
87 local i = 2
88 local p = 4 -- p == 2^i
89 repeat
90 for j=-3,3 do
91 assert(load(string.format([[local a=%s;
92 a=a+%s;
93 assert(a ==2^%s)]], j, p-j, i), '')) ()
94 assert(load(string.format([[local a=%s;
95 a=a-%s;
96 assert(a==-2^%s)]], -j, p-j, i), '')) ()
97 assert(load(string.format([[local a,b=0,%s;
98 a=b-%s;
99 assert(a==-2^%s)]], -j, p-j, i), '')) ()
100 end
101 p = 2 * p; i = i + 1
102 until p <= 0
103end
104
105print'+'
106
107
108if rawget(_G, "querytab") then
109 -- testing clearing of dead elements from tables
110 collectgarbage("stop") -- stop GC
111 local a = {[{}] = 4, [3] = 0, alo = 1,
112 a1234567890123456789012345678901234567890 = 10}
113
114 local t = querytab(a)
115
116 for k,_ in pairs(a) do a[k] = nil end
117 collectgarbage() -- restore GC and collect dead fiels in `a'
118 for i=0,t-1 do
119 local k = querytab(a, i)
120 assert(k == nil or type(k) == 'number' or k == 'alo')
121 end
122end
123
124
125-- testing lexical environments
126
127assert(_ENV == _G)
128
129do
130local dummy
131local _ENV = (function (...) return ... end)(_G, dummy) -- {
132
133do local _ENV = {assert=assert}; assert(true) end
134mt = {_G = _G}
135local foo,x
136A = false -- "declare" A
137do local _ENV = mt
138 function foo (x)
139 A = x
140 do local _ENV = _G; A = 1000 end
141 return function (x) return A .. x end
142 end
143end
144assert(getenv(foo) == mt)
145x = foo('hi'); assert(mt.A == 'hi' and A == 1000)
146assert(x('*') == mt.A .. '*')
147
148do local _ENV = {assert=assert, A=10};
149 do local _ENV = {assert=assert, A=20};
150 assert(A==20);x=A
151 end
152 assert(A==10 and x==20)
153end
154assert(x==20)
155
156
157print('OK')
158
159return 5,f
160
161end -- }
162