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

tgen::list<int> t = tgen::list<int>(/*size=*/10, /*value_l=*/1, /*value_r=*/100);
List generator.
Definition tgen.h:1190

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

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.

tgen::list<int>::value inst = t.equal(/*idx_1=*/0, /*idx_2=*/1).gen();
List value.
Definition tgen.h:1320
list & equal(int idx_1, int idx_2)
Restricts generator s.t. values at two indices are equal.
Definition tgen.h:1253

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

inst.reverse();
value & reverse()
Reverses the value.
Definition tgen.h:1356

Finally, there can be random operations defined for the generator value.

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

Combining everything into one line:

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

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

Random Palindrome of length 7.

std::cout << tgen::str(7, 'a', 'z').palindrome().gen() << std::endl;
// "iczpzci"
String generator.
Definition tgen.h:3076
str & palindrome(int left, int right)
Restricts generator s.t. range is a palindrome.
Definition tgen.h:3134
value gen() const
Generates a random value from the set of valid strings.
Definition tgen.h:3252

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

std::cout <<
tgen::list<int>(12, 1, 10)
.equal_range(0, 3).equal_range(4, 7).equal_range(8, 11)
.different({0, 4, 8}).gen() << std::endl;
// "3 3 3 3 2 2 2 2 9 9 9 9"
list & equal_range(int left, int right)
Restricts generator s.t. all values at index range are equal.
Definition tgen.h:1276
list & different(std::set< int > indices)
Restricts generator s.t. all values in index set are different.
Definition tgen.h:1290

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

auto s2 = tgen::list(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"

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

std::cout <<
tgen::list<int>(10, 0, 1)
.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 value from the valid set until a condition is met.
Definition tgen.h:341
auto to_std() const
Converts the value to a std::vector.
Definition tgen.h:1388
list & fix(int idx, T val)
Restricts generator s.t. value at index is fixed.
Definition tgen.h:1228

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:1725
permutation & cycles(const std::vector< int > &cycle_sizes)
Restricts generator s.t. cycle sizes are fixed.
Definition tgen.h:1744

Inverse of a random odd permutation of size 5.

std::cout << tgen::permutation(5)
.gen_until([](const auto &perm) { return perm.parity() == -1; }, 100)
.inverse() << std::endl;
// "4 2 3 1 0"

Random prime in [1, 1e18].

std::cout << tgen::math::gen_prime(1, 1e18) << std::endl;
// "104297037245455381"
uint64_t gen_prime(uint64_t left, uint64_t right)
Generates a random prime in given range.
Definition tgen.h:2369

Largest prime gap that fits in uint64_t.

auto [l, r] = tgen::math::prime_gap_upto(std::numeric_limits<uint64_t>::max());
std::cout << l << " " << r << " " << r - l << std::endl;
// "6787988999657777798 6787988999657779306 1508"
std::pair< uint64_t, uint64_t > prime_gap_upto(uint64_t right)
Largest prime gap up to given number.
Definition tgen.h:2300

Random partition of 10 into 2 parts in [3, 7].

std::cout << tgen::print(tgen::math::gen_partition_fixed_size(10, 2, 3, 7)) << std::endl;
// "6 4"
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.
Definition tgen.h:2669
Printer helper for standard types.
Definition tgen.h:429

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;
// "395192209976851520716904879188 507650968099964477977292350849 549612473618635975427061717252"
auto gen_list(int size, Args &&...args) const
Generates a list of several generation calls.
Definition tgen.h:329

Random perfect matching of K_10.

auto g = tgen::distinct([&]() { return tgen::next(0, 9); });
for (int i = 0; i < 5; ++i)
std::cout << g.gen() << "," << g.gen() << " ";
// "9,5 3,1 0,4 7,6 8,2 "
Distinct generator for discrete uniform functions.
Definition tgen.h:235
auto gen()
Generates a distinct value.
Definition tgen.h:276

All primes in [1, 10], in order.

std::cout << tgen::distinct(tgen::math::gen_prime, 1, 10).gen_all().sort() << std::endl;
// "2 3 5 7"
auto gen_all()
Generates all distinct values left to generate.
Definition tgen.h:302

5 random square numbers in [1, 1e4].

std::cout << tgen::distinct([&]() {
int x = tgen::next(1, 100);
return x * x;
}).gen_list(5) << std::endl;
// "676 1936 484 3481 9604"

Some random parenthesis sequences.

std::cout << tgen::distinct(tgen::misc::gen_parenthesis, 6).gen_list(5) << std::endl;
// "()(()) (())() (()()) ((())) ()()()"
std::string gen_parenthesis(int size)
Generates a random valid parenthesis sequence.
Definition tgen.h:3963
auto gen_list(int size)
Generates a list of several distinct values.
Definition tgen.h:288

All pairs (a, b) in [1, 3] with a <= b.

std::cout << tgen::pair<int>(1, 3).leq().distinct().gen_all().separator('|') << std::endl;
// "1 3|1 2|3 3|1 1|2 2|2 3"