[Bug #21106] Fix tests for custom random object

When a positive integer limit is given, `rand` method of a RNG object
is expected to return a value between 0 and the limit (exclusive).

Fix shuffle_spec.rb like as the similar code in sample_spec.rb, and
add tests for greater values.

TODO:
- Return a value that is equal to or greater than the limit given to
  the RNG object.
- Extract common code about RNG objects to a shared file.
This commit is contained in:
Nobuyoshi Nakada 2025-02-02 20:59:59 +09:00
parent 8dd0d63550
commit 571f3394f2
No known key found for this signature in database
GPG Key ID: 3582D74E1FEE4465
Notes: git 2025-02-02 16:19:09 +00:00
2 changed files with 18 additions and 2 deletions

View File

@ -114,6 +114,13 @@ describe "Array#sample" do
-> { [1, 2].sample(random: random) }.should raise_error(RangeError)
end
it "raises a RangeError if the value is greater than the Array size" do
random = mock("array_sample_random")
random.should_receive(:rand).and_return(3)
-> { [1, 2].sample(random: random) }.should raise_error(RangeError)
end
end
end

View File

@ -69,9 +69,18 @@ describe "Array#shuffle" do
-> { [1, 2].shuffle(random: random) }.should raise_error(RangeError)
end
it "raises a RangeError if the value is equal to one" do
it "raises a RangeError if the value is equal to the Array size" do
value = mock("array_shuffle_random_value")
value.should_receive(:to_int).at_least(1).times.and_return(1)
value.should_receive(:to_int).at_least(1).times.and_return(2)
random = mock("array_shuffle_random")
random.should_receive(:rand).at_least(1).times.and_return(value)
-> { [1, 2].shuffle(random: random) }.should raise_error(RangeError)
end
it "raises a RangeError if the value is greater than the Array size" do
value = mock("array_shuffle_random_value")
value.should_receive(:to_int).at_least(1).times.and_return(3)
random = mock("array_shuffle_random")
random.should_receive(:rand).at_least(1).times.and_return(value)