From 7f95eed19e22cb9a4867819355fe4ab99f85fd16 Mon Sep 17 00:00:00 2001 From: mame Date: Thu, 19 Apr 2018 15:18:50 +0000 Subject: Introduce endless range [Feature#12912] Typical usages: ``` p ary[1..] # drop the first element; identical to ary[1..-1] (1..).each {|n|...} # iterate forever from 1; identical to 1.step{...} ``` git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63192 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/ruby/test_array.rb | 13 ++++++++++ test/ruby/test_range.rb | 67 ++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 77 insertions(+), 3 deletions(-) (limited to 'test') diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb index 8bb0058576..7a5fbdb766 100644 --- a/test/ruby/test_array.rb +++ b/test/ruby/test_array.rb @@ -41,6 +41,7 @@ class TestArray < Test::Unit::TestCase assert_equal(2, x[2]) assert_equal([1, 2, 3], x[1..3]) assert_equal([1, 2, 3], x[1,3]) + assert_equal([3, 4, 5], x[3..]) x[0, 2] = 10 assert_equal([10, 2, 3, 4, 5], x) @@ -199,6 +200,8 @@ class TestArray < Test::Unit::TestCase assert_equal([0, 1, 2, 13, 4, 5], [0, 1, 2, 3, 4, 5].fill(3...4){|i| i+10}) assert_equal([0, 1, 12, 13, 14, 5], [0, 1, 2, 3, 4, 5].fill(2..-2){|i| i+10}) assert_equal([0, 1, 12, 13, 4, 5], [0, 1, 2, 3, 4, 5].fill(2...-2){|i| i+10}) + assert_equal([0, 1, 2, 13, 14, 15], [0, 1, 2, 3, 4, 5].fill(3..){|i| i+10}) + assert_equal([0, 1, 2, 13, 14, 15], [0, 1, 2, 3, 4, 5].fill(3...){|i| i+10}) end # From rubicon @@ -346,7 +349,9 @@ class TestArray < Test::Unit::TestCase assert_equal(@cls[99], a[-2..-2]) assert_equal(@cls[10, 11, 12], a[9..11]) + assert_equal(@cls[98, 99, 100], a[97..]) assert_equal(@cls[10, 11, 12], a[-91..-89]) + assert_equal(@cls[98, 99, 100], a[-3..]) assert_nil(a[10, -3]) assert_equal [], a[10..7] @@ -428,6 +433,10 @@ class TestArray < Test::Unit::TestCase assert_equal(nil, a[10..19] = nil) assert_equal(@cls[*(0..9).to_a] + @cls[nil] + @cls[*(20..99).to_a], a) + a = @cls[*(0..99).to_a] + assert_equal(nil, a[10..] = nil) + assert_equal(@cls[*(0..9).to_a] + @cls[nil], a) + a = @cls[1, 2, 3] a[1, 0] = a assert_equal([1, 1, 2, 3, 2, 3], a) @@ -1378,9 +1387,12 @@ class TestArray < Test::Unit::TestCase assert_equal(@cls[99], a.slice(-2..-2)) assert_equal(@cls[10, 11, 12], a.slice(9..11)) + assert_equal(@cls[98, 99, 100], a.slice(97..)) + assert_equal(@cls[10, 11, 12], a.slice(-91..-89)) assert_equal(@cls[10, 11, 12], a.slice(-91..-89)) assert_nil(a.slice(-101..-1)) + assert_nil(a.slice(-101..)) assert_nil(a.slice(10, -3)) assert_equal @cls[], a.slice(10..7) @@ -2106,6 +2118,7 @@ class TestArray < Test::Unit::TestCase assert_raise(IndexError) { [0][LONGP] = 2 } assert_raise(IndexError) { [0][(LONGP + 1) / 2 - 1] = 2 } assert_raise(IndexError) { [0][LONGP..-1] = 2 } + assert_raise(IndexError) { [0][LONGP..] = 2 } a = [0] a[2] = 4 assert_equal([0, nil, 4], a) diff --git a/test/ruby/test_range.rb b/test/ruby/test_range.rb index fef7acc062..c8a1be0e59 100644 --- a/test/ruby/test_range.rb +++ b/test/ruby/test_range.rb @@ -12,6 +12,9 @@ class TestRange < Test::Unit::TestCase assert_raise(ArgumentError) { (1.."3") } + assert_equal((0..nil), Range.new(0, nil, false)) + assert_equal((0...nil), Range.new(0, nil, true)) + obj = Object.new def obj.<=>(other) raise RuntimeError, "cmp" @@ -31,14 +34,17 @@ class TestRange < Test::Unit::TestCase assert_equal(["a"], ("a" .. "a").to_a) assert_equal(["a"], ("a" ... "b").to_a) assert_equal(["a", "b"], ("a" .. "b").to_a) + assert_equal([*"a".."z", "aa"], ("a"..).take(27)) end def test_range_numeric_string assert_equal(["6", "7", "8"], ("6".."8").to_a, "[ruby-talk:343187]") assert_equal(["6", "7"], ("6"..."8").to_a) assert_equal(["9", "10"], ("9".."10").to_a) + assert_equal(["9", "10"], ("9"..).take(2)) assert_equal(["09", "10"], ("09".."10").to_a, "[ruby-dev:39361]") assert_equal(["9", "10"], (SimpleDelegator.new("9").."10").to_a) + assert_equal(["9", "10"], (SimpleDelegator.new("9")..).take(2)) assert_equal(["9", "10"], ("9"..SimpleDelegator.new("10")).to_a) end @@ -123,9 +129,10 @@ class TestRange < Test::Unit::TestCase assert_equal(r, Marshal.load(Marshal.dump(r))) r = 1...2 assert_equal(r, Marshal.load(Marshal.dump(r))) - s = Marshal.dump(r) - s.sub!(/endi./n, 'end0') - assert_raise(ArgumentError) {Marshal.load(s)} + r = (1..) + assert_equal(r, Marshal.load(Marshal.dump(r))) + r = (1...) + assert_equal(r, Marshal.load(Marshal.dump(r))) end def test_bad_value @@ -135,6 +142,8 @@ class TestRange < Test::Unit::TestCase def test_exclude_end assert_not_predicate(0..1, :exclude_end?) assert_predicate(0...1, :exclude_end?) + assert_not_predicate(0.., :exclude_end?) + assert_predicate(0..., :exclude_end?) end def test_eq @@ -145,8 +154,17 @@ class TestRange < Test::Unit::TestCase assert_not_equal(r, (1..2)) assert_not_equal(r, (0..2)) assert_not_equal(r, (0...1)) + assert_not_equal(r, (0..nil)) subclass = Class.new(Range) assert_equal(r, subclass.new(0,1)) + + r = (0..nil) + assert_equal(r, r) + assert_equal(r, (0..nil)) + assert_not_equal(r, 0) + assert_not_equal(r, (0...nil)) + subclass = Class.new(Range) + assert_equal(r, subclass.new(0,nil)) end def test_eql @@ -159,12 +177,22 @@ class TestRange < Test::Unit::TestCase assert_not_operator(r, :eql?, 0...1) subclass = Class.new(Range) assert_operator(r, :eql?, subclass.new(0,1)) + + r = (0..nil) + assert_operator(r, :eql?, r) + assert_operator(r, :eql?, 0..nil) + assert_not_operator(r, :eql?, 0) + assert_not_operator(r, :eql?, 0...nil) + subclass = Class.new(Range) + assert_operator(r, :eql?, subclass.new(0,nil)) end def test_hash assert_kind_of(Integer, (0..1).hash) assert_equal((0..1).hash, (0..1).hash) assert_not_equal((0..1).hash, (0...1).hash) + assert_equal((0..nil).hash, (0..nil).hash) + assert_not_equal((0..nil).hash, (0...nil).hash) end def test_step @@ -172,17 +200,31 @@ class TestRange < Test::Unit::TestCase (0..10).step {|x| a << x } assert_equal([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], a) + a = [] + (0..).step {|x| a << x; break if a.size == 10 } + assert_equal([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], a) + a = [] (0..10).step(2) {|x| a << x } assert_equal([0, 2, 4, 6, 8, 10], a) + a = [] + (0..).step(2) {|x| a << x; break if a.size == 10 } + assert_equal([0, 2, 4, 6, 8, 10, 12, 14, 16, 18], a) + assert_raise(ArgumentError) { (0..10).step(-1) { } } assert_raise(ArgumentError) { (0..10).step(0) { } } + assert_raise(ArgumentError) { (0..).step(-1) { } } + assert_raise(ArgumentError) { (0..).step(0) { } } a = [] ("a" .. "z").step(2) {|x| a << x } assert_equal(%w(a c e g i k m o q s u w y), a) + a = [] + ("a" .. ).step(2) {|x| a << x; break if a.size == 13 } + assert_equal(%w(a c e g i k m o q s u w y), a) + a = [] ("a" .. "z").step(2**32) {|x| a << x } assert_equal(["a"], a) @@ -192,12 +234,16 @@ class TestRange < Test::Unit::TestCase assert_equal([4294967295, 4294967297], a) zero = (2**32).coerce(0).first assert_raise(ArgumentError) { (2**32-1 .. 2**32+1).step(zero) { } } + a = [] + (2**32-1 .. ).step(2) {|x| a << x; break if a.size == 2 } + assert_equal([4294967295, 4294967297], a) o1 = Object.new o2 = Object.new def o1.<=>(x); -1; end def o2.<=>(x); 0; end assert_raise(TypeError) { (o1..o2).step(1) { } } + assert_raise(TypeError) { (o1..).step(1) { } } class << o1; self; end.class_eval do define_method(:succ) { o2 } @@ -216,6 +262,10 @@ class TestRange < Test::Unit::TestCase (0..2).step(0.5) {|x| a << x } assert_equal([0, 0.5, 1.0, 1.5, 2.0], a) + a = [] + (0..).step(0.5) {|x| a << x; break if a.size == 5 } + assert_equal([0, 0.5, 1.0, 1.5, 2.0], a) + a = [] (0x40000000..0x40000002).step(0.5) {|x| a << x } assert_equal([1073741824, 1073741824.5, 1073741825.0, 1073741825.5, 1073741826], a) @@ -239,6 +289,10 @@ class TestRange < Test::Unit::TestCase (0..10).each {|x| a << x } assert_equal([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], a) + a = [] + (0..).each {|x| a << x; break if a.size == 10 } + assert_equal([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], a) + o1 = Object.new o2 = Object.new def o1.setcmp(v) @cmpresult = v end @@ -285,6 +339,9 @@ class TestRange < Test::Unit::TestCase assert_equal(0, (0..1).begin) assert_equal(1, (0..1).end) assert_equal(1, (0...1).end) + assert_equal(0, (0..nil).begin) + assert_equal(nil, (0..nil).end) + assert_equal(nil, (0...nil).end) end def test_first_last @@ -308,6 +365,8 @@ class TestRange < Test::Unit::TestCase def test_to_s assert_equal("0..1", (0..1).to_s) assert_equal("0...1", (0...1).to_s) + assert_equal("0..", (0..nil).to_s) + assert_equal("0...", (0...nil).to_s) bug11767 = '[ruby-core:71811] [Bug #11767]' assert_predicate(("0".taint.."1").to_s, :tainted?, bug11767) @@ -318,6 +377,8 @@ class TestRange < Test::Unit::TestCase def test_inspect assert_equal("0..1", (0..1).inspect) assert_equal("0...1", (0...1).inspect) + assert_equal("0..", (0..nil).inspect) + assert_equal("0...", (0...nil).inspect) bug11767 = '[ruby-core:71811] [Bug #11767]' assert_predicate(("0".taint.."1").inspect, :tainted?, bug11767) -- cgit v1.2.3