Skip to content

Commit

Permalink
isequal(::Set, ::Set) + tests.
Browse files Browse the repository at this point in the history
The current behaviour when comparing sets of unrelated types is not
particularly consistent. (I suspect that similar inconsistencies will
be present in the implementations of intersect, setdiff and union.)
The tests describing this behaviour do not necessarily state that this
is the correct behaviour, thery merely document what is currently
present.

The implementation was provided earlier, mixed in with the tests for
Set. Now the implementation has been moved to base/set.jl, and some
tests for it have been added to test/corelib.jl

A helper macro (@assert_raises) was added (in test/corelib.jl), to
make it easier to express tests about error conditions. This should be
moved out to some more sensible place.
  • Loading branch information
Jacek Generowicz committed Oct 31, 2012
1 parent d086403 commit a7ea7e9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
2 changes: 2 additions & 0 deletions base/set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,5 @@ setdiff(a::Set, b::Set) = del_each(copy(a),b)
|(s::Set...) = union(s...)
(&)(s::Set...) = intersect(s...)
-(a::Set, b::Set) = setdiff(a,b)

isequal(l::Set, r::Set) = length(l) == length(r) == length(intersect(l,r))
49 changes: 43 additions & 6 deletions test/corelib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,6 @@ end

# #################### set ####################

# Is there any good reason why sets (and dicts) do not implement
# isequal? I'm providing a quick'n'dirty implementation here to help
# with the following tests. Will suggest implementation of isequal for
# sets and dicts, in a later pull request.
isequal(l::Set,r::Set) = length(l) == length(r) == length(intersect(l,r))

# show

# isempty
Expand Down Expand Up @@ -277,4 +271,47 @@ for data_in in ((7,8,4,5),
@assert has(s,e)
end
end

# isequal
@assert isequal(Set(), Set())
@assert !isequal(Set(), Set(1))
@assert isequal(Set{Any}(1,2), Set{Int}(1,2))
@assert !isequal(Set{Any}(1,2), Set{Int}(1,2,3))

# Helper for writing tests about error conditions, used in the
# following tests
macro assert_raises(ExcType, expression)
quote
try
$expression
catch e
if ! (typeof(e) <: $ExcType)
println("Error : ", e )
println("Type of error: ", typeof(e))
println("Expected : ", $ExcType)
@assert false
end
end
end
end

# Comparison of unrelated types seems rather inconsistent

# Not sure whether the behaviour of isequal (as demonstrated below) on
# sets of unrelated types, is desirable, but it's what there is at the
# moment.
@assert isequal(Set{Int}(), Set{String}())
@assert_raises TypeError isequal(Set{Int}(), Set{String}{""})
@assert !isequal(Set{String}(), Set{Int}(0))
@assert_raises MethodError isequal(Set{Int}(1), Set{String}())

@assert isequal(Set{Any}(1,2,3), Set{Int}(1,2,3))
@assert isequal(Set{Int}(1,2,3), Set{Any}(1,2,3))

@assert !isequal(Set{Any}(1,2,3), Set{Int}(1,2,3,4))
@assert !isequal(Set{Int}(1,2,3), Set{Any}(1,2,3,4))

@assert !isequal(Set{Any}(1,2,3,4), Set{Int}(1,2,3))
@assert !isequal(Set{Int}(1,2,3,4), Set{Any}(1,2,3))

# ########## end of set tests ##########

0 comments on commit a7ea7e9

Please sign in to comment.