Skip to content

Commit

Permalink
fix few failing tests with a fresh seed, and duplicate guardsrand's l…
Browse files Browse the repository at this point in the history
…ogic

guardsrand can't be used directly, as then the testset's body is wrapped
in a function, which causes problem with overwriting loop variable ("outer"),
using `using`, defining new methods, etc.
  • Loading branch information
rfourquet committed Nov 2, 2017
1 parent b573c48 commit dffee11
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 37 deletions.
73 changes: 40 additions & 33 deletions stdlib/Test/src/Test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -941,23 +941,28 @@ function testset_beginend(args, tests, source)
# finally removing the testset and giving it a chance to take
# action (such as reporting the results)
quote
# GLOBAL_RNG is re-seeded with its own seed to ease reproduce a failed test
guardsrand(Base.GLOBAL_RNG.seed) do
ts = $(testsettype)($desc; $options...)
# this empty loop is here to force the block to be compiled,
# which is needed for backtrace scrubbing to work correctly.
while false; end
push_testset(ts)
try
$(esc(tests))
catch err
# something in the test block threw an error. Count that as an
# error in this test set
record(ts, Error(:nontest_error, :(), err, catch_backtrace(), $(QuoteNode(source))))
end
pop_testset()
finish(ts)
ts = $(testsettype)($desc; $options...)
# this empty loop is here to force the block to be compiled,
# which is needed for backtrace scrubbing to work correctly.
while false; end
push_testset(ts)
# we reproduce the logic of guardsrand, but this function
# cannot be used as it changes slightly the semantic of @testset,
# by wrapping the body in a function
oldrng = copy(Base.GLOBAL_RNG)
try
# GLOBAL_RNG is re-seeded with its own seed to ease reproduce a failed test
srand(Base.GLOBAL_RNG.seed)
$(esc(tests))
catch err
# something in the test block threw an error. Count that as an
# error in this test set
record(ts, Error(:nontest_error, :(), err, catch_backtrace(), $(QuoteNode(source))))
finally
copy!(Base.GLOBAL_RNG, oldrng)
end
pop_testset()
finish(ts)
end
end

Expand Down Expand Up @@ -1001,23 +1006,25 @@ function testset_forloop(args, testloop, source)
# wrapped in the outer loop provided by the user
tests = testloop.args[2]
blk = quote
guardsrand(Base.GLOBAL_RNG.seed) do
# Trick to handle `break` and `continue` in the test code before
# they can be handled properly by `finally` lowering.
if !first_iteration
pop_testset()
push!(arr, finish(ts))
end
ts = $(testsettype)($desc; $options...)
push_testset(ts)
first_iteration = false
try
$(esc(tests))
catch err
# Something in the test block threw an error. Count that as an
# error in this test set
record(ts, Error(:nontest_error, :(), err, catch_backtrace(), $(QuoteNode(source))))
end
# Trick to handle `break` and `continue` in the test code before
# they can be handled properly by `finally` lowering.
if !first_iteration
pop_testset()
push!(arr, finish(ts))
end
ts = $(testsettype)($desc; $options...)
push_testset(ts)
first_iteration = false
oldrng = copy(Base.GLOBAL_RNG)
try
srand(Base.GLOBAL_RNG.seed)
$(esc(tests))
catch err
# Something in the test block threw an error. Count that as an
# error in this test set
record(ts, Error(:nontest_error, :(), err, catch_backtrace(), $(QuoteNode(source))))
finally
copy!(Base.GLOBAL_RNG, oldrng)
end
end
quote
Expand Down
5 changes: 3 additions & 2 deletions stdlib/Test/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ end
end
@testset "some loops fail" begin
@testset for i in 1:5
@test i <= rand(1:3)
@test i <= 4
end
# should add 3 errors and 3 passing tests
@testset for i in 1:6
Expand Down Expand Up @@ -651,7 +651,8 @@ end
rm(f; force=true)
end

@testset "@testset is wrapped in a `guardsrand` block" begin
@testset "@testset preserves GLOBAL_RNG's state, and re-seeds it" begin
# i.e. it behaves as if it was wrapped in a `guardsrand(GLOBAL_RNG.seed)` block
seed = rand(UInt128)
srand(seed)
a = rand()
Expand Down
1 change: 1 addition & 0 deletions test/linalg/arnoldi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ using Test
end

@testset "Symmetric generalized with singular B" begin
srand(123)
n = 10
k = 3
A = randn(n,n); A = A'A
Expand Down
1 change: 1 addition & 0 deletions test/linalg/lu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ dimg = randn(n)/2
end

@testset "conversion" begin
srand(3)
a = Tridiagonal(rand(9),rand(10),rand(9))
fa = Array(a)
falu = lufact(fa)
Expand Down
3 changes: 1 addition & 2 deletions test/sparse/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -426,8 +426,7 @@ end
@test_throws ArgumentError permute!(A, p, (r = copy(q); r[2] = n + 1; r))
end
@testset "overall functionality of [c]transpose[!] and permute[!]" begin
for (_m, _n) in ((smalldim, smalldim), (smalldim, largedim), (largedim, smalldim))
m, n = _m, _n # to avoid overwriting variables from above in the for loop
for (m, n) in ((smalldim, smalldim), (smalldim, largedim), (largedim, smalldim))
A = sprand(m, n, nzprob)
At = transpose(A)
# transpose[!]
Expand Down

0 comments on commit dffee11

Please sign in to comment.