-
Notifications
You must be signed in to change notification settings - Fork 145
/
FieldVector.jl
69 lines (52 loc) · 2.13 KB
/
FieldVector.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
@testset "FieldVector" begin
@testset "Immutable Point3D" begin
eval(quote
immutable Point3D <: FieldVector{Float64}
x::Float64
y::Float64
z::Float64
end
StaticArrays.similar_type(::Type{Point3D}, ::Type{Float64}, s::Size{(3,)}) = Point3D
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
@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(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}
end
@testset "Mutable Point2D" begin
eval(quote
type Point2D{T} <: FieldVector{T}
x::T
y::T
end
StaticArrays.similar_type{P2D<:Point2D,T}(::Type{P2D}, ::Type{T}, s::Size{(2,)}) = Point2D{T}
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
@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))) == SVector{4,Float64}
@test @inferred(similar_type(Point2D{Float64}, Float32, Size(4))) == SVector{4,Float32}
end
end