Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bring coverage of fixed SparseMatrixCSC to 100% #392

Merged
merged 8 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading