summaryrefslogtreecommitdiffstats
path: root/test/lua
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2015-04-07 15:04:10 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2015-04-07 15:04:29 +0100
commit224c69e485d7cd589bfdb07a3a31cecd68697986 (patch)
tree6d8f18e99881cfbe9dcf7b271ed40df0953182dc /test/lua
parentb02707fb59f7c83d91be69d16b8ac7bcc85f67b1 (diff)
downloadrspamd-224c69e485d7cd589bfdb07a3a31cecd68697986.tar.gz
rspamd-224c69e485d7cd589bfdb07a3a31cecd68697986.zip
Add unit tests for lua_trie.
Diffstat (limited to 'test/lua')
-rw-r--r--test/lua/unit/test_tld.dat4
-rw-r--r--test/lua/unit/trie.lua57
2 files changed, 61 insertions, 0 deletions
diff --git a/test/lua/unit/test_tld.dat b/test/lua/unit/test_tld.dat
new file mode 100644
index 000000000..fbdaa71e7
--- /dev/null
+++ b/test/lua/unit/test_tld.dat
@@ -0,0 +1,4 @@
+com
+org
+net
+рф \ No newline at end of file
diff --git a/test/lua/unit/trie.lua b/test/lua/unit/trie.lua
new file mode 100644
index 000000000..cff933d61
--- /dev/null
+++ b/test/lua/unit/trie.lua
@@ -0,0 +1,57 @@
+-- Trie search tests
+
+context("Trie search functions", function()
+ local t = require "rspamd_trie"
+
+ test("Trie search", function()
+ local patterns = {
+ 'test',
+ 'est',
+ 'he',
+ 'she',
+ 'str\0ing'
+ }
+
+ local trie = t.create(patterns)
+ assert_not_nil(trie, "cannot create trie")
+
+ local cases = {
+ {'test', true, {{4, 0}, {4, 1}}},
+ {'she test test', true, {{3, 3}, {3, 2}, {8, 0}, {8, 1}, {13, 0}, {13, 1}}},
+ {'non-existent', false},
+ {'str\0ing test', true, {{7, 4}, {12, 0}, {12, 1}}},
+ }
+
+ local function comparetables(t1, t2)
+ if #t1 ~= #t2 then return false end
+ for i=1,#t1 do
+ if type(t1[i]) ~= type(t2[i]) then return false
+ elseif type(t1[i]) == 'table' then
+ if not comparetables(t1[i], t2[i]) then return false end
+ elseif t1[i] ~= t2[i] then
+ return false
+ end
+ end
+ return true
+ end
+
+ for _,c in ipairs(cases) do
+ local res = {}
+ local function cb(idx, pos)
+ table.insert(res, {pos, idx})
+
+ return 0
+ end
+
+ ret = trie:search_text(c[1], cb)
+
+ assert_equal(c[2], ret, tostring(c[2]) .. ' while matching ' .. c[1])
+
+ if ret then
+ local cmp = comparetables(res, c[3])
+ assert_true(cmp, 'valid results for case: ' .. c[1])
+ end
+ end
+
+ end)
+end) \ No newline at end of file