Skip to content

Commit

Permalink
Thoroughly test similar methods for SparseMatrixCSCs.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sacha0 committed Aug 5, 2016
1 parent 03b0b1d commit 31e6622
Showing 1 changed file with 64 additions and 8 deletions.
72 changes: 64 additions & 8 deletions test/sparsedir/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1166,14 +1166,70 @@ A = sprand(Bool, 5,5,0.2)
@test_throws DimensionMismatch reshape(A,(20,2))
@test_throws ArgumentError squeeze(A,(1,1))

# test similar with type conversion
A = speye(5)
@test size(similar(A,Complex128,Int)) == (5,5)
@test typeof(similar(A,Complex128,Int)) == SparseMatrixCSC{Complex128,Int}
@test size(similar(A,Complex128,Int8)) == (5,5)
@test typeof(similar(A,Complex128,Int8)) == SparseMatrixCSC{Complex128,Int8}
@test similar(A,Complex128,(6,6)) == spzeros(Complex128,6,6)
@test convert(Matrix,A) == full(A)
# test similar methods for SparseMatrixCSCs
let
A = speye(5)
# test similar without specifications (preserves nonzero structure)
simA = similar(A)
@test typeof(simA) == typeof(A)
@test size(simA) == size(A)
@test simA.colptr == A.colptr
@test simA.rowval == A.rowval
@test length(simA.nzval) == length(A.nzval)
# test similar with entry type specification (preserves nonzero structure)
simA = similar(A, Complex128)
@test typeof(simA) == SparseMatrixCSC{Complex128,eltype(A.colptr)}
@test size(simA) == size(A)
@test simA.colptr == A.colptr
@test simA.rowval == A.rowval
@test length(simA.nzval) == length(A.nzval)
# test similar with entry and index type specification (preserves nonzero structure)
simA = similar(A, Complex128, Int8)
@test typeof(simA) == SparseMatrixCSC{Complex128,Int8}
@test size(simA) == size(A)
@test simA.colptr == A.colptr
@test simA.rowval == A.rowval
@test length(simA.nzval) == length(A.nzval)
# test similar with Dims{2} specification (preserves storage only, not nonzero structure)
simA = similar(A, (6,6))
@test typeof(simA) == typeof(A)
@test size(simA) == (6,6)
@test simA.colptr == ones(eltype(A.colptr), 6+1)
@test length(simA.rowval) == length(A.rowval)
@test length(simA.nzval) == length(A.nzval)
# test similar with entry type and Dims{2} specification (preserves storage structure only)
simA = similar(A, Complex128, (6,6))
@test typeof(simA) == SparseMatrixCSC{Complex128,eltype(A.colptr)}
@test size(simA) == (6,6)
@test simA.colptr == ones(eltype(A.colptr), 6+1)
@test length(simA.rowval) == length(A.rowval)
@test length(simA.nzval) == length(A.nzval)
# test similar with entry type, index type, and Dims{2} specification (preserves storage structure only)
simA = similar(A, Complex128, Int8, (6,6))
@test typeof(simA) == SparseMatrixCSC{Complex128, Int8}
@test size(simA) == (6,6)
@test simA.colptr == ones(eltype(A.colptr), 6+1)
@test length(simA.rowval) == length(A.rowval)
@test length(simA.nzval) == length(A.nzval)
# test similar with Dims{1} specification (preserves no structure)
simA = similar(A, (6,))
@test typeof(simA) == SparseVector{eltype(A.nzval),eltype(A.colptr)}
@test size(simA) == (6,)
@test length(simA.nzind) == 0
@test length(simA.nzval) == 0
# test similar with entry type and Dims{1} specification (preserves no structure)
simA = similar(A, Complex128, (6,))
@test typeof(simA) == SparseVector{Complex128,eltype(A.colptr)}
@test size(simA) == (6,)
@test length(simA.nzind) == 0
@test length(simA.nzval) == 0
# test similar with entry type, index type, and Dims{1} specification (preserves no structure)
simA = similar(A, Complex128, Int8, (6,))
@test typeof(simA) == SparseVector{Complex128,Int8}
@test size(simA) == (6,)
@test length(simA.nzind) == 0
@test length(simA.nzval) == 0
end

# test float
A = sprand(Bool, 5,5,0.0)
Expand Down

0 comments on commit 31e6622

Please sign in to comment.