Skip to content

Commit

Permalink
test: remove SparseArrays dependency from hashing test (#50751)
Browse files Browse the repository at this point in the history
These tests now belong in SparseArrays, since it is removed from Base.
Since SparseArrays does some piracy, that can break or distort later
tests.

Refs #50550
  • Loading branch information
vtjnash authored Aug 10, 2023
1 parent 517ad06 commit 5db443a
Showing 1 changed file with 3 additions and 26 deletions.
29 changes: 3 additions & 26 deletions test/hashing.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

using Random, LinearAlgebra, SparseArrays
using Random, LinearAlgebra
isdefined(Main, :OffsetArrays) || @eval Main include("testhelpers/OffsetArrays.jl")
using .Main.OffsetArrays

Expand Down Expand Up @@ -92,9 +92,8 @@ vals = Any[
Dict(x => x for x in 1:10),
Dict(7=>7,9=>9,4=>4,10=>10,2=>2,3=>3,8=>8,5=>5,6=>6,1=>1),
[], [1], [2], [1, 1], [1, 2], [1, 3], [2, 2], [1, 2, 2], [1, 3, 3],
zeros(2, 2), spzeros(2, 2), Matrix(1.0I, 2, 2), sparse(1.0I, 2, 2),
sparse(fill(1., 2, 2)), fill(1., 2, 2), sparse([0 0; 1 0]), [0 0; 1 0],
[-0. 0; -0. 0.], SparseMatrixCSC(2, 2, [1, 3, 3], [1, 2], [-0., -0.]),
zeros(2, 2), Matrix(1.0I, 2, 2), fill(1., 2, 2),
[-0. 0; -0. 0.],
# issue #16364
1:4, 1:1:4, 1:-1:0, 1.0:4.0, 1.0:1.0:4.0, range(1, stop=4, length=4),
# issue #35597, when `LinearIndices` does not begin at 1
Expand Down Expand Up @@ -141,21 +140,13 @@ vals = Any[
[5 1; 0 0], [1 1; 0 1], [0 2; 3 0], [0 2; 4 6], [4 0; 0 1],
[0 0 0; 0 0 0], [1 0 0; 0 0 1], [0 0 2; 3 0 0], [0 0 7; 6 1 2],
[4 0 0; 3 0 1], [0 2 4; 6 0 0],
# various stored zeros patterns
sparse([1], [1], [0]), sparse([1], [1], [-0.0]),
sparse([1, 2], [1, 1], [-0.0, 0.0]), sparse([1, 2], [1, 1], [0.0, -0.0]),
sparse([1, 2], [1, 1], [-0.0, 0.0], 3, 1), sparse([1, 2], [1, 1], [0.0, -0.0], 3, 1),
sparse([1, 3], [1, 1], [-0.0, 0.0], 3, 1), sparse([1, 3], [1, 1], [0.0, -0.0], 3, 1),
sparse([1, 2, 3], [1, 1, 1], [-1, 0, 1], 3, 1), sparse([1, 2, 3], [1, 1, 1], [-1.0, -0.0, 1.0], 3, 1),
sparse([1, 3], [1, 1], [-1, 0], 3, 1), sparse([1, 2], [1, 1], [-1, 0], 3, 1)
]

for a in vals
b = Array(a)
@test hash(convert(Array{Any}, a)) == hash(b)
@test hash(convert(Array{supertype(eltype(a))}, a)) == hash(b)
@test hash(convert(Array{Float64}, a)) == hash(b)
@test hash(sparse(a)) == hash(b)
if !any(x -> isequal(x, -0.0), a)
@test hash(convert(Array{Int}, a)) == hash(b)
if all(x -> typemin(Int8) <= x <= typemax(Int8), a)
Expand All @@ -169,20 +160,6 @@ end
@test hash(Any[Int8(127), Int8(-128), 129, 130]) ==
hash([127, -128, 129, 130]) != hash([127, 128, 129, 130])

# Test hashing sparse matrix with type which does not support -
struct CustomHashReal
x::Float64
end
Base.hash(x::CustomHashReal, h::UInt) = hash(x.x, h)
Base.:(==)(x::CustomHashReal, y::Number) = x.x == y
Base.:(==)(x::Number, y::CustomHashReal) = x == y.x
Base.zero(::Type{CustomHashReal}) = CustomHashReal(0.0)
Base.zero(x::CustomHashReal) = zero(CustomHashReal)

let a = sparse([CustomHashReal(0), CustomHashReal(3), CustomHashReal(3)])
@test hash(a) == hash(Array(a))
end

vals = Any[
0.0:0.1:0.3, 0.3:-0.1:0.0,
0:-1:1, 0.0:-1.0:1.0, 0.0:1.1:10.0, -4:10,
Expand Down

0 comments on commit 5db443a

Please sign in to comment.