Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mruby
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
mruby
Commits
2f6baf60
Unverified
Commit
2f6baf60
authored
Apr 18, 2019
by
Yukihiro "Matz" Matsumoto
Committed by
GitHub
Apr 18, 2019
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #4387 from shuujii/add-Array-sample-test
Add `Array#sample` test
parents
77c42988
2ae727ac
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
40 additions
and
14 deletions
+40
-14
mrbgems/mruby-random/test/random.rb
mrbgems/mruby-random/test/random.rb
+40
-14
No files found.
mrbgems/mruby-random/test/random.rb
View file @
2f6baf60
...
...
@@ -39,28 +39,20 @@ assert("return class of Kernel.rand") do
end
assert
(
"Array#shuffle"
)
do
ary
=
[
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
]
orig
=
ary
.
dup
orig
=
[
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
]
ary
=
orig
.
dup
shuffled
=
ary
.
shuffle
assert_equal
(
orig
,
ary
)
assert_not_equal
(
ary
,
shuffled
)
assert_equal
(
ary
.
size
,
shuffled
.
size
)
shuffled
.
each
do
|
x
|
assert_include
(
ary
,
x
)
ary
.
delete
(
x
)
end
assert_equal
(
orig
,
shuffled
.
sort
)
end
assert
(
'Array#shuffle!'
)
do
ary
=
[
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
]
orig
=
ary
.
dup
orig
=
[
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
]
ary
=
orig
.
dup
assert_same
(
ary
,
ary
.
shuffle!
)
assert_not_equal
(
orig
,
ary
)
assert_equal
(
orig
.
size
,
ary
.
size
)
ary
.
each
do
|
x
|
assert_include
(
orig
,
x
)
orig
.
delete
(
x
)
end
assert_equal
(
orig
,
ary
.
sort
)
end
assert
(
"Array#shuffle(random)"
)
do
...
...
@@ -94,3 +86,37 @@ assert('Array#shuffle!(random)') do
assert_equal
(
ary1
,
ary2
)
assert_not_equal
(
ary1
,
ary3
)
end
assert
(
'Array#sample'
)
do
100
.
times
do
assert_include
([
0
,
1
,
2
],
[
2
,
1
,
0
].
sample
)
[
2
,
1
,
0
].
sample
(
2
).
each
{
|
sample
|
assert_include
([
0
,
1
,
2
],
sample
)
}
h
=
{}
(
1
..
10
).
to_a
.
sample
(
7
).
each
do
|
sample
|
assert_not_include
(
h
,
sample
)
h
[
sample
]
=
true
end
end
assert_nil
([].
sample
)
assert_equal
([],
[].
sample
(
1
))
assert_equal
([],
[
2
,
1
].
sample
(
0
))
assert_raise
(
TypeError
)
{
[
2
,
1
].
sample
(
true
)
}
assert_raise
(
ArgumentError
)
{
[
2
,
1
].
sample
(
-
1
)
}
end
assert
(
'Array#sample(random)'
)
do
assert_raise
(
TypeError
)
do
# this will cause an exception due to the wrong argument
[
1
,
2
].
sample
(
2
,
"Not a Random instance"
)
end
# verify that the same seed causes the same results
ary
=
(
1
..
10
).
to_a
srand
(
15
)
samples1
=
ary
.
sample
(
4
)
samples2
=
ary
.
sample
(
4
,
Random
.
new
(
15
))
samples3
=
ary
.
sample
(
4
,
Random
.
new
(
16
))
assert_equal
(
samples1
,
samples2
)
assert_not_equal
(
samples1
,
samples3
)
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment