View
@@ -12,6 +12,9 @@ def test_new
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 @@ def test_range_string
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 @@ def test_marshal
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 @@ def test_bad_value
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 @@ def test_eq
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,30 +177,54 @@ def test_eql
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
a = []
(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 @@ def test_step
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 << o1; self; end.class_eval do
(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 @@ def test_each
(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 @@ def test_begin_end
assert_equal(0, (0..1).begin)
assert_equal(1, (0..1).end)
assert_equal(1, (0...1).end)
assert_equal(0, (0..nil).begin)

This comment has been minimized.

Show comment
Hide comment
@soveran

soveran Apr 21, 2018

Could this syntax become a potential source of bugs? Let's say there's an application where a value n is expected to be an integer, but instead is nil. Let's suppose this application iterates over the range (0..n). In Ruby 2.5, the bug will result in an ArgumentError, but after this change it looks like it will loop forever. Is that the case?

@soveran

soveran Apr 21, 2018

Could this syntax become a potential source of bugs? Let's say there's an application where a value n is expected to be an integer, but instead is nil. Let's suppose this application iterates over the range (0..n). In Ruby 2.5, the bug will result in an ArgumentError, but after this change it looks like it will loop forever. Is that the case?

assert_equal(nil, (0..nil).end)
assert_equal(nil, (0...nil).end)
end
def test_first_last
@@ -308,6 +365,8 @@ def test_first_last
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 @@ def test_to_s
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)