Class: Hash
- Defined in:
- lib/propr/random/hash.rb,
lib/propr/shrink/hash.rb
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.random(options = {}, m = Propr::Random) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/propr/random/hash.rb', line 21 def random( = {}, m = Propr::Random) min = [:min] || 0 max = [:max] || 10 pair = yield # @todo: Be sure we created enough *unique* keys # # Hash.random(min: 10) do # # key space could have at most 6 elements # m.sequence([Integer.random(min: 0, max: 5), String.random]) # end # m.bind(Integer.random(.merge(min: min, max: max))) do |size| m.bind(m.sequence([pair]*size)) do |pairs| m.unit(Hash[pairs]) end end end |
.random_vals(hash, m = Propr::Random) ⇒ Object
46 47 48 49 50 51 52 53 54 |
# File 'lib/propr/random/hash.rb', line 46 def random_vals(hash, m = Propr::Random) # Convert hash of key => generator to a list of pair-generators, # where the pairs correspond to the original set of [key, value] pairs = hash.map{|k,v| m.bind(v){|v| m.unit([k, v]) }} m.bind(m.sequence(pairs)) do |pairs| m.unit(Hash[pairs]) end end |
Instance Method Details
#random(m = Propr::Random) ⇒ Object
2 3 4 5 6 |
# File 'lib/propr/random/hash.rb', line 2 def random(m = Propr::Random) m.bind(keys.random) do |k| m.unit([k, self[k]]) end end |
#shrink ⇒ Array<Hash>
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/propr/shrink/hash.rb', line 3 def shrink return Array.new if empty? array = to_a array.combination(size - 1).map{|pairs| Hash[pairs] }.tap do |shrunken| shrunken << Hash.new size.times do |n| head = array[0, n] tail = array[n+1..-1] k, v = array[n] shrunken.concat(v.shrink.map{|m| Hash[head + [[k, m]] + tail] }) end end end |