Skip to content

Commit f90e612

Browse files
committed
Move tests from arraymath.jl to broadcast.jl and add more tests
1 parent ba94bd1 commit f90e612

File tree

2 files changed

+149
-32
lines changed

2 files changed

+149
-32
lines changed

test/arraymath.jl

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,4 @@
11
@testset "Array math" begin
2-
@testset "AbstractArray-of-StaticArray with scalar math" begin
3-
v = SVector{2,Float64}[SVector{2,Float64}(1,1)]
4-
@test @inferred(v .* 1.0)::typeof(v) == v
5-
@test @inferred(1 .- v)::typeof(v) == v .- v
6-
v2 = SVector{2,Int}[SVector{2,Int}(1,1)]
7-
@test @inferred(v2 .* 1.0)::typeof(v) == v
8-
end
9-
10-
@testset "Array-scalar math" begin
11-
m = @SMatrix [1 2; 3 4]
12-
13-
@test @inferred(m .+ 1) === @SMatrix [2 3; 4 5]
14-
@test @inferred(1 .+ m) === @SMatrix [2 3; 4 5]
15-
@test @inferred(m .* 2) === @SMatrix [2 4; 6 8]
16-
@test @inferred(2 .* m) === @SMatrix [2 4; 6 8]
17-
@test @inferred(m .- 1) === @SMatrix [0 1; 2 3]
18-
@test @inferred(1 .- m) === @SMatrix [0 -1; -2 -3]
19-
@test @inferred(m ./ 2) === @SMatrix [0.5 1.0; 1.5 2.0]
20-
@test @inferred(12 ./ m) === @SMatrix [12.0 6.0; 4.0 3.0]
21-
22-
end
23-
24-
@testset "Elementwise array math" begin
25-
m1 = @SMatrix [1 2; 3 4]
26-
m2 = @SMatrix [4 3; 2 1]
27-
28-
@test @inferred(m1 .+ m2) === @SMatrix [5 5; 5 5]
29-
@test @inferred(m1 .* m2) === @SMatrix [4 6; 6 4]
30-
@test @inferred(m1 .- m2) === @SMatrix [-3 -1; 1 3]
31-
@test @inferred(m1 ./ m2) === @SMatrix [0.25 2/3; 1.5 4.0]
32-
end
33-
342
@testset "zeros() and ones()" begin
353
@test @inferred(zeros(SVector{3,Float64})) === @SVector [0.0, 0.0, 0.0]
364
@test @inferred(zeros(SVector{3,Int})) === @SVector [0, 0, 0]

test/broadcast.jl

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
# issue 191
2+
@testset "Broadcast sizes" begin
3+
@test broadcast_sizes(1, 1, 1) === (Size(), Size(), Size())
4+
for t in (SVector{2}, MVector{2}, SMatrix{2, 2}, MMatrix{2, 2})
5+
@test broadcast_sizes(ones(t), ones(t), ones(t)) === (Size(t), Size(t), Size(t))
6+
@test broadcast_sizes(ones(t), 1, ones(t)) === (Size(t), Size(), Size(t))
7+
@test broadcast_sizes(1, ones(t), ones(t)) === (Size(), Size(t), Size(t))
8+
@test broadcast_sizes(ones(t), ones(t), 1) === (Size(t), Size(t), Size())
9+
@test broadcast_sizes(1, ones(t), 1) === (Size(), Size(t), Size())
10+
@test broadcast_sizes(ones(t), 1, 1) === (Size(t), Size(), Size())
11+
@test broadcast_sizes(1, 1, ones(t)) === (Size(), Size(), Size(t))
12+
end
13+
@test broadcast((a,b,c)->0, SVector(1,1), 0, 0) == SVector(0, 0)
14+
end
15+
16+
@testset "Broadcast" begin
17+
@testset "2x2 StaticMatrix with StaticVector" begin
18+
m = @SMatrix [1 2; 3 4]
19+
v = SVector(1, 4)
20+
@test @inferred(broadcast(+, m, v)) === @SMatrix [2 3; 7 8]
21+
@test @inferred(m .+ v) === @SMatrix [2 3; 7 8]
22+
@test @inferred(v .+ m) === @SMatrix [2 3; 7 8]
23+
@test @inferred(m .* v) === @SMatrix [1 2; 12 16]
24+
@test @inferred(v .* m) === @SMatrix [1 2; 12 16]
25+
@test @inferred(m ./ v) === @SMatrix [1 2; 3/4 1]
26+
@test @inferred(v ./ m) === @SMatrix [1 1/2; 4/3 1]
27+
@test @inferred(m .- v) === @SMatrix [0 1; -1 0]
28+
@test @inferred(v .- m) === @SMatrix [0 -1; 1 0]
29+
@test @inferred(m .^ v) === @SMatrix [1 2; 81 256]
30+
@test @inferred(v .^ m) === @SMatrix [1 1; 64 256]
31+
end
32+
33+
@testset "2x2 StaticMatrix with 1x2 StaticMatrix" begin
34+
m1 = @SMatrix [1 2; 3 4]
35+
m2 = @SMatrix [1 4]
36+
# @test @inferred(broadcast(+, m1, m2)) === @SMatrix [2 6; 4 8] #197
37+
# @test @inferred(m1 .+ m2) === @SMatrix [2 6; 4 8] #197
38+
@test @inferred(m2 .+ m1) === @SMatrix [2 6; 4 8]
39+
# @test @inferred(m1 .* m2) === @SMatrix [1 8; 3 16] #197
40+
@test @inferred(m2 .* m1) === @SMatrix [1 8; 3 16]
41+
# @test @inferred(m1 ./ m2) === @SMatrix [1 1/2; 3 1] #197
42+
@test @inferred(m2 ./ m1) === @SMatrix [1 2; 1/3 1]
43+
# @test @inferred(m1 .- m2) === @SMatrix [0 -2; 2 0] #197
44+
@test @inferred(m2 .- m1) === @SMatrix [0 2; -2 0]
45+
# @test @inferred(m1 .^ m2) === @SMatrix [1 16; 1 256] #197
46+
end
47+
48+
@testset "1x2 StaticMatrix with StaticVector" begin
49+
m = @SMatrix [1 2]
50+
v = SVector(1, 4)
51+
@test @inferred(broadcast(+, m, v)) === @SMatrix [2 3; 5 6]
52+
@test @inferred(m .+ v) === @SMatrix [2 3; 5 6]
53+
# @test @inferred(v .+ m) === @SMatrix [2 3; 5 6] #197
54+
@test @inferred(m .* v) === @SMatrix [1 2; 4 8]
55+
# @test @inferred(v .* m) === @SMatrix [1 2; 4 8] #197
56+
@test @inferred(m ./ v) === @SMatrix [1 2; 1/4 1/2]
57+
# @test @inferred(v ./ m) === @SMatrix [1 1/2; 4 2] #197
58+
@test @inferred(m .- v) === @SMatrix [0 1; -3 -2]
59+
# @test @inferred(v .- m) === @SMatrix [0 -1; 3 2] #197
60+
@test @inferred(m .^ v) === @SMatrix [1 2; 1 16]
61+
# @test @inferred(v .^ m) === @SMatrix [1 1; 4 16] #197
62+
end
63+
64+
@testset "StaticVector with StaticVector" begin
65+
v1 = SVector(1, 2)
66+
v2 = SVector(1, 4)
67+
@test @inferred(broadcast(+, v1, v2)) === SVector(2, 6)
68+
@test @inferred(v1 .+ v2) === SVector(2, 6)
69+
@test @inferred(v2 .+ v1) === SVector(2, 6)
70+
@test @inferred(v1 .* v2) === SVector(1, 8)
71+
@test @inferred(v2 .* v1) === SVector(1, 8)
72+
@test @inferred(v1 ./ v2) === SVector(1, 1/2)
73+
@test @inferred(v2 ./ v1) === SVector(1, 2/1)
74+
@test @inferred(v1 .- v2) === SVector(0, -2)
75+
@test @inferred(v2 .- v1) === SVector(0, 2)
76+
@test @inferred(v1 .^ v2) === SVector(1, 16)
77+
@test @inferred(v2 .^ v1) === SVector(1, 16)
78+
# test case issue #199
79+
@test @inferred(SVector(1) .+ SVector()) === SVector()
80+
# @test @inferred(SVector() .+ SVector(1)) === SVector()
81+
# test case issue #200
82+
# @test @inferred(v1 .+ v2') === @SMatrix [2 5; 3 5] # issue
83+
end
84+
85+
@testset "StaticVector with Scalar" begin
86+
v = SVector(1, 2)
87+
@test @inferred(broadcast(+, v, 2)) === SVector(3, 4)
88+
@test @inferred(v .+ 2) === SVector(3, 4)
89+
@test @inferred(2 .+ v) === SVector(3, 4)
90+
@test @inferred(v .* 2) === SVector(2, 4)
91+
@test @inferred(2 .* v) === SVector(2, 4)
92+
@test @inferred(v ./ 2) === SVector(1/2, 1)
93+
@test @inferred(2 ./ v) === SVector(2, 1/1)
94+
@test @inferred(v .- 2) === SVector(-1, 0)
95+
@test @inferred(2 .- v) === SVector(1, 0)
96+
@test @inferred(v .^ 2) === SVector(1, 4)
97+
@test @inferred(2 .^ v) === SVector(2, 4)
98+
end
99+
100+
@testset "Mutating broadcast!" begin
101+
# No setindex! error
102+
A = eye(SMatrix{2, 2}); @test_throws ErrorException broadcast!(+, A, A, SVector(1, 4))
103+
A = eye(MMatrix{2, 2}); @test @inferred(broadcast!(+, A, A, SVector(1, 4))) == @MMatrix [2 1; 4 5]
104+
A = eye(MMatrix{2, 2}); @test @inferred(broadcast!(+, A, A, @SMatrix([1 4]))) == @MMatrix [2 4; 1 5]
105+
A = @MMatrix([1 0]); @test_throws DimensionMismatch broadcast!(+, A, A, SVector(1, 4))
106+
A = @MMatrix([1 0]); @test @inferred(broadcast!(+, A, A, @SMatrix([1 4]))) == @MMatrix [2 4]
107+
A = @MMatrix([1 0]); @test @inferred(broadcast!(+, A, A, 2)) == @MMatrix [3 2]
108+
end
109+
110+
@testset "f.(args...) syntax" begin
111+
x = SVector(1, 3.2, 4.7)
112+
y = SVector(3.5, pi, 1e-4)
113+
α = 0.2342
114+
@test sin.(x) === broadcast(sin, x)
115+
@test sin.(α) === broadcast(sin, α)
116+
@test sin.(3.2) === broadcast(sin, 3.2) == sin(3.2)
117+
@test factorial.(3) === broadcast(factorial, 3)
118+
@test atan2.(x, y) === broadcast(atan2, x, y)
119+
# test case issue #200
120+
# @test atan2.(x, y') === broadcast(atan2, x, y')
121+
@test atan2.(x, α) === broadcast(atan2, x, α)
122+
@test atan2.(α, y') === broadcast(atan2, α, y')
123+
end
124+
125+
@testset "eltype after broadcast" begin
126+
# test cases issue #198
127+
# let a = SVector{4, Number}(2, 2.0, 4//2, 2+0im)
128+
# @test eltype(a + 2) == Number
129+
# @test eltype(a - 2) == Number
130+
# @test eltype(a * 2) == Number
131+
# @test eltype(a / 2) == Number
132+
# end
133+
# let a = SVector{3, Real}(2, 2.0, 4//2)
134+
# @test eltype(a + 2) == Real
135+
# @test eltype(a - 2) == Real
136+
# @test eltype(a * 2) == Real
137+
# @test eltype(a / 2) == Real
138+
# end
139+
# let a = SVector{3, Real}(2, 2.0, 4//2)
140+
# @test eltype(a + 2.0) == Float64
141+
# @test eltype(a - 2.0) == Float64
142+
# @test eltype(a * 2.0) == Float64
143+
# @test eltype(a / 2.0) == Float64
144+
# end
145+
let a = broadcast(Float32, SVector(3, 4, 5))
146+
@test eltype(a) == Float32
147+
end
148+
end
149+
end

0 commit comments

Comments
 (0)