From bb83120638150400fac60a40c11f4ffb73ed3682 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathieu=20Besan=C3=A7on?= Date: Mon, 19 Sep 2022 12:46:03 +0200 Subject: [PATCH] tests --- src/sparsematrix.jl | 5 +++++ test/sparsematrix_constructors_indexing.jl | 5 +++++ test/sparsevector.jl | 7 +++++++ 3 files changed, 17 insertions(+) diff --git a/src/sparsematrix.jl b/src/sparsematrix.jl index 96f99942..f5c936b0 100644 --- a/src/sparsematrix.jl +++ b/src/sparsematrix.jl @@ -47,6 +47,11 @@ end SparseMatrixCSC(m, n, colptr::ReadOnly, rowval::ReadOnly, nzval::Vector) = SparseMatrixCSC(m, n, copy(parent(colptr)), copy(parent(rowval)), nzval) +""" + SparseMatrixCSC{Tv,Ti}(::UndefInitializer, m::Integer, n::Integer) + +Creates an empty sparse matrix with element type `Tv` and integer type `Ti` of size `m × n`. +""" SparseMatrixCSC{Tv,Ti}(::UndefInitializer, m::Integer, n::Integer) where {Tv, Ti} = spzeros(Tv, Ti, m, n) """ diff --git a/test/sparsematrix_constructors_indexing.jl b/test/sparsematrix_constructors_indexing.jl index 31818684..c7e919b9 100644 --- a/test/sparsematrix_constructors_indexing.jl +++ b/test/sparsematrix_constructors_indexing.jl @@ -51,6 +51,11 @@ end @test sparse([1, 1, 2, 2, 2], [1, 2, 1, 2, 2], 1.0, 2, 2, +) == sparse([1, 1, 2, 2], [1, 2, 1, 2], [1.0, 1.0, 1.0, 2.0], 2, 2) @test sparse([1, 1, 2, 2, 2], [1, 2, 1, 2, 2], -1.0, 2, 2, *) == sparse([1, 1, 2, 2], [1, 2, 1, 2], [-1.0, -1.0, -1.0, 1.0], 2, 2) @test sparse(sparse(Int32.(1:5), Int32.(1:5), trues(5))') isa SparseMatrixCSC{Bool,Int32} + # undef initializer + m = SparseMatrixCSC{Float32, Int16}(undef, 3, 4) + @test size(m) == (3, 4) + @test eltype(m) === Float32 + @test m == spzeros(3, 4) end @testset "concatenation tests" begin diff --git a/test/sparsevector.jl b/test/sparsevector.jl index 4ddab85e..b19a013a 100644 --- a/test/sparsevector.jl +++ b/test/sparsevector.jl @@ -216,6 +216,13 @@ end end end end + + @testset "Undef initializer" begin + v = SparseVector{Float32, Int16}(undef, 4) + @test size(v) == (4, ) + @test eltype(v) === Float32 + @test v == spzeros(Float32, 4) + end end ### Element access