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 instances
All data types specified above define a generator, that when called upon will generate a uniformly random instance with the given constraints. Let's see an example with tgen::sequence:
This will create a sequence generator representing the set of all sequences with 10 values from 1 to 100.
Every generator of type GEN has a method gen(), that returns a GEN::instance representing an element chosen uniformly at random from the set of all valid elements from the current state of the generator. A GEN::instance can be fed to std::cout to be printed.
In our example, we can call gen() to generate and print a random sequence of 10 elements from 1 to 100.
std::cout << seq_gen.
gen() << std::endl;
instance gen() const
Generates a random instance from the set of valid sequences.
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 sequence have to be the same.
sequence & equal(int idx_1, int idx_2)
Restricts generator s.t. values at two indices are the same.
The returned instance can also be modified by some deterministic operations (specific to each type).
instance & reverse()
Reverses the instance.
Finally, there can be random operations defined for the type instance.
std::cout << tgen::sequence_op::any(inst) << std::endl;
Combining everything into one line:
std::cout << tgen::sequence_op::any(
.equal(0, 1)
.gen()
.reverse()
) << std::endl;
Examples
Opts configuration
#include "tgen.h"
#include <iostream>
int main(int argc, char** argv) {
}
T next(T l, T r)
Returns a random number in [l, r].
T opt(const Key &key, 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 <<
sequence & distinct(std::set< int > indices)
Restricts generator s.t. all values at index set are distinct.
Random Palindrome of length 7.
for (int i = 0; i <= 2; ++i) s.equal(i, 6-i);
std::cout << s.gen() << std::endl;
Random 3 runs of 4 equal numbers. Values between runs are distinct.
std::cout <<
.
distinct({0, 4, 8}).gen() << std::endl;
sequence & equal_range(int left, int right)
Restricts generator s.t. all values at index range are the same.
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;
sequence & different(int idx_1, int idx_2)
Restricts generator s.t. values at two indices are different.
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;
instance gen_until(Pred predicate, int max_tries) const
Generates a random instance from the set of valid sequences until a condition is met.
sequence & set(int idx, T value)
Restricts generator s.t. value at index is fixed.
std::vector< T > to_std() const
Converts the instance to a std::vector.
Random 1-based permutation of size 5 with only one cycle.
instance gen() const
Generates a random instance from the set of valid permutations.
Inverse of a random odd permutation of size 5.
std::cout <<
.
gen_until([](
const auto &perm) {
return perm.parity() == -1; }, 100)
.inverse()
<< std::endl;
instance gen_until(Pred predicate, int max_tries, Args &&... args) const
Generates a random instance from the set of valid permutations until a condition is met.