2003-10-16 22:18:01 -04:00
|
|
|
require 'test/unit'
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
2003-10-16 22:18:01 -04:00
|
|
|
end
|