Skip to content

Commit d2b421d

Browse files
kshyattfredrikekre
authored andcommitted
A few missing tests for sparse broadcast styles and converters (#31802)
1 parent 9a3ef6b commit d2b421d

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

test/higherorderfns.jl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ end
101101
fa, fA = Array(a), Array(A)
102102
@test broadcast(sin, a) == sparse(broadcast(sin, fa))
103103
@test broadcast(sin, A) == sparse(broadcast(sin, fA))
104+
# also test the typed broadcast
105+
@test broadcast(convert, Float32, A) == sparse(broadcast(convert, Float32, fA))
104106
end
105107

106108
@testset "broadcast! implementation specialized for a single (input) sparse vector/matrix" begin
@@ -639,7 +641,7 @@ end
639641
@test_broken ((_, _, _, _, x) -> x).(Int, Int, Int, Int, spzeros(3)) == spzeros(3)
640642
end
641643

642-
using SparseArrays.HigherOrderFns: SparseVecStyle
644+
using SparseArrays.HigherOrderFns: SparseVecStyle, SparseMatStyle
643645

644646
@testset "Issue #30120: method ambiguity" begin
645647
# HigherOrderFns._copy(f) was ambiguous. It may be impossible to
@@ -681,4 +683,15 @@ end
681683
@test all(isnan, y)
682684
end
683685

686+
@testset "Vec/Mat Style" begin
687+
@test SparseVecStyle(Val(0)) == SparseVecStyle()
688+
@test SparseVecStyle(Val(1)) == SparseVecStyle()
689+
@test SparseVecStyle(Val(2)) == SparseMatStyle()
690+
@test SparseVecStyle(Val(3)) == Broadcast.DefaultArrayStyle{3}()
691+
@test SparseMatStyle(Val(0)) == SparseMatStyle()
692+
@test SparseMatStyle(Val(1)) == SparseMatStyle()
693+
@test SparseMatStyle(Val(2)) == SparseMatStyle()
694+
@test SparseMatStyle(Val(3)) == Broadcast.DefaultArrayStyle{3}()
695+
end
696+
684697
end # module

test/sparse.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ end
3939
@test SparseArrays.indtype(sparse(Int8[1,1],Int8[1,1],[1,1])) == Int8
4040
end
4141

42+
@testset "conversion to AbstractMatrix/SparseMatrix of same eltype" begin
43+
a = sprand(5, 5, 0.2)
44+
@test AbstractMatrix{eltype(a)}(a) == a
45+
@test SparseMatrixCSC{eltype(a)}(a) == a
46+
@test SparseMatrixCSC{eltype(a), Int}(a) == a
47+
end
48+
4249
@testset "sparse matrix construction" begin
4350
@test (A = fill(1.0+im,5,5); isequal(Array(sparse(A)), A))
4451
@test_throws ArgumentError sparse([1,2,3], [1,2], [1,2,3], 3, 3)
@@ -2350,6 +2357,15 @@ end
23502357
@test SparseMatrixCSC(A') isa SparseMatrixCSC
23512358
@test transpose(A) == SparseMatrixCSC(transpose(A))
23522359
@test SparseMatrixCSC(transpose(A)) isa SparseMatrixCSC
2360+
@test SparseMatrixCSC{eltype(A)}(transpose(A)) == transpose(A)
2361+
@test SparseMatrixCSC{eltype(A), Int}(transpose(A)) == transpose(A)
2362+
@test SparseMatrixCSC{Float16}(transpose(A)) == transpose(SparseMatrixCSC{Float16}(A))
2363+
@test SparseMatrixCSC{Float16, Int}(transpose(A)) == transpose(SparseMatrixCSC{Float16}(A))
2364+
B = sprand(ComplexF64, 10, 10, 0.75)
2365+
@test SparseMatrixCSC{eltype(B)}(adjoint(B)) == adjoint(B)
2366+
@test SparseMatrixCSC{eltype(B), Int}(adjoint(B)) == adjoint(B)
2367+
@test SparseMatrixCSC{ComplexF16}(adjoint(B)) == adjoint(SparseMatrixCSC{ComplexF16}(B))
2368+
@test SparseMatrixCSC{ComplexF16, Int8}(adjoint(B)) == adjoint(SparseMatrixCSC{ComplexF16, Int8}(B))
23532369
end
23542370

23552371
# PR 28242

0 commit comments

Comments
 (0)