From 8bfec165c9f66690be21e7d07d15f4ead4bac5b5 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Sat, 6 Apr 2024 23:33:50 +0530 Subject: [PATCH] Make tests modular (#116) --- test/mylazyarray.jl | 10 ++++++ test/runtests.jl | 76 ++-------------------------------------- test/test_banded.jl | 20 ++++++++++- test/test_bidiag.jl | 8 +++-- test/test_block.jl | 42 ++++++++++++++-------- test/test_blockconcat.jl | 18 ++++++---- test/test_blockkron.jl | 10 ++++-- test/test_misc.jl | 59 +++++++++++++++++++++++++++++++ test/test_special.jl | 4 +++ test/test_tridiag.jl | 5 +++ 10 files changed, 152 insertions(+), 100 deletions(-) create mode 100644 test/mylazyarray.jl create mode 100644 test/test_misc.jl diff --git a/test/mylazyarray.jl b/test/mylazyarray.jl new file mode 100644 index 0000000..b1d6657 --- /dev/null +++ b/test/mylazyarray.jl @@ -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) diff --git a/test/runtests.jl b/test/runtests.jl index d9ff20c..35bd96f 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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 - D² = 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") diff --git a/test/test_banded.jl b/test/test_banded.jl index c084b31..2ecd4a8 100644 --- a/test/test_banded.jl +++ b/test/test_banded.jl @@ -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} @@ -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 \ No newline at end of file +end + +end # module diff --git a/test/test_bidiag.jl b/test/test_bidiag.jl index 9cfd7ab..338f161 100644 --- a/test/test_bidiag.jl +++ b/test/test_bidiag.jl @@ -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! @@ -482,4 +484,6 @@ import LinearAlgebra: mul! @test B.dv ≡ 1:5 @test B.ev ≡ Ones{Int}(4) end -end # module TestBidiagonal \ No newline at end of file +end # testset + +end # module TestBidiagonal diff --git a/test/test_block.jl b/test/test_block.jl index 6e906e5..f938185 100644 --- a/test/test_block.jl +++ b/test/test_block.jl @@ -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 @@ -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))) @@ -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 @@ -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] @@ -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) @@ -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) @@ -208,4 +218,6 @@ using BlockArrays: blockcolsupport @test paddeddata(p) == [1:5; 0] @test blocksize(paddeddata(p),1) == 3 end -end \ No newline at end of file +end + +end # module diff --git a/test/test_blockconcat.jl b/test/test_blockconcat.jl index 76ec92e..39495ca 100644 --- a/test/test_blockconcat.jl +++ b/test/test_blockconcat.jl @@ -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)) @@ -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 @@ -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]) @@ -452,4 +456,6 @@ end @test MemoryLayout(c) isa PaddedLayout @test paddeddata(c) isa BlockVec @test paddeddata(c) == [2] -end \ No newline at end of file +end + +end # module diff --git a/test/test_blockkron.jl b/test/test_blockkron.jl index c830fe6..3e34a49 100644 --- a/test/test_blockkron.jl +++ b/test/test_blockkron.jl @@ -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 @@ -361,4 +365,6 @@ import BandedMatrices: BandedColumns @test B == Δ end end -end \ No newline at end of file +end + +end # module diff --git a/test/test_misc.jl b/test/test_misc.jl new file mode 100644 index 0000000..8c58dc7 --- /dev/null +++ b/test/test_misc.jl @@ -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 + D² = 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 diff --git a/test/test_special.jl b/test/test_special.jl index a86348d..635ee76 100644 --- a/test/test_special.jl +++ b/test/test_special.jl @@ -1,8 +1,11 @@ # This file is based on LinearAlgebra/test/special.jl a part of Julia. License is MIT: https://julialang.org/license +module TestSpecial + using Test, SparseArrays, Random, LazyBandedMatrices import LazyBandedMatrices: Bidiagonal, Tridiagonal, SymTridiagonal import LinearAlgebra +import LinearAlgebra: Diagonal, UpperTriangular, LowerTriangular, triu, Symmetric import LinearAlgebra: UniformScaling @testset "Tri/Bidiagonal special" begin @@ -272,5 +275,6 @@ import LinearAlgebra: UniformScaling end end end +end # testset end # module TestSpecial diff --git a/test/test_tridiag.jl b/test/test_tridiag.jl index b33624c..11ad5da 100644 --- a/test/test_tridiag.jl +++ b/test/test_tridiag.jl @@ -1,4 +1,7 @@ # This file based on a part of Julia, LinearAlgebra/test/tridiag.jl. License is MIT: https://julialang.org/license + +module TestTridiagonal + using Test, SparseArrays, Random, LazyBandedMatrices, FillArrays, LazyArrays import LinearAlgebra # need to avoid confusion with LinearAlgebra.Tridiagonal @@ -458,4 +461,6 @@ import LazyBandedMatrices: SymTridiagonal, Tridiagonal B = randn(5,5) @test SymTridiagonal(ApplyArray(*, A, B)) ≈ SymTridiagonal(A*B) end +end # testset + end # module TestTridiagonal