Class: Fuzzer
Instance Method Summary collapse
- #fuzz(current = nil) ⇒ Object
-
#initialize(n, freqs = {}) ⇒ Fuzzer
constructor
A new instance of Fuzzer.
- #make_pick ⇒ Object
- #pick ⇒ Object
- #random_string ⇒ Object
Constructor Details
#initialize(n, freqs = {}) ⇒ Fuzzer
Returns a new instance of Fuzzer.
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/gems/json_pure-1.1.3/tools/fuzz.rb', line 13 def initialize(n, freqs = {}) sum = freqs.inject(0.0) { |s, x| s + x.last } freqs.each_key { |x| freqs[x] /= sum } s = 0.0 freqs.each_key do |x| freqs[x] = s .. (s + t = freqs[x]) s += t end @freqs = freqs @n = n @alpha = (0..0xff).to_a end |
Instance Method Details
#fuzz(current = nil) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/gems/json_pure-1.1.3/tools/fuzz.rb', line 54 def fuzz(current = nil) if @n > 0 case current when nil @n -= 1 current = fuzz [ Hash, Array ][rand(2)].new when Array while @n > 0 @n -= 1 current << case p = make_pick when Array, Hash fuzz(p) else p end end when Hash while @n > 0 @n -= 1 current[random_string] = case p = make_pick when Array, Hash fuzz(p) else p end end end end current end |
#make_pick ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/gems/json_pure-1.1.3/tools/fuzz.rb', line 38 def make_pick k = pick case when k == Hash, k == Array k.new when k == true, k == false, k == nil k when k == String random_string when k == Fixnum rand(2 ** 30) - 2 ** 29 when k == Bignum rand(2 ** 70) - 2 ** 69 end end |
#pick ⇒ Object
32 33 34 35 36 |
# File 'lib/gems/json_pure-1.1.3/tools/fuzz.rb', line 32 def pick r = rand found = @freqs.find { |k, f| f.include? rand } found && found.first end |
#random_string ⇒ Object
26 27 28 29 30 |
# File 'lib/gems/json_pure-1.1.3/tools/fuzz.rb', line 26 def random_string s = '' 30.times { s << @alpha[rand(@alpha.size)] } s.to_utf8 end |