diff options
Diffstat (limited to 'testes/locals.lua')
-rw-r--r-- | testes/locals.lua | 162 |
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 | |||
4 | print('testing local variables and environments') | ||
5 | |||
6 | local debug = require"debug" | ||
7 | |||
8 | |||
9 | -- bug in 5.1: | ||
10 | |||
11 | local function f(x) x = nil; return x end | ||
12 | assert(f(10) == nil) | ||
13 | |||
14 | local function f() local x; return x end | ||
15 | assert(f(10) == nil) | ||
16 | |||
17 | local function f(x) x = nil; local y; return x, y end | ||
18 | assert(f(10) == nil and select(2, f(20)) == nil) | ||
19 | |||
20 | do | ||
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 | ||
31 | end | ||
32 | |||
33 | |||
34 | |||
35 | f = nil | ||
36 | |||
37 | local f | ||
38 | x = 1 | ||
39 | |||
40 | a = nil | ||
41 | load('local a = {}')() | ||
42 | assert(a == nil) | ||
43 | |||
44 | function 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 | ||
59 | end | ||
60 | |||
61 | local b=10 | ||
62 | local a; repeat local b; a,b=1,2; assert(a+1==b); until a+b==3 | ||
63 | |||
64 | |||
65 | assert(x == 1) | ||
66 | |||
67 | f(2) | ||
68 | assert(type(f) == 'function') | ||
69 | |||
70 | |||
71 | local function getenv (f) | ||
72 | local a,b = debug.getupvalue(f, 1) | ||
73 | assert(a == '_ENV') | ||
74 | return b | ||
75 | end | ||
76 | |||
77 | -- test for global table of loaded chunks | ||
78 | assert(getenv(load"a=3") == _G) | ||
79 | local c = {}; local f = load("a = 3", nil, nil, c) | ||
80 | assert(getenv(f) == c) | ||
81 | assert(c.a == nil) | ||
82 | f() | ||
83 | assert(c.a == 3) | ||
84 | |||
85 | -- old test for limits for special instructions (now just a generic test) | ||
86 | do | ||
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 | ||
103 | end | ||
104 | |||
105 | print'+' | ||
106 | |||
107 | |||
108 | if 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 | ||
122 | end | ||
123 | |||
124 | |||
125 | -- testing lexical environments | ||
126 | |||
127 | assert(_ENV == _G) | ||
128 | |||
129 | do | ||
130 | local dummy | ||
131 | local _ENV = (function (...) return ... end)(_G, dummy) -- { | ||
132 | |||
133 | do local _ENV = {assert=assert}; assert(true) end | ||
134 | mt = {_G = _G} | ||
135 | local foo,x | ||
136 | A = false -- "declare" A | ||
137 | do 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 | ||
143 | end | ||
144 | assert(getenv(foo) == mt) | ||
145 | x = foo('hi'); assert(mt.A == 'hi' and A == 1000) | ||
146 | assert(x('*') == mt.A .. '*') | ||
147 | |||
148 | do 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) | ||
153 | end | ||
154 | assert(x==20) | ||
155 | |||
156 | |||
157 | print('OK') | ||
158 | |||
159 | return 5,f | ||
160 | |||
161 | end -- } | ||
162 | |||