aboutsummaryrefslogtreecommitdiffstats
path: root/test/lua/unit/trie.lua
blob: 6fa7573f93802e35a0992680e823c9191f99156b (plain)
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
-- 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:match(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)