|
Hegel 0.3.5
Property-based testing for C++
|
Hegel generators. More...
Classes | |
| struct | BasicGenerator |
| class | CompositeGenerator |
| class | MappedGenerator |
| struct | IGenerator |
| */ More... | |
| class | Generator |
| The base class of all generators. More... | |
| struct | Field |
| Helper for named field initialization in builds_agg(). More... | |
| struct | VectorsParams |
| Parameters for vectors() generator. More... | |
| struct | SetsParams |
| Parameters for sets() generator. More... | |
| struct | MapsParams |
| Parameters for maps() generator. More... | |
| class | DerivedGenerator |
| A Generator produced by default_generator<T>(). More... | |
| struct | DomainsParams |
| Parameters for domains() generator. More... | |
| struct | IpAddressesParams |
| Parameters for ip_addresses() generator. More... | |
| struct | IntegersParams |
| Parameters for integers() generator. More... | |
| struct | FloatsParams |
| Parameters for floats() generator. More... | |
| struct | RandomsParams |
| Parameters for randoms() generator. More... | |
| class | HegelRandom |
| A random engine that integrates with Hypothesis via Hegel. More... | |
| struct | TextParams |
| Parameters for text() generator. More... | |
| struct | CharactersParams |
| Parameters for characters() generator. More... | |
| struct | BinaryParams |
| Parameters for binary() generator. More... | |
Functions | |
| template<typename F> | |
| auto | compose (F &&fn) |
| */ | |
Combinators | |
| template<typename T, typename... Gens> | |
| Generator< T > | builds (Gens... gens) |
| Construct objects using positional constructor arguments. | |
| template<auto MemberPtr, typename Gen> | |
| Field< MemberPtr, Gen > | field (Gen gen) |
| Create a field specification for builds_agg(). | |
| template<typename T, typename... Fields> | |
| Generator< T > | builds_agg (Fields... fields) |
| Construct aggregates using named field initialization. | |
| template<typename T> | |
| Generator< T > | one_of (std::vector< Generator< T > > gens) |
| Choose from multiple generators of the same type. | |
| template<typename T> | |
| Generator< T > | one_of (std::initializer_list< Generator< T > > gens) |
| Choose from a list of generators (initializer list). | |
| template<typename... Ts> | |
| Generator< std::variant< Ts... > > | variant (Generator< Ts >... gens) |
| Generate std::variant from heterogeneous generators. | |
| template<typename T> | |
| Generator< std::optional< T > > | optional (Generator< T > gen) |
| Generate optional values (present or absent). | |
Collections | |
| template<typename T> | |
| Generator< std::vector< T > > | vectors (Generator< T > elements, VectorsParams params={}) |
| Generate vectors with elements from another generator. | |
| template<typename T> | |
| Generator< std::set< T > > | sets (Generator< T > elements, SetsParams params={}) |
| Generate sets with elements from another generator. | |
| template<typename K, typename V> | |
| Generator< std::map< K, V > > | maps (Generator< K > keys, Generator< V > values, MapsParams params={}) |
| Generate maps with configurable key and value types. | |
| template<typename... Ts> | |
| Generator< std::tuple< Ts... > > | tuples (Generator< Ts >... gens) |
| Generate tuples from multiple generators. | |
Misc | |
| template<typename T> | |
| Generator< T > | sampled_from (const std::vector< T > &elements) |
| Sample from a fixed set of values. | |
| template<typename T> | |
| Generator< T > | sampled_from (std::initializer_list< T > elements) |
| Sample from a fixed set of values (initializer list). | |
| Generator< std::string > | sampled_from (std::initializer_list< const char * > elements) |
| Sample from a fixed set of C-string literals. | |
| Generator< std::string > | ip_addresses (IpAddressesParams params={}) |
| Generate IP addresses. | |
Typing | |
| template<typename T> | |
| DerivedGenerator< T > | default_generator () |
| Create a default generator for type T. | |
Strings | |
| Generator< std::string > | emails () |
| Generate valid email addresses. | |
| Generator< std::string > | domains (DomainsParams params={}) |
| Generate valid domain names. | |
| Generator< std::string > | urls () |
| Generate valid URLs. | |
| Generator< std::string > | text (TextParams params={}) |
| Generate random text strings. | |
| Generator< std::string > | characters (const CharactersParams ¶ms={}) |
| Generate single-character UTF-8 strings. | |
| Generator< std::vector< uint8_t > > | binary (BinaryParams params={}) |
| Generate random binary data (byte sequences). | |
| Generator< std::string > | from_regex (const std::string &pattern, bool fullmatch=false) |
| Generate strings matching a regular expression. | |
Datetime | |
| Generator< std::string > | dates () |
| Generate calendar dates. | |
| Generator< std::string > | times () |
| Generate times of day. | |
| Generator< std::string > | datetimes () |
| Generate datetimes. | |
Numeric | |
| template<typename T = int64_t> requires std::is_integral_v<T> | |
| Generator< T > | integers (IntegersParams< T > params={}) |
| Generate random integers. For a given integral type T, produces values in the range [std::numeric_limits<T>::min(), std::numeric_limits<T>::max()] by default. | |
| template<typename T = double> requires std::is_floating_point_v<T> | |
| Generator< T > | floats (FloatsParams< T > params={}) |
| Generate random floating point numbers. | |
Primitives | |
| Generator< bool > | booleans () |
| Generate random boolean values. | |
| template<typename T> | |
| Generator< T > | just (T value) |
| Generate a constant value. | |
| Generator< std::string > | just (const char *value) |
| Overload for just so that just("a string literal") has type Generator<std::string> rather than Generator<const char*>. | |
Random | |
| Generator< HegelRandom > | randoms (RandomsParams params={}) |
| Generate random number generators. | |
Hegel generators.
| Generator< std::vector< uint8_t > > hegel::generators::binary | ( | BinaryParams | params = {} | ) |
Generate random binary data (byte sequences).
| params | Size constraints |
| Generator< bool > hegel::generators::booleans | ( | ) |
Generate random boolean values.
| Generator< T > hegel::generators::builds | ( | Gens... | gens | ) |
Construct objects using positional constructor arguments.
Generates constructor arguments from the provided generators and calls T's constructor.
| T | Type to construct |
| Gens | Generator types for constructor arguments |
| gens | Generators providing constructor arguments |
| Generator< T > hegel::generators::builds_agg | ( | Fields... | fields | ) |
Construct aggregates using named field initialization.
Useful for structs where you want to specify fields by name rather than position. Each field() specifies a member pointer and generator.
| T | Aggregate type to construct |
| Fields | Field specification types |
| Generator< std::string > hegel::generators::characters | ( | const CharactersParams & | params = {} | ) |
Generate single-character UTF-8 strings.
| params | Character filtering constraints |
| auto hegel::generators::compose | ( | F && | fn | ) |
*/
*/
Build a generator from imperative code that draws from a TestCase.
The element type is deduced from fn's return type. To force a specific type, give the lambda an explicit trailing return type.
| F | A callable taking const TestCase& |
| fn | Function that draws from the TestCase and returns a value |
fn | Generator< std::string > hegel::generators::dates | ( | ) |
Generate calendar dates.
Generates a date between January 01, 0001 through December 31, 9999 in the proleptic Gregorian calendar) and returns the ISO 8601 serialization (YYYY-MM-DD). Values shrink towards January 1st, 2000.
| Generator< std::string > hegel::generators::datetimes | ( | ) |
Generate datetimes.
Generates datetimes between January 01, 0001 at 00:00:00 and December 31, 9999 at 23:59:59.999999 and returns the ISO 8601 serialization (YYYY-MM-DDTHH:MM:SS[.ffffff]). No timezone is requested; generated values are naive. Examples from this strategy shrink towards midnight on January 1st 2000.
| DerivedGenerator< T > hegel::generators::default_generator | ( | ) |
Create a default generator for type T.
Dispatches to the appropriate built-in generator based on the type:
For structs, each field is generated using default_generator for its type. Call .override(...) on the returned generator to customize individual fields:
| T | The type to generate |
| Generator< std::string > hegel::generators::domains | ( | DomainsParams | params = {} | ) |
Generate valid domain names.
Generates RFC 1035-compliant fully qualified domain names.
| params | Length constraints. max_length (default 255) must be in the range [4, 255]; |
| Generator< std::string > hegel::generators::emails | ( | ) |
Generate valid email addresses.
Generates addresses in the format specified by RFC 5322 Section 3.4.1 (i.e. local-part@domain). Values shrink towards shorter local-parts and host domains.
| Field< MemberPtr, Gen > hegel::generators::field | ( | Gen | gen | ) |
Create a field specification for builds_agg().
| MemberPtr | Pointer-to-member specifying which field |
| Gen | Generator type |
| gen | Generator for the field value |
| Generator< T > hegel::generators::floats | ( | FloatsParams< T > | params = {} | ) |
Generate random floating point numbers.
| T | Floating point type (default: double) |
| params | Bounds and exclusion constraints |
| Generator< std::string > hegel::generators::from_regex | ( | const std::string & | pattern, |
| bool | fullmatch = false ) |
Generate strings matching a regular expression.
The pattern is interpreted server-side using Python's re syntax which differs from C++ std::regex — notably it supports \d, \w, \s, non-greedy quantifiers, and Unicode character classes.
| pattern | Regex pattern (Python re syntax). |
| fullmatch | If true, the entire generated string must match the pattern (equivalent to anchoring with ^ and $). If false (default), the generated string need only contain a substring that matches; arbitrary characters may appear before or after the match. |
pattern under the selected match mode. | Generator< T > hegel::generators::integers | ( | IntegersParams< T > | params = {} | ) |
Generate random integers. For a given integral type T, produces values in the range [std::numeric_limits<T>::min(), std::numeric_limits<T>::max()] by default.
| T | Integer type (default: int64_t) |
| params | Bounds constraints |
| Generator< std::string > hegel::generators::ip_addresses | ( | IpAddressesParams | params = {} | ) |
Generate IP addresses.
Generates IP addresses serialized to text:
Any address in the selected version's space may be produced.
| params | Version constraint: v = 4 for IPv4 only, v = 6 for IPv6 only, or std::nullopt (default) for a mix of both. |
|
inline |
| Generator< T > hegel::generators::just | ( | T | value | ) |
| Generator< std::map< K, V > > hegel::generators::maps | ( | Generator< K > | keys, |
| Generator< V > | values, | ||
| MapsParams | params = {} ) |
Generate maps with configurable key and value types.
| K | Key type |
| V | Value type |
| Generator< T > hegel::generators::one_of | ( | std::initializer_list< Generator< T > > | gens | ) |
Choose from a list of generators (initializer list).
| T | Value type produced by each generator |
| gens | Generators to choose from (must not be empty) |
Choose from multiple generators of the same type.
Randomly selects one generator and uses it to produce a value.
| T | Value type (all generators must produce this type) |
| gens | Vector of generators to choose from (must not be empty) |
| Generator< std::optional< T > > hegel::generators::optional | ( | Generator< T > | gen | ) |
Generate optional values (present or absent).
Randomly produces either a value from the given generator or std::nullopt.
| T | Value type |
| gen | Generator for the value when present |
| Generator< HegelRandom > hegel::generators::randoms | ( | RandomsParams | params = {} | ) |
Generate random number generators.
Returns a Generator that produces HegelRandom instances satisfying UniformRandomBitGenerator, enabling use with any <random> distribution. If use_true_random is set to true then values will be drawn from their usual distribution, seeded by Hegel. Otherwise they will actually be Hegel generated values (and will be shrunk accordingly for any failing test case). Setting use_true_random=false will tend to expose bugs that would occur with very low probability when it is set to true, and this flag should only be set to true when your code relies on the distribution of values for correctness.
| params | Configuration (use_true_random to switch modes) |
| Generator< T > hegel::generators::sampled_from | ( | const std::vector< T > & | elements | ) |
Sample from a fixed set of values.
| T | Element type |
| elements | Vector of values to sample from (must not be empty) |
|
inline |
Sample from a fixed set of C-string literals.
| elements | String literals to sample from (must not be empty) |
| Generator< T > hegel::generators::sampled_from | ( | std::initializer_list< T > | elements | ) |
Sample from a fixed set of values (initializer list).
| T | Element type |
| elements | Values to sample from (must not be empty) |
| Generator< std::set< T > > hegel::generators::sets | ( | Generator< T > | elements, |
| SetsParams | params = {} ) |
Generate sets with elements from another generator.
| T | Element type (must be comparable) |
| elements | Generator for set elements |
| params | Size constraints |
| Generator< std::string > hegel::generators::text | ( | TextParams | params = {} | ) |
Generate random text strings.
| params | Length and character filtering constraints |
| Generator< std::string > hegel::generators::times | ( | ) |
Generate times of day.
Generates a time between 00:00:00 and 23:59:59.999999 and returns the ISO 8601 serialization (HH:MM:SS[.ffffff]). Values shrink towards midnight. No timezone component is requested; generated values are naive.
| Generator< std::tuple< Ts... > > hegel::generators::tuples | ( | Generator< Ts >... | gens | ) |
Generate tuples from multiple generators.
Each generator produces one element of the resulting tuple.
| Ts | Element types |
| gens | Generators for each tuple element |
| Generator< std::string > hegel::generators::urls | ( | ) |
Generate valid URLs.
Generates RFC 3986-compliant URLs with either the http or https scheme.
| Generator< std::variant< Ts... > > hegel::generators::variant | ( | Generator< Ts >... | gens | ) |
Generate std::variant from heterogeneous generators.
Each generator produces one possible variant alternative.
| Ts | Variant alternative types |
| gens | Generators for each alternative |
| Generator< std::vector< T > > hegel::generators::vectors | ( | Generator< T > | elements, |
| VectorsParams | params = {} ) |
Generate vectors with elements from another generator.
| T | Element type |
| elements | Generator for vector elements |
| params | Size and uniqueness constraints |