4 assert('Range#cover?') do
5 assert_true ("a".."z").cover?("c")
6 assert_true !("a".."z").cover?("5")
7 assert_true ("a".."z").cover?("cc")
10 assert('Range#first') do
11 assert_equal 10, (10..20).first
12 assert_equal [10, 11, 12], (10..20).first(3)
14 skip unless Object.const_defined?(:Float)
15 assert_equal [0, 1, 2], (0..Float::INFINITY).first(3)
18 assert('Range#last') do
19 assert_equal 20, (10..20).last
20 assert_equal 20, (10...20).last
21 assert_equal [18, 19, 20], (10..20).last(3)
22 assert_equal [17, 18, 19], (10...20).last(3)
25 assert('Range#size') do
26 assert_equal 42, (1..42).size
27 assert_equal 41, (1...42).size
28 assert_nil ('a'..'z').size
30 skip unless Object.const_defined?(:Float)
31 assert_equal 6, (1...6.3).size
32 assert_equal 5, (1...6.0).size
33 assert_equal 5, (1.1...6).size
34 assert_equal 15, (1.0..15.9).size
35 assert_equal Float::INFINITY, (0..Float::INFINITY).size
38 assert('Range#max') do
39 # returns the maximum value in the range when called with no arguments
40 assert_equal 10, (1..10).max
41 assert_equal 9, (1...10).max
42 assert_equal 536870911, (0...2**29).max
44 # returns nil when the endpoint is less than the start point
45 assert_equal nil, (100..10).max
47 # returns nil when the endpoint equals the start point and the range is exclusive
48 assert_equal nil, (5...5).max
50 # returns the endpoint when the endpoint equals the start point and the range is inclusive
51 assert_equal 5, (5..5).max
53 skip unless Object.const_defined?(:Float)
55 # returns the maximum value in the Float range when called with no arguments
56 assert_equal 908.1111, (303.20..908.1111).max
58 # raises TypeError when called on an exclusive range and a non Integer value
59 assert_raise(TypeError) { (303.20...908.1111).max }
61 # returns nil when the endpoint is less than the start point in a Float range
62 assert_equal nil, (3003.20..908.1111).max
65 assert('Range#max given a block') do
66 # passes each pair of values in the range to the block
73 (1..10).each do |value|
74 assert_true acc.include?(value)
77 # passes each pair of elements to the block in reversed order
83 assert_equal [[2, 1], [3, 2], [4, 3], [5, 4]], acc
85 # returns the element the block determines to be the maximum
86 assert_equal 1, ((1..3).max { |_a, _b| -3 })
88 # returns nil when the endpoint is less than the start point
89 assert_equal nil, ((100..10).max { |x, y| x <=> y })
90 assert_equal nil, ((5...5).max { |x, y| x <=> y })
93 assert('Range#min') do
94 # returns the minimum value in the range when called with no arguments
95 assert_equal 1, (1..10).min
96 assert_equal 1, (1...10).min
98 # returns nil when the start point is greater than the endpoint
99 assert_equal nil, (100..10).min
101 # returns nil when the endpoint equals the start point and the range is exclusive
102 assert_equal nil, (5...5).max
104 # returns the endpoint when the endpoint equals the start point and the range is inclusive
105 assert_equal 5, (5..5).max
107 skip unless Object.const_defined?(:Float)
109 # returns the minimum value in the Float range when called with no arguments
110 assert_equal 303.20, (303.20..908.1111).min
112 # returns nil when the start point is greater than the endpoint in a Float range
113 assert_equal nil, (3003.20..908.1111).max
116 assert('Range#min given a block') do
117 # passes each pair of values in the range to the block
119 (1..10).min do |a, b|
124 (1..10).each do |value|
125 assert_true acc.include?(value)
128 # passes each pair of elements to the block in reversed order
134 assert_equal [[2, 1], [3, 1], [4, 1], [5, 1]], acc
136 # returns the element the block determines to be the minimum
137 assert_equal 3, ((1..3).min { |_a, _b| -3 })
139 # returns nil when the start point is greater than the endpoint
140 assert_equal nil, ((100..10).min { |x, y| x <=> y })
141 assert_equal nil, ((5...5).min { |x, y| x <=> y })