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