diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2022-12-28 18:34:11 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2022-12-28 18:34:11 -0300 |
commit | 314745ed8438d1276c6c928d5f9d4be018dfadb6 (patch) | |
tree | 594b7e873f2c29113d95c75147ab10865cdd772c /testes/constructs.lua | |
parent | 0825cf237d9d3505155f8b40bcf83ea1b135e8da (diff) | |
download | lua-314745ed8438d1276c6c928d5f9d4be018dfadb6.tar.gz lua-314745ed8438d1276c6c928d5f9d4be018dfadb6.tar.bz2 lua-314745ed8438d1276c6c928d5f9d4be018dfadb6.zip |
Avoid excessive name pollution in test files
Test files are more polite regarding the use of globals when locals
would do, and when globals are necessary deleting them after use.
Diffstat (limited to 'testes/constructs.lua')
-rw-r--r-- | testes/constructs.lua | 36 |
1 files changed, 20 insertions, 16 deletions
diff --git a/testes/constructs.lua b/testes/constructs.lua index 0d9ec92d..6ac68166 100644 --- a/testes/constructs.lua +++ b/testes/constructs.lua | |||
@@ -11,6 +11,7 @@ local function checkload (s, msg) | |||
11 | end | 11 | end |
12 | 12 | ||
13 | -- testing semicollons | 13 | -- testing semicollons |
14 | local a | ||
14 | do ;;; end | 15 | do ;;; end |
15 | ; do ; a = 3; assert(a == 3) end; | 16 | ; do ; a = 3; assert(a == 3) end; |
16 | ; | 17 | ; |
@@ -49,10 +50,10 @@ assert((((nil and true) or false) and true) == false) | |||
49 | 50 | ||
50 | local a,b = 1,nil; | 51 | local a,b = 1,nil; |
51 | assert(-(1 or 2) == -1 and (1 and 2)+(-1.25 or -4) == 0.75); | 52 | assert(-(1 or 2) == -1 and (1 and 2)+(-1.25 or -4) == 0.75); |
52 | x = ((b or a)+1 == 2 and (10 or a)+1 == 11); assert(x); | 53 | local x = ((b or a)+1 == 2 and (10 or a)+1 == 11); assert(x); |
53 | x = (((2<3) or 1) == true and (2<3 and 4) == 4); assert(x); | 54 | x = (((2<3) or 1) == true and (2<3 and 4) == 4); assert(x); |
54 | 55 | ||
55 | x,y=1,2; | 56 | local x, y = 1, 2; |
56 | assert((x>y) and x or y == 2); | 57 | assert((x>y) and x or y == 2); |
57 | x,y=2,1; | 58 | x,y=2,1; |
58 | assert((x>y) and x or y == 2); | 59 | assert((x>y) and x or y == 2); |
@@ -77,13 +78,13 @@ do -- testing operators with diffent kinds of constants | |||
77 | local gab = f(o1, o2) | 78 | local gab = f(o1, o2) |
78 | 79 | ||
79 | _ENV.XX = o1 | 80 | _ENV.XX = o1 |
80 | code = string.format("return XX %s %s", op, o2) | 81 | local code = string.format("return XX %s %s", op, o2) |
81 | res = assert(load(code))() | 82 | local res = assert(load(code))() |
82 | assert(res == gab) | 83 | assert(res == gab) |
83 | 84 | ||
84 | _ENV.XX = o2 | 85 | _ENV.XX = o2 |
85 | local code = string.format("return (%s) %s XX", o1, op) | 86 | code = string.format("return (%s) %s XX", o1, op) |
86 | local res = assert(load(code))() | 87 | res = assert(load(code))() |
87 | assert(res == gab) | 88 | assert(res == gab) |
88 | 89 | ||
89 | code = string.format("return (%s) %s %s", o1, op, o2) | 90 | code = string.format("return (%s) %s %s", o1, op, o2) |
@@ -92,6 +93,7 @@ do -- testing operators with diffent kinds of constants | |||
92 | end | 93 | end |
93 | end | 94 | end |
94 | end | 95 | end |
96 | _ENV.XX = nil | ||
95 | end | 97 | end |
96 | 98 | ||
97 | 99 | ||
@@ -100,7 +102,7 @@ repeat until 1; repeat until true; | |||
100 | while false do end; while nil do end; | 102 | while false do end; while nil do end; |
101 | 103 | ||
102 | do -- test old bug (first name could not be an `upvalue') | 104 | do -- test old bug (first name could not be an `upvalue') |
103 | local a; function f(x) x={a=1}; x={x=1}; x={G=1} end | 105 | local a; local function f(x) x={a=1}; x={x=1}; x={G=1} end |
104 | end | 106 | end |
105 | 107 | ||
106 | 108 | ||
@@ -128,7 +130,7 @@ do -- bug since 5.4.0 | |||
128 | end | 130 | end |
129 | 131 | ||
130 | 132 | ||
131 | function f (i) | 133 | local function f (i) |
132 | if type(i) ~= 'number' then return i,'jojo'; end; | 134 | if type(i) ~= 'number' then return i,'jojo'; end; |
133 | if i > 0 then return i, f(i-1); end; | 135 | if i > 0 then return i, f(i-1); end; |
134 | end | 136 | end |
@@ -154,10 +156,10 @@ end | |||
154 | assert(f(3) == 'a' and f(12) == 'b' and f(26) == 'c' and f(100) == nil) | 156 | assert(f(3) == 'a' and f(12) == 'b' and f(26) == 'c' and f(100) == nil) |
155 | 157 | ||
156 | for i=1,1000 do break; end; | 158 | for i=1,1000 do break; end; |
157 | n=100; | 159 | local n=100; |
158 | i=3; | 160 | local i=3; |
159 | t = {}; | 161 | local t = {}; |
160 | a=nil | 162 | local a=nil |
161 | while not a do | 163 | while not a do |
162 | a=0; for i=1,n do for i=i,1,-1 do a=a+1; t[i]=1; end; end; | 164 | a=0; for i=1,n do for i=i,1,-1 do a=a+1; t[i]=1; end; end; |
163 | end | 165 | end |
@@ -200,14 +202,14 @@ a={y=1} | |||
200 | x = {a.y} | 202 | x = {a.y} |
201 | assert(x[1] == 1) | 203 | assert(x[1] == 1) |
202 | 204 | ||
203 | function f(i) | 205 | local function f (i) |
204 | while 1 do | 206 | while 1 do |
205 | if i>0 then i=i-1; | 207 | if i>0 then i=i-1; |
206 | else return; end; | 208 | else return; end; |
207 | end; | 209 | end; |
208 | end; | 210 | end; |
209 | 211 | ||
210 | function g(i) | 212 | local function g(i) |
211 | while 1 do | 213 | while 1 do |
212 | if i>0 then i=i-1 | 214 | if i>0 then i=i-1 |
213 | else return end | 215 | else return end |
@@ -272,7 +274,7 @@ function g (a,b,c,d,e) | |||
272 | if not (a>=b or c or d and e or nil) then return 0; else return 1; end; | 274 | if not (a>=b or c or d and e or nil) then return 0; else return 1; end; |
273 | end | 275 | end |
274 | 276 | ||
275 | function h (a,b,c,d,e) | 277 | local function h (a,b,c,d,e) |
276 | while (a>=b or c or (d and e) or nil) do return 1; end; | 278 | while (a>=b or c or (d and e) or nil) do return 1; end; |
277 | return 0; | 279 | return 0; |
278 | end; | 280 | end; |
@@ -300,7 +302,7 @@ do | |||
300 | assert(a==2) | 302 | assert(a==2) |
301 | end | 303 | end |
302 | 304 | ||
303 | function F(a) | 305 | local function F (a) |
304 | assert(debug.getinfo(1, "n").name == 'F') | 306 | assert(debug.getinfo(1, "n").name == 'F') |
305 | return a,2,3 | 307 | return a,2,3 |
306 | end | 308 | end |
@@ -393,6 +395,8 @@ for n = 1, level do | |||
393 | if i % 60000 == 0 then print('+') end | 395 | if i % 60000 == 0 then print('+') end |
394 | end | 396 | end |
395 | end | 397 | end |
398 | IX = nil | ||
399 | _G.GLOB1 = nil | ||
396 | ------------------------------------------------------------------ | 400 | ------------------------------------------------------------------ |
397 | 401 | ||
398 | -- testing some syntax errors (chosen through 'gcov') | 402 | -- testing some syntax errors (chosen through 'gcov') |