1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
-- Trie search tests
context("Trie search functions", function()
local t = require "rspamd_trie"
local logger = require "rspamd_logger"
local patterns = {
'test',
'est',
'he',
'she',
'str\1ing'
}
local trie = t.create(patterns)
local cases = {
{'test', true, {{4, 1}, {4, 2}}},
{'she test test', true, {{3, 4}, {3, 3}, {8, 1}, {8, 2}, {13, 1}, {13, 2}}},
{'non-existent', false},
{'str\1ing test', true, {{7, 5}, {12, 1}, {12, 2}}},
}
local function cmp_tables(t1, t2)
if t1[2] ~= t2[2] then
return t1[2] < t2[2]
else
return t1[1] < t2[1]
end
end
for i,c in ipairs(cases) do
test("Trie search " .. i, function()
local res = {}
local function cb(idx, pos)
table.insert(res, {pos, idx})
return 0
end
ret = trie:match(c[1], cb)
assert_equal(c[2], ret, tostring(c[2]) .. ' while matching ' .. c[1])
if ret then
table.sort(c[3], cmp_tables)
table.sort(res, cmp_tables)
assert_rspamd_table_eq({
expect = c[3],
actual = res
})
end
end)
end
for i,c in ipairs(cases) do
test("Trie search, table version " .. i, function()
local match = {}
match = trie:match(c[1])
assert_equal(c[2], #match > 0, tostring(c[2]) .. ' while matching ' .. c[1])
if match and #match > 0 then
local res = {}
-- Convert to something that this test expects
for pat,hits in pairs(match) do
for _,pos in ipairs(hits) do
table.insert(res, {pos, pat})
end
end
table.sort(c[3], cmp_tables)
table.sort(res, cmp_tables)
assert_rspamd_table_eq({
expect = c[3],
actual = res
})
end
end)
end
end)
|