]> source.dussan.org Git - rspamd.git/commitdiff
Add unit tests for lua_trie.
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 7 Apr 2015 14:04:10 +0000 (15:04 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 7 Apr 2015 14:04:29 +0000 (15:04 +0100)
test/lua/unit/test_tld.dat [new file with mode: 0644]
test/lua/unit/trie.lua [new file with mode: 0644]

diff --git a/test/lua/unit/test_tld.dat b/test/lua/unit/test_tld.dat
new file mode 100644 (file)
index 0000000..fbdaa71
--- /dev/null
@@ -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 (file)
index 0000000..cff933d
--- /dev/null
@@ -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