aboutsummaryrefslogtreecommitdiff
path: root/testes/locals.lua
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--testes/locals.lua181
1 files changed, 181 insertions, 0 deletions
diff --git a/testes/locals.lua b/testes/locals.lua
new file mode 100644
index 00000000..f0780a03
--- /dev/null
+++ b/testes/locals.lua
@@ -0,0 +1,181 @@
1-- $Id: locals.lua,v 1.41 2018/06/19 12:25:39 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, "T") 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 = T.querytab(a)
115
116 for k,_ in pairs(a) do a[k] = undef 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
122
123 -- testing allocation errors during table insertions
124 local a = {}
125 local function additems ()
126 a.x = true; a.y = true; a.z = true
127 a[1] = true
128 a[2] = true
129 end
130 for i = 1, math.huge do
131 T.alloccount(i)
132 local st, msg = pcall(additems)
133 T.alloccount()
134 local count = 0
135 for k, v in pairs(a) do
136 assert(a[k] == v)
137 count = count + 1
138 end
139 if st then assert(count == 5); break end
140 end
141end
142
143
144-- testing lexical environments
145
146assert(_ENV == _G)
147
148do
149local dummy
150local _ENV = (function (...) return ... end)(_G, dummy) -- {
151
152do local _ENV = {assert=assert}; assert(true) end
153mt = {_G = _G}
154local foo,x
155A = false -- "declare" A
156do local _ENV = mt
157 function foo (x)
158 A = x
159 do local _ENV = _G; A = 1000 end
160 return function (x) return A .. x end
161 end
162end
163assert(getenv(foo) == mt)
164x = foo('hi'); assert(mt.A == 'hi' and A == 1000)
165assert(x('*') == mt.A .. '*')
166
167do local _ENV = {assert=assert, A=10};
168 do local _ENV = {assert=assert, A=20};
169 assert(A==20);x=A
170 end
171 assert(A==10 and x==20)
172end
173assert(x==20)
174
175
176print('OK')
177
178return 5,f
179
180end -- }
181