Skip to content

Commit

Permalink
Test: fixup issues detected by JET (#46730)
Browse files Browse the repository at this point in the history
- removes stale definition of `test_approx_eq` (this seems to have been
  reserved for the 0.7 compat, but is currently broken as `full` is
  really not defined anymore)
- fixes invalid construction of `Fail` object
- makes `LogTestFailure` more type stable struct
- improves type stabilities slightly
  • Loading branch information
aviatesk authored Sep 14, 2022
1 parent f7dea04 commit 7cbea73
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 49 deletions.
48 changes: 3 additions & 45 deletions stdlib/Test/src/Test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1204,10 +1204,11 @@ function get_test_counts(ts::DefaultTestSet)
end
end
ts.anynonpass = (fails + errors + c_fails + c_errors > 0)
duration = if isnothing(ts.time_end)
(; time_start, time_end) = ts
duration = if isnothing(time_end)
""
else
dur_s = ts.time_end - ts.time_start
dur_s = time_end - time_start
if dur_s < 60
string(round(dur_s, digits = 1), "s")
else
Expand Down Expand Up @@ -2126,49 +2127,6 @@ function _check_bitarray_consistency(B::BitArray{N}) where N
return true
end

# 0.7 deprecations

begin
approx_full(x::AbstractArray) = x
approx_full(x::Number) = x
approx_full(x) = full(x)

function test_approx_eq(va, vb, Eps, astr, bstr)
va = approx_full(va)
vb = approx_full(vb)
la, lb = length(LinearIndices(va)), length(LinearIndices(vb))
if la != lb
error("lengths of ", astr, " and ", bstr, " do not match: ",
"\n ", astr, " (length $la) = ", va,
"\n ", bstr, " (length $lb) = ", vb)
end
diff = real(zero(eltype(va)))
for (xa, xb) = zip(va, vb)
if isfinite(xa) && isfinite(xb)
diff = max(diff, abs(xa-xb))
elseif !isequal(xa,xb)
error("mismatch of non-finite elements: ",
"\n ", astr, " = ", va,
"\n ", bstr, " = ", vb)
end
end

if !isnan(Eps) && !(diff <= Eps)
sdiff = string("|", astr, " - ", bstr, "| <= ", Eps)
error("assertion failed: ", sdiff,
"\n ", astr, " = ", va,
"\n ", bstr, " = ", vb,
"\n difference = ", diff, " > ", Eps)
end
end

array_eps(a::AbstractArray{Complex{T}}) where {T} = eps(float(maximum(x->(isfinite(x) ? abs(x) : T(NaN)), a)))
array_eps(a) = eps(float(maximum(x->(isfinite(x) ? abs(x) : oftype(x,NaN)), a)))

test_approx_eq(va, vb, astr, bstr) =
test_approx_eq(va, vb, 1E4*length(LinearIndices(va))*max(array_eps(va), array_eps(vb)), astr, bstr)
end

include("logging.jl")

end # module
8 changes: 4 additions & 4 deletions stdlib/Test/src/logging.jl
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ end
# Log testing tools

# Failure result type for log testing
mutable struct LogTestFailure <: Result
struct LogTestFailure <: Result
orig_expr
source::Union{Nothing,LineNumberNode}
source::LineNumberNode
patterns
logs
end
Expand Down Expand Up @@ -153,8 +153,8 @@ function record(ts::DefaultTestSet, t::LogTestFailure)
println()
end
# Hack: convert to `Fail` so that test summarization works correctly
push!(ts.results, Fail(:test, t.orig_expr, t.logs, nothing, nothing, t.source))
t
push!(ts.results, Fail(:test, t.orig_expr, t.logs, nothing, nothing, t.source, false))
return t
end

"""
Expand Down

0 comments on commit 7cbea73

Please sign in to comment.