- Timestamp:
- Jul 9, 2020, 8:51:43 AM (4 years ago)
- 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 2 2 # Random Test 3 3 4 assert("Random #srand") do4 assert("Random.new") do 5 5 r1 = Random.new(123) 6 6 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) 8 10 end 9 11 10 assert("Kernel ::srand") do12 assert("Kernel.srand") do 11 13 srand(234) 12 14 r1 = rand 13 15 srand(234) 14 16 r2 = rand 15 r1 == r2 17 srand(235) 18 r3 = rand 19 assert_equal(r1, r2) 20 assert_not_equal(r1, r3) 16 21 end 17 22 18 assert("Random ::srand") do23 assert("Random.srand") do 19 24 Random.srand(345) 20 25 r1 = rand 21 26 srand(345) 22 27 r2 = Random.rand 23 r1 == r2 28 Random.srand(346) 29 r3 = rand 30 assert_equal(r1, r2) 31 assert_not_equal(r1, r3) 24 32 end 25 33 26 assert("fixnum") do 27 rand(3).class == Fixnum 28 end 29 30 assert("float") do 31 rand.class == Float 34 assert("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)) 32 39 end 33 40 34 41 assert("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 36 44 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) 39 48 end 40 49 41 50 assert('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) 46 56 end 47 57 … … 53 63 54 64 # verify that the same seed causes the same results 55 ary 1= [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]56 shuffle 1 = ary1.shuffle Random.new 34557 ary2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]58 shuffle 2 = ary2.shuffle Random.new 34559 60 a ry1 != shuffle1 and 10.times { |x| shuffle1.include? x } and shuffle1 == shuffle265 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) 61 71 end 62 72 … … 72 82 ary2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 73 83 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) 76 88 end 77 89 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 90 assert('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 84 98 end 85 99 end 86 100 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) } 88 106 end 107 108 assert('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) 122 end
Note:
See TracChangeset
for help on using the changeset viewer.