diff --git a/src/sparsematrix.jl b/src/sparsematrix.jl index 08d8a21a..2f8bd9ce 100644 --- a/src/sparsematrix.jl +++ b/src/sparsematrix.jl @@ -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) diff --git a/test/fixed.jl b/test/fixed.jl index 1441929e..59c10346 100644 --- a/test/fixed.jl +++ b/test/fixed.jl @@ -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) @@ -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) @@ -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)) @@ -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 @@ -139,4 +169,4 @@ always_false(x...) = false @test all(iszero, nonzeros(b)) end -end \ No newline at end of file +end