Skip to content

Commit

Permalink
fix JuliaLang#32550: issetequal with duplicate values
Browse files Browse the repository at this point in the history
add tests that set ops fail for non-sets (JuliaLang#32550)
  • Loading branch information
StefanKarpinski committed Aug 8, 2019
1 parent e4456d0 commit a135040
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
16 changes: 15 additions & 1 deletion base/abstractset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,22 @@ julia> issetequal([1, 2], [2, 1])
true
```
"""
issetequal(l, r) = length(l) == length(r) && l r
issetequal(l::AbstractSet, r::AbstractSet) = l == r
issetequal(l::AbstractSet, r) = issetequal(l, Set(r))

function issetequal(l, r::AbstractSet)
if haslength(l)
# check r for too many unique elements
length(l) < length(r) && return false
end
return issetequal(Set(l), r)
end

function issetequal(l, r)
haslength(l) && return issetequal(l, Set(r))
haslength(r) && return issetequal(r, Set(l))
return issetequal(Set(l), Set(r))
end

## partial ordering of sets by containment

Expand Down
23 changes: 14 additions & 9 deletions test/sets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -592,11 +592,16 @@ end
end

@testset "⊆, ⊊, ⊈, ⊇, ⊋, ⊉, <, <=, issetequal" begin
a = [1, 2]
b = [2, 1, 3]
for C = (Tuple, identity, Set, BitSet, Base.IdSet{Int})
A = C(a)
B = C(b)
a = [2, 1, 2]
b = [2, 3, 1, 3]
ua = unique(a)
ub = unique(b)
for TA in (Tuple, identity, Set, BitSet, Base.IdSet{Int}),
TB in (Tuple, identity, Set, BitSet, Base.IdSet{Int}),
uA = false:true,
uB = false:true
A = TA(uA ? ua : a)
B = TB(uB ? ub : b)
@test A B
@test A B
@test !(A B)
Expand All @@ -611,6 +616,10 @@ end
@test !(B A)
@test !issetequal(A, B)
@test !issetequal(B, A)
for T = (Tuple, identity, Set, BitSet, Base.IdSet{Int})
@test issetequal(A, T(A))
@test issetequal(B, T(B))
end
if A isa AbstractSet && B isa AbstractSet
@test A <= B
@test A < B
Expand All @@ -621,10 +630,6 @@ end
@test B >= A
@test B > A
end
for D = (Tuple, identity, Set, BitSet)
@test issetequal(A, D(A))
@test !issetequal(A, D(B))
end
end
end

Expand Down

0 comments on commit a135040

Please sign in to comment.