interplane.Get Omega
Read in your language

lua

[Ported from github.com/lua/lua] Cast in 'rl_readline_name'

Brought by drakestapleton

branch master Branches Tags History

lua / testes / sort.lua

354 lines · 9625 bytes Raw Blame History
1-- $Id: testes/sort.lua $
2-- See Copyright Notice in file lua.h
3
4print "testing (parts of) table library"
5
6local maxI = math.maxinteger
7local minI = math.mininteger
8
9
10local function checkerror (msg, f, ...)
11 local s, err = pcall(f, ...)
12 assert(not s and string.find(err, msg))
13end
14
15
16do print "testing 'table.create'"
17 local N = 10000
18 collectgarbage()
19 local m = collectgarbage("count") * 1024
20 local t = table.create(N)
21 local memdiff = collectgarbage("count") * 1024 - m
22 assert(memdiff > N * 4)
23 for i = 1, 20 do
24 assert(#t == i - 1)
25 t[i] = 0
26 end
27 for i = 1, 20 do t[#t + 1] = i * 10 end
28 assert(#t == 40 and t[39] == 190)
29 assert(not T or T.querytab(t) == N)
30 t = nil
31 collectgarbage()
32 m = collectgarbage("count") * 1024
33 t = table.create(0, 1024)
34 memdiff = collectgarbage("count") * 1024 - m
35 assert(memdiff > 1024 * 12)
36 assert(not T or select(2, T.querytab(t)) == 1024)
37
38 local maxint1 = 1 << (string.packsize("i") * 8 - 1)
39 checkerror("out of range", table.create, maxint1)
40 checkerror("out of range", table.create, 0, maxint1)
41 checkerror("table overflow", table.create, 0, maxint1 - 1)
42end
43
44
45print "testing unpack"
46
47local unpack = table.unpack
48
49
50checkerror("wrong number of arguments", table.insert, {}, 2, 3, 4)
51
52local x,y,z,a,n
53a = {}; local lim = _soft and 200 or 2000
54for i=1, lim do a[i]=i end
55assert(select(lim, unpack(a)) == lim and select('#', unpack(a)) == lim)
56x = unpack(a)
57assert(x == 1)
58x = {unpack(a)}
59assert(#x == lim and x[1] == 1 and x[lim] == lim)
60x = {unpack(a, lim-2)}
61assert(#x == 3 and x[1] == lim-2 and x[3] == lim)
62x = {unpack(a, 10, 6)}
63assert(next(x) == nil) -- no elements
64x = {unpack(a, 11, 10)}
65assert(next(x) == nil) -- no elements
66x,y = unpack(a, 10, 10)
67assert(x == 10 and y == nil)
68x,y,z = unpack(a, 10, 11)
69assert(x == 10 and y == 11 and z == nil)
70a,x = unpack{1}
71assert(a==1 and x==nil)
72a,x = unpack({1,2}, 1, 1)
73assert(a==1 and x==nil)
74
75
76do -- unpack with non-tables
77 local debug = require"debug"
78 local oldmt = debug.getmetatable(0)
79 local str = "hello"
80 debug.setmetatable(0,
81 { __len = function () return #str end,
82 __index = function (_, i) return string.sub(str, i, i) end})
83 assert(table.concat({table.unpack(0)}) == str)
84 debug.setmetatable(0, oldmt) -- restore original metatable for numbers
85end
86
87
88do
89 local maxi = (1 << 31) - 1 -- maximum value for an int (usually)
90 local mini = -(1 << 31) -- minimum value for an int (usually)
91 checkerror("too many results", unpack, {}, 0, maxi)
92 checkerror("too many results", unpack, {}, 1, maxi)
93 checkerror("too many results", unpack, {}, 0, maxI)
94 checkerror("too many results", unpack, {}, 1, maxI)
95 checkerror("too many results", unpack, {}, mini, maxi)
96 checkerror("too many results", unpack, {}, -maxi, maxi)
97 checkerror("too many results", unpack, {}, minI, maxI)
98 unpack({}, maxi, 0)
99 unpack({}, maxi, 1)
100 unpack({}, maxI, minI)
101 pcall(unpack, {}, 1, maxi + 1)
102 local a, b = unpack({[maxi] = 20}, maxi, maxi)
103 assert(a == 20 and b == nil)
104 a, b = unpack({[maxi] = 20}, maxi - 1, maxi)
105 assert(a == nil and b == 20)
106 local t = {[maxI - 1] = 12, [maxI] = 23}
107 a, b = unpack(t, maxI - 1, maxI); assert(a == 12 and b == 23)
108 a, b = unpack(t, maxI, maxI); assert(a == 23 and b == nil)
109 a, b = unpack(t, maxI, maxI - 1); assert(a == nil and b == nil)
110 t = {[minI] = 12.3, [minI + 1] = 23.5}
111 a, b = unpack(t, minI, minI + 1); assert(a == 12.3 and b == 23.5)
112 a, b = unpack(t, minI, minI); assert(a == 12.3 and b == nil)
113 a, b = unpack(t, minI + 1, minI); assert(a == nil and b == nil)
114end
115
116do -- length is not an integer
117 local t = setmetatable({}, {__len = function () return 'abc' end})
118 assert(#t == 'abc')
119 checkerror("object length is not an integer", table.insert, t, 1)
120end
121
122print "testing pack"
123
124a = table.pack()
125assert(a[1] == undef and a.n == 0)
126
127a = table.pack(table)
128assert(a[1] == table and a.n == 1)
129
130a = table.pack(nil, nil, nil, nil)
131assert(a[1] == nil and a.n == 4)
132
133
134-- testing move
135do
136
137 checkerror("table expected", table.move, 1, 2, 3, 4)
138
139 local function eqT (a, b)
140 for k, v in pairs(a) do assert(b[k] == v) end
141 for k, v in pairs(b) do assert(a[k] == v) end
142 end
143
144 local a = table.move({10,20,30}, 1, 3, 2) -- move forward
145 eqT(a, {10,10,20,30})
146
147 -- move forward with overlap of 1
148 a = table.move({10, 20, 30}, 1, 3, 3)
149 eqT(a, {10, 20, 10, 20, 30})
150
151 -- moving to the same table (not being explicit about it)
152 a = {10, 20, 30, 40}
153 table.move(a, 1, 4, 2, a)
154 eqT(a, {10, 10, 20, 30, 40})
155
156 a = table.move({10,20,30}, 2, 3, 1) -- move backward
157 eqT(a, {20,30,30})
158
159 a = {} -- move to new table
160 assert(table.move({10,20,30}, 1, 3, 1, a) == a)
161 eqT(a, {10,20,30})
162
163 a = {}
164 assert(table.move({10,20,30}, 1, 0, 3, a) == a) -- empty move (no move)
165 eqT(a, {})
166
167 a = table.move({10,20,30}, 1, 10, 1) -- move to the same place
168 eqT(a, {10,20,30})
169
170 -- moving on the fringes
171 a = table.move({[maxI - 2] = 1, [maxI - 1] = 2, [maxI] = 3},
172 maxI - 2, maxI, -10, {})
173 eqT(a, {[-10] = 1, [-9] = 2, [-8] = 3})
174
175 a = table.move({[minI] = 1, [minI + 1] = 2, [minI + 2] = 3},
176 minI, minI + 2, -10, {})
177 eqT(a, {[-10] = 1, [-9] = 2, [-8] = 3})
178
179 a = table.move({45}, 1, 1, maxI)
180 eqT(a, {45, [maxI] = 45})
181
182 a = table.move({[maxI] = 100}, maxI, maxI, minI)
183 eqT(a, {[minI] = 100, [maxI] = 100})
184
185 a = table.move({[minI] = 100}, minI, minI, maxI)
186 eqT(a, {[minI] = 100, [maxI] = 100})
187
188 a = setmetatable({}, {
189 __index = function (_,k) return k * 10 end,
190 __newindex = error})
191 local b = table.move(a, 1, 10, 3, {})
192 eqT(a, {})
193 eqT(b, {nil,nil,10,20,30,40,50,60,70,80,90,100})
194
195 b = setmetatable({""}, {
196 __index = error,
197 __newindex = function (t,k,v)
198 t[1] = string.format("%s(%d,%d)", t[1], k, v)
199 end})
200 table.move(a, 10, 13, 3, b)
201 assert(b[1] == "(3,100)(4,110)(5,120)(6,130)")
202 local stat, msg = pcall(table.move, b, 10, 13, 3, b)
203 assert(not stat and msg == b)
204end
205
206do
207 -- for very long moves, just check initial accesses and interrupt
208 -- move with an error
209 local function checkmove (f, e, t, x, y)
210 local pos1, pos2
211 local a = setmetatable({}, {
212 __index = function (_,k) pos1 = k end,
213 __newindex = function (_,k) pos2 = k; error() end, })
214 local st, msg = pcall(table.move, a, f, e, t)
215 assert(not st and pos1 == x and pos2 == y)
216 end
217 checkmove(1, maxI, 0, 1, 0)
218 checkmove(0, maxI - 1, 1, maxI - 1, maxI)
219 checkmove(minI, -2, -5, -2, maxI - 6)
220 checkmove(minI + 1, -1, -2, -1, maxI - 3)
221 checkmove(minI, -2, 0, minI, 0) -- non overlapping
222 checkmove(minI + 1, -1, 1, minI + 1, 1) -- non overlapping
223end
224
225checkerror("too many", table.move, {}, 0, maxI, 1)
226checkerror("too many", table.move, {}, -1, maxI - 1, 1)
227checkerror("too many", table.move, {}, minI, -1, 1)
228checkerror("too many", table.move, {}, minI, maxI, 1)
229checkerror("wrap around", table.move, {}, 1, maxI, 2)
230checkerror("wrap around", table.move, {}, 1, 2, maxI)
231checkerror("wrap around", table.move, {}, minI, -2, 2)
232
233
234print"testing sort"
235
236
237-- strange lengths
238local a = setmetatable({}, {__len = function () return -1 end})
239assert(#a == -1)
240table.sort(a, error) -- should not compare anything
241a = setmetatable({}, {__len = function () return maxI end})
242checkerror("too big", table.sort, a)
243
244-- test checks for invalid order functions
245local function check (t)
246 local function f(a, b) assert(a and b); return true end
247 checkerror("invalid order function", table.sort, t, f)
248end
249
250check{1,2,3,4}
251check{1,2,3,4,5}
252check{1,2,3,4,5,6}
253
254
255function check (a, f)
256 f = f or function (x,y) return x<y end;
257 for n = #a, 2, -1 do
258 assert(not f(a[n], a[n-1]))
259 end
260end
261
262a = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
263 "Oct", "Nov", "Dec"}
264
265table.sort(a)
266check(a)
267
268local function perm (s, n)
269 n = n or #s
270 if n == 1 then
271 local t = {unpack(s)}
272 table.sort(t)
273 check(t)
274 else
275 for i = 1, n do
276 s[i], s[n] = s[n], s[i]
277 perm(s, n - 1)
278 s[i], s[n] = s[n], s[i]
279 end
280 end
281end
282
283perm{}
284perm{1}
285perm{1,2}
286perm{1,2,3}
287perm{1,2,3,4}
288perm{2,2,3,4}
289perm{1,2,3,4,5}
290perm{1,2,3,3,5}
291perm{1,2,3,4,5,6}
292perm{2,2,3,3,5,6}
293
294local function timesort (a, n, func, msg, pre)
295 local x = os.clock()
296 table.sort(a, func)
297 x = (os.clock() - x) * 1000
298 pre = pre or ""
299 print(string.format("%ssorting %d %s elements in %.2f msec.", pre, n, msg, x))
300 check(a, func)
301end
302
303local limit = 50000
304if _soft then limit = 5000 end
305
306a = {}
307for i=1,limit do
308 a[i] = math.random()
309end
310
311timesort(a, limit, nil, "random")
312
313timesort(a, limit, nil, "sorted", "re-")
314
315a = {}
316for i=1,limit do
317 a[i] = math.random()
318end
319
320local x = os.clock(); local i = 0
321table.sort(a, function(x,y) i=i+1; return y<x end)
322x = (os.clock() - x) * 1000
323print(string.format("Invert-sorting other %d elements in %.2f msec., with %i comparisons",
324 limit, x, i))
325check(a, function(x,y) return y<x end)
326
327
328table.sort{} -- empty array
329
330for i=1,limit do a[i] = false end
331timesort(a, limit, function(x,y) return nil end, "equal")
332
333for i,v in pairs(a) do assert(v == false) end
334
335AA = {"\xE1lo", "\0first :-)", "alo", "then this one", "45", "and a new"}
336table.sort(AA)
337check(AA)
338
339table.sort(AA, function (x, y)
340 load(string.format("AA[%q] = ''", x), "")()
341 collectgarbage()
342 return x<y
343 end)
344
345_G.AA = nil
346
347local tt = {__lt = function (a,b) return a.val < b.val end}
348a = {}
349for i=1,10 do a[i] = {val=math.random(100)}; setmetatable(a[i], tt); end
350table.sort(a)
351check(a, tt.__lt)
352check(a)
353
354print"OK"