Skip to content

Commit b951535

Browse files
committed
a few more tests for OffsetIntegers & FixedArrays
1 parent 23b2346 commit b951535

File tree

3 files changed

+47
-24
lines changed

3 files changed

+47
-24
lines changed

src/fixed_arrays.jl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ macro fixed_vector(name_parent)
5757
function $(name){S}(x::T) where {S,T <: Tuple}
5858
return $(name){S,StaticArrays.promote_tuple_eltype(T)}(x)
5959
end
60-
$(name){S,T}(x::StaticVector) where {S,T} = $(name){S,T}(Tuple(x))
60+
$(name){S,T}(x::StaticVector{S}) where {S,T} = $(name){S,T}(Tuple(x))
61+
$(name){S,T}(x::StaticVector) where {S,T} = $(name){S,T}(ntuple(i -> x[i], S))
6162

6263
@generated function (::Type{$(name){S,T}})(x::$(name)) where {S,T}
6364
idx = [:(x[$i]) for i in 1:S]
@@ -139,7 +140,7 @@ const VecTypes{N,T} = Union{StaticVector{N,T},NTuple{N,T}}
139140
const Vecf{N} = Vec{N,Float32}
140141
const PointT{T} = Point{N,T} where N
141142
const Pointf{N} = Point{N,Float32}
142-
143+
143144
Base.isnan(p::Union{AbstractPoint,Vec}) = any(isnan, p)
144145
Base.isinf(p::Union{AbstractPoint,Vec}) = any(isinf, p)
145146
Base.isfinite(p::Union{AbstractPoint,Vec}) = all(isfinite, p)
@@ -177,9 +178,9 @@ export Vecf, Pointf
177178
Vec{N, T}(args...)
178179
Vec{N, T}(args::Union{AbstractVector, Tuple, NTuple, StaticVector})
179180
180-
Constructs a Vec of length `N` from the given arguments.
181+
Constructs a Vec of length `N` from the given arguments.
181182
182-
Note that Point and Vec don't follow strict mathematical definitions. Instead
183+
Note that Point and Vec don't follow strict mathematical definitions. Instead
183184
we allow them to be used interchangeably.
184185
185186
## Aliases
@@ -197,9 +198,9 @@ Vec
197198
Point{N, T}(args...)
198199
Point{N, T}(args::Union{AbstractVector, Tuple, NTuple, StaticVector})
199200
200-
Constructs a Point of length `N` from the given arguments.
201+
Constructs a Point of length `N` from the given arguments.
201202
202-
Note that Point and Vec don't follow strict mathematical definitions. Instead
203+
Note that Point and Vec don't follow strict mathematical definitions. Instead
203204
we allow them to be used interchangeably.
204205
205206
## Aliases

test/fixed_arrays.jl

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
using Test
22

3-
@testset "conversion" begin
3+
@testset "Construction and Conversion" begin
4+
for VT in [Point, Vec]
5+
for T in [Int32, Float32, Float64, UInt16, BigFloat]
6+
p = VT{3, T}(1,2,3)
7+
@test p[1] == T(1)
8+
@test p[2] == T(2)
9+
@test p[3] == T(3)
10+
end
11+
12+
for VT2 in [Point, Vec]
13+
@test VT{2, Float32}(VT2{3, Float32}(1,2,3)) == VT{2, Float32}(1,2)
14+
@test VT{2, Float32}(VT2{3, Float64}(1,2,3)) == VT{2, Float32}(1,2)
15+
end
16+
@test VT{2, Float32}(Float32[1,2,3]) == VT{2, Float32}(1,2)
17+
@test VT{2, Float32}([1,2,3]) == VT{2, Float32}(1,2)
18+
end
19+
420
@test convert(Point, (2, 3)) === Point(2, 3)
521
@test convert(Point, (2.0, 3)) === Point(2.0, 3.0)
622
end
@@ -17,6 +33,10 @@ end
1733
T = ifelse(T1 == Point, Point, ifelse(T2 == Point, Point, Vec))
1834
@test T(2, 2, 4) == T1(1,2,3) .+ T2(1, 0, 1)
1935
@test T(foo.((1,2,3), (1, 0, 1))) == foo.(T1(1,2,3), T2(1, 0, 1))
36+
@test T1(1,2,3) .* T2(1,2,3) == T(1, 4, 9)
37+
# TODO: repair this:
38+
# @test foo.(T1(1,2,3), [T2(1,1,1), T2(2,2,2)]) == [T(1,2,3), T(2,4,6)]
39+
# @test foo.([T2(1,1,1), T2(2,2,2)], T1(1,2,3)) == [T(0, -3, -8), T(3, 0, -5)]
2040
end
2141
end
2242

@@ -51,7 +71,7 @@ end
5171
for T2 in (Vec, Point, tuple)
5272
T1 == tuple && T2 == tuple && continue
5373
T = ifelse(T1 == Point, Point, ifelse(T2 == Point, Point, Vec))
54-
@test T(foo2.((1,2,3), (1, 0, 1), (3, 2, 1), (2,2,0))) ==
74+
@test T(foo2.((1,2,3), (1, 0, 1), (3, 2, 1), (2,2,0))) ==
5575
foo2.(T1(1,2,3), T2(1, 0, 1), T2(3, 2, 1), T2(2,2,0))
5676
end
5777
end
@@ -80,7 +100,7 @@ end
80100
@testset "Mat" begin
81101
M3 = Mat3(1,2,3, 4,5,6, 7,8,9)
82102
@test M3 isa Mat{3,3,Int,9}
83-
103+
84104
@testset "indexing" begin
85105
for i in 1:9
86106
@test getindex(M3, i) == i

test/runtests.jl

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -312,21 +312,23 @@ end
312312
x = OffsetInteger{0}(1)
313313
@test typeof(x) == OffsetInteger{0,Int}
314314

315-
x1 = OffsetInteger{0}(2)
316-
x2 = 1
317-
@test Base.to_index(x1) == 2
318-
@test -(x1) == OffsetInteger{0,Int}(-2)
319-
@test abs(x1) == OffsetInteger{0,Int}(2)
320-
@test +(x, x1) == OffsetInteger{0,Int}(3)
321-
@test *(x, x1) == OffsetInteger{0,Int}(2)
322-
@test -(x, x1) == OffsetInteger{0,Int}(-1)
323-
#test for /
324-
@test div(x, x1) == OffsetInteger{0,Int}(0)
325-
@test !==(x, x1)
326-
@test !>=(x, x1)
327-
@test <=(x, x1)
328-
@test !>(x, x1)
329-
@test <(x, x1)
315+
for x1 in [OffsetInteger{0}(2), 2, 0x02]
316+
@test Base.to_index(x1) == 2
317+
@test -(x1) == OffsetInteger{0,Int}(-2)
318+
@test abs(x1) == OffsetInteger{0,Int}(2)
319+
for x in [OffsetInteger{0}(1), 1, 0x01]
320+
@test +(x, x1) == OffsetInteger{0,Int}(3)
321+
@test *(x, x1) == OffsetInteger{0,Int}(2)
322+
@test -(x, x1) == OffsetInteger{0,Int}(-1)
323+
#test for /
324+
@test div(x, x1) == OffsetInteger{0,Int}(0)
325+
@test !==(x, x1)
326+
@test !>=(x, x1)
327+
@test <=(x, x1)
328+
@test !>(x, x1)
329+
@test <(x, x1)
330+
end
331+
end
330332
end
331333

332334
@testset "Tests from GeometryTypes" begin

0 commit comments

Comments
 (0)