fedora-migrator /vendor/gems/facets-2.4.5/test/more/test_random.rb

Language Ruby Lines 147
MD5 Hash c563142c77565bd1eb358bc61560d9f1 Estimated Cost $2,738 (why?)
Repository https://bitbucket.org/mediashelf/fedora-migrator View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# Test for facets/random.rb

require 'facets/random.rb'
require 'test/unit'

class TestKernelRandom < Test::Unit::TestCase

  def test_maybe
    assert_nothing_raised { maybe }
  end

end

class TestArrayRandom < Test::Unit::TestCase

  def test_at_rand
    a = [1,2,3,4,5]
    20.times{ assert_nothing_raised{ a.at_rand } }
    20.times{ assert( a.include?( a.at_rand ) ) }
  end

  def test_at_rand!
    a = ['a','b','c']
    assert_equal( 1, a.at_rand!.length )
    assert_equal( 2, a.length )
  end

  def test_pick
    a = ['a','b','c']
    assert_equal( 3, a.pick(3).length )
    assert_equal( 3, a.length )
    a = ['a','b','c']
    assert_equal( 3, a.pick(4).length )
    assert_equal( 3, a.length )
  end

  def test_pick!
    a = ['a','b','c']
    assert_equal( 3, a.pick!(3).length )
    assert_equal( 0, a.length )
    a = ['a','b','c']
    assert_equal( 3, a.pick!(4).length )
    assert_equal( 0, a.length )
  end

  def test_rand_index
    10.times {
      i = [1,2,3].rand_index
      assert( (0..2).include?(i) )
    }
  end

  def test_rand_subset
    10.times {
      a = [1,2,3,4].rand_subset
      assert( a.size <= 4 )
    }
  end

  def test_shuffle
    a = [1,2,3,4,5]
    b = a.shuffle
    assert_equal( a, b.sort )
  end

  def test_shuffle!
    a = [1,2,3,4,5]
    b = a.dup
    b.shuffle!
    assert_equal( a, b.sort )
  end

end

class TestHashRandom < Test::Unit::TestCase

  def test_rand_key
    h = { :a=>1, :b=>2, :c=>3 }
    10.times { assert( h.keys.include?( h.rand_key ) ) }
  end

  def test_rand_pair
    h = { :a=>1, :b=>2, :c=>3 }
    10.times { k,v = *h.rand_pair; assert_equal( v, h[k] ) }
  end

  def test_rand_value
    h = { :a=>1, :b=>2, :c=>3 }
    10.times { assert( h.values.include?( h.rand_value ) ) }
  end

  def test_shuffle
    h = {:a=>1, :b=>2, :c=>3 }
    assert_nothing_raised { h.shuffle }
  end

  def test_shuffle!
    h = {:a=>1, :b=>2, :c=>3 }
    assert_nothing_raised { h.shuffle! }
  end

end

class TestStringRandom < Test::Unit::TestCase

  def test_String_rand_letter
    100.times { |i| assert( /[a-zA-z]/ =~ String.rand_letter ) }
  end

  def test_at_rand
    a = '12345'
    20.times{ assert_nothing_raised{ a.at_rand } }
    20.times{ assert( a.include?( a.at_rand ) ) }
  end

  def test_at_rand!
    x = 'ab'
    r = x.at_rand!
    assert( r == 'a' || r == 'b' )
    assert( x == 'a' || x == 'b' )
  end

  def test_rand_index
    10.times { assert( (0..2).include?( 'abc'.rand_index ) ) }
  end

  def test_rand_byte
    s = 'ab'
    r = s.rand_byte
    assert( r == 97 || r == 98 )
  end

  def test_rand_byte_again
    s = 'ab'
    r = s.rand_byte
    assert( r == 97 || r == 98 )
    assert( s = 'a' || s = 'b' )
  end

  def test_shuffle
    assert_nothing_raised { "abc 123".shuffle }
    #assert_nothing_raised { "abc 123".shuffle! }
  end

end
Back to Top