e71251fea89f249a2d31b8c2e45fbedfa59e986c
[metze/wireshark/wip.git] / test / lua / common_sets.lua
1 -- See Copyright Notice in the file LICENSE
2
3 -- This file should contain only test sets that behave identically
4 -- when being run with pcre or posix regex libraries.
5
6 local luatest = require "luatest"
7 local N = luatest.NT
8
9 local function norm(a) return a==nil and N or a end
10
11 local function get_gsub (lib)
12   return lib.gsub or
13     function (subj, pattern, repl, n)
14       return lib.new (pattern) : gsub (subj, repl, n)
15     end
16 end
17
18 local function set_f_gmatch (lib, flg)
19   -- gmatch (s, p, [cf], [ef])
20   local function test_gmatch (subj, patt)
21     local out, guard = {}, 10
22     for a, b in lib.gmatch (subj, patt) do
23       table.insert (out, { norm(a), norm(b) })
24       guard = guard - 1
25       if guard == 0 then break end
26     end
27     return unpack (out)
28   end
29   return {
30     Name = "Function gmatch",
31     Func = test_gmatch,
32   --{  subj             patt         results }
33     { {"ab",            lib.new"."}, {{"a",N}, {"b",N} } },
34     { {("abcd"):rep(3), "(.)b.(d)"}, {{"a","d"},{"a","d"},{"a","d"}} },
35     { {"abcd",          ".*" },      {{"abcd",N},{"",N}  } },--zero-length match
36     { {"abc",           "^." },      {{"a",N}} },--anchored pattern
37   }
38 end
39
40 local function set_f_split (lib, flg)
41   -- split (s, p, [cf], [ef])
42   local function test_split (subj, patt)
43     local out, guard = {}, 10
44     for a, b, c in lib.split (subj, patt) do
45       table.insert (out, { norm(a), norm(b), norm(c) })
46       guard = guard - 1
47       if guard == 0 then break end
48     end
49     return unpack (out)
50   end
51   return {
52     Name = "Function split",
53     Func = test_split,
54   --{  subj             patt      results }
55     { {"ab",     lib.new","},     {{"ab",N,N},                           } },
56     { {"ab",            ","},     {{"ab",N,N},                           } },
57     { {",",             ","},     {{"",",",N},     {"", N, N},           } },
58     { {",,",            ","},     {{"",",",N},     {"",",",N},  {"",N,N} } },
59     { {"a,b",           ","},     {{"a",",",N},    {"b",N,N},            } },
60     { {",a,b",          ","},     {{"",",",N},     {"a",",",N}, {"b",N,N}} },
61     { {"a,b,",          ","},     {{"a",",",N},    {"b",",",N}, {"",N,N} } },
62     { {"a,,b",          ","},     {{"a",",",N},    {"",",",N},  {"b",N,N}} },
63     { {"ab<78>c", "<(.)(.)>"},    {{"ab","7","8"}, {"c",N,N},            } },
64     { {"abc",          "^."},     {{"", "a",N},    {"bc",N,N},           } },--anchored pattern
65     { {"abc",           "^"},     {{"", "", N},    {"abc",N,N},          } },
66 --  { {"abc",           "$"},     {{"abc","",N},   {"",N,N},             } },
67 --  { {"abc",         "^|$"},     {{"", "", N},    {"abc","",N},{"",N,N},} },
68   }
69 end
70
71 local function set_f_find (lib, flg)
72   return {
73     Name = "Function find",
74     Func = lib.find,
75   --  {subj, patt, st},         { results }
76     { {"abcd", lib.new".+"},    { 1,4 }   },      -- [none]
77     { {"abcd", ".+"},           { 1,4 }   },      -- [none]
78     { {"abcd", ".+", 2},        { 2,4 }   },      -- positive st
79     { {"abcd", ".+", -2},       { 3,4 }   },      -- negative st
80     { {"abcd", ".*"},           { 1,4 }   },      -- [none]
81     { {"abc",  "bc"},           { 2,3 }   },      -- [none]
82     { {"abcd", "(.)b.(d)"},     { 1,4,"a","d" }}, -- [captures]
83   }
84 end
85
86 local function set_f_match (lib, flg)
87   return {
88     Name = "Function match",
89     Func = lib.match,
90   --  {subj, patt, st},         { results }
91     { {"abcd", lib.new".+"},    {"abcd"}  }, -- [none]
92     { {"abcd", ".+"},           {"abcd"}  }, -- [none]
93     { {"abcd", ".+", 2},        {"bcd"}   }, -- positive st
94     { {"abcd", ".+", -2},       {"cd"}    }, -- negative st
95     { {"abcd", ".*"},           {"abcd"}  }, -- [none]
96     { {"abc",  "bc"},           {"bc"}    }, -- [none]
97     { {"abcd", "(.)b.(d)"},     {"a","d"} }, -- [captures]
98   }
99 end
100
101 local function set_m_exec (lib, flg)
102   return {
103     Name = "Method exec",
104     Method = "exec",
105   --{patt},                 {subj, st}           { results }
106     { {".+"},               {"abcd"},            {1,4,{}}  }, -- [none]
107     { {".+"},               {"abcd",2},          {2,4,{}}  }, -- positive st
108     { {".+"},               {"abcd",-2},         {3,4,{}}  }, -- negative st
109     { {".*"},               {"abcd"},            {1,4,{}}  }, -- [none]
110     { {"bc"},               {"abc"},             {2,3,{}}  }, -- [none]
111     { { "(.)b.(d)"},        {"abcd"},            {1,4,{1,1,4,4}}},--[captures]
112     { {"(a+)6+(b+)"},       {"Taa66bbT",2},      {2,7,{2,3,6,7}}},--[st+captures]
113   }
114 end
115
116 local function set_m_tfind (lib, flg)
117   return {
118     Name = "Method tfind",
119     Method = "tfind",
120   --{patt},                 {subj, st}           { results }
121     { {".+"},               {"abcd"},            {1,4,{}}  }, -- [none]
122     { {".+"},               {"abcd",2},          {2,4,{}}  }, -- positive st
123     { {".+"},               {"abcd",-2},         {3,4,{}}  }, -- negative st
124     { {".*"},               {"abcd"},            {1,4,{}}  }, -- [none]
125     { {"bc"},               {"abc"},             {2,3,{}}  }, -- [none]
126     { {"(.)b.(d)"},         {"abcd"},            {1,4,{"a","d"}}},--[captures]
127   }
128 end
129
130 local function set_m_find (lib, flg)
131   return {
132     Name = "Method find",
133     Method = "find",
134   --{patt},                 {subj, st}           { results }
135     { {".+"},               {"abcd"},            {1,4}  }, -- [none]
136     { {".+"},               {"abcd",2},          {2,4}  }, -- positive st
137     { {".+"},               {"abcd",-2},         {3,4}  }, -- negative st
138     { {".*"},               {"abcd"},            {1,4}  }, -- [none]
139     { {"bc"},               {"abc"},             {2,3}  }, -- [none]
140     { {"(.)b.(d)"},         {"abcd"},            {1,4,"a","d"}},--[captures]
141   }
142 end
143
144 local function set_m_match (lib, flg)
145   return {
146     Name = "Method match",
147     Method = "match",
148   --{patt},                 {subj, st}           { results }
149     { {".+"},               {"abcd"},            {"abcd"}  }, -- [none]
150     { {".+"},               {"abcd",2},          {"bcd" }  }, -- positive st
151     { {".+"},               {"abcd",-2},         {"cd"  }  }, -- negative st
152     { {".*"},               {"abcd"},            {"abcd"}  }, -- [none]
153     { {"bc"},               {"abc"},             {"bc"  }  }, -- [none]
154     {{ "(.)b.(d)"},         {"abcd"},            {"a","d"} }, --[captures]
155   }
156 end
157
158 local function set_f_gsub1 (lib, flg)
159   local subj, pat = "abcdef", "[abef]+"
160   local cpat = lib.new(pat)
161   return {
162     Name = "Function gsub, set1",
163     Func = get_gsub (lib),
164   --{ s,       p,    f,   n,    res1,  res2, res3 },
165     { {subj,  cpat,  "",  0},   {subj,    0, 0} }, -- test "n" + empty_replace
166     { {subj,   pat,  "",  0},   {subj,    0, 0} }, -- test "n" + empty_replace
167     { {subj,   pat,  "", -1},   {subj,    0, 0} }, -- test "n" + empty_replace
168     { {subj,   pat,  "",  1},   {"cdef",  1, 1} },
169     { {subj,   pat,  "",  2},   {"cd",    2, 2} },
170     { {subj,   pat,  "",  3},   {"cd",    2, 2} },
171     { {subj,   pat,  ""    },   {"cd",    2, 2} },
172     { {subj,   pat,  "#", 0},   {subj,    0, 0} }, -- test "n" + non-empty_replace
173     { {subj,   pat,  "#", 1},   {"#cdef", 1, 1} },
174     { {subj,   pat,  "#", 2},   {"#cd#",  2, 2} },
175     { {subj,   pat,  "#", 3},   {"#cd#",  2, 2} },
176     { {subj,   pat,  "#"   },   {"#cd#",  2, 2} },
177     { {"abc",  "^.", "#"   },   {"#bc",   1, 1} }, -- anchored pattern
178   }
179 end
180
181 local function set_f_gsub2 (lib, flg)
182   local subj, pat = "abc", "([ac])"
183   return {
184     Name = "Function gsub, set2",
185     Func = get_gsub (lib),
186   --{ s,     p,   f,   n,     res1,    res2, res3 },
187     { {subj, pat, "<%1>" },   {"<a>b<c>", 2, 2} }, -- test non-escaped chars in f
188     { {subj, pat, "%<%1%>" }, {"<a>b<c>", 2, 2} }, -- test escaped chars in f
189     { {subj, pat, "" },       {"b",       2, 2} }, -- test empty replace
190     { {subj, pat, "1" },      {"1b1",     2, 2} }, -- test odd and even %'s in f
191     { {subj, pat, "%1" },     {"abc",     2, 2} },
192     { {subj, pat, "%%1" },    {"%1b%1",   2, 2} },
193     { {subj, pat, "%%%1" },   {"%ab%c",   2, 2} },
194     { {subj, pat, "%%%%1" },  {"%%1b%%1", 2, 2} },
195     { {subj, pat, "%%%%%1" }, {"%%ab%%c", 2, 2} },
196   }
197 end
198
199 local function set_f_gsub3 (lib, flg)
200   return {
201     Name = "Function gsub, set3",
202     Func = get_gsub (lib),
203   --{ s,      p,      f,  n,   res1,res2,res3 },
204     { {"abc", "a",    "%0" }, {"abc", 1, 1} }, -- test (in)valid capture index
205     { {"abc", "a",    "%1" }, {"abc", 1, 1} },
206     { {"abc", "[ac]", "%1" }, {"abc", 2, 2} },
207     { {"abc", "(a)",  "%1" }, {"abc", 1, 1} },
208     { {"abc", "(a)",  "%2" }, "invalid capture index" },
209   }
210 end
211
212 local function set_f_gsub4 (lib, flg)
213   return {
214     Name = "Function gsub, set4",
215     Func = get_gsub (lib),
216   --{ s,           p,              f, n,  res1,      res2, res3 },
217     { {"a2c3",     ".",            "#" }, {"####",      4, 4} }, -- test .
218     { {"a2c3",     ".+",           "#" }, {"#",         1, 1} }, -- test .+
219     { {"a2c3",     ".*",           "#" }, {"##",        2, 2} }, -- test .*
220     { {"/* */ */", "\\/\\*(.*)\\*\\/", "#" }, {"#",     1, 1} },
221     { {"a2c3",     "[0-9]",        "#" }, {"a#c#",      2, 2} }, -- test %d
222     { {"a2c3",     "[^0-9]",       "#" }, {"#2#3",      2, 2} }, -- test %D
223     { {"a \t\nb",  "[ \t\n]",      "#" }, {"a###b",     3, 3} }, -- test %s
224     { {"a \t\nb",  "[^ \t\n]",     "#" }, {"# \t\n#",   2, 2} }, -- test %S
225   }
226 end
227
228 local function set_f_gsub5 (lib, flg)
229   local function frep1 () end                       -- returns nothing
230   local function frep2 () return "#" end            -- ignores arguments
231   local function frep3 (...) return table.concat({...}, ",") end -- "normal"
232   local function frep4 () return {} end             -- invalid return type
233   local function frep5 () return "7", "a" end       -- 2-nd return is "a"
234   local function frep6 () return "7", "break" end   -- 2-nd return is "break"
235   local subj = "a2c3"
236   return {
237     Name = "Function gsub, set5",
238     Func = get_gsub (lib),
239   --{ s,     p,          f,   n,   res1,     res2, res3 },
240     { {subj, "a(.)c(.)", frep1 }, {subj,        1, 0} },
241     { {subj, "a(.)c(.)", frep2 }, {"#",         1, 1} },
242     { {subj, "a(.)c(.)", frep3 }, {"2,3",       1, 1} },
243     { {subj, "a.c.",     frep3 }, {subj,        1, 1} },
244     { {subj, "z*",       frep1 }, {subj,        5, 0} },
245     { {subj, "z*",       frep2 }, {"#a#2#c#3#", 5, 5} },
246     { {subj, "z*",       frep3 }, {subj,        5, 5} },
247     { {subj, subj,       frep4 }, "invalid return type" },
248     { {"abc",".",        frep5 }, {"777",       3, 3} },
249     { {"abc",".",        frep6 }, {"777",       3, 3} },
250   }
251 end
252
253 local function set_f_gsub6 (lib, flg)
254   local tab1, tab2, tab3 = {}, { ["2"] = 56 }, { ["2"] = {} }
255   local subj = "a2c3"
256   return {
257     Name = "Function gsub, set6",
258     Func = get_gsub (lib),
259   --{ s,     p,          f, n,   res1,res2,res3 },
260     { {subj, "a(.)c(.)", tab1 }, {subj,  1, 0} },
261     { {subj, "a(.)c(.)", tab2 }, {"56",  1, 1} },
262     { {subj, "a(.)c(.)", tab3 }, "invalid replacement type" },
263     { {subj, "a.c.",     tab1 }, {subj,  1, 0} },
264     { {subj, "a.c.",     tab2 }, {subj,  1, 0} },
265     { {subj, "a.c.",     tab3 }, {subj,  1, 0} },
266   }
267 end
268
269 local function set_f_gsub8 (lib, flg)
270   local subj, patt, repl = "abcdef", "..", "*"
271   return {
272     Name = "Function gsub, set8",
273     Func = get_gsub (lib),
274   --{ s,     p,       f, n,                                    res1,  res2, res3 },
275     { {subj, patt, repl, function() end },                    {"abcdef", 3, 0} },
276     { {subj, patt, repl, function() return nil end },         {"abcdef", 3, 0} },
277     { {subj, patt, repl, function() return false end },       {"abcdef", 3, 0} },
278     { {subj, patt, repl, function() return true end },        {"***",    3, 3} },
279     { {subj, patt, repl, function() return {} end },          {"***",    3, 3} },
280     { {subj, patt, repl, function() return "#" end },         {"###",    3, 3} },
281     { {subj, patt, repl, function() return 57 end },          {"575757", 3, 3} },
282     { {subj, patt, repl, function (from) return from end },   {"135",    3, 3} },
283     { {subj, patt, repl, function (from, to) return to end }, {"246",    3, 3} },
284     { {subj, patt, repl, function (from,to,rep) return rep end },
285                                                               {"***",    3, 3} },
286     { {subj, patt, repl, function (from, to, rep) return rep..to..from end },
287                                                            {"*21*43*65", 3, 3} },
288     { {subj, patt, repl, function() return nil end },         {"abcdef", 3, 0} },
289     { {subj, patt, repl, function() return nil, nil end },    {"abcdef", 3, 0} },
290     { {subj, patt, repl, function() return nil, false end },  {"abcdef", 3, 0} },
291     { {subj, patt, repl, function() return nil, true end },   {"ab**",   3, 2} },
292     { {subj, patt, repl, function() return true, true end },  {"***",    3, 3} },
293     { {subj, patt, repl, function() return nil, 0 end },      {"abcdef", 1, 0} },
294     { {subj, patt, repl, function() return true, 0 end },     {"*cdef",  1, 1} },
295     { {subj, patt, repl, function() return nil, 1 end },      {"ab*ef",  2, 1} },
296     { {subj, patt, repl, function() return true, 1 end },     {"**ef",   2, 2} },
297   }
298 end
299
300 return function (libname, isglobal)
301   local lib = isglobal and _G[libname] or require (libname)
302   return {
303     set_f_gmatch    (lib),
304     set_f_split     (lib),
305     set_f_find      (lib),
306     set_f_match     (lib),
307     set_m_exec      (lib),
308     set_m_tfind     (lib),
309     set_m_find      (lib),
310     set_m_match     (lib),
311     set_f_gsub1     (lib),
312     set_f_gsub2     (lib),
313     set_f_gsub3     (lib),
314     set_f_gsub4     (lib),
315     set_f_gsub5     (lib),
316     set_f_gsub6     (lib),
317     set_f_gsub8     (lib),
318   }
319 end