Hegel 0.3.5
Property-based testing for C++
Loading...
Searching...
No Matches
hegel::generators::Generator< T > Class Template Reference

The base class of all generators. More...

#include <core.h>

Inheritance diagram for hegel::generators::Generator< T >:
hegel::generators::IGenerator< T > hegel::generators::DerivedGenerator< T >

Public Member Functions

template<typename F>
Generator< std::invoke_result_t< F, T > > map (F f) const
 *‍/
template<typename F>
std::invoke_result_t< F, T > flat_map (F &&f) const
 Chain generators for dependent generation.
Generator< T > filter (std::function< bool(const T &)> pred) const
 Filter generated values by a predicate.

Detailed Description

template<typename T>
class hegel::generators::Generator< T >

The base class of all generators.

namespace gs = hegel::generators;
// Create a generator and draw a value
auto int_gen = gs::integers<int>({.min_value = 0, .max_value = 100});
int value = tc.draw(int_gen);
// Transform with map
auto squared = int_gen.map([](int x) { return x * x; });
// Filter values
auto even = int_gen.filter([](int x) { return x % 2 == 0; });
// Dependent generation with flat_map
auto sized = gs::integers<size_t>({.min_value = 1, .max_value = 10})
.flat_map([](size_t len) {
return gs::text({.min_size = len, .max_size = len});
});
});
Handle to the currently-executing test case.
Definition test_case.h:34
T draw(const generators::Generator< T > &gen) const
Draw a random value from a generator.
Definition core.h:371
std::invoke_result_t< F, T > flat_map(F &&f) const
Chain generators for dependent generation.
Definition core.h:228
Hegel generators.
Definition core.h:16
void test(const std::function< void(TestCase &)> &test_fn, const Settings &settings={})
Run a Hegel test.
Template Parameters
TThe type to generate values for

Member Function Documentation

◆ filter()

template<typename T>
Generator< T > hegel::generators::Generator< T >::filter ( std::function< bool(const T &)> pred) const
inline

Filter generated values by a predicate.

Creates a Generator that only produces values satisfying the predicate. The new Generator has the same type as this Generator.

So for example, if you want sorted lists of length N, you should generate sorted lists of length N, not generate random lists and filter by a predicate of 'length == N && is_sorted'. (Although the latter is logically correct, it would be a performance nightmare, so Hegel doesn't let you do it that way.)

For example, if you want sorted lists of length N, you should generate lists of length N and sort them, not generate random lists and filter by a predicate of 'length == N && is_sorted'.

auto even = integers<int>({.min_value = 0, .max_value = 100})
.filter([](int x) { return x % 2 == 0; });
// even is Generator<int>
Generator< T > filter(std::function< bool(const T &)> pred) const
Filter generated values by a predicate.
Definition core.h:269
Generator< T > integers(IntegersParams< T > params={})
Generate random integers. For a given integral type T, produces values in the range [std::numeric_lim...
Definition numeric.h:146
Parameters
predPredicate that values must satisfy
Returns
Generator<T> producing only values satisfying pred

◆ flat_map()

template<typename T>
template<typename F>
std::invoke_result_t< F, T > hegel::generators::Generator< T >::flat_map ( F && f) const
inline

Chain generators for dependent generation.

Given a Generator<T> and a function T -> Generator<S>, creates a Generator<S>. Useful when generation parameters depend on previously generated values.

auto sized_string =
integers<size_t>({.min_value = 1, .max_value = 10})
.flat_map([](size_t len) {
return text({.min_size = len, .max_size = len});
});
// sized_string is Generator<std::string>
Generator< std::string > text(TextParams params={})
Generate random text strings.
Template Parameters
FFunction type (T -> Generator<S>)
Parameters
fFunction that takes a T and returns a Generator<S>
Returns
Generator<S> producing values from the chained generator
See also
map(), text()

◆ map()

template<typename T>
template<typename F>
Generator< std::invoke_result_t< F, T > > hegel::generators::Generator< T >::map ( F f) const
inline

*‍/

Transform generated values with a function.

Given a Generator<T> and a function T -> S, creates a Generator<S>.

This works by generating values from the Generator<T> and applying a transformation to each value.

Here's an example of how you'd use this:

auto halved = integers<int>().map(
[](int x) { return double(x) / 2.0; }
);
// halved is Generator<double>
Template Parameters
FFunction type (T -> S)
Parameters
fTransformation function with signature S f(T)
Returns
Generator<S> producing transformed values
See also
flat_map()

The documentation for this class was generated from the following file: