-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathFieldVector.jl
More file actions
153 lines (119 loc) · 4.91 KB
/
FieldVector.jl
File metadata and controls
153 lines (119 loc) · 4.91 KB
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
@testset "FieldVector" begin
@testset "Immutable Point3D" begin
eval(quote
struct Point3D <: FieldVector{3, Float64}
x::Float64
y::Float64
z::Float64
end
# No need to define similar_type for non-parametric FieldVector (#792)
end)
p = Point3D(1.0, 2.0, 3.0)
@test_throws Exception p[2] = 4.0
@test size(p) === (3,)
@test length(p) === 3
@test eltype(p) === Float64
@testinf Tuple(p) === (1.0, 2.0, 3.0)
@test (p + p) === Point3D(2.0, 4.0, 6.0)
@test (p[1], p[2], p[3]) === (p.x, p.y, p.z)
m = @SMatrix [2.0 0.0 0.0;
0.0 2.0 0.0;
0.0 0.0 2.0]
@test @inferred(m*p) === Point3D(2.0, 4.0, 6.0)
@test @inferred(SA[2.0 0.0 0.0;
0.0 2.0 0.0]*p) === SVector((2.0, 4.0))
@test @inferred(similar_type(Point3D)) == Point3D
@test @inferred(similar_type(Point3D, Float64)) == Point3D
@test @inferred(similar_type(Point3D, Float32)) == SVector{3,Float32}
@test @inferred(similar_type(Point3D, Size(4))) == SVector{4,Float64}
@test @inferred(similar_type(Point3D, Float32, Size(4))) == SVector{4,Float32}
# Issue 146
@test [[Point3D(1.0,2.0,3.0)]; [Point3D(4.0,5.0,6.0)]]::Vector{Point3D} == [Point3D(1.0,2.0,3.0), Point3D(4.0,5.0,6.0)]
# Issue 342
@test_throws DimensionMismatch("No precise constructor for Point3D found. Length of input was 4.") Point3D(1,2,3,4)
eval(quote
struct Point2DT{T} <: FieldVector{2, T}
x::T
y::T
end
end)
# eltype promotion
@test Point2DT(1., 2) === Point2DT(1.0, 2.0) && Tuple(Point2DT(1.0, 2.0)) === (1.0, 2.0)
@test Point2DT{Int}(1., 2) === Point2DT(1, 2) && Tuple(Point2DT(1, 2)) === (1, 2)
end
@testset "Mutable Point2D" begin
eval(quote
mutable struct Point2D{T} <: FieldVector{2, T}
x::T
y::T
end
end)
p = Point2D(0.0, 0.0)
p[1] = 1.0
p[2] = 2.0
@test size(p) === (2,)
@test length(p) === 2
@test eltype(p) === Float64
@testinf Tuple(p) === (1.0, 2.0)
@test (p[1], p[2]) === (p.x, p.y)
@test (p[1], p[2]) === (1.0, 2.0)
m = @SMatrix [2.0 0.0;
0.0 2.0]
@test @inferred(m*p)::Point2D == Point2D(2.0, 4.0)
@test @inferred(similar_type(Point2D{Float64})) == Point2D{Float64}
@test @inferred(similar_type(Point2D{Float64}, Float32)) == Point2D{Float32}
@test @inferred(similar_type(Point2D{Float64}, Size(4))) == MVector{4,Float64}
@test @inferred(similar_type(Point2D{Float64}, Float32, Size(4))) == MVector{4,Float32}
# eltype promotion
@test Point2D(1f0, 2) isa Point2D{Float32}
@test Point2D{Int}(1f0, 2) isa Point2D{Int}
p = Point2D(0, 0.0)
@test p[1] === p[2] === 0.0
end
@testset "FieldVector with Tuple fields" begin
# verify that having a field which is itself a Tuple
# doesn't break anything
eval(quote
struct TupleField <: FieldVector{1, NTuple{2, Int}}
x::NTuple{2, Int}
end
end)
x = TupleField((1,2))
@test length(x) == 1
@test length(x[1]) == 2
@test x.x == (1, 2)
end
@testset "FieldVector with parametric eltype and without similar_type" begin
eval(quote
struct FVT{T} <: FieldVector{2, T}
x::T
y::T
end
# No similar_type defined - test fallback codepath
end)
@test @inferred(similar_type(FVT{Float64}, Float32)) == FVT{Float32}
@test @inferred(similar_type(FVT{Float64}, Size(2))) == FVT{Float64}
@test @inferred(similar_type(FVT{Float64}, Size(3))) == SVector{3,Float64}
@test @inferred(similar_type(FVT{Float64}, Float32, Size(3))) == SVector{3,Float32}
end
@testset "similar_type for some ill FieldVector" begin
# extra parameters
struct IllFV{T,N} <: FieldVector{3,T}
x::T
y::T
z::T
end
@test @inferred(similar_type(IllFV{Float64}, Float64)) == IllFV{Float64}
@test @inferred(similar_type(IllFV{Float64,Int}, Float64)) == IllFV{Float64,Int}
@test @inferred(similar_type(IllFV{Float64}, Float32)) == SVector{3,Float32}
@test @inferred(similar_type(IllFV{Float64,Int}, Float32)) == SVector{3,Float32}
# invalid `eltype`
struct IllFV2{T} <: FieldVector{3,T}
x::Int
y::Float64
z::Int8
end
@test @inferred(similar_type(IllFV2{Float64}, Float64)) == IllFV2{Float64}
@test @inferred(similar_type(IllFV2{Float64}, Float32)) == SVector{3,Float32}
end
end