Skip to content

Commit e38dc08

Browse files
committed
Fix tests
1 parent bd54ec3 commit e38dc08

File tree

16 files changed

+191
-180
lines changed

16 files changed

+191
-180
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ julia> Pkg.add("BlockSparseArrays")
3535

3636
````julia
3737
using BlockArrays: BlockArrays, BlockedVector, Block, blockedrange
38-
using BlockSparseArrays: BlockSparseArray, block_storedlength
38+
using BlockSparseArrays: BlockSparseArray, blockstoredlength
3939
using Test: @test, @test_broken
4040

4141
function main()
@@ -62,13 +62,13 @@ function main()
6262
]
6363
b = BlockSparseArray(nz_blocks, d_blocks, i_axes)
6464

65-
@test block_storedlength(b) == 2
65+
@test blockstoredlength(b) == 2
6666

6767
# Blocks with discontiguous underlying data
6868
d_blocks = randn.(nz_block_sizes)
6969
b = BlockSparseArray(nz_blocks, d_blocks, i_axes)
7070

71-
@test block_storedlength(b) == 2
71+
@test blockstoredlength(b) == 2
7272

7373
# Access a block
7474
@test b[Block(1, 1)] == d_blocks[1]
@@ -92,7 +92,7 @@ function main()
9292
@test b + b Array(b) + Array(b)
9393
@test b + b isa BlockSparseArray
9494
# TODO: Fix this, broken.
95-
@test_broken block_storedlength(b + b) == 2
95+
@test_broken blockstoredlength(b + b) == 2
9696

9797
scaled_b = 2b
9898
@test scaled_b 2Array(b)

examples/README.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ julia> Pkg.add("BlockSparseArrays")
4040
# ## Examples
4141

4242
using BlockArrays: BlockArrays, BlockedVector, Block, blockedrange
43-
using BlockSparseArrays: BlockSparseArray, block_storedlength
43+
using BlockSparseArrays: BlockSparseArray, blockstoredlength
4444
using Test: @test, @test_broken
4545

4646
function main()
@@ -67,13 +67,13 @@ function main()
6767
]
6868
b = BlockSparseArray(nz_blocks, d_blocks, i_axes)
6969

70-
@test block_storedlength(b) == 2
70+
@test blockstoredlength(b) == 2
7171

7272
## Blocks with discontiguous underlying data
7373
d_blocks = randn.(nz_block_sizes)
7474
b = BlockSparseArray(nz_blocks, d_blocks, i_axes)
7575

76-
@test block_storedlength(b) == 2
76+
@test blockstoredlength(b) == 2
7777

7878
## Access a block
7979
@test b[Block(1, 1)] == d_blocks[1]
@@ -97,7 +97,7 @@ function main()
9797
@test b + b Array(b) + Array(b)
9898
@test b + b isa BlockSparseArray
9999
## TODO: Fix this, broken.
100-
@test_broken block_storedlength(b + b) == 2
100+
@test_broken blockstoredlength(b + b) == 2
101101

102102
scaled_b = 2b
103103
@test scaled_b 2Array(b)

ext/BlockSparseArraysGradedUnitRangesExt/test/runtests.jl

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using Test: @test, @testset
33
using BlockArrays:
44
AbstractBlockArray, Block, BlockedOneTo, blockedrange, blocklengths, blocksize
5-
using BlockSparseArrays: BlockSparseArray, block_stored_length
5+
using BlockSparseArrays: BlockSparseArray, blockstoredlength
66
using GradedUnitRanges:
77
GradedUnitRanges,
88
GradedOneTo,
@@ -13,7 +13,7 @@ using GradedUnitRanges:
1313
gradedrange,
1414
isdual
1515
using LabelledNumbers: label
16-
using SparseArraysBase: stored_length
16+
using SparseArraysBase: storedlength
1717
using SymmetrySectors: U1
1818
using TensorAlgebra: fusedims, splitdims
1919
using LinearAlgebra: adjoint
@@ -40,8 +40,8 @@ const elts = (Float32, Float64, Complex{Float32}, Complex{Float64})
4040
@test size(b) == (4, 4, 4, 4)
4141
@test blocksize(b) == (2, 2, 2, 2)
4242
@test blocklengths.(axes(b)) == ([2, 2], [2, 2], [2, 2], [2, 2])
43-
@test stored_length(b) == 32
44-
@test block_stored_length(b) == 2
43+
@test storedlength(b) == 32
44+
@test blockstoredlength(b) == 2
4545
for i in 1:ndims(a)
4646
@test axes(b, i) isa GradedOneTo
4747
end
@@ -58,8 +58,8 @@ const elts = (Float32, Float64, Complex{Float32}, Complex{Float64})
5858
@test size(b) == (4, 4, 4, 4)
5959
@test blocksize(b) == (2, 2, 2, 2)
6060
@test blocklengths.(axes(b)) == ([2, 2], [2, 2], [2, 2], [2, 2])
61-
@test stored_length(b) == 256
62-
@test block_stored_length(b) == 16
61+
@test storedlength(b) == 256
62+
@test blockstoredlength(b) == 16
6363
for i in 1:ndims(a)
6464
@test axes(b, i) isa BlockedOneTo{Int}
6565
end
@@ -71,8 +71,8 @@ const elts = (Float32, Float64, Complex{Float32}, Complex{Float64})
7171
b = a[2:3, 2:3, 2:3, 2:3]
7272
@test size(b) == (2, 2, 2, 2)
7373
@test blocksize(b) == (2, 2, 2, 2)
74-
@test stored_length(b) == 2
75-
@test block_stored_length(b) == 2
74+
@test storedlength(b) == 2
75+
@test blockstoredlength(b) == 2
7676
for i in 1:ndims(a)
7777
@test axes(b, i) isa GradedOneTo
7878
end
@@ -156,7 +156,7 @@ const elts = (Float32, Float64, Complex{Float32}, Complex{Float64})
156156
a[i] = randn(elt, size(a[i]))
157157
end
158158
b = 2 * a
159-
@test block_stored_length(b) == 2
159+
@test blockstoredlength(b) == 2
160160
@test Array(b) == 2 * Array(a)
161161
for i in 1:2
162162
@test axes(b, i) isa GradedOneTo
@@ -177,7 +177,7 @@ const elts = (Float32, Float64, Complex{Float32}, Complex{Float64})
177177
a[i] = randn(elt, size(a[i]))
178178
end
179179
b = 2 * a
180-
@test block_stored_length(b) == 2
180+
@test blockstoredlength(b) == 2
181181
@test Array(b) == 2 * Array(a)
182182
for i in 1:2
183183
@test axes(b, i) isa GradedUnitRange
@@ -204,7 +204,7 @@ const elts = (Float32, Float64, Complex{Float32}, Complex{Float64})
204204
a[i] = randn(elt, size(a[i]))
205205
end
206206
b = 2 * a
207-
@test block_stored_length(b) == 2
207+
@test blockstoredlength(b) == 2
208208
@test Array(b) == 2 * Array(a)
209209
for i in 1:2
210210
@test axes(b, i) isa GradedUnitRangeDual
@@ -229,7 +229,7 @@ const elts = (Float32, Float64, Complex{Float32}, Complex{Float64})
229229
a[i] = randn(elt, size(a[i]))
230230
end
231231
b = 2 * a
232-
@test block_stored_length(b) == 2
232+
@test blockstoredlength(b) == 2
233233
@test Array(b) == 2 * Array(a)
234234
for i in 1:2
235235
@test axes(b, i) isa GradedUnitRangeDual
@@ -255,7 +255,7 @@ const elts = (Float32, Float64, Complex{Float32}, Complex{Float64})
255255
a[i] = randn(elt, size(a[i]))
256256
end
257257
b = 2 * a
258-
@test block_stored_length(b) == 2
258+
@test blockstoredlength(b) == 2
259259
@test Array(b) == 2 * Array(a)
260260
@test a[:, :] isa BlockSparseArray
261261
for i in 1:2
@@ -280,7 +280,7 @@ const elts = (Float32, Float64, Complex{Float32}, Complex{Float64})
280280
a[i] = randn(elt, size(a[i]))
281281
end
282282
b = 2 * a'
283-
@test block_stored_length(b) == 2
283+
@test blockstoredlength(b) == 2
284284
@test Array(b) == 2 * Array(a)'
285285
for ax in axes(b)
286286
@test ax isa typeof(dual(r))

ext/BlockSparseArraysTensorAlgebraExt/src/BlockSparseArraysTensorAlgebraExt.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module BlockSparseArraysTensorAlgebraExt
22
using BlockArrays: AbstractBlockedUnitRange
3-
using ..BlockSparseArrays: AbstractBlockSparseArray, block_reshape
3+
using ..BlockSparseArrays: AbstractBlockSparseArray, blockreshape
44
using GradedUnitRanges: tensor_product
55
using TensorAlgebra: TensorAlgebra, FusionStyle, BlockReshapeFusion
66

@@ -13,12 +13,12 @@ TensorAlgebra.FusionStyle(::AbstractBlockedUnitRange) = BlockReshapeFusion()
1313
function TensorAlgebra.fusedims(
1414
::BlockReshapeFusion, a::AbstractArray, axes::AbstractUnitRange...
1515
)
16-
return block_reshape(a, axes)
16+
return blockreshape(a, axes)
1717
end
1818

1919
function TensorAlgebra.splitdims(
2020
::BlockReshapeFusion, a::AbstractArray, axes::AbstractUnitRange...
2121
)
22-
return block_reshape(a, axes)
22+
return blockreshape(a, axes)
2323
end
2424
end

src/BlockArraysExtensions/BlockArraysExtensions.jl

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -264,17 +264,17 @@ function blocks_to_cartesianindices(d::Dictionary{<:Block})
264264
return Dictionary(blocks_to_cartesianindices(eachindex(d)), d)
265265
end
266266

267-
function block_reshape(a::AbstractArray, dims::Tuple{Vararg{Vector{Int}}})
268-
return block_reshape(a, blockedrange.(dims))
267+
function blockreshape(a::AbstractArray, dims::Tuple{Vararg{Vector{Int}}})
268+
return blockreshape(a, blockedrange.(dims))
269269
end
270270

271-
function block_reshape(a::AbstractArray, dims::Vararg{Vector{Int}})
272-
return block_reshape(a, dims)
271+
function blockreshape(a::AbstractArray, dims::Vararg{Vector{Int}})
272+
return blockreshape(a, dims)
273273
end
274274

275275
tuple_oneto(n) = ntuple(identity, n)
276276

277-
function block_reshape(a::AbstractArray, axes::Tuple{Vararg{AbstractUnitRange}})
277+
function blockreshape(a::AbstractArray, axes::Tuple{Vararg{AbstractUnitRange}})
278278
reshaped_blocks_a = reshape(blocks(a), blocklength.(axes))
279279
reshaped_a = similar(a, axes)
280280
for I in eachstoredindex(reshaped_blocks_a)
@@ -285,8 +285,8 @@ function block_reshape(a::AbstractArray, axes::Tuple{Vararg{AbstractUnitRange}})
285285
return reshaped_a
286286
end
287287

288-
function block_reshape(a::AbstractArray, axes::Vararg{AbstractUnitRange})
289-
return block_reshape(a, axes)
288+
function blockreshape(a::AbstractArray, axes::Vararg{AbstractUnitRange})
289+
return blockreshape(a, axes)
290290
end
291291

292292
function cartesianindices(axes::Tuple, b::Block)
@@ -473,10 +473,6 @@ function findblocks(axis::AbstractUnitRange, range::AbstractUnitRange)
473473
return findblock(axis, first(range)):findblock(axis, last(range))
474474
end
475475

476-
function block_eachstoredindex(a::AbstractArray)
477-
return Block.(Tuple.(eachstoredindex(blocks(a))))
478-
end
479-
480476
_block(indices) = block(indices)
481477
_block(indices::CartesianIndices) = Block(ntuple(Returns(1), ndims(indices)))
482478

@@ -550,7 +546,7 @@ function SparseArraysBase.storedlength(a::BlockView)
550546
# TODO: Store whether or not the block is stored already as
551547
# a Bool in `BlockView`.
552548
I = CartesianIndex(Int.(a.block))
553-
# TODO: Use `block_eachstoredindex`.
549+
# TODO: Use `eachblockstoredindex`.
554550
if I eachstoredindex(blocks(parent(a)))
555551
return storedlength(blocks(parent(a))[I])
556552
end

src/abstractblocksparsearray/abstractblocksparsearray.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using BlockArrays:
22
BlockArrays, AbstractBlockArray, Block, BlockIndex, BlockedUnitRange, blocks
3-
using SparseArraysBase: sparse_getindex, sparse_setindex!
43

54
# TODO: Delete this. This function was replaced
65
# by `stored_length` but is still used in `NDTensors`.
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
# TODO: Change to `AnyAbstractBlockSparseArray`.
1+
using Derive: @interface, interface
2+
3+
# TODO: Define with `@derive`.
24
function Base.cat(as::AnyAbstractBlockSparseArray...; dims)
3-
# TODO: Use `sparse_cat` instead, currently
4-
# that erroneously allocates too many blocks that are
5-
# zero and shouldn't be stored.
6-
return blocksparse_cat(as...; dims)
5+
return @interface interface(as...) cat(as...; dims)
76
end

src/abstractblocksparsearray/map.jl

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,13 @@ using ArrayLayouts: LayoutArray
22
using BlockArrays: blockisequal
33
using Derive: @interface, interface
44
using LinearAlgebra: Adjoint, Transpose
5-
using SparseArraysBase:
6-
SparseArraysBase,
7-
SparseArrayStyle,
8-
sparse_map!,
9-
sparse_copy!,
10-
sparse_copyto!,
11-
sparse_permutedims!,
12-
sparse_mapreduce,
13-
sparse_iszero,
14-
sparse_isreal
5+
using SparseArraysBase: SparseArraysBase, SparseArrayStyle
156

167
# Returns `Vector{<:CartesianIndices}`
178
function union_stored_blocked_cartesianindices(as::Vararg{AbstractArray})
189
combined_axes = combine_axes(axes.(as)...)
1910
stored_blocked_cartesianindices_as = map(as) do a
20-
return blocked_cartesianindices(axes(a), combined_axes, block_eachstoredindex(a))
11+
return blocked_cartesianindices(axes(a), combined_axes, eachblockstoredindex(a))
2112
end
2213
return (stored_blocked_cartesianindices_as...)
2314
end
@@ -102,11 +93,13 @@ end
10293

10394
# TODO: Move to `blocksparsearrayinterface/map.jl`.
10495
@interface ::AbstractBlockSparseArrayInterface function Base.iszero(a::AbstractArray)
96+
# TODO: Just call `iszero(blocks(a))`?
10597
return @interface interface(blocks(a)) iszero(blocks(a))
10698
end
10799

108100
# TODO: Move to `blocksparsearrayinterface/map.jl`.
109101
@interface ::AbstractBlockSparseArrayInterface function Base.isreal(a::AbstractArray)
102+
# TODO: Just call `isreal(blocks(a))`?
110103
return @interface interface(blocks(a)) isreal(blocks(a))
111104
end
112105

src/abstractblocksparsearray/sparsearrayinterface.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ using BlockArrays: Block
22
using SparseArraysBase: SparseArraysBase, eachstoredindex, storedlength, storedvalues
33

44
# Structure storing the block sparse storage
5+
# TODO: Delete this in favor of `storedvalues(blocks(a))`,
6+
# and rename `storedblocks(a)` and/or `eachstoredblock(a)`.
57
struct BlockSparseStorage{Arr<:AbstractBlockSparseArray}
68
array::Arr
79
end
@@ -29,11 +31,12 @@ function Base.iterate(s::BlockSparseStorage, args...)
2931
return iterate(values(s), args...)
3032
end
3133

32-
## TODO: Delete this, define `getstoredindex`, etc.
33-
## function SparseArraysBase.sparse_storage(a::AbstractBlockSparseArray)
34+
## TODO: Bring back this deifinition but check that it makes sense.
35+
## function SparseArraysBase.storedvaluese(a::AbstractBlockSparseArray)
3436
## return BlockSparseStorage(a)
3537
## end
3638

39+
# TODO: Turn this into an `@interface ::AbstractBlockSparseArrayInterface` function.
3740
function SparseArraysBase.storedlength(a::AnyAbstractBlockSparseArray)
3841
return sum(storedlength, storedvalues(blocks(a)); init=zero(Int))
3942
end

src/abstractblocksparsearray/views.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ function BlockArrays.viewblock(
6868
a::AbstractBlockSparseArray{<:Any,N}, block::Vararg{Block{1},N}
6969
) where {N}
7070
I = CartesianIndex(Int.(block))
71-
# TODO: Use `block_eachstoredindex`.
71+
# TODO: Use `eachblockstoredindex`.
7272
if I eachstoredindex(blocks(a))
7373
return blocks(a)[I]
7474
end
@@ -185,7 +185,7 @@ function BlockArrays.viewblock(
185185
block::Vararg{Block{1},N},
186186
) where {T,N}
187187
I = CartesianIndex(Int.(block))
188-
# TODO: Use `block_eachstoredindex`.
188+
# TODO: Use `eachblockstoredindex`.
189189
if I eachstoredindex(blocks(a))
190190
return blocks(a)[I]
191191
end

0 commit comments

Comments
 (0)