Skip to content

Commit 833d4a7

Browse files
committed
Add more tests for blocktype and blockstype
1 parent d04693f commit 833d4a7

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/blocksparsearrayinterface/blocksparsearrayinterface.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ function eachstoredblock(a::AbstractArray)
4141
return storedvalues(blocks(a))
4242
end
4343

44+
function blockstype(a::AbstractArray)
45+
return typeof(blocks(a))
46+
end
4447
function blocktype(a::AbstractArray)
4548
if isempty(blocks(a))
4649
# TODO: Unfortunately, this doesn't always give
@@ -63,6 +66,12 @@ function blocktype(a::AbstractArray)
6366
return mapreduce(typeof, promote_type, blocks(a))
6467
end
6568

69+
using BlockArrays: BlockArray
70+
blockstype(::Type{<:BlockArray{<:Any,<:Any,B}}) where {B} = B
71+
blockstype(a::BlockArray) = blockstype(typeof(a))
72+
blocktype(arraytype::Type{<:BlockArray}) = eltype(blockstype(arraytype))
73+
blocktype(a::BlockArray) = eltype(blocks(a))
74+
6675
abstract type AbstractBlockSparseArrayInterface <: AbstractSparseArrayInterface end
6776

6877
# TODO: Also support specifying the `blocktype` along with the `eltype`.

test/test_basics.jl

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,34 @@ arrayts = (Array, JLArray)
132132
end
133133
end
134134
end
135-
@testset "blocktype" begin
136-
@test blocktype(arrayt(randn(elt, 2, 2))) <: SubArray{elt,2,arrayt{elt,2}}
135+
@testset "blockstype, blocktype" begin
136+
a = arrayt(randn(elt, 2, 2))
137+
@test blockstype(a) <: BlockArrays.BlocksView{elt,2}
138+
# TODO: This is difficult to determine just from type information.
139+
@test_broken blockstype(typeof(a)) <: BlockArrays.BlocksView{elt,2}
140+
@test blocktype(a) <: SubArray{elt,2,arrayt{elt,2}}
141+
# TODO: This is difficult to determine just from type information.
142+
@test_broken blocktype(typeof(a)) <: SubArray{elt,2,arrayt{elt,2}}
143+
144+
a = BlockSparseMatrix{elt,arrayt{elt,2}}([1, 1], [1, 1])
145+
@test blockstype(a) <: SparseMatrixDOK{arrayt{elt,2}}
146+
@test blockstype(typeof(a)) <: SparseMatrixDOK{arrayt{elt,2}}
147+
@test blocktype(a) <: arrayt{elt,2}
148+
@test blocktype(typeof(a)) <: arrayt{elt,2}
149+
150+
a = BlockArray(arrayt(randn(elt, (2, 2))), [1, 1], [1, 1])
151+
@test blockstype(a) === Matrix{arrayt{elt,2}}
152+
@test blockstype(typeof(a)) === Matrix{arrayt{elt,2}}
153+
@test blocktype(a) <: arrayt{elt,2}
154+
@test blocktype(typeof(a)) <: arrayt{elt,2}
155+
156+
a = BlockedArray(arrayt(randn(elt, 2, 2)), [1, 1], [1, 1])
157+
@test blockstype(a) <: BlockArrays.BlocksView{elt,2}
158+
# TODO: This is difficult to determine just from type information.
159+
@test_broken blockstype(typeof(a)) <: BlockArrays.BlocksView{elt,2}
160+
@test blocktype(a) <: SubArray{elt,2,arrayt{elt,2}}
161+
# TODO: This is difficult to determine just from type information.
162+
@test_broken blocktype(typeof(a)) <: SubArray{elt,2,arrayt{elt,2}}
137163
end
138164
@testset "Basics" begin
139165
a = dev(BlockSparseArray{elt}([2, 3], [2, 3]))

0 commit comments

Comments
 (0)