tgen
Loading...
Searching...
No Matches
Registering and Opts

Registering and opts parsing. More...

Functions

bool tgen::has_opt (std::size_t index)
 Checks if opt at some index exists.
bool tgen::has_opt (const std::string &key)
 Checks if opt with with some key exists.
template<typename T>
tgen::opt (size_t index, std::optional< T > default_value=std::nullopt)
 Gets opt by key.
template<typename T>
tgen::opt (const std::string &key, std::optional< T > default_value=std::nullopt)
 Gets opt by key.
void tgen::register_gen (int argc, char **argv)
 Sets up the generator.
void tgen::register_gen (std::optional< long long > seed=std::nullopt)
 Sets up the generator without arguments.
void tgen::set_cpp_version (int version)
 Sets C++ version.
void tgen::set_compiler (_detail::compiler_value compiler)
 Sets compiler.

Detailed Description

Registering and opts parsing.

Registering

The first line of your generator should be:

#include "tgen.h"
int main(int argc, char** argv) {
tgen::register_gen(argc, argv);
// Rest of the generator.
}
void register_gen(int argc, char **argv)
Sets up the generator.
Definition tgen.h:1102

This does two things:

  1. Sets up the seed for random generator based on the arguments (excluding the executable name). That is, ./a.out -n 10 and ./b.out -n 10 will use the same seed.
  2. Parses the opts (given in the arguments).

Opts

Opts are a list of either named or positional options.

Named options is given in one of the following formats:

  1. -keyname=value or –keyname=value (ex. -n=10, –test-count=20)
  2. -keyname value or –keyname value (ex. -n 10, –test-count 20)

Positional options are numbered from 0 sequentially.

For example, if you compile your code to a.out, calling ./a.out -n 10 str –q=5 -1.1e2 results in the following opts:

  • Named
    • n: 10
    • q: 5
  • Positional
    • 0: str
    • 1: -110

Consuming opts

You can consume the opts by using opt<>:

int n = tgen::opt<int>("n");
std::string s = tgen::opt<std::string>(0, ""); // Default value, if opt is not found.
T opt(size_t index, std::optional< T > default_value=std::nullopt)
Gets opt by key.
Definition tgen.h:1077

Function Documentation

◆ has_opt() [1/2]

bool tgen::has_opt ( const std::string & key)
inline

Checks if opt with with some key exists.

Parameters
keyKey of named opt.
Returns
If there is named opt with key.

Examples

// Checks if there is an opt with "q".
if (tgen::has_opt("q")) std::cout << "q found!" << std::endl;
bool has_opt(std::size_t index)
Checks if opt at some index exists.
Definition tgen.h:1063

Definition at line 1069 of file tgen.h.

◆ has_opt() [2/2]

bool tgen::has_opt ( std::size_t index)
inline

Checks if opt at some index exists.

Parameters
indexIndex of positional opt.
Returns
If there is positional opt at index.

Examples

// Checks if there is an opt at 0.
if (tgen::has_opt(0)) std::cout << "opt found!" << std::endl;

Definition at line 1063 of file tgen.h.

◆ opt() [1/2]

template<typename T>
T tgen::opt ( const std::string & key,
std::optional< T > default_value = std::nullopt )

Gets opt by key.

Template Parameters
TType of the opt.
Parameters
keyKey to fetch.
default_valueDefault value to be returned, if key is not found.
Returns
The value of opt with key. If not found, returns default_value.
Exceptions
std::runtime_errorif opt is not found and default_value is not given.

Examples

// Consumes opt with "q".
int q = tgen::opt<int>("q");

Definition at line 1091 of file tgen.h.

◆ opt() [2/2]

template<typename T>
T tgen::opt ( size_t index,
std::optional< T > default_value = std::nullopt )

Gets opt by key.

Template Parameters
TType of the opt.
Parameters
indexindex to fetch.
default_valueDefault value to be returned, if index is not found.
Returns
The value of opt at index. If not found, returns default_value.
Exceptions
std::runtime_errorif opt is not found and default_value is not given.

Examples

// Consumes opt at 0.
int q = tgen::opt<int>(0);

Definition at line 1077 of file tgen.h.

◆ register_gen() [1/2]

void tgen::register_gen ( int argc,
char ** argv )
inline

Sets up the generator.

Parameters
argcArgument count, from the main function.
argvArgument vector, from the main function.

Sets up the opts, and the seed of the random number generator. The seed is dependent on the arguments (excluding the executable name).

Examples

#include "tgen.h"
int main(int argc, char** argv) {
// Register generator.
tgen::register_gen(argc, argv);
}

Definition at line 1102 of file tgen.h.

◆ register_gen() [2/2]

void tgen::register_gen ( std::optional< long long > seed = std::nullopt)
inline

Sets up the generator without arguments.

Parameters
seedSeed for the random number generator.

Examples

#include "tgen.h"
int main() {
// Register generator with seed 42.
}

Or even:

#include "tgen.h"
int main() {
// Register generator with default seed.
}

Definition at line 1113 of file tgen.h.

◆ set_compiler()

void tgen::set_compiler ( _detail::compiler_value compiler)
inline

Sets compiler.

Parameters
compilerThe compiler (tgen::gcc(...) or tgen::clang(...)).

This can be used by some functions to find counter testcases for specific systems.

Examples

// Sets as GCC (version unspecified).
tgen::set_compiler(tgen::gcc());
// Sets as GCC 15.
tgen::set_compiler(tgen::gcc(15));
// Sets as GCC 15.2.
tgen::set_compiler(tgen::gcc(15, 2));
// Sets as clang (version unspecified).
tgen::set_compiler(tgen::clang());
void set_compiler(_detail::compiler_value compiler)
Sets compiler.
Definition tgen.h:877

This can also be set by command argument:

  • ./executable tgen::GCC: equivalent to tgen::set_compiler(tgen::gcc()).
  • ./executable tgen::GCC:15: equivalent to tgen::set_compiler(tgen::gcc(15)).
  • ./executable tgen::GCC:15.2: equivalent to tgen::set_compiler(tgen::gcc(15, 2)).

Definition at line 877 of file tgen.h.

◆ set_cpp_version()

void tgen::set_cpp_version ( int version)
inline

Sets C++ version.

Parameters
versionThe C++ version (17, 20, or 23).

This can be used by some functions to find counter testcases for specific systems.

Examples

// Sets as C++17.
void set_cpp_version(int version)
Sets C++ version.
Definition tgen.h:858

This can also be set by command argument:

  • ./executable tgen::CPP:20: equivalent to tgen::set_cpp_version(20).

Definition at line 858 of file tgen.h.