print "testing (parts of) table library" print "testing unpack" local unpack = table.unpack local x,y,z,a,n a = {}; lim = 2000 for i=1, lim do a[i]=i end assert(select(lim, unpack(a)) == lim and select('#', unpack(a)) == lim) x = unpack(a) assert(x == 1) x = {unpack(a)} assert(#x == lim and x[1] == 1 and x[lim] == lim) x = {unpack(a, lim-2)} assert(#x == 3 and x[1] == lim-2 and x[3] == lim) x = {unpack(a, 10, 6)} assert(next(x) == nil) -- no elements x = {unpack(a, 11, 10)} assert(next(x) == nil) -- no elements x,y = unpack(a, 10, 10) assert(x == 10 and y == nil) x,y,z = unpack(a, 10, 11) assert(x == 10 and y == 11 and z == nil) a,x = unpack{1} assert(a==1 and x==nil) a,x = unpack({1,2}, 1, 1) assert(a==1 and x==nil) if not _no32 then assert(not pcall(unpack, {}, 0, 2^31-1)) assert(not pcall(unpack, {}, 1, 2^31-1)) assert(not pcall(unpack, {}, -(2^31), 2^31-1)) assert(not pcall(unpack, {}, -(2^31 - 1), 2^31-1)) assert(pcall(unpack, {}, 2^31-1, 0)) assert(pcall(unpack, {}, 2^31-1, 1)) pcall(unpack, {}, 1, 2^31) a, b = unpack({[2^31-1] = 20}, 2^31-1, 2^31-1) assert(a == 20 and b == nil) a, b = unpack({[2^31-1] = 20}, 2^31-2, 2^31-1) assert(a == nil and b == 20) end print "testing pack" a = table.pack() assert(a[1] == nil and a.n == 0) a = table.pack(table) assert(a[1] == table and a.n == 1) a = table.pack(nil, nil, nil, nil) assert(a[1] == nil and a.n == 4) print"testing sort" -- test checks for invalid order functions local function check (t) local function f(a, b) assert(a and b); return true end local s, e = pcall(table.sort, t, f) assert(not s and e:find("invalid order function")) end check{1,2,3,4} check{1,2,3,4,5} check{1,2,3,4,5,6} function check (a, f) f = f or function (x,y) return x