Skip to content

Commit 3a199fb

Browse files
committed
Fix printing, start adding tests
1 parent e6fdb68 commit 3a199fb

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

src/blocksparsearrayinterface/map.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,12 @@ end
151151
function map_stored_blocks(f, a::AbstractArray)
152152
block_stored_indices = collect(eachblockstoredindex(a))
153153
if isempty(block_stored_indices)
154+
eltype_a′ = Base.promote_op(f, eltype(a))
154155
blocktype_a′ = Base.promote_op(f, blocktype(a))
155-
return BlockSparseArray{eltype(blocktype_a′),ndims(a),blocktype_a′}(undef, axes(a))
156+
eltype_a′′ = !isconcretetype(eltype_a′) ? Any : eltype_a′
157+
blocktype_a′′ =
158+
!isconcretetype(blocktype_a′) ? AbstractArray{eltype_a′′,ndims(a)} : blocktype_a′
159+
return BlockSparseArray{eltype_a′′,ndims(a),blocktype_a′′}(undef, axes(a))
156160
end
157161
stored_blocks = map(B -> f(@view!(a[B])), block_stored_indices)
158162
blocktype_a′ = eltype(stored_blocks)

test/test_basics.jl

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ end
7070

7171
arrayts = (Array, JLArray)
7272
@testset "BlockSparseArrays (arraytype=$arrayt, eltype=$elt)" for arrayt in arrayts,
73-
elt in (Float32, Float64, Complex{Float32}, Complex{Float64})
73+
elt in (Float32, Float64, ComplexF32)
7474

7575
dev(a) = adapt(arrayt, a)
7676
@testset "Broken" begin
@@ -342,6 +342,42 @@ arrayts = (Array, JLArray)
342342
end
343343
end
344344

345+
@testset "Abstract block type" begin
346+
a = BlockSparseMatrix{elt,AbstractMatrix{elt}}(undef, [2, 3], [2, 3])
347+
@test sprint(show, MIME"text/plain"(), a) isa String
348+
@test iszero(storedlength(a))
349+
@test iszero(blockstoredlength(a))
350+
a[Block(1, 1)] = dev(randn(elt, 2, 2))
351+
a[Block(2, 2)] = dev(randn(elt, 3, 3))
352+
@test !iszero(a[Block(1, 1)])
353+
@test a[Block(1, 1)] isa arrayt{elt,2}
354+
@test !iszero(a[Block(2, 2)])
355+
@test a[Block(2, 2)] isa arrayt{elt,2}
356+
@test iszero(a[Block(2, 1)])
357+
@test a[Block(2, 1)] isa Matrix{elt}
358+
@test iszero(a[Block(1, 2)])
359+
@test a[Block(1, 2)] isa Matrix{elt}
360+
361+
if arrayt === Array
362+
b = copy(a)
363+
@test Array(b) Array(a)
364+
365+
b = a + a
366+
@test Array(b) Array(a) + Array(a)
367+
368+
b = 3a
369+
@test Array(b) 3Array(a)
370+
371+
b = a * a
372+
@test Array(b) Array(a) * Array(a)
373+
else
374+
@test_broken copy(a)
375+
@test_broken a * a
376+
@test_broken a + a
377+
@test_broken 3a
378+
end
379+
end
380+
345381
@testset "Transpose" begin
346382
a = dev(BlockSparseArray{elt}(undef, [2, 2], [3, 3, 1]))
347383
a[Block(1, 1)] = dev(randn(elt, 2, 3))

test/test_factorizations.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,3 +473,25 @@ end
473473
@test sort(diagview(D[Block(1, 1)]); by=abs, rev=true) D1[1:1]
474474
@test sort(diagview(D[Block(2, 2)]); by=abs, rev=true) D2[1:2]
475475
end
476+
477+
@testset "Abstract block type" begin
478+
arrayt = Array
479+
elt = Float32
480+
dev = adapt(arrayt)
481+
482+
a = BlockSparseMatrix{elt,AbstractMatrix{elt}}(undef, ([2, 3], [2, 3]))
483+
a[Block(1, 1)] = dev(randn(elt, 2, 2))
484+
a[Block(2, 2)] = dev(randn(elt, 3, 3))
485+
@test_broken eig_full(a)
486+
@test_broken eigh_full(a)
487+
@test_broken svd_compact(a)
488+
@test_broken svd_full(a)
489+
@test_broken left_orth(a)
490+
@test_broken right_orth(a)
491+
@test_broken left_polar(a)
492+
@test_broken right_polar(a)
493+
@test_broken qr_compact(a)
494+
@test_broken qr_full(a)
495+
@test_broken lq_compact(a)
496+
@test_broken lq_full(a)
497+
end

0 commit comments

Comments
 (0)