diff options
author | Kazuki Yamaguchi <k@rhe.jp> | 2016-02-14 23:47:39 +0900 |
---|---|---|
committer | Kazuki Yamaguchi <k@rhe.jp> | 2016-03-18 14:53:55 +0900 |
commit | f2ece415562cf8525241464c6ff50e3dff6b1c80 (patch) | |
tree | 952935685e54075962e9f6f4ad52f61ae6bae15f | |
parent | 7e81ca14feaff4db75feaa1556dff64cbe30d7df (diff) | |
download | ruby-fix/set-sortedset-comparison.tar.gz |
set: fix SortedSet#superset? with rbtreefix/set-sortedset-comparison
* lib/set.rb (Set#{<,<=,>,>=}): check if the internal @hash has
<,<=,>,>= operators. In SortedSet, @hash may be a RBTree, which
doesn't have them.
-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 |