1-- This script is to test ahocorasick.so not libac.so
2--
3local ac = require "ahocorasick"
4
5local ac_create = ac.create
6local ac_match = ac.match
7local string_fmt = string.format
8local string_sub = string.sub
9
10local err_cnt = 0
11local function mytest(testname, dict, match, notmatch)
12    print(">Testing ", testname)
13
14    io.write(string_fmt("Dictionary: "));
15    for i=1, #dict do
16       io.write(string_fmt("%s, ", dict[i]))
17    end
18    print ""
19
20    local ac_inst = ac_create(dict);
21    for i=1, #match do
22        local str = match[i][1]
23        local substr = match[i][2]
24        io.write(string_fmt("Matching %s, ", str))
25        local b, e = ac_match(ac_inst, str)
26        if b and e and (string_sub(str, b+1, e+1) == substr) then
27            print "pass"
28        else
29            err_cnt = err_cnt + 1
30            print "fail"
31        end
32        --print("gc is called")
33        collectgarbage()
34    end
35
36    if notmatch == nil then
37        return
38    end
39
40    for i = 1, #notmatch do
41        local str = notmatch[i]
42        io.write(string_fmt("*Matching %s, ", str))
43        local r = ac_match(ac_inst, str)
44        if r then
45            err_cnt = err_cnt + 1
46            print("fail")
47        else
48            print("succ")
49        end
50        collectgarbage()
51    end
52end
53
54mytest("test1",
55       {"he", "she", "his", "her", "str\0ing"},
56       -- matching cases
57       { {"he", "he"}, {"she", "she"}, {"his", "his"}, {"hers", "he"},
58         {"ahe", "he"}, {"shhe", "he"}, {"shis2", "his"}, {"ahhe", "he"},
59         {"str\0ing", "str\0ing"}
60       },
61
62       -- not matching case
63       {"str\0", "str"}
64
65       )
66
67os.exit((err_cnt == 0) and 0 or 1)
68