annotate test/suite_aggrs/rand-sig.lua @ 462:653b65580cb4

suite_aggr: - added arrays (inside of structs/unions, only, as only way to pass/return them by value) - cleanups and pregen of source
author Tassilo Philipp
date Tue, 01 Feb 2022 21:44:18 +0100
parents 236015fdf7a8
children bd8f5da2c74b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
432
167faab0c0be first usable version of test suite for aggregates, handling only non-nested struct params, at the moment;
Tassilo Philipp
parents:
diff changeset
1 require"config"
167faab0c0be first usable version of test suite for aggregates, handling only non-nested struct params, at the moment;
Tassilo Philipp
parents:
diff changeset
2
167faab0c0be first usable version of test suite for aggregates, handling only non-nested struct params, at the moment;
Tassilo Philipp
parents:
diff changeset
3 -- assure aggr chars are present in pairs (can be weighted, though), to avoid
461
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
4 -- inf loops; closing chars are allowe to appear alone, as they are ignored
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
5 -- without any opening char (does not make a lot of sense, though)
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
6 pairs_op = { '{', '<' } --, '[' }
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
7 pairs_cl = { '}', '>' } --, ']' }
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
8
462
653b65580cb4 suite_aggr:
Tassilo Philipp
parents: 461
diff changeset
9 aggr_op_pattern = '[%'..table.concat(pairs_op,'%')..']'
653b65580cb4 suite_aggr:
Tassilo Philipp
parents: 461
diff changeset
10
461
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
11 for i = 1, #pairs_op do
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
12 if string.find(types, '%'..pairs_op[i]) and not string.find(types, '%'..pairs_cl[i]) then
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
13 types = types..pairs_cl[i]
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
14 end
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
15 end
432
167faab0c0be first usable version of test suite for aggregates, handling only non-nested struct params, at the moment;
Tassilo Philipp
parents:
diff changeset
16
167faab0c0be first usable version of test suite for aggregates, handling only non-nested struct params, at the moment;
Tassilo Philipp
parents:
diff changeset
17 rtypes = "v"..types
167faab0c0be first usable version of test suite for aggregates, handling only non-nested struct params, at the moment;
Tassilo Philipp
parents:
diff changeset
18
461
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
19 function mkaggr(n_nest, maxdepth, o, c)
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
20 local s = o
432
167faab0c0be first usable version of test suite for aggregates, handling only non-nested struct params, at the moment;
Tassilo Philipp
parents:
diff changeset
21
167faab0c0be first usable version of test suite for aggregates, handling only non-nested struct params, at the moment;
Tassilo Philipp
parents:
diff changeset
22 repeat
167faab0c0be first usable version of test suite for aggregates, handling only non-nested struct params, at the moment;
Tassilo Philipp
parents:
diff changeset
23 local id = math.random(#types)
167faab0c0be first usable version of test suite for aggregates, handling only non-nested struct params, at the moment;
Tassilo Philipp
parents:
diff changeset
24 local t = types:sub(id,id)
461
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
25 s = s..mktype(t, n_nest, maxdepth, o)
462
653b65580cb4 suite_aggr:
Tassilo Philipp
parents: 461
diff changeset
26
653b65580cb4 suite_aggr:
Tassilo Philipp
parents: 461
diff changeset
27 -- member (which cannot be first char) as array? Disallow multidimensional arrays
653b65580cb4 suite_aggr:
Tassilo Philipp
parents: 461
diff changeset
28 if #s > 1 and t ~= c and s:sub(-1) ~= ']' and math.random(arraydice) == 1 then
653b65580cb4 suite_aggr:
Tassilo Philipp
parents: 461
diff changeset
29 s = s..'['..math.random(maxarraylen)..']'
653b65580cb4 suite_aggr:
Tassilo Philipp
parents: 461
diff changeset
30 end
461
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
31 until t == c
432
167faab0c0be first usable version of test suite for aggregates, handling only non-nested struct params, at the moment;
Tassilo Philipp
parents:
diff changeset
32
167faab0c0be first usable version of test suite for aggregates, handling only non-nested struct params, at the moment;
Tassilo Philipp
parents:
diff changeset
33 return s
167faab0c0be first usable version of test suite for aggregates, handling only non-nested struct params, at the moment;
Tassilo Philipp
parents:
diff changeset
34 end
167faab0c0be first usable version of test suite for aggregates, handling only non-nested struct params, at the moment;
Tassilo Philipp
parents:
diff changeset
35
461
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
36 function mktype(t, n_nest, maxdepth, aggr_open)
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
37 -- aggregate opener?
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
38 local aggr_i = 0
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
39 for i = 1, #pairs_op do
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
40 if pairs_op[i] == t then
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
41 aggr_i = i
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
42 break
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
43 end
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
44 end
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
45
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
46 -- ignore new aggregates if above depth limit
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
47 if aggr_i ~= 0 and t == pairs_op[aggr_i] then
434
3d2c5d156d78 - test/suite_aggrs: support for nested structs, now
Tassilo Philipp
parents: 432
diff changeset
48 if n_nest < maxdepth then
461
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
49 return mkaggr(n_nest + 1, maxdepth, pairs_op[aggr_i], pairs_cl[aggr_i])
432
167faab0c0be first usable version of test suite for aggregates, handling only non-nested struct params, at the moment;
Tassilo Philipp
parents:
diff changeset
50 else
167faab0c0be first usable version of test suite for aggregates, handling only non-nested struct params, at the moment;
Tassilo Philipp
parents:
diff changeset
51 return ''
167faab0c0be first usable version of test suite for aggregates, handling only non-nested struct params, at the moment;
Tassilo Philipp
parents:
diff changeset
52 end
167faab0c0be first usable version of test suite for aggregates, handling only non-nested struct params, at the moment;
Tassilo Philipp
parents:
diff changeset
53 end
167faab0c0be first usable version of test suite for aggregates, handling only non-nested struct params, at the moment;
Tassilo Philipp
parents:
diff changeset
54
461
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
55 -- aggregate closer?
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
56 for i = 1, #pairs_cl do
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
57 if pairs_cl[i] == t then
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
58 aggr_i = i
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
59 break
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
60 end
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
61 end
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
62
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
63 -- if closing char, without any open, ignore
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
64 if aggr_i ~= 0 and (aggr_open == nil or pairs_op[aggr_i] ~= aggr_open) then
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
65 return ''
432
167faab0c0be first usable version of test suite for aggregates, handling only non-nested struct params, at the moment;
Tassilo Philipp
parents:
diff changeset
66 end
167faab0c0be first usable version of test suite for aggregates, handling only non-nested struct params, at the moment;
Tassilo Philipp
parents:
diff changeset
67
167faab0c0be first usable version of test suite for aggregates, handling only non-nested struct params, at the moment;
Tassilo Philipp
parents:
diff changeset
68 return t
167faab0c0be first usable version of test suite for aggregates, handling only non-nested struct params, at the moment;
Tassilo Philipp
parents:
diff changeset
69 end
167faab0c0be first usable version of test suite for aggregates, handling only non-nested struct params, at the moment;
Tassilo Philipp
parents:
diff changeset
70
461
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
71 function contains_empty_aggr(s)
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
72 for i = 1, #pairs_op do
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
73 if string.match(s, '%'..pairs_op[i]..'%'..pairs_cl[i]) ~= nil then
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
74 return true
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
75 end
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
76 end
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
77 return false
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
78 end
432
167faab0c0be first usable version of test suite for aggregates, handling only non-nested struct params, at the moment;
Tassilo Philipp
parents:
diff changeset
79
167faab0c0be first usable version of test suite for aggregates, handling only non-nested struct params, at the moment;
Tassilo Philipp
parents:
diff changeset
80 math.randomseed(seed)
167faab0c0be first usable version of test suite for aggregates, handling only non-nested struct params, at the moment;
Tassilo Philipp
parents:
diff changeset
81 local id
438
b4ddad459690 suite_aggr;
Tassilo Philipp
parents: 434
diff changeset
82 local uniq_sigs = { }
432
167faab0c0be first usable version of test suite for aggregates, handling only non-nested struct params, at the moment;
Tassilo Philipp
parents:
diff changeset
83 for i = 1, ncases do
434
3d2c5d156d78 - test/suite_aggrs: support for nested structs, now
Tassilo Philipp
parents: 432
diff changeset
84 local l = ''
3d2c5d156d78 - test/suite_aggrs: support for nested structs, now
Tassilo Philipp
parents: 432
diff changeset
85 repeat
461
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
86 local nargs = math.random(minargs,maxargs)
434
3d2c5d156d78 - test/suite_aggrs: support for nested structs, now
Tassilo Philipp
parents: 432
diff changeset
87 id = math.random(#rtypes)
461
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
88 local sig = { mktype(rtypes:sub(id,id), 0, math.random(maxaggrdepth), nil) } -- random depth avoids excessive nesting
434
3d2c5d156d78 - test/suite_aggrs: support for nested structs, now
Tassilo Philipp
parents: 432
diff changeset
89 for j = 1, nargs do
3d2c5d156d78 - test/suite_aggrs: support for nested structs, now
Tassilo Philipp
parents: 432
diff changeset
90 id = math.random(#types)
461
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
91 sig[#sig+1] = mktype(types:sub(id,id), 0, math.random(maxaggrdepth), nil) -- random depth avoids excessive nesting
434
3d2c5d156d78 - test/suite_aggrs: support for nested structs, now
Tassilo Philipp
parents: 432
diff changeset
92 end
438
b4ddad459690 suite_aggr;
Tassilo Philipp
parents: 434
diff changeset
93 l = table.concat(sig)
446
54c1dc2e6ea5 suite_aggrs: added knob to enable/disable generation of empty structs (some compilers like Plan9's pcc don't allow them)
Tassilo Philipp
parents: 438
diff changeset
94 -- reject dupes, sigs without any aggregate (as this is about aggrs after all), and empty ones (if not wanted)
461
236015fdf7a8 suite_aggrs:
Tassilo Philipp
parents: 446
diff changeset
95 until string.match(l, aggr_op_pattern) ~= nil and uniq_sigs[l] == nil and (emptyaggrs or not contains_empty_aggr(l))
438
b4ddad459690 suite_aggr;
Tassilo Philipp
parents: 434
diff changeset
96 uniq_sigs[l] = 1
434
3d2c5d156d78 - test/suite_aggrs: support for nested structs, now
Tassilo Philipp
parents: 432
diff changeset
97 io.write(l.."\n")
432
167faab0c0be first usable version of test suite for aggregates, handling only non-nested struct params, at the moment;
Tassilo Philipp
parents:
diff changeset
98 end
167faab0c0be first usable version of test suite for aggregates, handling only non-nested struct params, at the moment;
Tassilo Philipp
parents:
diff changeset
99