Class: Pbt::Arbitrary::IntegerArbitrary
- Defined in:
- lib/pbt/arbitrary/integer_arbitrary.rb
Overview
Generates random integers between ‘min` and `max`.
Instance Method Summary collapse
- #generate(rng) ⇒ Object
-
#initialize(min, max) ⇒ IntegerArbitrary
constructor
A new instance of IntegerArbitrary.
- #shrink(current, target: nil) ⇒ Object
Methods inherited from Arbitrary
Constructor Details
#initialize(min, max) ⇒ IntegerArbitrary
Returns a new instance of IntegerArbitrary.
12 13 14 15 |
# File 'lib/pbt/arbitrary/integer_arbitrary.rb', line 12 def initialize(min, max) @min = min @max = max end |
Instance Method Details
#generate(rng) ⇒ Object
18 19 20 |
# File 'lib/pbt/arbitrary/integer_arbitrary.rb', line 18 def generate(rng) rng.rand(@min..@max) end |
#shrink(current, target: nil) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/pbt/arbitrary/integer_arbitrary.rb', line 23 def shrink(current, target: nil) # If no target is specified, use the appropriate bound as target target ||= DEFAULT_TARGET.clamp(@min, @max) # Ensure target is within bounds target = target.clamp(@min, @max) gap = current - target return Enumerator.new { |_| } if gap == 0 is_positive_gap = gap > 0 Enumerator.new do |y| while (diff = (current - target).abs) > 1 halved = diff / 2 current -= is_positive_gap ? halved : -halved # Ensure current stays within bounds current = current.clamp(@min, @max) y.yield current end # Only yield target if it's different from current if current != target y.yield target end end end |