Skip to content

Commit

Permalink
more testing
Browse files Browse the repository at this point in the history
  • Loading branch information
daanhb committed Jan 31, 2025
1 parent bbfc8b2 commit bb6486c
Show file tree
Hide file tree
Showing 14 changed files with 156 additions and 75 deletions.
3 changes: 2 additions & 1 deletion src/FunctionMaps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export canonicalmap
# from generic/composite.jl
export composedmap
# from generic/product.jl
export ProductMap, productmap
export ProductMap, productmap,
factors, nfactors, factor

# from concrete/basic.jl
export IdentityMap,
Expand Down
1 change: 0 additions & 1 deletion src/concrete/affine/special.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

##############################
# Numbers and arrays as a map
##############################
Expand Down
3 changes: 2 additions & 1 deletion src/generic/product.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

"""
A product map is diagonal and acts on each of the components of x separately:
`y = f(x)` becomes `y_i = f_i(x_i)`.
Expand Down Expand Up @@ -85,6 +84,8 @@ canonicalmap(m::ProductMap) = any(map(hascanonicalmap, factors(m))) ?
ProductMap(map(canonicalmap, factors(m))) : m
canonicalmap(::Equal, m::ProductMap) = any(map(hasequalmap, factors(m))) ?
ProductMap(map(equalmap, factors(m))) : m
canonicalmap(::Equivalent, m::ProductMap{NTuple{N,T}}) where {N,T} =
equivalentmap(convert(Map{SVector{N,T}}, m))
canonicalmap(::Equivalent, m::ProductMap) = any(map(hasequivalentmap, factors(m))) ?
ProductMap(map(equivalentmap, factors(m))) : m

Expand Down
8 changes: 5 additions & 3 deletions src/util/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@ isrealtype(::Type{Any}) = false
const StaticTypes = Union{Number,<:StaticVector{N} where N,<:NTuple{N,Any} where N}

"Apply the `hash` function recursively to the given arguments."
hashrec() = zero(UInt)
hashrec(x) = hash(x)
hashrec(x, args...) = hash(x, hashrec(args...))

# Workaround for #88, manually compute the hash of an array using all its elements
hashrec() = zero(UInt)
function hashrec(A::AbstractArray, args...)
function hash_array(A)
h = hash(size(A))
for x in A
h = hash(x, h)
end
hash(h, hashrec(args...))
h
end
hashrec(A::AbstractArray) = hash_array(A)
hashrec(A::AbstractArray, args...) = hash(hash_array(A), hashrec(args...))

"What is the euclidean dimension of the given type (if applicable)?"
euclideandimension(::Type{T}) where {T <: Number} = 1
Expand Down
9 changes: 4 additions & 5 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module FunctionMapsTests
using Test

using Test, LinearAlgebra, StaticArrays
using LinearAlgebra, StaticArrays
using CompositeTypes, CompositeTypes.Indexing

include("using_fmaps.jl")
using FunctionMaps

include("aqua.jl")

Expand All @@ -13,8 +13,7 @@ include("test_interface.jl")
include("test_generic.jl")
include("test_basic.jl")
include("test_affine.jl")
include("test_canonical.jl")
include("test_product.jl")
include("test_maps.jl")
include("test_arithmetics.jl")

end
4 changes: 4 additions & 0 deletions test/test_affine.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
using FunctionMaps:
to_matrix, to_vector,
matrix_pinv

function test_affine_maps(T)
A = rand(T,2,2)
@test FunctionMaps.to_matrix(Vector{T}, A) == A
Expand Down
4 changes: 4 additions & 0 deletions test/test_arithmetics.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
using FunctionMaps:
interval_map,
bounded_interval_map

@testset "map interval" begin
@test interval_map(1.0, Inf, 2.0, Inf) == AffineMap(1.0, 1.0)
@test interval_map(1.0, Inf, Inf, 2.0) == AffineMap(-1.0, 3.0)
Expand Down
30 changes: 30 additions & 0 deletions test/test_canonical.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using FunctionMaps:
hascanonicalmap,
hasequalmap,
equivalentmap,
hasequivalentmap,
tofunctionmap,
equalmap

struct MyCanonicalType <: FunctionMaps.CanonicalType end

function test_canonical()
m = LinearMap(2.0)
@test canonicalmap(m) == m
@test !hascanonicalmap(m)
@test canonicalmap(MyCanonicalType(), m) == m
@test !hascanonicalmap(MyCanonicalType(), m)

@test tofunctionmap(m) == m
@test tofunctionmap(MapRef(m)) == m

@test canonicalmap(FunctionMaps.Equal(), 5.0) isa Map
@test hasequalmap(5.0)
@test equalmap(5.0) isa ScalarLinearMap{Float64}
@test isequalmap(LinearMap(5), 5.0)

m1 = LinearMap(2.0)
m2tuple = FunctionMaps.TupleProductMap(m1, m1)
@test hasequivalentmap(m2tuple)
@test equivalentmap(m2tuple) isa FunctionMaps.VcatMap
end
81 changes: 70 additions & 11 deletions test/test_common.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@

using FunctionMaps:
hashrec,
convert_numtype,
promote_numtype,
to_numtype,
convert_prectype,
promote_prectype,
to_prectype,
convert_eltype,
euclideandimension,
isrealtype

function test_hashrec()
@test hashrec() == 0
@test hashrec() isa UInt
A = [1,2]
@test hashrec(A) == hash(2, hash(1, hash((2,))))
@test hashrec(1, 2) == hash(1, hash(2))
end

function test_dimension()
@test euclideandimension(Int) == 1
@test euclideandimension(Float64) == 1
Expand All @@ -10,6 +31,7 @@ function test_dimension()
end

function test_prectype()
@test prectype(:some_symbol) == Any
@test prectype(1.0) == Float64
@test prectype(big(1.0)) == BigFloat
@test prectype(1) == typeof(float(1))
Expand Down Expand Up @@ -44,10 +66,26 @@ function test_prectype()
@test promote_prectype(2) == 2
@test promote_prectype(2, 3.0) isa Tuple{Float64,Float64}
@test promote_prectype(2, 3.0+im, big(4)) isa Tuple{BigFloat,Complex{BigFloat},BigFloat}

@test to_prectype(Float64, Int) === Float64
@test to_prectype(Float32, Float64) === Float32
@test to_prectype(Int, Int) === Int

@test to_prectype(Float64, Complex{Int}) === Complex{Float64}
@test to_prectype(Float32, Complex{Float64}) === Complex{Float32}
@test to_prectype(Float64, Vector{Int}) === Vector{Float64}
@test to_prectype(Float32, Vector{Float64}) === Vector{Float32}
@test to_prectype(Float64, SVector{3,Int}) === SVector{3,Float64}
@test to_prectype(Float32, MVector{3,Float64}) === MVector{3,Float32}
@test to_prectype(Float64, SMatrix{2,2,Int}) === SMatrix{2,2,Float64}
@test to_prectype(Float32, MMatrix{2,2,Float64}) === MMatrix{2,2,Float32}
@test_throws ArgumentError to_prectype(Float64, String)
@test_throws ArgumentError to_prectype(Float64, Dict{Int,Float64})
end


function test_numtype()
@test numtype(:some_symbol) == Any
@test numtype(1.0) == Float64
@test numtype(big(1.0)) == BigFloat
@test numtype(1) == Int
Expand All @@ -64,6 +102,7 @@ function test_numtype()
@test numtype((1.0, 2.0, 3.0, 4.0)) == Float64
@test numtype(1.0, big(2.0), 3.0+im) == Complex{BigFloat}
@test numtype(typeof((1.0, big(2.0), 3.0+im))) == Complex{BigFloat}
@test numtype(Tuple{Int,Int,Int,Int,Float64}) == Float64
@test @inferred(numtype(1, 2.0)) == Float64
@test @inferred(numtype(typeof((1, 2.0, 3, 40+im)))) == Complex{Float64}

Expand All @@ -82,9 +121,34 @@ function test_numtype()
@test promote_numtype(2) == 2
@test promote_numtype(2, 3.0) isa Tuple{Float64,Float64}
@test promote_numtype(2, 3.0+im, big(4)) isa Tuple{Complex{BigFloat},Complex{BigFloat},Complex{BigFloat}}

@test to_numtype(Float64, Int) === Float64
@test to_numtype(Float32, Float64) === Float32
@test to_numtype(Int, Int) === Int

@test to_numtype(Float64, Vector{Int}) === Vector{Float64}
@test to_numtype(Float32, Vector{Float64}) === Vector{Float32}
@test to_numtype(Float64, SVector{3,Int}) === SVector{3,Float64}
@test to_numtype(Float32, MVector{3,Float64}) === MVector{3,Float32}
@test to_numtype(Float64, SMatrix{2,2,Int}) === SMatrix{2,2,Float64}
@test to_numtype(Float32, MMatrix{2,2,Float64}) === MMatrix{2,2,Float32}
@test_throws ArgumentError to_numtype(Float64, String)
@test_throws ArgumentError to_numtype(Float64, Dict{Int,Float64})
end

using FunctionMaps: isrealtype
function test_eltype()
@test convert_eltype(Float64, Diagonal([1,2])) == Diagonal([1,2])
@test eltype(convert_eltype(Float64, Diagonal([1,2]))) == Float64
@test convert_eltype(Float64, 1:4) == 1:4
@test eltype(convert_eltype(Float64, 1:4)) == Float64
@test convert_eltype(Float64, Set([1,4])) == Set([1,4])
@test eltype(convert_eltype(Float64, Set([1,4]))) == Float64
@test convert_eltype(Float64, 5) == 5
@test eltype(convert_eltype(Float64, 5)) == Float64

@test FunctionMaps.promotable_eltypes(Int,Float64)
@test FunctionMaps.promotable_eltypes(Vector{Int},Vector{Float64})
end

function test_realtype()
@test isrealtype(Any) == false
Expand All @@ -94,15 +158,10 @@ function test_realtype()
end

@testset "common functionality" begin
@testset "dimension" begin
test_dimension()
end
@testset "prectype" begin
test_prectype()
end
@testset "numtype" begin
test_numtype()
end

test_hashrec()
test_dimension()
test_prectype()
test_numtype()
test_eltype()
test_realtype()
end
5 changes: 5 additions & 0 deletions test/test_generic.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
using FunctionMaps:
convert_domaintype, convert_codomaintype,
map_hash,
LazyInverse, jacobian!

function generic_map_tests(T)
for map in maps_to_test(T)
@test prectype(map) == T
Expand Down
3 changes: 3 additions & 0 deletions test/test_interface.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using FunctionMaps:
MapStyle, IsMap, NotMap,
functionmap, checkmap

struct MySimpleMap end
FunctionMaps.MapStyle(::Type{MySimpleMap}) = IsMap()
Expand Down
24 changes: 24 additions & 0 deletions test/test_maps.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
using FunctionMaps: ScalarAffineMap,
VectorAffineMap,
StaticAffineMap,
GenericAffineMap,
ScalarLinearMap,
VectorLinearMap,
StaticLinearMap,
GenericLinearMap,
ScalarTranslation,
VectorTranslation,
StaticTranslation,
GenericTranslation,
ProductMap,
TupleProductMap, VcatMap, VectorProductMap,
WrappedMap,
interval_map, multiply_map,
SumMap, sum_map,
composedmap, ComposedMap,
composite_jacobian, sum_jacobian,
CartToPolarMap, PolarToCartMap,
UnitCircleMap, AngleMap, UnitDiskMap,
VectorToComplex


maps_to_test(T) = [
IdentityMap{T}(),
Expand Down Expand Up @@ -58,6 +81,7 @@ issquarematrix(A::AbstractArray) = size(A,1)==size(A,2)
function test_maps()
@testset "generic functionality" begin
test_generic_functionality()
test_canonical()
end

@testset "generic map tests" begin
Expand Down
3 changes: 3 additions & 0 deletions test/test_product.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,7 @@ function test_product_map(T)
@test pm isa VcatMap{T,6,4,(3,3),(2,2)}
@test jacobian(pm, SA[one(T),0,0,0]) isa SMatrix{6,4,T}
@test diffvolume(pm, SA[one(T),0,0,0]) == 24
@test factors(pm) == components(pm)
@test nfactors(pm) == ncomponents(pm)
@test factor(pm, 1) == component(pm, 1)
end
53 changes: 0 additions & 53 deletions test/using_fmaps.jl

This file was deleted.

0 comments on commit bb6486c

Please sign in to comment.