Skip to content

Commit d1123fb

Browse files
committed
Add tests
1 parent d8576c2 commit d1123fb

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

src/abstractsparsearray.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ sparse(::Union{AbstractDict,AbstractDictionary}, dims...; kwargs...)
5151
const AbstractDictOrDictionary = Union{AbstractDict,AbstractDictionary}
5252
# checked constructor from data: use `setindex!` to validate/convert input
5353
function sparse(storage::AbstractDictOrDictionary, dims::Dims; kwargs...)
54-
A = SparseArrayDOK{eltype(storage)}(undef, dims; kwargs...)
54+
A = SparseArrayDOK{valtype(storage)}(undef, dims; kwargs...)
5555
for (i, v) in pairs(storage)
5656
A[i] = v
5757
end

test/test_sparsearraydok.jl

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,20 @@ using JLArrays: JLArray, @allowscalar
44
using SparseArraysBase:
55
SparseArraysBase,
66
SparseArrayDOK,
7+
SparseMatrixDOK,
78
eachstoredindex,
89
getstoredindex,
910
getunstoredindex,
1011
isstored,
1112
setstoredindex!,
1213
setunstoredindex!,
14+
sparse,
15+
sparserand,
16+
sparsezeros,
1317
storedlength,
1418
storedpairs,
1519
storedvalues
20+
using StableRNGs: StableRNG
1621
using Test: @test, @testset
1722

1823
elts = (Float32, Float64, Complex{Float32}, Complex{Float64})
@@ -216,4 +221,53 @@ arrayts = (Array,)
216221
@test storedlength(r1) == 2
217222
@test a == [11 12; 12 22]
218223
@test storedlength(a) == 4
224+
225+
d = Dict([CartesianIndex(1, 2) => elt(12), CartesianIndex(2, 1) => elt(21)])
226+
for a in (
227+
sparse(d, 2, 2),
228+
sparse(d, 2, 2; getunstored=Returns(zero(elt))),
229+
sparse(d, (2, 2)),
230+
sparse(d, (2, 2); getunstored=Returns(zero(elt))),
231+
)
232+
@test !iszero(a)
233+
@test iszero(a[1, 1])
234+
@test a[2, 1] == elt(21)
235+
@test a[1, 2] == elt(12)
236+
@test iszero(a[2, 2])
237+
@test size(a) == (2, 2)
238+
@test storedlength(a) == 2
239+
@test eltype(a) === elt
240+
@test a isa SparseMatrixDOK{elt}
241+
end
242+
243+
for (a, elt′) in (
244+
(sparsezeros(elt, 2, 2), elt),
245+
(sparsezeros(elt, 2, 2; getunstored=Returns(zero(elt))), elt),
246+
(sparsezeros(elt, (2, 2)), elt),
247+
(sparsezeros(elt, (2, 2); getunstored=Returns(zero(elt))), elt),
248+
(sparsezeros(2, 2), Float64),
249+
(sparsezeros(2, 2; getunstored=Returns(zero(Float64))), Float64),
250+
(sparsezeros((2, 2)), Float64),
251+
(sparsezeros((2, 2); getunstored=Returns(zero(Float64))), Float64),
252+
)
253+
@test iszero(a)
254+
@test size(a) == (2, 2)
255+
@test storedlength(a) == 0
256+
@test eltype(a) === elt′
257+
@test a isa SparseMatrixDOK{elt′}
258+
end
259+
260+
rng = StableRNG(123)
261+
for (a, elt′) in (
262+
(sparserand(rng, elt, 20, 20; density=0.5), elt),
263+
(sparserand(rng, elt, (20, 20); density=0.5), elt),
264+
(sparserand(rng, 20, 20; density=0.5), Float64),
265+
(sparserand(rng, (20, 20); density=0.5), Float64),
266+
)
267+
@test !iszero(a)
268+
@test size(a) == (20, 20)
269+
@test !iszero(storedlength(a))
270+
@test eltype(a) === elt′
271+
@test a isa SparseMatrixDOK{elt′}
272+
end
219273
end

0 commit comments

Comments
 (0)