2003-10-16 22:18:01 -04:00
|
|
|
require 'test/unit'
|
2009-08-17 12:41:00 -04:00
|
|
|
require 'delegate'
|
2009-11-26 07:17:47 -05:00
|
|
|
require 'timeout'
|
2003-10-16 22:18:01 -04:00
|
|
|
|
|
|
|
class TestRange < Test::Unit::TestCase
|
|
|
|
def test_range_string
|
|
|
|
# XXX: Is this really the test of Range?
|
|
|
|
assert_equal([], ("a" ... "a").to_a)
|
|
|
|
assert_equal(["a"], ("a" .. "a").to_a)
|
|
|
|
assert_equal(["a"], ("a" ... "b").to_a)
|
|
|
|
assert_equal(["a", "b"], ("a" .. "b").to_a)
|
|
|
|
end
|
2005-06-30 02:27:05 -04:00
|
|
|
|
2009-08-16 18:43:48 -04:00
|
|
|
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)
|
2009-09-23 00:14:23 -04:00
|
|
|
assert_equal(["09", "10"], ("09".."10").to_a, "[ruby-dev:39361]")
|
2009-08-17 12:41:00 -04:00
|
|
|
assert_equal(["9", "10"], (SimpleDelegator.new("9").."10").to_a)
|
|
|
|
assert_equal(["9", "10"], ("9"..SimpleDelegator.new("10")).to_a)
|
2009-08-16 18:43:48 -04:00
|
|
|
end
|
|
|
|
|
2009-08-17 13:00:47 -04:00
|
|
|
def test_range_symbol
|
|
|
|
assert_equal([:a, :b], (:a .. :b).to_a)
|
|
|
|
end
|
|
|
|
|
2005-06-30 02:27:05 -04:00
|
|
|
def test_evaluation_order
|
|
|
|
arr = [1,2]
|
|
|
|
r = (arr.shift)..(arr.shift)
|
|
|
|
assert_equal(1..2, r, "[ruby-dev:26383]")
|
|
|
|
end
|
2005-08-04 01:19:44 -04:00
|
|
|
|
|
|
|
class DuckRange
|
2005-08-05 19:56:02 -04:00
|
|
|
def initialize(b,e,excl=false)
|
2005-08-04 01:19:44 -04:00
|
|
|
@begin = b
|
|
|
|
@end = e
|
2005-08-05 19:56:02 -04:00
|
|
|
@excl = excl
|
2005-08-04 01:19:44 -04:00
|
|
|
end
|
|
|
|
attr_reader :begin, :end
|
2005-08-05 19:56:02 -04:00
|
|
|
|
|
|
|
def exclude_end?
|
|
|
|
@excl
|
|
|
|
end
|
2005-08-04 01:19:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_duckrange
|
|
|
|
assert_equal("bc", "abcd"[DuckRange.new(1,2)])
|
|
|
|
end
|
2005-11-25 00:42:28 -05:00
|
|
|
|
|
|
|
def test_min
|
|
|
|
assert_equal(1, (1..2).min)
|
|
|
|
assert_equal(nil, (2..1).min)
|
|
|
|
assert_equal(1, (1...2).min)
|
|
|
|
|
|
|
|
assert_equal(1.0, (1.0..2.0).min)
|
|
|
|
assert_equal(nil, (2.0..1.0).min)
|
|
|
|
assert_equal(1, (1.0...2.0).min)
|
2007-07-12 22:33:11 -04:00
|
|
|
|
|
|
|
assert_equal(0, (0..0).min)
|
|
|
|
assert_equal(nil, (0...0).min)
|
2005-11-25 00:42:28 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_max
|
|
|
|
assert_equal(2, (1..2).max)
|
|
|
|
assert_equal(nil, (2..1).max)
|
|
|
|
assert_equal(1, (1...2).max)
|
|
|
|
|
|
|
|
assert_equal(2.0, (1.0..2.0).max)
|
|
|
|
assert_equal(nil, (2.0..1.0).max)
|
|
|
|
assert_raise(TypeError) { (1.0...2.0).max }
|
2007-07-12 09:23:29 -04:00
|
|
|
|
|
|
|
assert_equal(-0x80000002, ((-0x80000002)...(-0x80000001)).max)
|
2007-07-12 22:33:11 -04:00
|
|
|
|
|
|
|
assert_equal(0, (0..0).max)
|
|
|
|
assert_equal(nil, (0...0).max)
|
2005-11-25 00:42:28 -05:00
|
|
|
end
|
2007-09-10 02:25:40 -04:00
|
|
|
|
|
|
|
def test_initialize_twice
|
|
|
|
r = eval("1..2")
|
|
|
|
assert_raise(NameError) { r.instance_eval { initialize 3, 4 } }
|
|
|
|
end
|
2007-09-10 02:50:22 -04:00
|
|
|
|
|
|
|
def test_uninitialized_range
|
|
|
|
r = Range.allocate
|
|
|
|
s = Marshal.dump(r)
|
|
|
|
r = Marshal.load(s)
|
|
|
|
assert_nothing_raised { r.instance_eval { initialize 5, 6} }
|
|
|
|
end
|
2008-01-29 11:26:07 -05:00
|
|
|
|
|
|
|
def test_bad_value
|
|
|
|
assert_raise(ArgumentError) { (1 .. :a) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_exclude_end
|
|
|
|
assert(!((0..1).exclude_end?))
|
|
|
|
assert((0...1).exclude_end?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_eq
|
|
|
|
r = (0..1)
|
|
|
|
assert(r == r)
|
|
|
|
assert(r == (0..1))
|
|
|
|
assert(r != 0)
|
|
|
|
assert(r != (1..2))
|
|
|
|
assert(r != (0..2))
|
|
|
|
assert(r != (0...1))
|
2009-08-05 01:01:12 -04:00
|
|
|
subclass = Class.new(Range)
|
|
|
|
assert(r == subclass.new(0,1))
|
2008-01-29 11:26:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_eql
|
|
|
|
r = (0..1)
|
|
|
|
assert(r.eql?(r))
|
|
|
|
assert(r.eql?(0..1))
|
|
|
|
assert(!r.eql?(0))
|
|
|
|
assert(!r.eql?(1..2))
|
|
|
|
assert(!r.eql?(0..2))
|
|
|
|
assert(!r.eql?(0...1))
|
2009-08-05 01:01:12 -04:00
|
|
|
subclass = Class.new(Range)
|
|
|
|
assert(r.eql?(subclass.new(0,1)))
|
2008-01-29 11:26:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_hash
|
|
|
|
assert((0..1).hash.is_a?(Fixnum))
|
|
|
|
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..10).step(2) {|x| a << x }
|
|
|
|
assert_equal([0, 2, 4, 6, 8, 10], a)
|
|
|
|
|
|
|
|
assert_raise(ArgumentError) { (0..10).step(-1) { } }
|
|
|
|
assert_raise(ArgumentError) { (0..10).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" .. "z").step(2**32) {|x| a << x }
|
|
|
|
assert_equal(["a"], a)
|
|
|
|
|
|
|
|
a = []
|
|
|
|
(2**32-1 .. 2**32+1).step(2) {|x| a << x }
|
|
|
|
assert_equal([4294967295, 4294967297], a)
|
|
|
|
zero = (2**32).coerce(0).first
|
|
|
|
assert_raise(ArgumentError) { (2**32-1 .. 2**32+1).step(zero) { } }
|
|
|
|
|
|
|
|
o1 = Object.new
|
|
|
|
o2 = Object.new
|
|
|
|
def o1.<=>(x); -1; end
|
|
|
|
def o2.<=>(x); 0; end
|
|
|
|
assert_raise(TypeError) { (o1..o2).step(1) { } }
|
|
|
|
|
|
|
|
class << o1; self; end.class_eval do
|
|
|
|
define_method(:succ) { o2 }
|
|
|
|
end
|
|
|
|
a = []
|
|
|
|
(o1..o2).step(1) {|x| a << x }
|
|
|
|
assert_equal([o1, o2], a)
|
|
|
|
|
|
|
|
a = []
|
|
|
|
(o1...o2).step(1) {|x| a << x }
|
|
|
|
assert_equal([o1], a)
|
2008-05-02 03:52:01 -04:00
|
|
|
|
|
|
|
assert_nothing_raised("[ruby-dev:34557]") { (0..2).step(0.5) {|x| } }
|
|
|
|
|
|
|
|
a = []
|
|
|
|
(0..2).step(0.5) {|x| a << x }
|
|
|
|
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)
|
|
|
|
|
|
|
|
o = Object.new
|
|
|
|
def o.to_int() 1 end
|
|
|
|
assert_nothing_raised("[ruby-dev:34558]") { (0..2).step(o) {|x| } }
|
2008-01-29 11:26:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_each
|
|
|
|
a = []
|
|
|
|
(0..10).each {|x| a << x }
|
|
|
|
assert_equal([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], a)
|
|
|
|
|
|
|
|
o1 = Object.new
|
|
|
|
o2 = Object.new
|
2010-01-26 07:34:51 -05:00
|
|
|
def o1.setcmp(v) @cmpresult = v end
|
|
|
|
o1.setcmp(-1)
|
|
|
|
def o1.<=>(x); @cmpresult; end
|
|
|
|
def o2.setcmp(v) @cmpresult = v end
|
|
|
|
o2.setcmp(0)
|
|
|
|
def o2.<=>(x); @cmpresult; end
|
2008-01-29 11:26:07 -05:00
|
|
|
class << o1; self; end.class_eval do
|
|
|
|
define_method(:succ) { o2 }
|
|
|
|
end
|
|
|
|
|
|
|
|
r1 = (o1..o2)
|
|
|
|
r2 = (o1...o2)
|
|
|
|
|
|
|
|
a = []
|
|
|
|
r1.each {|x| a << x }
|
|
|
|
assert_equal([o1, o2], a)
|
|
|
|
|
|
|
|
a = []
|
|
|
|
r2.each {|x| a << x }
|
|
|
|
assert_equal([o1], a)
|
|
|
|
|
2010-01-26 07:34:51 -05:00
|
|
|
o2.setcmp(1)
|
2008-01-29 11:26:07 -05:00
|
|
|
|
|
|
|
a = []
|
|
|
|
r1.each {|x| a << x }
|
|
|
|
assert_equal([o1], a)
|
|
|
|
|
2010-01-26 07:34:51 -05:00
|
|
|
o2.setcmp(nil)
|
2008-01-29 11:26:07 -05:00
|
|
|
|
|
|
|
a = []
|
|
|
|
r1.each {|x| a << x }
|
|
|
|
assert_equal([o1], a)
|
|
|
|
|
2010-01-26 07:34:51 -05:00
|
|
|
o1.setcmp(nil)
|
2008-01-29 11:26:07 -05:00
|
|
|
|
|
|
|
a = []
|
|
|
|
r2.each {|x| a << x }
|
|
|
|
assert_equal([], a)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_begin_end
|
|
|
|
assert_equal(0, (0..1).begin)
|
|
|
|
assert_equal(1, (0..1).end)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_first_last
|
|
|
|
assert_equal([0, 1, 2], (0..10).first(3))
|
|
|
|
assert_equal([8, 9, 10], (0..10).last(3))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_to_s
|
|
|
|
assert_equal("0..1", (0..1).to_s)
|
|
|
|
assert_equal("0...1", (0...1).to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_inspect
|
|
|
|
assert_equal("0..1", (0..1).inspect)
|
|
|
|
assert_equal("0...1", (0...1).inspect)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_eqq
|
|
|
|
assert((0..10) === 5)
|
|
|
|
assert(!((0..10) === 11))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_include
|
|
|
|
assert(("a".."z").include?("c"))
|
|
|
|
assert(!(("a".."z").include?("5")))
|
|
|
|
assert(("a"..."z").include?("y"))
|
|
|
|
assert(!(("a"..."z").include?("z")))
|
|
|
|
assert(!(("a".."z").include?("cc")))
|
|
|
|
assert((0...10).include?(5))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_cover
|
|
|
|
assert(("a".."z").cover?("c"))
|
|
|
|
assert(!(("a".."z").cover?("5")))
|
|
|
|
assert(("a"..."z").cover?("y"))
|
|
|
|
assert(!(("a"..."z").cover?("z")))
|
|
|
|
assert(("a".."z").cover?("cc"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_beg_len
|
|
|
|
o = Object.new
|
|
|
|
assert_raise(TypeError) { [][o] }
|
2010-01-26 07:34:51 -05:00
|
|
|
class << o; attr_accessor :begin end
|
|
|
|
o.begin = -10
|
2008-01-29 11:26:07 -05:00
|
|
|
assert_raise(TypeError) { [][o] }
|
2010-01-26 07:34:51 -05:00
|
|
|
class << o; attr_accessor :end end
|
|
|
|
o.end = 0
|
2008-01-29 11:26:07 -05:00
|
|
|
assert_raise(NoMethodError) { [][o] }
|
2010-01-26 07:34:51 -05:00
|
|
|
def o.exclude_end=(v) @exclude_end = v end
|
|
|
|
def o.exclude_end?() @exclude_end end
|
|
|
|
o.exclude_end = false
|
2008-01-29 11:26:07 -05:00
|
|
|
assert_nil([0][o])
|
|
|
|
assert_raise(RangeError) { [0][o] = 1 }
|
2010-01-26 07:34:51 -05:00
|
|
|
o.begin = 10
|
|
|
|
o.end = 10
|
2008-01-29 11:26:07 -05:00
|
|
|
assert_nil([0][o])
|
2010-01-26 07:34:51 -05:00
|
|
|
o.begin = 0
|
2008-01-29 11:26:07 -05:00
|
|
|
assert_equal([0], [0][o])
|
2010-01-26 07:34:51 -05:00
|
|
|
o.begin = 2
|
|
|
|
o.end = 0
|
2008-01-29 11:26:07 -05:00
|
|
|
assert_equal([], [0, 1, 2][o])
|
|
|
|
end
|
2008-12-02 12:53:48 -05:00
|
|
|
|
|
|
|
class CyclicRange < Range
|
|
|
|
def <=>(other); true; end
|
|
|
|
end
|
|
|
|
def test_cyclic_range_inspect
|
|
|
|
o = CyclicRange.allocate
|
|
|
|
o.instance_eval { initialize(o, 1) }
|
|
|
|
assert_equal("(... .. ...)..1", o.inspect)
|
|
|
|
end
|
2009-11-26 07:17:47 -05:00
|
|
|
|
|
|
|
def test_comparison_when_recursive
|
|
|
|
x = CyclicRange.allocate; x.send(:initialize, x, 1)
|
|
|
|
y = CyclicRange.allocate; y.send(:initialize, y, 1)
|
|
|
|
Timeout.timeout(1) {
|
|
|
|
assert x == y
|
|
|
|
assert x.eql? y
|
|
|
|
}
|
|
|
|
|
|
|
|
z = CyclicRange.allocate; z.send(:initialize, z, :another)
|
|
|
|
Timeout.timeout(1) {
|
|
|
|
assert x != z
|
|
|
|
assert !x.eql?(z)
|
|
|
|
}
|
|
|
|
|
|
|
|
x = CyclicRange.allocate
|
|
|
|
y = CyclicRange.allocate
|
|
|
|
x.send(:initialize, y, 1)
|
|
|
|
y.send(:initialize, x, 1)
|
|
|
|
Timeout.timeout(1) {
|
|
|
|
assert x == y
|
|
|
|
assert x.eql?(y)
|
|
|
|
}
|
|
|
|
|
|
|
|
x = CyclicRange.allocate
|
|
|
|
z = CyclicRange.allocate
|
|
|
|
x.send(:initialize, z, 1)
|
|
|
|
z.send(:initialize, x, :other)
|
|
|
|
Timeout.timeout(1) {
|
|
|
|
assert x != z
|
|
|
|
assert !x.eql?(z)
|
|
|
|
}
|
|
|
|
end
|
2003-10-16 22:18:01 -04:00
|
|
|
end
|