1 | ##
|
---|
2 | # Range(Ext) Test
|
---|
3 |
|
---|
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")
|
---|
8 | end
|
---|
9 |
|
---|
10 | assert('Range#first') do
|
---|
11 | assert_equal 10, (10..20).first
|
---|
12 | assert_equal [10, 11, 12], (10..20).first(3)
|
---|
13 |
|
---|
14 | skip unless Object.const_defined?(:Float)
|
---|
15 | assert_equal [0, 1, 2], (0..Float::INFINITY).first(3)
|
---|
16 | end
|
---|
17 |
|
---|
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)
|
---|
23 | end
|
---|
24 |
|
---|
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
|
---|
29 |
|
---|
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
|
---|
36 | end
|
---|
37 |
|
---|
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
|
---|
43 |
|
---|
44 | # returns nil when the endpoint is less than the start point
|
---|
45 | assert_equal nil, (100..10).max
|
---|
46 |
|
---|
47 | # returns nil when the endpoint equals the start point and the range is exclusive
|
---|
48 | assert_equal nil, (5...5).max
|
---|
49 |
|
---|
50 | # returns the endpoint when the endpoint equals the start point and the range is inclusive
|
---|
51 | assert_equal 5, (5..5).max
|
---|
52 |
|
---|
53 | skip unless Object.const_defined?(:Float)
|
---|
54 |
|
---|
55 | # returns the maximum value in the Float range when called with no arguments
|
---|
56 | assert_equal 908.1111, (303.20..908.1111).max
|
---|
57 |
|
---|
58 | # raises TypeError when called on an exclusive range and a non Integer value
|
---|
59 | assert_raise(TypeError) { (303.20...908.1111).max }
|
---|
60 |
|
---|
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
|
---|
63 | end
|
---|
64 |
|
---|
65 | assert('Range#max given a block') do
|
---|
66 | # passes each pair of values in the range to the block
|
---|
67 | acc = []
|
---|
68 | (1..10).max do |a, b|
|
---|
69 | acc << a
|
---|
70 | acc << b
|
---|
71 | a
|
---|
72 | end
|
---|
73 | (1..10).each do |value|
|
---|
74 | assert_true acc.include?(value)
|
---|
75 | end
|
---|
76 |
|
---|
77 | # passes each pair of elements to the block in reversed order
|
---|
78 | acc = []
|
---|
79 | (1..5).max do |a, b|
|
---|
80 | acc << [a, b]
|
---|
81 | a
|
---|
82 | end
|
---|
83 | assert_equal [[2, 1], [3, 2], [4, 3], [5, 4]], acc
|
---|
84 |
|
---|
85 | # returns the element the block determines to be the maximum
|
---|
86 | assert_equal 1, ((1..3).max { |_a, _b| -3 })
|
---|
87 |
|
---|
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 })
|
---|
91 | end
|
---|
92 |
|
---|
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
|
---|
97 |
|
---|
98 | # returns nil when the start point is greater than the endpoint
|
---|
99 | assert_equal nil, (100..10).min
|
---|
100 |
|
---|
101 | # returns nil when the endpoint equals the start point and the range is exclusive
|
---|
102 | assert_equal nil, (5...5).max
|
---|
103 |
|
---|
104 | # returns the endpoint when the endpoint equals the start point and the range is inclusive
|
---|
105 | assert_equal 5, (5..5).max
|
---|
106 |
|
---|
107 | skip unless Object.const_defined?(:Float)
|
---|
108 |
|
---|
109 | # returns the minimum value in the Float range when called with no arguments
|
---|
110 | assert_equal 303.20, (303.20..908.1111).min
|
---|
111 |
|
---|
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
|
---|
114 | end
|
---|
115 |
|
---|
116 | assert('Range#min given a block') do
|
---|
117 | # passes each pair of values in the range to the block
|
---|
118 | acc = []
|
---|
119 | (1..10).min do |a, b|
|
---|
120 | acc << a
|
---|
121 | acc << b
|
---|
122 | a
|
---|
123 | end
|
---|
124 | (1..10).each do |value|
|
---|
125 | assert_true acc.include?(value)
|
---|
126 | end
|
---|
127 |
|
---|
128 | # passes each pair of elements to the block in reversed order
|
---|
129 | acc = []
|
---|
130 | (1..5).min do |a, b|
|
---|
131 | acc << [a, b]
|
---|
132 | a
|
---|
133 | end
|
---|
134 | assert_equal [[2, 1], [3, 1], [4, 1], [5, 1]], acc
|
---|
135 |
|
---|
136 | # returns the element the block determines to be the minimum
|
---|
137 | assert_equal 3, ((1..3).min { |_a, _b| -3 })
|
---|
138 |
|
---|
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 })
|
---|
142 | end
|
---|