Ignore:
Timestamp:
Jul 9, 2020, 8:51:43 AM (4 years ago)
Author:
coas-nagasima
Message:

mrubyを2.1.1に更新

Location:
EcnlProtoTool/trunk/mruby-2.1.1
Files:
1 edited
1 moved

Legend:

Unmodified
Added
Removed
  • EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-random/test/random.rb

    r331 r439  
    22# Random Test
    33
    4 assert("Random#srand") do
     4assert("Random.new") do
    55  r1 = Random.new(123)
    66  r2 = Random.new(123)
    7   r1.rand == r2.rand
     7  r3 = Random.new(124)
     8  assert_equal(r1.rand, r2.rand)
     9  assert_not_equal(r1.rand, r3.rand)
    810end
    911
    10 assert("Kernel::srand") do
     12assert("Kernel.srand") do
    1113  srand(234)
    1214  r1 = rand
    1315  srand(234)
    1416  r2 = rand
    15   r1 == r2
     17  srand(235)
     18  r3 = rand
     19  assert_equal(r1, r2)
     20  assert_not_equal(r1, r3)
    1621end
    1722
    18 assert("Random::srand") do
     23assert("Random.srand") do
    1924  Random.srand(345)
    2025  r1 = rand
    2126  srand(345)
    2227  r2 = Random.rand
    23   r1 == r2
     28  Random.srand(346)
     29  r3 = rand
     30  assert_equal(r1, r2)
     31  assert_not_equal(r1, r3)
    2432end
    2533
    26 assert("fixnum") do
    27   rand(3).class == Fixnum
    28 end
    29 
    30 assert("float") do
    31   rand.class == Float
     34assert("return class of Kernel.rand") do
     35  assert_kind_of(Fixnum, rand(3))
     36  assert_kind_of(Fixnum, rand(1.5))
     37  assert_kind_of(Float, rand)
     38  assert_kind_of(Float, rand(0.5))
    3239end
    3340
    3441assert("Array#shuffle") do
    35   ary = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
     42  orig = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
     43  ary = orig.dup
    3644  shuffled = ary.shuffle
    37 
    38   ary == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] and shuffled != ary and 10.times { |x| ary.include? x }
     45  assert_equal(orig, ary)
     46  assert_not_equal(ary, shuffled)
     47  assert_equal(orig, shuffled.sort)
    3948end
    4049
    4150assert('Array#shuffle!') do
    42   ary = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    43   ary.shuffle!
    44 
    45   ary != [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] and 10.times { |x| ary.include? x }
     51  orig = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
     52  ary = orig.dup
     53  assert_same(ary, ary.shuffle!)
     54  assert_not_equal(orig, ary)
     55  assert_equal(orig, ary.sort)
    4656end
    4757
     
    5363
    5464  # verify that the same seed causes the same results
    55   ary1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    56   shuffle1 = ary1.shuffle Random.new 345
    57   ary2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    58   shuffle2 = ary2.shuffle Random.new 345
    59 
    60   ary1 != shuffle1 and 10.times { |x| shuffle1.include? x } and shuffle1 == shuffle2
     65  ary = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
     66  shuffled1 = ary.shuffle Random.new 345
     67  shuffled2 = ary.shuffle Random.new 345
     68  shuffled3 = ary.shuffle Random.new 346
     69  assert_equal(shuffled1, shuffled2)
     70  assert_not_equal(shuffled1, shuffled3)
    6171end
    6272
     
    7282  ary2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    7383  ary2.shuffle! Random.new 345
    74 
    75   ary1 != [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] and 10.times { |x| ary1.include? x } and ary1 == ary2
     84  ary3 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
     85  ary3.shuffle! Random.new 346
     86  assert_equal(ary1, ary2)
     87  assert_not_equal(ary1, ary3)
    7688end
    7789
    78 assert('Array#sample checks input length after reading arguments') do
    79   $ary = [1, 2, 3]
    80   class ArrayChange
    81     def to_i
    82       $ary << 4
    83       4
     90assert('Array#sample') do
     91  100.times do
     92    assert_include([0, 1, 2], [2, 1, 0].sample)
     93    [2, 1, 0].sample(2).each { |sample| assert_include([0, 1, 2], sample) }
     94    h = {}
     95    (1..10).to_a.sample(7).each do |sample|
     96      assert_not_include(h, sample)
     97      h[sample] = true
    8498    end
    8599  end
    86100
    87   assert_equal [1, 2, 3, 4], $ary.sample(ArrayChange.new).sort
     101  assert_nil([].sample)
     102  assert_equal([], [].sample(1))
     103  assert_equal([], [2, 1].sample(0))
     104  assert_raise(TypeError) { [2, 1].sample(true) }
     105  assert_raise(ArgumentError) { [2, 1].sample(-1) }
    88106end
     107
     108assert('Array#sample(random)') do
     109  assert_raise(TypeError) do
     110    # this will cause an exception due to the wrong argument
     111    [1, 2].sample(2, "Not a Random instance")
     112  end
     113
     114  # verify that the same seed causes the same results
     115  ary = (1..10).to_a
     116  srand(15)
     117  samples1 = ary.sample(4)
     118  samples2 = ary.sample(4, Random.new(15))
     119  samples3 = ary.sample(4, Random.new(16))
     120  assert_equal(samples1, samples2)
     121  assert_not_equal(samples1, samples3)
     122end
Note: See TracChangeset for help on using the changeset viewer.