---
source: src/main.rs
expression: compiled
input_file: test-data/lua5.2-tests/sort.lua
---
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 {
a[(i)] = i
}
assert(select(lim, unpack(a)) == lim && select('#', unpack(a)) == lim);
x = unpack(a)
assert(x == 1);
x = {
unpack(a)
}
assert(#x == lim && x[(1)] == 1 && x[(lim)] == lim);
x = {
unpack(a, lim - 2)
}
assert(#x == 3 && x[(1)] == lim - 2 && x[(3)] == lim);
x = {
unpack(a, 10, 6)
}
assert(next(x) == nil);
x = {
unpack(a, 11, 10)
}
assert(next(x) == nil);
x, y = unpack(a, 10, 10)
assert(x == 10 && y == nil);
x, y, z = unpack(a, 10, 11)
assert(x == 10 && y == 11 && z == nil);
a, x = unpack({
1
})
assert(a == 1 && x == nil);
a, x = unpack({
1,
2
}, 1, 1)
assert(a == 1 && x == nil);
if !_no32 {
assert(!pcall(unpack, {}, 0, 2 ^ 31 - 1));
assert(!pcall(unpack, {}, 1, 2 ^ 31 - 1));
assert(!pcall(unpack, {}, -(2 ^ 31), 2 ^ 31 - 1));
assert(!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 && b == nil);
a, b = unpack({
2 ^ 31 - 1 = 20
}, 2 ^ 31 - 2, 2 ^ 31 - 1)
assert(a == nil && b == 20);
}
print("testing pack");
a = table.pack()
assert(a[(1)] == nil && a.n == 0);
a = table.pack(table)
assert(a[(1)] == table && a.n == 1);
a = table.pack(nil, nil, nil, nil)
assert(a[(1)] == nil && a.n == 4);
print("testing sort");
local fn check(t) {
local fn f(a, b) {
assert(a && b);
return true
}
local s, e = pcall(table.sort, t, f)
assert(!s && e::find("invalid order function"));
}
check({
1,
2,
3,
4
});
check({
1,
2,
3,
4,
5
});
check({
1,
2,
3,
4,
5,
6
});
global fn check(a, f) {
f = f || fn (x, y) {
return x < y
}
for n = #a, 2, -1 {
assert(!f(a[(n)], a[(n - 1)]));
}
}
a = {
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
}
table.sort(a);
check(a);
global fn perm(s, n) {
n = n || #s
if n == 1 {
local t = {
unpack(s)
}
table.sort(t);
check(t);
} else {
for i = 1, n {
s[(i)], s[(n)] = s[(n)], s[(i)]
perm(s, n - 1);
s[(i)], s[(n)] = s[(n)], s[(i)]
}
}
}
perm({});
perm({
1
});
perm({
1,
2
});
perm({
1,
2,
3
});
perm({
1,
2,
3,
4
});
perm({
2,
2,
3,
4
});
perm({
1,
2,
3,
4,
5
});
perm({
1,
2,
3,
3,
5
});
perm({
1,
2,
3,
4,
5,
6
});
perm({
2,
2,
3,
3,
5,
6
});
limit = 30000
if _soft {
limit = 5000
}
a = {}
for i = 1, limit {
a[(i)] = math.random()
}
local x = os.clock()
table.sort(a);
print(string.format("Sorting %d elements in %.2f sec.", limit, os.clock() - x));
check(a);
x = os.clock()
table.sort(a);
print(string.format("Re-sorting %d elements in %.2f sec.", limit, os.clock() - x));
check(a);
a = {}
for i = 1, limit {
a[(i)] = math.random()
}
x = os.clock()
i = 0
table.sort(a, fn (x, y) {
i = i + 1
return y < x
});
print(string.format("Invert-sorting other %d elements in %.2f sec., with %i comparisons", limit, os.clock() - x, i));
check(a, fn (x, y) {
return y < x
});
table.sort({});
for i = 1, limit {
a[(i)] = false
}
x = os.clock()
table.sort(a, fn (x, y) {
return nil
});
print(string.format("Sorting %d equal elements in %.2f sec.", limit, os.clock() - x));
check(a, fn (x, y) {
return nil
});
for i, v with pairs(a) {
assert(!v || i == 'n' && v == limit);
}
A = {
"álo",
"\0first :-)",
"alo",
"then this one",
"45",
"and a new"
}
table.sort(A);
check(A);
table.sort(A, fn (x, y) {
load(string.format("A[%q] = ''", x))();
collectgarbage();
return x < y
});
tt = {
__lt = fn (a, b) {
return a.val < b.val
}
}
a = {}
for i = 1, 10 {
a[(i)] = {
val = math.random(100)
}
setmetatable(a[(i)], tt);
}
table.sort(a);
check(a, tt.__lt);
check(a);
print("OK");