Class: QueenCheck::Gen

Inherits:
Object
  • Object
show all
Defined in:
lib/queencheck/gen.rb

Constant Summary collapse

DEFAULT_BOUND =
1000

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) {|p, r| ... } ⇒ Gen

Returns a new instance of Gen.

Parameters:

  • options (Hash) (defaults to: {})

    generater options

Options Hash (options):

Yields:

  • (p, r)

    progress and random int



12
13
14
15
16
17
18
# File 'lib/queencheck/gen.rb', line 12

def initialize(options = {}, &block)
  @proc = block

  @bound_min  = options['min'] || options[:min] || 0
  @bound_max  = options['max'] || options[:max] || DEFAULT_BOUND
  @conditions = options['conditions'] || options[:conditions] || []
end

Class Method Details

.choose(lo, hi) ⇒ QueenCheck::Gen

choose value where >= lo and <= hi

Examples:

QueenCheck::Gen.choose(0, 100)

Parameters:

Returns:



127
128
129
# File 'lib/queencheck/gen.rb', line 127

def self.choose(lo, hi)
  elements_of((lo .. hi).to_a)
end

.elements_of(ary) ⇒ QueenCheck::Gen

choose value from ary

Examples:

QueenCheck::Gen.elements_of([0, 1, 2])

Parameters:

  • ary (Array)

Returns:



136
137
138
139
140
141
142
143
# File 'lib/queencheck/gen.rb', line 136

def self.elements_of(ary)
  new({
    :min => 0,
    :max => ary.size
  }) { | p, r |
    ary[r]
  }
end

.frequency(ary) ⇒ QueenCheck::Gen

frequency generater

Examples:

QueenCheck::Gen.frequency([
  [1, QueenCheck::Gen.elements_of(['a', 'b', 'c'])],
  [2, QueenCheck::Gen.elements_of(['1', '2', '3'])]
])
# propability of 1/3 => 'a', 'b' or 'c'
# propability of 2/3 => '1', '2' or '3'

Parameters:

Returns:

Raises:

  • (ArgumentsError)


172
173
174
175
176
177
178
179
180
181
182
# File 'lib/queencheck/gen.rb', line 172

def self.frequency(ary)
  generaters = []
  ary.each do | pair |
    pair[0].times do 
      generaters << pair[1]
    end
  end
  raise ArgumentsError, "frequency: illigal weight total N > 0" if generaters.empty?

  one_of(generaters)
end

.one_of(ary) ⇒ QueenCheck::Gen

one of generater

Examples:

QueenCheck::Gen.one_of([
  QueenCheck::Gen.elements_of([0, 1, 3]),
  QueenCheck::Gen.elements_of(["A", "B", "C"])
])

Parameters:

Returns:



153
154
155
156
157
158
159
160
# File 'lib/queencheck/gen.rb', line 153

def self.one_of(ary)
  elements_of(ary).bind { | gen |
    gen.instance_of?(QueenCheck::Gen) ? gen : (
      gen.instance_of?(QueenCheck::Arbitrary) ? 
        gen.gen : QueenCheck::Arbitrary(gen).gen
    )
  }
end

.progressQueenCheck::Gen

progress generater

Returns:



208
209
210
211
212
# File 'lib/queencheck/gen.rb', line 208

def self.progress
  new { | p, r |
    p
  }
end

.quadratic(a, b = 1, c = 0) ⇒ QueenCheck::Gen

quadratic function (general form) generater f(x) = ax^2 + bx + c

Parameters:

  • a (Integer)

    factor a

  • b (Integer) (defaults to: 1)

    facror b

  • c (Integer) (defaults to: 0)

    factor c

Returns:



248
249
250
251
252
# File 'lib/queencheck/gen.rb', line 248

def self.quadratic(a, b = 1, c = 0)
  new { |p, r|
    (a * (p ** 2)) + (b * p) + c
  }
end

.randQueenCheck::Gen

random generater

Examples:

QueenCheck::Gen.rand.resize(-100, 100)

Returns:



188
189
190
191
192
# File 'lib/queencheck/gen.rb', line 188

def self.rand
  new { | p, r |
    r
  }
end

.step_up(ary) ⇒ QueenCheck::Gen

step up generater

Examples:

QueenCheck::Gen.step_up([
  [1, QueenCheck::Gen.elements_of(['a', 'b', 'c'])],
  [2, QueenCheck::Gen.elements_of(['A', 'B', 'C'])]
])

Parameters:

  • ary (Array<[weight, Gen]>)

Returns:



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/queencheck/gen.rb', line 222

def self.step_up(ary)
  total = ary.inject(0) { | total, pair | total + pair.first }

  self.progress.bind { |p, r|
    index = total * p

    gen = nil
    ary.each do | pair |
      gen = pair.last 
      break if index <= pair.first
      index -= pair.first
    end

    gen.instance_of?(QueenCheck::Gen) ? gen : (
      gen.instance_of?(QueenCheck::Arbitrary) ? 
        gen.gen : QueenCheck::Arbitrary(gen).gen
    )
  }
end

.unit(n) ⇒ QueenCheck::Gen

unit generater

Examples:

QueenCheck::Gen.unit(1)
# always generate 1

Parameters:

  • n (Object)

    any object

Returns:



200
201
202
203
204
# File 'lib/queencheck/gen.rb', line 200

def self.unit(n)
  new {
    n
  }
end

Instance Method Details

#bind {|x| ... } ⇒ QueenCheck::Gen

bind function

Yields:

  • (x)

    x is generated value

Yield Returns:

Returns:



71
72
73
74
75
# File 'lib/queencheck/gen.rb', line 71

def bind
  self.class.new(option) { | p, r |
    yield(value(p)[0]).value(p)[0]
  }
end

#fmap {|x| ... } ⇒ QueenCheck::Gen

Yields:

  • (x)

    x is generated value

Yield Returns:

  • (Object)

Returns:



80
81
82
83
84
# File 'lib/queencheck/gen.rb', line 80

def fmap
  bind { | x |
    self.class.unit(yield(x))
  }
end

#inspectObject



49
50
51
# File 'lib/queencheck/gen.rb', line 49

def inspect
  "<QueenCheck::Gen: {#{@proc.to_s}}>"
end

#optionHash

Returns option.

Returns:

  • (Hash)

    option



40
41
42
43
44
45
46
# File 'lib/queencheck/gen.rb', line 40

def option
  {
    :min        => @bound_min,
    :max        => @bound_max,
    :conditions => @conditions
  }
end

#randomInteger

Returns random int in @bound_min .. @bound_max.

Returns:

  • (Integer)

    random int in @bound_min .. @bound_max



35
36
37
# File 'lib/queencheck/gen.rb', line 35

def random
  (@bound_min + rand * (@bound_max - @bound_min)).to_i
end

#resize(lo, hi) ⇒ QueenCheck::Gen

resize bound of generater

Examples:

QueenCheck::Gen.rand.resize(-100, 100) == QueenCheck::Gen.choose(-100, 100)

Parameters:

Returns:



60
61
62
63
64
65
# File 'lib/queencheck/gen.rb', line 60

def resize(lo, hi)
  self.class.new(option.merge({
    :min => lo,
    :max => hi
  }), &@proc)
end

#value(progress) ⇒ Array<Any, Boolean>

get value form generater

Parameters:

  • progress (Float)

    0 .. 1

Returns:

  • (Array<Any, Boolean>)

    Any Value & Condition Matched



24
25
26
27
28
29
30
31
32
# File 'lib/queencheck/gen.rb', line 24

def value(progress)
  v = @proc.call(progress, random)

  cond = @conditions.inject(true) { | bool, cond |
    bool && cond.match?(v)
  }

  return [v, cond]
end

#where(conditions) ⇒ QueenCheck::Gen #where {|value| ... } ⇒ QueenCheck::Gen

set conditions

Examples:

QueenCheck::Gen.elements_of(['a', 'b', 'c']).where(
  :include? => ['1', 'a', 'A'],
  :equal? => 'a'
)

QueenCheck::Gen.choose(0, 100).where { |v|
  v.odd?
}

Overloads:

  • #where(conditions) ⇒ QueenCheck::Gen

    Parameters:

    • conditions (Hash)

      { conditon_name => condition_param }

    Options Hash (conditions):

  • #where {|value| ... } ⇒ QueenCheck::Gen

    Yields:

    • (value)

      value is generated value

    Yield Returns:

Returns:

See Also:



108
109
110
111
112
113
114
115
116
117
# File 'lib/queencheck/gen.rb', line 108

def where(conditions = {}, &block)
  self.class.new(option.merge({
    :conditions => @conditions + conditions.to_a.map { | el |
      cond = el[0].to_s.sub(/([^\?])$/){$1 + '?'}
      QueenCheck::Condition.method(cond).call(el[1])
    } + (
      !block.nil? ? [QueenCheck::Condition.new(&block)] : []
    )
  }), &@proc)
end