Skip to content

Commit

Permalink
Split some tests in different files for practical reasons
Browse files Browse the repository at this point in the history
  • Loading branch information
emmt committed Jan 16, 2020
1 parent 2a59aca commit cc3ea41
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 196 deletions.
95 changes: 95 additions & 0 deletions test/diff-tests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#
# diff-tests.jl -
#
# Tests for finite differences.
#

using LazyAlgebra
using Test

@testset "Finite differences" begin
types = (Float32, Float64)
alphas = (0, 1, -1, 2.71, π)
betas = (0, 1, -1, -1.33, Base.MathConstants.φ)
sizes = ((50,), (8,9), (4,5,6))
D = SimpleFiniteDifferences()
DtD = gram(D)
for T in types, dims in sizes
x = randn(T, dims)
xsav = vcopy(x)
y = randn(T, ndims(x), size(x)...)
ysav = vcopy(y)
z = randn(T, size(x))
zsav = vcopy(z)
# Apply direct and adjoint of D "by-hand".
Dx_truth = Array{T}(undef, size(y))
Dty_truth = Array{T}(undef, size(x))
fill!(Dty_truth, 0)
if ndims(x) == 1
Dx_truth[1,1:end-1] = x[2:end] - x[1:end-1]
Dx_truth[1,end] = 0
Dty_truth[2:end] += y[1,1:end-1]
Dty_truth[1:end-1] -= y[1,1:end-1]
elseif ndims(x) == 2
Dx_truth[1,1:end-1,:] = x[2:end,:] - x[1:end-1,:]
Dx_truth[1,end,:] .= 0
Dx_truth[2,:,1:end-1] = x[:,2:end] - x[:,1:end-1]
Dx_truth[2,:,end] .= 0
Dty_truth[2:end,:] += y[1,1:end-1,:]
Dty_truth[1:end-1,:] -= y[1,1:end-1,:]
Dty_truth[:,2:end] += y[2,:,1:end-1]
Dty_truth[:,1:end-1] -= y[2,:,1:end-1]
elseif ndims(x) == 3
Dx_truth[1,1:end-1,:,:] = x[2:end,:,:] - x[1:end-1,:,:]
Dx_truth[1,end,:,:] .= 0
Dx_truth[2,:,1:end-1,:] = x[:,2:end,:] - x[:,1:end-1,:]
Dx_truth[2,:,end,:] .= 0
Dx_truth[3,:,:,1:end-1] = x[:,:,2:end] - x[:,:,1:end-1]
Dx_truth[3,:,:,end] .= 0
Dty_truth[2:end,:,:] += y[1,1:end-1,:,:]
Dty_truth[1:end-1,:,:] -= y[1,1:end-1,:,:]
Dty_truth[:,2:end,:] += y[2,:,1:end-1,:]
Dty_truth[:,1:end-1,:] -= y[2,:,1:end-1,:]
Dty_truth[:,:,2:end] += y[3,:,:,1:end-1]
Dty_truth[:,:,1:end-1] -= y[3,:,:,1:end-1]
end
Dx = D*x; @test x == xsav
Dty = D'*y; @test y == ysav
DtDx = DtD*x; @test x == xsav
# There should be no differences between Dx and Dx_truth because
# they are computed in the exact same way. For Dty and Dty_truth,
# the comparsion must be approximative. For testing DtD against
# D'*D, parenthesis are needed to avoid simplifications.
atol, rtol = zero(T), 4*eps(T)
@test vdot(y,Dx) vdot(Dty,x) atol=atol rtol=sqrt(eps(T))
@test vnorm2(Dx - Dx_truth) == 0
@test Dty Dty_truth atol=atol rtol=rtol norm=vnorm2
@test DtDx D'*(D*x) atol=atol rtol=rtol norm=vnorm2
for α in alphas,
β in betas,
scratch in (false, true)
@test apply!(α, Direct, D, x, scratch, β, vcopy(y))
T(α)*Dx + T(β)*y atol=atol rtol=rtol norm=vnorm2
if scratch
vcopy!(x, xsav)
else
@test x == xsav
end
@test apply!(α, Adjoint, D, y, scratch, β, vcopy(x))
T(α)*Dty + T(β)*x atol=atol rtol=rtol norm=vnorm2
if scratch
vcopy!(y, ysav)
else
@test y == ysav
end
@test apply!(α, Direct, DtD, x, scratch, β, vcopy(z))
T(α)*DtDx + T(β)*z atol=atol rtol=rtol norm=vnorm2
if scratch
vcopy!(x, xsav)
else
@test x == xsav
end
end
end
end
nothing
48 changes: 26 additions & 22 deletions test/fft-tests.jl
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
#
# fft-tests.jl -
#
# Tests for FFT and circulant convolution operators.
#

using Test
using LazyAlgebra
using LazyAlgebra.FFTs
using AbstractFFTs, FFTW

let FLOATS = (Float32, Float64),
TYPES = (Float32, #Float64, Complex{Float32},
Complex{Float64}),
ALPHAS = (0, 1, -1, 2.71, π),
BETAS = (0, 1, -1, -1.33, Base.MathConstants.φ)
@testset "FFT methods" begin
floats = (Float32, Float64)
types = (Float32, #Float64, Complex{Float32},
Complex{Float64})
alphas = (0, 1, -1, 2.71, π)
betas = (0, 1, -1, -1.33, Base.MathConstants.φ)

@testset "FFT utilities" begin
dims1 = (1, 2, 3, 4, 5, 7, 9, 287, 511)
dims2 = (1, 2, 3, 4, 5, 8, 9, 288, 512)
dims3 = (1, 2, 3, 4, 5, 8, 9, 288, 512)
@test goodfftdims(dims1) == dims2
@test goodfftdims(map(Int16, dims1)) == dims2
@test goodfftdims(dims1...) == dims2
@test rfftdims(1,2,3,4,5) == (1,2,3,4,5)
@test rfftdims(2,3,4,5) == (2,3,4,5)
@test rfftdims(3,4,5) == (2,4,5)
@test rfftdims(4,5) == (3,5)
@test rfftdims(5) == (3,)
@test LazyAlgebra.FFTs.goodfftdims(dims1) == dims2
@test LazyAlgebra.FFTs.goodfftdims(map(Int16, dims1)) == dims2
@test LazyAlgebra.FFTs.goodfftdims(dims1...) == dims2
@test LazyAlgebra.FFTs.rfftdims(1,2,3,4,5) == (1,2,3,4,5)
@test LazyAlgebra.FFTs.rfftdims(2,3,4,5) == (2,3,4,5)
@test LazyAlgebra.FFTs.rfftdims(3,4,5) == (2,4,5)
@test LazyAlgebra.FFTs.rfftdims(4,5) == (3,5)
@test LazyAlgebra.FFTs.rfftdims(5) == (3,)
@test LazyAlgebra.FFTs.fftfreq(1) == [0]
@test LazyAlgebra.FFTs.fftfreq(2) == [0,-1]
@test LazyAlgebra.FFTs.fftfreq(3) == [0,1,-1]
@test LazyAlgebra.FFTs.fftfreq(4) == [0,1,-2,-1]
@test LazyAlgebra.FFTs.fftfreq(5) == [0,1,2,-2,-1]
end

@testset "FFT operator ($T)" for T in TYPES
@testset "FFT operator ($T)" for T in types
R = real(T)
ϵ = sqrt(eps(R)) # relative tolerance, can certainly be much tighter
for dims in ((45,), (20,), (33,12), (30,20), (4,5,6))
Expand Down Expand Up @@ -96,8 +102,8 @@ let FLOATS = (Float32, Float64),
@test x == xsav # check that input has been preserved
@test y == ysav # check that input has been preserved
@test y == ysav # check that input has been preserved
for α in ALPHAS,
β in BETAS,
for α in alphas,
β in betas,
scratch in (false, true)
@test apply!(α, Direct, F, x, scratch, β, vcopy(y))
R(α)*z + R(β)*y atol=0 rtol=ϵ
Expand Down Expand Up @@ -131,7 +137,7 @@ let FLOATS = (Float32, Float64),
end
end

@testset "Circular convolution ($T)" for T in TYPES
@testset "Circular convolution ($T)" for T in types
R = real(T)
ϵ = sqrt(eps(R)) # relative tolerance, can certainly be much tighter
n1, n2, n3 = 18, 12, 4
Expand Down Expand Up @@ -182,8 +188,8 @@ let FLOATS = (Float32, Float64),
z4 = irfft(conj.(rfft(h)).*rfft(y), n1)
@test z1 z4 atol=0 rtol=ϵ
end
for α in ALPHAS,
β in BETAS,
for α in alphas,
β in betas,
scratch in (false, true)
@test apply!(α, Direct, H, x, scratch, β, vcopy(y))
R(α)*y1 + R(β)*y atol=0 rtol=ϵ
Expand All @@ -202,7 +208,5 @@ let FLOATS = (Float32, Float64),
end
end
end

end

nothing
9 changes: 6 additions & 3 deletions test/genmult-tests.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#
# genmult-tests.jl -
#
# Tests for generalized matrix-vector and matrix-matrix products.
#

module GenMultTests

using Test
using Test: print_test_results

#Test.TESTSET_PRINT_ENABLE[] = true

using LazyAlgebra
import LazyAlgebra: GenMult, convert_multiplier
import .GenMult: Reals, Complexes, Floats, BlasFloat
Expand Down
142 changes: 2 additions & 140 deletions test/map-tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,6 @@
#
# Tests for basic mappings.
#
#-------------------------------------------------------------------------------
#
# This file is part of LazyAlgebra (https://github.com/emmt/LazyAlgebra.jl)
# released under the MIT "Expat" license.
#
# Copyright (c) 2017-2020 Éric Thiébaut.
#

#isdefined(:LazyAlgebra) || include("../src/LazyAlgebra.jl")

module LazyAlgebraMappingTests

Expand All @@ -26,11 +17,6 @@ using Test
using Printf
import Base: show, axes

# Deal with compatibility issues.
@static if isdefined(Base, :MathConstants)
import Base.MathConstants: φ
end

const I = Identity()
@static if VERSION < v"0.7.0-DEV.3449"
using Base.LinAlg: UniformScaling
Expand All @@ -39,7 +25,7 @@ else
end

const ALPHAS = (0, 1, -1, 2.71, π)
const BETAS = (0, 1, -1, -1.33, φ)
const BETAS = (0, 1, -1, -1.33, Base.MathConstants.φ)
const OPERATIONS = (Direct, Adjoint, Inverse, InverseAdjoint)
const FLOATS = (Float32, Float64)
const COMPLEXES = (ComplexF32, ComplexF64)
Expand Down Expand Up @@ -168,13 +154,7 @@ function test_all()
test_rank_one_operator()
test_non_uniform_scaling()
test_generalized_matrices()
test_finite_differences()
if VERSION v"0.7"
test_scaling()
test_sparse_operator()
else
@warn "Some tests are broken for Julia < 0.7"
end
test_scaling()
end
end

Expand Down Expand Up @@ -578,122 +558,4 @@ function test_generalized_matrices()
end
end

function test_sparse_operator(verbose::Bool=false)
ntests = 0
nfailures = 0
rows, cols = (2,3,4), (5,6)
nrows, ncols = prod(rows), prod(cols)
#@testset "Sparse matrices ($T)"
for T in FLOATS
A = randn(T, rows..., cols...)
A[rand(T, size(A)) .≤ 0.7] .= 0 # 70% of zeros
x = randn(T, cols)
y = randn(T, rows)
G = GeneralMatrix(A)
S = SparseOperator(A, length(rows))
@mytest is_endomorphism(S) == (rows == cols)
@mytest (EndomorphismType(S) == Endomorphism) == (rows == cols)
@mytest output_size(S) == rows
@mytest input_size(S) == cols
@mytest_throws DimensionMismatch vcreate(Direct, S,
randn(T, size(x) .+ 1))
@mytest_throws DimensionMismatch vcreate(Adjoint, S,
randn(T, size(y) .+ 1))
@mytest A == Array(S)
atol, rtol = zero(T), sqrt(eps(T))
mA = reshape(A, nrows, ncols)
vx = reshape(x, ncols)
vy = reshape(y, nrows)
Sx = S*x
Sty = S'*y
@mytest almost_equal(Sx, reshape(mA*vx, rows))
@mytest almost_equal(Sty, reshape(mA'*vy, cols))
@mytest almost_equal(Sx, G*x)
@mytest almost_equal(Sty, G'*y)
## Use another constructor with integer conversion.
R = SparseOperator(Int32.(LazyAlgebra.rows(S)),
Int64.(LazyAlgebra.cols(S)),
LazyAlgebra.coefs(S),
Int32.(output_size(S)),
Int64.(input_size(S)))
@mytest almost_equal(Sx, R*x)
@mytest almost_equal(Sty, R'*y)
for α in ALPHAS,
β in BETAS
@mytest almost_equal(apply!(α, Direct, S, x, β, vcopy(y)),
T(α)*Sx + T(β)*y)
@mytest almost_equal(apply!(α, Adjoint, S, y, β, vcopy(x)),
T(α)*Sty + T(β)*x)
end
end

@mytest_report
end

function test_finite_differences()
sizes = ((50,), (8, 9), (4,5,6))
@testset "Finite differences ($T)" for T in FLOATS
D = SimpleFiniteDifferences()
DtD = gram(D)
for dims in sizes
x = randn(T, dims)
y = randn(T, ndims(x), size(x)...)
z = randn(T, size(x))
# Apply direct and adjoint of D "by-hand".
Dx_truth = Array{T}(undef, size(y))
Dty_truth = Array{T}(undef, size(x))
fill!(Dty_truth, 0)
if ndims(x) == 1
Dx_truth[1,1:end-1] = x[2:end] - x[1:end-1]
Dx_truth[1,end] = 0
Dty_truth[2:end] += y[1,1:end-1]
Dty_truth[1:end-1] -= y[1,1:end-1]
elseif ndims(x) == 2
Dx_truth[1,1:end-1,:] = x[2:end,:] - x[1:end-1,:]
Dx_truth[1,end,:] .= 0
Dx_truth[2,:,1:end-1] = x[:,2:end] - x[:,1:end-1]
Dx_truth[2,:,end] .= 0
Dty_truth[2:end,:] += y[1,1:end-1,:]
Dty_truth[1:end-1,:] -= y[1,1:end-1,:]
Dty_truth[:,2:end] += y[2,:,1:end-1]
Dty_truth[:,1:end-1] -= y[2,:,1:end-1]
elseif ndims(x) == 3
Dx_truth[1,1:end-1,:,:] = x[2:end,:,:] - x[1:end-1,:,:]
Dx_truth[1,end,:,:] .= 0
Dx_truth[2,:,1:end-1,:] = x[:,2:end,:] - x[:,1:end-1,:]
Dx_truth[2,:,end,:] .= 0
Dx_truth[3,:,:,1:end-1] = x[:,:,2:end] - x[:,:,1:end-1]
Dx_truth[3,:,:,end] .= 0
Dty_truth[2:end,:,:] += y[1,1:end-1,:,:]
Dty_truth[1:end-1,:,:] -= y[1,1:end-1,:,:]
Dty_truth[:,2:end,:] += y[2,:,1:end-1,:]
Dty_truth[:,1:end-1,:] -= y[2,:,1:end-1,:]
Dty_truth[:,:,2:end] += y[3,:,:,1:end-1]
Dty_truth[:,:,1:end-1] -= y[3,:,:,1:end-1]
end
Dx = D*x
Dty = D'*y
DtDx = DtD*x
# There should be no differences between Dx and Dx_truth because
# they are computed in the exact same way. For Dty and Dty_truth,
# the comparsion must be approximative. For testing DtD against
# D'*D, parenthesis are needed to avoid simplifications.
atol, rtol = zero(T), 4*eps(T)
@test vdot(y,Dx) vdot(Dty,x) atol=atol rtol=sqrt(eps(T))
@test vnorm2(Dx - Dx_truth) == 0
@test Dty Dty_truth atol=atol rtol=rtol norm=vnorm2
@test DtDx D'*(D*x) atol=atol rtol=rtol norm=vnorm2
for α in ALPHAS,
β in BETAS
@test apply!(α, Direct, D, x, β, vcopy(y))
T(α)*Dx + T(β)*y atol=atol rtol=rtol norm=vnorm2
@test apply!(α, Adjoint, D, y, β, vcopy(x))
T(α)*Dty + T(β)*x atol=atol rtol=rtol norm=vnorm2
@test apply!(α, Direct, DtD, x, β, vcopy(z))
T(α)*DtDx + T(β)*z atol=atol rtol=rtol norm=vnorm2
end
end
end
end

end # module
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ include("coder-tests.jl")
include("vect-tests.jl")
include("map-tests.jl")
LazyAlgebraMappingTests.test_all()
include("diff-tests.jl")
include("sparse-tests.jl")
include("crop-tests.jl")
include("fft-tests.jl")
include("cg-tests.jl")
Expand Down
Loading

0 comments on commit cc3ea41

Please sign in to comment.