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


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.

#include "tgen.h"

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:

tgen::sequence<int> seq_gen = tgen::sequence<int>(/*size=*/10, /*value_l=*/1, /*value_r=*/100);
Sequence generator.
Definition tgen.h:615

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.
Definition tgen.h:828

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.

tgen::sequence<int>::instance inst = seq_gen.equal(/*idx_1=*/0, /*idx_2=*/1).gen();
sequence & equal(int idx_1, int idx_2)
Restricts generator s.t. values at two indices are the same.
Definition tgen.h:676
Sequence instance.
Definition tgen.h:721

The returned instance can also be modified by some deterministic operations (specific to each type).

inst.reverse();
instance & reverse()
Reverses the instance.
Definition tgen.h:747

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(
tgen::sequence<int>(10, 1, 100)
.equal(0, 1)
.gen()
.reverse()
) << std::endl;

Examples

Opts configuration

#include "tgen.h"
#include <iostream>
int main(int argc, char** argv) {
tgen::register_gen(argc, argv);
int n_max = tgen::opt<int>("n");
std::cout << tgen::next(1, n) << std::endl;
}
T next(T l, T r)
Returns a random number in [l, r].
Definition tgen.h:302
T opt(const Key &key, std::optional< T > default_value=std::nullopt)
Gets opt by key.
Definition tgen.h:573
void register_gen(int argc, char **argv)
Sets up the generator.
Definition tgen.h:595

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 <<
tgen::sequence<int>(20, 1, 100).distinct().gen() << std::endl;
// "67 96 80 11 46 52 42 2 93 1 28 3 48 82 90 99 53 98 94 88"
sequence & distinct(std::set< int > indices)
Restricts generator s.t. all values at index set are distinct.
Definition tgen.h:700

Random Palindrome of length 7.

auto s = tgen::sequence<int>(7, 0, 9);
for (int i = 0; i <= 2; ++i) s.equal(i, 6-i);
std::cout << s.gen() << std::endl;
// "3 1 9 6 9 1 3"

Random 3 runs of 4 equal numbers. Values between runs are distinct.

std::cout <<
.equal_range(0, 3).equal_range(4, 7).equal_range(8, 11)
.distinct({0, 4, 8}).gen() << std::endl;
// "3 3 3 3 2 2 2 2 9 9 9 9"
sequence & equal_range(int left, int right)
Restricts generator s.t. all values at index range are the same.
Definition tgen.h:689

Random DNA sequence of length 8 with no equal adjacent values.

auto s2 = tgen::sequence(8, {'A','C','G','T'});
for (int i = 1; i < 8; i++) s2.different(i-1, i);
std::cout << s2.gen() << std::endl;
// "T C T G T G A C"
sequence & different(int idx_1, int idx_2)
Restricts generator s.t. values at two indices are different.
Definition tgen.h:707

Random binary sequence of length 10 with 5 1's that start with 1.

std::cout <<
.set(0, 1)
.gen_until([](const auto& inst) {
auto vec = inst.to_std();
return std::accumulate(vec.begin(), vec.end(), 0) == 5;
}, 100) << std::endl;
// "1 0 0 1 0 1 1 0 1 0"
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.
Definition tgen.h:651
std::vector< T > to_std() const
Converts the instance to a std::vector.
Definition tgen.h:773

Random 1-based permutation of size 5 with only one cycle.

std::cout << tgen::permutation(5).gen({5}).add_1() << std::endl;
// "2 5 4 1 3"
instance gen() const
Generates a random instance from the set of valid permutations.
Definition tgen.h:1213
Permutation generator.
Definition tgen.h:1102

Inverse of a random odd permutation of size 5.

std::cout <<
.gen_until([](const auto &perm) { return perm.parity() == -1; }, 100)
.inverse()
<< std::endl;
// "4 2 3 1 0"
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.