tgen
Loading...
Searching...
No Matches
Strings

Generation of strings. More...

Classes

struct  tgen::str
 String generator. More...
struct  tgen::str::instance
 String instance. More...

Detailed Description

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:

  1. str("0 | [1-9][0-9]{0,2} | 1000") generates random numbers in [0, 1000].
  2. 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.
  3. str("[1-9][0-9]{%d}", n-1) generates n-digit numbers.

Examples

Prints a random string of 20 lower ou uppercase letters.

// Prints a random string of 20 lower and uppercase letters.
std::cout << tgen::str(20, "[a-zA-Z]").gen() << std::endl;
// "OewztXVHjOvOTZwYWRrf"
String generator.
Definition tgen.h:3055
instance gen() const
Generates a random instance from the set of valid strings.
Definition tgen.h:3214

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;
// "843 647", for n = 3