summaryrefslogtreecommitdiffstats
path: root/modules/policy/lua-aho-corasick/tests/lua_test.lua
blob: cfe178f3ff8bf8372cd13adf186d72024c09602f (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
58
59
60
61
62
63
64
65
66
67
-- This script is to test ahocorasick.so not libac.so
--
local ac = require "ahocorasick"

local ac_create = ac.create
local ac_match = ac.match
local string_fmt = string.format
local string_sub = string.sub

local err_cnt = 0
local function mytest(testname, dict, match, notmatch)
    print(">Testing ", testname)

    io.write(string_fmt("Dictionary: "));
    for i=1, #dict do
       io.write(string_fmt("%s, ", dict[i]))
    end
    print ""

    local ac_inst = ac_create(dict);
    for i=1, #match do
        local str = match[i][1]
        local substr = match[i][2]
        io.write(string_fmt("Matching %s, ", str))
        local b, e = ac_match(ac_inst, str)
        if b and e and (string_sub(str, b+1, e+1) == substr) then
            print "pass"
        else
            err_cnt = err_cnt + 1
            print "fail"
        end
        --print("gc is called")
        collectgarbage()
    end

    if notmatch == nil then
        return
    end

    for i = 1, #notmatch do
        local str = notmatch[i]
        io.write(string_fmt("*Matching %s, ", str))
        local r = ac_match(ac_inst, str)
        if r then
            err_cnt = err_cnt + 1
            print("fail")
        else
            print("succ")
        end
        collectgarbage()
    end
end

mytest("test1",
       {"he", "she", "his", "her", "str\0ing"},
       -- matching cases
       { {"he", "he"}, {"she", "she"}, {"his", "his"}, {"hers", "he"},
         {"ahe", "he"}, {"shhe", "he"}, {"shis2", "his"}, {"ahhe", "he"},
         {"str\0ing", "str\0ing"}
       },

       -- not matching case
       {"str\0", "str"}

       )

os.exit((err_cnt == 0) and 0 or 1)