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: DEFAULT_TARGET) ⇒ Object
Methods inherited from Arbitrary
Constructor Details
permalink #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
permalink #generate(rng) ⇒ Object
18 19 20 |
# File 'lib/pbt/arbitrary/integer_arbitrary.rb', line 18 def generate(rng) rng.rand(@min..@max) end |
permalink #shrink(current, target: DEFAULT_TARGET) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/pbt/arbitrary/integer_arbitrary.rb', line 23 def shrink(current, target: DEFAULT_TARGET) 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 y.yield current end y.yield target # no diff here end end |