summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-07-30 09:58:13 +0000
committerknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-07-30 09:58:13 +0000
commitbd304ed85bc2f3c0cc57334fe624fc0efa122cbc (patch)
treeb71bc1e56607fa98e3c77f3c7575cc5e888de376 /test
parent2e0fc4b21e6c14565afea8eb8c4d38097ead77ee (diff)
downloadruby-bd304ed85bc2f3c0cc57334fe624fc0efa122cbc.tar.gz
Add Set#intersect? and #disjoint?.
* lib/set.rb (Set#intersect?, Set#disjoint?): Add new methods for testing if two sets have any element in common. [ruby-core:45641] [Feature #6588] Based on the code by marcandre. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42253 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test')
-rw-r--r--test/test_set.rb40
1 files changed, 40 insertions, 0 deletions
diff --git a/test/test_set.rb b/test/test_set.rb
index cbf4eb0234..1f6f2436f5 100644
--- a/test/test_set.rb
+++ b/test/test_set.rb
@@ -297,6 +297,46 @@ class TC_Set < Test::Unit::TestCase
assert_equal(false, Set[].proper_subset?(Set[]))
end
+ def assert_intersect(expected, set, other)
+ case expected
+ when true
+ assert_send([set, :intersect?, other])
+ assert_send([other, :intersect?, set])
+ assert_not_send([set, :disjoint?, other])
+ assert_not_send([other, :disjoint?, set])
+ when false
+ assert_not_send([set, :intersect?, other])
+ assert_not_send([other, :intersect?, set])
+ assert_send([set, :disjoint?, other])
+ assert_send([other, :disjoint?, set])
+ when Class
+ assert_raises(expected) {
+ set.intersect?(other)
+ }
+ assert_raises(expected) {
+ set.disjoint?(other)
+ }
+ else
+ raise ArgumentError, "%s: unsupported expected value: %s" % [__method__, expected.inspect]
+ end
+ end
+
+ def test_intersect?
+ set = Set[3,4,5]
+
+ assert_intersect(ArgumentError, set, 3)
+ assert_intersect(ArgumentError, set, [2,4,6])
+
+ assert_intersect(true, set, Set[2,4])
+ assert_intersect(true, set, Set[5,6,7])
+ assert_intersect(true, set, Set[1,2,6,8,4])
+
+ assert_intersect(false, set, Set[])
+ assert_intersect(false, set, Set[0,2])
+ assert_intersect(false, set, Set[0,2,6])
+ assert_intersect(false, set, Set[0,2,6,8,10])
+ end
+
def test_each
ary = [1,3,5,7,10,20]
set = Set.new(ary)