Skip to content

Commit

Permalink
Add tests for zero dimensional BlockSparseArray
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman committed Nov 9, 2024
1 parent 60626ae commit a2cf2f5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,11 @@ function Base.similar(
return blocksparse_similar(a, elt, axes)
end

# Fixes ambiguity error.
function Base.similar(a::BlockSparseArrayLike{<:Any,0}, elt::Type, axes::Tuple{})
return blocksparse_similar(a, elt, axes)
end

# Fixes ambiguity error with `BlockArrays`.
function Base.similar(
a::BlockSparseArrayLike,
Expand Down
31 changes: 31 additions & 0 deletions NDTensors/src/lib/BlockSparseArrays/test/test_basics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,37 @@ using .NDTensorsTestUtils: devices_list, is_supported_eltype

a[3, 3] = NaN
@test isnan(norm(a))

# Empty constructor
for a in (dev(BlockSparseArray{elt}()), dev(BlockSparseArray{elt}(undef)))
@test size(a) == ()
@test isone(length(a))
@test blocksize(a) == ()
@test blocksizes(a) == fill(())
@test iszero(block_nstored(a))
@test iszero(@allowscalar(a[]))
@test iszero(@allowscalar(a[CartesianIndex()]))
@test a[Block()] == dev(fill(0))
@test iszero(@allowscalar(a[Block()][]))
# Broken:
## @test b[Block()[]] == 2
for b in (
(b = copy(a); @allowscalar b[] = 2; b),
(b = copy(a); @allowscalar b[CartesianIndex()] = 2; b),
)
@test size(b) == ()
@test isone(length(b))
@test blocksize(b) == ()
@test blocksizes(b) == fill(())
@test isone(block_nstored(b))
@test @allowscalar(b[]) == 2
@test @allowscalar(b[CartesianIndex()]) == 2
@test b[Block()] == dev(fill(2))
@test @allowscalar(b[Block()][]) == 2
# Broken:
## @test b[Block()[]] == 2
end
end
end
@testset "Tensor algebra" begin
a = dev(BlockSparseArray{elt}(undef, ([2, 3], [3, 4])))
Expand Down

0 comments on commit a2cf2f5

Please sign in to comment.