Skip to content

Commit e48e964

Browse files
committed
[BlockSparseArrays] Tests for slicing
1 parent 957f2af commit e48e964

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

NDTensors/src/lib/BlockSparseArrays/src/blocksparsearrayinterface/blocksparsearrayinterface.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,7 @@ end
199199
function blocksparse_blocks(a::SubArray)
200200
return SparseSubArrayBlocks(a)
201201
end
202+
203+
using BlockArrays: BlocksView
204+
# TODO: Is this correct in general?
205+
SparseArrayInterface.nstored(a::BlocksView) = 1

NDTensors/src/lib/BlockSparseArrays/test/basics.jl

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,57 @@ include("TestBlockSparseArraysUtils.jl")
7878
b = map(x -> 2x, a)
7979
@test Array(b) 2 * Array(a)
8080
@test eltype(b) == elt
81+
@test size(b) == size(a)
82+
@test blocksize(b) == (2, 2)
8183
@test block_nstored(b) == 2
8284
@test nstored(b) == 2 * 4 + 3 * 3
85+
86+
b = a[[Block(2), Block(1)], [Block(2), Block(1)]]
87+
@test b[Block(1, 1)] == a[Block(2, 2)]
88+
@test b[Block(1, 2)] == a[Block(2, 1)]
89+
@test b[Block(2, 1)] == a[Block(1, 2)]
90+
@test b[Block(2, 2)] == a[Block(1, 1)]
91+
@test size(b) == size(a)
92+
@test blocksize(b) == (2, 2)
93+
@test nstored(b) == nstored(a)
94+
@test block_nstored(b) == 2
95+
96+
b = a[Block(1):Block(2), Block(1):Block(2)]
97+
@test b == a
98+
@test size(b) == size(a)
99+
@test blocksize(b) == (2, 2)
100+
@test nstored(b) == nstored(a)
101+
@test block_nstored(b) == 2
102+
103+
b = a[Block(1):Block(1), Block(1):Block(2)]
104+
@test b == Array(a)[1:2, 1:end]
105+
@test b[Block(1, 1)] == a[Block(1, 1)]
106+
@test b[Block(1, 2)] == a[Block(1, 2)]
107+
@test size(b) == (2, 7)
108+
@test blocksize(b) == (1, 2)
109+
@test nstored(b) == nstored(a[Block(1, 2)])
110+
@test block_nstored(b) == 1
111+
112+
b = a[2:4, 2:4]
113+
@test b == Array(a)[2:4, 2:4]
114+
@test size(b) == (3, 3)
115+
@test blocksize(b) == (2, 2)
116+
@test nstored(b) == 1 * 1 + 2 * 2
117+
@test block_nstored(b) == 2
118+
119+
b = a[Block(2, 1)[1:2, 2:3]]
120+
@test b == Array(a)[3:4, 2:3]
121+
@test size(b) == (2, 2)
122+
@test blocksize(b) == (1, 1)
123+
@test nstored(b) == 2 * 2
124+
@test block_nstored(b) == 1
125+
126+
# Broken, need to fix.
127+
@test_broken a[Block(1), Block(1):Block(2)]
128+
# This is outputting only zero blocks.
129+
b = a[Block(2):Block(2), Block(1):Block(2)]
130+
@test_broken block_nstored(b) == 1
131+
@test_broken b == Array(a)[3:5, 1:end]
83132
end
84133
@testset "LinearAlgebra" begin
85134
a1 = BlockSparseArray{elt}([2, 3], [2, 3])

0 commit comments

Comments
 (0)