Debian Codegraph @

vendor/mruby-source/mrbgems/mruby-range-ext/test/range.rb

raw
1 ##
2 # Range(Ext) Test
3
4 assert('Range#cover?') do
5 assert_true ("a".."z").cover?("c")
6 assert_false ("a".."z").cover?("5")
7 assert_true ("a".."z").cover?("cc")
8 assert_false ("a".."z").cover?(nil)
9 assert_true ("a"..).cover?("c")
10 assert_false ("a"..).cover?("5")
11 assert_true ("a"..).cover?("cc")
12 assert_true (.."z").cover?("a")
13 assert_false (..."z").cover?("z")
14 assert_true (.."z").cover?("z")
15 assert_true (nil..nil).cover?(nil)
16
17 assert_true ("a".."c").cover?("b".."d")
18 assert_true ("a"..).cover?("b"..)
19 assert_false ("a"..).cover?(1..)
20 assert_false ("d"..).cover?(.."b")
21 assert_true (.."c").cover?("b".."d")
22 assert_true (.."c").cover?(.."d")
23 assert_false (.."c").cover?(..2)
24 assert_false (.."c").cover?("d"..)
25 end
26
27 assert('Range#first') do
28 assert_equal 10, (10..20).first
29 assert_equal [10, 11, 12], (10..20).first(3)
30 assert_equal 10, (10..).first
31 assert_equal [10, 11, 12], (10..).first(3)
32 assert_raise(RangeError) { (..1).first }
33
34 skip unless Object.const_defined?(:Float)
35 assert_equal [0, 1, 2], (0..Float::INFINITY).first(3)
36 end
37
38 assert('Range#last') do
39 assert_equal 20, (10..20).last
40 assert_equal 20, (10...20).last
41 assert_raise(RangeError) { (10..).last }
42 assert_raise(RangeError) { (10...).last }
43 assert_equal [18, 19, 20], (10..20).last(3)
44 assert_equal [17, 18, 19], (10...20).last(3)
45 end
46
47 assert('Range#size') do
48 assert_equal 42, (1..42).size
49 assert_equal 41, (1...42).size
50 assert_nil ('a'..'z').size
51 assert_nil ('a'..).size
52
53 assert_nil (1..).size unless Object.const_defined?(:Float)
54
55 skip unless Object.const_defined?(:Float)
56 assert_equal 6, (1...6.3).size
57 assert_equal 5, (1...6.0).size
58 assert_equal 5, (1.1...6).size
59 assert_equal 15, (1.0..15.9).size
60 assert_equal Float::INFINITY, (0..Float::INFINITY).size
61
62 assert_equal Float::INFINITY, (1..).size
63 assert_equal Float::INFINITY, (1...).size
64 assert_equal Float::INFINITY, (1.0..).size
65 end
66
67 assert('Range#max') do
68 # returns the maximum value in the range when called with no arguments
69 assert_equal 10, (1..10).max
70 assert_equal 9, (1...10).max
71 assert_equal 536870911, (0...2**29).max
72
73 # returns nil when the endpoint is less than the start point
74 assert_equal nil, (100..10).max
75
76 # returns nil when the endpoint equals the start point and the range is exclusive
77 assert_equal nil, (5...5).max
78
79 # returns the endpoint when the endpoint equals the start point and the range is inclusive
80 assert_equal 5, (5..5).max
81
82 # raises RangeError when called on an endless range
83 assert_raise(RangeError) { (10..).max }
84 assert_raise(RangeError) { (10...).max }
85
86 skip unless Object.const_defined?(:Float)
87
88 # returns the maximum value in the Float range when called with no arguments
89 assert_equal 908.1111, (303.20..908.1111).max
90
91 # raises TypeError when called on an exclusive range and a non Integer value
92 assert_raise(TypeError) { (303.20...908.1111).max }
93
94 # returns nil when the endpoint is less than the start point in a Float range
95 assert_equal nil, (3003.20..908.1111).max
96 end
97
98 assert('Range#max given a block') do
99 # passes each pair of values in the range to the block
100 acc = []
101 (1..10).max do |a, b|
102 acc << a
103 acc << b
104 a
105 end
106 (1..10).each do |value|
107 assert_true acc.include?(value)
108 end
109
110 # passes each pair of elements to the block in reversed order
111 acc = []
112 (1..5).max do |a, b|
113 acc << [a, b]
114 a
115 end
116 assert_equal [[2, 1], [3, 2], [4, 3], [5, 4]], acc
117
118 # returns the element the block determines to be the maximum
119 assert_equal 1, ((1..3).max { |_a, _b| -3 })
120
121 # returns nil when the endpoint is less than the start point
122 assert_equal nil, ((100..10).max { |x, y| x <=> y })
123 assert_equal nil, ((5...5).max { |x, y| x <=> y })
124 end
125
126 assert('Range#min') do
127 # returns the minimum value in the range when called with no arguments
128 assert_equal 1, (1..10).min
129 assert_equal 1, (1...10).min
130 assert_equal 1, (1..).min
131
132 # returns nil when the start point is greater than the endpoint
133 assert_equal nil, (100..10).min
134
135 # returns nil when the endpoint equals the start point and the range is exclusive
136 assert_equal nil, (5...5).min
137
138 # returns the endpoint when the endpoint equals the start point and the range is inclusive
139 assert_equal 5, (5..5).min
140
141 skip unless Object.const_defined?(:Float)
142
143 # returns the minimum value in the Float range when called with no arguments
144 assert_equal 303.20, (303.20..908.1111).min
145 assert_equal 1, (1.0..).min
146
147 # returns nil when the start point is greater than the endpoint in a Float range
148 assert_equal nil, (3003.20..908.1111).min
149 end
150
151 assert('Range#min given a block') do
152 # raise when called with a block in endless range
153 assert_raise(RangeError) { (1..).min{} }
154
155 # passes each pair of values in the range to the block
156 acc = []
157 (1..10).min do |a, b|
158 acc << a
159 acc << b
160 a
161 end
162 (1..10).each do |value|
163 assert_true acc.include?(value)
164 end
165
166 # passes each pair of elements to the block in reversed order
167 acc = []
168 (1..5).min do |a, b|
169 acc << [a, b]
170 a
171 end
172 assert_equal [[2, 1], [3, 1], [4, 1], [5, 1]], acc
173
174 # returns the element the block determines to be the minimum
175 assert_equal 3, ((1..3).min { |_a, _b| -3 })
176
177 # returns nil when the start point is greater than the endpoint
178 assert_equal nil, ((100..10).min { |x, y| x <=> y })
179 assert_equal nil, ((5...5).min { |x, y| x <=> y })
180 end
181
182 assert('Range#overlap?') do
183 assert_false((0..2).overlap?(-2..-1))
184 assert_false((0..2).overlap?(-2...0))
185 assert_true((0..2).overlap?(-1..0))
186 assert_true((0..2).overlap?(1..2))
187 assert_true((0..2).overlap?(2..3))
188 assert_false((0..2).overlap?(3...4))
189 assert_false((0...2).overlap?(2..3))
190
191 assert_true((..0).overlap?(-1..0))
192 assert_true((...0).overlap?(-1..0))
193 assert_true((..0).overlap?(0..1))
194 assert_true((..0).overlap?(..1))
195 assert_false((..0).overlap?(1..2))
196 assert_false((...0).overlap?(0..1))
197
198 assert_false((0..).overlap?(-2..-1))
199 assert_false((0..).overlap?(...0))
200 assert_true((0..).overlap?(..0))
201 assert_true((0..).overlap?(0..1))
202 assert_true((0..).overlap?(1..2))
203 assert_true((0..).overlap?(-1..0))
204 assert_true((0..).overlap?(1..))
205
206 assert_true((0..).overlap?(-1..0))
207 assert_true((0..).overlap?(..0))
208 assert_true((0..).overlap?(0..1))
209 assert_true((0..).overlap?(1..2))
210 assert_true((0..).overlap?(1..))
211
212 assert_raise(TypeError) { (0..).overlap?(1) }
213 assert_raise(TypeError) { (0..).overlap?(nil) }
214 end