Skip to content

Commit bb6486c

Browse files
committed
more testing
1 parent bbfc8b2 commit bb6486c

14 files changed

+156
-75
lines changed

src/FunctionMaps.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ export canonicalmap
2727
# from generic/composite.jl
2828
export composedmap
2929
# from generic/product.jl
30-
export ProductMap, productmap
30+
export ProductMap, productmap,
31+
factors, nfactors, factor
3132

3233
# from concrete/basic.jl
3334
export IdentityMap,

src/concrete/affine/special.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
##############################
32
# Numbers and arrays as a map
43
##############################

src/generic/product.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
"""
32
A product map is diagonal and acts on each of the components of x separately:
43
`y = f(x)` becomes `y_i = f_i(x_i)`.
@@ -85,6 +84,8 @@ canonicalmap(m::ProductMap) = any(map(hascanonicalmap, factors(m))) ?
8584
ProductMap(map(canonicalmap, factors(m))) : m
8685
canonicalmap(::Equal, m::ProductMap) = any(map(hasequalmap, factors(m))) ?
8786
ProductMap(map(equalmap, factors(m))) : m
87+
canonicalmap(::Equivalent, m::ProductMap{NTuple{N,T}}) where {N,T} =
88+
equivalentmap(convert(Map{SVector{N,T}}, m))
8889
canonicalmap(::Equivalent, m::ProductMap) = any(map(hasequivalentmap, factors(m))) ?
8990
ProductMap(map(equivalentmap, factors(m))) : m
9091

src/util/common.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,20 @@ isrealtype(::Type{Any}) = false
77
const StaticTypes = Union{Number,<:StaticVector{N} where N,<:NTuple{N,Any} where N}
88

99
"Apply the `hash` function recursively to the given arguments."
10+
hashrec() = zero(UInt)
1011
hashrec(x) = hash(x)
1112
hashrec(x, args...) = hash(x, hashrec(args...))
1213

1314
# Workaround for #88, manually compute the hash of an array using all its elements
14-
hashrec() = zero(UInt)
15-
function hashrec(A::AbstractArray, args...)
15+
function hash_array(A)
1616
h = hash(size(A))
1717
for x in A
1818
h = hash(x, h)
1919
end
20-
hash(h, hashrec(args...))
20+
h
2121
end
22+
hashrec(A::AbstractArray) = hash_array(A)
23+
hashrec(A::AbstractArray, args...) = hash(hash_array(A), hashrec(args...))
2224

2325
"What is the euclidean dimension of the given type (if applicable)?"
2426
euclideandimension(::Type{T}) where {T <: Number} = 1

test/runtests.jl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
module FunctionMapsTests
1+
using Test
22

3-
using Test, LinearAlgebra, StaticArrays
3+
using LinearAlgebra, StaticArrays
44
using CompositeTypes, CompositeTypes.Indexing
55

6-
include("using_fmaps.jl")
6+
using FunctionMaps
77

88
include("aqua.jl")
99

@@ -13,8 +13,7 @@ include("test_interface.jl")
1313
include("test_generic.jl")
1414
include("test_basic.jl")
1515
include("test_affine.jl")
16+
include("test_canonical.jl")
1617
include("test_product.jl")
1718
include("test_maps.jl")
1819
include("test_arithmetics.jl")
19-
20-
end

test/test_affine.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
using FunctionMaps:
2+
to_matrix, to_vector,
3+
matrix_pinv
4+
15
function test_affine_maps(T)
26
A = rand(T,2,2)
37
@test FunctionMaps.to_matrix(Vector{T}, A) == A

test/test_arithmetics.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
using FunctionMaps:
2+
interval_map,
3+
bounded_interval_map
4+
15
@testset "map interval" begin
26
@test interval_map(1.0, Inf, 2.0, Inf) == AffineMap(1.0, 1.0)
37
@test interval_map(1.0, Inf, Inf, 2.0) == AffineMap(-1.0, 3.0)

test/test_canonical.jl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using FunctionMaps:
2+
hascanonicalmap,
3+
hasequalmap,
4+
equivalentmap,
5+
hasequivalentmap,
6+
tofunctionmap,
7+
equalmap
8+
9+
struct MyCanonicalType <: FunctionMaps.CanonicalType end
10+
11+
function test_canonical()
12+
m = LinearMap(2.0)
13+
@test canonicalmap(m) == m
14+
@test !hascanonicalmap(m)
15+
@test canonicalmap(MyCanonicalType(), m) == m
16+
@test !hascanonicalmap(MyCanonicalType(), m)
17+
18+
@test tofunctionmap(m) == m
19+
@test tofunctionmap(MapRef(m)) == m
20+
21+
@test canonicalmap(FunctionMaps.Equal(), 5.0) isa Map
22+
@test hasequalmap(5.0)
23+
@test equalmap(5.0) isa ScalarLinearMap{Float64}
24+
@test isequalmap(LinearMap(5), 5.0)
25+
26+
m1 = LinearMap(2.0)
27+
m2tuple = FunctionMaps.TupleProductMap(m1, m1)
28+
@test hasequivalentmap(m2tuple)
29+
@test equivalentmap(m2tuple) isa FunctionMaps.VcatMap
30+
end

test/test_common.jl

Lines changed: 70 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
2+
using FunctionMaps:
3+
hashrec,
4+
convert_numtype,
5+
promote_numtype,
6+
to_numtype,
7+
convert_prectype,
8+
promote_prectype,
9+
to_prectype,
10+
convert_eltype,
11+
euclideandimension,
12+
isrealtype
13+
14+
function test_hashrec()
15+
@test hashrec() == 0
16+
@test hashrec() isa UInt
17+
A = [1,2]
18+
@test hashrec(A) == hash(2, hash(1, hash((2,))))
19+
@test hashrec(1, 2) == hash(1, hash(2))
20+
end
21+
122
function test_dimension()
223
@test euclideandimension(Int) == 1
324
@test euclideandimension(Float64) == 1
@@ -10,6 +31,7 @@ function test_dimension()
1031
end
1132

1233
function test_prectype()
34+
@test prectype(:some_symbol) == Any
1335
@test prectype(1.0) == Float64
1436
@test prectype(big(1.0)) == BigFloat
1537
@test prectype(1) == typeof(float(1))
@@ -44,10 +66,26 @@ function test_prectype()
4466
@test promote_prectype(2) == 2
4567
@test promote_prectype(2, 3.0) isa Tuple{Float64,Float64}
4668
@test promote_prectype(2, 3.0+im, big(4)) isa Tuple{BigFloat,Complex{BigFloat},BigFloat}
69+
70+
@test to_prectype(Float64, Int) === Float64
71+
@test to_prectype(Float32, Float64) === Float32
72+
@test to_prectype(Int, Int) === Int
73+
74+
@test to_prectype(Float64, Complex{Int}) === Complex{Float64}
75+
@test to_prectype(Float32, Complex{Float64}) === Complex{Float32}
76+
@test to_prectype(Float64, Vector{Int}) === Vector{Float64}
77+
@test to_prectype(Float32, Vector{Float64}) === Vector{Float32}
78+
@test to_prectype(Float64, SVector{3,Int}) === SVector{3,Float64}
79+
@test to_prectype(Float32, MVector{3,Float64}) === MVector{3,Float32}
80+
@test to_prectype(Float64, SMatrix{2,2,Int}) === SMatrix{2,2,Float64}
81+
@test to_prectype(Float32, MMatrix{2,2,Float64}) === MMatrix{2,2,Float32}
82+
@test_throws ArgumentError to_prectype(Float64, String)
83+
@test_throws ArgumentError to_prectype(Float64, Dict{Int,Float64})
4784
end
4885

4986

5087
function test_numtype()
88+
@test numtype(:some_symbol) == Any
5189
@test numtype(1.0) == Float64
5290
@test numtype(big(1.0)) == BigFloat
5391
@test numtype(1) == Int
@@ -64,6 +102,7 @@ function test_numtype()
64102
@test numtype((1.0, 2.0, 3.0, 4.0)) == Float64
65103
@test numtype(1.0, big(2.0), 3.0+im) == Complex{BigFloat}
66104
@test numtype(typeof((1.0, big(2.0), 3.0+im))) == Complex{BigFloat}
105+
@test numtype(Tuple{Int,Int,Int,Int,Float64}) == Float64
67106
@test @inferred(numtype(1, 2.0)) == Float64
68107
@test @inferred(numtype(typeof((1, 2.0, 3, 40+im)))) == Complex{Float64}
69108

@@ -82,9 +121,34 @@ function test_numtype()
82121
@test promote_numtype(2) == 2
83122
@test promote_numtype(2, 3.0) isa Tuple{Float64,Float64}
84123
@test promote_numtype(2, 3.0+im, big(4)) isa Tuple{Complex{BigFloat},Complex{BigFloat},Complex{BigFloat}}
124+
125+
@test to_numtype(Float64, Int) === Float64
126+
@test to_numtype(Float32, Float64) === Float32
127+
@test to_numtype(Int, Int) === Int
128+
129+
@test to_numtype(Float64, Vector{Int}) === Vector{Float64}
130+
@test to_numtype(Float32, Vector{Float64}) === Vector{Float32}
131+
@test to_numtype(Float64, SVector{3,Int}) === SVector{3,Float64}
132+
@test to_numtype(Float32, MVector{3,Float64}) === MVector{3,Float32}
133+
@test to_numtype(Float64, SMatrix{2,2,Int}) === SMatrix{2,2,Float64}
134+
@test to_numtype(Float32, MMatrix{2,2,Float64}) === MMatrix{2,2,Float32}
135+
@test_throws ArgumentError to_numtype(Float64, String)
136+
@test_throws ArgumentError to_numtype(Float64, Dict{Int,Float64})
85137
end
86138

87-
using FunctionMaps: isrealtype
139+
function test_eltype()
140+
@test convert_eltype(Float64, Diagonal([1,2])) == Diagonal([1,2])
141+
@test eltype(convert_eltype(Float64, Diagonal([1,2]))) == Float64
142+
@test convert_eltype(Float64, 1:4) == 1:4
143+
@test eltype(convert_eltype(Float64, 1:4)) == Float64
144+
@test convert_eltype(Float64, Set([1,4])) == Set([1,4])
145+
@test eltype(convert_eltype(Float64, Set([1,4]))) == Float64
146+
@test convert_eltype(Float64, 5) == 5
147+
@test eltype(convert_eltype(Float64, 5)) == Float64
148+
149+
@test FunctionMaps.promotable_eltypes(Int,Float64)
150+
@test FunctionMaps.promotable_eltypes(Vector{Int},Vector{Float64})
151+
end
88152

89153
function test_realtype()
90154
@test isrealtype(Any) == false
@@ -94,15 +158,10 @@ function test_realtype()
94158
end
95159

96160
@testset "common functionality" begin
97-
@testset "dimension" begin
98-
test_dimension()
99-
end
100-
@testset "prectype" begin
101-
test_prectype()
102-
end
103-
@testset "numtype" begin
104-
test_numtype()
105-
end
106-
161+
test_hashrec()
162+
test_dimension()
163+
test_prectype()
164+
test_numtype()
165+
test_eltype()
107166
test_realtype()
108167
end

test/test_generic.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
using FunctionMaps:
2+
convert_domaintype, convert_codomaintype,
3+
map_hash,
4+
LazyInverse, jacobian!
5+
16
function generic_map_tests(T)
27
for map in maps_to_test(T)
38
@test prectype(map) == T

0 commit comments

Comments
 (0)