Skip to content

Commit

Permalink
fix shuffle! on empty arrays (#28727)
Browse files Browse the repository at this point in the history
shuffle([]) doesn't work anymore since nextpow2(n) has
been replaced by nextpow(2, n), where n == 0.
  • Loading branch information
rfourquet authored and KristofferC committed Aug 19, 2018
1 parent 6458059 commit 728d31d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions stdlib/Random/src/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ julia> shuffle!(rng, Vector(1:16))
function shuffle!(r::AbstractRNG, a::AbstractArray)
@assert !has_offset_axes(a)
n = length(a)
n <= 1 && return a # nextpow below won't work with n == 0
@assert n <= Int64(2)^52
mask = nextpow(2, n) - 1
for i = n:-1:2
Expand Down
8 changes: 8 additions & 0 deletions stdlib/Random/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -686,3 +686,11 @@ end
@test Random.gentype(Random.UInt52(UInt128)) == UInt128
@test Random.gentype(Random.UInt104()) == UInt128
end

@testset "shuffle[!]" begin
a = []
@test shuffle(a) == a # issue #28727
@test shuffle!(a) === a
a = rand(Int, 1)
@test shuffle(a) == a
end

0 comments on commit 728d31d

Please sign in to comment.