You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

trie.lua 1.8KB

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