module RubyCheck

RubyCheck - a Ruby port of the QuickCheck unit test framework

See www.yellosoft.us/quickcheck for an introduction to QuickCheck property testing.

RubyCheck defines test case generators for several basic Ruby types, and encourages monkeypatching for defining generators for custom types.

RubyCheck

Constants

TRIALS

Number of test cases to generate per for_all run

VERSION

VERSION is defined once, available to gemspec during packaging, and available programmatically to Ruby code.

Public Class Methods

for_all(property, gen_syms) click to toggle source

Evaluates property, TRIALS times, over random inputs generated by gen_syms.

Returns true, or the first failing test case.

Example:

RubyCheck::for_all( ->(i) { i.even? }, [:gen_uint])
=> [9]

RubyCheck::for_all( ->(i) { i.even? || i.odd? }, [:gen_uint])
=> true
# File lib/rubycheck.rb, line 180
def self.for_all(property, gen_syms)
  TRIALS.times do ||
    test_case = gen_syms.map { |gen_sym| send(gen_sym) }
    fail PropertyFailure.new(test_case), 'test case error' unless property.call(*test_case)
  end
rescue PropertyFailure => e
  e.test_case
else
  true
end
gen_array(gen_sym) click to toggle source

Generate a random array, populated with values generated with gen_sym.

Example:

RubyCheck::gen_array(:gen_uint)
=> [1, 3, 3, 7]
# File lib/rubycheck.rb, line 122
def self.gen_array(gen_sym)
  len = gen_uint % 100

  0.upto(len).map { || send(gen_sym) }
end
gen_bool() click to toggle source

Generate a random boolean.

Example:

RubyCheck::gen_bool
=> false
# File lib/rubycheck.rb, line 25
def self.gen_bool
  Random.rand < 0.5
end
gen_byte() click to toggle source

Generate a random byte in [0, 255].

Example:

RubyCheck::gen_byte
=> 96
# File lib/rubycheck.rb, line 96
def self.gen_byte
  gen_uint % 256
end
gen_char() click to toggle source

Generate a random ASCII character.

Example:

RubyCheck::gen_char
=> "Q"
# File lib/rubycheck.rb, line 109
def self.gen_char
  (gen_uint % 128).chr
end
gen_float() click to toggle source

Generate a random float in [0, 10^10 - 1].

Example:

RubyCheck::gen_float
=> 0.223
# File lib/rubycheck.rb, line 38
def self.gen_float
  Random.rand(10e10)
end
gen_int() click to toggle source

Generate a random integer in [(-1 * 10^10) + 1, 10^10 - 1].

Example:

RubyCheck::gen_int
=> -4
# File lib/rubycheck.rb, line 77
def self.gen_int
  i = Random.rand(10e10).to_i

  if gen_bool
    i
  else
    i * -1
  end
end
gen_str() click to toggle source

Generate a random ASCII string.

Example:

RubyCheck::gen_str
=> "qwerty123!@#"
# File lib/rubycheck.rb, line 137
def self.gen_str
  gen_array(:gen_char).join('')
end
gen_uint() click to toggle source

Generate a random unsigned integer in [0, 10^10 - 1].

Example:

RubyCheck::gen_uint
=> 4
# File lib/rubycheck.rb, line 51
def self.gen_uint
  Random.rand(10e10).to_i
end
gen_uint16() click to toggle source

Generate a random unsigned integer in [0, 2^16 - 1].

Example:

RubyCheck::gen_uint16
=> 4
# File lib/rubycheck.rb, line 64
def self.gen_uint16
  Random.rand(65535).to_i
end