2 --*****************************************************************************
3 --* Copyright (C) 1994-2016 Lua.org, PUC-Rio.
5 --* Permission is hereby granted, free of charge, to any person obtaining
6 --* a copy of this software and associated documentation files (the
7 --* "Software"), to deal in the Software without restriction, including
8 --* without limitation the rights to use, copy, modify, merge, publish,
9 --* distribute, sublicense, and/or sell copies of the Software, and to
10 --* permit persons to whom the Software is furnished to do so, subject to
11 --* the following conditions:
13 --* The above copyright notice and this permission notice shall be
14 --* included in all copies or substantial portions of the Software.
16 --* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 --* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 --* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 --* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 --* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 --* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 --* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 --*****************************************************************************
26 -- testing table library
28 -- workaround missing pcall in zfs lua implementation
29 local function tuple(...)
30 return {n=select('#', ...), ...}
33 function pcall(f, ...)
34 local co = coroutine.create(f)
35 local res = tuple(coroutine.resume(co, ...))
36 if res[1] and coroutine.status(co) == "suspended" then
39 return table.unpack(res, 1, res.n)
43 -- workaround missing math lib in zfs lua implementation
44 local A1, A2 = 727595, 798405 -- 5^17=D20*A1+A2
45 local D20, D40 = 1048576, 1099511627776 -- 2^20, 2^40
49 local V = (X1*A2 + X2*A1) % D20
59 local unpack = table.unpack
63 for i=1, lim do a[i]=i end
64 assert(select(lim, unpack(a)) == lim and select('#', unpack(a)) == lim)
68 assert(#x == lim and x[1] == 1 and x[lim] == lim)
69 x = {unpack(a, lim-2)}
70 assert(#x == 3 and x[1] == lim-2 and x[3] == lim)
71 x = {unpack(a, 10, 6)}
72 assert(next(x) == nil) -- no elements
73 x = {unpack(a, 11, 10)}
74 assert(next(x) == nil) -- no elements
75 x,y = unpack(a, 10, 10)
76 assert(x == 10 and y == nil)
77 x,y,z = unpack(a, 10, 11)
78 assert(x == 10 and y == 11 and z == nil)
80 assert(a==1 and x==nil)
81 a,x = unpack({1,2}, 1, 1)
82 assert(a==1 and x==nil)
85 assert(not pcall(unpack, {}, 0, 2^31-1))
86 assert(not pcall(unpack, {}, 1, 2^31-1))
87 assert(not pcall(unpack, {}, -(2^31), 2^31-1))
88 assert(not pcall(unpack, {}, -(2^31 - 1), 2^31-1))
89 assert(pcall(unpack, {}, 2^31-1, 0))
90 assert(pcall(unpack, {}, 2^31-1, 1))
91 pcall(unpack, {}, 1, 2^31)
92 a, b = unpack({[2^31-1] = 20}, 2^31-1, 2^31-1)
93 assert(a == 20 and b == nil)
94 a, b = unpack({[2^31-1] = 20}, 2^31-2, 2^31-1)
95 assert(a == nil and b == 20)
101 assert(a[1] == nil and a.n == 0)
103 a = table.pack(table)
104 assert(a[1] == table and a.n == 1)
106 a = table.pack(nil, nil, nil, nil)
107 assert(a[1] == nil and a.n == 4)
113 -- test checks for invalid order functions
114 local function check (t)
115 local function f(a, b) assert(a and b); return true end
116 local s, e = pcall(table.sort, t, f)
117 assert(not s and e:find("invalid order function"))
125 function check (a, f)
126 f = f or function (x,y) return x<y end;
128 assert(not f(a[n], a[n-1]))
132 a = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
141 local t = {unpack(s)}
146 s[i], s[n] = s[n], s[i]
148 s[i], s[n] = s[n], s[i]
183 table.sort(a, function(x,y) i=i+1; return y<x end)
184 check(a, function(x,y) return y<x end)
187 table.sort{} -- empty array
189 for i=1,limit do a[i] = false end
190 table.sort(a, function(x,y) return nil end)
191 check(a, function(x,y) return nil end)
192 for i,v in pairs(a) do assert(not v or i=='n' and v==limit) end
194 A = {"álo", "\0first :-)", "alo", "then this one", "45", "and a new"}
198 tt = {__lt = function (a,b) return a.val < b.val end}
200 for i=1,10 do a[i] = {val=rand(100)}; setmetatable(a[i], tt); end
207 local function test (a)
208 table.insert(a, 10); table.insert(a, 2, 20);
209 table.insert(a, 1, -1); table.insert(a, 40);
210 table.insert(a, #a+1, 50)
211 table.insert(a, 2, -2)
212 assert(table.remove(a,1) == -1)
213 assert(table.remove(a,1) == -2)
214 assert(table.remove(a,1) == 10)
215 assert(table.remove(a,1) == 20)
216 assert(table.remove(a,1) == 40)
217 assert(table.remove(a,1) == 50)
218 assert(table.remove(a,1) == nil)
221 a = {n=0, [-7] = "ban"}
223 assert(a.n == 0 and a[-7] == "ban")
227 assert(a.n == nil and #a == 0 and a[-7] == "ban")
230 table.insert(a, 1, 10); table.insert(a, 1, 20); table.insert(a, 1, -1)
231 assert(table.remove(a) == 10)
232 assert(table.remove(a) == 20)
233 assert(table.remove(a) == -1)
236 table.insert(a, 3, 'a')
238 assert(table.remove(a, 1) == 'c')
239 assert(table.remove(a, 1) == 'd')
240 assert(table.remove(a, 1) == 'a')
241 assert(table.remove(a, 1) == 'b')
242 assert(#a == 0 and a.n == nil)
246 assert(table.remove(a, #a) == 40)
248 assert(table.remove(a, 2) == 20)
249 assert(a[#a] == 30 and #a == 2)