Skip to content

Commit 169d081

Browse files
Fix rand with zero-element array (#1122)
* update tests with zero element rand * fix return type for zero element rand * add tests for rand, randn, randexp, rand!, randn!, and randexp!
1 parent b394eee commit 169d081

File tree

5 files changed

+155
-0
lines changed

5 files changed

+155
-0
lines changed

src/arraymath.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ using Random: SamplerType
4646
T = Float64
4747
end
4848
v = [:(rand(rng, $T)) for i = 1:prod(s)]
49+
if SA <: SArray
50+
SA = SArray{Tuple{s...}, T, length(s), prod(s)}
51+
elseif SA <: MArray
52+
SA = MArray{Tuple{s...}, T, length(s), prod(s)}
53+
elseif SA <: SizedArray
54+
SA = SizedArray{Tuple{s...}, T, length(s)}
55+
end
4956
return quote
5057
@_inline_meta
5158
$SA(tuple($(v...)))
@@ -71,6 +78,13 @@ end
7178
T = Float64
7279
end
7380
v = [:(randn(rng, $T)) for i = 1:prod(s)]
81+
if SA <: SArray
82+
SA = SArray{Tuple{s...}, T, length(s), prod(s)}
83+
elseif SA <: MArray
84+
SA = MArray{Tuple{s...}, T, length(s), prod(s)}
85+
elseif SA <: SizedArray
86+
SA = SizedArray{Tuple{s...}, T, length(s)}
87+
end
7488
return quote
7589
@_inline_meta
7690
$SA(tuple($(v...)))
@@ -84,6 +98,13 @@ end
8498
T = Float64
8599
end
86100
v = [:(randexp(rng, $T)) for i = 1:prod(s)]
101+
if SA <: SArray
102+
SA = SArray{Tuple{s...}, T, length(s), prod(s)}
103+
elseif SA <: MArray
104+
SA = MArray{Tuple{s...}, T, length(s), prod(s)}
105+
elseif SA <: SizedArray
106+
SA = SizedArray{Tuple{s...}, T, length(s)}
107+
end
87108
return quote
88109
@_inline_meta
89110
$SA(tuple($(v...)))

test/MArray.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@
106106
@test isa(@MArray(rand(2,2,1)), MArray{Tuple{2,2,1}, Float64})
107107
@test isa(@MArray(randn(2,2,1)), MArray{Tuple{2,2,1}, Float64})
108108
@test isa(@MArray(randexp(2,2,1)), MArray{Tuple{2,2,1}, Float64})
109+
@test isa(@MArray(rand(2,2,0)), MArray{Tuple{2,2,0}, Float64})
110+
@test isa(@MArray(randn(2,2,0)), MArray{Tuple{2,2,0}, Float64})
111+
@test isa(@MArray(randexp(2,2,0)), MArray{Tuple{2,2,0}, Float64})
109112

110113
@test isa(randn!(@MArray zeros(2,2,1)), MArray{Tuple{2,2,1}, Float64})
111114
@test isa(randexp!(@MArray zeros(2,2,1)), MArray{Tuple{2,2,1}, Float64})
@@ -115,6 +118,9 @@
115118
@test isa(@MArray(rand(Float32, 2, 2, 1)), MArray{Tuple{2,2,1}, Float32})
116119
@test isa(@MArray(randn(Float32, 2, 2, 1)), MArray{Tuple{2,2,1}, Float32})
117120
@test isa(@MArray(randexp(Float32, 2, 2, 1)), MArray{Tuple{2,2,1}, Float32})
121+
@test isa(@MArray(rand(Float32, 2, 2, 0)), MArray{Tuple{2,2,0}, Float32})
122+
@test isa(@MArray(randn(Float32, 2, 2, 0)), MArray{Tuple{2,2,0}, Float32})
123+
@test isa(@MArray(randexp(Float32, 2, 2, 0)), MArray{Tuple{2,2,0}, Float32})
118124

119125
m = [1 2; 3 4]
120126
@test MArray{Tuple{2,2}}(m) == @MArray [1 2; 3 4]

test/SArray.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,14 @@
101101
@test ((@SArray fill(3.,2,2,1))::SArray{Tuple{2,2,1}, Float64}).data === (3.0, 3.0, 3.0, 3.0)
102102
@test ((@SArray zeros(2,2,1))::SArray{Tuple{2,2,1}, Float64}).data === (0.0, 0.0, 0.0, 0.0)
103103
@test ((@SArray ones(2,2,1))::SArray{Tuple{2,2,1}, Float64}).data === (1.0, 1.0, 1.0, 1.0)
104+
@test isa(@SArray(rand(2,2,0)), SArray{Tuple{2,2,0}, Float64})
104105
@test isa(@SArray(rand(2,2,1)), SArray{Tuple{2,2,1}, Float64})
105106
@test isa(@SArray(randn(2,2,1)), SArray{Tuple{2,2,1}, Float64})
106107
@test isa(@SArray(randexp(2,2,1)), SArray{Tuple{2,2,1}, Float64})
107108

108109
@test ((@SArray zeros(Float32, 2, 2, 1))::SArray{Tuple{2,2,1},Float32}).data === (0.0f0, 0.0f0, 0.0f0, 0.0f0)
109110
@test ((@SArray ones(Float32, 2, 2, 1))::SArray{Tuple{2,2,1},Float32}).data === (1.0f0, 1.0f0, 1.0f0, 1.0f0)
111+
@test isa(@SArray(rand(Float32, 2, 2, 0)), SArray{Tuple{2,2,0}, Float32})
110112
@test isa(@SArray(rand(Float32, 2, 2, 1)), SArray{Tuple{2,2,1}, Float32})
111113
@test isa(@SArray(randn(Float32, 2, 2, 1)), SArray{Tuple{2,2,1}, Float32})
112114
@test isa(@SArray(randexp(Float32, 2, 2, 1)), SArray{Tuple{2,2,1}, Float32})

test/SMatrix.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,18 @@
7575
@test isa(@SMatrix(rand(2,2)), SMatrix{2, 2, Float64})
7676
@test isa(@SMatrix(randn(2,2)), SMatrix{2, 2, Float64})
7777
@test isa(@SMatrix(randexp(2,2)), SMatrix{2, 2, Float64})
78+
@test isa(@SMatrix(rand(2, 0)), SMatrix{2, 0, Float64})
79+
@test isa(@SMatrix(randn(2, 0)), SMatrix{2, 0, Float64})
80+
@test isa(@SMatrix(randexp(2, 0)), SMatrix{2, 0, Float64})
7881

7982
@test ((@SMatrix zeros(Float32, 2, 2))::SMatrix{2,2,Float32}).data === (0.0f0, 0.0f0, 0.0f0, 0.0f0)
8083
@test ((@SMatrix ones(Float32, 2, 2))::SMatrix{2,2,Float32}).data === (1.0f0, 1.0f0, 1.0f0, 1.0f0)
8184
@test isa(@SMatrix(rand(Float32, 2, 2)), SMatrix{2, 2, Float32})
8285
@test isa(@SMatrix(randn(Float32, 2, 2)), SMatrix{2, 2, Float32})
8386
@test isa(@SMatrix(randexp(Float32, 2, 2)), SMatrix{2, 2, Float32})
87+
@test isa(@SMatrix(rand(Float32, 2, 0)), SMatrix{2, 0, Float32})
88+
@test isa(@SMatrix(randn(Float32, 2, 0)), SMatrix{2, 0, Float32})
89+
@test isa(@SMatrix(randexp(Float32, 2, 0)), SMatrix{2, 0, Float32})
8490

8591
@test isa(SMatrix(@SMatrix zeros(4,4)), SMatrix{4, 4, Float64})
8692

test/arraymath.jl

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,24 @@ import StaticArrays.arithmetic_closure
5757
@test all(check)
5858
m = rand(1:1, SVector{3})
5959
@test rand(m) == 1
60+
61+
for T in (SVector, MVector, SizedVector)
62+
v1 = rand(T{3})
63+
@test v1 isa T{3, Float64}
64+
@test all(0 .< v1 .< 1)
65+
66+
v2 = rand(T{0})
67+
@test v2 isa T{0, Float64}
68+
@test all(0 .< v2 .< 1)
69+
70+
v3 = rand(T{3, Float32})
71+
@test v3 isa T{3, Float32}
72+
@test all(0 .< v3 .< 1)
73+
74+
v4 = rand(T{0, Float32})
75+
@test v4 isa T{0, Float32}
76+
@test all(0 .< v4 .< 1)
77+
end
6078
end
6179

6280
@testset "rand!()" begin
@@ -68,6 +86,108 @@ import StaticArrays.arithmetic_closure
6886
rand!(m, 1:2)
6987
check = ((m .>= 1) .& (m .<= 2))
7088
@test all(check)
89+
90+
for T in (MVector, SizedVector)
91+
v1 = rand(T{3})
92+
rand!(v1)
93+
@test v1 isa T{3, Float64}
94+
@test all(0 .< v1 .< 1)
95+
96+
v2 = rand(T{0})
97+
rand!(v2)
98+
@test v2 isa T{0, Float64}
99+
@test all(0 .< v2 .< 1)
100+
101+
v3 = rand(T{3, Float32})
102+
rand!(v3)
103+
@test v3 isa T{3, Float32}
104+
@test all(0 .< v3 .< 1)
105+
106+
v4 = rand(T{0, Float32})
107+
rand!(v4)
108+
@test v4 isa T{0, Float32}
109+
@test all(0 .< v4 .< 1)
110+
end
111+
end
112+
113+
@testset "randn()" begin
114+
for T in (SVector, MVector, SizedVector)
115+
v1 = randn(T{3})
116+
@test v1 isa T{3, Float64}
117+
118+
v2 = randn(T{0})
119+
@test v2 isa T{0, Float64}
120+
121+
v3 = randn(T{3, Float32})
122+
@test v3 isa T{3, Float32}
123+
124+
v4 = randn(T{0, Float32})
125+
@test v4 isa T{0, Float32}
126+
end
127+
end
128+
129+
@testset "randn!()" begin
130+
for T in (MVector, SizedVector)
131+
v1 = randn(T{3})
132+
randn!(v1)
133+
@test v1 isa T{3, Float64}
134+
135+
v2 = randn(T{0})
136+
randn!(v2)
137+
@test v2 isa T{0, Float64}
138+
139+
v3 = randn(T{3, Float32})
140+
randn!(v3)
141+
@test v3 isa T{3, Float32}
142+
143+
v4 = randn(T{0, Float32})
144+
randn!(v4)
145+
@test v4 isa T{0, Float32}
146+
end
147+
end
148+
149+
@testset "randexp()" begin
150+
for T in (SVector, MVector, SizedVector)
151+
v1 = randexp(T{3})
152+
@test v1 isa T{3, Float64}
153+
@test all(0 .< v1)
154+
155+
v2 = randexp(T{0})
156+
@test v2 isa T{0, Float64}
157+
@test all(0 .< v2)
158+
159+
v3 = randexp(T{3, Float32})
160+
@test v3 isa T{3, Float32}
161+
@test all(0 .< v3)
162+
163+
v4 = randexp(T{0, Float32})
164+
@test v4 isa T{0, Float32}
165+
@test all(0 .< v4)
166+
end
167+
end
168+
169+
@testset "randexp!()" begin
170+
for T in (MVector, SizedVector)
171+
v1 = randexp(T{3})
172+
randexp!(v1)
173+
@test v1 isa T{3, Float64}
174+
@test all(0 .< v1)
175+
176+
v2 = randexp(T{0})
177+
randexp!(v2)
178+
@test v2 isa T{0, Float64}
179+
@test all(0 .< v2)
180+
181+
v3 = randexp(T{3, Float32})
182+
randexp!(v3)
183+
@test v3 isa T{3, Float32}
184+
@test all(0 .< v3)
185+
186+
v4 = randexp(T{0, Float32})
187+
randexp!(v4)
188+
@test v4 isa T{0, Float32}
189+
@test all(0 .< v4)
190+
end
71191
end
72192

73193
@testset "arithmetic_closure" for T0 in [subtypes(Unsigned);

0 commit comments

Comments
 (0)