Skip to content

Commit 8d3d550

Browse files
add tests
1 parent ea965a1 commit 8d3d550

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

stdlib/Random/test/runtests.jl

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,3 +1018,50 @@ guardseed() do
10181018
@test f42752(true) === val
10191019
end
10201020
end
1021+
1022+
@testset "TaskLocalRNG: stream collision smoke test" begin
1023+
# spawn a trinary tree of tasks:
1024+
# - spawn three recursive child tasks in each
1025+
# - generate a random UInt64 in each before, after and between
1026+
# - collect and count all the generated random values
1027+
# these should all be distinct across all tasks
1028+
function gen(d)
1029+
r = rand(UInt64)
1030+
vals = [r]
1031+
if d 0
1032+
append!(vals, gent(d - 1))
1033+
isodd(r) && append!(vals, gent(d - 1))
1034+
push!(vals, rand(UInt64))
1035+
iseven(r) && append!(vals, gent(d - 1))
1036+
end
1037+
push!(vals, rand(UInt64))
1038+
end
1039+
gent(d) = fetch(@async gen(d))
1040+
seeds = rand(RandomDevice(), UInt64, 5)
1041+
for seed in seeds
1042+
Random.seed!(seed)
1043+
vals = gen(6)
1044+
@test allunique(vals)
1045+
end
1046+
end
1047+
1048+
@testset "TaskLocalRNG: child doesn't affect parent" begin
1049+
seeds = rand(RandomDevice(), UInt64, 5)
1050+
for seed in seeds
1051+
Random.seed!(seed)
1052+
x = rand(UInt64)
1053+
y = rand(UInt64)
1054+
n = 3
1055+
for i = 1:n
1056+
Random.seed!(seed)
1057+
@sync for j = 0:i
1058+
@async rand(UInt64)
1059+
end
1060+
@test x == rand(UInt64)
1061+
@sync for j = 0:(n-i)
1062+
@async rand(UInt64)
1063+
end
1064+
@test y == rand(UInt64)
1065+
end
1066+
end
1067+
end

0 commit comments

Comments
 (0)