Class: Pbt::Arbitrary::ArrayArbitrary
- Defined in:
- lib/pbt/arbitrary/array_arbitrary.rb
Overview
Generates arrays of values generated by ‘value_arb` with type `T`.
Instance Method Summary collapse
- #generate(rng) ⇒ Object
-
#initialize(value_arb, min_length = 0, max_length = DEFAULT_MAX_SIZE) ⇒ ArrayArbitrary
constructor
A new instance of ArrayArbitrary.
- #shrink(current) ⇒ Object
Methods inherited from Arbitrary
Constructor Details
permalink #initialize(value_arb, min_length = 0, max_length = DEFAULT_MAX_SIZE) ⇒ ArrayArbitrary
Returns a new instance of ArrayArbitrary.
13 14 15 16 17 18 19 20 |
# File 'lib/pbt/arbitrary/array_arbitrary.rb', line 13 def initialize(value_arb, min_length = 0, max_length = DEFAULT_MAX_SIZE) raise ArgumentError, "min_length must be zero or positive number" if min_length < 0 @min_length = min_length @max_length = max_length @value_arb = value_arb @length_arb = IntegerArbitrary.new(min_length, max_length) end |
Instance Method Details
permalink #generate(rng) ⇒ Object
23 24 25 26 |
# File 'lib/pbt/arbitrary/array_arbitrary.rb', line 23 def generate(rng) length = @length_arb.generate(rng) length.times.map { @value_arb.generate(rng) } end |
permalink #shrink(current) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/pbt/arbitrary/array_arbitrary.rb', line 29 def shrink(current) return Enumerator.new { |_| } if current.size == @min_length Enumerator.new do |y| # First, shrink the length of the array and try combinations of elements. # But this doesn't try all possible combinations since it'd be too huge and slow. @length_arb.shrink(current.size).each do |length| if length == 0 y.yield [] next end current.each_cons(length) do |con| y.yield con end end # Second, shrink each element of the array. current.each_with_index do |item, i| @value_arb.shrink(item).each do |val| y.yield [*current[...i], val, *current[i + 1..]] end end end end |