summaryrefslogtreecommitdiffstats
path: root/modules/policy/lua-aho-corasick/tests/load_ac_test.lua
blob: 7fb7db9c7ba5ceab7de091d80ea06f524bd0018b (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
-- This script is to test load_ac.lua
--
-- Some notes:
--   1. The purpose of this script is not to check if the libac.so work
--      properly, it is to check if there are something stupid in load_ac.lua
--
--   2. There are bunch of collectgarbage() calls, the purpose is to make
--      sure the shared lib is not unloaded after GC.

-- load_ac.lua looks up libac.so via package.cpath rather than LD_LIBRARY_PATH,
-- prepend (instead of appending) some insane paths here to see if it quit
-- prematurely.
--
package.cpath = ".;./?.so;" .. package.cpath

local ac = require "load_ac"

local ac_create = ac.create_ac
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);
    collectgarbage()
    for i=1, #match do
        local str = match[i]
        io.write(string_fmt("Matching %s, ", str))
        local b = ac_match(ac_inst, str)
        if b then
            print "pass"
        else
            err_cnt = err_cnt + 1
            print "fail"
        end
        collectgarbage()
    end

    if notmatch == nil then
        return
    end

    collectgarbage()

    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
    ac_inst = nil
    collectgarbage()
end

print("")
print("====== Test to see if load_ac.lua works properly ========")

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

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

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