source: EcnlProtoTool/trunk/mruby-1.3.0/mrbgems/mruby-random/test/random.rb@ 331

Last change on this file since 331 was 331, checked in by coas-nagasima, 6 years ago

prototoolに関連するプロジェクトをnewlibからmuslを使うよう変更・更新
ntshellをnewlibの下位の実装から、muslのsyscallの実装に変更・更新
以下のOSSをアップデート
・mruby-1.3.0
・musl-1.1.18
・onigmo-6.1.3
・tcc-0.9.27
以下のOSSを追加
・openssl-1.1.0e
・curl-7.57.0
・zlib-1.2.11
以下のmrbgemsを追加
・iij/mruby-digest
・iij/mruby-env
・iij/mruby-errno
・iij/mruby-iijson
・iij/mruby-ipaddr
・iij/mruby-mock
・iij/mruby-require
・iij/mruby-tls-openssl

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-ruby;charset=UTF-8
File size: 1.9 KB
Line 
1##
2# Random Test
3
4assert("Random#srand") do
5 r1 = Random.new(123)
6 r2 = Random.new(123)
7 r1.rand == r2.rand
8end
9
10assert("Kernel::srand") do
11 srand(234)
12 r1 = rand
13 srand(234)
14 r2 = rand
15 r1 == r2
16end
17
18assert("Random::srand") do
19 Random.srand(345)
20 r1 = rand
21 srand(345)
22 r2 = Random.rand
23 r1 == r2
24end
25
26assert("fixnum") do
27 rand(3).class == Fixnum
28end
29
30assert("float") do
31 rand.class == Float
32end
33
34assert("Array#shuffle") do
35 ary = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
36 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 }
39end
40
41assert('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 }
46end
47
48assert("Array#shuffle(random)") do
49 assert_raise(TypeError) do
50 # this will cause an exception due to the wrong argument
51 [1, 2].shuffle "Not a Random instance"
52 end
53
54 # 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
61end
62
63assert('Array#shuffle!(random)') do
64 assert_raise(TypeError) do
65 # this will cause an exception due to the wrong argument
66 [1, 2].shuffle! "Not a Random instance"
67 end
68
69 # verify that the same seed causes the same results
70 ary1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
71 ary1.shuffle! Random.new 345
72 ary2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
73 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
76end
77
78assert('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
84 end
85 end
86
87 assert_equal [1, 2, 3, 4], $ary.sample(ArrayChange.new).sort
88end
Note: See TracBrowser for help on using the repository browser.