vuls: init at 0.27.0 (#348530)
[NixPkgs.git] / lib / path / tests / generate.awk
blob811dd0c46d33232db14d3b2a700b69e7541b625a
1 # Generate random path-like strings, separated by null characters.
3 # Invocation:
5 # awk -f ./generate.awk -v <variable>=<value> | tr '\0' '\n'
7 # Customizable variables (all default to 0):
8 # - seed: Deterministic random seed to use for generation
9 # - count: Number of paths to generate
10 # - extradotweight: Give extra weight to dots being generated
11 # - extraslashweight: Give extra weight to slashes being generated
12 # - extranullweight: Give extra weight to null being generated, making paths shorter
13 BEGIN {
14 # Random seed, passed explicitly for reproducibility
15 srand(seed)
17 # Don't include special characters below 32
18 minascii = 32
19 # Don't include DEL at 128
20 maxascii = 127
21 upperascii = maxascii - minascii
23 # add extra weight for ., in addition to the one weight from the ascii range
24 upperdot = upperascii + extradotweight
26 # add extra weight for /, in addition to the one weight from the ascii range
27 upperslash = upperdot + extraslashweight
29 # add extra weight for null, indicating the end of the string
30 # Must be at least 1 to have strings end at all
31 total = upperslash + 1 + extranullweight
33 # new=1 indicates that it's a new string
34 new=1
35 while (count > 0) {
37 # Random integer between [0, total)
38 value = int(rand() * total)
40 if (value < upperascii) {
41 # Ascii range
42 printf("%c", value + minascii)
43 new=0
45 } else if (value < upperdot) {
46 # Dot range
47 printf "."
48 new=0
50 } else if (value < upperslash) {
51 # If it's the start of a new path, only generate a / in 10% of cases
52 # This is always an invalid subpath, which is not a very interesting case
53 if (new && rand() > 0.1) continue
54 printf "/"
56 } else {
57 # Do not generate empty strings
58 if (new) continue
59 printf "\x00"
60 count--
61 new=1