annotate test/common/rand-sig.lua @ 625:3fc677ff16e5

- lua signature generator for test code: support for generating '.' vararg sigs
author Tassilo Philipp
date Fri, 28 Oct 2022 13:53:11 +0200
parents da5232da6270
children 667fe7b2be62
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
494
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
1 -- following knobs control generation:
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
2
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
3 -- required to be defined by who is using this:
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
4 -- minargs
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
5 -- maxargs
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
6 -- ncases
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
7 -- types
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
8 -- seed
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
9
495
45ac093ca822 - test/callback_suite: make it use shared random sig generator from call_suite and call_suite_aggrs
Tassilo Philipp
parents: 494
diff changeset
10 -- optional:
45ac093ca822 - test/callback_suite: make it use shared random sig generator from call_suite and call_suite_aggrs
Tassilo Philipp
parents: 494
diff changeset
11 -- rtypes (if not set, it'll be 'v'..types)
625
3fc677ff16e5 - lua signature generator for test code: support for generating '.' vararg sigs
Tassilo Philipp
parents: 496
diff changeset
12 -- ellipsis
495
45ac093ca822 - test/callback_suite: make it use shared random sig generator from call_suite and call_suite_aggrs
Tassilo Philipp
parents: 494
diff changeset
13
494
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
14 -- optional (when including aggregate generation):
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
15 -- minaggrfields
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
16 -- maxaggrfields
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
17 -- maxarraylen
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
18 -- arraydice
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
19 -- maxaggrdepth
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
20 -- reqaggrinsig
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
21
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
22
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
23 --------------------------------
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
24
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
25 if maxaggrdepth == nil then
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
26 maxaggrdepth = 3
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
27 end
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
28
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
29
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
30 -- assure aggr chars are present in pairs (can be weighted, though), to avoid
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
31 -- inf loops; closing chars are allowed to appear alone, as they are ignored
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
32 -- without any opening char (does not make a lot of sense, though)
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
33 pairs_op = { '{', '<' } --, '[' }
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
34 pairs_cl = { '}', '>' } --, ']' }
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
35
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
36 for i = 1, #pairs_op do
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
37 if string.find(types, '%'..pairs_op[i]) and not string.find(types, '%'..pairs_cl[i]) then
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
38 types = types..pairs_cl[i]
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
39 end
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
40 end
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
41
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
42
495
45ac093ca822 - test/callback_suite: make it use shared random sig generator from call_suite and call_suite_aggrs
Tassilo Philipp
parents: 494
diff changeset
43 if rtypes == nil then
45ac093ca822 - test/callback_suite: make it use shared random sig generator from call_suite and call_suite_aggrs
Tassilo Philipp
parents: 494
diff changeset
44 rtypes = "v"..types
45ac093ca822 - test/callback_suite: make it use shared random sig generator from call_suite and call_suite_aggrs
Tassilo Philipp
parents: 494
diff changeset
45 end
45ac093ca822 - test/callback_suite: make it use shared random sig generator from call_suite and call_suite_aggrs
Tassilo Philipp
parents: 494
diff changeset
46
494
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
47
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
48 function mkaggr(n_nest, maxdepth, o, c)
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
49 local s = o
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
50 local nfields = 0
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
51
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
52 repeat
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
53 local t = c
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
54 if nfields < maxaggrfields then
495
45ac093ca822 - test/callback_suite: make it use shared random sig generator from call_suite and call_suite_aggrs
Tassilo Philipp
parents: 494
diff changeset
55 repeat
494
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
56 local id = math.random(#types)
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
57 t = types:sub(id,id)
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
58 until t ~= c or nfields >= minaggrfields
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
59 end
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
60
625
3fc677ff16e5 - lua signature generator for test code: support for generating '.' vararg sigs
Tassilo Philipp
parents: 496
diff changeset
61 s_ = mktype(t, n_nest, maxdepth, o) or ''
495
45ac093ca822 - test/callback_suite: make it use shared random sig generator from call_suite and call_suite_aggrs
Tassilo Philipp
parents: 494
diff changeset
62 if(#s_ > 0) then
494
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
63 nfields = nfields + 1
495
45ac093ca822 - test/callback_suite: make it use shared random sig generator from call_suite and call_suite_aggrs
Tassilo Philipp
parents: 494
diff changeset
64 end
494
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
65 s = s..s_
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
66
496
da5232da6270 - test code: rand-sig now spitting out dyncallish sigs (with ')' end delim and rettype at end), purely for consistency
Tassilo Philipp
parents: 495
diff changeset
67 -- member (which cannot be first char) as array? Disallow multidimensional arrays
494
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
68 if #s > 1 and t ~= c and s:sub(-1) ~= ']' and math.random(arraydice) == 1 then
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
69 s = s..'['..math.random(maxarraylen)..']'
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
70 end
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
71 until t == c
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
72
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
73 return s
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
74 end
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
75
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
76 function mktype(t, n_nest, maxdepth, aggr_open)
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
77 -- aggregate opener?
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
78 local aggr_i = 0
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
79 for i = 1, #pairs_op do
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
80 if pairs_op[i] == t then
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
81 aggr_i = i
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
82 break
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
83 end
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
84 end
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
85
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
86 -- ignore new aggregates if above depth limit
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
87 if aggr_i ~= 0 and t == pairs_op[aggr_i] then
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
88 if n_nest < maxdepth then
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
89 return mkaggr(n_nest + 1, maxdepth, pairs_op[aggr_i], pairs_cl[aggr_i])
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
90 else
625
3fc677ff16e5 - lua signature generator for test code: support for generating '.' vararg sigs
Tassilo Philipp
parents: 496
diff changeset
91 return nil
494
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
92 end
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
93 end
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
94
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
95 -- aggregate closer?
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
96 for i = 1, #pairs_cl do
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
97 if pairs_cl[i] == t then
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
98 aggr_i = i
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
99 break
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
100 end
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
101 end
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
102
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
103 -- if closing char, without any open, ignore
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
104 if aggr_i ~= 0 and (aggr_open == nil or pairs_op[aggr_i] ~= aggr_open) then
625
3fc677ff16e5 - lua signature generator for test code: support for generating '.' vararg sigs
Tassilo Philipp
parents: 496
diff changeset
105 return nil
494
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
106 end
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
107
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
108 return t
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
109 end
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
110
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
111 -- pattern matching aggregate start chars
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
112 local aggr_op_pattern = '[%'..table.concat(pairs_op,'%')..']'
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
113
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
114 math.randomseed(seed)
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
115 local id
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
116 local uniq_sigs = { }
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
117 for i = 1, ncases do
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
118 local l = ''
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
119 repeat
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
120 local nargs = math.random(minargs,maxargs)
625
3fc677ff16e5 - lua signature generator for test code: support for generating '.' vararg sigs
Tassilo Philipp
parents: 496
diff changeset
121 local varargstart = (ellipsis and nargs > 0 and math.random(0,ellipsis) == 0) and math.random(0,nargs-1) or nargs -- generate vararg sigs?
496
da5232da6270 - test code: rand-sig now spitting out dyncallish sigs (with ')' end delim and rettype at end), purely for consistency
Tassilo Philipp
parents: 495
diff changeset
122 local sig = { }
494
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
123 for j = 1, nargs do
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
124 id = math.random(#types)
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
125 sig[#sig+1] = mktype(types:sub(id,id), 0, math.random(maxaggrdepth), nil) -- random depth avoids excessive nesting
625
3fc677ff16e5 - lua signature generator for test code: support for generating '.' vararg sigs
Tassilo Philipp
parents: 496
diff changeset
126 -- start vararg part?
3fc677ff16e5 - lua signature generator for test code: support for generating '.' vararg sigs
Tassilo Philipp
parents: 496
diff changeset
127 if j > varargstart and #sig > 0 then
3fc677ff16e5 - lua signature generator for test code: support for generating '.' vararg sigs
Tassilo Philipp
parents: 496
diff changeset
128 sig[#sig+1] = "."
3fc677ff16e5 - lua signature generator for test code: support for generating '.' vararg sigs
Tassilo Philipp
parents: 496
diff changeset
129 varargstart = nargs
3fc677ff16e5 - lua signature generator for test code: support for generating '.' vararg sigs
Tassilo Philipp
parents: 496
diff changeset
130 end
494
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
131 end
496
da5232da6270 - test code: rand-sig now spitting out dyncallish sigs (with ')' end delim and rettype at end), purely for consistency
Tassilo Philipp
parents: 495
diff changeset
132 repeat
da5232da6270 - test code: rand-sig now spitting out dyncallish sigs (with ')' end delim and rettype at end), purely for consistency
Tassilo Philipp
parents: 495
diff changeset
133 id = math.random(#rtypes)
da5232da6270 - test code: rand-sig now spitting out dyncallish sigs (with ')' end delim and rettype at end), purely for consistency
Tassilo Philipp
parents: 495
diff changeset
134 r = mktype(rtypes:sub(id,id), 0, math.random(maxaggrdepth), nil) -- random depth avoids excessive nesting
625
3fc677ff16e5 - lua signature generator for test code: support for generating '.' vararg sigs
Tassilo Philipp
parents: 496
diff changeset
135 until r
496
da5232da6270 - test code: rand-sig now spitting out dyncallish sigs (with ')' end delim and rettype at end), purely for consistency
Tassilo Philipp
parents: 495
diff changeset
136 sig[#sig+1] = ')'..r
494
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
137 l = table.concat(sig)
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
138 -- reject dupes, sigs without any aggregate (as this is about aggrs after all), and empty ones (if not wanted)
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
139 until (reqaggrinsig ~= true or string.match(l, aggr_op_pattern) ~= nil) and uniq_sigs[l] == nil
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
140 uniq_sigs[l] = 1
495
45ac093ca822 - test/callback_suite: make it use shared random sig generator from call_suite and call_suite_aggrs
Tassilo Philipp
parents: 494
diff changeset
141
496
da5232da6270 - test code: rand-sig now spitting out dyncallish sigs (with ')' end delim and rettype at end), purely for consistency
Tassilo Philipp
parents: 495
diff changeset
142 io.write(l.."\n")
494
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
143 end
d45c582b5457 - sharing sig generator between call_suite and call_suite_aggrs tests
Tassilo Philipp
parents:
diff changeset
144