diff --git a/base/deprecated.jl b/base/deprecated.jl index e80f7574373ea..9877ef4853603 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -1674,4 +1674,71 @@ iteratoreltype(::Type{Task}) = EltypeUnknown() isempty(::Task) = error("isempty not defined for Tasks") +# BEGIN code from base/test.jl +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{T}(a::AbstractArray{Complex{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) + +""" + @test_approx_eq_eps(a, b, tol) + +Test two floating point numbers `a` and `b` for equality taking into account +a margin of tolerance given by `tol`. +""" +macro test_approx_eq_eps(a, b, c) + Base.depwarn(string("@test_approx_eq_eps is deprecated, use `@test ", a, " ≈ ", b, " atol=", c, "` instead"), + Symbol("@test_approx_eq_eps")) + :(test_approx_eq($(esc(a)), $(esc(b)), $(esc(c)), $(string(a)), $(string(b)))) +end +export @test_approx_eq_eps + +""" + @test_approx_eq(a, b) + +Deprecated. Test two floating point numbers `a` and `b` for equality taking into +account small numerical errors. +""" +macro test_approx_eq(a, b) + Base.depwarn(string("@test_approx_eq is deprecated, use `@test ", a, " ≈ ", b, "` instead"), + Symbol("@test_approx_eq")) + :(test_approx_eq($(esc(a)), $(esc(b)), $(string(a)), $(string(b)))) +end +export @test_approx_eq +# END code from base/test.jl + # End deprecations scheduled for 0.6 diff --git a/base/test.jl b/base/test.jl index 2e0a29845a77c..837b21536ad3a 100644 --- a/base/test.jl +++ b/base/test.jl @@ -16,7 +16,7 @@ module Test export @test, @test_throws, @test_broken, @test_skip, @test_warn, @test_nowarn export @testset # Legacy approximate testing functions, yet to be included -export @test_approx_eq_eps, @inferred +export @inferred export detect_ambiguities export GenericString @@ -203,15 +203,47 @@ end const comparison_prec = Base.operator_precedence(:(==)) +""" + test_expr!(ex, kws...) + +Preprocess test expressions of function calls with trailing keyword arguments +so that e.g. `@test a ≈ b atol=ε` means `@test ≈(a, b, atol=ε)`. +""" +test_expr!(m, ex) = ex + +function test_expr!(m, ex, kws...) + ex isa Expr && ex.head == :call || @goto fail + for kw in kws + kw isa Expr && kw.head == :(=) || @goto fail + kw.head = :kw + push!(ex.args, kw) + end + return ex +@label fail + error("invalid test macro call: $m $ex $(join(kws," "))") +end + # @test - check if the expression evaluates to true """ @test ex + @test f(args...) key=val ... Tests that the expression `ex` evaluates to `true`. Returns a `Pass` `Result` if it does, a `Fail` `Result` if it is `false`, and an `Error` `Result` if it could not be evaluated. + +The `@test f(args...) key=val...` form is equivalent to writing +`@test f(args..., key=val...)` which can be useful when the expression +is a call using infix syntax such as approximate comparisons: + + @test a ≈ b atol=ε + +This is equivalent to the uglier test `@test ≈(a, b, atol=ε)`. +It is an error to supply more than one expression unless the first +is a call expression and the rest are assignments (`k=v`). """ -macro test(ex) +macro test(ex, kws...) + test_expr!("@test", ex, kws...) orig_ex = Expr(:inert, ex) result = get_test_result(ex) :(do_test($result, $orig_ex)) @@ -219,13 +251,17 @@ end """ @test_broken ex + @test_broken f(args...) key=val ... Indicates a test that should pass but currently consistently fails. Tests that the expression `ex` evaluates to `false` or causes an exception. Returns a `Broken` `Result` if it does, or an `Error` `Result` if the expression evaluates to `true`. + +The `@test_broken f(args...) key=val...` form works as for the `@test` macro. """ -macro test_broken(ex) +macro test_broken(ex, kws...) + test_expr!("@test_broken", ex, kws...) orig_ex = Expr(:inert, ex) result = get_test_result(ex) # code to call do_test with execution result and original expr @@ -234,12 +270,16 @@ end """ @test_skip ex + @test_skip f(args...) key=val ... Marks a test that should not be executed but should be included in test summary reporting as `Broken`. This can be useful for tests that intermittently fail, or tests of not-yet-implemented functionality. + +The `@test_skip f(args...) key=val...` form works as for the `@test` macro. """ -macro test_skip(ex) +macro test_skip(ex, kws...) + test_expr!("@test_skip", ex, kws...) orig_ex = Expr(:inert, ex) testres = :(Broken(:skipped, $orig_ex)) :(record(get_testset(), $testres)) @@ -326,6 +366,7 @@ end @test_throws extype ex Tests that the expression `ex` throws an exception of type `extype`. +Note that `@test_throws` does not support a trailing keyword form. """ macro test_throws(extype, ex) orig_ex = Expr(:inert, ex) @@ -960,71 +1001,6 @@ function get_testset_depth() return length(testsets) end -#----------------------------------------------------------------------- -# Legacy approximate testing functions, yet to be included - -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{T}(a::AbstractArray{Complex{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) - -""" - @test_approx_eq_eps(a, b, tol) - -Test two floating point numbers `a` and `b` for equality taking into account -a margin of tolerance given by `tol`. -""" -macro test_approx_eq_eps(a, b, c) - :(test_approx_eq($(esc(a)), $(esc(b)), $(esc(c)), $(string(a)), $(string(b)))) -end - -""" - @test_approx_eq(a, b) - -Deprecated. Test two floating point numbers `a` and `b` for equality taking into -account small numerical errors. -""" -macro test_approx_eq(a, b) - Base.depwarn(string("@test_approx_eq is deprecated, use `@test ", a, " ≈ ", b, "` instead"), - Symbol("@test_approx_eq")) - :(test_approx_eq($(esc(a)), $(esc(b)), $(string(a)), $(string(b)))) -end -export @test_approx_eq - _args_and_call(args...; kwargs...) = (args[1:end-1], kwargs, args[end](args[1:end-1]...; kwargs...)) """ @inferred f(x) @@ -1119,13 +1095,13 @@ end # Raises an error if any columnwise vector norm exceeds err. Otherwise, returns # nothing. function test_approx_eq_modphase{S<:Real,T<:Real}( - a::StridedVecOrMat{S}, b::StridedVecOrMat{T}, err=nothing) + a::StridedVecOrMat{S}, b::StridedVecOrMat{T}, + err = length(indices(a,1))^3*(eps(S)+eps(T)) + ) @test indices(a,1) == indices(b,1) && indices(a,2) == indices(b,2) - m = length(indices(a,1)) - err === nothing && (err=m^3*(eps(S)+eps(T))) for i in indices(a,2) v1, v2 = a[:, i], b[:, i] - @test_approx_eq_eps min(abs(norm(v1-v2)), abs(norm(v1+v2))) 0.0 err + @test min(abs(norm(v1-v2)),abs(norm(v1+v2))) ≈ 0.0 atol=err end end diff --git a/test/complex.jl b/test/complex.jl index 8bd7435457e74..55c8782228047 100644 --- a/test/complex.jl +++ b/test/complex.jl @@ -101,7 +101,7 @@ end @test exp(x) ≈ exp(big(x)) @test exp10(x) ≈ exp10(big(x)) @test exp2(x) ≈ exp2(big(x)) - @test_approx_eq_eps expm1(x) expm1(big(x)) eps(T) + @test expm1(x) ≈ expm1(big(x)) atol=eps(T) @test log(x) ≈ log(big(x)) @test log10(x) ≈ log10(big(x)) @test log1p(x) ≈ log1p(big(x)) diff --git a/test/linalg/arnoldi.jl b/test/linalg/arnoldi.jl index b47c936ea0480..73bc287c8643c 100644 --- a/test/linalg/arnoldi.jl +++ b/test/linalg/arnoldi.jl @@ -34,7 +34,7 @@ using Base.Test @test a*v[:,2] ≈ d[2]*v[:,2] @test norm(v) > testtol # eigenvectors cannot be null vectors # (d,v) = eigs(a, b, nev=3, tol=1e-8) # not handled yet - # @test_approx_eq_eps a*v[:,2] d[2]*b*v[:,2] testtol + # @test a*v[:,2] ≈ d[2]*b*v[:,2] atol=testtol # @test norm(v) > testtol # eigenvectors cannot be null vectors (d,v) = eigs(asym, nev=3) @@ -47,7 +47,7 @@ using Base.Test @test eigs(apd; nev=1, sigma=d[3])[1][1] ≈ d[3] (d,v) = eigs(apd, bpd, nev=3, tol=1e-8) - @test_approx_eq_eps apd*v[:,2] d[2]*bpd*v[:,2] testtol + @test apd*v[:,2] ≈ d[2]*bpd*v[:,2] atol=testtol @test norm(v) > testtol # eigenvectors cannot be null vectors @testset "(shift-and-)invert mode" begin @@ -56,7 +56,7 @@ using Base.Test @test norm(v) > testtol # eigenvectors cannot be null vectors (d,v) = eigs(apd, bpd, nev=3, sigma=0, tol=1e-8) - @test_approx_eq_eps apd*v[:,1] d[1]*bpd*v[:,1] testtol + @test apd*v[:,1] ≈ d[1]*bpd*v[:,1] atol=testtol @test norm(v) > testtol # eigenvectors cannot be null vectors end @@ -99,7 +99,7 @@ let A6965 = [ # 0.8 0.1 0.1 # 0.7 0.1 0.2 ] #d,v,nconv = eigs(T6965,nev=1,which=:LM) - #@test_approx_eq_eps T6965*v d[1]*v 1e-6 + # @test T6965*v ≈ d[1]*v atol=1e-6 end # Example from Quantum Information Theory diff --git a/test/linalg/bidiag.jl b/test/linalg/bidiag.jl index 3ff104c84d30b..ba0b6b6a42f33 100644 --- a/test/linalg/bidiag.jl +++ b/test/linalg/bidiag.jl @@ -198,7 +198,7 @@ srand(1) Test.test_approx_eq_modphase(u1, u2) Test.test_approx_eq_modphase(v1, v2) end - @test_approx_eq_eps 0 vecnorm(u2*diagm(d2)*v2'-Tfull) n*max(n^2*eps(relty), vecnorm(u1*diagm(d1)*v1' - Tfull)) + @test 0 ≈ vecnorm(u2*diagm(d2)*v2'-Tfull) atol=n*max(n^2*eps(relty),vecnorm(u1*diagm(d1)*v1'-Tfull)) @inferred svdvals(T) @inferred svd(T) end diff --git a/test/linalg/bunchkaufman.jl b/test/linalg/bunchkaufman.jl index e1eb40b3766be..db569b563d624 100644 --- a/test/linalg/bunchkaufman.jl +++ b/test/linalg/bunchkaufman.jl @@ -57,7 +57,7 @@ bimg = randn(n,2)/2 @test logabsdet(bc1)[2] ≈ sign(det(bc1)) end @test inv(bc1)*asym ≈ eye(n) - @test_approx_eq_eps asym*(bc1\b) b 1000ε + @test asym*(bc1\b) ≈ b atol=1000ε @testset for rook in (false, true) @test inv(bkfact(a.'+a, :U, true, rook))*(a.'+a) ≈ eye(n) @test size(bc1) == size(bc1.LD) @@ -76,7 +76,7 @@ bimg = randn(n,2)/2 @test logabsdet(bc2)[1] ≈ log(abs(det(bc2))) @test logabsdet(bc2)[2] == sign(det(bc2)) @test inv(bc2)*apd ≈ eye(n) - @test_approx_eq_eps apd * (bc2\b) b 150000ε + @test apd*(bc2\b) ≈ b atol=150000ε @test ishermitian(bc2) == !issymmetric(bc2) end end diff --git a/test/linalg/dense.jl b/test/linalg/dense.jl index f3c14beb9b779..3e3a337f0d7eb 100644 --- a/test/linalg/dense.jl +++ b/test/linalg/dense.jl @@ -26,10 +26,10 @@ for elty in (Float32, Float64, Complex64, Complex128) a = view(ainit, 1:n, 1:n) end # cond - @test_approx_eq_eps cond(a, 1) 4.837320054554436e+02 0.01 - @test_approx_eq_eps cond(a, 2) 1.960057871514615e+02 0.01 - @test_approx_eq_eps cond(a, Inf) 3.757017682707787e+02 0.01 - @test_approx_eq_eps cond(a[:,1:5]) 10.233059337453463 0.01 + @test cond(a,1) ≈ 4.837320054554436e+02 atol=0.01 + @test cond(a,2) ≈ 1.960057871514615e+02 atol=0.01 + @test cond(a,Inf) ≈ 3.757017682707787e+02 atol=0.01 + @test cond(a[:,1:5]) ≈ 10.233059337453463 atol=0.01 @test_throws ArgumentError cond(a,3) end end @@ -75,8 +75,8 @@ debug && println("Solve square general system of equations") debug && println("Test nullspace") a15null = nullspace(a[:,1:n1]') @test rank([a[:,1:n1] a15null]) == 10 - @test_approx_eq_eps norm(a[:,1:n1]'a15null, Inf) zero(eltya) 300ε - @test_approx_eq_eps norm(a15null'a[:,1:n1], Inf) zero(eltya) 400ε + @test norm(a[:,1:n1]'a15null,Inf) ≈ zero(eltya) atol=300ε + @test norm(a15null'a[:,1:n1],Inf) ≈ zero(eltya) atol=400ε @test size(nullspace(b), 2) == 0 @test nullspace(zeros(eltya,n)) == eye(eltya,1) end diff --git a/test/linalg/diagonal.jl b/test/linalg/diagonal.jl index 706f72e04a696..59f51ef3425b2 100644 --- a/test/linalg/diagonal.jl +++ b/test/linalg/diagonal.jl @@ -47,17 +47,17 @@ srand(1) end for func in (det, trace) - @test_approx_eq_eps func(D) func(DM) n^2*eps(relty)*(elty<:Complex ? 2:1) + @test func(D) ≈ func(DM) atol=n^2*eps(relty)*(1+(elty<:Complex)) end if relty <: BlasFloat for func in (expm,) - @test_approx_eq_eps func(D) func(DM) n^3*eps(relty) + @test func(D) ≈ func(DM) atol=n^3*eps(relty) end - @test_approx_eq_eps logm(Diagonal(abs.(D.diag))) logm(abs.(DM)) n^3*eps(relty) + @test logm(Diagonal(abs.(D.diag))) ≈ logm(abs.(DM)) atol=n^3*eps(relty) end if elty <: BlasComplex for func in (logdet, sqrtm) - @test_approx_eq_eps func(D) func(DM) n^2*eps(relty)*2 + @test func(D) ≈ func(DM) atol=n^2*eps(relty)*2 end end end @@ -73,18 +73,18 @@ srand(1) U = view(UU, 1:n, 1:2) end - @test_approx_eq_eps D*v DM*v n*eps(relty)*(elty<:Complex ? 2:1) - @test_approx_eq_eps D*U DM*U n^2*eps(relty)*(elty<:Complex ? 2:1) + @test D*v ≈ DM*v atol=n*eps(relty)*(1+(elty<:Complex)) + @test D*U ≈ DM*U atol=n^2*eps(relty)*(1+(elty<:Complex)) @test U.'*D ≈ U.'*Array(D) @test U'*D ≈ U'*Array(D) if relty != BigFloat - @test_approx_eq_eps D\v DM\v 2n^2*eps(relty)*(elty<:Complex ? 2:1) - @test_approx_eq_eps D\U DM\U 2n^3*eps(relty)*(elty<:Complex ? 2:1) - @test_approx_eq_eps A_ldiv_B!(D,copy(v)) DM\v 2n^2*eps(relty)*(elty<:Complex ? 2:1) - @test_approx_eq_eps A_ldiv_B!(D,copy(U)) DM\U 2n^3*eps(relty)*(elty<:Complex ? 2:1) - @test_approx_eq_eps A_ldiv_B!(D,eye(D)) D\eye(D) 2n^3*eps(relty)*(elty<:Complex ? 2:1) + @test D\v ≈ DM\v atol=2n^2*eps(relty)*(1+(elty<:Complex)) + @test D\U ≈ DM\U atol=2n^3*eps(relty)*(1+(elty<:Complex)) + @test A_ldiv_B!(D,copy(v)) ≈ DM\v atol=2n^2*eps(relty)*(1+(elty<:Complex)) + @test A_ldiv_B!(D,copy(U)) ≈ DM\U atol=2n^3*eps(relty)*(1+(elty<:Complex)) + @test A_ldiv_B!(D,eye(D)) ≈ D\eye(D) atol=2n^3*eps(relty)*(1+(elty<:Complex)) @test_throws DimensionMismatch A_ldiv_B!(D, ones(elty, n + 1)) @test_throws SingularException A_ldiv_B!(Diagonal(zeros(relty,n)),copy(v)) b = rand(elty,n,n) diff --git a/test/linalg/eigen.jl b/test/linalg/eigen.jl index f25bb65725abd..3ae52efb28c80 100644 --- a/test/linalg/eigen.jl +++ b/test/linalg/eigen.jl @@ -65,7 +65,7 @@ aimg = randn(n,n)/2 f = eigfact(asym_sg, a_sg'a_sg) @test asym_sg*f[:vectors] ≈ (a_sg'a_sg*f[:vectors]) * Diagonal(f[:values]) @test f[:values] ≈ eigvals(asym_sg, a_sg'a_sg) - @test_approx_eq_eps prod(f[:values]) prod(eigvals(asym_sg/(a_sg'a_sg))) 200ε + @test prod(f[:values]) ≈ prod(eigvals(asym_sg/(a_sg'a_sg))) atol=200ε @test eigvecs(asym_sg, a_sg'a_sg) == f[:vectors] @test eigvals(f) === f[:values] @test eigvecs(f) === f[:vectors] @@ -86,7 +86,7 @@ aimg = randn(n,n)/2 f = eigfact(a1_nsg, a2_nsg) @test a1_nsg*f[:vectors] ≈ (a2_nsg*f[:vectors]) * Diagonal(f[:values]) @test f[:values] ≈ eigvals(a1_nsg, a2_nsg) - @test_approx_eq_eps prod(f[:values]) prod(eigvals(a1_nsg/a2_nsg)) 50000ε + @test prod(f[:values]) ≈ prod(eigvals(a1_nsg/a2_nsg)) atol=50000ε @test eigvecs(a1_nsg, a2_nsg) == f[:vectors] @test_throws KeyError f[:Z] diff --git a/test/linalg/generic.jl b/test/linalg/generic.jl index 4ecae2fc82d3b..bc1f165284bf8 100644 --- a/test/linalg/generic.jl +++ b/test/linalg/generic.jl @@ -107,17 +107,17 @@ y = linspace(50, 200, 100) # Anscombe's quartet (https://en.wikipedia.org/wiki/Anscombe%27s_quartet) x123 = [10.0; 8.0; 13.0; 9.0; 11.0; 14.0; 6.0; 4.0; 12.0; 7.0; 5.0] y1 = [8.04; 6.95; 7.58; 8.81; 8.33; 9.96; 7.24; 4.26; 10.84; 4.82; 5.68] -@test_approx_eq_eps [linreg(x123, y1)...] [3.0, 0.5] 10e-5 +@test [linreg(x123,y1)...] ≈ [3.0,0.5] atol=15e-5 y2 = [9.14; 8.14; 8.74; 8.77; 9.26; 8.10; 6.12; 3.10; 9.13; 7.26; 4.74] -@test_approx_eq_eps [linreg(x123, y2)...] [3.0, 0.5] 10e-3 +@test [linreg(x123,y2)...] ≈ [3.0,0.5] atol=10e-3 y3 = [7.46; 6.77; 12.74; 7.11; 7.81; 8.84; 6.08; 5.39; 8.15; 6.42; 5.73] -@test_approx_eq_eps [linreg(x123, y3)...] [3.0, 0.5] 10e-3 +@test [linreg(x123,y3)...] ≈ [3.0,0.5] atol=10e-3 x4 = [8.0; 8.0; 8.0; 8.0; 8.0; 8.0; 8.0; 19.0; 8.0; 8.0; 8.0] y4 = [6.58; 5.76; 7.71; 8.84; 8.47; 7.04; 5.25; 12.50; 5.56; 7.91; 6.89] -@test_approx_eq_eps [linreg(x4, y4)...] [3.0, 0.5] 10e-3 +@test [linreg(x4,y4)...] ≈ [3.0,0.5] atol=10e-3 # test diag let A = eye(4) diff --git a/test/linalg/lq.jl b/test/linalg/lq.jl index 48007daa9bf06..5fba2518762cd 100644 --- a/test/linalg/lq.jl +++ b/test/linalg/lq.jl @@ -59,18 +59,18 @@ bimg = randn(n,2)/2 @test full(copy(lqa)) ≈ a end @testset "Binary ops" begin - @test_approx_eq_eps a*(lqa\b) b 3000ε - @test_approx_eq_eps lqa*b qra[:Q]*qra[:R]*b 3000ε - @test_approx_eq_eps A_mul_Bc(eye(eltyb,size(q.factors,2)),q)*full(q, thin=false) eye(n) 5000ε + @test a*(lqa\b) ≈ b atol=3000ε + @test lqa*b ≈ qra[:Q]*qra[:R]*b atol=3000ε + @test A_mul_Bc(eye(eltyb,size(q.factors,2)),q)*full(q,thin=false) ≈ eye(n) atol=5000ε if eltya != Int @test eye(eltyb,n)*q ≈ convert(AbstractMatrix{tab},q) end - @test_approx_eq_eps q*b full(q, thin=false)*b 100ε - @test_approx_eq_eps q.'*b full(q, thin=false).'*b 100ε - @test_approx_eq_eps q'*b full(q, thin=false)'*b 100ε - @test_approx_eq_eps a*q a*full(q, thin=false) 100ε - @test_approx_eq_eps a*q.' a*full(q, thin=false).' 100ε - @test_approx_eq_eps a*q' a*full(q, thin=false)' 100ε + @test q*b ≈ full(q,thin=false)*b atol=100ε + @test q.'*b ≈ full(q,thin=false).'*b atol=100ε + @test q'*b ≈ full(q,thin=false)'*b atol=100ε + @test a*q ≈ a*full(q,thin=false) atol=100ε + @test a*q.' ≈ a*full(q,thin=false).' atol=100ε + @test a*q' ≈ a*full(q,thin=false)' atol=100ε @test_throws DimensionMismatch q*b[1:n1 + 1] @test_throws DimensionMismatch Ac_mul_B(q,ones(eltya,n+2,n+2)) @test_throws DimensionMismatch ones(eltyb,n+2,n+2)*q diff --git a/test/linalg/pinv.jl b/test/linalg/pinv.jl index 1f5c8049dc20e..e01c0e253c1bd 100644 --- a/test/linalg/pinv.jl +++ b/test/linalg/pinv.jl @@ -92,9 +92,9 @@ function test_pinv(a,m,n,tol1,tol2,tol3) debug && println("=== julia/matlab pinv, default tol=eps(1.0)*max(size(a)) ===") apinv = @inferred pinv(a) - @test_approx_eq_eps vecnorm(a*apinv*a - a)/vecnorm(a) 0 tol1 + @test vecnorm(a*apinv*a-a)/vecnorm(a) ≈ 0 atol=tol1 x0 = randn(n); b = a*x0; x = apinv*b - @test_approx_eq_eps vecnorm(a*x-b)/vecnorm(b) 0 tol1 + @test vecnorm(a*x-b)/vecnorm(b) ≈ 0 atol=tol1 debug && println(vecnorm(a*apinv*a - a)/vecnorm(a)) debug && println(vecnorm(a*x-b)/vecnorm(b)) @@ -102,9 +102,9 @@ function test_pinv(a,m,n,tol1,tol2,tol3) debug && println("=== julia pinv, tol=sqrt(eps(1.0)) ===") apinv = pinv(a,sqrt(eps(real(one(eltype(a)))))) - @test_approx_eq_eps vecnorm(a*apinv*a - a)/vecnorm(a) 0 tol2 + @test vecnorm(a*apinv*a-a)/vecnorm(a) ≈ 0 atol=tol2 x0 = randn(n); b = a*x0; x = apinv*b - @test_approx_eq_eps vecnorm(a*x-b)/vecnorm(b) 0 tol2 + @test vecnorm(a*x-b)/vecnorm(b) ≈ 0 atol=tol2 debug && println(vecnorm(a*apinv*a - a)/vecnorm(a)) debug && println(vecnorm(a*x-b)/vecnorm(b)) end diff --git a/test/linalg/qr.jl b/test/linalg/qr.jl index 80f5c2a1d8686..b1f48325fcb1a 100644 --- a/test/linalg/qr.jl +++ b/test/linalg/qr.jl @@ -52,9 +52,9 @@ debug && println("QR decomposition (without pivoting)") @test full(q, thin=false)'q ≈ eye(n) @test eye(n)'q' ≈ full(q, thin=false)' @test q*r ≈ a - @test_approx_eq_eps a*(qra\b) b 3000ε + @test a*(qra\b) ≈ b atol=3000ε @test full(qra) ≈ a - @test_approx_eq_eps A_mul_Bc(eye(eltyb,size(q.factors,2)),q)*full(q, thin=false) eye(n) 5000ε + @test A_mul_Bc(eye(eltyb,size(q.factors,2)),q)*full(q,thin=false) ≈ eye(n) atol=5000ε if eltya != Int @test eye(eltyb,n)*q ≈ convert(AbstractMatrix{tab},q) ac = copy(a) @@ -69,11 +69,11 @@ debug && println("Thin QR decomposition (without pivoting)") @test q'*full(q, thin=false) ≈ eye(n) @test q'*full(q) ≈ eye(n,n1) @test q*r ≈ a[:,1:n1] - @test_approx_eq_eps q*b[1:n1] full(q)*b[1:n1] 100ε - @test_approx_eq_eps q*b full(q, thin=false)*b 100ε + @test q*b[1:n1] ≈ full(q)*b[1:n1] atol=100ε + @test q*b ≈ full(q,thin=false)*b atol=100ε @test_throws DimensionMismatch q*b[1:n1 + 1] @test_throws DimensionMismatch b[1:n1 + 1]*q' - @test_approx_eq_eps A_mul_Bc(UpperTriangular(eye(eltyb,size(q.factors,2))),q)*full(q, thin=false) eye(n1,n) 5000ε + @test A_mul_Bc(UpperTriangular(eye(eltyb,size(q.factors,2))),q)*full(q,thin=false) ≈ eye(n1,n) atol=5000ε if eltya != Int @test eye(eltyb,n)*q ≈ convert(AbstractMatrix{tab},q) end @@ -92,7 +92,7 @@ debug && println("(Automatic) Fat (pivoted) QR decomposition") @test q*r ≈ (isa(qrpa,QRPivoted) ? a[1:n1,p] : a[1:n1,:]) @test q*r[:,invperm(p)] ≈ a[1:n1,:] @test q*r*qrpa[:P].' ≈ a[1:n1,:] - @test_approx_eq_eps a[1:n1,:]*(qrpa\b[1:n1]) b[1:n1] 5000ε + @test a[1:n1,:]*(qrpa\b[1:n1]) ≈ b[1:n1] atol=5000ε @test full(qrpa) ≈ a[1:5,:] @test_throws DimensionMismatch q*b[1:n1+1] @test_throws DimensionMismatch b[1:n1+1]*q' @@ -112,7 +112,7 @@ debug && println("(Automatic) Thin (pivoted) QR decomposition") @test full(qrpa) ≈ a[:,1:5] @test_throws DimensionMismatch q*b[1:n1+1] @test_throws DimensionMismatch b[1:n1+1]*q' - @test_approx_eq_eps A_mul_Bc(UpperTriangular(eye(eltyb,size(q.factors,2))),q)*full(q, thin=false) eye(n1,n) 5000ε + @test A_mul_Bc(UpperTriangular(eye(eltyb,size(q.factors,2))),q)*full(q,thin=false) ≈ eye(n1,n) atol=5000ε if eltya != Int @test eye(eltyb,n)*q ≈ convert(AbstractMatrix{tab},q) end diff --git a/test/linalg/triangular.jl b/test/linalg/triangular.jl index 8b20f96b59d4e..92ba457e1e818 100644 --- a/test/linalg/triangular.jl +++ b/test/linalg/triangular.jl @@ -224,8 +224,8 @@ for elty1 in (Float32, Float64, BigFloat, Complex64, Complex128, Complex{BigFloa end # Determinant - @test_approx_eq_eps det(A1) det(lufact(full(A1))) sqrt(eps(real(float(one(elty1)))))*n*n - @test_approx_eq_eps logdet(A1) logdet(lufact(full(A1))) sqrt(eps(real(float(one(elty1)))))*n*n + @test det(A1) ≈ det(lufact(full(A1))) atol=sqrt(eps(real(float(one(elty1)))))*n*n + @test logdet(A1) ≈ logdet(lufact(full(A1))) atol=sqrt(eps(real(float(one(elty1)))))*n*n # Matrix square root @test sqrtm(A1) |> t -> t*t ≈ A1 @@ -237,14 +237,14 @@ for elty1 in (Float32, Float64, BigFloat, Complex64, Complex128, Complex{BigFloa if !(elty1 in (BigFloat, Complex{BigFloat})) # Not handled yet vals, vecs = eig(A1) if (t1 == UpperTriangular || t1 == LowerTriangular) && elty1 != Int # Cannot really handle degenerate eigen space and Int matrices will probably have repeated eigenvalues. - @test_approx_eq_eps vecs*diagm(vals)/vecs full(A1) sqrt(eps(float(real(one(vals[1])))))*(norm(A1, Inf)*n)^2 + @test vecs*diagm(vals)/vecs ≈ full(A1) atol=sqrt(eps(float(real(one(vals[1])))))*(norm(A1,Inf)*n)^2 end end # Condition number tests - can be VERY approximate if elty1 <:BlasFloat for p in (1.0, Inf) - @test_approx_eq_eps cond(A1, p) cond(A1, p) (cond(A1, p) + cond(A1, p)) + @test cond(A1,p) ≈ cond(A1,p) atol=(cond(A1,p)+cond(A1,p)) end @test cond(A1,2) == cond(full(A1),2) end diff --git a/test/linalg/tridiag.jl b/test/linalg/tridiag.jl index 568b0b46b7d4a..2f6571ac44721 100644 --- a/test/linalg/tridiag.jl +++ b/test/linalg/tridiag.jl @@ -173,7 +173,7 @@ for elty in (Float32, Float64, Complex64, Complex128, Int) end # issue #1490 - @test_approx_eq_eps det(ones(elty, 3,3)) zero(elty) 3*eps(real(one(elty))) + @test det(ones(elty,3,3)) ≈ zero(elty) atol=3*eps(real(one(elty))) @test det(SymTridiagonal(elty[],elty[])) == one(elty) @@ -216,7 +216,7 @@ function test_approx_eq_vecs{S<:Real,T<:Real}(a::StridedVecOrMat{S}, b::StridedV ev1, ev2 = a[:,i], b[:,i] deviation = min(abs(norm(ev1-ev2)),abs(norm(ev1+ev2))) if !isnan(deviation) - @test_approx_eq_eps deviation 0.0 error + @test deviation ≈ 0.0 atol=error end end end @@ -267,7 +267,7 @@ let n = 12 #Size of matrix problem to test debug && println("Simple unary functions") for func in (det, inv) - @test_approx_eq_eps func(A) func(fA) n^2*sqrt(eps(relty)) + @test func(A) ≈ func(fA) atol=n^2*sqrt(eps(relty)) end debug && println("Rounding to Ints") @@ -385,7 +385,7 @@ let n = 12 #Size of matrix problem to test debug && println("Simple unary functions") for func in (det, inv) - @test_approx_eq_eps func(A) func(fA) n^2*sqrt(eps(relty)) + @test func(A) ≈ func(fA) atol=n^2*sqrt(eps(relty)) end debug && println("Rounding to Ints") diff --git a/test/math.jl b/test/math.jl index 119b2142d4e2a..4912688318119 100644 --- a/test/math.jl +++ b/test/math.jl @@ -161,38 +161,38 @@ end @test isequal(T(1//4)^2, T(1//16)) @test isequal(acos(T(1)), T(0)) @test isequal(acosh(T(1)), T(0)) - @test_approx_eq_eps asin(T(1)) T(pi)/2 eps(T) - @test_approx_eq_eps atan(T(1)) T(pi)/4 eps(T) - @test_approx_eq_eps atan2(T(1),T(1)) T(pi)/4 eps(T) + @test asin(T(1)) ≈ T(pi)/2 atol=eps(T) + @test atan(T(1)) ≈ T(pi)/4 atol=eps(T) + @test atan2(T(1),T(1)) ≈ T(pi)/4 atol=eps(T) @test isequal(cbrt(T(0)), T(0)) @test isequal(cbrt(T(1)), T(1)) @test isequal(cbrt(T(1000000000)), T(1000)) @test isequal(cos(T(0)), T(1)) - @test_approx_eq_eps cos(T(pi)/2) T(0) eps(T) + @test cos(T(pi)/2) ≈ T(0) atol=eps(T) @test isequal(cos(T(pi)), T(-1)) - @test_approx_eq_eps exp(T(1)) T(e) 10*eps(T) + @test exp(T(1)) ≈ T(e) atol=10*eps(T) @test isequal(exp10(T(1)), T(10)) @test isequal(exp2(T(1)), T(2)) @test isequal(expm1(T(0)), T(0)) - @test_approx_eq_eps expm1(T(1)) T(e)-1 10*eps(T) + @test expm1(T(1)) ≈ T(e)-1 atol=10*eps(T) @test isequal(hypot(T(3),T(4)), T(5)) @test isequal(log(T(1)), T(0)) @test isequal(log(e,T(1)), T(0)) - @test_approx_eq_eps log(T(e)) T(1) eps(T) + @test log(T(e)) ≈ T(1) atol=eps(T) @test isequal(log10(T(1)), T(0)) @test isequal(log10(T(10)), T(1)) @test isequal(log1p(T(0)), T(0)) - @test_approx_eq_eps log1p(T(e)-1) T(1) eps(T) + @test log1p(T(e)-1) ≈ T(1) atol=eps(T) @test isequal(log2(T(1)), T(0)) @test isequal(log2(T(2)), T(1)) @test isequal(sin(T(0)), T(0)) @test isequal(sin(T(pi)/2), T(1)) - @test_approx_eq_eps sin(T(pi)) T(0) eps(T) + @test sin(T(pi)) ≈ T(0) atol=eps(T) @test isequal(sqrt(T(0)), T(0)) @test isequal(sqrt(T(1)), T(1)) @test isequal(sqrt(T(100000000)), T(10000)) @test isequal(tan(T(0)), T(0)) - @test_approx_eq_eps tan(T(pi)/4) T(1) eps(T) + @test tan(T(pi)/4) ≈ T(1) atol=eps(T) end @testset "Inverses" begin @test acos(cos(x)) ≈ x @@ -309,8 +309,8 @@ end @testset "$T" for T = (Float32,Float64,Rational{Int}) fT = typeof(float(one(T))) for x = -400:40:400 - @test_approx_eq_eps sind(convert(T,x))::fT convert(fT,sin(pi/180*x)) eps(deg2rad(convert(fT,x))) - @test_approx_eq_eps cosd(convert(T,x))::fT convert(fT,cos(pi/180*x)) eps(deg2rad(convert(fT,x))) + @test sind(convert(T,x))::fT ≈ convert(fT,sin(pi/180*x)) atol=eps(deg2rad(convert(fT,x))) + @test cosd(convert(T,x))::fT ≈ convert(fT,cos(pi/180*x)) atol=eps(deg2rad(convert(fT,x))) end @testset "sind" begin @test sind(convert(T,0.0))::fT === zero(fT) @@ -329,8 +329,8 @@ end @testset "sinpi and cospi" begin for x = -3:0.3:3 - @test_approx_eq_eps sinpi(convert(T,x))::fT convert(fT,sin(pi*x)) eps(pi*convert(fT,x)) - @test_approx_eq_eps cospi(convert(T,x))::fT convert(fT,cos(pi*x)) eps(pi*convert(fT,x)) + @test sinpi(convert(T,x))::fT ≈ convert(fT,sin(pi*x)) atol=eps(pi*convert(fT,x)) + @test cospi(convert(T,x))::fT ≈ convert(fT,cos(pi*x)) atol=eps(pi*convert(fT,x)) end @test sinpi(convert(T,0.0))::fT === zero(fT) @@ -403,14 +403,14 @@ end for elty in [Float32,Float64] for x in logspace(-200, -0.01) - @test_approx_eq_eps erf(erfinv(x)) x 1e-12*x - @test_approx_eq_eps erf(erfinv(-x)) -x 1e-12*x - @test_approx_eq_eps erfc(erfcinv(2*x)) 2*x 1e-12*x + @test erf(erfinv(x)) ≈ x atol=1e-12*x + @test erf(erfinv(-x)) ≈ -x atol=1e-12*x + @test erfc(erfcinv(2*x)) ≈ 2*x atol=1e-12*x if x > 1e-20 xf = Float32(x) - @test_approx_eq_eps erf(erfinv(xf)) xf 1e-5*xf - @test_approx_eq_eps erf(erfinv(-xf)) -xf 1e-5*xf - @test_approx_eq_eps erfc(erfcinv(2xf)) 2xf 1e-5*xf + @test erf(erfinv(xf)) ≈ xf atol=1e-5*xf + @test erf(erfinv(-xf)) ≈ -xf atol=1e-5*xf + @test erfc(erfcinv(2xf)) ≈ 2xf atol=1e-5*xf end end @test erfinv(one(elty)) == Inf diff --git a/test/mpfr.jl b/test/mpfr.jl index 2660e151cf218..5a7f90103ccaf 100644 --- a/test/mpfr.jl +++ b/test/mpfr.jl @@ -756,7 +756,7 @@ tol = 1e-12 a = parse(BigFloat,"12.34567890121") b = parse(BigFloat,"12.34567890122") -@test_approx_eq_eps a+1e-11 b tol +@test a+1e-11 ≈ b atol=tol @test !(b == a) @test b > a @test b >= a @@ -765,15 +765,15 @@ b = parse(BigFloat,"12.34567890122") c = parse(BigFloat,"24.69135780242") @test typeof(a * 2) == BigFloat -@test_approx_eq_eps a*2 c tol -@test_approx_eq_eps (c-a) a tol +@test a*2 ≈ c atol=tol +@test (c-a) ≈ a atol=tol d = parse(BigFloat,"-24.69135780242") @test typeof(d) == BigFloat -@test_approx_eq_eps d+c 0 tol +@test d+c ≈ 0 atol=tol -@test_approx_eq_eps (BigFloat(3)/BigFloat(2)) BigFloat(1.5) tol +@test (BigFloat(3)/BigFloat(2)) ≈ BigFloat(1.5) atol=tol @test typeof(BigFloat(typemax(Int8))) == BigFloat @test typeof(BigFloat(typemax(Int16))) == BigFloat @@ -802,29 +802,29 @@ g = parse(BigFloat,"1234567891.123") tol = 1e-3 -@test_approx_eq_eps f+Int8(1) g tol -@test_approx_eq_eps f+Int16(1) g tol -@test_approx_eq_eps f+Int32(1) g tol -@test_approx_eq_eps f+Int64(1) g tol -@test_approx_eq_eps f+Int128(1) g tol +@test f+Int8(1) ≈ g atol=tol +@test f+Int16(1) ≈ g atol=tol +@test f+Int32(1) ≈ g atol=tol +@test f+Int64(1) ≈ g atol=tol +@test f+Int128(1) ≈ g atol=tol -@test_approx_eq_eps f+true g tol -@test_approx_eq_eps f+UInt8(1) g tol -@test_approx_eq_eps f+UInt16(1) g tol -@test_approx_eq_eps f+UInt32(1) g tol -@test_approx_eq_eps f+UInt64(1) g tol -@test_approx_eq_eps f+UInt128(1) g tol +@test f+true ≈ g atol=tol +@test f+UInt8(1) ≈ g atol=tol +@test f+UInt16(1) ≈ g atol=tol +@test f+UInt32(1) ≈ g atol=tol +@test f+UInt64(1) ≈ g atol=tol +@test f+UInt128(1) ≈ g atol=tol -@test_approx_eq_eps f+BigInt(1) g tol +@test f+BigInt(1) ≈ g atol=tol -@test_approx_eq_eps f+1f0 g tol -@test_approx_eq_eps f+1e0 g tol +@test f+1f0 ≈ g atol=tol +@test f+1e0 ≈ g atol=tol -@test_approx_eq_eps f+BigFloat(1) g tol +@test f+BigFloat(1) ≈ g atol=tol -@test_approx_eq_eps f+(1//1) g tol +@test f+(1//1) ≈ g atol=tol -@test_approx_eq_eps f+one(Rational{BigInt}) g tol +@test f+one(Rational{BigInt}) ≈ g atol=tol # issue #5963 @test typemax(Int128) == convert(BigFloat, typemax(Int128)) diff --git a/test/sparse/sparse.jl b/test/sparse/sparse.jl index 65f987f61351c..551486aef1806 100644 --- a/test/sparse/sparse.jl +++ b/test/sparse/sparse.jl @@ -1489,9 +1489,9 @@ end Ar = sprandn(20,20,.5) Ari = ceil.(Int64, 100*Ar) if Base.USE_GPL_LIBS - @test_approx_eq_eps Base.SparseArrays.normestinv(Ac,3) norm(inv(Array(Ac)),1) 1e-4 - @test_approx_eq_eps Base.SparseArrays.normestinv(Aci,3) norm(inv(Array(Aci)),1) 1e-4 - @test_approx_eq_eps Base.SparseArrays.normestinv(Ar) norm(inv(Array(Ar)),1) 1e-4 + @test Base.SparseArrays.normestinv(Ac,3) ≈ norm(inv(Array(Ac)),1) atol=1e-4 + @test Base.SparseArrays.normestinv(Aci,3) ≈ norm(inv(Array(Aci)),1) atol=1e-4 + @test Base.SparseArrays.normestinv(Ar) ≈ norm(inv(Array(Ar)),1) atol=1e-4 @test_throws ArgumentError Base.SparseArrays.normestinv(Ac,0) @test_throws ArgumentError Base.SparseArrays.normestinv(Ac,21) end diff --git a/test/test.jl b/test/test.jl index 3efa731036ba2..011e7a596b212 100644 --- a/test/test.jl +++ b/test/test.jl @@ -193,12 +193,6 @@ end @test isapprox(.1+.1+.1, .3) @test !isapprox(.1+.1+.1, .4) -@test_throws ErrorException Test.test_approx_eq(ones(10),ones(11),1e-8,"a","b") -@test_throws ErrorException Test.test_approx_eq(ones(10),zeros(10),1e-8,"a","b") - -# Test @test_approx_eq_eps -# TODO - ts = @testset "@testset should return the testset" begin @test true end