summaryrefslogtreecommitdiffstats
path: root/test/lua/unit/trie.lua
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-10 21:30:40 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-10 21:30:40 +0000
commit133a45c109da5310add55824db21af5239951f93 (patch)
treeba6ac4c0a950a0dda56451944315d66409923918 /test/lua/unit/trie.lua
parentInitial commit. (diff)
downloadrspamd-133a45c109da5310add55824db21af5239951f93.tar.xz
rspamd-133a45c109da5310add55824db21af5239951f93.zip
Adding upstream version 3.8.1.upstream/3.8.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'test/lua/unit/trie.lua')
-rw-r--r--test/lua/unit/trie.lua81
1 files changed, 81 insertions, 0 deletions
diff --git a/test/lua/unit/trie.lua b/test/lua/unit/trie.lua
new file mode 100644
index 0000000..9532eae
--- /dev/null
+++ b/test/lua/unit/trie.lua
@@ -0,0 +1,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)