Skip to content

Commit af9a627

Browse files
committed
add tests
1 parent c804b06 commit af9a627

File tree

5 files changed

+42
-17
lines changed

5 files changed

+42
-17
lines changed

test/blockmap.jl

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using Test, LinearMaps, LinearAlgebra, SparseArrays, BenchmarkTools, InteractiveUtils
2-
using LinearMaps: FiveArg, ThreeArg
2+
using LinearMaps: FiveArg
33

44
@testset "block maps" begin
55
@testset "hcat" begin
@@ -8,6 +8,10 @@ using LinearMaps: FiveArg, ThreeArg
88
A12 = rand(elty, 10, n2)
99
v = rand(elty, 10)
1010
L = @inferred hcat(LinearMap(A11), LinearMap(A12))
11+
@test L.maps isa Tuple
12+
Lv = @inferred LinearMaps.BlockMap{elty}([LinearMap(A11), LinearMap(A12)], (2,))
13+
@test Lv.maps isa Vector
14+
@test L == Lv == LinearMaps.BlockMap([LinearMap(A11), LinearMap(A12)], (2,))
1115
@test occursin("10×$(10+n2) LinearMaps.BlockMap{$elty}", sprint((t, s) -> show(t, "text/plain", s), L))
1216
@test @inferred(LinearMaps.MulStyle(L)) === FiveArg()
1317
@test L isa LinearMaps.BlockMap{elty}
@@ -16,9 +20,9 @@ using LinearMaps: FiveArg, ThreeArg
1620
end
1721
A = [A11 A12]
1822
x = rand(10+n2)
19-
@test size(L) == size(A)
20-
@test Matrix(L) A
21-
@test L * x A * x
23+
@test size(L) == size(A) == size(Lv)
24+
@test Matrix(L) A Matrix(Lv)
25+
@test L * x A * x Lv * x
2226
L = @inferred hcat(LinearMap(A11), LinearMap(A12), LinearMap(A11))
2327
A = [A11 A12 A11]
2428
@test Matrix(L) A
@@ -53,15 +57,19 @@ using LinearMaps: FiveArg, ThreeArg
5357
@test Matrix(L) A11
5458
A21 = rand(elty, 20, 10)
5559
L = @inferred vcat(LinearMap(A11), LinearMap(A21))
60+
@test L.maps isa Tuple
61+
Lv = LinearMaps.BlockMap{elty}([LinearMap(A11), LinearMap(A21)], (1,1))
62+
@test Lv.maps isa Vector
63+
@test L == Lv
5664
@test occursin("30×10 LinearMaps.BlockMap{$elty}", sprint((t, s) -> show(t, "text/plain", s), L))
5765
@test L isa LinearMaps.BlockMap{elty}
5866
@test @inferred(LinearMaps.MulStyle(L)) === FiveArg()
5967
@test (@which [A11; A21]).module != LinearMaps
6068
A = [A11; A21]
6169
x = rand(10)
6270
@test size(L) == size(A)
63-
@test Matrix(L) A
64-
@test L * x A * x
71+
@test Matrix(L) == Matrix(Lv) == A
72+
@test L * x Lv * x A * x
6573
A = [I; I; I; A11; A11; A11; v v v v v v v v v v]
6674
@test (@which [I; I; I; A11; A11; A11; v v v v v v v v v v]).module != LinearMaps
6775
L = @inferred vcat(I, I, I, LinearMap(A11), LinearMap(A11), LinearMap(A11), reduce(hcat, fill(v, 10)))
@@ -85,14 +93,17 @@ using LinearMaps: FiveArg, ThreeArg
8593
@test (@which [A11 A12; A21 A22]).module != LinearMaps
8694
@inferred hvcat((2,2), LinearMap(A11), LinearMap(A12), LinearMap(A21), LinearMap(A22))
8795
L = [LinearMap(A11) LinearMap(A12); LinearMap(A21) LinearMap(A22)]
96+
@test L.maps isa Tuple
97+
Lv = @inferred LinearMaps.BlockMap{elty}([LinearMap(A11), LinearMap(A12), LinearMap(A21), LinearMap(A22)], (2,2))
98+
@test Lv.maps isa Vector
8899
@test @inferred(LinearMaps.MulStyle(L)) === FiveArg()
89100
@test @inferred !issymmetric(L)
90101
@test @inferred !ishermitian(L)
91102
x = rand(30)
92103
@test L isa LinearMaps.BlockMap{elty}
93104
@test size(L) == size(A)
94-
@test L * x A * x
95-
@test Matrix(L) == A
105+
@test L * x Lv * x A * x
106+
@test Matrix(L) == Matrix(Lv) == A
96107
@test convert(AbstractMatrix, L) == A
97108
A = [I A12; A21 I]
98109
@test (@which [I A12; A21 I]).module != LinearMaps
@@ -202,6 +213,9 @@ using LinearMaps: FiveArg, ThreeArg
202213
@test (@which cat(M1, M2, M3, M2, M1; dims=(1,2))).module != LinearMaps
203214
x = randn(elty, size(Md, 2))
204215
Bd = @inferred blockdiag(L1, L2, L3, L2, L1)
216+
@test Bd.maps isa Tuple
217+
Bdv = @inferred LinearMaps.BlockDiagonalMap{elty}([L1, L2, L3, L2, L1])
218+
@test Bdv.maps isa Vector
205219
@test @inferred(LinearMaps.MulStyle(Bd)) === FiveArg()
206220
@test occursin("25×39 LinearMaps.BlockDiagonalMap{$elty}", sprint((t, s) -> show(t, "text/plain", s), Bd))
207221
@test Matrix(Bd) == Md
@@ -211,24 +225,26 @@ using LinearMaps: FiveArg, ThreeArg
211225
@test Matrix(@inferred blockdiag(L1, L2)) == blockdiag(sparse.((M1, M2))...)
212226
Bd2 = @inferred cat(L1, L2, L3, L2, L1; dims=(1,2))
213227
@test_throws ArgumentError cat(L1, L2, L3, L2, L1; dims=(2,2))
214-
@test Bd == Bd2
228+
@test Bd == Bdv == Bd2
215229
@test Bd == blockdiag(L1, M2, M3, M2, M1)
216230
@test size(Bd) == (25, 39)
217231
@test !issymmetric(Bd)
218232
@test !ishermitian(Bd)
219-
@test @inferred Bd * x Md * x
233+
@test (@inferred Bd * x) Bdv * x Md * x
220234
for transform in (identity, adjoint, transpose)
221235
@test Matrix(@inferred transform(Bd)) == transform(Md)
222236
@test Matrix(@inferred transform(LinearMap(Bd))) == transform(Md)
223237
end
224238
y = randn(elty, size(Md, 1))
225239
for α in (0, 1, rand(elty)), β in (0, 1, rand(elty))
226240
@test mul!(copy(y), Bd, x, α, β) y*β .+ Md*x*α
241+
@test mul!(copy(y), Bdv, x, α, β) y*β .+ Md*x*α
227242
end
228243
X = randn(elty, size(Md, 2), 10)
229244
Y = randn(elty, size(Md, 1), 10)
230245
for α in (0, 1, rand(elty)), β in (0, 1, rand(elty))
231246
@test mul!(copy(Y), Bd, X, α, β) Y*β .+ Md*X*α
247+
@test mul!(copy(Y), Bdv, X, α, β) Y*β .+ Md*X*α
232248
end
233249
end
234250
end

test/composition.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ using Test, LinearMaps, LinearAlgebra, SparseArrays
2323
F2 = F*F
2424
FC2 = FC*FC
2525
F4 = FC2 * F2
26+
F4v = @inferred LinearMaps.CompositeMap{ComplexF64}([FC2, F2])
2627
@test occursin("10×10 LinearMaps.CompositeMap{$(eltype(F4))}", sprint((t, s) -> show(t, "text/plain", s), F4))
2728
@test length(F4.maps) == 4
28-
@test @inferred F4 * v == @inferred F * (F * (F * (F * v)))
29+
@test (@inferred F4 * v) == F4v * v == (@inferred F * (F * (F * (F * v))))
2930
@test @inferred Matrix(M * transpose(M)) A * transpose(A)
3031
@test @inferred !isposdef(M * transpose(M))
3132
@test @inferred isposdef(LinearMap(M * M', isposdef=true))

test/kronecker.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ using Test, LinearMaps, LinearAlgebra, SparseArrays
88
LA = LinearMap(A)
99
LB = LinearMap(B)
1010
LK = @inferred kron(LA, LB)
11+
LKv = @inferred LinearMaps.KroneckerMap{ComplexF64}([LA, LB])
12+
@test LK * ones(6) LKv * ones(6)
13+
@test LKv.maps isa Vector
1114
@test kron(LA, 2LB) isa LinearMaps.ScaledMap
1215
@test kron(3LA, LB) isa LinearMaps.ScaledMap
1316
@test kron(3LA, 2LB) isa LinearMaps.ScaledMap

test/linearcombination.jl

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Test, LinearMaps, LinearAlgebra, SparseArrays, BenchmarkTools
2+
using LinearMaps: FiveArg
23

34
@testset "linear combinations" begin
45
CS! = LinearMap{ComplexF64}(cumsum!,
@@ -9,15 +10,20 @@ using Test, LinearMaps, LinearAlgebra, SparseArrays, BenchmarkTools
910
b = @benchmarkable mul!($u, $CS!, $v)
1011
@test run(b, samples=3).allocs == 0
1112
n = 10
12-
L = sum(fill(CS!, n))
13-
M = Matrix(L)
14-
@test M == LowerTriangular(fill(n, size(L)))
13+
L = @inferred sum(ntuple(_ -> CS!, n))
14+
Lv = @inferred LinearMaps.LinearCombination{ComplexF64}(fill(CS!, n))
15+
@test L == Lv
16+
M, Mv = Matrix.((L, Lv))
17+
@test M == Mv == LowerTriangular(fill(n, size(L)))
1518
@test_throws AssertionError LinearMaps.LinearCombination{Float64}((CS!, CS!))
16-
@test occursin("10×10 LinearMaps.LinearCombination{$(eltype(L))}", sprint((t, s) -> show(t, "text/plain", s), L))
17-
@test occursin("10×10 LinearMaps.LinearCombination{$(eltype(L))}", sprint((t, s) -> show(t, "text/plain", s), L+CS!))
19+
@test occursin("10×10 $LinearMaps.LinearCombination{$(eltype(L))}", sprint((t, s) -> show(t, "text/plain", s), L))
20+
@test occursin("10×10 $LinearMaps.LinearCombination{$(eltype(L))}", sprint((t, s) -> show(t, "text/plain", s), L+CS!))
1821
@test mul!(u, L, v) n * cumsum(v)
22+
@test mul!(u, Lv, v) n * cumsum(v)
1923
b = @benchmarkable mul!($u, $L, $v, 2, 2)
2024
@test run(b, samples=5).allocs <= 1
25+
b = @benchmarkable mul!($u, $Lv, $v, 2, 2)
26+
@test run(b, samples=5).allocs <= 1
2127
for α in (false, true, rand(ComplexF64)), β in (false, true, rand(ComplexF64))
2228
for transform in (identity, adjoint, transpose)
2329
@test mul!(copy(u), transform(L), v, α, β) transform(M)*v*α + u*β

test/runtests.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Test, LinearMaps, Aqua
2-
using LinearMaps: FiveArg, ThreeArg
32

43
@testset "code quality" begin
54
Aqua.test_all(LinearMaps)

0 commit comments

Comments
 (0)