diff options
Diffstat (limited to 'test/ruby/test_enumerator.rb')
-rw-r--r-- | test/ruby/test_enumerator.rb | 111 |
1 files changed, 105 insertions, 6 deletions
diff --git a/test/ruby/test_enumerator.rb b/test/ruby/test_enumerator.rb index 3ca33126d5..7599d43463 100644 --- a/test/ruby/test_enumerator.rb +++ b/test/ruby/test_enumerator.rb @@ -127,6 +127,17 @@ class TestEnumerator < Test::Unit::TestCase assert_equal([[1,5],[2,6],[3,7]], @obj.to_enum(:foo, 1, 2, 3).with_index(5).to_a) end + def test_with_index_under_gc_compact_stress + omit "compaction doesn't work well on s390x" if RUBY_PLATFORM =~ /s390x/ # https://github.com/ruby/ruby/pull/5077 + EnvUtil.under_gc_compact_stress do + assert_equal([[1, 0], [2, 1], [3, 2]], @obj.to_enum(:foo, 1, 2, 3).with_index.to_a) + assert_equal([[1, 5], [2, 6], [3, 7]], @obj.to_enum(:foo, 1, 2, 3).with_index(5).to_a) + + s = 1 << (8 * 1.size - 2) + assert_equal([[1, s], [2, s + 1], [3, s + 2]], @obj.to_enum(:foo, 1, 2, 3).with_index(s).to_a) + end + end + def test_with_index_large_offset bug8010 = '[ruby-dev:47131] [Bug #8010]' s = 1 << (8*1.size-2) @@ -244,6 +255,26 @@ class TestEnumerator < Test::Unit::TestCase assert_equal(res, exc.result) end + def test_stopiteration_rescue + e = [1].each + res = e.each {} + e.next + exc0 = assert_raise(StopIteration) { e.peek } + assert_include(exc0.backtrace.first, "test_enumerator.rb:#{__LINE__-1}:") + assert_nil(exc0.cause) + assert_equal(res, exc0.result) + + exc1 = assert_raise(StopIteration) { e.next } + assert_include(exc1.backtrace.first, "test_enumerator.rb:#{__LINE__-1}:") + assert_same(exc0, exc1.cause) + assert_equal(res, exc1.result) + + exc2 = assert_raise(StopIteration) { e.next } + assert_include(exc2.backtrace.first, "test_enumerator.rb:#{__LINE__-1}:") + assert_same(exc0, exc2.cause) + assert_equal(res, exc2.result) + end + def test_next_values o = Object.new def o.each @@ -832,6 +863,21 @@ class TestEnumerator < Test::Unit::TestCase assert_equal(33, chain.next) end + def test_lazy_chain_under_gc_compact_stress + omit "compaction doesn't work well on s390x" if RUBY_PLATFORM =~ /s390x/ # https://github.com/ruby/ruby/pull/5077 + EnvUtil.under_gc_compact_stress do + ea = (10..).lazy.select(&:even?).take(10) + ed = (20..).lazy.select(&:odd?) + chain = (ea + ed).select{|x| x % 3 == 0} + assert_equal(12, chain.next) + assert_equal(18, chain.next) + assert_equal(24, chain.next) + assert_equal(21, chain.next) + assert_equal(27, chain.next) + assert_equal(33, chain.next) + end + end + def test_chain_undef_methods chain = [1].to_enum + [2].to_enum meths = (chain.methods & [:feed, :next, :next_values, :peek, :peek_values]) @@ -907,41 +953,94 @@ class TestEnumerator < Test::Unit::TestCase assert_equal(true, e.is_lambda) end - def test_product + def test_product_new + # 0-dimensional 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 } + e.each { |x| elts << x } assert_equal [[]], elts + assert_equal elts, e.to_a + heads = [] + e.each { |x,| heads << x } + assert_equal [nil], heads + # 1-dimensional + e = Enumerator::Product.new(1..3) + assert_instance_of(Enumerator::Product, e) + assert_kind_of(Enumerator, e) + assert_equal(3, e.size) + elts = [] + e.each { |x| elts << x } + assert_equal [[1], [2], [3]], elts + assert_equal elts, e.to_a + + # 2-dimensional 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 } + e.each { |x| elts << x } assert_equal [[1, "a"], [1, "b"], [2, "a"], [2, "b"], [3, "a"], [3, "b"]], elts + assert_equal elts, e.to_a + heads = [] + e.each { |x,| heads << x } + assert_equal [1, 1, 2, 2, 3, 3], heads + + # Any enumerable is 0 size + assert_equal(0, Enumerator::Product.new([], 1..).size) + # Reject keyword arguments + assert_raise(ArgumentError) { + Enumerator::Product.new(1..3, foo: 1, bar: 2) + } + end + + def test_s_product + # without a block e = Enumerator.product(1..3, %w[a b]) assert_instance_of(Enumerator::Product, e) + # with a block 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 + ret = Enumerator.product(1..3) { |x| elts << x } + assert_equal(nil, ret) + assert_equal [[1], [2], [3]], elts + assert_equal elts, Enumerator.product(1..3).to_a + # an infinite enumerator and a finite enumerable 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) + # an infinite enumerator and an unknown enumerator 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) + # an infinite enumerator and an unknown enumerator 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) + + assert_equal(0, Enumerator.product([], 1..).size) + + # Reject keyword arguments + assert_raise(ArgumentError) { + Enumerator.product(1..3, foo: 1, bar: 2) + } + end + + def test_freeze + e = 3.times.freeze + assert_raise(FrozenError) { e.next } + assert_raise(FrozenError) { e.next_values } + assert_raise(FrozenError) { e.peek } + assert_raise(FrozenError) { e.peek_values } + assert_raise(FrozenError) { e.feed 1 } + assert_raise(FrozenError) { e.rewind } end end |