Class: Hegel::Stateful::Pool

Inherits:
Object
  • Object
show all
Defined in:
lib/hegel/stateful/pool.rb,
sig/hegel.rbs

Overview

A pool of previously generated values, for a later stateful rule to draw one back out. Build one from the running test case, inside the machine's own constructor:

Hegel::Stateful::Pool.new(tc)

#add records a value under a fresh variable id (hegel_pool_add); #size and #empty? read the Ruby-side count directly. Drawing goes through the two generators below, not through this class's own storage, so a chosen id is drawn (and shrunk, and recorded in a failure report) the same way any other value is: #values_reusable leaves the drawn value in the pool, #values_consumed removes it. hegel-rust's own Pool (src/stateful.rs) keeps the same three-way split between the engine's variable-id choice, this class's own id-to-value map, and the two generators.

A caller never frees a pool. docs/adr/0011 has the reason: #initialize opens the native handle through Hegel::TestCase#new_pool, which records it on tc itself, and Hegel::Runner frees every pool a test case recorded once that test case is done -- the same "the test case owns what it opened" split this library already uses for a state-machine handle.

Defined Under Namespace

Classes: ValuesConsumed, ValuesReusable

Instance Method Summary collapse

Constructor Details

#initialize(tc) ⇒ Pool

Returns a new instance of Pool.

Parameters:



30
31
32
33
34
# File 'lib/hegel/stateful/pool.rb', line 30

def initialize(tc)
  @tc = tc
  @pool = tc.new_pool
  @values = {}
end

Instance Method Details

#add(value) ⇒ Pool

Records value under a fresh variable id from hegel_pool_add. Returns self, Set#add's own contract -- hegel-rust's own Pool::add returns nothing instead, since Rust has no builder-chaining idiom for this method to match.

Parameters:

  • value (Object)

Returns:



50
51
52
53
54
# File 'lib/hegel/stateful/pool.rb', line 50

def add(value)
  variable_id = @tc.pool_add(@pool)
  @values[variable_id] = value
  self
end

#empty?Boolean

True when no values are in the pool.

Returns:

  • (Boolean)


42
43
44
# File 'lib/hegel/stateful/pool.rb', line 42

def empty?
  @values.empty?
end

#sizeInteger

Number of values currently in the pool.

Returns:

  • (Integer)


37
38
39
# File 'lib/hegel/stateful/pool.rb', line 37

def size
  @values.size
end

#values_consumedValuesConsumed

A Hegel::Generator over this pool's values: drawing it removes the chosen value, so it is never drawn again.

Returns:



64
65
66
# File 'lib/hegel/stateful/pool.rb', line 64

def values_consumed
  ValuesConsumed.new(@pool, @values)
end

#values_reusableValuesReusable

A Hegel::Generator over this pool's values: drawing it leaves the chosen value in place, so the same value can be drawn again.

Returns:



58
59
60
# File 'lib/hegel/stateful/pool.rb', line 58

def values_reusable
  ValuesReusable.new(@pool, @values)
end