Skip to content

Commit

Permalink
Make tests modular (#386)
Browse files Browse the repository at this point in the history
  • Loading branch information
jishnub authored Apr 7, 2024
1 parent 64339dc commit b60aa48
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 17 deletions.
16 changes: 10 additions & 6 deletions test/test_blockarrayinterface.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using BlockArrays, LinearAlgebra, FillArrays, Test
module TestBlockArrayInterface

using BlockArrays, LinearAlgebra, FillArrays, Test, ArrayLayouts

# avoid fast-paths for view
bview(a, b) = Base.invoke(view, Tuple{AbstractArray,Any}, a, b)
Expand Down Expand Up @@ -51,7 +53,7 @@ end
@test H[Block(2, 3)] == A[2:3, 4:6]
@test H[Block(3, 2)] == A[2:3, 4:6]'

@test sprint(show, "text/plain", UpperTriangular(A)) == "10×10 UpperTriangular{ComplexF64, PseudoBlockMatrix{ComplexF64, Matrix{ComplexF64}, $(typeof(axes(A)))}} with indices 1:1:10×1:1:10:\n 1.0+0.0im │ 11.0+0.0im 21.0+0.0im │ 31.0+0.0im 41.0+0.0im 51.0+0.0im │ 61.0+0.0im 71.0+0.0im 81.0+0.0im 91.0+0.0im\n ───────────┼──────────────────────────┼──────────────────────────────────────┼─────────────────────────────────────────────────\n ⋅ │ 12.0+0.0im 22.0+0.0im │ 32.0+0.0im 42.0+0.0im 52.0+0.0im │ 62.0+0.0im 72.0+0.0im 82.0+0.0im 92.0+0.0im\n ⋅ │ ⋅ 23.0+0.0im │ 33.0+0.0im 43.0+0.0im 53.0+0.0im │ 63.0+0.0im 73.0+0.0im 83.0+0.0im 93.0+0.0im\n ───────────┼──────────────────────────┼──────────────────────────────────────┼─────────────────────────────────────────────────\n ⋅ │ ⋅ ⋅ │ 34.0+0.0im 44.0+0.0im 54.0+0.0im │ 64.0+0.0im 74.0+0.0im 84.0+0.0im 94.0+0.0im\n ⋅ │ ⋅ ⋅ │ ⋅ 45.0+0.0im 55.0+0.0im │ 65.0+0.0im 75.0+0.0im 85.0+0.0im 95.0+0.0im\n ⋅ │ ⋅ ⋅ │ ⋅ ⋅ 56.0+0.0im │ 66.0+0.0im 76.0+0.0im 86.0+0.0im 96.0+0.0im\n ───────────┼──────────────────────────┼──────────────────────────────────────┼─────────────────────────────────────────────────\n ⋅ │ ⋅ ⋅ │ ⋅ ⋅ ⋅ │ 67.0+0.0im 77.0+0.0im 87.0+0.0im 97.0+0.0im\n ⋅ │ ⋅ ⋅ │ ⋅ ⋅ ⋅ │ ⋅ 78.0+0.0im 88.0+0.0im 98.0+0.0im\n ⋅ │ ⋅ ⋅ │ ⋅ ⋅ ⋅ │ ⋅ ⋅ 89.0+0.0im 99.0+0.0im\n ⋅ │ ⋅ ⋅ │ ⋅ ⋅ ⋅ │ ⋅ ⋅ ⋅ 100.0+0.0im"
@test sprint(show, "text/plain", UpperTriangular(A)) == "10×10 $UpperTriangular{ComplexF64, $(PseudoBlockMatrix{ComplexF64, Matrix{ComplexF64}, typeof(axes(A))})} with indices 1:1:10×1:1:10:\n 1.0+0.0im │ 11.0+0.0im 21.0+0.0im │ 31.0+0.0im 41.0+0.0im 51.0+0.0im │ 61.0+0.0im 71.0+0.0im 81.0+0.0im 91.0+0.0im\n ───────────┼──────────────────────────┼──────────────────────────────────────┼─────────────────────────────────────────────────\n ⋅ │ 12.0+0.0im 22.0+0.0im │ 32.0+0.0im 42.0+0.0im 52.0+0.0im │ 62.0+0.0im 72.0+0.0im 82.0+0.0im 92.0+0.0im\n ⋅ │ ⋅ 23.0+0.0im │ 33.0+0.0im 43.0+0.0im 53.0+0.0im │ 63.0+0.0im 73.0+0.0im 83.0+0.0im 93.0+0.0im\n ───────────┼──────────────────────────┼──────────────────────────────────────┼─────────────────────────────────────────────────\n ⋅ │ ⋅ ⋅ │ 34.0+0.0im 44.0+0.0im 54.0+0.0im │ 64.0+0.0im 74.0+0.0im 84.0+0.0im 94.0+0.0im\n ⋅ │ ⋅ ⋅ │ ⋅ 45.0+0.0im 55.0+0.0im │ 65.0+0.0im 75.0+0.0im 85.0+0.0im 95.0+0.0im\n ⋅ │ ⋅ ⋅ │ ⋅ ⋅ 56.0+0.0im │ 66.0+0.0im 76.0+0.0im 86.0+0.0im 96.0+0.0im\n ───────────┼──────────────────────────┼──────────────────────────────────────┼─────────────────────────────────────────────────\n ⋅ │ ⋅ ⋅ │ ⋅ ⋅ ⋅ │ 67.0+0.0im 77.0+0.0im 87.0+0.0im 97.0+0.0im\n ⋅ │ ⋅ ⋅ │ ⋅ ⋅ ⋅ │ ⋅ 78.0+0.0im 88.0+0.0im 98.0+0.0im\n ⋅ │ ⋅ ⋅ │ ⋅ ⋅ ⋅ │ ⋅ ⋅ 89.0+0.0im 99.0+0.0im\n ⋅ │ ⋅ ⋅ │ ⋅ ⋅ ⋅ │ ⋅ ⋅ ⋅ 100.0+0.0im"

V = view(A, Block.(1:2), Block.(1:2))
@test blockisequal(axes(Symmetric(V)), axes(view(A, Block.(1:2), Block.(1:2))))
Expand Down Expand Up @@ -130,7 +132,7 @@ end
@test_throws BlockBoundsError A[Block(1, 3)]
@test A == [1 2 0 0; 0 0 1 2]
@test BlockArray(A) == A
@test sprint(show, "text/plain", A) == "2×2-blocked 2×4 BlockMatrix{$Int, Diagonal{Matrix{$Int}, Vector{Matrix{$Int}}}, Tuple{BlockedUnitRange{Vector{$Int}}, BlockedUnitRange{Vector{$Int}}}}:\n 1 2 │ ⋅ ⋅\n ──────┼──────\n ⋅ ⋅ │ 1 2"
@test sprint(show, "text/plain", A) == "2×2-blocked 2×4 $(BlockMatrix{Int, Diagonal{Matrix{Int}, Vector{Matrix{Int}}}, Tuple{BlockedUnitRange{Vector{Int}}, BlockedUnitRange{Vector{Int}}}}):\n 1 2 │ ⋅ ⋅\n ──────┼──────\n ⋅ ⋅ │ 1 2"

N = 3
D = Diagonal(mortar(Fill.(-(0:N) - (0:N) .^ 2, 1:2:2N+1)))
Expand Down Expand Up @@ -187,9 +189,9 @@ end

U = UpperTriangular(Ones((blockedrange([1, 2]), blockedrange([2, 1]))))

@test sprint(show, "text/plain", A) == "5-element Fill{$Int, 1, Tuple{BlockedUnitRange{Vector{$Int}}}} with indices 1:1:5, with entries equal to 2"
@test sprint(show, "text/plain", B) == "3×3 Diagonal{Float64, Ones{Float64, 1, Tuple{BlockedUnitRange{Vector{$Int}}}}} with indices 1:1:3×1:1:3"
@test sprint(show, "text/plain", U) == "3×3 UpperTriangular{Float64, Ones{Float64, 2, Tuple{BlockedUnitRange{Vector{$Int}}, BlockedUnitRange{Vector{$Int}}}}} with indices 1:1:3×1:1:3:\n 1.0 1.0 │ 1.0\n ──────────┼─────\n ⋅ 1.0 │ 1.0\n ⋅ ⋅ │ 1.0"
@test sprint(show, "text/plain", A) == "5-element $(Fill{Int, 1, Tuple{BlockedUnitRange{Vector{Int}}}}) with indices 1:1:5, with entries equal to 2"
@test sprint(show, "text/plain", B) == "3×3 $(Diagonal{Float64, Ones{Float64, 1, Tuple{BlockedUnitRange{Vector{Int}}}}}) with indices 1:1:3×1:1:3"
@test sprint(show, "text/plain", U) == "3×3 $(UpperTriangular{Float64, Ones{Float64, 2, Tuple{BlockedUnitRange{Vector{Int}}, BlockedUnitRange{Vector{Int}}}}}) with indices 1:1:3×1:1:3:\n 1.0 1.0 │ 1.0\n ──────────┼─────\n ⋅ 1.0 │ 1.0\n ⋅ ⋅ │ 1.0"

@testset "views" begin
# This in theory can be dropped because `view` returns the block, but we keep in case needed
Expand All @@ -200,3 +202,5 @@ end
@test FillArrays.getindex_value(bview(f, Block(2))) == 2
end
end

end # module
32 changes: 21 additions & 11 deletions test/test_blockarrays.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module TestBlockArrays

using SparseArrays, BlockArrays, FillArrays, LinearAlgebra, Test, OffsetArrays
import BlockArrays: _BlockArray

Expand Down Expand Up @@ -545,24 +547,30 @@ end
end

@testset "replstring" begin
@test sprint(show, "text/plain", BlockArray(collect(1:4), [1,3])) == "2-blocked 4-element BlockVector{$Int}:\n 1\n\n 2\n 3\n 4"
@test sprint(show, "text/plain", PseudoBlockArray(collect(1:4), [1,3])) == "2-blocked 4-element PseudoBlockVector{$Int}:\n 1\n\n 2\n 3\n 4"
@test sprint(show, "text/plain", BlockArray(collect(reshape(1:16, 4, 4)), [1,3], [2,2])) == "2×2-blocked 4×4 BlockMatrix{$Int}:\n 1 5 │ 9 13\n ──────┼────────\n 2 6 │ 10 14\n 3 7 │ 11 15\n 4 8 │ 12 16"
@test sprint(show, "text/plain", PseudoBlockArray(collect(reshape(1:16, 4, 4)), [1,3], [2,2])) == "2×2-blocked 4×4 PseudoBlockMatrix{$Int}:\n 1 5 │ 9 13\n ──────┼────────\n 2 6 │ 10 14\n 3 7 │ 11 15\n 4 8 │ 12 16"
@test sprint(show, "text/plain", BlockArray(collect(reshape(1:8, 2, 2, 2)), [1,1], [1,1], [1,1])) == "2×2×2-blocked 2×2×2 BlockArray{$Int, 3}:\n[:, :, 1] =\n 1 3\n 2 4\n\n[:, :, 2] =\n 5 7\n 6 8"
@test sprint(show, "text/plain", PseudoBlockArray(collect(reshape(1:8, 2, 2, 2)), [1,1], [1,1], [1,1])) == "2×2×2-blocked 2×2×2 PseudoBlockArray{$Int, 3}:\n[:, :, 1] =\n 1 3\n 2 4\n\n[:, :, 2] =\n 5 7\n 6 8"
A = BlockArray(collect(1:4), [1,3])
@test sprint(show, "text/plain", A) == "$(summary(A)):\n 1\n\n 2\n 3\n 4"
A = PseudoBlockArray(collect(1:4), [1,3])
@test sprint(show, "text/plain", A) == "$(summary(A)):\n 1\n\n 2\n 3\n 4"
A = BlockArray(collect(reshape(1:16, 4, 4)), [1,3], [2,2])
@test sprint(show, "text/plain", A) == "$(summary(A)):\n 1 5 │ 9 13\n ──────┼────────\n 2 6 │ 10 14\n 3 7 │ 11 15\n 4 8 │ 12 16"
A = PseudoBlockArray(collect(reshape(1:16, 4, 4)), [1,3], [2,2])
@test sprint(show, "text/plain", A) == "$(summary(A)):\n 1 5 │ 9 13\n ──────┼────────\n 2 6 │ 10 14\n 3 7 │ 11 15\n 4 8 │ 12 16"
A = BlockArray(collect(reshape(1:8, 2, 2, 2)), [1,1], [1,1], [1,1])
@test sprint(show, "text/plain", A) == "$(summary(A)):\n[:, :, 1] =\n 1 3\n 2 4\n\n[:, :, 2] =\n 5 7\n 6 8"
A = PseudoBlockArray(collect(reshape(1:8, 2, 2, 2)), [1,1], [1,1], [1,1])
@test sprint(show, "text/plain", A) == "$(summary(A)):\n[:, :, 1] =\n 1 3\n 2 4\n\n[:, :, 2] =\n 5 7\n 6 8"
design = zeros(Int16,6,9);
A = BlockArray(design,[6],[4,5])
@test sprint(show, "text/plain", A) == "1×2-blocked 6×9 BlockMatrix{Int16}:\n 0 0 0 0 │ 0 0 0 0 0\n 0 0 0 0 │ 0 0 0 0 0\n 0 0 0 0 │ 0 0 0 0 0\n 0 0 0 0 │ 0 0 0 0 0\n 0 0 0 0 │ 0 0 0 0 0\n 0 0 0 0 │ 0 0 0 0 0"
@test sprint(show, "text/plain", A) == "$(summary(A)):\n 0 0 0 0 │ 0 0 0 0 0\n 0 0 0 0 │ 0 0 0 0 0\n 0 0 0 0 │ 0 0 0 0 0\n 0 0 0 0 │ 0 0 0 0 0\n 0 0 0 0 │ 0 0 0 0 0\n 0 0 0 0 │ 0 0 0 0 0"
A = PseudoBlockArray(design,[6],[4,5])
@test sprint(show, "text/plain", A) == "1×2-blocked 6×9 PseudoBlockMatrix{Int16}:\n 0 0 0 0 │ 0 0 0 0 0\n 0 0 0 0 │ 0 0 0 0 0\n 0 0 0 0 │ 0 0 0 0 0\n 0 0 0 0 │ 0 0 0 0 0\n 0 0 0 0 │ 0 0 0 0 0\n 0 0 0 0 │ 0 0 0 0 0"
@test sprint(show, "text/plain", A) == "$(summary(A)):\n 0 0 0 0 │ 0 0 0 0 0\n 0 0 0 0 │ 0 0 0 0 0\n 0 0 0 0 │ 0 0 0 0 0\n 0 0 0 0 │ 0 0 0 0 0\n 0 0 0 0 │ 0 0 0 0 0\n 0 0 0 0 │ 0 0 0 0 0"
D = PseudoBlockArray(Diagonal(1:3), [1,2], [2,1])
@test sprint(show, "text/plain", D) == "2×2-blocked 3×3 $(PseudoBlockMatrix{Int, Diagonal{Int, UnitRange{Int}}, Tuple{BlockedUnitRange{Vector{Int}}, BlockedUnitRange{Vector{Int}}}}):\n 1 ⋅ │ ⋅\n ──────┼───\n ⋅ 2 │ ⋅\n ⋅ ⋅ │ 3"
@test sprint(show, "text/plain", D) == "$(summary(D)):\n 1 ⋅ │ ⋅\n ──────┼───\n ⋅ 2 │ ⋅\n ⋅ ⋅ │ 3"

a = BlockArray{Int}(undef_blocks, [1,2])
@test sprint(show, "text/plain", a) == "2-blocked 3-element BlockVector{Int64}:\n #undef\n ──────\n #undef\n #undef"
@test sprint(show, "text/plain", a) == "$(summary(a)):\n #undef\n ──────\n #undef\n #undef"
B = BlockArray{Int}(undef_blocks, [1,2], [1,1])
@test sprint(show, "text/plain", B) == "2×2-blocked 3×2 BlockMatrix{Int64}:\n #undef │ #undef\n ────────┼────────\n #undef │ #undef\n #undef │ #undef"
@test sprint(show, "text/plain", B) == "$(summary(B)):\n #undef │ #undef\n ────────┼────────\n #undef │ #undef\n #undef │ #undef"
end

@testset "AbstractVector{Int} blocks" begin
Expand Down Expand Up @@ -781,3 +789,5 @@ end
@test size(a[:,1]) == (8,)
end
end

end # module
4 changes: 4 additions & 0 deletions test/test_blockbroadcast.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module TestBlockBroadcast

using BlockArrays, FillArrays, Test
import BlockArrays: SubBlockIterator, BlockIndexRange, Diagonal

Expand Down Expand Up @@ -291,3 +293,5 @@ import BlockArrays: SubBlockIterator, BlockIndexRange, Diagonal
@test w == reverse(v)
end
end

end # module
3 changes: 3 additions & 0 deletions test/test_blockcholesky.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module TestBlockCholesky

using BlockArrays, Test, LinearAlgebra, Random


Expand Down Expand Up @@ -60,3 +62,4 @@ Random.seed!(0)
@test cholesky(E8; check=false).info == 8
end

end # module
4 changes: 4 additions & 0 deletions test/test_blockdeque.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module TestBlockDeque

using BlockArrays, Test

@testset "block dequeue" begin
Expand Down Expand Up @@ -192,3 +194,5 @@ using BlockArrays, Test
end
end
end

end # module
4 changes: 4 additions & 0 deletions test/test_blockindices.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module TestBlockIndices

using BlockArrays, FillArrays, Test, StaticArrays, ArrayLayouts
using OffsetArrays
import BlockArrays: BlockIndex, BlockIndexRange, BlockSlice
Expand Down Expand Up @@ -516,3 +518,5 @@ end
@test !blockisequal(axB, axv)
@test !blockisequal(axv, axB)
end

end # module
6 changes: 6 additions & 0 deletions test/test_blocklinalg.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
module TestBlockLinalg

using BlockArrays, ArrayLayouts, LinearAlgebra, Test
import BlockArrays: BlockLayout
import ArrayLayouts: DenseRowMajor, ColumnMajor, StridedLayout

bview(a, b) = Base.invoke(view, Tuple{AbstractArray,Any}, a, b)

@testset "Linear Algebra" begin
@testset "BlockArray scalar * matrix" begin
A = BlockArray{Float64}(randn(6,6), fill(2,3), 1:3)
Expand Down Expand Up @@ -248,3 +252,5 @@ import ArrayLayouts: DenseRowMajor, ColumnMajor, StridedLayout
@test mul!(view(copy(C),:,1:3), Ã, B̃, 1, 2) A*B + 2C
end
end

end # module
4 changes: 4 additions & 0 deletions test/test_blockproduct.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module TestBlockProduct

using BlockArrays, Test

@testset "block product" begin
Expand Down Expand Up @@ -143,3 +145,5 @@ using BlockArrays, Test
@test_throws MethodError BlockKron(a)
end
end

end # module
4 changes: 4 additions & 0 deletions test/test_blockrange.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module TestBlockRange

using BlockArrays, Test

@testset "block range" begin
Expand Down Expand Up @@ -99,3 +101,5 @@ end
@test size(bi) == (2,2)
end
end

end # module
4 changes: 4 additions & 0 deletions test/test_blockreduce.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module TestBlockReduce

using BlockArrays, Test

@testset "foldl" begin
Expand Down Expand Up @@ -25,3 +27,5 @@ end
@test blockisequal(axes(A,2), axes(sum(A; dims=1),2))
@test blockisequal(axes(A,1), axes(sum(A; dims=2),1))
end

end # module
4 changes: 4 additions & 0 deletions test/test_blocks.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module TestBlocks

using Test, BlockArrays

@testset "blocks" begin
Expand Down Expand Up @@ -102,3 +104,5 @@ using Test, BlockArrays
@test parent(m) === parent(b).blocks # special path works
end
end

end # module
5 changes: 5 additions & 0 deletions test/test_blockviews.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
module TestBlockViews

using BlockArrays, ArrayLayouts, Test
using FillArrays

# useds to force SubArray return
bview(a, b) = Base.invoke(view, Tuple{AbstractArray,Any}, a, b)
Expand Down Expand Up @@ -348,3 +351,5 @@ bview(a, b) = Base.invoke(view, Tuple{AbstractArray,Any}, a, b)
@test v[Block(1)] == a[Block(1)]
end
end

end # module

0 comments on commit b60aa48

Please sign in to comment.