summaryrefslogtreecommitdiff
path: root/test/ruby/test_array.rb
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-09-20 03:18:52 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-09-20 03:18:52 +0000
commit744e816f55757df92caa49b4787cf06af6120a20 (patch)
tree924a4754454851c5bdc7c67af7684f3b4ffbd16d /test/ruby/test_array.rb
parentd325d74174c3ffffa0d75e248897cbb1aae93407 (diff)
Add union method to Array
I introduce a `union` method equivalent to the `|` operator, but which accept more than array as argument. This improved readability, and it is also coherent with the `+` operator, which has a similar `concat` method. The method doesn't modify the original object and return a new object instead. It is plan to introduce a `union!` method as well. Tests and documentation are included. It solves partially https://bugs.ruby-lang.org/issues/14097 [Fix GH-1747] [Feature #14097] From: Ana María Martínez Gómez <ammartinez@suse.de> git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64787 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/ruby/test_array.rb')
-rw-r--r--test/ruby/test_array.rb38
1 files changed, 38 insertions, 0 deletions
diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb
index 9962745f1c..e1a77b73b1 100644
--- a/test/ruby/test_array.rb
+++ b/test/ruby/test_array.rb
@@ -1884,6 +1884,44 @@ class TestArray < Test::Unit::TestCase
assert_equal((1..128).to_a, b)
end
+ def test_union
+ assert_equal(@cls[], @cls[].union(@cls[]))
+ assert_equal(@cls[1], @cls[1].union(@cls[]))
+ assert_equal(@cls[1], @cls[].union(@cls[1]))
+ assert_equal(@cls[1], @cls[].union(@cls[], @cls[1]))
+ assert_equal(@cls[1], @cls[1].union(@cls[1]))
+ assert_equal(@cls[1], @cls[1].union(@cls[1], @cls[1], @cls[1]))
+
+ assert_equal(@cls[1,2], @cls[1].union(@cls[2]))
+ assert_equal(@cls[1,2], @cls[1, 1].union(@cls[2, 2]))
+ assert_equal(@cls[1,2], @cls[1, 2].union(@cls[1, 2]))
+ assert_equal(@cls[1,2], @cls[1, 1].union(@cls[1, 1], @cls[1, 2], @cls[2, 1], @cls[2, 2, 2]))
+
+ a = %w(a b c)
+ b = %w(a b c d e)
+ c = a.union(b)
+ assert_equal(c, b)
+ assert_not_same(c, b)
+ assert_equal(%w(a b c), a)
+ assert_equal(%w(a b c d e), b)
+ assert(a.none?(&:frozen?))
+ assert(b.none?(&:frozen?))
+ assert(c.none?(&:frozen?))
+ end
+
+ def test_union_big_array
+ assert_equal(@cls[1,2], (@cls[1]*64).union(@cls[2]*64))
+ assert_equal(@cls[1,2,3], (@cls[1, 2]*64).union(@cls[1, 2]*64, @cls[3]*60))
+
+ a = (1..64).to_a
+ b = (1..128).to_a
+ c = a | b
+ assert_equal(c, b)
+ assert_not_same(c, b)
+ assert_equal((1..64).to_a, a)
+ assert_equal((1..128).to_a, b)
+ end
+
def test_combination
a = @cls[]
assert_equal(1, a.combination(0).size)