summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-array-ext/test
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2021-11-04 15:54:09 +0900
committerGitHub <[email protected]>2021-11-04 15:54:09 +0900
commit8f67a0d0e1c6c05a82645917ee3e1c0dbcd6e0b4 (patch)
treea3fe8bbd974100e787ada69a6eb66cf584b7b07d /mrbgems/mruby-array-ext/test
parentb940ed69d9acc51ed06c3e97364d94253f41e378 (diff)
parent5ea26260fa0e178537199c7fd608dd3e1e1eafdb (diff)
downloadmruby-8f67a0d0e1c6c05a82645917ee3e1c0dbcd6e0b4.tar.gz
mruby-8f67a0d0e1c6c05a82645917ee3e1c0dbcd6e0b4.zip
Merge pull request #5569 from dearblue/array-product
Added `Array#product` method
Diffstat (limited to 'mrbgems/mruby-array-ext/test')
-rw-r--r--mrbgems/mruby-array-ext/test/array.rb22
1 files changed, 22 insertions, 0 deletions
diff --git a/mrbgems/mruby-array-ext/test/array.rb b/mrbgems/mruby-array-ext/test/array.rb
index 3f73ad8b9..879980c7e 100644
--- a/mrbgems/mruby-array-ext/test/array.rb
+++ b/mrbgems/mruby-array-ext/test/array.rb
@@ -421,3 +421,25 @@ assert('Array#transpose') do
assert_raise(TypeError) { [1].transpose }
assert_raise(IndexError) { [[1], [2,3,4]].transpose }
end
+
+assert "Array#product" do
+ assert_equal [[1], [2], [3]], [1, 2, 3].product
+ assert_equal [], [1, 2, 3].product([])
+ assert_equal [], [1, 2, 3].product([4, 5, 6], [])
+
+ expect = [[1, 5, 8], [1, 5, 9], [1, 6, 8], [1, 6, 9], [1, 7, 8], [1, 7, 9],
+ [2, 5, 8], [2, 5, 9], [2, 6, 8], [2, 6, 9], [2, 7, 8], [2, 7, 9],
+ [3, 5, 8], [3, 5, 9], [3, 6, 8], [3, 6, 9], [3, 7, 8], [3, 7, 9],
+ [4, 5, 8], [4, 5, 9], [4, 6, 8], [4, 6, 9], [4, 7, 8], [4, 7, 9]]
+ assert_equal expect, [1, 2, 3, 4].product([5, 6, 7], [8, 9])
+
+ expect = [[1, 4, 7], [1, 4, 8], [1, 4, 9], [1, 5, 7], [1, 5, 8], [1, 5, 9], [1, 6, 7], [1, 6, 8], [1, 6, 9],
+ [2, 4, 7], [2, 4, 8], [2, 4, 9], [2, 5, 7], [2, 5, 8], [2, 5, 9], [2, 6, 7], [2, 6, 8], [2, 6, 9],
+ [3, 4, 7], [3, 4, 8], [3, 4, 9], [3, 5, 7], [3, 5, 8], [3, 5, 9], [3, 6, 7], [3, 6, 8], [3, 6, 9]]
+
+ assert_equal expect, [1, 2, 3].product([4, 5, 6], [7, 8, 9])
+ base = [1, 2, 3]
+ x = []
+ assert_equal base, base.product([4, 5, 6], [7, 8, 9]) { |e| x << e }
+ assert_equal expect, x
+end