From 28d0e1ed4acd252b4ce20cae411ec4fc060407da Mon Sep 17 00:00:00 2001 From: Rafael Fourquet Date: Sun, 17 Sep 2023 17:43:49 +0200 Subject: [PATCH] TaskLocalRNG: test that `copy` handles the splitmix state (#51355) This adds a test for #51332. --- stdlib/Random/test/runtests.jl | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/stdlib/Random/test/runtests.jl b/stdlib/Random/test/runtests.jl index 3f570d862b743..94749b12123c3 100644 --- a/stdlib/Random/test/runtests.jl +++ b/stdlib/Random/test/runtests.jl @@ -1065,3 +1065,26 @@ end end end end + +@testset "TaskLocalRNG: copy and copy! handle the splitmix state" begin + seeds = rand(RandomDevice(), UInt64, 5) + for seed in seeds + Random.seed!(seed) + rng1 = copy(TaskLocalRNG()) + x = fetch(@async rand(UInt64)) + rng2 = copy(TaskLocalRNG()) + y = fetch(@async rand(UInt64)) + rng3 = copy(TaskLocalRNG()) + @test x != y + @test rng1 != rng2 + Random.seed!(seed) + @test TaskLocalRNG() == rng1 + @test x == fetch(@async rand(UInt64)) + @test TaskLocalRNG() == rng2 + # this should be a no-op: + copy!(TaskLocalRNG(), copy(TaskLocalRNG())) + @test TaskLocalRNG() == rng2 + @test y == fetch(@async rand(UInt64)) + @test TaskLocalRNG() == rng3 + end +end