Skip to content

Commit

Permalink
bring coverage of fixed SparseMatrixCSC to 100% (#392)
Browse files Browse the repository at this point in the history

Co-authored-by: Daniel Karrasch <daniel.karrasch@posteo.de>
Co-authored-by: Viral B. Shah <ViralBShah@users.noreply.github.com>
  • Loading branch information
3 people authored Jul 18, 2023
1 parent 0eb9c04 commit f8f0f40
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ Get a writable copy of x. See `_unsafe_unfix(x)`
"""
SparseMatrixCSC(x::FixedSparseCSC) = SparseMatrixCSC(size(x, 1), size(x, 2),
copy(parent(getcolptr(x))),
copy(parent(rowval(x))),
copy(parent(rowvals(x))),
copy(nonzeros(x)))

function sparse_check_Ti(m::Integer, n::Integer, Ti::Type)
Expand Down
34 changes: 32 additions & 2 deletions test/fixed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ using SparseArrays: AbstractSparseVector, AbstractSparseMatrixCSC, FixedSparseCS
@test (r[1] = r[1]; true)
end

@testset "SparseMatrixCSC from readonly" begin

# test that SparseMatrixCSC from readonly does copy
A = sprandn(12, 11, 0.3)
B = SparseMatrixCSC(size(A)..., ReadOnly(getcolptr(A)), ReadOnly(rowvals(A)), nonzeros(A))

@test typeof(B) == typeof(A)
@test A == B

@test getcolptr(A) == getcolptr(B)
@test getcolptr(A) !== getcolptr(B)
@test rowvals(A) == rowvals(B)
@test rowvals(A) !== rowvals(B)
@test nonzeros(A) == nonzeros(B)
@test nonzeros(A) === nonzeros(B)
end

struct_eq(A, B, C...) = struct_eq(A, B) && struct_eq(B, C...)
struct_eq(A::AbstractSparseMatrixCSC, B::AbstractSparseMatrixCSC) =
getcolptr(A) == getcolptr(B) && rowvals(A) == rowvals(B)
Expand All @@ -30,6 +47,10 @@ struct_eq(A::AbstractSparseVector, B::AbstractSparseVector) =
A = sprandn(10, 10, 0.3)

F = FixedSparseCSC(copy(A))
Ft = FixedSparseCSC{eltype(A),eltype(rowvals(A))}(A)
@test typeof(Ft) == typeof(F)
@test Ft == F

@test struct_eq(F, A)
nonzeros(F) .= 0
@test struct_eq(F, A)
Expand Down Expand Up @@ -69,7 +90,16 @@ struct_eq(A::AbstractSparseVector, B::AbstractSparseVector) =
@test typeof(B) == typeof(F)
@test struct_eq(B, F)
end
@testset "SparseMatrixCSC conversions" begin
A = sprandn(10, 10, 0.3)
F = fixed(copy(A))
B = SparseMatrixCSC(F)
@test A == B

# fixed(x...)
@test sparse(2I, 3, 3) == sparse(fixed(2I, 3, 3))
@test SparseArrays._unsafe_unfix(A) == A
end
@testset "FixedSparseVector" begin
y = sprandn(10, 0.3)
x = FixedSparseVector(copy(y))
Expand All @@ -88,7 +118,7 @@ end
@test f(x, y, z) == 0
t = similar(x)
@test typeof(t) == typeof(x)
@test struct_eq(t, x)
@test struct_eq(t, x)
end

@testset "Issue #190" begin
Expand Down Expand Up @@ -139,4 +169,4 @@ always_false(x...) = false
@test all(iszero, nonzeros(b))

end
end
end

0 comments on commit f8f0f40

Please sign in to comment.