-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_common.jl
167 lines (149 loc) · 7.09 KB
/
test_common.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
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
@test euclideandimension(ComplexF64) == 1
@test euclideandimension(SVector{2,Float64}) == 2
@test euclideandimension(MVector{2,Float64}) == 2
@test euclideandimension(Tuple{Int,Int}) == 2
@test euclideandimension(Tuple{Int,Float64}) == 2
@test_throws ArgumentError euclideandimension(Vector{Float64})
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))
@test prectype(SVector(1,2)) == typeof(float(1))
@test prectype(SVector(1,big(2))) == typeof(float(big(2)))
@test prectype(1.0+2.0im) == Float64
@test prectype([1.0+2.0im, 3.0]) == Float64
@test prectype(NTuple{2,Int}) == Float64
@test prectype(typeof((1.0,))) == Float64
@test prectype(typeof((1.0, 2.0))) == Float64
@test prectype(typeof((1.0, 2.0, 3.0))) == Float64
@test prectype(typeof((1.0, big(2.0), 3.0+im))) == BigFloat
@test prectype(NTuple{4,Int}) == Float64
@test @inferred(prectype(1, 2.0)) == Float64
@test @inferred(prectype(typeof((1, 2.0, 3, 40+im)))) == Float64
@test convert_prectype(Float64, 2) == 2
@test convert_prectype(Float64, 2) isa Float64
@test convert_prectype(BigFloat, 1.0+im) == 1+im
@test convert_prectype(BigFloat, 1.0+im) isa Complex{BigFloat}
@test convert_prectype(Float64, SA[1,2]) == SA[1.0,2.0]
@test convert_prectype(Float64, SA[1,2]) isa SVector{2,Float64}
@test convert_prectype(Float64, MVector(1,2)) == MVector(1.0,2.0)
@test convert_prectype(Float64, MVector(1,2)) isa MVector
@test convert_prectype(Float64, SA[1 2; 3 4]) == SA[1.0 2.0; 3.0 4.0]
@test convert_prectype(Float64, SA[1 2; 3 4]) isa SMatrix{2,2,Float64}
@test convert_prectype(Float64, MMatrix{2,2}(1, 2, 3, 4)) == SA[1.0 3.0; 2.0 4.0]
@test convert_prectype(Float64, MMatrix{2,2}(1, 2, 3, 4)) isa MMatrix{2,2,Float64}
@test convert_prectype(BigFloat, SA[1,2+im]) isa SVector{2,Complex{BigFloat}}
@test_throws ArgumentError convert_prectype(BigFloat, "a")
@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
@test numtype([1,2,3], [4,5,6]) == Int
@test numtype(SVector(1,2)) == Int
@test numtype(SVector(1,big(2))) == BigInt
@test numtype(Array{Float64,2}) == Float64
@test numtype(1.0+2.0im) == Complex{Float64}
@test numtype([1.0+2.0im, 3.0]) == Complex{Float64}
@test numtype(NTuple{2,Complex{Int}}) == Complex{Int}
@test numtype(typeof((1.0,))) == Float64
@test numtype(typeof((1.0, 2.0))) == Float64
@test numtype((1.0, 2.0, 3.0)) == Float64
@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}
@test convert_numtype(Float64, 2) == 2
@test convert_numtype(Float64, 2) isa Float64
@test convert_numtype(Float64, SA[1,2]) == SA[1.0,2.0]
@test convert_numtype(Float64, SA[1,2]) isa SVector{2,Float64}
@test convert_numtype(Float64, MVector(1,2)) == MVector(1.0,2.0)
@test convert_numtype(Float64, MVector(1,2)) isa MVector
@test convert_numtype(Float64, SA[1 2; 3 4]) == SA[1.0 2.0; 3.0 4.0]
@test convert_numtype(Float64, SA[1 2; 3 4]) isa SMatrix{2,2,Float64}
@test convert_numtype(Float64, MMatrix{2,2}(1, 2, 3, 4)) == SA[1.0 3.0; 2.0 4.0]
@test convert_numtype(Float64, MMatrix{2,2}(1, 2, 3, 4)) isa MMatrix{2,2,Float64}
@test_throws ArgumentError convert_numtype(BigFloat, "a")
@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
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
@test isrealtype(Int) == true
@test isrealtype(ComplexF64) == false
@test isrealtype(Matrix{Float64}) == true
end
@testset "common functionality" begin
test_hashrec()
test_dimension()
test_prectype()
test_numtype()
test_eltype()
test_realtype()
end