Skip to content

Commit

Permalink
Make tests modular (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
jishnub authored Apr 6, 2024
1 parent 48a9c1d commit 8bfec16
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 100 deletions.
10 changes: 10 additions & 0 deletions test/mylazyarray.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
struct MyLazyArray{T,N} <: AbstractArray{T,N}
data::Array{T,N}
end


Base.size(A::MyLazyArray) = size(A.data)
Base.getindex(A::MyLazyArray, j::Int...) = A.data[j...]
LazyArrays.MemoryLayout(::Type{<:MyLazyArray}) = LazyLayout()
Base.BroadcastStyle(::Type{<:MyLazyArray{<:Any,N}}) where N = LazyArrayStyle{N}()
LinearAlgebra.factorize(A::MyLazyArray) = factorize(A.data)
76 changes: 2 additions & 74 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,84 +1,12 @@
using LazyBandedMatrices, BlockBandedMatrices, BandedMatrices, LazyArrays, BlockArrays,
ArrayLayouts, MatrixFactorizations, Random, Test
import LinearAlgebra
import LinearAlgebra: qr, rmul!, lmul!
import LazyArrays: Applied, resizedata!, FillLayout, MulStyle, arguments, colsupport, rowsupport, LazyLayout, ApplyStyle,
PaddedLayout, paddeddata, call, ApplyLayout, LazyArrayStyle, simplifiable
import LazyBandedMatrices: VcatBandedMatrix, BroadcastBlockBandedLayout, BroadcastBandedLayout,
ApplyBandedLayout, ApplyBlockBandedLayout, ApplyBandedBlockBandedLayout, BlockKron, LazyBandedLayout, BroadcastBandedBlockBandedLayout
import BandedMatrices: BandedStyle, _BandedMatrix, AbstractBandedMatrix, BandedRows, BandedColumns
import ArrayLayouts: StridedLayout, OnesLayout
import BlockArrays: blockcolsupport, blockrowsupport
import Random

Random.seed!(0)

struct MyLazyArray{T,N} <: AbstractArray{T,N}
data::Array{T,N}
end


Base.size(A::MyLazyArray) = size(A.data)
Base.getindex(A::MyLazyArray, j::Int...) = A.data[j...]
LazyArrays.MemoryLayout(::Type{<:MyLazyArray}) = LazyLayout()
Base.BroadcastStyle(::Type{<:MyLazyArray{<:Any,N}}) where N = LazyArrayStyle{N}()
LinearAlgebra.factorize(A::MyLazyArray) = factorize(A.data)

include("test_tridiag.jl")
include("test_bidiag.jl")
include("test_special.jl")
include("test_banded.jl")
include("test_block.jl")




@testset "Misc" begin

@testset "Diagonal interface" begin
n = 10
h = 1/n
= BandedMatrix(0 => Fill(-2,n), 1 => Fill(1,n-1), -1 => Fill(1,n-1))/h^2
D_xx = BandedBlockBandedMatrix(BlockKron(D², Eye(n)))

D = Diagonal(randn(n^2))
@test D_xx + D isa BandedBlockBandedMatrix
@test blockbandwidths(D_xx + D) == blockbandwidths(D_xx)
@test subblockbandwidths(D_xx + D) == subblockbandwidths(D_xx)
@test D_xx + D == Matrix(D_xx) + D

@test D_xx - D isa BandedBlockBandedMatrix
@test blockbandwidths(D_xx - D) == blockbandwidths(D_xx)
@test subblockbandwidths(D_xx - D) == subblockbandwidths(D_xx)
@test D_xx - D == Matrix(D_xx) - D

@test D_xx*D == Matrix(D_xx)*D
@test D_xx*D isa BandedBlockBandedMatrix
@test blockbandwidths(D_xx*D) == blockbandwidths(D_xx)
@test subblockbandwidths(D_xx*D) == subblockbandwidths(D_xx)

@test D*D_xx == D*Matrix(D_xx)
@test D*D_xx isa BandedBlockBandedMatrix
@test blockbandwidths(D*D_xx) == blockbandwidths(D_xx)
@test subblockbandwidths(D*D_xx) == subblockbandwidths(D_xx)
end

# @testset "Padded columns" begin
# B = brand(8,8,1,2)
# v = view(B,:,4)
# w = view(B,3,:)
# @test MemoryLayout(v) isa PaddedLayout
# @test_broken MemoryLayout(w) isa PaddedLayout
# @test paddeddata(v) isa Vcat
# paddeddata(v) == B[:,4]
# end

@testset "Block broadcast" begin
a = PseudoBlockArray(BroadcastArray(exp, randn(5)), [3,2])
@test call(a) == exp
end
end



include("test_misc.jl")
include("test_blockkron.jl")
include("test_blockconcat.jl")
20 changes: 19 additions & 1 deletion test/test_banded.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
module TestBanded

using LazyBandedMatrices, BandedMatrices, LazyArrays, Test
import LazyBandedMatrices: VcatBandedMatrix, BroadcastBlockBandedLayout, BroadcastBandedLayout,
ApplyBandedLayout, ApplyBlockBandedLayout, ApplyBandedBlockBandedLayout,
BlockKron, LazyBandedLayout, BroadcastBandedBlockBandedLayout

import LazyArrays: Applied, resizedata!, MulStyle, arguments, colsupport, rowsupport, LazyLayout,
PaddedLayout, call, ApplyLayout, LazyArrayStyle, simplifiable

using ArrayLayouts
import ArrayLayouts: StridedLayout, OnesLayout
using LinearAlgebra
import BandedMatrices: BandedStyle, BandedRows, BandedColumns
using BandedMatrices: _BandedMatrix, isbanded
using LazyBandedMatrices: ApplyBandedLayout
using MatrixFactorizations

include("mylazyarray.jl")

struct PseudoBandedMatrix{T} <: AbstractMatrix{T}
data::Array{T}
Expand Down Expand Up @@ -730,4 +746,6 @@ LinearAlgebra.lmul!(β::Number, A::PseudoBandedMatrix) = (lmul!(β, A.data); A)
@test Li * x L \ x
@test Bi * x B \ x
end
end
end

end # module
8 changes: 6 additions & 2 deletions test/test_bidiag.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module TestBidiag

# This file is based on a part of Julia LinearAlgebra/test/bidiag.jl. License is MIT: https://julialang.org/license
using Test, LazyBandedMatrices, SparseArrays, Random, FillArrays
import LinearAlgebra
using LinearAlgebra
import LazyBandedMatrices: Bidiagonal, SymTridiagonal, Tridiagonal
import LinearAlgebra: mul!

Expand Down Expand Up @@ -482,4 +484,6 @@ import LinearAlgebra: mul!
@test B.dv 1:5
@test B.ev Ones{Int}(4)
end
end # module TestBidiagonal
end # testset

end # module TestBidiagonal
42 changes: 27 additions & 15 deletions test/test_block.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
module TestBlock

using LazyBandedMatrices, LazyArrays, BlockBandedMatrices, BlockArrays, Test
using LinearAlgebra
using ArrayLayouts
using BandedMatrices
using LazyArrays: paddeddata
using BlockArrays: blockcolsupport
import BlockArrays: blockcolsupport, blockrowsupport
import LazyArrays: arguments, colsupport, rowsupport,
PaddedLayout, paddeddata, ApplyLayout, LazyArrayStyle
import LazyBandedMatrices: BroadcastBlockBandedLayout, BroadcastBandedBlockBandedLayout,
ApplyBlockBandedLayout, ApplyBandedBlockBandedLayout


@testset "Block" begin
@testset "LazyBlock" begin
Expand All @@ -18,7 +28,7 @@ using BlockArrays: blockcolsupport
@test view(n, Block(5)) Fill(5,5)
@test view(k,Block(5)) Base.OneTo(5)
a = b = c = 0.0
# for some reason the following was causing major slowdown. I think it
# for some reason the following was causing major slowdown. I think it
# went pass a limit to Base.Broadcast.flatten which caused `bc.f` to have a strange type.
# bc = Base.Broadcast.instantiate(Base.broadcasted(/, Base.broadcasted(*, k, Base.broadcasted(-, Base.broadcasted(-, k, n), a)), Base.broadcasted(+, 2k, b+c-1)))

Expand All @@ -27,7 +37,7 @@ using BlockArrays: blockcolsupport
u = (k .* (k .- n .- a) ./ (2k .+ (b+c-1)))
@test u == (Vector(k) .* (Vector(k) .- Vector(n) .- a) ./ (2Vector(k) .+ (b+c-1)))
@test copyto!(u, bc) == (k .* (k .- n .- a) ./ (2k .+ (b+c-1)))
@test @allocated(copyto!(u, bc)) 1000
@test @allocated(copyto!(u, bc)) 1000
# not clear why allocatinos so high: all allocations are coming from checking
# axes

Expand All @@ -52,7 +62,7 @@ using BlockArrays: blockcolsupport
@test b[Block.(2:3)] == b[2:6]
end


@testset "block padded" begin
c = PseudoBlockVector(Vcat(1, Zeros(5)), 1:3)
@test paddeddata(c) == [1]
Expand Down Expand Up @@ -123,25 +133,25 @@ using BlockArrays: blockcolsupport

x = randn(size(B,2))
@test B*x 2A*x

C = BroadcastMatrix(*, 2, im*A)
@test MemoryLayout(C') isa LazyBandedMatrices.LazyBlockBandedLayout
@test MemoryLayout(transpose(C)) isa LazyBandedMatrices.LazyBlockBandedLayout

E = BroadcastMatrix(*, A, 2)
@test MemoryLayout(E) == BroadcastBlockBandedLayout{typeof(*)}()


D = Diagonal(PseudoBlockArray(randn(6),1:3))
@test MemoryLayout(BroadcastMatrix(*, A, D)) isa BroadcastBlockBandedLayout{typeof(*)}
@test MemoryLayout(BroadcastMatrix(*, D, A)) isa BroadcastBlockBandedLayout{typeof(*)}

F = BroadcastMatrix(*, A, A)
@test MemoryLayout(F) == BroadcastBlockBandedLayout{typeof(*)}()
end
@testset "BroadcastBandedBlockBanded" begin
A = BandedBlockBandedMatrix(randn(6,6),1:3,1:3,(1,1),(1,1))

B = BroadcastMatrix(*, 2, A)
@test blockbandwidths(B) == (1,1)
@test subblockbandwidths(B) == (1,1)
Expand All @@ -151,18 +161,18 @@ using BlockArrays: blockcolsupport
@test BandedBlockBandedMatrix(B') == B'
@test MemoryLayout(Symmetric(B)) isa LazyBandedMatrices.LazyBandedBlockBandedLayout
@test MemoryLayout(Hermitian(B)) isa LazyBandedMatrices.LazyBandedBlockBandedLayout

C = BroadcastMatrix(*, 2, im*A)
@test MemoryLayout(C') isa LazyBandedMatrices.LazyBandedBlockBandedLayout
@test MemoryLayout(transpose(C)) isa LazyBandedMatrices.LazyBandedBlockBandedLayout

E = BroadcastMatrix(*, A, 2)
@test MemoryLayout(E) == BroadcastBandedBlockBandedLayout{typeof(*)}()

D = Diagonal(PseudoBlockArray(randn(6),1:3))
@test MemoryLayout(BroadcastMatrix(*, A, D)) isa BroadcastBandedBlockBandedLayout{typeof(*)}
@test MemoryLayout(BroadcastMatrix(*, D, A)) isa BroadcastBandedBlockBandedLayout{typeof(*)}

F = BroadcastMatrix(*, Ones(axes(A,1)), A)
@test blockbandwidths(F) == (1,1)
@test subblockbandwidths(F) == (1,1)
Expand Down Expand Up @@ -208,4 +218,6 @@ using BlockArrays: blockcolsupport
@test paddeddata(p) == [1:5; 0]
@test blocksize(paddeddata(p),1) == 3
end
end
end

end # module
18 changes: 12 additions & 6 deletions test/test_blockconcat.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
module TestBlockConcat

using LazyBandedMatrices, BlockBandedMatrices, BlockArrays, StaticArrays, FillArrays, LazyArrays, ArrayLayouts, BandedMatrices, Test
import LazyBandedMatrices: BlockBroadcastArray, ApplyLayout, blockcolsupport, blockrowsupport, arguments, paddeddata, resizedata!, BlockVec
import LazyBandedMatrices: BlockBroadcastArray, blockcolsupport, blockrowsupport, arguments, paddeddata, resizedata!, BlockVec
import BlockArrays: blockvec
import LinearAlgebra: Adjoint, Transpose
import LazyArrays: PaddedArray, PaddedLayout
using LinearAlgebra
import LazyArrays: resizedata!, arguments, colsupport, rowsupport, LazyLayout,
PaddedLayout, paddeddata, ApplyLayout, PaddedArray


@testset "unitblocks" begin
a = unitblocks(Base.OneTo(5))
Expand Down Expand Up @@ -137,7 +141,7 @@ end
@testset "broadcast" begin
A = BlockVcat(randn(2,3), randn(1,3))
@test A + I isa BroadcastArray

a = BlockVcat(randn(2), randn(3))
@test a' .+ 1 isa BroadcastArray
end
Expand Down Expand Up @@ -403,7 +407,7 @@ end
@test blockcolsupport(A, Block(2)) == Block.(1:3)
@test blockrowsupport(A, Block(3)) == Block.(2:4)


V = view(A, Block.(1:3),Block.(1:3))
@test MemoryLayout(V) isa LazyBandedMatrices.BlockBandedInterlaceLayout
@test arguments(V) == (2,a[1:3,1:3],z[1:3,1:3],z[1:3,1:3],a[1:3,1:3])
Expand Down Expand Up @@ -452,4 +456,6 @@ end
@test MemoryLayout(c) isa PaddedLayout
@test paddeddata(c) isa BlockVec
@test paddeddata(c) == [2]
end
end

end # module
10 changes: 8 additions & 2 deletions test/test_blockkron.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
module TestBlockKron

using LazyBandedMatrices, FillArrays, BandedMatrices, BlockBandedMatrices, BlockArrays, ArrayLayouts, LazyArrays, Test
using LinearAlgebra
import BlockBandedMatrices: isbandedblockbanded, isbanded, BandedBlockBandedStyle, BandedLayout
import LazyBandedMatrices: KronTravBandedBlockBandedLayout, BroadcastBandedLayout, BroadcastBandedBlockBandedLayout, arguments, FillLayout, OnesLayout, call, blockcolsupport, InvDiagTrav, invdiagtrav
import LazyArrays: resizedata!
import LazyArrays: resizedata!, FillLayout, arguments, colsupport, call, LazyArrayStyle
import BandedMatrices: BandedColumns

include("mylazyarray.jl")

@testset "Kron" begin
@testset "Banded kron" begin
Expand Down Expand Up @@ -361,4 +365,6 @@ import BandedMatrices: BandedColumns
@test B == Δ
end
end
end
end

end # module
59 changes: 59 additions & 0 deletions test/test_misc.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
module TestMisc

using FillArrays
using BlockBandedMatrices
using BandedMatrices
using BlockArrays
using Test
using LazyArrays
import LazyArrays: PaddedLayout, paddeddata, call
using LinearAlgebra
using LazyBandedMatrices

@testset "Misc" begin

@testset "Diagonal interface" begin
n = 10
h = 1/n
= BandedMatrix(0 => Fill(-2,n), 1 => Fill(1,n-1), -1 => Fill(1,n-1))/h^2
D_xx = BandedBlockBandedMatrix(BlockKron(D², Eye(n)))

D = Diagonal(randn(n^2))
@test D_xx + D isa BandedBlockBandedMatrix
@test blockbandwidths(D_xx + D) == blockbandwidths(D_xx)
@test subblockbandwidths(D_xx + D) == subblockbandwidths(D_xx)
@test D_xx + D == Matrix(D_xx) + D

@test D_xx - D isa BandedBlockBandedMatrix
@test blockbandwidths(D_xx - D) == blockbandwidths(D_xx)
@test subblockbandwidths(D_xx - D) == subblockbandwidths(D_xx)
@test D_xx - D == Matrix(D_xx) - D

@test D_xx*D == Matrix(D_xx)*D
@test D_xx*D isa BandedBlockBandedMatrix
@test blockbandwidths(D_xx*D) == blockbandwidths(D_xx)
@test subblockbandwidths(D_xx*D) == subblockbandwidths(D_xx)

@test D*D_xx == D*Matrix(D_xx)
@test D*D_xx isa BandedBlockBandedMatrix
@test blockbandwidths(D*D_xx) == blockbandwidths(D_xx)
@test subblockbandwidths(D*D_xx) == subblockbandwidths(D_xx)
end

# @testset "Padded columns" begin
# B = brand(8,8,1,2)
# v = view(B,:,4)
# w = view(B,3,:)
# @test MemoryLayout(v) isa PaddedLayout
# @test_broken MemoryLayout(w) isa PaddedLayout
# @test paddeddata(v) isa Vcat
# paddeddata(v) == B[:,4]
# end

@testset "Block broadcast" begin
a = PseudoBlockArray(BroadcastArray(exp, randn(5)), [3,2])
@test call(a) == exp
end
end

end # module
Loading

0 comments on commit 8bfec16

Please sign in to comment.