Class: RandomArray

Inherits:
Array
  • Object
show all
Extended by:
RushCheck::Arbitrary
Includes:
RushCheck::Coarbitrary
Defined in:
lib/rushcheck/array.rb

Overview

class RandomArray acts a random generator of arrays. Programmer can make a subclass of RandomArray to get user defined generators.

Direct Known Subclasses

NonEmptyRandomArray

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RushCheck::Arbitrary

arbitrary

Class Method Details

.arbitraryObject



64
65
66
67
68
69
70
71
# File 'lib/rushcheck/array.rb', line 64

def self.arbitrary
  self.arrange_len do |len|
    if len == 0
    then RushCheck::Gen.unit([])
    else self.create_array(len)
    end
  end
end

.arrange_lenObject

a private method for self.arbitrary



55
56
57
58
59
60
61
62
# File 'lib/rushcheck/array.rb', line 55

def self.arrange_len
  RushCheck::Gen.sized do |m|
    m = 1 - m if m <= 0
    RushCheck::Gen.choose(0, m).bind do |len|
      yield len
    end
  end
end

.create_array(len) ⇒ Object

a private method for self.arbitrary



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rushcheck/array.rb', line 41

def self.create_array(len)
  RushCheck::Gen.new do |n, r|
    ary = [self.create_components(@@base, n, r)]
    r2 = r
    (1..len).each do |i|
      r1, r2 = r2.split
      ary << self.create_components(@@indp.call(ary, i), n, r1)
    end
    
    ary
  end
end

.create_components(cs, n, r) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/rushcheck/array.rb', line 29

def self.create_components(cs, n, r)
  case cs
  when Class
    cs.arbitrary.value(n, r)
  when Array
    cs.map {|c| self.create_components(c, n, r)}
  else
    raise RuntimeError, "Unexpected arguments #{cs.inspect}. Maybe forgotten calling set_pattern before?"
  end
end

.set_pattern(base, &f) ⇒ Object

self.set_pattern must be executed before calling self.arbitrary. self.set_pattern defines pattern of random arrays for self.arbitrary. self.set_pattern takes a variable and a block, where the variable should be a class which is used for the first element of random array. On the other hand, the block should define random array by inductive way; the block takes two variables and the first variable is assumed as an array and the second variable is the index of array.



24
25
26
27
# File 'lib/rushcheck/array.rb', line 24

def self.set_pattern(base, &f)
  @@base, @@indp = base, f
  nil
end

Instance Method Details

#coarbitrary(g) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/rushcheck/array.rb', line 73

def coarbitrary(g)
  r = g.variant(0)
  each do |c|
    r = c.coarbitrary(r.variant(1))
  end
  r
end