Class: SpecialString

Inherits:
String show all
Defined in:
lib/rushcheck/string.rb

Overview

class SpecialString is a subclass of String. SpecialString provides another generator which prefers control codes and special unprinted codes than usual alphabets or numbers. This class maybe useful to find a counter example efficiently than using the standard generator of String.

Constant Summary collapse

@@alphabet =

ASCII code (see man ascii)

(65..90).to_a + (97..122).to_a
@@control =
(0..32).to_a + [177]
@@number =
(48..57).to_a
@@special =
[[33,47],[58,64],[91,96],[123,126]].inject([]) do |ary, pair|
  lo, hi = pair
  ary = ary + (lo..hi).to_a 
end
@@frequency =
{ 'alphabet' => 3,
'control'  => 10,
'number'   => 2,
'special'  => 5 }

Class Method Summary collapse

Methods inherited from String

bound, #coarbitrary, random_range

Methods included from RushCheck::Arbitrary

#arbitrary

Methods included from RushCheck::HsRandom

#random, #random_array, #random_std

Methods included from RushCheck::Coarbitrary

#coarbitrary

Class Method Details

.arbitraryObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rushcheck/string.rb', line 77

def self.arbitrary
  f = @@frequency
  frq = []
  [f['alphabet'],f['control'],f['number'],f['special']].zip(
  [ @@alphabet,   @@control,   @@number,   @@special]) do 
    |weight, table|
      gen = RushCheck::Gen.oneof(table.map {|n| RushCheck::Gen.unit(n.chr)})
      frq << [weight, gen]
  end

  RushCheck::Gen.sized do |m|
    RushCheck::Gen.choose(0, m).bind do |len|
      RushCheck::Gen.new do |n, r|
        r2 = r
        (1..len).map do
          r1, r2 = r2.split
          RushCheck::Gen.frequency(frq).value(n, r1)
        end.join
      end
    end
  end
end