File: range.rb

package info (click to toggle)
mruby 3.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,584 kB
  • sloc: ansic: 51,933; ruby: 29,510; yacc: 7,077; cpp: 517; makefile: 51; sh: 42
file content (211 lines) | stat: -rw-r--r-- 6,366 bytes parent folder | download
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
##
# Range(Ext) Test

assert('Range#cover?') do
  assert_true ("a".."z").cover?("c")
  assert_false ("a".."z").cover?("5")
  assert_true ("a".."z").cover?("cc")
  assert_false ("a".."z").cover?(nil)
  assert_true ("a"..).cover?("c")
  assert_false ("a"..).cover?("5")
  assert_true ("a"..).cover?("cc")
  assert_true (.."z").cover?("a")
  assert_false (..."z").cover?("z")
  assert_true (.."z").cover?("z")
  assert_true (nil..nil).cover?(nil)

  assert_true ("a".."c").cover?("b".."d")
  assert_true ("a"..).cover?("b"..)
  assert_false ("a"..).cover?(1..)
  assert_false ("d"..).cover?(.."b")
  assert_true (.."c").cover?("b".."d")
  assert_true (.."c").cover?(.."d")
  assert_false (.."c").cover?(..2)
  assert_false (.."c").cover?("d"..)
end

assert('Range#first') do
  assert_equal 10, (10..20).first
  assert_equal [10, 11, 12], (10..20).first(3)
  assert_equal 10, (10..).first
  assert_equal [10, 11, 12], (10..).first(3)
  assert_raise(RangeError) { (..1).first }

  skip unless Object.const_defined?(:Float)
  assert_equal [0, 1, 2], (0..Float::INFINITY).first(3)
end

assert('Range#last') do
  assert_equal 20, (10..20).last
  assert_equal 20, (10...20).last
  assert_raise(RangeError) { (10..).last }
  assert_raise(RangeError) { (10...).last }
  assert_equal [18, 19, 20], (10..20).last(3)
  assert_equal [17, 18, 19], (10...20).last(3)
end

assert('Range#size') do
  assert_equal 42, (1..42).size
  assert_equal 41, (1...42).size
  assert_nil ('a'..'z').size
  assert_nil ('a'..).size

  assert_nil (1..).size unless Object.const_defined?(:Float)

  skip unless Object.const_defined?(:Float)
  assert_equal 6, (1...6.3).size
  assert_equal 5, (1...6.0).size
  assert_equal Float::INFINITY, (0..Float::INFINITY).size

  assert_equal Float::INFINITY, (1..).size
  assert_equal Float::INFINITY, (1...).size
end

assert('Range#max') do
  # returns the maximum value in the range when called with no arguments
  assert_equal 10, (1..10).max
  assert_equal 9, (1...10).max
  assert_equal 536870911, (0...2**29).max

  # returns nil when the endpoint is less than the start point
  assert_equal nil, (100..10).max

  # returns nil when the endpoint equals the start point and the range is exclusive
  assert_equal nil, (5...5).max

  # returns the endpoint when the endpoint equals the start point and the range is inclusive
  assert_equal 5, (5..5).max

  # raises RangeError when called on an endless range
  assert_raise(RangeError) { (10..).max }
  assert_raise(RangeError) { (10...).max }

  skip unless Object.const_defined?(:Float)

  # returns the maximum value in the Float range when called with no arguments
  assert_equal 908.1111, (303.20..908.1111).max

  # raises TypeError when called on an exclusive range and a non Integer value
  assert_raise(TypeError) { (303.20...908.1111).max }

  # returns nil when the endpoint is less than the start point in a Float range
  assert_equal nil, (3003.20..908.1111).max
end

assert('Range#max given a block') do
  # passes each pair of values in the range to the block
  acc = []
  (1..10).max do |a, b|
    acc << a
    acc << b
    a
  end
  (1..10).each do |value|
    assert_true acc.include?(value)
  end

  # passes each pair of elements to the block in reversed order
  acc = []
  (1..5).max do |a, b|
    acc << [a, b]
    a
  end
  assert_equal [[2, 1], [3, 2], [4, 3], [5, 4]], acc

  # returns the element the block determines to be the maximum
  assert_equal 1, ((1..3).max { |_a, _b| -3 })

  # returns nil when the endpoint is less than the start point
  assert_equal nil, ((100..10).max { |x, y| x <=> y })
  assert_equal nil, ((5...5).max { |x, y| x <=> y })
end

assert('Range#min') do
  # returns the minimum value in the range when called with no arguments
  assert_equal 1, (1..10).min
  assert_equal 1, (1...10).min
  assert_equal 1, (1..).min

  # returns nil when the start point is greater than the endpoint
  assert_equal nil, (100..10).min

  # returns nil when the endpoint equals the start point and the range is exclusive
  assert_equal nil, (5...5).min

  # returns the endpoint when the endpoint equals the start point and the range is inclusive
  assert_equal 5, (5..5).min

  skip unless Object.const_defined?(:Float)

  # returns the minimum value in the Float range when called with no arguments
  assert_equal 303.20, (303.20..908.1111).min
  assert_equal 1, (1.0..).min

  # returns nil when the start point is greater than the endpoint in a Float range
  assert_equal nil, (3003.20..908.1111).min
end

assert('Range#min given a block') do
  # raise when called with a block in endless range
  assert_raise(RangeError) { (1..).min{} }

  # passes each pair of values in the range to the block
  acc = []
  (1..10).min do |a, b|
    acc << a
    acc << b
    a
  end
  (1..10).each do |value|
    assert_true acc.include?(value)
  end

  # passes each pair of elements to the block in reversed order
  acc = []
  (1..5).min do |a, b|
    acc << [a, b]
    a
  end
  assert_equal [[2, 1], [3, 1], [4, 1], [5, 1]], acc

  # returns the element the block determines to be the minimum
  assert_equal 3, ((1..3).min { |_a, _b| -3 })

  # returns nil when the start point is greater than the endpoint
  assert_equal nil, ((100..10).min { |x, y| x <=> y })
  assert_equal nil, ((5...5).min { |x, y| x <=> y })
end

assert('Range#overlap?') do
  assert_false((0..2).overlap?(-2..-1))
  assert_false((0..2).overlap?(-2...0))
  assert_true((0..2).overlap?(-1..0))
  assert_true((0..2).overlap?(1..2))
  assert_true((0..2).overlap?(2..3))
  assert_false((0..2).overlap?(3...4))
  assert_false((0...2).overlap?(2..3))

  assert_true((..0).overlap?(-1..0))
  assert_true((...0).overlap?(-1..0))
  assert_true((..0).overlap?(0..1))
  assert_true((..0).overlap?(..1))
  assert_false((..0).overlap?(1..2))
  assert_false((...0).overlap?(0..1))

  assert_false((0..).overlap?(-2..-1))
  assert_false((0..).overlap?(...0))
  assert_true((0..).overlap?(..0))
  assert_true((0..).overlap?(0..1))
  assert_true((0..).overlap?(1..2))
  assert_true((0..).overlap?(-1..0))
  assert_true((0..).overlap?(1..))

  assert_true((0..).overlap?(-1..0))
  assert_true((0..).overlap?(..0))
  assert_true((0..).overlap?(0..1))
  assert_true((0..).overlap?(1..2))
  assert_true((0..).overlap?(1..))

  assert_raise(TypeError) { (0..).overlap?(1) }
  assert_raise(TypeError) { (0..).overlap?(nil) }
end