|
| 1 | +# As part of the migration of SparseArrays.jl into its own repo, |
| 2 | +# these tests have been moved from other files in julia tests to |
| 3 | +# the SparseArrays.jl repo |
| 4 | + |
| 5 | +using Random, LinearAlgebra, SparseArrays |
| 6 | + |
| 7 | +# From arrayops.jl |
| 8 | + |
| 9 | +@testset "copy!" begin |
| 10 | + @testset "AbstractVector" begin |
| 11 | + s = Vector([1, 2]) |
| 12 | + for a = ([1], UInt[1], [3, 4, 5], UInt[3, 4, 5]) |
| 13 | + @test s === copy!(s, SparseVector(a)) == Vector(a) |
| 14 | + end |
| 15 | +end |
| 16 | + |
| 17 | +@testset "CartesianIndex" begin |
| 18 | + a = spzeros(2,3) |
| 19 | + @test CartesianIndices(size(a)) == eachindex(a) |
| 20 | + a[CartesianIndex{2}(2,3)] = 5 |
| 21 | + @test a[2,3] == 5 |
| 22 | + b = view(a, 1:2, 2:3) |
| 23 | + b[CartesianIndex{2}(1,1)] = 7 |
| 24 | + @test a[1,2] == 7 |
| 25 | +end |
| 26 | + |
| 27 | +@testset "Assignment of singleton array to sparse array (julia #43644)" begin |
| 28 | + K = spzeros(3,3) |
| 29 | + b = zeros(3,3) |
| 30 | + b[3,:] = [1,2,3] |
| 31 | + K[3,1:3] += [1.0 2.0 3.0]' |
| 32 | + @test K == b |
| 33 | + K[3:3,1:3] += zeros(1, 3) |
| 34 | + @test K == b |
| 35 | + K[3,1:3] += zeros(3) |
| 36 | + @test K == b |
| 37 | + K[3,:] += zeros(3,1) |
| 38 | + @test K == b |
| 39 | + @test_throws DimensionMismatch K[3,1:2] += [1.0 2.0 3.0]' |
| 40 | +end |
| 41 | + |
| 42 | +# From abstractarray.jl |
| 43 | + |
| 44 | +@testset "julia #17088" begin |
| 45 | + n = 10 |
| 46 | + M = rand(n, n) |
| 47 | + @testset "vector of vectors" begin |
| 48 | + v = [[M]; [M]] # using vcat |
| 49 | + @test size(v) == (2,) |
| 50 | + @test !issparse(v) |
| 51 | + end |
| 52 | + @testset "matrix of vectors" begin |
| 53 | + m1 = [[M] [M]] # using hcat |
| 54 | + m2 = [[M] [M];] # using hvcat |
| 55 | + @test m1 == m2 |
| 56 | + @test size(m1) == (1,2) |
| 57 | + @test !issparse(m1) |
| 58 | + @test !issparse(m2) |
| 59 | + end |
| 60 | +end |
| 61 | + |
| 62 | +@testset "mapslices julia #21123" begin |
| 63 | + @test mapslices(nnz, sparse(1.0I, 3, 3), dims=1) == [1 1 1] |
| 64 | +end |
| 65 | + |
| 66 | +@testset "itr, iterate" begin |
| 67 | + r = sparse(2:3:8) |
| 68 | + itr = eachindex(r) |
| 69 | + y = iterate(itr) |
| 70 | + @test y !== nothing |
| 71 | + y = iterate(itr, y[2]) |
| 72 | + y = iterate(itr, y[2]) |
| 73 | + @test y !== nothing |
| 74 | + val, state = y |
| 75 | + @test r[val] == 8 |
| 76 | + @test iterate(itr, state) == nothing |
| 77 | +end |
| 78 | + |
| 79 | +# From core.jl |
| 80 | + |
| 81 | +# issue #12960 |
| 82 | +mutable struct T12960 end |
| 83 | +import Base.zero |
| 84 | +Base.zero(::Type{T12960}) = T12960() |
| 85 | +Base.zero(x::T12960) = T12960() |
| 86 | +let |
| 87 | + A = sparse(1.0I, 3, 3) |
| 88 | + B = similar(A, T12960) |
| 89 | + @test repr(B) == "sparse([1, 2, 3], [1, 2, 3], $T12960[#undef, #undef, #undef], 3, 3)" |
| 90 | + @test occursin( |
| 91 | + "\n #undef ⋅ ⋅ \n ⋅ #undef ⋅ \n ⋅ ⋅ #undef", |
| 92 | + repr(MIME("text/plain"), B), |
| 93 | + ) |
| 94 | + |
| 95 | + B[1,2] = T12960() |
| 96 | + @test repr(B) == "sparse([1, 1, 2, 3], [1, 2, 2, 3], $T12960[#undef, $T12960(), #undef, #undef], 3, 3)" |
| 97 | + @test occursin( |
| 98 | + "\n #undef T12960() ⋅ \n ⋅ #undef ⋅ \n ⋅ ⋅ #undef", |
| 99 | + repr(MIME("text/plain"), B), |
| 100 | + ) |
| 101 | +end |
| 102 | + |
| 103 | +# julia issue #12063 |
| 104 | +# NOTE: should have > MAX_TUPLETYPE_LEN arguments |
| 105 | +f12063(tt, g, p, c, b, v, cu::T, d::AbstractArray{T, 2}, ve) where {T} = 1 |
| 106 | +f12063(args...) = 2 |
| 107 | +g12063() = f12063(0, 0, 0, 0, 0, 0, 0.0, spzeros(0,0), Int[]) |
| 108 | +@test g12063() == 1 |
0 commit comments