/
testRange.rb
84 lines (68 loc) · 1.92 KB
/
testRange.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
require 'test/minirunit'
test_check "Test Range"
values = []
(5..10).each do |i| values.push i end
test_equal([5,6,7,8,9,10], values)
values = []
for i in 1 .. 3
values << i
end
test_equal([1, 2, 3], values)
# Parser once choked on the range below
def testBreakWithInterval
break unless 1..2
end
test_equal(1, (1..4).begin)
test_equal(4, (1..4).end)
test_equal(4.8, (4.8..7.2).begin)
test_equal(7.2, (4.8..7.2).end)
test_ok((1..4).include?(2.2))
test_ok((4.8..7.2).include?(5.5))
test_ok(!(4.8..7.2).include?(4))
test_ok(!(4.8..7.2).include?(7.3))
test_equal(false, (1..2).include?(nil));
test_equal(false, (1..2) === nil);
# member? / include ? / ===
def test_member(expect, recv, arg)
for method in ["member?", "include?", "==="]
test_equal(expect, recv.send(method, arg))
end
end
az_incl = 'aa'..'az'
az_excl = 'aa'...'az'
test_member(true, az_incl, 'az')
test_member(false, az_excl, 'az')
test_member(true, az_incl, 'ak')
test_member(true, az_excl, 'ak')
test_member(false, az_incl, 'bb')
test_member(false, az_excl, 'bb')
##### step #####
test_exception(ArgumentError) { (1..2).step(-1) }
r = Range.new('a','d',false)
sum = 0
r.step(3) { sum += 1}
test_equal(2,sum)
r = Range.new('a','d',false)
sum = 0
r.step(27) { sum += 1}
test_equal(1,sum)
# exclusive tests
r = Range.new('A', 'J', false)
sum = 0
r.each {|x| sum += 1}
test_equal(10, sum)
r = Range.new('A', 'J', true)
sum = 0
r.each {|x| sum += 1}
test_equal(9, sum)
test_equal(['A','B','C'],Array[*('A'..'C')])
# JRUBY-2311
test_exception(TypeError) { (1..10).step(nil) }
test_exception(TypeError) { (1..10).step("a") }
test_exception(TypeError) { (1..10).step(Object.new) }
# Float ranges. We currently follow MRI 1.8.6 pl111 behavior.
# Newer changes in MRI trunk introduced different behavior.
# See: http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/15990
res = []
(1.0..10.0).step(1.5){ |x| res << x }
test_equal([1.0, 2.5, 4.0, 5.5, 7.0, 8.5, 10.0], res)