Mercurial > pub > dyncall > dyncall
comparison test/suite_aggrs/rand-sig.lua @ 461:236015fdf7a8
suite_aggrs:
- added support to gen unions in addition to structs
- regenerated struct/union-mixed and nested cases
- made rand-sig.lua ignore closing struct/union chars if not opened, effectively reducing number of empty aggregates as it now generated way too much
author | Tassilo Philipp |
---|---|
date | Mon, 31 Jan 2022 14:41:11 +0100 |
parents | 54c1dc2e6ea5 |
children | 653b65580cb4 |
comparison
equal
deleted
inserted
replaced
460:0ae555528709 | 461:236015fdf7a8 |
---|---|
1 require"config" | 1 require"config" |
2 | 2 |
3 -- assure aggr chars are present in pairs (can be weighted, though), to avoid | 3 -- assure aggr chars are present in pairs (can be weighted, though), to avoid |
4 -- inf loops | 4 -- inf loops; closing chars are allowe to appear alone, as they are ignored |
5 if string.match(types,'{') and not string.match(types,'}') then types = types..'}' end | 5 -- without any opening char (does not make a lot of sense, though) |
6 pairs_op = { '{', '<' } --, '[' } | |
7 pairs_cl = { '}', '>' } --, ']' } | |
8 | |
9 for i = 1, #pairs_op do | |
10 if string.find(types, '%'..pairs_op[i]) and not string.find(types, '%'..pairs_cl[i]) then | |
11 types = types..pairs_cl[i] | |
12 end | |
13 end | |
6 | 14 |
7 rtypes = "v"..types | 15 rtypes = "v"..types |
8 | 16 |
9 | 17 function mkaggr(n_nest, maxdepth, o, c) |
10 function mkstruct(n_nest, maxdepth) | 18 local s = o |
11 local s = "{" | |
12 | 19 |
13 repeat | 20 repeat |
14 local id = math.random(#types) | 21 local id = math.random(#types) |
15 local t = types:sub(id,id) | 22 local t = types:sub(id,id) |
16 s = s..mktype(t, n_nest, maxdepth) | 23 s = s..mktype(t, n_nest, maxdepth, o) |
17 until t == '}' | 24 until t == c |
18 | 25 |
19 return s | 26 return s |
20 end | 27 end |
21 | 28 |
22 function mktype(t, n_nest, maxdepth) | 29 function mktype(t, n_nest, maxdepth, aggr_open) |
23 -- ignore new structs if above depth limit | 30 -- aggregate opener? |
24 if t == '{' then | 31 local aggr_i = 0 |
32 for i = 1, #pairs_op do | |
33 if pairs_op[i] == t then | |
34 aggr_i = i | |
35 break | |
36 end | |
37 end | |
38 | |
39 -- ignore new aggregates if above depth limit | |
40 if aggr_i ~= 0 and t == pairs_op[aggr_i] then | |
25 if n_nest < maxdepth then | 41 if n_nest < maxdepth then |
26 return mkstruct(n_nest + 1, maxdepth) | 42 return mkaggr(n_nest + 1, maxdepth, pairs_op[aggr_i], pairs_cl[aggr_i]) |
27 else | 43 else |
28 return '' | 44 return '' |
29 end | 45 end |
30 end | 46 end |
31 | 47 |
32 -- if '}', without any open, use empty struct | 48 -- aggregate closer? |
33 if n_nest == 0 and t == '}' then | 49 for i = 1, #pairs_cl do |
34 return "{}" | 50 if pairs_cl[i] == t then |
51 aggr_i = i | |
52 break | |
53 end | |
54 end | |
55 | |
56 -- if closing char, without any open, ignore | |
57 if aggr_i ~= 0 and (aggr_open == nil or pairs_op[aggr_i] ~= aggr_open) then | |
58 return '' | |
35 end | 59 end |
36 | 60 |
37 return t | 61 return t |
38 end | 62 end |
39 | 63 |
64 function contains_empty_aggr(s) | |
65 for i = 1, #pairs_op do | |
66 if string.match(s, '%'..pairs_op[i]..'%'..pairs_cl[i]) ~= nil then | |
67 return true | |
68 end | |
69 end | |
70 return false | |
71 end | |
40 | 72 |
41 math.randomseed(seed) | 73 math.randomseed(seed) |
42 local id | 74 local id |
43 local uniq_sigs = { } | 75 local uniq_sigs = { } |
76 local aggr_op_pattern = '[%'..table.concat(pairs_op,'%')..']' | |
44 for i = 1, ncases do | 77 for i = 1, ncases do |
45 local nargs = math.random(minargs,maxargs) | |
46 local l = '' | 78 local l = '' |
47 repeat | 79 repeat |
80 local nargs = math.random(minargs,maxargs) | |
48 id = math.random(#rtypes) | 81 id = math.random(#rtypes) |
49 local sig = { mktype(rtypes:sub(id,id), 0, math.random(maxaggrdepth)) } -- random depth avoids excessive nesting | 82 local sig = { mktype(rtypes:sub(id,id), 0, math.random(maxaggrdepth), nil) } -- random depth avoids excessive nesting |
50 for j = 1, nargs do | 83 for j = 1, nargs do |
51 id = math.random(#types) | 84 id = math.random(#types) |
52 sig[#sig+1] = mktype(types:sub(id,id), 0, math.random(maxaggrdepth)) -- random depth avoids excessive nesting | 85 sig[#sig+1] = mktype(types:sub(id,id), 0, math.random(maxaggrdepth), nil) -- random depth avoids excessive nesting |
53 end | 86 end |
54 l = table.concat(sig) | 87 l = table.concat(sig) |
55 -- reject dupes, sigs without any aggregate (as this is about aggrs after all), and empty ones (if not wanted) | 88 -- reject dupes, sigs without any aggregate (as this is about aggrs after all), and empty ones (if not wanted) |
56 until string.match(l, '{') ~= nil and uniq_sigs[l] == nil and (emptyaggrs or string.match(l, '{}') == nil) | 89 until string.match(l, aggr_op_pattern) ~= nil and uniq_sigs[l] == nil and (emptyaggrs or not contains_empty_aggr(l)) |
57 uniq_sigs[l] = 1 | 90 uniq_sigs[l] = 1 |
58 io.write(l.."\n") | 91 io.write(l.."\n") |
59 end | 92 end |
60 | 93 |