]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/zfs-tests/tests/functional/channel_program/lua_core/tst.lib_table.lua
Vendor import of openzfs master @ 184df27eef0abdc7ab2105b21257f753834b936b
[FreeBSD/FreeBSD.git] / tests / zfs-tests / tests / functional / channel_program / lua_core / tst.lib_table.lua
1 --[[
2 --*****************************************************************************
3 --* Copyright (C) 1994-2016 Lua.org, PUC-Rio.
4 --*
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:
12 --*
13 --* The above copyright notice and this permission notice shall be
14 --* included in all copies or substantial portions of the Software.
15 --*
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 --*****************************************************************************
24 --]]
25
26 -- testing table library
27
28 -- workaround missing pcall in zfs lua implementation
29 local function tuple(...)
30   return {n=select('#', ...), ...}
31 end
32
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
37     res[1] = false
38   end
39   return table.unpack(res, 1, res.n)
40 end
41
42
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
46 local X1, X2 = 0, 1
47 function rand()
48     local U = X2*A2
49     local V = (X1*A2 + X2*A1) % D20
50     V = (V*D20 + U) % D40
51     X1 = V/D20
52     X2 = V - X1*D20
53     return V*100/D40
54 end
55
56
57 -- testing unpack
58
59 local unpack = table.unpack
60
61 local x,y,z,a,n
62 a = {}; lim = 2000
63 for i=1, lim do a[i]=i end
64 assert(select(lim, unpack(a)) == lim and select('#', unpack(a)) == lim)
65 x = unpack(a)
66 assert(x == 1)
67 x = {unpack(a)}
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)
79 a,x = unpack{1}
80 assert(a==1 and x==nil)
81 a,x = unpack({1,2}, 1, 1)
82 assert(a==1 and x==nil)
83
84 if not _no32 then
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)
96 end
97
98 -- testing pack
99
100 a = table.pack()
101 assert(a[1] == nil and a.n == 0)
102
103 a = table.pack(table)
104 assert(a[1] == table and a.n == 1)
105
106 a = table.pack(nil, nil, nil, nil)
107 assert(a[1] == nil and a.n == 4)
108
109
110 -- testing sort
111
112
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"))
118 end
119
120 check{1,2,3,4}
121 check{1,2,3,4,5}
122 check{1,2,3,4,5,6}
123
124
125 function check (a, f)
126   f = f or function (x,y) return x<y end;
127   for n = #a, 2, -1 do
128     assert(not f(a[n], a[n-1]))
129   end
130 end
131
132 a = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
133      "Oct", "Nov", "Dec"}
134
135 table.sort(a)
136 check(a)
137
138 function perm (s, n)
139   n = n or #s
140   if n == 1 then
141     local t = {unpack(s)}
142     table.sort(t)
143     check(t)
144   else
145     for i = 1, n do
146       s[i], s[n] = s[n], s[i]
147       perm(s, n - 1)
148       s[i], s[n] = s[n], s[i]
149     end
150   end
151 end
152
153 perm{}
154 perm{1}
155 perm{1,2}
156 perm{1,2,3}
157 perm{1,2,3,4}
158 perm{2,2,3,4}
159 perm{1,2,3,4,5}
160 perm{1,2,3,3,5}
161 perm{1,2,3,4,5,6}
162 perm{2,2,3,3,5,6}
163
164 limit = 5000
165
166 a = {}
167 for i=1,limit do
168   a[i] = rand()
169 end
170
171 table.sort(a)
172 check(a)
173
174 table.sort(a)
175 check(a)
176
177 a = {}
178 for i=1,limit do
179   a[i] = rand()
180 end
181
182 i=0
183 table.sort(a, function(x,y) i=i+1; return y<x end)
184 check(a, function(x,y) return y<x end)
185
186
187 table.sort{}  -- empty array
188
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
193
194 A = {"álo", "\0first :-)", "alo", "then this one", "45", "and a new"}
195 table.sort(A)
196 check(A)
197
198 tt = {__lt = function (a,b) return a.val < b.val end}
199 a = {}
200 for i=1,10 do  a[i] = {val=rand(100)}; setmetatable(a[i], tt); end
201 table.sort(a)
202 check(a, tt.__lt)
203 check(a)
204
205
206 -- test remove
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)
219 end
220
221 a = {n=0, [-7] = "ban"}
222 test(a)
223 assert(a.n == 0 and a[-7] == "ban")
224
225 a = {[-7] = "ban"};
226 test(a)
227 assert(a.n == nil and #a == 0 and a[-7] == "ban")
228
229
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)
234
235 a = {'c', 'd'}
236 table.insert(a, 3, 'a')
237 table.insert(a, 'b')
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)
243
244 a = {10,20,30,40}
245 assert(a[#a] == 40)
246 assert(table.remove(a, #a) == 40)
247 assert(a[#a] == 30)
248 assert(table.remove(a, 2) == 20)
249 assert(a[#a] == 30 and #a == 2)
250
251
252 return "OK"