Generation of strings.
More...
Generation of strings.
Its instances have the properties:
Regex
Regex syntax compatible with testlib's regex.
Operations:
- A single character yields itself (a, 3).
- A list of characters inside square braces yields any a random element from the list ([abc123]).
- A range of characters inside [] is equivalent to listing them ([a-z1-9A-Z]).
- A pattern followed by {n} yields the pattern repeated n times (a{3}).
- A pattern followed by {l,r} yields the pattern repeated between l and r times (a{3,5}).
- A list of patterns separated by | yields a random pattern from the list (abc|def|ghi).
- Parentheses can be used for grouping (a((a|b){3})).
Operations defined by {n} and {l,r} are applied to what comes before it: a character, a group defined by [] or (), or {n}/{l,r} expressions.
Regex generation is uniform (each string has the same probability of being generated), as long as each generated string can be generated uniquely from the regex. Otherwise, the probability of each string being generated is proportional to the number of ways it cab be generated from the regex.
Examples:
- str("0 | [1-9][0-9]{0,2} | 1000") generates random numbers in [0, 1000].
- str("a[b-d]{2}|e") generates e or a random string of length 3, with the first character being a and the second and third characters being b, c, or d.
- str("[1-9][0-9]{%d}", n-1) generates n-digit numbers.
Examples
Prints a random string of 20 lower ou uppercase letters.
instance gen() const
Generates a random instance from the set of valid strings.
Prints 2 uniformly random numbers in [0, 10^n].
auto leq1eN =
tgen::str(
"0 | [1-9][0-9]{0,%d} | 10{%d}", n-1, n);
std::cout << leq1eN.gen() << " " << leq1eN.gen() << std::endl;