Hegel 0.11.4
Property-based testing for C++
Loading...
Searching...
No Matches
hegel::stateful::Pool< T > Class Template Reference

A pool of previously generated values. A pool lets data flow from one rule to another, so a rule can act on a handle or identifier that an earlier rule produced rather than on a freshly drawn value. More...

#include <stateful.h>

Public Member Functions

 Pool (const TestCase &tc)
 Creates an empty pool. Pools are tied to a test case. Do not reuse one across test cases.
void add (const T &element)
 Adds element to the pool. Overload for copying lvalues.
void add (T &&element)
 Adds element to the pool. Overload for moving rvalues.
std::size_t size () const
 Returns the number of variables in the pool.
 Pool (const Pool &)=delete

Friends

class VariablesGenerator< T >

Detailed Description

template<typename T>
class hegel::stateful::Pool< T >

A pool of previously generated values. A pool lets data flow from one rule to another, so a rule can act on a handle or identifier that an earlier rule produced rather than on a freshly drawn value.

Create one with its constructor and populate it with add. To draw from the pool, use the generators values_reusable (returns a value without removing it) and values_consumed (removes and returns a value).

A Pool is neither copyable nor movable. A state that embeds a pool is passed to run by reference and never copied.

Example: a resource allocator. The alloc rule creates a fresh resource and deposits it in the pool. The free rule draws one of those resource back out and releases it.

struct Allocator
: hegel::stateful::StateMachine<Allocator, std::set<int>> {
// The pool is tied to a test case and cannot be copied, so it is
// a member of the machine rather than part of the state.
int next_handle = 0;
explicit Allocator(hegel::TestCase& tc)
: StateMachine({.initial_state = {}}), handles(tc) {}
std::vector<hegel::stateful::Rule<Allocator>> rules() {
return {
"alloc", [](hegel::TestCase&, Allocator& m) {
int h = m.next_handle++;
m.handles.add(h);
m.state.insert(h);
}),
hegel::stateful::Rule<Allocator>(
"free", [](hegel::TestCase& tc, Allocator& m) {
// draws a handle a prior alloc put in the pool
auto h = tc.draw(
"h",
m.state.erase(h);
}),
};
}
};
HEGEL_TEST(allocator_state_machine)(hegel::TestCase& tc) {
Allocator machine(tc);
hegel::stateful::run(machine, tc);
}
Handle to the currently-executing test case.
Definition test_case.h:40
T draw(const generators::Generator< T > &gen) const
Draw a random value from a generator.
Definition core.h:264
A pool of previously generated values. A pool lets data flow from one rule to another,...
Definition stateful.h:122
A rule is one possible action in a stateful test.
Definition stateful.h:289
Base class for a state machine. Holds the state and declares the rules and the invariants that act on...
Definition stateful.h:406
#define HEGEL_TEST(name,...)
Define a Hegel property test.
Definition hegel.h:575
Generator< T > values_consumed(Pool< T > &p)
Returns a value from the pool and removes it.
Definition stateful.h:242
void run(M &machine, TestCase &tc, const RunParams &params={})
Executes a stateful test by repeatedly applying randomly chosen rules from machine to it and checking...
Definition stateful.h:508
Template Parameters
TThe type of variables in the pool.

Constructor & Destructor Documentation

◆ Pool()

template<typename T>
hegel::stateful::Pool< T >::Pool ( const TestCase & tc)
inlineexplicit

Creates an empty pool. Pools are tied to a test case. Do not reuse one across test cases.

Parameters
tcThe test case tied to the pool

Member Function Documentation

◆ add() [1/2]

template<typename T>
void hegel::stateful::Pool< T >::add ( const T & element)
inline

Adds element to the pool. Overload for copying lvalues.

Parameters
elementThe element to add to the pool

◆ add() [2/2]

template<typename T>
void hegel::stateful::Pool< T >::add ( T && element)
inline

Adds element to the pool. Overload for moving rvalues.

Parameters
elementThe element to add to the pool

◆ size()

template<typename T>
std::size_t hegel::stateful::Pool< T >::size ( ) const
inline

Returns the number of variables in the pool.

Returns
The number of variables in the pool

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