aboutsummaryrefslogtreecommitdiff
path: root/testes/sort.lua
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2022-12-28 18:34:11 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2022-12-28 18:34:11 -0300
commit314745ed8438d1276c6c928d5f9d4be018dfadb6 (patch)
tree594b7e873f2c29113d95c75147ab10865cdd772c /testes/sort.lua
parent0825cf237d9d3505155f8b40bcf83ea1b135e8da (diff)
downloadlua-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.lua23
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
20checkerror("wrong number of arguments", table.insert, {}, 2, 3, 4) 20checkerror("wrong number of arguments", table.insert, {}, 2, 3, 4)
21 21
22local x,y,z,a,n 22local x,y,z,a,n
23a = {}; lim = _soft and 200 or 2000 23a = {}; local lim = _soft and 200 or 2000
24for i=1, lim do a[i]=i end 24for i=1, lim do a[i]=i end
25assert(select(lim, unpack(a)) == lim and select('#', unpack(a)) == lim) 25assert(select(lim, unpack(a)) == lim and select('#', unpack(a)) == lim)
26x = unpack(a) 26x = unpack(a)
@@ -222,7 +222,7 @@ a = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
222table.sort(a) 222table.sort(a)
223check(a) 223check(a)
224 224
225function perm (s, n) 225local 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}
248perm{1,2,3,4,5,6} 248perm{1,2,3,4,5,6}
249perm{2,2,3,3,5,6} 249perm{2,2,3,3,5,6}
250 250
251function timesort (a, n, func, msg, pre) 251local 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)
258end 258end
259 259
260limit = 50000 260local limit = 50000
261if _soft then limit = 5000 end 261if _soft then limit = 5000 end
262 262
263a = {} 263a = {}
@@ -274,7 +274,7 @@ for i=1,limit do
274 a[i] = math.random() 274 a[i] = math.random()
275end 275end
276 276
277x = os.clock(); i=0 277local x = os.clock(); local i = 0
278table.sort(a, function(x,y) i=i+1; return y<x end) 278table.sort(a, function(x,y) i=i+1; return y<x end)
279x = (os.clock() - x) * 1000 279x = (os.clock() - x) * 1000
280print(string.format("Invert-sorting other %d elements in %.2f msec., with %i comparisons", 280print(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
290for i,v in pairs(a) do assert(v == false) end 290for i,v in pairs(a) do assert(v == false) end
291 291
292A = {"álo", "\0first :-)", "alo", "then this one", "45", "and a new"} 292AA = {"álo", "\0first :-)", "alo", "then this one", "45", "and a new"}
293table.sort(A) 293table.sort(AA)
294check(A) 294check(AA)
295 295
296table.sort(A, function (x, y) 296table.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
303tt = {__lt = function (a,b) return a.val < b.val end} 304local tt = {__lt = function (a,b) return a.val < b.val end}
304a = {} 305a = {}
305for i=1,10 do a[i] = {val=math.random(100)}; setmetatable(a[i], tt); end 306for i=1,10 do a[i] = {val=math.random(100)}; setmetatable(a[i], tt); end
306table.sort(a) 307table.sort(a)