Mercurial > pub > dyncall > dyncall
comparison test/call_suite_aggrs/rand-sig.lua @ 494:d45c582b5457
- sharing sig generator between call_suite and call_suite_aggrs tests
author | Tassilo Philipp |
---|---|
date | Mon, 21 Mar 2022 16:06:16 +0100 |
parents | 0c68b3f91367 |
children |
comparison
equal
deleted
inserted
replaced
493:75cb8f79d725 | 494:d45c582b5457 |
---|---|
1 require"config" | 1 require"config" |
2 | 2 |
3 -- assure aggr chars are present in pairs (can be weighted, though), to avoid | 3 -- use shared sig generator |
4 -- inf loops; closing chars are allowe to appear alone, as they are ignored | 4 package.path = '../common/?.lua;' .. package.path |
5 -- without any opening char (does not make a lot of sense, though) | 5 require"rand-sig" |
6 pairs_op = { '{', '<' } --, '[' } | |
7 pairs_cl = { '}', '>' } --, ']' } | |
8 | 6 |
9 aggr_op_pattern = '[%'..table.concat(pairs_op,'%')..']' | |
10 | |
11 for i = 1, #pairs_op do | |
12 if string.find(types, '%'..pairs_op[i]) and not string.find(types, '%'..pairs_cl[i]) then | |
13 types = types..pairs_cl[i] | |
14 end | |
15 end | |
16 | |
17 rtypes = "v"..types | |
18 | |
19 function mkaggr(n_nest, maxdepth, o, c) | |
20 local s = o | |
21 local nfields = 0 | |
22 | |
23 repeat | |
24 local t = c | |
25 if nfields < maxaggrfields then | |
26 repeat | |
27 local id = math.random(#types) | |
28 t = types:sub(id,id) | |
29 until t ~= c or nfields >= minaggrfields | |
30 end | |
31 | |
32 s_ = mktype(t, n_nest, maxdepth, o) | |
33 if(#s_ > 0) then | |
34 nfields = nfields + 1 | |
35 end | |
36 s = s..s_ | |
37 | |
38 -- member (which cannot be first char) as array? Disallow multidimensional arrays | |
39 if #s > 1 and t ~= c and s:sub(-1) ~= ']' and math.random(arraydice) == 1 then | |
40 s = s..'['..math.random(maxarraylen)..']' | |
41 end | |
42 until t == c | |
43 | |
44 return s | |
45 end | |
46 | |
47 function mktype(t, n_nest, maxdepth, aggr_open) | |
48 -- aggregate opener? | |
49 local aggr_i = 0 | |
50 for i = 1, #pairs_op do | |
51 if pairs_op[i] == t then | |
52 aggr_i = i | |
53 break | |
54 end | |
55 end | |
56 | |
57 -- ignore new aggregates if above depth limit | |
58 if aggr_i ~= 0 and t == pairs_op[aggr_i] then | |
59 if n_nest < maxdepth then | |
60 return mkaggr(n_nest + 1, maxdepth, pairs_op[aggr_i], pairs_cl[aggr_i]) | |
61 else | |
62 return '' | |
63 end | |
64 end | |
65 | |
66 -- aggregate closer? | |
67 for i = 1, #pairs_cl do | |
68 if pairs_cl[i] == t then | |
69 aggr_i = i | |
70 break | |
71 end | |
72 end | |
73 | |
74 -- if closing char, without any open, ignore | |
75 if aggr_i ~= 0 and (aggr_open == nil or pairs_op[aggr_i] ~= aggr_open) then | |
76 return '' | |
77 end | |
78 | |
79 return t | |
80 end | |
81 | |
82 math.randomseed(seed) | |
83 local id | |
84 local uniq_sigs = { } | |
85 for i = 1, ncases do | |
86 local l = '' | |
87 repeat | |
88 local nargs = math.random(minargs,maxargs) | |
89 id = math.random(#rtypes) | |
90 local sig = { mktype(rtypes:sub(id,id), 0, math.random(maxaggrdepth), nil) } -- random depth avoids excessive nesting | |
91 for j = 1, nargs do | |
92 id = math.random(#types) | |
93 sig[#sig+1] = mktype(types:sub(id,id), 0, math.random(maxaggrdepth), nil) -- random depth avoids excessive nesting | |
94 end | |
95 l = table.concat(sig) | |
96 -- reject dupes, sigs without any aggregate (as this is about aggrs after all), and empty ones (if not wanted) | |
97 until string.match(l, aggr_op_pattern) ~= nil and uniq_sigs[l] == nil | |
98 uniq_sigs[l] = 1 | |
99 io.write(l.."\n") | |
100 end | |
101 |