Module: CheapTest

Defined in:
lib/cheap_test.rb

Class Method Summary collapse

Class Method Details

.cheap_perm_check!(perm, s) ⇒ Object



7
8
9
10
11
# File 'lib/cheap_test.rb', line 7

def self.cheap_perm_check!(perm, s)
  return nil if length > 256
  CheapRandom::permute perm, s, 0, length
  CheapRandom::unpermute perm, s, 0, length
end

.identity_permObject



21
22
23
24
25
26
27
# File 'lib/cheap_test.rb', line 21

def self.identity_perm
  s = ' ' * 256
  (0..255).each do |x|
    s.setbyte x, x
  end
  s
end

.is_reversible?Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
# File 'lib/cheap_test.rb', line 42

def self.is_reversible?
  s = random_string(random(10000))
  x = s + 'X'
  ip = random_perm
  CheapRandom::cheap_random3!(true, ip, s)
  CheapRandom::cheap_random3!(false, ip, s)
  s == x[0...(s.length)]
end

.random(n) ⇒ Object



3
4
5
# File 'lib/cheap_test.rb', line 3

def self.random(n)
  rand(n)
end

.random_permObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cheap_test.rb', line 29

def self.random_perm
  s = identity_perm
  i = 256
  (0..255).each do |x|
    temp = s.getbyte x
    y = x + random(i)
    s.setbyte x, s.getbyte(y)
    s.setbyte y, temp
    i -= 1
  end
  s
end

.random_string(len) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/cheap_test.rb', line 13

def self.random_string(len)
  s = ' ' * len
  (0...len).each do |x|
    s.setbyte x, random(256)
  end
  s
end