Skip to content

Commit 2d03f84

Browse files
committed
improve test coverage
1 parent bb6486c commit 2d03f84

File tree

7 files changed

+111
-28
lines changed

7 files changed

+111
-28
lines changed

src/concrete/basic.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
"Supertype of identity maps."
32
abstract type IdentityMap{T} <: Map{T} end
43

src/generic/jacobian.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ DeterminantMap{T}(m::Map{S}) where {S,T} = DeterminantMap{T}(convert(Map{T}, m))
3535

3636
determinantmap(m) = DeterminantMap(m)
3737

38-
applymap(m::DeterminantMap, x) = det(supermap(m)(x))
38+
applymap(m::DeterminantMap, x) = det(applymap(supermap(m), x))
39+
40+
mapsize(m::DeterminantMap) = (1,size(supermap(m),2))
3941

4042

4143
"""
@@ -62,6 +64,8 @@ absmap(m) = AbsMap(m)
6264

6365
applymap(m::AbsMap, x) = abs(supermap(m)(x))
6466

67+
mapsize(m::AbsMap) = (1,size(supermap(m),2))
68+
6569

6670
"A lazy volume element evaluates to `diffvolume(m, x)` on the fly."
6771
struct LazyDiffVolume{T,M} <: SimpleLazyMap{T}
@@ -75,6 +79,8 @@ LazyDiffVolume{T}(m::Map{S}) where {S,T} = LazyDiffVolume{T}(convert(Map{T}, m))
7579

7680
applymap(m::LazyDiffVolume, x) = diffvolume(supermap(m), x)
7781

82+
mapsize(m::LazyDiffVolume) = (1, mapsize(supermap(m),2))
83+
7884
Display.displaystencil(m::LazyDiffVolume) = ["LazyDiffVolume(", supermap(m), ")"]
7985
show(io::IO, mime::MIME"text/plain", m::LazyDiffVolume) = composite_show(io, mime, m)
8086

@@ -162,7 +168,7 @@ zeromatrix(m, ::Type{T}, ::Type{U}) where {N,M,T<:StaticVector{N},U<:StaticVecto
162168
zeromatrix(m, ::Type{T}, ::Type{U}) where {T<:Number,M,U<:StaticVector{M}} =
163169
zero(SVector{M,promote_type(T,eltype(U))})
164170
zeromatrix(m, ::Type{T}, ::Type{U}) where {T<:AbstractVector,U<:Number} =
165-
transpose(zeros(promote_type(eltype(T),U), mapsize(m,1)))
171+
transpose(zeros(promote_type(eltype(T),U), mapsize(m,2)))
166172

167173

168174
"Return a zero vector of the same size as the codomain of the map."

src/generic/lazy.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ struct WrappedMap{T,M} <: DerivedMap{T}
3434
map :: M
3535
end
3636
WrappedMap{T}(map) where {T} = WrappedMap{T,typeof(map)}(map)
37-
WrappedMap(map) = WrappedMap{Float64}(map)
37+
WrappedMap(map) = WrappedMap{domaintype(map)}(map)
3838

3939
similarmap(m::WrappedMap, ::Type{T}) where {T} = WrappedMap{T}(m)
4040

src/generic/map.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ codomaintype(m) = codomaintype(m, domaintype(m))
3838
codomaintype(m, ::Type{T}) where {T} = _codomaintype(typeof(m), T)
3939
_codomaintype(::Type{M}, ::Type{T}) where {M,T} = Base.promote_op(applymap, M, T)
4040
_codomaintype(::Type{M}, ::Type{Any}) where {M} = Any
41-
_codomaintype(M::Type{<:TypedMap{T,U}}, ::Type{T}) where {T,U} = U
42-
_codomaintype(M::Type{<:TypedMap{T,U}}, ::Type{Any}) where {T,U} = U
41+
_codomaintype(::Type{<:TypedMap{T,U}}, ::Type{T}) where {T,U} = U
42+
_codomaintype(::Type{<:TypedMap{T,U}}, ::Type{Any}) where {T,U} = U
4343

4444
prectype(::Type{<:Map{T}}) where T = prectype(T)
4545
numtype(::Type{<:Map{T}}) where T = numtype(T)

test/test_affine.jl

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,48 @@
11
using FunctionMaps:
2-
to_matrix, to_vector,
2+
to_matrix,
3+
to_vector,
4+
zeromatrix,
5+
zerovector,
36
matrix_pinv
47

58
function test_affine_maps(T)
69
A = rand(T,2,2)
7-
@test FunctionMaps.to_matrix(Vector{T}, A) == A
8-
@test FunctionMaps.to_matrix(T, 2) == 2
9-
@test FunctionMaps.to_matrix(SVector{2,T}, 2) == SMatrix{2,2}(2,0,0,2)
10-
@test FunctionMaps.to_matrix(SVector{2,T}, LinearAlgebra.I) == SMatrix{2,2}(1,0,0,1)
11-
@test FunctionMaps.to_matrix(Vector{T}, 2) == UniformScaling(2)
12-
@test FunctionMaps.to_matrix(Vector{T}, LinearAlgebra.I) == LinearAlgebra.I
10+
@test to_matrix(Vector{T}, A) == A
11+
@test to_matrix(T, 2) == 2
12+
@test to_matrix(SVector{2,T}, 2) == SMatrix{2,2}(2,0,0,2)
13+
@test to_matrix(SVector{2,T}, LinearAlgebra.I) == SMatrix{2,2}(1,0,0,1)
14+
@test to_matrix(Vector{T}, 2) == UniformScaling(2)
15+
@test to_matrix(T, LinearAlgebra.I) == one(T)
16+
@test to_matrix(Vector{T}, LinearAlgebra.I) == LinearAlgebra.I
1317
# test fallback with nonsensical call
14-
@test FunctionMaps.to_matrix(Tuple{Int}, 2) == 2
15-
16-
@test FunctionMaps.to_matrix(T, A, 2) == A
17-
@test FunctionMaps.to_matrix(T, 2, 3) == 2
18-
@test FunctionMaps.to_matrix(T, UniformScaling(2), 3) == 2
19-
@test FunctionMaps.to_matrix(T, LinearAlgebra.I, zero(T)) isa T
20-
@test FunctionMaps.to_matrix(SVector{2,T}, 2, SVector(1,1)) == SMatrix{2,2}(2,0,0,2)
21-
@test FunctionMaps.to_matrix(Vector{T}, 2, [1,2]) == [2 0 ; 0 2]
22-
23-
@test FunctionMaps.to_vector(T, 2) == 0
24-
@test FunctionMaps.to_vector(SVector{2,T}, 2) == SVector(0,0)
25-
@test FunctionMaps.to_vector(Vector{T}, A) == [0,0]
26-
@test FunctionMaps.to_vector(T, 2, 3) == 3
18+
@test to_matrix(Tuple{Int}, 2) == 2
19+
20+
@test to_matrix(Tuple{Int}, 2, 3) == 2
21+
@test to_matrix(T, A, 2) == A
22+
@test to_matrix(T, 2, 3) == 2
23+
@test to_matrix(T, UniformScaling(2), 3) == 2
24+
@test to_matrix(T, LinearAlgebra.I, zero(T)) isa T
25+
@test to_matrix(SVector{2,T}, 2, SVector(1,1)) == SMatrix{2,2}(2,0,0,2)
26+
@test to_matrix(Vector{T}, 2, [1,2]) == [2 0 ; 0 2]
27+
28+
@test to_vector(T, 2) == 0
29+
@test to_vector(SVector{2,T}, 2) == SVector(0,0)
30+
@test to_vector(Vector{T}, A) == [0,0]
31+
@test to_vector(T, 2, 3) == 3
32+
33+
@test zeromatrix(LinearMap(2.0)) == 0
34+
@test zeromatrix(LinearMap([2.0])) == [0.0]
35+
@test zeromatrix(LinearMap(SA[2.0])) isa SVector
36+
@test zeromatrix(LinearMap(SA[2.0])) == [0.0]
37+
@test zeromatrix(LinearMap(SA[2.0])) isa SVector
38+
@test zeromatrix(LinearMap(SA[2 1; 1 2])) isa SMatrix{2,2}
39+
@test zeromatrix(LinearMap(SA[2 1; 1 2])) == [0 0; 0 0]
40+
@test zeromatrix(ConstantMap{SVector{2,Float64}}(2.0)) isa Transpose{Float64,SVector{2,Float64}}
41+
@test zeromatrix(ConstantMap{SVector{2,Float64}}(2.0)) == [0 0]
42+
@test zeromatrix(LinearMap{SVector{2,Int}}(transpose(SA[1, 2]))) isa Transpose{Int,SVector{2,Int}}
43+
@test zeromatrix(LinearMap{SVector{2,Int}}(transpose(SA[1, 2]))) == [0 0]
44+
@test zeromatrix(LinearMap(transpose([1, 2]))) isa Transpose{Int,Vector{Int}}
45+
@test zeromatrix(LinearMap(transpose([1, 2]))) == [0 0]
2746

2847
if T != BigFloat # BigFloat's make pinv fail for StaticArrays
2948
@test FunctionMaps.matrix_pinv(SMatrix{2,2}(rand(T),rand(T),rand(T),rand(T))) isa SMatrix{2,2}

test/test_basic.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ function test_wrapped_maps(T)
111111
m3 = m1 m2
112112
@test m3(one(T)) cos(sin(one(T)))
113113

114-
@test WrappedMap(cos) isa WrappedMap{Float64}
114+
@test WrappedMap(cos) isa WrappedMap{Any}
115115
@test convert(Map, cos) isa WrappedMap
116116
@test convert(Map{BigFloat}, m1) isa WrappedMap{BigFloat}
117117

test/test_generic.jl

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
using FunctionMaps:
22
convert_domaintype, convert_codomaintype,
33
map_hash,
4-
LazyInverse, jacobian!
4+
LazyInverse, jacobian!,
5+
determinantmap,
6+
absmap,
7+
promote_maps,
8+
ScalarLinearMap
59

610
function generic_map_tests(T)
711
for map in maps_to_test(T)
@@ -134,7 +138,53 @@ function test_generic_jacobian(m)
134138
end
135139
end
136140

141+
function test_generic_functionality_inverse()
142+
m = inverse(cos)
143+
@test m isa LazyInverse
144+
@test inverse(m) == cos
145+
@test !FunctionMaps.implements_inverse(cos)
146+
@test isequalmap(inverse(cos), inverse(cos))
147+
@test map_hash(m) == 0xe007467f7ac77adc
148+
end
149+
150+
function test_generic_functionality_jacobian()
151+
@test jacobian(cos) isa FunctionMaps.LazyJacobian
152+
m_jac = jacobian(cos)
153+
@test domaintype(m_jac) == Any
154+
@test_throws MethodError mapsize(m_jac)
155+
156+
@test determinantmap(cos) isa FunctionMaps.DeterminantMap
157+
@test FunctionMaps.DeterminantMap{Float64}(LinearMap(2)) isa FunctionMaps.DeterminantMap{Float64}
158+
m_det = determinantmap(cos)
159+
@test domaintype(m_det) == Any
160+
@test m_det(4.0) == det(cos(4.0))
161+
162+
@test absmap(cos) isa FunctionMaps.AbsMap
163+
@test FunctionMaps.AbsMap{Float64}(LinearMap(2)) isa FunctionMaps.AbsMap{Float64}
164+
m_abs = absmap(cos)
165+
@test domaintype(m_abs) == Any
166+
@test m_abs(4.0) == abs(cos(4.0))
167+
168+
@test diffvolume(cos) isa FunctionMaps.LazyDiffVolume
169+
@test FunctionMaps.LazyDiffVolume{Float64}(LinearMap(2)) isa FunctionMaps.LazyDiffVolume{Float64}
170+
m_dvol = diffvolume(cos)
171+
@test_throws MethodError applymap(m_dvol, 2.0)
172+
end
173+
174+
137175
function test_generic_functionality()
176+
@test Map(cos) isa FunctionMaps.WrappedMap{Any}
177+
@test Map{Float64}(cos) isa FunctionMaps.WrappedMap{Float64}
178+
179+
@test FunctionMaps.isvectorvalued_type(Float64)
180+
@test FunctionMaps.isvectorvalued_type(Vector{Float64})
181+
@test !FunctionMaps.isvectorvalued_type(Symbol)
182+
183+
@test FunctionMaps.is_scalar_to_scalar(LinearMap(2))
184+
@test FunctionMaps.is_scalar_to_vector(LinearMap([2,2]))
185+
@test FunctionMaps.is_vector_to_vector(LinearMap(rand(2,2)))
186+
@test FunctionMaps.is_vector_to_scalar(ConstantMap{SVector{2,Float64}}(2))
187+
138188
m = LinearMap(2)
139189
@test convert_domaintype(Float64, m) isa ScalarLinearMap{Float64}
140190
@test convert_domaintype(Complex{Float64}, m) isa ScalarLinearMap{Complex{Float64}}
@@ -147,4 +197,13 @@ function test_generic_functionality()
147197
m2 = LinearMap{SVector{2,Float64}}(2)
148198
@test convert_domaintype(SVector{2,BigFloat}, m2) isa GenericLinearMap{SVector{2, BigFloat}, BigFloat}
149199
@test convert_codomaintype(SVector{2,BigFloat}, m2) isa GenericLinearMap{SVector{2, BigFloat}, BigFloat}
200+
201+
@test promote_maps() == ()
202+
@test promote_maps(cos) == cos
203+
@test FunctionMaps.promotable_maps(LinearMap(2), LinearMap(2.0))
204+
@test promote_maps(LinearMap(2), LinearMap(2.0)) isa Tuple{ScalarLinearMap{Float64}, ScalarLinearMap{Float64}}
205+
@test promote_maps(LinearMap(2), LinearMap(2.0), LinearMap(2.0)) isa Tuple{ScalarLinearMap{Float64}, ScalarLinearMap{Float64}, ScalarLinearMap{Float64}}
206+
207+
test_generic_functionality_inverse()
208+
test_generic_functionality_jacobian()
150209
end

0 commit comments

Comments
 (0)