/usr/share/julia/base/test.jl is in julia-common 0.4.7-6.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | # This file is a part of Julia. License is MIT: http://julialang.org/license
module Test
export @test, @test_throws, @test_approx_eq, @test_approx_eq_eps, @inferred
abstract Result
type Success <: Result
expr
resultexpr
res
Success(expr, resultexpr=nothing, res=nothing) = new(expr, resultexpr, res)
end
type Failure <: Result
expr
resultexpr
end
Failure(expr) = Failure(expr, nothing)
type Error <: Result
expr
err
backtrace
end
default_handler(r::Success) = r.res
function default_handler(r::Failure)
if r.resultexpr !== nothing
error("test failed: $(r.resultexpr)\n in expression: $(r.expr)")
else
error("test failed in expression: $(r.expr)")
end
end
default_handler(r::Error) = rethrow(r)
handler() = get(task_local_storage(), :TEST_HANDLER, default_handler)
with_handler(f::Function, handler) =
task_local_storage(f, :TEST_HANDLER, handler)
import Base.showerror
showerror(io::IO, r::Error) = showerror(io, r, [])
function showerror(io::IO, r::Error, bt)
println(io, "test error in expression: $(r.expr)")
showerror(io, r.err, r.backtrace)
end
function do_test(body,qex)
handler()(try
rex, val = body()
val ? Success(qex, rex) : Failure(qex,rex)
catch err
Error(qex,err,catch_backtrace())
end)
end
function do_test_throws(body, qex, bt, extype)
handler()(try
body()
Failure(qex, "$qex did not throw $(extype === nothing ? "anything" : extype)")
catch err
if extype === nothing
Base.warn("""
@test_throws without an exception type is deprecated;
Use `@test_throws $(typeof(err)) $(qex)` instead.
""", bt = bt)
Success(qex, nothing, err)
else
if isa(err, extype)
Success(qex, nothing, err)
else
if isa(err,Type)
Failure(qex, "the type $err was thrown instead of an instance of $extype")
else
Failure(qex, "$err was thrown instead of $extype")
end
end
end
end)
end
macro test(ex)
if typeof(ex) == Expr && ex.head == :comparison
syms = [gensym() for i = 1:length(ex.args)]
func_block = Expr(:block)
# insert assignment into a block
func_block.args = [:($(syms[i]) = $(esc(ex.args[i]))) for i = 1:length(ex.args)]
# finish the block with a return
push!(func_block.args, Expr(:return, :(Expr(:comparison, $(syms...)), $(Expr(:comparison, syms...)))))
:(do_test(()->($func_block), $(Expr(:quote,ex))))
else
:(do_test(()->($(Expr(:quote,ex)), $(esc(ex))), $(Expr(:quote,ex))))
end
end
macro test_throws(args...)
ex = nothing
extype = nothing
# Users should pass (ExceptionType, Expression) but we give a warning to users that only pass (Expression)
if length(args) == 1
ex = args[1]
elseif length(args) == 2
ex = args[2]
extype = args[1]
end
:(do_test_throws(()->($(esc(ex))),$(Expr(:quote,ex)),backtrace(),$(esc(extype))))
end
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)
if length(va) != length(vb)
error("lengths of ", astr, " and ", bstr, " do not match: ",
"\n ", astr, " (length $(length(va))) = ", va,
"\n ", bstr, " (length $(length(vb))) = ", vb)
end
diff = real(zero(eltype(va)))
for i = 1:length(va)
xa = va[i]; xb = vb[i]
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(va)*max(array_eps(va), array_eps(vb)), astr, bstr)
macro test_approx_eq_eps(a, b, c)
:(test_approx_eq($(esc(a)), $(esc(b)), $(esc(c)), $(string(a)), $(string(b))))
end
macro test_approx_eq(a, b)
:(test_approx_eq($(esc(a)), $(esc(b)), $(string(a)), $(string(b))))
end
"""
@inferred f(x)
Tests that the call expression `f(x)` returns a value of the same type
inferred by the compiler. It's useful to check for type stability.
`f(x)` can be any call expression.
Returns the result of `f(x)` if the types match,
and an `Error` `Result` if it finds different types.
```jldoctest
julia> using Base.Test
julia> f(a,b,c) = b > 1 ? 1 : 1.0
f (generic function with 1 method)
julia> typeof(f(1,2,3))
Int64
julia> @code_warntype f(1,2,3)
Variables:
a::Int64
b::Int64
c::Int64
<BLANKLINE>
Body:
begin # none, line 1:
unless (Base.slt_int)(1,b::Int64)::Bool goto 0
return 1
0:
return 1.0
end::UNION{FLOAT64,INT64}
julia> @inferred f(1,2,3)
ERROR: return type Int64 does not match inferred return type Union{Float64,Int64}
in error at ./error.jl:21
julia> @inferred max(1,2)
2
```
"""
macro inferred(ex)
ex.head == :call || error("@inferred requires a call expression")
quote
vals = ($([esc(ex.args[i]) for i = 2:length(ex.args)]...),)
inftypes = Base.return_types($(esc(ex.args[1])), Base.typesof(vals...))
@assert length(inftypes) == 1
result = $(esc(ex.args[1]))(vals...)
rettype = isa(result, Type) ? Type{result} : typeof(result)
rettype == inftypes[1] || error("return type $rettype does not match inferred return type $(inftypes[1])")
result
end
end
# Test approximate equality of vectors or columns of matrices modulo floating
# point roundoff and phase (sign) differences.
#
# This function is design to test for equality between vectors of floating point
# numbers when the vectors are defined only up to a global phase or sign, such as
# normalized eigenvectors or singular vectors. The global phase is usually
# defined consistently, but may occasionally change due to small differences in
# floating point rounding noise or rounding modes, or through the use of
# different conventions in different algorithms. As a result, most tests checking
# such vectors have to detect and discard such overall phase differences.
#
# Inputs:
# a, b:: StridedVecOrMat to be compared
# err :: Default: m^3*(eps(S)+eps(T)), where m is the number of rows
#
# 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)
m, n = size(a)
@test n==size(b, 2) && m==size(b, 1)
err === nothing && (err=m^3*(eps(S)+eps(T)))
for i=1:n
v1, v2 = a[:, i], b[:, i]
@test_approx_eq_eps min(abs(norm(v1-v2)), abs(norm(v1+v2))) 0.0 err
end
end
end # module
|