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:1135

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:1367

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 instance.
Definition tgen.h:1242
sequence & equal(int idx_1, int idx_2)
Restricts generator s.t. values at two indices are the same.
Definition tgen.h:1197

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:1279

Finally, there can be random operations defined for the type instance.

std::cout << tgen::any(inst) << std::endl;
It::value_type any(It first, It last)
Choses a random element from iterator range.
Definition tgen.h:624

Combining everything into one line:

std::cout << tgen::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 n)
Returns a random number up to value.
Definition tgen.h:528
T opt(size_t index, std::optional< T > default_value=std::nullopt)
Gets opt by key.
Definition tgen.h:1077
void register_gen(int argc, char **argv)
Sets up the generator.
Definition tgen.h:1102

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 in index set are distinct.
Definition tgen.h:1221

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:1210

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:1228

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

std::cout <<
.fix(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"
auto gen_until(Pred predicate, int max_tries, Args &&...args) const
Generates a random instance from the valid set until a condition is met.
Definition tgen.h:335
std::vector< T > to_std() const
Converts the instance to a std::vector.
Definition tgen.h:1312
sequence & fix(int idx, T value)
Restricts generator s.t. value at index is fixed.
Definition tgen.h:1172

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

std::cout << tgen::permutation(5).cycles({5}).gen().add_1() << std::endl;
// "2 5 4 1 3"
Permutation generator.
Definition tgen.h:1642
permutation & cycles(const std::vector< int > &cycle_sizes)
Restricts generator s.t. cycle sizes are fixed.
Definition tgen.h:1661

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"