forked from SciML/RecursiveArrayTools.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils_test.jl
More file actions
194 lines (160 loc) · 6.31 KB
/
utils_test.jl
File metadata and controls
194 lines (160 loc) · 6.31 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
using RecursiveArrayTools, StaticArrays
using Test
t = collect(range(0, stop = 10, length = 200))
randomized = VectorOfArray([0.01randn(2) for i in 1:10])
data = convert(Array, randomized)
@test typeof(data) <: Matrix{Float64}
## Test means
A = [[1 2; 3 4], [1 3; 4 6], [5 6; 7 8]]
@test recursive_mean(A) ≈ [
2.33333333 3.666666666
4.6666666666 6.0
]
A = zeros(5, 5)
@test recursive_unitless_eltype(A) == Float64
@test vecvecapply(x -> abs.(x), -1) == 1
@test vecvecapply(x -> abs.(x), [-1, -2, 3, -4]) == [1, 2, 3, 4]
v = [[-1 2; 3 -4], [5 -6; -7 -8]]
vv = [1, 3, 2, 4, 5, 7, 6, 8]
@test vecvecapply(x -> abs.(x), v) == vv
@test vecvecapply(x -> abs.(x), VectorOfArray(v)) == vv
using Unitful
A = zeros(5, 5) * 1u"kg"
@test recursive_unitless_eltype(A) == Float64
AA = [zeros(5, 5) for i in 1:5]
@test recursive_unitless_eltype(AA) == Array{Float64, 2}
AofA = [copy(A) for i in 1:5]
@test recursive_unitless_eltype(AofA) == Array{Float64, 2}
AofSA = [@SVector [2.0, 3.0] for i in 1:5]
@test recursive_unitless_eltype(AofSA) == SVector{2, Float64}
AofuSA = [@SVector [2.0u"kg", 3.0u"kg"] for i in 1:5]
@test recursive_unitless_eltype(AofuSA) == SVector{2, Float64}
A = [ArrayPartition(ones(1), ones(1))]
function test_recursive_bottom_eltype()
function test_value(val::Any, expected_type::Type)
# It should return the expected type for the given expected type
@test recursive_bottom_eltype(expected_type) == expected_type
# It should return the expected type for the given value
@test recursive_bottom_eltype(val) == expected_type
# It should return the expected type for an array of the given value
Aval = [val for i in 1:5]
@test recursive_bottom_eltype(Aval) == expected_type
# It should return expected type for a nested array of the gicen value
AAval = [Aval for i in 1:5]
@test recursive_bottom_eltype(AAval) == expected_type
# It should return expected type for an array of vectors of chars
AVval = [@SVector [val, val] for i in 1:5]
return @test recursive_bottom_eltype(AVval) == expected_type
end
# testing chars
test_value('c', Char)
# testing strings
# We expect recursive_bottom_eltype to return `Char` for a string, because
# `eltype("Some String") == Char`
test_value("Some String", Char)
# testing integer values
test_value(1, Int)
test_value(1u"kg", eltype(1u"kg"))
# testing float values
test_value(1.0, Float64)
return test_value(1.0u"kg", eltype(1.0u"kg"))
end
test_recursive_bottom_eltype()
x = zeros(10)
recursivefill!(x, 1.0)
@test x == ones(10)
x = [zeros(10), zeros(10)]
recursivefill!(x, 1.0)
@test x[1] == ones(10)
@test x[2] == ones(10)
x = [SVector{10}(zeros(10)), SVector{10}(zeros(10))]
recursivefill!(x, SVector{10}(ones(10)))
@test x[1] == SVector{10}(ones(10))
@test x[2] == SVector{10}(ones(10))
x = [MVector{10}(zeros(10)), MVector{10}(zeros(10))]
recursivefill!(x, 1.0)
@test x[1] == MVector{10}(ones(10))
@test x[2] == MVector{10}(ones(10))
x = [similar(x[1]), similar(x[1])]
recursivefill!(x, true)
@test x[1] == MVector{10}(ones(10))
@test x[2] == MVector{10}(ones(10))
x = similar(x)
recursivefill!(x, true)
@test x[1] == MVector{10}(ones(10))
@test x[2] == MVector{10}(ones(10))
# Test VectorOfArray + recursivefill! + static arrays
@testset "VectorOfArray + recursivefill! + static arrays" begin
Vec3 = SVector{3, Float64}
x = [randn(Vec3, n) for n in 1:4] # vector of vectors of static arrays
x_voa = VectorOfArray(x)
@test eltype(x_voa) === Vec3
@test first(x_voa) isa AbstractVector{Vec3}
y_voa = recursivecopy(x_voa)
recursivefill!(y_voa, true)
@test all(y_voa[:, n] == fill(ones(Vec3), n) for n in 1:4)
y_voa = recursivecopy(x_voa)
recursivefill!(y_voa, ones(Vec3))
@test all(y_voa[:, n] == fill(ones(Vec3), n) for n in 1:4)
end
@testset "VectorOfArray recursivecopy!" begin
u1 = VectorOfArray([fill(2, MVector{2, Float64}), ones(MVector{2, Float64})])
u2 = VectorOfArray([fill(4, MVector{2, Float64}), 2 .* ones(MVector{2, Float64})])
recursivecopy!(u1, u2)
@test u1.u[1] == [4.0, 4.0]
@test u1.u[2] == [2.0, 2.0]
@test u1.u[1] isa MVector
@test u1.u[2] isa MVector
u1 = VectorOfArray([fill(2, SVector{2, Float64}), ones(SVector{2, Float64})])
u2 = VectorOfArray([fill(4, SVector{2, Float64}), 2 .* ones(SVector{2, Float64})])
recursivecopy!(u1, u2)
@test u1.u[1] == [4.0, 4.0]
@test u1.u[2] == [2.0, 2.0]
@test u1.u[1] isa SVector
@test u1.u[2] isa SVector
end
@testset "VectorOfArray similar with nested scalar leaves" begin
a = VectorOfArray([ones(2), VectorOfArray([1.0, 1.0])])
b = similar(a, Float64)
@test b isa typeof(a)
@test b.u[1] isa Vector{Float64}
@test b.u[2] isa typeof(a.u[2])
@test b.u[2].u isa Vector{Float64}
@test length(b.u[2].u) == 2
end
@testset "recursivefill! with nested union partitions" begin
a = VectorOfArray([ones(2), VectorOfArray([1.0, 1.0])])
recursivefill!(a, true)
@test a.u[1] == ones(2)
@test a.u[2].u == ones(2)
end
# Test recursivefill! with immutable StaticArrays (issue #461)
@testset "recursivefill! with immutable StaticArrays (issue #461)" begin
# Test with only immutable SVectors
x = VectorOfArray([SVector{2}(ones(2)), SVector{2}(ones(2))])
recursivefill!(x, 0.0)
@test all(x.u[i] == SVector{2}(zeros(2)) for i in 1:2)
@test all(x.u[i] isa SVector for i in 1:2)
# Test with mixed immutable and mutable StaticArrays
x = VectorOfArray([SVector{2}(ones(2)), MVector{2}(ones(2))])
recursivefill!(x, 0.0)
@test all(x.u[i] == [0.0, 0.0] for i in 1:2)
@test x.u[1] isa SVector
@test x.u[2] isa MVector
# Test fill! on VectorOfArray with immutable SVectors
x = VectorOfArray([SVector{2}(ones(2)), SVector{2}(ones(2))])
fill!(x, 0.0)
@test all(x.u[i] == SVector{2}(zeros(2)) for i in 1:2)
@test all(x.u[i] isa SVector for i in 1:2)
# Test fill! on VectorOfArray with mixed types
x = VectorOfArray([SVector{2}(ones(2)), MVector{2}(ones(2))])
fill!(x, 0.0)
@test all(x.u[i] == [0.0, 0.0] for i in 1:2)
@test x.u[1] isa SVector
@test x.u[2] isa MVector
end
import KernelAbstractions: get_backend
@testset "KernelAbstractions" begin
v = VectorOfArray([randn(2) for i in 1:10])
@test get_backend(v) === get_backend(parent(v)[1])
end