Skip to content

Commit 73bec0d

Browse files
committed
test that mul!(::LinearCombination) allocates at most once
1 parent d734ee5 commit 73bec0d

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

test/runtests.jl

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,23 @@ using LinearMaps
33
using SparseArrays
44
using LinearAlgebra
55

6+
# adopted from: https://discourse.julialang.org/t/way-to-return-the-number-of-allocations/5167/10
7+
macro numalloc(expr)
8+
return quote
9+
let
10+
local f
11+
function f()
12+
n1 = Base.gc_num()
13+
$(expr)
14+
n2 = Base.gc_num()
15+
diff = Base.GC_Diff(n2, n1)
16+
Base.gc_alloc_count(diff)
17+
end
18+
f()
19+
end
20+
end
21+
end
22+
623
import Base: *
724
import LinearAlgebra: issymmetric, mul!
825

@@ -185,10 +202,14 @@ end
185202
end
186203

187204
CS! = LinearMap(cumsum!, 10; ismutating=true)
188-
v = rand(ComplexF64, 10)
205+
v = rand(10)
189206
u = similar(v)
190207
mul!(u, CS!, v)
191208
@test ((@allocated mul!(u, CS!, v)) == 0)
209+
n = 10
210+
L = sum(fill(CS!, n))
211+
@test mul!(u, L, v) n * cumsum(v)
212+
@test ((@numalloc mul!(u, L, v)) <= 1)
192213

193214
A = 2 * rand(ComplexF64, (10, 10)) .- 1
194215
B = rand(size(A)...)

0 commit comments

Comments
 (0)