Mercurial > pub > dyncall > dyncall
comparison 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 |
comparison
equal
deleted
inserted
replaced
624:d28b09fa2ff9 | 625:3fc677ff16e5 |
---|---|
7 -- types | 7 -- types |
8 -- seed | 8 -- seed |
9 | 9 |
10 -- optional: | 10 -- optional: |
11 -- rtypes (if not set, it'll be 'v'..types) | 11 -- rtypes (if not set, it'll be 'v'..types) |
12 -- ellipsis | |
12 | 13 |
13 -- optional (when including aggregate generation): | 14 -- optional (when including aggregate generation): |
14 -- minaggrfields | 15 -- minaggrfields |
15 -- maxaggrfields | 16 -- maxaggrfields |
16 -- maxarraylen | 17 -- maxarraylen |
55 local id = math.random(#types) | 56 local id = math.random(#types) |
56 t = types:sub(id,id) | 57 t = types:sub(id,id) |
57 until t ~= c or nfields >= minaggrfields | 58 until t ~= c or nfields >= minaggrfields |
58 end | 59 end |
59 | 60 |
60 s_ = mktype(t, n_nest, maxdepth, o) | 61 s_ = mktype(t, n_nest, maxdepth, o) or '' |
61 if(#s_ > 0) then | 62 if(#s_ > 0) then |
62 nfields = nfields + 1 | 63 nfields = nfields + 1 |
63 end | 64 end |
64 s = s..s_ | 65 s = s..s_ |
65 | 66 |
85 -- ignore new aggregates if above depth limit | 86 -- ignore new aggregates if above depth limit |
86 if aggr_i ~= 0 and t == pairs_op[aggr_i] then | 87 if aggr_i ~= 0 and t == pairs_op[aggr_i] then |
87 if n_nest < maxdepth then | 88 if n_nest < maxdepth then |
88 return mkaggr(n_nest + 1, maxdepth, pairs_op[aggr_i], pairs_cl[aggr_i]) | 89 return mkaggr(n_nest + 1, maxdepth, pairs_op[aggr_i], pairs_cl[aggr_i]) |
89 else | 90 else |
90 return '' | 91 return nil |
91 end | 92 end |
92 end | 93 end |
93 | 94 |
94 -- aggregate closer? | 95 -- aggregate closer? |
95 for i = 1, #pairs_cl do | 96 for i = 1, #pairs_cl do |
99 end | 100 end |
100 end | 101 end |
101 | 102 |
102 -- if closing char, without any open, ignore | 103 -- if closing char, without any open, ignore |
103 if aggr_i ~= 0 and (aggr_open == nil or pairs_op[aggr_i] ~= aggr_open) then | 104 if aggr_i ~= 0 and (aggr_open == nil or pairs_op[aggr_i] ~= aggr_open) then |
104 return '' | 105 return nil |
105 end | 106 end |
106 | 107 |
107 return t | 108 return t |
108 end | 109 end |
109 | 110 |
115 local uniq_sigs = { } | 116 local uniq_sigs = { } |
116 for i = 1, ncases do | 117 for i = 1, ncases do |
117 local l = '' | 118 local l = '' |
118 repeat | 119 repeat |
119 local nargs = math.random(minargs,maxargs) | 120 local nargs = math.random(minargs,maxargs) |
121 local varargstart = (ellipsis and nargs > 0 and math.random(0,ellipsis) == 0) and math.random(0,nargs-1) or nargs -- generate vararg sigs? | |
120 local sig = { } | 122 local sig = { } |
121 for j = 1, nargs do | 123 for j = 1, nargs do |
122 id = math.random(#types) | 124 id = math.random(#types) |
123 sig[#sig+1] = mktype(types:sub(id,id), 0, math.random(maxaggrdepth), nil) -- random depth avoids excessive nesting | 125 sig[#sig+1] = mktype(types:sub(id,id), 0, math.random(maxaggrdepth), nil) -- random depth avoids excessive nesting |
126 -- start vararg part? | |
127 if j > varargstart and #sig > 0 then | |
128 sig[#sig+1] = "." | |
129 varargstart = nargs | |
130 end | |
124 end | 131 end |
125 r = '' | |
126 repeat | 132 repeat |
127 id = math.random(#rtypes) | 133 id = math.random(#rtypes) |
128 r = mktype(rtypes:sub(id,id), 0, math.random(maxaggrdepth), nil) -- random depth avoids excessive nesting | 134 r = mktype(rtypes:sub(id,id), 0, math.random(maxaggrdepth), nil) -- random depth avoids excessive nesting |
129 until r ~= '' | 135 until r |
130 sig[#sig+1] = ')'..r | 136 sig[#sig+1] = ')'..r |
131 l = table.concat(sig) | 137 l = table.concat(sig) |
132 -- reject dupes, sigs without any aggregate (as this is about aggrs after all), and empty ones (if not wanted) | 138 -- reject dupes, sigs without any aggregate (as this is about aggrs after all), and empty ones (if not wanted) |
133 until (reqaggrinsig ~= true or string.match(l, aggr_op_pattern) ~= nil) and uniq_sigs[l] == nil | 139 until (reqaggrinsig ~= true or string.match(l, aggr_op_pattern) ~= nil) and uniq_sigs[l] == nil |
134 uniq_sigs[l] = 1 | 140 uniq_sigs[l] = 1 |