Skip to content

Commit

Permalink
Fix tests (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman authored Jan 16, 2025
1 parent 10ea80c commit 60ec997
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 25 deletions.
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "BlockSparseArrays"
uuid = "2c9a651f-6452-4ace-a6ac-809f4280fbb4"
authors = ["ITensor developers <[email protected]> and contributors"]
version = "0.2.9"
version = "0.2.10"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand All @@ -11,6 +11,7 @@ DerivableInterfaces = "6c5e35bf-e59e-4898-b73c-732dcc4ba65f"
DiagonalArrays = "74fd4be6-21e2-4f6f-823a-4360d37c7a77"
Dictionaries = "85a47980-9c8c-11e8-2b9f-f7ca1fa99fb4"
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
GPUArraysCore = "46192b85-c4d5-4398-a991-12ede77f4527"
GradedUnitRanges = "e2de450a-8a67-46c7-b59c-01d5a3d041c5"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
Expand All @@ -24,7 +25,6 @@ LabelledNumbers = "f856a3a6-4152-4ec4-b2a7-02c1a55d7993"
TensorAlgebra = "68bd88dc-f39d-4e12-b2ca-f046b68fcc6a"

[extensions]
BlockSparseArraysAdaptExt = "Adapt"
BlockSparseArraysTensorAlgebraExt = ["LabelledNumbers", "TensorAlgebra"]

[compat]
Expand Down
1 change: 1 addition & 0 deletions src/BlockSparseArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ include("abstractblocksparsearray/broadcast.jl")
include("abstractblocksparsearray/map.jl")
include("abstractblocksparsearray/linearalgebra.jl")
include("abstractblocksparsearray/cat.jl")
include("abstractblocksparsearray/adapt.jl")

# functions specifically for BlockSparseArray
include("blocksparsearray/defaults.jl")
Expand Down
6 changes: 6 additions & 0 deletions src/abstractblocksparsearray/abstractblocksparsearray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ function Base.setindex!(a::AbstractBlockSparseArray{<:Any,0}, value)
return a
end

# Catch zero-dimensional case to avoid scalar indexing.
function Base.setindex!(a::AbstractBlockSparseArray{<:Any,0}, value, ::Block{0})
blocks(a)[] = value
return a
end

function Base.setindex!(
a::AbstractBlockSparseArray{<:Any,N}, value, I::Vararg{Block{1},N}
) where {N}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
module BlockSparseArraysAdaptExt
using Adapt: Adapt, adapt
using BlockSparseArrays: AbstractBlockSparseArray, map_stored_blocks
Adapt.adapt_structure(to, x::AbstractBlockSparseArray) = map_stored_blocks(adapt(to), x)
end
3 changes: 2 additions & 1 deletion src/abstractblocksparsearray/map.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using ArrayLayouts: LayoutArray
using BlockArrays: blockisequal
using DerivableInterfaces: @interface, AbstractArrayInterface, interface
using GPUArraysCore: @allowscalar
using LinearAlgebra: Adjoint, Transpose
using SparseArraysBase: SparseArraysBase, SparseArrayStyle

Expand Down Expand Up @@ -55,7 +56,7 @@ function map_zero_dim! end
@interface ::AbstractArrayInterface function map_zero_dim!(
f, a_dest::AbstractArray, a_srcs::AbstractArray...
)
a_dest[] = f.(map(a_src -> a_src[], a_srcs)...)
@allowscalar a_dest[] = f.(map(a_src -> a_src[], a_srcs)...)
return a_dest
end

Expand Down
4 changes: 1 addition & 3 deletions src/blocksparsearrayinterface/blocksparsearrayinterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ end
@interface ::AbstractBlockSparseArrayInterface function Base.getindex(
a::AbstractArray{<:Any,0}
)
# TODO: Use `Block()[]` once https://github.com/JuliaArrays/BlockArrays.jl/issues/430
# is fixed.
return a[BlockIndex()]
return a[Block()[]]
end

# a[1:2, 1:2]
Expand Down
File renamed without changes.
10 changes: 5 additions & 5 deletions test/basics/test_basics.jl → test/test_basics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,15 @@ arrayts = (Array, JLArray)
@test iszero(@allowscalar(a[CartesianIndex()]))
@test a[Block()] == dev(fill(0))
@test iszero(@allowscalar(a[Block()][]))
@test @allowscalar(a[Block()[]]) == 0
@test iszero(@allowscalar(a[Block()[]]))
@test Array(a) isa Array{elt,0}
@test Array(a) == fill(0)
for b in (
(b = copy(a); @allowscalar b[] = 2; b),
(b = copy(a); @allowscalar b[CartesianIndex()] = 2; b),
(b = copy(a); @allowscalar b[CartesianIndex()] = 2; b),
(b = copy(a); @allowscalar(b[] = 2); b),
(b = copy(a); @allowscalar(b[CartesianIndex()] = 2); b),
(b = copy(a); @allowscalar(b[Block()[]] = 2); b),
# Regression test for https://github.com/ITensor/BlockSparseArrays.jl/issues/27.
(b = copy(a); @allowscalar b[Block()] = dev(fill(2)); b),
(b = copy(a); b[Block()] = dev(fill(2)); b),
)
@test size(b) == ()
@test isone(length(b))
Expand Down
File renamed without changes.
28 changes: 17 additions & 11 deletions test/basics/test_svd.jl → test/test_svd.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
using Test
using BlockSparseArrays
using BlockSparseArrays: BlockSparseArray, svd, BlockDiagonal, eachblockstoredindex
using BlockArrays
using Random
using BlockArrays: Block, BlockedMatrix, BlockedVector, blocks, mortar
using BlockSparseArrays: BlockSparseArray, BlockDiagonal, eachblockstoredindex, svd
using DiagonalArrays: diagonal
using LinearAlgebra: LinearAlgebra
using Random: Random
using Test: @inferred, @testset, @test

function test_svd(a, usv)
function test_svd(a, usv; broken=false)
U, S, V = usv

@test U * diagonal(S) * V' a
@test U * diagonal(S) * V' a broken = broken
@test U' * U LinearAlgebra.I
@test V' * V LinearAlgebra.I
end
Expand Down Expand Up @@ -41,9 +39,17 @@ end
@testset "($m, $n) BlockDiagonal{$T}" for ((m, n), T) in
Iterators.product(blockszs, eltypes)
a = BlockDiagonal([rand(T, i, j) for (i, j) in zip(m, n)])
usv = svd(a)
# TODO: `BlockDiagonal * Adjoint` errors
test_svd(a, usv)
if VERSION v"1.11"
usv = svd(a)
# TODO: `BlockDiagonal * Adjoint` errors
# TODO: This is broken because of https://github.com/JuliaLang/julia/issues/57034,
# fix and reenable.
test_svd(a, usv; broken=true)
else
# `svd(a)` depends on `diagind(::AbstractMatrix, ::IndexStyle)`
# being defined, but it was only introduced in Julia v1.11.
@test svd(a) broken = true
end
end

# blocksparse
Expand Down
File renamed without changes.

0 comments on commit 60ec997

Please sign in to comment.