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/sort.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/sort.lua')
-rw-r--r-- | testes/sort.lua | 23 |
1 files changed, 12 insertions, 11 deletions
diff --git a/testes/sort.lua b/testes/sort.lua index ef405d92..52919b8c 100644 --- a/testes/sort.lua +++ b/testes/sort.lua | |||
@@ -20,7 +20,7 @@ end | |||
20 | checkerror("wrong number of arguments", table.insert, {}, 2, 3, 4) | 20 | checkerror("wrong number of arguments", table.insert, {}, 2, 3, 4) |
21 | 21 | ||
22 | local x,y,z,a,n | 22 | local x,y,z,a,n |
23 | a = {}; lim = _soft and 200 or 2000 | 23 | a = {}; local lim = _soft and 200 or 2000 |
24 | for i=1, lim do a[i]=i end | 24 | for i=1, lim do a[i]=i end |
25 | assert(select(lim, unpack(a)) == lim and select('#', unpack(a)) == lim) | 25 | assert(select(lim, unpack(a)) == lim and select('#', unpack(a)) == lim) |
26 | x = unpack(a) | 26 | x = unpack(a) |
@@ -222,7 +222,7 @@ a = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", | |||
222 | table.sort(a) | 222 | table.sort(a) |
223 | check(a) | 223 | check(a) |
224 | 224 | ||
225 | function perm (s, n) | 225 | local function perm (s, n) |
226 | n = n or #s | 226 | n = n or #s |
227 | if n == 1 then | 227 | if n == 1 then |
228 | local t = {unpack(s)} | 228 | local t = {unpack(s)} |
@@ -248,7 +248,7 @@ perm{1,2,3,3,5} | |||
248 | perm{1,2,3,4,5,6} | 248 | perm{1,2,3,4,5,6} |
249 | perm{2,2,3,3,5,6} | 249 | perm{2,2,3,3,5,6} |
250 | 250 | ||
251 | function timesort (a, n, func, msg, pre) | 251 | local function timesort (a, n, func, msg, pre) |
252 | local x = os.clock() | 252 | local x = os.clock() |
253 | table.sort(a, func) | 253 | table.sort(a, func) |
254 | x = (os.clock() - x) * 1000 | 254 | x = (os.clock() - x) * 1000 |
@@ -257,7 +257,7 @@ function timesort (a, n, func, msg, pre) | |||
257 | check(a, func) | 257 | check(a, func) |
258 | end | 258 | end |
259 | 259 | ||
260 | limit = 50000 | 260 | local limit = 50000 |
261 | if _soft then limit = 5000 end | 261 | if _soft then limit = 5000 end |
262 | 262 | ||
263 | a = {} | 263 | a = {} |
@@ -274,7 +274,7 @@ for i=1,limit do | |||
274 | a[i] = math.random() | 274 | a[i] = math.random() |
275 | end | 275 | end |
276 | 276 | ||
277 | x = os.clock(); i=0 | 277 | local x = os.clock(); local i = 0 |
278 | table.sort(a, function(x,y) i=i+1; return y<x end) | 278 | table.sort(a, function(x,y) i=i+1; return y<x end) |
279 | x = (os.clock() - x) * 1000 | 279 | x = (os.clock() - x) * 1000 |
280 | print(string.format("Invert-sorting other %d elements in %.2f msec., with %i comparisons", | 280 | print(string.format("Invert-sorting other %d elements in %.2f msec., with %i comparisons", |
@@ -289,18 +289,19 @@ timesort(a, limit, function(x,y) return nil end, "equal") | |||
289 | 289 | ||
290 | for i,v in pairs(a) do assert(v == false) end | 290 | for i,v in pairs(a) do assert(v == false) end |
291 | 291 | ||
292 | A = {"álo", "\0first :-)", "alo", "then this one", "45", "and a new"} | 292 | AA = {"álo", "\0first :-)", "alo", "then this one", "45", "and a new"} |
293 | table.sort(A) | 293 | table.sort(AA) |
294 | check(A) | 294 | check(AA) |
295 | 295 | ||
296 | table.sort(A, function (x, y) | 296 | table.sort(AA, function (x, y) |
297 | load(string.format("A[%q] = ''", x), "")() | 297 | load(string.format("AA[%q] = ''", x), "")() |
298 | collectgarbage() | 298 | collectgarbage() |
299 | return x<y | 299 | return x<y |
300 | end) | 300 | end) |
301 | 301 | ||
302 | _G.AA = nil | ||
302 | 303 | ||
303 | tt = {__lt = function (a,b) return a.val < b.val end} | 304 | local tt = {__lt = function (a,b) return a.val < b.val end} |
304 | a = {} | 305 | a = {} |
305 | for i=1,10 do a[i] = {val=math.random(100)}; setmetatable(a[i], tt); end | 306 | for i=1,10 do a[i] = {val=math.random(100)}; setmetatable(a[i], tt); end |
306 | table.sort(a) | 307 | table.sort(a) |