106 lines
2 KiB
Lua
106 lines
2 KiB
Lua
require "itertools"
|
|
require "testtools"
|
|
|
|
-- itertools tests
|
|
|
|
function test_iteritems()
|
|
local t1 = {a = 0, b = 1, c = 2, d = 3, e = 4}
|
|
local t2 = {}
|
|
for pair in iteritems(t1) do
|
|
t2[getkey(pair)] = getvalue(pair)
|
|
end
|
|
check_tables(t1, t2)
|
|
end
|
|
|
|
function test_itervalues ()
|
|
local t1 = { 0, 1, 2, 3, 4}
|
|
local t2 = {}
|
|
for v in itervalues(t1) do
|
|
table.insert(t2, v)
|
|
end
|
|
-- This is only true on array, not generic table
|
|
check_tables(t1, t2)
|
|
end
|
|
|
|
function test_iterkeys()
|
|
t1 = {a = true, b = true, c = true, d = true, e = true}
|
|
t2 = {}
|
|
for k in iterkeys(t1) do
|
|
t2[k] = true
|
|
end
|
|
check_tables(t1, t2)
|
|
end
|
|
|
|
function test_collect()
|
|
t0 = nil
|
|
t1 = {0, 1, 2, 3, 4}
|
|
t2 = collect(t1)
|
|
check_tables(t1, t2)
|
|
end
|
|
|
|
function test_filter()
|
|
function iseven(x)
|
|
return x % 2 == 0
|
|
end
|
|
res = collect(filter(iseven, {0, 1, 2, 3, 4}))
|
|
check_tables({0, 2, 4}, res)
|
|
end
|
|
|
|
function test_map()
|
|
function double(x)
|
|
return 2 * x
|
|
end
|
|
res = collect(map(double, {1, 2, 3}))
|
|
check_tables({2, 4, 6}, res)
|
|
end
|
|
|
|
function test_reduce()
|
|
function add(x, y)
|
|
return x + y
|
|
end
|
|
t = reduce(add, 10, {1, 2, 3, 4})
|
|
assert(20 == t)
|
|
end
|
|
|
|
function test_nulliter()
|
|
res = collect(nulliter())
|
|
check_tables({}, res)
|
|
end
|
|
|
|
function test_concat()
|
|
t1 = {0, 1, 2}
|
|
t2 = {3, 4, 5}
|
|
res = collect(concat(t1, t2))
|
|
check_tables({0, 1, 2, 3, 4, 5}, res)
|
|
end
|
|
|
|
function test_concat_from_iterable()
|
|
t = {
|
|
a = {0, 1, 2},
|
|
b = {3, 4, 5}
|
|
}
|
|
res = collect(concat_from_iterable(t))
|
|
check_tables({0, 1, 2, 3, 4, 5}, res)
|
|
end
|
|
|
|
function test_unique()
|
|
t1 = {0, 1, 4, 0, 3, 2, 1, 4}
|
|
res = collect(unique(t1))
|
|
check_tables({0, 1, 4, 3, 2}, res)
|
|
end
|
|
|
|
function test_any()
|
|
assert(any({false, true, false}))
|
|
assert(not any({false, false, false}))
|
|
end
|
|
|
|
function test_all()
|
|
assert(all({true, true, true}))
|
|
assert(not all({true, true, false}))
|
|
end
|
|
|
|
function test_none()
|
|
assert(none({false, false, false}))
|
|
assert(not none({false, false, true}))
|
|
end
|
|
|