Testcase generation for random inputs.
Overview
tgen is a C++ library to help you generate random stuff, useful for testcase generation (such as jngen or testlib). The code is in a single file tgen.h, that should be added to your directory.
The first thing is to register the generator. That defines the seed for random generation and parses the opts.
There are:
and operations for specific data types:
Type generators and values
All data types specified above define a generator, that when called upon will generate a uniformly random value with the given constraints. Let's see an example with tgen::list:
This will create a list generator representing the set of all lists with 10 values from 1 to 100.
Every generator of type Gen has a method gen(), that returns a Gen::value representing an element chosen uniformly at random from the set of all valid elements from the current state of the generator. A Gen::value can be fed to std::cout to be printed.
In our example, we can call gen() to generate and print a random list of 10 elements from 1 to 100.
std::cout << t.
gen() << std::endl;
value gen() const
Generates a random value from the set of valid lists.
The nice thing is that we can add restrictions (specific to each type) to the generator, shrinking the set of valid arrays. For example, we can add the restriction that the first and second elements of the list have to be the same.
list & equal(int idx_1, int idx_2)
Restricts generator s.t. values at two indices are equal.
The returned value can also be modified by some deterministic operations (specific to each type).
value & reverse()
Reverses the value.
Finally, there can be random operations defined for the generator value.
It::value_type pick(It first, It last)
Choses a random element from iterator range.
Combining everything into one line:
.equal(0, 1)
.gen()
.reverse()
) << std::endl;
Examples
Opts configuration
#include "tgen.h"
#include <iostream>
int main(int argc, char** argv) {
}
T next(T right)
Returns a random number up to value.
T opt(size_t index, std::optional< T > default_value=std::nullopt)
Gets opt by key.
void register_gen(int argc, char **argv)
Sets up the generator.
Calling this code with ./a.out -n 100 will generate a random number from 1 to 100.
Generation
Random 20 distinct values from 1 to 100.
std::cout <<
list & all_different()
Restricts generator s.t. all values are different.
Random Palindrome of length 7.
str & palindrome(int left, int right)
Restricts generator s.t. range is a palindrome.
value gen() const
Generates a random value from the set of valid strings.
Random 3 runs of 4 equal numbers. Values between runs are different.
std::cout <<
list & equal_range(int left, int right)
Restricts generator s.t. all values at index range are equal.
list & different(std::set< int > indices)
Restricts generator s.t. all values in index set are different.
Random DNA sequence of length 8 with no equal adjacent values.
for (
int i = 1; i < 8; i++) s2.
different(i-1, i);
std::cout << s2.
gen() << std::endl;
Random binary sequence of length 10 with 5 1's that start with 1.
std::cout <<
return std::accumulate(vec.begin(), vec.end(), 0) == 5;
}, 100) << std::endl;
auto gen_until(Pred predicate, int max_tries, Args &&...args) const
Generates a random value from the valid set until a condition is met.
auto to_std() const
Converts the value to a std::vector.
list & fix(int idx, T val)
Restricts generator s.t. value at index is fixed.
Random 1-based permutation of size 5 with only one cycle.
permutation & cycles(const std::vector< int > &cycle_sizes)
Restricts generator s.t. cycle sizes are fixed.
Inverse of a random odd permutation of size 5.
.
gen_until([](
const auto &perm) {
return perm.parity() == -1; }, 100)
.inverse() << std::endl;
Random prime in [1, 1e18].
uint64_t gen_prime(uint64_t left, uint64_t right)
Generates a random prime in given range.
Largest prime gap that fits in uint64_t.
std::cout << l << " " << r << " " << r - l << std::endl;
std::pair< uint64_t, uint64_t > prime_gap_upto(uint64_t right)
Largest prime gap up to given number.
Random partition of 10 into 2 parts in [3, 7].
std::vector< int > gen_partition_fixed_size(int n, int k, int part_left=0, std::optional< int > part_right=std::nullopt)
Generates a random partition with fixed size of a number.
Printer helper for standard types.
Random numbers in [0, 1e30].
std::cout <<
tgen::str(
"0 | [1-9][0-9]{0,%d} | 10{%d}", 30 - 1, 30).
gen_list(3) << std::endl;
auto gen_list(int size, Args &&...args) const
Generates a list of several generation calls.
Random perfect matching of K_10.
for (int i = 0; i < 5; ++i)
std::cout << g.
gen() <<
"," << g.gen() <<
" ";
Distinct generator for discrete uniform functions.
auto gen()
Generates a distinct value.
All primes in [1, 10], in order.
auto gen_all()
Generates all distinct values left to generate.
5 random square numbers in [1, 1e4].
return x * x;
}).gen_list(5) << std::endl;
Some random parenthesis sequences.
std::string gen_parenthesis(int size)
Generates a random valid parenthesis sequence.
auto gen_list(int size)
Generates a list of several distinct values.
All pairs (a, b) in [1, 3] with a <= b.
std::cout << tgen::pair<int>(1, 3).leq().distinct().gen_all().separator('|') << std::endl;