diff options
-rw-r--r-- | lib/set.rb | 8 | ||||
-rw-r--r-- | test/test_set.rb | 20 |
2 files changed, 24 insertions, 4 deletions
diff --git a/lib/set.rb b/lib/set.rb index a8f4345f35..d1f99e0eb3 100644 --- a/lib/set.rb +++ b/lib/set.rb @@ -219,7 +219,7 @@ class Set # Returns true if the set is a superset of the given set. def superset?(set) case - when set.instance_of?(self.class) + when set.instance_of?(self.class) && @hash.respond_to?(:>=) @hash >= set.instance_variable_get(:@hash) when set.is_a?(Set) size >= set.size && set.all? { |o| include?(o) } @@ -232,7 +232,7 @@ class Set # Returns true if the set is a proper superset of the given set. def proper_superset?(set) case - when set.instance_of?(self.class) + when set.instance_of?(self.class) && @hash.respond_to?(:>) @hash > set.instance_variable_get(:@hash) when set.is_a?(Set) size > set.size && set.all? { |o| include?(o) } @@ -245,7 +245,7 @@ class Set # Returns true if the set is a subset of the given set. def subset?(set) case - when set.instance_of?(self.class) + when set.instance_of?(self.class) && @hash.respond_to?(:<=) @hash <= set.instance_variable_get(:@hash) when set.is_a?(Set) size <= set.size && all? { |o| set.include?(o) } @@ -258,7 +258,7 @@ class Set # Returns true if the set is a proper subset of the given set. def proper_subset?(set) case - when set.instance_of?(self.class) + when set.instance_of?(self.class) && @hash.respond_to?(:<) @hash < set.instance_variable_get(:@hash) when set.is_a?(Set) size < set.size && all? { |o| set.include?(o) } diff --git a/test/test_set.rb b/test/test_set.rb index 15c5a13d21..2adbadddcf 100644 --- a/test/test_set.rb +++ b/test/test_set.rb @@ -708,6 +708,26 @@ class TC_SortedSet < Test::Unit::TestCase set << 42 assert_equal(7, e.size) end + + def test_superset + set = SortedSet.new([1,2,3]) + + assert_equal(false, set.superset?(Set.new([1,2,3,4]))) + assert_equal(true, set >= SortedSet.new([1,2,3])) + + assert_equal(false, set.proper_superset?(Set.new([1,2,3,4]))) + assert_equal(false, set > SortedSet.new([1,2,3])) + end + + def test_subset + set = SortedSet.new([1,2,3]) + + assert_equal(true, set.subset?(Set.new([1,2,3,4]))) + assert_equal(true, set <= SortedSet.new([1,2,3])) + + assert_equal(true, set.proper_subset?(Set.new([1,2,3,4]))) + assert_equal(false, set < SortedSet.new([1,2,3])) + end end class TC_Enumerable < Test::Unit::TestCase |