diff options
author | Akinori MUSHA <knu@idaemons.org> | 2022-07-29 13:56:54 +0900 |
---|---|---|
committer | Akinori MUSHA <knu@idaemons.org> | 2022-07-30 20:05:14 +0900 |
commit | 1a73a6cdd2068b815430b775fe25186dab693faa (patch) | |
tree | 69b17760f8d2d4680d63c9d61e22e6d34001a441 /test | |
parent | 233884542101737892ce3b56e03ce47731a78057 (diff) |
Implement Enumerator::Product and Enumerator.product [Feature #18685]
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/6197
Diffstat (limited to 'test')
-rw-r--r-- | test/ruby/test_enumerator.rb | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/test/ruby/test_enumerator.rb b/test/ruby/test_enumerator.rb index c823b79c6d..3ca33126d5 100644 --- a/test/ruby/test_enumerator.rb +++ b/test/ruby/test_enumerator.rb @@ -906,4 +906,42 @@ class TestEnumerator < Test::Unit::TestCase e.chain.each(&->{}) assert_equal(true, e.is_lambda) end + + def test_product + e = Enumerator::Product.new + assert_instance_of(Enumerator::Product, e) + assert_kind_of(Enumerator, e) + assert_equal(1, e.size) + elts = [] + e.each { |*x| elts << x } + assert_equal [[]], elts + + e = Enumerator::Product.new(1..3, %w[a b]) + assert_instance_of(Enumerator::Product, e) + assert_kind_of(Enumerator, e) + assert_equal(3 * 2, e.size) + elts = [] + e.each { |*x| elts << x } + assert_equal [[1, "a"], [1, "b"], [2, "a"], [2, "b"], [3, "a"], [3, "b"]], elts + + e = Enumerator.product(1..3, %w[a b]) + assert_instance_of(Enumerator::Product, e) + + elts = [] + ret = Enumerator.product(1..3, %w[a b]) { |*x| elts << x } + assert_instance_of(Enumerator::Product, ret) + assert_equal [[1, "a"], [1, "b"], [2, "a"], [2, "b"], [3, "a"], [3, "b"]], elts + + e = Enumerator.product(1.., 'a'..'c') + assert_equal(Float::INFINITY, e.size) + assert_equal [[1, "a"], [1, "b"], [1, "c"], [2, "a"]], e.take(4) + + e = Enumerator.product(1.., Enumerator.new { |y| y << 'a' << 'b' }) + assert_equal(Float::INFINITY, e.size) + assert_equal [[1, "a"], [1, "b"], [2, "a"], [2, "b"]], e.take(4) + + e = Enumerator.product(1..3, Enumerator.new { |y| y << 'a' << 'b' }) + assert_equal(nil, e.size) + assert_equal [[1, "a"], [1, "b"], [2, "a"], [2, "b"]], e.take(4) + end end |