Skip to content

Commit

Permalink
tests: use ≈ and ≉ instead of isapprox, !isapprox
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanKarpinski committed Jan 26, 2017
1 parent 3ff5554 commit a73a1e2
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 130 deletions.
3 changes: 1 addition & 2 deletions test/error.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
@test collect(ExponentialBackOff(n=10, first_delay=0.01))[1] == 0.01
@test maximum(ExponentialBackOff(n=10, max_delay=0.06)) == 0.06
ratio(x) = x[2:end]./x[1:end-1]
@test all(x->isapprox(x,10.0), ratio(collect(
ExponentialBackOff(n=10, max_delay=Inf, factor=10, jitter=0.0))))
@test all(x->x 10.0, ratio(collect(ExponentialBackOff(n=10, max_delay=Inf, factor=10, jitter=0.0))))
srand(12345)
@test (mean(ratio(collect(ExponentialBackOff(n=100, max_delay=Inf, factor=1, jitter=0.1)))) - 1.0) < 1e-4

Expand Down
169 changes: 84 additions & 85 deletions test/fastmath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,10 @@ fm_fast_64_upd(x) = @fastmath (r=x; r+=eps64_2; r+=eps64_2)
fm_fast_64_upd(one64) == one64 + eps64 > one64)

let epsf = 1.0f0/2^15, one_epsf = 1+epsf
@test isapprox((@fastmath one_epsf * one_epsf - 1),
Float32(65537/1073741824))
@test @fastmath(one_epsf * one_epsf - 1) Float32(65537/1073741824)
end
let eps = 1.0/2^30, one_eps = 1+eps
@test isapprox((@fastmath one_eps * one_eps - 1),
2147483649/1152921504606846976)
@test @fastmath(one_eps * one_eps - 1) 2147483649/1152921504606846976
end

for T in (Float32, Float64, BigFloat)
Expand All @@ -53,52 +51,52 @@ for T in (Float32, Float64, BigFloat)
two = convert(T, 2) + 1//10
three = convert(T, 3) + 1//100

@test isapprox((@fastmath +two), +two)
@test isapprox((@fastmath -two), -two)
@test isapprox((@fastmath zero+one+two), zero+one+two)
@test isapprox((@fastmath zero-one-two), zero-one-two)
@test isapprox((@fastmath one*two*three), one*two*three)
@test isapprox((@fastmath one/two/three), one/two/three)
@test isapprox((@fastmath rem(two, three)), rem(two, three))
@test isapprox((@fastmath mod(two, three)), mod(two, three))
@test (@fastmath cmp(two, two)) == cmp(two, two)
@test (@fastmath cmp(two, three)) == cmp(two, three)
@test (@fastmath cmp(three, two)) == cmp(three, two)
@test (@fastmath one/zero) == convert(T, Inf)
@test (@fastmath -one/zero) == -convert(T, Inf)
@test isnan(@fastmath zero/zero) # must not throw
@test @fastmath(+two) +two
@test @fastmath(-two) -two
@test @fastmath(zero+one+two) zero+one+two
@test @fastmath(zero-one-two) zero-one-two
@test @fastmath(one*two*three) one*two*three
@test @fastmath(one/two/three) one/two/three
@test @fastmath(rem(two,three)) rem(two,three)
@test @fastmath(mod(two,three)) mod(two,three)
@test @fastmath(cmp(two,two)) == cmp(two,two)
@test @fastmath(cmp(two,three)) == cmp(two,three)
@test @fastmath(cmp(three,two)) == cmp(three,two)
@test @fastmath(one/zero) == convert(T,Inf)
@test @fastmath(-one/zero) == -convert(T,Inf)
@test isnan(@fastmath(zero/zero)) # must not throw

for x in (zero, two, convert(T, Inf), convert(T, NaN))
@test (@fastmath isfinite(x))
@test !(@fastmath isinf(x))
@test !(@fastmath isnan(x))
@test !(@fastmath issubnormal(x))
@test @fastmath(isfinite(x))
@test !@fastmath(isinf(x))
@test !@fastmath(isnan(x))
@test !@fastmath(issubnormal(x))
end
end

for T in (Complex64, Complex128, Complex{BigFloat})
zero = convert(T, 0)
one = convert(T, 1) + im*eps(real(convert(T,1)))
two = convert(T, 2) + im//10
three = convert(T, 3) + im//100

@test isapprox((@fastmath +two), +two)
@test isapprox((@fastmath -two), -two)
@test isapprox((@fastmath zero+one+two), zero+one+two)
@test isapprox((@fastmath zero-one-two), zero-one-two)
@test isapprox((@fastmath one*two*three), one*two*three)
@test isapprox((@fastmath one/two/three), one/two/three)
@test (@fastmath three == two) == (three == two)
@test (@fastmath three != two) == (three != two)
@test isnan(@fastmath one/zero) # must not throw
@test isnan(@fastmath -one/zero) # must not throw
@test isnan(@fastmath zero/zero) # must not throw
zero = convert(T,0)
one = convert(T,1) + im*eps(real(convert(T,1)))
two = convert(T,2) + im//10
three = convert(T,3) + im//100

@test @fastmath(+two) +two
@test @fastmath(-two) -two
@test @fastmath(zero+one+two) zero+one+two
@test @fastmath(zero-one-two) zero-one-two
@test @fastmath(one*two*three) one*two*three
@test @fastmath(one/two/three) one/two/three
@test @fastmath(three == two) == (three == two)
@test @fastmath(three != two) == (three != two)
@test isnan(@fastmath(one/zero)) # must not throw
@test isnan(@fastmath(-one/zero)) # must not throw
@test isnan(@fastmath(zero/zero)) # must not throw

for x in (zero, two, convert(T, Inf), convert(T, NaN))
@test (@fastmath isfinite(x))
@test !(@fastmath isinf(x))
@test !(@fastmath isnan(x))
@test !(@fastmath issubnormal(x))
@test @fastmath(isfinite(x))
@test !@fastmath(isinf(x))
@test !@fastmath(isnan(x))
@test !@fastmath(issubnormal(x))
end
end

Expand All @@ -107,36 +105,38 @@ end

# real arithmetic
for T in (Float32, Float64, BigFloat)
half = 1/convert(T, 2)
third = 1/convert(T, 3)
half = 1/convert(T,2)
third = 1/convert(T,3)

for f in (:+, :-, :abs, :abs2, :conj, :inv, :sign,
:acos, :asin, :asinh, :atan, :atanh, :cbrt, :cos, :cosh,
:exp10, :exp2, :exp, :expm1, :lgamma, :log10, :log1p,
:log2, :log, :sin, :sinh, :sqrt, :tan, :tanh)
@test isapprox((@eval @fastmath $f($half)), (@eval $f($half)))
@test isapprox((@eval @fastmath $f($third)), (@eval $f($third)))
@eval begin
@test @fastmath($f($half)) $f($half)
@test @fastmath($f($third)) $f($third)
end
end
for f in (:acosh,)
@test isapprox((@eval @fastmath $f(1+$half)), (@eval $f(1+$half)))
@test isapprox((@eval @fastmath $f(1+$third)), (@eval $f(1+$third)))
@eval begin
@test @fastmath($f(1+$half)) $f(1+$half)
@test @fastmath($f(1+$third)) $f(1+$third)
end
end
for f in (:+, :-, :*, :/, :%, :(==), :!=, :<, :<=, :>, :>=, :^,
:atan2, :hypot, :max, :min)
@test isapprox((@eval @fastmath $f($half, $third)),
(@eval $f($half, $third)))
@test isapprox((@eval @fastmath $f($third, $half)),
(@eval $f($third, $half)))
@eval begin
@test @fastmath($f($half, $third)) $f($half, $third)
@test @fastmath($f($third, $half)) $f($third, $half)
end
end
for f in (:minmax,)
@test isapprox((@eval @fastmath $f($half, $third))[1],
(@eval $f($half, $third))[1])
@test isapprox((@eval @fastmath $f($half, $third))[2],
(@eval $f($half, $third))[2])
@test isapprox((@eval @fastmath $f($third, $half))[1],
(@eval $f($third, $half))[1])
@test isapprox((@eval @fastmath $f($third, $half))[2],
(@eval $f($third, $half))[2])
@eval begin
@test @fastmath($f($half, $third)[1]) $f($half, $third)[1]
@test @fastmath($f($half, $third)[2]) $f($half, $third)[2]
@test @fastmath($f($third, $half)[1]) $f($third, $half)[1]
@test @fastmath($f($third, $half)[2]) $f($third, $half)[2]
end
end
end

Expand All @@ -153,14 +153,16 @@ for T in (Complex64, Complex128, Complex{BigFloat})
:acos, :acosh, :asin, :asinh, :atan, :atanh, :cis, :cos,
:cosh, :exp10, :exp2, :exp, :expm1, :log10, :log1p,
:log2, :log, :sin, :sinh, :sqrt, :tan, :tanh)
@test isapprox((@eval @fastmath $f($half)), (@eval $f($half)), rtol=rtol)
@test isapprox((@eval @fastmath $f($third)), (@eval $f($third)), rtol=rtol)
@eval begin
@test @fastmath($f($half)) $f($half) rtol=$rtol
@test @fastmath($f($third)) $f($third) rtol=$rtol
end
end
for f in (:+, :-, :*, :/, :(==), :!=, :^)
@test isapprox((@eval @fastmath $f($half, $third)),
(@eval $f($half, $third)), rtol=rtol)
@test isapprox((@eval @fastmath $f($third, $half)),
(@eval $f($third, $half)), rtol=rtol)
@eval begin
@test @fastmath($f($half, $third)) $f($half, $third) rtol=$rtol
@test @fastmath($f($third, $half)) $f($third, $half) rtol=$rtol
end
end
end

Expand All @@ -173,32 +175,29 @@ for T in (Float32, Float64, BigFloat)
cthird = (1-1im)/CT(3)

for f in (:+, :-, :*, :/, :(==), :!=, :^)
@test isapprox((@eval @fastmath $f($chalf, $third)),
(@eval $f($chalf, $third)))
@test isapprox((@eval @fastmath $f($half, $cthird)),
(@eval $f($half, $cthird)))
@test isapprox((@eval @fastmath $f($cthird, $half)),
(@eval $f($cthird, $half)))
@test isapprox((@eval @fastmath $f($third, $chalf)),
(@eval $f($third, $chalf)))
@eval begin
@test @fastmath($f($chalf, $third)) $f($chalf, $third)
@test @fastmath($f($half, $cthird)) $f($half, $cthird)
@test @fastmath($f($cthird, $half)) $f($cthird, $half)
@test @fastmath($f($third, $chalf)) $f($third, $chalf)
end
end

@test isapprox((@fastmath third^3), third^3)

@test isapprox((@fastmath chalf/third), chalf/third)
@test isapprox((@fastmath chalf^3), chalf^3)
@test isapprox((@fastmath cis(third)), cis(third))
@test @fastmath(third^3) third^3
@test @fastmath(chalf/third) chalf/third
@test @fastmath(chalf^3) chalf^3
@test @fastmath(cis(third)) cis(third)
end

# issue #10544
let a = ones(2,2), b = ones(2,2)
@test isapprox((@fastmath a[1] += 2.0), b[1] += 2.0)
@test isapprox((@fastmath a[2] -= 2.0), b[2] -= 2.0)
@test isapprox((@fastmath a[1,1] *= 2.0), b[1,1] *= 2.0)
@test isapprox((@fastmath a[2,2] /= 2.0), b[2,2] /= 2.0)
@test isapprox((@fastmath a[1,2] ^= 2.0), b[1,2] ^= 2.0)
@test @fastmath(a[1] += 2.0) (b[1] += 2.0)
@test @fastmath(a[2] -= 2.0) (b[2] -= 2.0)
@test @fastmath(a[1,1] *= 2.0) (b[1,1] *= 2.0)
@test @fastmath(a[2,2] /= 2.0) (b[2,2] /= 2.0)
@test @fastmath(a[1,2] ^= 2.0) (b[1,2] ^= 2.0)

# test fallthrough for unsupported ops
local c = 0
@test Bool((@fastmath c |= 1))
@test @fastmath(c |= 1) == 1
end
40 changes: 20 additions & 20 deletions test/floatapprox.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
@test 4.000000002 4.00300002

# Other tolerance levels
@test isapprox(4.32, 4.3; rtol=0.1, atol=0.01)
@test isapprox(1.001, 1.002; rtol=0.001, atol=0.0001)
@test !isapprox(4.5, 4.9; rtol=0.001, atol=0.001)
@test 4.32 4.3 rtol=0.1 atol=0.01
@test 1.001 1.002 rtol=0.001 atol=0.0001
@test 4.5 4.9 rtol=0.001 atol=0.001

# Complex numbers
@test 1.0 + 1.0im 1.0 + 1.00000000000001im
@test 0.9999999999999 + 1.0im 1.0 + 1.000000000000001im
@test isapprox(0.9999 + 1.0im, 1.0 + 1.1im; rtol = 0.0001, atol=1.1)
@test 0.9999 + 1.0im 1.0 + 1.1im rtol=0.0001 atol=1.1

# Complex <-> reals
@test 1.0 + 0im 1.0000000000001
@test isapprox(0.9999999999999, 1.0 + 0im)
@test !isapprox(1.0+1im, 1.000000000000001)
@test 0.9999999999999 1.0 + 0im
@test 1.0+1im 1.000000000000001

# Comparing NaNs
@test 4.0 NaN
Expand All @@ -37,22 +37,22 @@
@test complex(0.0,Inf) complex(0.0,-Inf)

# Tests for integers and rationals
@test isapprox(4,4)
@test isapprox(4,5; atol=1)
@test !isapprox(4,6; atol=1)
@test !isapprox(4,5)
@test 4 4
@test 4 5 atol=1
@test 4 6 atol=1
@test 4 5

@test isapprox(1//2+3im, 1//2+3im)
@test isapprox(1//3, 0.33333333333333333)
@test isapprox(1//3, 0.3333; rtol=0.0001, atol=0.0001)
@test isapprox(1+1im, 1im+1)
@test 1//2+3im 1//2+3im
@test 1//3 0.33333333333333333
@test 1//3 0.3333 rtol=0.0001 atol=0.0001
@test 1+1im 1im+1

# Notably missing from this test suite at the moment
# * Tests for other magnitudes of numbers - very small, very big, and combinations of small and big
# * Tests for various odd combinations of types, e.g. isapprox(x::Integer, y::Rational)
# * Tests for various odd combinations of types, e.g. x::Integery::Rational

# issue #12375:
@test !isapprox(1e17, 1)
@test 1e17 1

# Tests for arrays:
@test [1,2,3] [1,2,3+1e-9]
Expand All @@ -64,8 +64,8 @@
for elty in (Float16,Float32,Float64)
nan = elty(NaN)
half = elty(0.5)
@test !isapprox(nan, nan)
@test isapprox(nan, nan, nans=true)
@test !isapprox([half, nan, half], [half, nan, half])
@test isapprox([half, nan, half], [half, nan, half], nans=true)
@test nan nan
@test nan nan nans=true
@test [half, nan, half] [half, nan, half]
@test [half, nan, half] [half, nan, half] nans=true
end
4 changes: 2 additions & 2 deletions test/linalg/arnoldi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ let
@test d[1] 1. # largest eigenvalue should be 1.
v=reshape(v,(50,50)) # reshape to matrix
v/=trace(v) # factor out arbitrary phase
@test isapprox(vecnorm(imag(v)),0.) # it should be real
@test vecnorm(imag(v)) 0. # it should be real
v=real(v)
# @test isapprox(vecnorm(v-v')/2,0.) # it should be Hermitian
# @test vecnorm(v-v')/2 ≈ 0. # it should be Hermitian
# Since this fails sometimes (numerical precision error),this test is commented out
v=(v+v')/2
@test isposdef(v)
Expand Down
4 changes: 2 additions & 2 deletions test/linalg/uniformscaling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ end
@test issymmetric(UniformScaling(complex(1.0,1.0)))
@test ishermitian(I)
@test !ishermitian(UniformScaling(complex(1.0,1.0)))
@test isapprox(UniformScaling(4.00000000000001), UniformScaling(4.0))
@test isapprox(UniformScaling(4.32), UniformScaling(4.3); rtol=0.1, atol=0.01)
@test UniformScaling(4.00000000000001) UniformScaling(4.0)
@test UniformScaling(4.32) UniformScaling(4.3) rtol=0.1 atol=0.01
end

@testset "* and / with number" begin
Expand Down
2 changes: 1 addition & 1 deletion test/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ end
0.00169495384841964531409376316336552555952269360134349446910im)
end

# useful test functions for relative error, which differ from isapprox
# useful test functions for relative error, which differ from isapprox (≈)
# in that relerrc separately looks at the real and imaginary parts
relerr(z, x) = z == x ? 0.0 : abs(z - x) / abs(x)
relerrc(z, x) = max(relerr(real(z),real(x)), relerr(imag(z),imag(x)))
Expand Down
17 changes: 7 additions & 10 deletions test/numbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -155,26 +155,23 @@ let eps = 1//BigInt(2)^30, one_eps = 1+eps,
@test eps64 == Float64(eps)
@test one_eps64 == Float64(one_eps)
@test one_eps64 * one_eps64 - 1 != Float64(one_eps * one_eps - 1)
@test isapprox(muladd(one_eps64, one_eps64, -1),
Float64(one_eps * one_eps - 1))
@test muladd(one_eps64, one_eps64, -1) Float64(one_eps * one_eps - 1)
end

let eps = 1//BigInt(2)^15, one_eps = 1+eps,
eps32 = Float32(eps), one_eps32 = Float32(one_eps)
@test eps32 == Float32(eps)
@test one_eps32 == Float32(one_eps)
@test one_eps32 * one_eps32 - 1 != Float32(one_eps * one_eps - 1)
@test isapprox(muladd(one_eps32, one_eps32, -1),
Float32(one_eps * one_eps - 1))
@test muladd(one_eps32, one_eps32, -1) Float32(one_eps * one_eps - 1)
end

let eps = 1//BigInt(2)^7, one_eps = 1+eps,
eps16 = Float16(Float32(eps)), one_eps16 = Float16(Float32(one_eps))
@test eps16 == Float16(Float32(eps))
@test one_eps16 == Float16(Float32(one_eps))
@test one_eps16 * one_eps16 - 1 != Float16(Float32(one_eps * one_eps - 1))
@test isapprox(muladd(one_eps16, one_eps16, -1),
Float16(Float32(one_eps * one_eps - 1)))
@test muladd(one_eps16, one_eps16, -1) Float16(Float32(one_eps * one_eps - 1))
end

@test muladd(1,2,3) == 1*2+3
Expand Down Expand Up @@ -1678,10 +1675,10 @@ end
@test isnan(eps(-Inf))

@test .1+.1+.1 != .3
@test isapprox(.1+.1+.1, .3)
@test !isapprox(.1+.1+.1-.3, 0)
@test isapprox(.1+.1+.1-.3, 0, atol=eps(.3))
@test isapprox(1.1,1.1f0)
@test .1+.1+.1 .3
@test .1+.1+.1-.3 0
@test .1+.1+.1-.3 0 atol=eps(.3)
@test 1.1 1.1f0

@test div(1e50,1) == 1e50
@test fld(1e50,1) == 1e50
Expand Down
Loading

0 comments on commit a73a1e2

Please sign in to comment.