range_extd 1.1.1 → 2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/ChangeLog +16 -0
- data/Makefile +7 -3
- data/News +4 -0
- data/README.en.rdoc +1146 -270
- data/README.ja.rdoc +1146 -270
- data/Rakefile +11 -5
- data/lib/range_extd/infinity.rb +426 -0
- data/lib/range_extd/load_all.rb +19 -0
- data/lib/range_extd/nil_class.rb +41 -0
- data/lib/range_extd/nowhere.rb +135 -0
- data/lib/range_extd/numeric.rb +160 -0
- data/lib/range_extd/object.rb +53 -0
- data/lib/range_extd/range.rb +401 -0
- data/lib/{range_extd/range_extd.rb → range_extd.rb} +387 -633
- data/range_extd.gemspec +50 -0
- data/test/all_required_test.rb +173 -0
- data/test/test_range_extd.rb +482 -148
- data/test/test_range_extd_nowhere.rb +84 -0
- metadata +29 -16
- data/lib/range_extd/infinity/infinity.rb +0 -600
data/test/test_range_extd.rb
CHANGED
@@ -2,46 +2,23 @@
|
|
2
2
|
|
3
3
|
$stdout.sync=true
|
4
4
|
$stderr.sync=true
|
5
|
+
|
5
6
|
# print '$LOAD_PATH=';p $LOAD_PATH
|
6
|
-
arlibrelpath = []
|
7
7
|
arlibbase = %w(range_extd range_extd/infinity) # range_extd/infinity is actually loaded from range_extd. But by writing here, the absolute path will be displayed.
|
8
8
|
|
9
|
-
arlibbase.
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
(['../lib/', 'lib/', ''].map{|i| i+elibbase+'/'} + ['']).each do |dir|
|
14
|
-
# eg., pathcand = %w(../lib/rangesmaller/ lib/rangesmaller/ rangesmaller/) + ['']
|
15
|
-
begin
|
16
|
-
s = dir+File.basename(elibbase)
|
17
|
-
arAllPaths.push(s)
|
18
|
-
#print "Trying: "; puts s
|
19
|
-
require s
|
20
|
-
pathnow = s
|
21
|
-
break
|
22
|
-
rescue LoadError => er
|
23
|
-
end
|
24
|
-
end # (['../lib/', 'lib/', ''].map{|i| i+elibbase+'/'} + '').each do |dir|
|
25
|
-
|
26
|
-
if pathnow.nil?
|
27
|
-
warn "Warning: All the attempts to load the following files have failed. Abort..."
|
28
|
-
warn arAllPaths.inspect
|
29
|
-
warn " NOTE: It may be because a require statement in that file failed,
|
30
|
-
rather than requiring the file itself.
|
31
|
-
Check with % ruby -r#{File.basename(elibbase)} -e p
|
32
|
-
or maybe add env RUBYLIB=$RUBYLIB:`pwd`"
|
33
|
-
# p $LOADED_FEATURES.grep(/#{Regexp.quote(File.basename(elibbase)+'.rb')}$/)
|
34
|
-
raise er
|
35
|
-
else
|
36
|
-
#print pathnow," is loaded!\n"
|
37
|
-
arlibrelpath.push pathnow
|
38
|
-
end
|
9
|
+
arlibrelbase = arlibbase.map{|i| "../lib/"+i}
|
10
|
+
|
11
|
+
arlibrelbase.each do |elibbase|
|
12
|
+
require_relative elibbase
|
39
13
|
end # arlibbase.each do |elibbase|
|
40
14
|
|
41
|
-
print "NOTE:
|
42
|
-
print "NOTE: Library
|
43
|
-
arlibbase.
|
44
|
-
|
15
|
+
print "NOTE: Running: "; p File.basename(__FILE__)
|
16
|
+
print "NOTE: Library relative paths: "; p arlibrelbase
|
17
|
+
arlibbase4full = arlibbase.map{|i| i.sub(%r@^(../)+@, "")}+%w(range_extd)
|
18
|
+
puts "NOTE: Library full paths for #{arlibbase4full.inspect}: "
|
19
|
+
arlibbase4full.each do |elibbase|
|
20
|
+
ar = $LOADED_FEATURES.grep(/(^|\/)#{Regexp.quote(File.basename(elibbase))}(\.rb)?$/).uniq
|
21
|
+
print elibbase+": " if ar.empty?; p ar
|
45
22
|
end
|
46
23
|
|
47
24
|
|
@@ -72,6 +49,8 @@ gem "minitest"
|
|
72
49
|
Xs.new(@length + 1)
|
73
50
|
end
|
74
51
|
def <=>(other)
|
52
|
+
# Apparently, this affects :== as well!!
|
53
|
+
# Therefore, (Xs.new(4) == Object.new) would raise NoMethodError, whereas (Object.new == Xs.new(4)) just returns false.
|
75
54
|
@length <=> other.length
|
76
55
|
end
|
77
56
|
def to_s
|
@@ -145,12 +124,13 @@ gem "minitest"
|
|
145
124
|
RangeExtd(*rest)
|
146
125
|
end
|
147
126
|
|
148
|
-
class TestUnitFoo < MiniTest::
|
127
|
+
class TestUnitFoo < MiniTest::Test
|
149
128
|
T = true
|
150
129
|
F = false
|
151
130
|
InfF = Float::INFINITY
|
152
131
|
InfP = RangeExtd::Infinity::POSITIVE
|
153
132
|
InfN = RangeExtd::Infinity::NEGATIVE
|
133
|
+
NOWHERE = RangeExtd::Nowhere::NOWHERE
|
154
134
|
|
155
135
|
def setup
|
156
136
|
@ib = 1
|
@@ -167,6 +147,10 @@ gem "minitest"
|
|
167
147
|
@foo = nil
|
168
148
|
end
|
169
149
|
|
150
|
+
####################################
|
151
|
+
# Ruby behaviours (to check out)
|
152
|
+
####################################
|
153
|
+
|
170
154
|
def test_object_compare
|
171
155
|
assert_equal 0, (3 <=> 3)
|
172
156
|
assert_equal 1, (4 <=> 3)
|
@@ -176,19 +160,174 @@ gem "minitest"
|
|
176
160
|
assert_equal 0, (IO <=> IO)
|
177
161
|
end # def test_object_compare
|
178
162
|
|
163
|
+
# Ruby-2.7 (and 3.1) default behaviours.
|
164
|
+
def test_ruby27_range01
|
165
|
+
num1 = (5..InfF)
|
166
|
+
num2 = (5..)
|
167
|
+
assert( num1.end != num2.end )
|
168
|
+
assert_equal InfF, num1.size
|
169
|
+
assert_equal InfF, num2.size
|
170
|
+
|
171
|
+
str1 = (?a..)
|
172
|
+
assert( str1.end != num1.end )
|
173
|
+
assert( str1.end == num2.end )
|
174
|
+
assert_nil str1.size, "For some reason, this is nil in Ruby 3.1"
|
175
|
+
assert_equal InfF, (..?z).size
|
176
|
+
end # def test_ruby27_range
|
177
|
+
|
178
|
+
# Ruby-2.7 (and 3.1) default behaviours.
|
179
|
+
def test_ruby27_range_nil
|
180
|
+
assert_equal "..3", (..3).to_s
|
181
|
+
assert_equal "3..", (3..).to_s
|
182
|
+
assert_equal "3..", (3..nil).to_s
|
183
|
+
assert_equal "..3", (nil..3).to_s
|
184
|
+
|
185
|
+
assert( (nil..) == (..nil) ) #
|
186
|
+
assert( (nil..) != (...nil)) # (because (I guess) exclude_end? differ)
|
187
|
+
assert( (...nil).exclude_end? ) # (because (I guess) exclude_end? differ)
|
188
|
+
assert_equal "abcdef", "abcdef"[..nil] # (i.e., it is interpreted as (0..IntegerInfinity)
|
189
|
+
# (n.b., nil.to_i==0; Integer(nil) #=> TypeError))
|
190
|
+
assert_raises(TypeError){ "abcdef"[..?a] } # raise: no implicit conversion of String into Integer (TypeError)
|
191
|
+
assert_equal "abcdef", "abcdef"[0..100]
|
192
|
+
assert_nil "abcdef"[-100..100]
|
193
|
+
|
194
|
+
assert_equal InfF, (..nil).size
|
195
|
+
|
196
|
+
assert_nil (..nil).begin
|
197
|
+
assert_raises(RangeError){ (..nil).first} # raise: cannot get the first element of beginless range (RangeError)
|
198
|
+
assert_raises(RangeError){ (..nil).last } # raise: cannot get the last element of endless range (RangeError)
|
199
|
+
assert_nil (..nil).end
|
200
|
+
|
201
|
+
assert( (..nil).cover? 5 )
|
202
|
+
assert( (..nil).cover? ?a )
|
203
|
+
assert( (..nil).cover? [?a])
|
204
|
+
assert( (..nil).cover? nil )
|
205
|
+
end # def test_ruby27_range_nil
|
206
|
+
|
207
|
+
# Ruby-2.7 (and 3.1) default behaviours.
|
208
|
+
def test_ruby27_range_int
|
209
|
+
num1 = (5..Float::INFINITY)
|
210
|
+
num2 = (5..)
|
211
|
+
assert( num1.end != num2.end) # (because I guess (Float::INFINITY != nil))
|
212
|
+
assert_equal InfF, num1.size
|
213
|
+
assert_equal InfF, num2.size
|
214
|
+
|
215
|
+
assert (3...) == (3...nil)
|
216
|
+
assert (3..) != (3...nil) # (because I guess exclude_end? differ)
|
217
|
+
|
218
|
+
assert_equal InfF, (3..).size
|
219
|
+
assert_nil (..3).begin # => nil
|
220
|
+
assert_raises(RangeError){ (..3).first} # raise: cannot get the first element of beginless range (RangeError)
|
221
|
+
assert_raises(RangeError){ (3..).last } # raise: cannot get the last element of endless range (RangeError)
|
222
|
+
assert_nil (3..).end # => nil
|
223
|
+
assert_raises(TypeError){ (..3).each{}} # raise: `each': can't iterate from NilClass (TypeError)
|
224
|
+
assert_raises(TypeError){ (..3).to_a } # raise: `each': can't iterate from NilClass (TypeError)
|
225
|
+
assert_raises(RangeError){ (3..).to_a } # raise: `to_a': cannot convert endless range to an array (RangeError)
|
226
|
+
# (3..Float::INFINITY).to_a # => Infinite loop!
|
227
|
+
|
228
|
+
assert_equal(-InfF, (-Float::INFINITY..4).first)
|
229
|
+
assert_equal InfF, (4..Float::INFINITY).last
|
230
|
+
assert_raises(TypeError){ (-Float::INFINITY..4).first(2)} # raise: can't iterate from Float (TypeError)
|
231
|
+
# (4..Float::INFINITY).last(2) # Infinite loop!
|
232
|
+
end # def test_ruby27_range_int
|
233
|
+
|
234
|
+
# Ruby-2.7 (and 3.1) default behaviours.
|
235
|
+
def test_ruby27_range_string
|
236
|
+
assert((?a..).end == (5..).end) # (because both are nil)
|
237
|
+
assert((?a..).end != (5..Float::INFINITY).end)
|
238
|
+
assert((..?a).begin == (..5).begin) # (because both are nil)
|
239
|
+
assert((..?a).begin != ((-Float::INFINITY)..5).begin)
|
240
|
+
assert_nil (?a..?b).size # Range#size is nil except for Numeric Ranges (specification).
|
241
|
+
assert_equal InfF, (..?a).size # Therefore, this contradicts the specification.
|
242
|
+
assert_nil (?a..).size
|
243
|
+
|
244
|
+
assert_nil (..?a).begin
|
245
|
+
assert_raises(RangeError){ (..?a).first} # raise: cannot get the first element of beginless range (RangeError)
|
246
|
+
assert_raises(RangeError){ (?a..).last } # raise: cannot get the last element of endless range (RangeError)
|
247
|
+
assert_nil (?a..).end
|
248
|
+
assert_raises(TypeError){ (..?a).each{}} # raise: `each': can't iterate from NilClass (TypeError)
|
249
|
+
assert_raises(TypeError){ (..?a).to_a } # raise: `each': can't iterate from NilClass (TypeError)
|
250
|
+
assert_raises(RangeError){(?a..).to_a } # raise: `to_a': cannot convert endless range to an array (RangeError)
|
251
|
+
assert_raises(ArgumentError){ (?a..Float::INFINITY).to_a } # raise: bad value for range (ArgumentError) # b/c it is not String!
|
252
|
+
end # def test_ruby27_range_string
|
253
|
+
|
254
|
+
####################################
|
255
|
+
# RangeExtd behaviours
|
256
|
+
####################################
|
257
|
+
|
258
|
+
# Tests listed in the doc
|
259
|
+
def test_doc_first
|
260
|
+
assert_equal Float::INFINITY, RaE(..nil).size
|
261
|
+
assert_nil RaE(..nil).begin
|
262
|
+
assert_raises(RangeError){RaE(..nil).first} # raise: cannot get the first element of beginless range (RangeError)
|
263
|
+
assert_raises(RangeError){RaE(..nil).last} # raise: cannot get the last element of endless range (RangeError)
|
264
|
+
assert_nil RaE(..nil).end
|
265
|
+
assert RaE(..nil).cover? 5
|
266
|
+
assert RaE(..nil).cover? ?a
|
267
|
+
assert RaE(..nil).cover? [?a]
|
268
|
+
assert RaE(..nil).cover? nil
|
269
|
+
end
|
270
|
+
|
271
|
+
# Tests listed in the doc
|
272
|
+
def test_doc_integer
|
273
|
+
assert_equal((3...), RaE(3...nil))
|
274
|
+
refute_equal((3..), RaE(3...nil)) # (because exclude_end? differ))
|
275
|
+
assert_equal(RaE(3...nil), (3...))
|
276
|
+
refute_equal(RaE(3...nil), (3..) ) # (because exclude_end? differ))
|
277
|
+
assert_equal Float::INFINITY, RaE(3..).size
|
278
|
+
assert_nil RaE(..3).begin
|
279
|
+
assert_raises(RangeError, "first() should raise RangeError"){RaE(..3).first} # raise: cannot get the first element of beginless range (RangeError)
|
280
|
+
assert_raises(RangeError, "last() should raise RangeError"){RaE(3..).last} # raise: cannot get the last element of endless range (RangeError)
|
281
|
+
assert_nil RaE(3..).end
|
282
|
+
assert_raises(TypeError){RaE(..3).each{}} # raise: `each': can't iterate from NilClass (TypeError)
|
283
|
+
assert_raises(TypeError){RaE(..3).to_a} # raise: `each': can't iterate from NilClass (TypeError)
|
284
|
+
assert_raises(RangeError){RaE(3..).to_a} # raise: `to_a': cannot convert endless range to an array (RangeError)
|
285
|
+
# (3..Float::INFINITY).to_a # Infinite loop!
|
286
|
+
end
|
287
|
+
|
288
|
+
# Tests listed in the doc
|
289
|
+
def test_doc_string
|
290
|
+
assert( (5..).end == RaE(?a..).end) # for direct comparison, assert_nil is required!
|
291
|
+
assert( RaE(5..).end == (?a..).end)
|
292
|
+
assert( RaE(5..).end == RaE(?a..).end)
|
293
|
+
refute_equal( (5..Float::INFINITY).end, RaE(?a..).end)
|
294
|
+
refute_equal(RaE(5..Float::INFINITY).end, (?a..).end)
|
295
|
+
refute_equal(RaE(5..Float::INFINITY).end, RaE(?a..).end)
|
296
|
+
assert( (..5).begin == RaE(..?a).begin) # (because both are nil)
|
297
|
+
assert( RaE(..5).begin == (..?a).begin)
|
298
|
+
assert( RaE(..5).begin == RaE(..?a).begin)
|
299
|
+
refute_equal( ((-Float::INFINITY)..5).begin, RaE(..?a).begin)
|
300
|
+
refute_equal(RaE((-Float::INFINITY)..5).begin, (..?a).begin)
|
301
|
+
refute_equal(RaE((-Float::INFINITY)..5).begin, RaE(..?a).begin)
|
302
|
+
assert_equal Float::INFINITY, RaE(..?a).size
|
303
|
+
assert_nil RaE(?a..).size
|
304
|
+
|
305
|
+
assert_nil RaE(..?a).begin
|
306
|
+
assert_raises(RangeError){RaE(..?a).first} # raise: cannot get the first element of beginless range (RangeError)
|
307
|
+
assert_raises(RangeError){RaE(?a..).last} # raise: cannot get the last element of endless range (RangeError)
|
308
|
+
assert_nil RaE(?a..).end
|
309
|
+
assert_raises(TypeError){RaE(..?a).each{}} # raise: `each': can't iterate from NilClass (TypeError)
|
310
|
+
assert_raises(TypeError){RaE(..?a).to_a} # raise: `each': can't iterate from NilClass (TypeError)
|
311
|
+
assert_raises(RangeError){RaE(?a..).to_a} # raise: `to_a': cannot convert endless range to an array (RangeError)
|
312
|
+
assert_raises(ArgumentError){RaE(?a..Float::INFINITY).to_a} # raise: bad value for range (ArgumentError) # b/c it is not String!
|
313
|
+
end
|
314
|
+
|
315
|
+
# IF and only if "range_extd/numeric" is required,
|
179
316
|
# InfP (RangeExtd::Infinity::POSITIVE) and InfN (RangeExtd::Infinity::NEGATIVE)
|
180
317
|
# are always comparable with any comparable objects except for
|
181
318
|
# Float::INFINITY, in which case ArgumentError is raised.
|
319
|
+
#
|
320
|
+
# In this case, the file is not required.
|
182
321
|
def test_infinity_compare
|
183
322
|
assert_operator 7.7, '<', InfF
|
184
|
-
|
185
|
-
|
323
|
+
assert_raises(ArgumentError){ 7.7 < InfP }
|
324
|
+
assert_raises(ArgumentError){ 7.7 > InfN }
|
186
325
|
assert_operator InfP, '>', 7.7
|
187
326
|
assert_operator InfN, '<', 7.7
|
188
327
|
assert_operator 8, '<', InfF
|
189
|
-
|
328
|
+
assert_raises(ArgumentError){ 8 < InfP }
|
190
329
|
assert_operator Rational(2, 3), '<', InfF
|
191
|
-
|
330
|
+
assert_raises(ArgumentError){ Rational(2, 3) < InfP }
|
192
331
|
assert_operator InfP, '>', Rational(2, 3)
|
193
332
|
assert_operator InfN, '<', Rational(2, 3)
|
194
333
|
assert_operator 'h', '<', InfP
|
@@ -204,12 +343,12 @@ gem "minitest"
|
|
204
343
|
assert_raises(ArgumentError) { InfP < Object.new }
|
205
344
|
assert_nil (InfF <=> InfP)
|
206
345
|
assert_nil (InfP <=> InfF)
|
207
|
-
|
208
|
-
|
346
|
+
assert_nil( 7.7 <=> InfP)
|
347
|
+
assert_nil( 7.7 <=> InfN)
|
209
348
|
assert_equal( 1, InfP <=> 7.7)
|
210
349
|
assert_equal(-1, InfN <=> 7.7)
|
211
|
-
|
212
|
-
|
350
|
+
assert_nil( 5 <=> InfP)
|
351
|
+
assert_nil( 5 <=> InfN)
|
213
352
|
assert_equal( 1, InfP <=> 5)
|
214
353
|
assert_equal(-1, InfN <=> 5)
|
215
354
|
assert_equal(-1, 'h' <=> InfP)
|
@@ -262,11 +401,10 @@ gem "minitest"
|
|
262
401
|
def test_rangeextd_new_infinity_c2
|
263
402
|
c2 = CLC2.new
|
264
403
|
assert_nil (c2 <=> 1) # Object#<=>
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
r=(RangeExtd::Infinity::NEGATIVE..c2)
|
404
|
+
assert_nil(c2 <=> RangeExtd::Infinity::POSITIVE)
|
405
|
+
assert_nil(c2 <=> RangeExtd::Infinity::NEGATIVE)
|
406
|
+
assert_raises(ArgumentError, '"bad value for range" should be raised when object.rb is not required'){ (c2..RangeExtd::Infinity::POSITIVE) }
|
407
|
+
r=(RangeExtd::Infinity::NEGATIVE..c2) # This is OK at least in Ruby-3.1 because (RangeExtd::Infinity::NEGATIVE <=> c2) is valid.
|
270
408
|
assert_equal RangeExtd::Infinity::NEGATIVE, r.begin
|
271
409
|
|
272
410
|
assert_raises(ArgumentError){ (true..RangeExtd::Infinity::POSITIVE) } # => bad value for range
|
@@ -293,12 +431,11 @@ gem "minitest"
|
|
293
431
|
|
294
432
|
def test_rangeextd_new_infinity_c3
|
295
433
|
c3 = CLC3.new
|
296
|
-
|
297
|
-
|
434
|
+
assert_nil(c3 <=> RangeExtd::Infinity::POSITIVE)
|
435
|
+
assert_nil(c3 <=> RangeExtd::Infinity::NEGATIVE)
|
298
436
|
|
299
|
-
|
300
|
-
|
301
|
-
r=(RangeExtd::Infinity::NEGATIVE..c3)
|
437
|
+
assert_raises(ArgumentError, '"bad value for range" should be raised when object.rb is not required'){(c3..RangeExtd::Infinity::POSITIVE) }
|
438
|
+
r=(RangeExtd::Infinity::NEGATIVE..c3) # This is OK at least in Ruby-3.1 because (RangeExtd::Infinity::NEGATIVE <=> c3) is valid.
|
302
439
|
assert_equal RangeExtd::Infinity::NEGATIVE, r.begin
|
303
440
|
end # def test_rangeextd_new_infinity_c3
|
304
441
|
|
@@ -311,7 +448,7 @@ gem "minitest"
|
|
311
448
|
arout = RangeExtd.class_eval{ _get_init_args(3, nil) } # class_eval from Ruby 1.8.7 (?)
|
312
449
|
assert_equal [3, nil, F, F], arout
|
313
450
|
arout = RangeExtd.class_eval{ _get_init_args(3, nil, F, T) } # true == exclude_end?
|
314
|
-
assert_equal [3, nil,
|
451
|
+
assert_equal [3, nil, F, T], arout
|
315
452
|
end
|
316
453
|
|
317
454
|
def test_new
|
@@ -393,8 +530,10 @@ gem "minitest"
|
|
393
530
|
assert_equal ra00.exclude_end?, rae0.exclude_end?
|
394
531
|
refute rae0.exclude_begin?
|
395
532
|
|
396
|
-
|
397
|
-
|
533
|
+
# https://ruby-doc.org/core-3.1.2/Range.html#method-i-size],
|
534
|
+
# > Returns the count of elements in self if both begin and end values are numeric; otherwise, returns nil
|
535
|
+
assert( (?d..).size == RangeExtd.new(?d, RangeExtd::Infinity::POSITIVE).size )
|
536
|
+
assert( (..?d).size == RangeExtd.new(RangeExtd::Infinity::NEGATIVE, ?d).size )
|
398
537
|
|
399
538
|
rae0 = RangeExtd.new(?d, nil, false, true)
|
400
539
|
assert rae0.valid?
|
@@ -426,6 +565,32 @@ gem "minitest"
|
|
426
565
|
refute rae0.exclude_begin?
|
427
566
|
end
|
428
567
|
|
568
|
+
def test_new_endless_range04
|
569
|
+
# Ruby 2.7 and later
|
570
|
+
#
|
571
|
+
# Note this would raise a SytaxError for the versions before 2.7.
|
572
|
+
|
573
|
+
assert_equal 9, RangeExtd(..9).end
|
574
|
+
assert_equal ?d, RangeExtd(..?d).end
|
575
|
+
assert_equal ?d, RangeExtd(..?d).last
|
576
|
+
assert_equal ?d, RangeExtd(..?d, true).last
|
577
|
+
assert_nil RangeExtd(..9).begin
|
578
|
+
assert_raises(RangeError){ RangeExtd(..9).first} # raise: cannot get the first element of beginless range (RangeError)
|
579
|
+
assert_nil RangeExtd(..?d).begin
|
580
|
+
assert_raises(RangeError){ (..?d).first} # raise: cannot get the first element of beginless range (RangeError)
|
581
|
+
|
582
|
+
# (nil..nil).size is Infininty, even thouth it does not agree with Ruby's doc's specification.
|
583
|
+
assert_equal (nil..).size, RaE(InfN...InfP, true).size
|
584
|
+
assert_equal (nil..).size, (InfN..InfP).size
|
585
|
+
assert_equal (nil..).size, (InfN...InfP).size
|
586
|
+
assert_equal (-InfF..InfF).size, RaE(-InfF..InfF, true).size
|
587
|
+
assert_equal 0, (InfP..InfN).size
|
588
|
+
assert_raises(FloatDomainError){ (InfP..InfP).size} # NaN
|
589
|
+
assert_raises(FloatDomainError){ (InfN..InfN).size} # NaN
|
590
|
+
assert( (nil..?a).size == (InfN..?a).size )
|
591
|
+
assert( (?a..nil).size == (?a..InfP).size )
|
592
|
+
end
|
593
|
+
|
429
594
|
def test_new_middle_strings
|
430
595
|
aru = ['[','(in)','(ex)',', ','(ex)','(in)',']']
|
431
596
|
ark = [nil, :math, aru, :default]
|
@@ -519,7 +684,8 @@ gem "minitest"
|
|
519
684
|
|
520
685
|
# Wrong range (Object input)
|
521
686
|
assert_raises(re){ RaE(2, -1) }
|
522
|
-
|
687
|
+
assert( RaE(nil, nil).valid?, 'RangeExtd(nil,nil) should be valid now, but..' )
|
688
|
+
assert( RaE(nil..nil).valid?, 'RangeExtd(nil..nil) should be valid now, but..' )
|
523
689
|
assert_raises(re){ RaE(nil, false) }
|
524
690
|
assert_raises(re){ RaE(?d..?a) }
|
525
691
|
assert_raises(re){ RaE(?a, 5) }
|
@@ -556,7 +722,6 @@ gem "minitest"
|
|
556
722
|
|
557
723
|
# Wrong range (Range input)
|
558
724
|
assert_raises(re){ RangeExtd(2..-1) }
|
559
|
-
assert_raises(re){ RangeExtd(nil..nil) }
|
560
725
|
assert_raises(re){ RangeExtd(?d..?a) }
|
561
726
|
|
562
727
|
# Range with contradictory boundary
|
@@ -570,13 +735,22 @@ gem "minitest"
|
|
570
735
|
end
|
571
736
|
|
572
737
|
def test_new_invalid_endless_range01
|
573
|
-
ra00 = (nil..) # This raises the uncapturable SyntaxError for Ruby 2.5 and before anyway.
|
574
|
-
refute RangeExtd.valid?(ra00)
|
575
|
-
assert_raises(RangeError){ RangeExtd.new(ra00) }
|
576
|
-
|
577
738
|
ra01 = (true..)
|
578
739
|
refute RangeExtd.valid?(ra01)
|
579
|
-
assert_raises(RangeError){ RangeExtd.new(ra01)
|
740
|
+
assert_raises(RangeError){ RangeExtd.new(ra01)} # the combination of the arguments does not constitute a valid RangeExtd instance.
|
741
|
+
ra02 = (..true)
|
742
|
+
refute RangeExtd.valid?(ra02)
|
743
|
+
assert_raises(RangeError){ RangeExtd.new(ra02)} # the combination of the arguments does not constitute a valid RangeExtd instance.
|
744
|
+
end
|
745
|
+
|
746
|
+
def test_new_valid_endless_range02
|
747
|
+
ras = [(nil..nil), (nil...nil), RangeExtd((..nil), true), RangeExtd((...nil), true)]
|
748
|
+
# These raise the uncapturable SyntaxError for Ruby 2.5 and earlier.
|
749
|
+
# However, the former two are a meaningful expression in Ruby-2.7+
|
750
|
+
ras.each do |er|
|
751
|
+
assert RangeExtd.valid?(er), er.inspect+" should be valid now."
|
752
|
+
RangeExtd.new(er) # Nothing should be raised
|
753
|
+
end
|
580
754
|
end
|
581
755
|
|
582
756
|
def test_new_exclude_begin_end
|
@@ -674,32 +848,50 @@ gem "minitest"
|
|
674
848
|
assert_raises ArgumentError do
|
675
849
|
RangeExtd.new(5)
|
676
850
|
end
|
677
|
-
assert_raises RangeError do
|
678
|
-
RangeExtd.new(nil,5)
|
679
|
-
end
|
680
|
-
assert_raises RangeError do
|
681
|
-
RangeExtd.new(nil,5,true)
|
682
|
-
end
|
683
|
-
end # def test_exclude_begin
|
684
851
|
|
852
|
+
RangeExtd.new(nil,5) # used to be RangeError (in RangeExtd Ver.1), but nothing should be raised now.
|
853
|
+
RangeExtd.new(nil,5,true) # used to be RangeError (in RangeExtd Ver.1), but nothing should be raised now.
|
854
|
+
end # def test_exclude_begin
|
685
855
|
|
686
|
-
# Test of Range(Extd)#is_none?
|
687
|
-
def test_is_none
|
688
|
-
assert (RangeExtd::NONE.is_none?)
|
689
|
-
assert !(RangeExtd( 1, 1,true,true).is_none?)
|
690
|
-
assert !(RangeExtd(?a,?a,true,true).is_none?)
|
691
|
-
assert !((1...1).is_none?)
|
692
|
-
end
|
693
856
|
|
694
|
-
# Test of Range(Extd)#is_all?
|
857
|
+
# Test of Range(Extd)#is_all? and Range#equiv_all?
|
695
858
|
def test_is_all
|
696
859
|
assert (RangeExtd::ALL.is_all?)
|
860
|
+
refute (RangeExtd::NONE.is_all?)
|
697
861
|
assert (RangeExtd(RangeExtd::Infinity::NEGATIVE, RangeExtd::Infinity::POSITIVE).is_all?) # You can create it, if you want.
|
698
|
-
assert
|
699
|
-
|
862
|
+
assert (RangeExtd(RangeExtd::Infinity::NEGATIVE, RangeExtd::Infinity::POSITIVE).equiv_all?)
|
863
|
+
refute (RangeExtd::Infinity::NEGATIVE..RangeExtd::Infinity::POSITIVE).is_all? # Not the standard Range, though.
|
864
|
+
assert (RangeExtd::Infinity::NEGATIVE..RangeExtd::Infinity::POSITIVE).equiv_all?
|
865
|
+
refute (-Float::INFINITY..Float::INFINITY).is_all?
|
866
|
+
refute (RangeExtd(-Float::INFINITY, Float::INFINITY).is_all?) # Different from Numeric, though.
|
867
|
+
assert (RangeExtd(-Float::INFINITY, Float::INFINITY).equiv_all?)
|
868
|
+
assert (-Float::INFINITY..Float::INFINITY).equiv_all?
|
869
|
+
assert (nil..).equiv_all?, '(nil..) should not be *equivalent* to RangeExtd::ALL.'
|
870
|
+
assert ((..nil).equiv_all?)
|
871
|
+
assert ((nil..nil).equiv_all?)
|
872
|
+
refute ((nil...).equiv_all?) # because of exclude_end
|
873
|
+
refute (nil..).is_all?, '(nil..) should not be *identical* to RangeExtd::ALL.'
|
874
|
+
refute ((..nil).is_all?)
|
875
|
+
refute ((nil..nil).is_all?)
|
876
|
+
refute ((nil...).is_all?)
|
877
|
+
assert (nil..nil).equiv?( RangeExtd::ALL)
|
878
|
+
refute (nil...nil).equiv?(RangeExtd::ALL)
|
879
|
+
assert_equal (nil..), (..nil)
|
880
|
+
assert_operator (nil..), :eql?, (..nil)
|
881
|
+
assert_equal (nil..), (RangeExtd::Infinity::NEGATIVE..RangeExtd::Infinity::POSITIVE)
|
882
|
+
assert_equal (RangeExtd::Infinity::NEGATIVE..RangeExtd::Infinity::POSITIVE), (nil..)
|
883
|
+
assert_equal RangeExtd::ALL, (nil..)
|
884
|
+
refute_equal RangeExtd::ALL, (nil...)
|
885
|
+
assert_equal((nil..), RangeExtd::ALL)
|
886
|
+
refute_equal((nil...), RangeExtd::ALL)
|
887
|
+
assert_equal RangeExtd::ALL, RaE((nil..))
|
888
|
+
refute_equal RangeExtd::ALL, RaE((nil...))
|
889
|
+
assert_equal RaE(nil..), RangeExtd::ALL
|
890
|
+
refute_equal RaE((nil..), true), RangeExtd::ALL
|
891
|
+
refute_operator RangeExtd::ALL, :eql?, (nil..) # They are NOT eql? (but :==)
|
892
|
+
refute_operator (nil..), :eql?, RangeExtd::ALL
|
700
893
|
end
|
701
894
|
|
702
|
-
|
703
895
|
# Test of Range#== because it has changed first!
|
704
896
|
def test_eql_range
|
705
897
|
assert (@r11 == (@ib..@ie))
|
@@ -776,6 +968,21 @@ gem "minitest"
|
|
776
968
|
assert_equal false, ((?a...?a) == RangeExtd(?a...?b, true))
|
777
969
|
end
|
778
970
|
|
971
|
+
def test_eql4
|
972
|
+
assert_equal((NOWHERE..nil), (NOWHERE..))
|
973
|
+
refute_equal((..nil), (..NOWHERE))
|
974
|
+
refute_equal((..nil), (NOWHERE..))
|
975
|
+
|
976
|
+
# The following is in fact unexpected...
|
977
|
+
# NOWHERE behaves like nil, and so I would expect it would be the same as (nil..?a).
|
978
|
+
# Note RangeExtd::Infinity::NEGATIVE is accepted. It seems that
|
979
|
+
# the begin object must have <=> method, unless it is *literally* NilClass nil.
|
980
|
+
#
|
981
|
+
# In this particular case, this is not a problem, because
|
982
|
+
# NOWHERE desirably should not be used in conjunction with other objects
|
983
|
+
# in the first place.
|
984
|
+
assert_raises(ArgumentError, '"bad value for range" would be raised'){(NOWHERE..?a)}
|
985
|
+
end
|
779
986
|
|
780
987
|
def test_begin
|
781
988
|
assert_equal(@ib, @s11.begin)
|
@@ -801,7 +1008,6 @@ gem "minitest"
|
|
801
1008
|
# irb> (5.2...9).first(1) # => TypeError: can't iterate from Float
|
802
1009
|
# irb> RangeExtd(5.2,9,:exclude_begin=>true).first(1) # => [6] # Not right!
|
803
1010
|
#
|
804
|
-
|
805
1011
|
assert_equal(@ib, @s11.first)
|
806
1012
|
assert_equal(@ib, @s12.first)
|
807
1013
|
assert_equal(@ib, @s21.first)
|
@@ -813,9 +1019,6 @@ gem "minitest"
|
|
813
1019
|
assert_raises RangeError do
|
814
1020
|
RangeExtd(9, 3, true)
|
815
1021
|
end
|
816
|
-
assert_raises ArgumentError do
|
817
|
-
@s22.first(1, 3)
|
818
|
-
end
|
819
1022
|
|
820
1023
|
## String
|
821
1024
|
sc1 = RangeExtd.new("D".."F", false)
|
@@ -841,10 +1044,17 @@ gem "minitest"
|
|
841
1044
|
sf2.first(1)
|
842
1045
|
end
|
843
1046
|
|
844
|
-
##
|
845
|
-
|
846
|
-
|
847
|
-
end
|
1047
|
+
## Infinity
|
1048
|
+
rain = RaE(InfN, InfP, true)
|
1049
|
+
assert( rain.begin.eql?(InfN) )
|
1050
|
+
assert( rain.end.eql?( InfP) )
|
1051
|
+
assert( rain.first.eql?(InfN) )
|
1052
|
+
assert( rain.last.eql?( InfP) )
|
1053
|
+
assert_raises(TypeError){ rain.first(1) }
|
1054
|
+
# assert_raises(TypeError){ rain.last(1) } # => infinite loop!
|
1055
|
+
|
1056
|
+
## beginless/endless
|
1057
|
+
RangeExtd.new(nil..nil, true) # used to be RangeError (in RangeExtd Ver.1), but nothing should be raised now.
|
848
1058
|
|
849
1059
|
assert_raises ArgumentError do
|
850
1060
|
@s22.first(-7) # "negative array size (or size too big)"
|
@@ -855,6 +1065,18 @@ gem "minitest"
|
|
855
1065
|
assert_raises TypeError do
|
856
1066
|
@s22.first(nil)
|
857
1067
|
end
|
1068
|
+
|
1069
|
+
# (..nil).first : cannot get the first element of beginless range (RangeError)
|
1070
|
+
# (2..2).first(-1) : negative array size (or size too big) (ArgumentError)
|
1071
|
+
# (2..2).first(1,2) : wrong number of arguments (given 2, expected 1) (ArgumentError)
|
1072
|
+
# (2..2).first(?a) : no implicit conversion of String into Integer (TypeError)
|
1073
|
+
# (true..true).first(1) : can't iterate from TrueClass (TypeError)
|
1074
|
+
assert_raises(RangeError){ RangeExtd::NONE.first }
|
1075
|
+
assert_equal RangeExtd::NONE, RaE(3...3, true)
|
1076
|
+
assert_equal 3, RaE(3...3, true).first, 'Alghough (3<...3) == NONE, (3<...3).first should return a value (in the same way as (-Float::INFINITY..5).first returns -Infinity)'
|
1077
|
+
assert_raises(RangeError){ RangeExtd::NONE.first(2) }
|
1078
|
+
assert_raises(ArgumentError){ RaE(2,3,true).first(1,2) } # Wrong number of arguments
|
1079
|
+
assert_raises(ArgumentError){ RaE(2,3,true).first(-1) } # Negative index
|
858
1080
|
end # def test_first
|
859
1081
|
|
860
1082
|
|
@@ -885,6 +1107,7 @@ gem "minitest"
|
|
885
1107
|
end
|
886
1108
|
|
887
1109
|
|
1110
|
+
# alias to :===, :member?
|
888
1111
|
def test_include
|
889
1112
|
assert @s11.include?(@ib)
|
890
1113
|
assert @s11.include?(@ie)
|
@@ -909,6 +1132,13 @@ gem "minitest"
|
|
909
1132
|
assert !(RangeExtd.new("a", "z", 777) === "a")
|
910
1133
|
assert (RangeExtd.new("a", "z", nil) === "a")
|
911
1134
|
assert (RangeExtd.new("a", "z", 777) === "b")
|
1135
|
+
|
1136
|
+
assert (RaE(..nil, true) === "b")
|
1137
|
+
assert (RaE(..nil, true) === nil)
|
1138
|
+
assert (RangeExtd::ALL === ?b)
|
1139
|
+
refute (RangeExtd::NONE === ?b)
|
1140
|
+
refute (RangeExtd::NONE === nil) # NONE includes nothing
|
1141
|
+
refute (RangeExtd::NONE === NOWHERE) # NONE includes nothing, even NOWHERE (because of exclude_begin/end)
|
912
1142
|
end # def test_include
|
913
1143
|
|
914
1144
|
|
@@ -977,10 +1207,41 @@ gem "minitest"
|
|
977
1207
|
end # def test_cover
|
978
1208
|
|
979
1209
|
|
1210
|
+
# No redefinition of the method, because super() works fine!
|
1211
|
+
def test_count
|
1212
|
+
assert_equal 5, RaE(1..5).count
|
1213
|
+
assert_equal 4, RaE(1...5).count
|
1214
|
+
assert_equal 3, RaE(1...5, true).count
|
1215
|
+
assert_equal 5, RaE(1...5.1).count
|
1216
|
+
assert_equal 4, RaE(1...5.1, true).count
|
1217
|
+
assert_raises(TypeError){ RaE(1.0..5).count } # can't iterate from Float
|
1218
|
+
assert_equal 0, RaE(1..5.1).count(5.1)
|
1219
|
+
assert_equal 1, RaE(1..5.1).count(1)
|
1220
|
+
assert_equal 0, RaE(1..5.1, true).count(1)
|
1221
|
+
assert_equal 2, RaE(1..5.1).count{|i| i<3}
|
1222
|
+
assert_equal 1, RaE(1..5.1, true).count{|i| i<3}
|
1223
|
+
assert_raises(TypeError){ (..5).count{|i| i<3} } # can't iterate from NilClass
|
1224
|
+
assert_raises(TypeError){ RaE(..5).count{|i| i<3} }
|
1225
|
+
# (1..).count{|i| i<3} # => infinite loop
|
1226
|
+
assert_equal InfF, RaE(5..).count
|
1227
|
+
assert_equal InfF, RaE(..5).count
|
1228
|
+
assert_equal InfF, (..nil).count
|
1229
|
+
assert_raises(TypeError){ (..nil).count(3) } # can't iterate from NilClass
|
1230
|
+
assert_raises(TypeError){ (..nil).count{|i| i<3} }
|
1231
|
+
assert_equal InfF, (-Float::INFINITY..nil).count
|
1232
|
+
assert_equal InfF, RaE(-Float::INFINITY..nil, true).count
|
1233
|
+
assert_raises(TypeError){ (-Float::INFINITY..Float::INFINITY).count } # can't iterate from Float (TypeError)
|
1234
|
+
assert_raises(TypeError){ RaE(-Float::INFINITY..Float::INFINITY).count }
|
1235
|
+
assert_equal InfF, RangeExtd::ALL.count # A special case, like (..nil).
|
1236
|
+
end
|
1237
|
+
|
1238
|
+
|
980
1239
|
def test_hash
|
981
1240
|
assert_equal(@r11.hash, @s11.hash)
|
982
1241
|
assert_equal(@r12.hash, @s12.hash)
|
983
|
-
|
1242
|
+
refute_equal(@r12.hash, @s22.hash)
|
1243
|
+
refute_equal RangeExtd::NONE.hash, (...nil).hash
|
1244
|
+
refute_equal RangeExtd::NONE.hash, RaE((...nil), true).hash
|
984
1245
|
end # def test_hash
|
985
1246
|
|
986
1247
|
def test_min
|
@@ -991,17 +1252,25 @@ gem "minitest"
|
|
991
1252
|
|
992
1253
|
assert_equal(@ie-1, @s22.min{|a,b| -a <=> -b})
|
993
1254
|
|
994
|
-
assert_raises
|
995
|
-
|
996
|
-
|
1255
|
+
assert_raises(TypeError){ RangeExtd.new(1.0, 5, :exclude_begin => true).min }
|
1256
|
+
assert_raises(RangeError){ (..3).min } # cannot get the minimum of beginless range
|
1257
|
+
|
1258
|
+
assert_equal(?b, (?b...).min)
|
1259
|
+
assert_equal(?b, (?b...InfP).min)
|
1260
|
+
assert_equal(?b, RaE(?b...InfP).min)
|
1261
|
+
assert_equal(?c, RaE(?b...InfP, true).min)
|
1262
|
+
assert_raises(RangeError){ RaE(..?z).min }
|
1263
|
+
assert_equal(-InfF, RaE(-InfF..9.2).min)
|
1264
|
+
assert_equal InfN, RaE(InfN..?z).min
|
997
1265
|
end # def test_min
|
998
1266
|
|
999
1267
|
def test_min_by
|
1000
1268
|
assert_equal(@ib+1, @s22.min_by.each{|i| i})
|
1001
1269
|
|
1002
|
-
|
1003
|
-
|
1004
|
-
|
1270
|
+
assert_equal Enumerator, RangeExtd.new(1.0, 5, true).min_by.class
|
1271
|
+
assert_raises(TypeError){ RangeExtd.new(1.0, 5, true).min_by{|i|} }
|
1272
|
+
assert_raises(TypeError){ RangeExtd.new(1.0, 5, true).min_by.each{|i| i} } # can't iterate from Float (TypeError)
|
1273
|
+
assert_equal 2, RangeExtd.new(1..5, exclude_begin: true).min_by.each{|i| i}
|
1005
1274
|
|
1006
1275
|
assert_equal(@ie-1, @s22.min_by{|a| -a })
|
1007
1276
|
end # def test_min_by
|
@@ -1015,24 +1284,26 @@ gem "minitest"
|
|
1015
1284
|
|
1016
1285
|
assert_equal([@ie-1,@ib+1], @s22.minmax{|a,b| -a <=> -b}) # Not the best test...
|
1017
1286
|
|
1018
|
-
assert_raises
|
1019
|
-
|
1020
|
-
|
1287
|
+
assert_raises(TypeError){ RangeExtd.new(1.0, 5, true).minmax }
|
1288
|
+
assert_raises(RangeError){ (?b...).minmax } # cannot get the maximum of endless range
|
1289
|
+
assert_raises(RangeError){ RaE(?b...).minmax }
|
1021
1290
|
end # def test_minmax
|
1022
1291
|
|
1023
1292
|
|
1024
1293
|
def test_minmax_by
|
1025
1294
|
assert_equal([@ib+1,@ie-1], @s22.minmax_by.each{|i| i})
|
1026
1295
|
|
1027
|
-
assert_raises
|
1028
|
-
|
1029
|
-
|
1296
|
+
assert_raises(TypeError){ (1.0...5).minmax_by{|i| } }
|
1297
|
+
assert_raises(TypeError){ RangeExtd.new(1.0, 5, true).minmax_by{|i| } }
|
1298
|
+
assert_equal Enumerator, RangeExtd.new(1.0, 5, true).minmax_by.class
|
1030
1299
|
|
1031
1300
|
assert_equal([@ie-1,@ib+1], @s22.minmax_by{|a| -a })
|
1032
1301
|
end # def test_minmax_by
|
1033
1302
|
|
1034
1303
|
|
1035
1304
|
def test_max
|
1305
|
+
assert_raises(TypeError){ (2.0...4.9).max } # cannot exclude non Integer end value
|
1306
|
+
assert_raises(TypeError){ (1.3...5).max } # cannot exclude end value with non Integer begin value
|
1036
1307
|
assert_equal(@ie, @s11.max)
|
1037
1308
|
assert_equal(@ie-1, @s12.max)
|
1038
1309
|
assert_equal(@ie, @s21.max)
|
@@ -1040,20 +1311,22 @@ gem "minitest"
|
|
1040
1311
|
|
1041
1312
|
assert_equal(@ib+1, @s22.max{|a,b| -a <=> -b}) # Not the best test...
|
1042
1313
|
|
1043
|
-
|
1044
|
-
|
1045
|
-
|
1314
|
+
assert_equal 5, RaE(1.0, 5, true).max
|
1315
|
+
|
1316
|
+
assert_raises(RangeError){ (...?z).max } # cannot get the maximum of beginless range with custom comparison method
|
1317
|
+
assert_raises(RangeError){ RaE(...?z, true).max }
|
1318
|
+
assert_equal(?z, RaE(InfN..?z).max)
|
1319
|
+
assert_nil RaE(5...6, true).max
|
1046
1320
|
end # def test_max
|
1047
1321
|
|
1048
1322
|
|
1049
1323
|
def test_max_by
|
1050
1324
|
assert_equal(@ie-1, @s22.max_by.each{|i| i})
|
1051
|
-
|
1052
|
-
assert_raises
|
1053
|
-
RangeExtd.new(1.0, 5, true).max_by
|
1054
|
-
end
|
1325
|
+
assert_equal Enumerator, RaE(1.0, 5, true).max_by.class
|
1326
|
+
assert_raises(TypeError){ RaE(1.0, 5, true).max_by{|i|} } # can't iterate from Float
|
1055
1327
|
|
1056
1328
|
assert_equal(@ib+1, @s22.max_by{|a| -a })
|
1329
|
+
assert_equal(3, RaE(3...6).max_by{|a| -a })
|
1057
1330
|
end # def test_max_by
|
1058
1331
|
|
1059
1332
|
|
@@ -1121,6 +1394,28 @@ gem "minitest"
|
|
1121
1394
|
end
|
1122
1395
|
end # def test_size
|
1123
1396
|
|
1397
|
+
# Following the main doc...
|
1398
|
+
def test_size2
|
1399
|
+
assert_equal InfF, RaE(5..InfF).size
|
1400
|
+
assert_equal InfF, RaE(..?z).size # This contradicts the specification. Should be nil.
|
1401
|
+
assert_equal InfF, RaE(...?z, true).size
|
1402
|
+
assert_equal InfF, RaE(..nil).size # This may contradict the specification?
|
1403
|
+
assert_equal InfF, RaE(...nil, true).size
|
1404
|
+
assert_equal InfF, RaE(3.., true).size
|
1405
|
+
assert_equal InfF, RaE(..?a, true).size # This contradicts the specification. Should be nil.
|
1406
|
+
assert_equal InfF, RaE(InfN...?a, true).size
|
1407
|
+
assert_nil RaE(?a.., true).size
|
1408
|
+
assert_nil RaE(?a...InfP, true).size
|
1409
|
+
assert_equal 0, RangeExtd::NONE.size # This specification may change.
|
1410
|
+
assert_nil RaE(?a...?a, true).size # Any size with non-Numeric Range is nil.
|
1411
|
+
|
1412
|
+
# More perplexing standard Ruby behaviours.
|
1413
|
+
# Bug #18993: https://bugs.ruby-lang.org/issues/18993
|
1414
|
+
assert_equal 3, (5.quo(3)...5).size # => 3
|
1415
|
+
assert_equal 4, (5.quo(3).to_f...5).size # => 4
|
1416
|
+
assert_equal 4, (5.quo(3)..5).size # => 4
|
1417
|
+
assert_equal 4, (5.quo(3).to_f..5).size # => 4
|
1418
|
+
end # def test_size2
|
1124
1419
|
|
1125
1420
|
def test_step
|
1126
1421
|
ns=0; @s11.step(2){|i| ns+=i}
|
@@ -1166,8 +1461,8 @@ gem "minitest"
|
|
1166
1461
|
assert_equal(-1, (RangeExtd::Infinity::NEGATIVE <=> RangeExtd::Infinity::POSITIVE))
|
1167
1462
|
assert_equal RangeExtd::Infinity::NEGATIVE, RangeExtd::Infinity::NEGATIVE
|
1168
1463
|
assert_equal RangeExtd::Infinity::NEGATIVE, RangeExtd::Infinity::NEGATIVE
|
1169
|
-
|
1170
|
-
|
1464
|
+
refute RangeExtd::Infinity::POSITIVE.respond_to? :succ
|
1465
|
+
refute RangeExtd::Infinity::NEGATIVE.respond_to? :succ
|
1171
1466
|
refute_equal(-Float::INFINITY, RangeExtd::Infinity::NEGATIVE)
|
1172
1467
|
refute_equal Float::INFINITY, RangeExtd::Infinity::POSITIVE
|
1173
1468
|
assert_raises(ArgumentError) { Float::INFINITY < RangeExtd::Infinity::POSITIVE }
|
@@ -1226,9 +1521,9 @@ gem "minitest"
|
|
1226
1521
|
end
|
1227
1522
|
|
1228
1523
|
def test_RangeExtdClass_valid
|
1229
|
-
assert
|
1230
|
-
assert
|
1231
|
-
assert
|
1524
|
+
assert RangeExtd.valid?(nil, nil, 9,9) # All 3 were true up to Version 0.1.0 and at 2.0+
|
1525
|
+
assert RangeExtd.valid?(nil...nil,9,9)
|
1526
|
+
assert RangeExtd.valid?(nil..nil, 9,9)
|
1232
1527
|
end # def test_RangeExtdClass_valid
|
1233
1528
|
|
1234
1529
|
def test_Range_valid
|
@@ -1239,7 +1534,7 @@ gem "minitest"
|
|
1239
1534
|
assert (?a..?a).valid? # single element
|
1240
1535
|
assert !(?a...?a).valid? # less than empty
|
1241
1536
|
assert (?a...?b).valid? # single element
|
1242
|
-
assert
|
1537
|
+
assert (nil..nil).valid?
|
1243
1538
|
assert !(true...true).valid?
|
1244
1539
|
assert RangeExtd(0..0).valid?
|
1245
1540
|
#assert !RangeExtd(0...0).valid?
|
@@ -1260,8 +1555,8 @@ gem "minitest"
|
|
1260
1555
|
assert (0..).valid? # This raises the uncapturable SyntaxError for Ruby 2.5 and before anyway.
|
1261
1556
|
assert (0...).valid?
|
1262
1557
|
refute (true..).valid?
|
1263
|
-
|
1264
|
-
|
1558
|
+
assert (nil...).valid? # false in Ver.1 (Ruby 2.6)
|
1559
|
+
assert (nil..nil).valid? # false in Ver.1 (Ruby 2.6)
|
1265
1560
|
assert (Float::INFINITY..Float::INFINITY).valid?
|
1266
1561
|
assert (-Float::INFINITY..Float::INFINITY).valid?
|
1267
1562
|
assert (-Float::INFINITY...Float::INFINITY).valid?
|
@@ -1276,7 +1571,8 @@ gem "minitest"
|
|
1276
1571
|
assert_nil (?a...?a).empty? # less than empty
|
1277
1572
|
assert !(?a...?b).empty? # single element
|
1278
1573
|
assert RangeExtd(?a...?b, :exclude_begin => true).empty? # empty
|
1279
|
-
|
1574
|
+
refute (nil..nil).empty?
|
1575
|
+
refute (nil..3).empty?
|
1280
1576
|
assert_nil (true...true).empty?
|
1281
1577
|
assert !RangeExtd(0..0).empty? # single element
|
1282
1578
|
assert_nil (0...0).empty?
|
@@ -1305,7 +1601,7 @@ gem "minitest"
|
|
1305
1601
|
assert (?a...?a).null? # less than empty
|
1306
1602
|
assert !(?a...?b).null? # single element
|
1307
1603
|
assert RangeExtd(?a...?b, :exclude_begin => true).null? # empty
|
1308
|
-
|
1604
|
+
refute (nil..nil).null?
|
1309
1605
|
assert (true...true).null?
|
1310
1606
|
assert !RangeExtd(0..0).null? # single element
|
1311
1607
|
assert (0...0).null?
|
@@ -1404,23 +1700,64 @@ gem "minitest"
|
|
1404
1700
|
end # def test_RangeExtd_equiv
|
1405
1701
|
|
1406
1702
|
|
1703
|
+
# Test of Range(Extd)#is_none? and RangeExtd::NONE
|
1407
1704
|
def test_RangeExtd_none
|
1705
|
+
# From examples in RangeExtd#is_none?
|
1706
|
+
assert RaE(0,0,true,true).valid?
|
1707
|
+
assert (RaE(0,0,true,true) == RangeExtd::NONE)
|
1708
|
+
assert RaE(0,0,true,true).empty?
|
1709
|
+
refute RaE(0,0,true,true).is_none?
|
1710
|
+
|
1711
|
+
refute (RangeExtd( 1, 1,true,true).is_none?)
|
1712
|
+
refute (RangeExtd(?a,?a,true,true).is_none?)
|
1713
|
+
refute ((1...1).is_none?)
|
1714
|
+
|
1408
1715
|
assert RangeExtd::NONE.is_none?
|
1409
1716
|
assert RangeExtd::NONE.valid?
|
1410
1717
|
assert RangeExtd::NONE.null?
|
1411
1718
|
assert RangeExtd::NONE.empty?
|
1412
|
-
|
1413
|
-
|
1414
|
-
|
1415
|
-
|
1719
|
+
assert_raises(RangeError){ RaE(NOWHERE...NOWHERE, true) }
|
1720
|
+
refute((NOWHERE...NOWHERE).valid?, "RangeExtd::Nowhere::NOWHERE should not reside anywhere but in RangeExtd::NONE")
|
1721
|
+
#assert_equal RaE(NOWHERE...NOWHERE, true), RangeExtd::NONE
|
1722
|
+
#refute RaE(NOWHERE...NOWHERE, true).is_none?, "Even an equivalent RangeExtd to NONE should NOT be true==is_none?"
|
1723
|
+
|
1724
|
+
assert_nil RangeExtd::NONE.begin
|
1725
|
+
assert RangeExtd::NONE.begin.nowhere?, "RangeExtd::NONE.begin should be RangeExtd::Nowhere::NOWHERE"
|
1726
|
+
assert_nil RangeExtd::NONE.end
|
1727
|
+
assert RangeExtd::NONE.end.nowhere?
|
1728
|
+
|
1729
|
+
refute RaE(0...0, true).is_none?
|
1730
|
+
refute (nil..nil).is_none?
|
1731
|
+
refute (nil...nil).is_none?
|
1732
|
+
assert RaE(...nil, true).valid?
|
1733
|
+
refute RaE(...nil, true).null?
|
1734
|
+
refute RaE(...nil, true).empty?
|
1735
|
+
|
1736
|
+
refute_equal RaE(0...0, true), RaE(?a...?a, true)
|
1737
|
+
assert_equal RaE(0...1, true), RaE(5...6, true)
|
1738
|
+
refute (RaE(0...1, true).eql?(RaE(5...6, true)))
|
1739
|
+
assert_equal RaE(0...1, true), RaE(1...1, true)
|
1740
|
+
assert_equal RaE(0...1, true), RangeExtd::NONE
|
1741
|
+
assert_equal RaE(?a...?b, true), RangeExtd::NONE
|
1742
|
+
refute_equal((1...1), RangeExtd::NONE, "comparison with invalid Range should return false")
|
1416
1743
|
assert_equal RaE(0, 0, true, true), RangeExtd::NONE
|
1417
1744
|
assert_equal RaE(?a, ?a, true, true), RangeExtd::NONE
|
1418
1745
|
assert_equal RaE(?a, ?b, true, true), RangeExtd::NONE
|
1419
1746
|
assert !RaE(?a, ?b, true, true).is_none?
|
1420
1747
|
assert RaE(?a, ?b, true, true).empty?
|
1421
1748
|
assert RaE(?a, ?b, true, true).null?
|
1422
|
-
|
1423
|
-
|
1749
|
+
|
1750
|
+
assert( RangeExtd::NONE == RangeExtd::NONE )
|
1751
|
+
refute( RangeExtd::NONE === RangeExtd::NONE )
|
1752
|
+
refute_equal( (..nil), RangeExtd::NONE)
|
1753
|
+
refute (RaE(...nil, true).eql?(RangeExtd::NONE))
|
1754
|
+
refute_equal(RaE(...nil, true), RangeExtd::NONE)
|
1755
|
+
refute_equal RangeExtd::NONE, (..nil)
|
1756
|
+
refute_equal RangeExtd::NONE, RaE(...nil, true)
|
1757
|
+
refute_equal RangeExtd::NONE, RangeExtd::ALL
|
1758
|
+
refute_equal RangeExtd::ALL, RangeExtd::NONE
|
1759
|
+
assert_equal RangeExtd::ALL, (..nil)
|
1760
|
+
assert_equal((..nil), RangeExtd::ALL)
|
1424
1761
|
end
|
1425
1762
|
|
1426
1763
|
|
@@ -1432,9 +1769,9 @@ gem "minitest"
|
|
1432
1769
|
assert RaE(?a, ?b, 5, 5) == RaE(?c, ?d, 5, 5)
|
1433
1770
|
assert RaE(11, 12, 5, 5) == RaE(11, 11, 5, 5)
|
1434
1771
|
assert RaE(11, 12, 5, 5) == RaE(1, 2, 5, 5)
|
1435
|
-
|
1772
|
+
refute RaE(11, 12, 5, 5).eql?( RaE(1, 2, 5, 5) )
|
1436
1773
|
assert RaE(11, 12, 5, 5) == RaE(3.0, 3.0, 5, 5)
|
1437
|
-
|
1774
|
+
refute RaE(11, 12, 5, 5).eql?( RaE(3.0, 3.0, 5, 5) )
|
1438
1775
|
#assert_equal RangeExtd::NONE, RangeExtd(0...0, :exclude_begin => true) # => ArgumentError
|
1439
1776
|
#assert_equal RangeExtd(0...0, :exclude_begin => true), RangeExtd::NONE
|
1440
1777
|
end
|
@@ -1477,9 +1814,8 @@ gem "minitest"
|
|
1477
1814
|
assert_raises TypeError do
|
1478
1815
|
r.first(?a)
|
1479
1816
|
end
|
1480
|
-
|
1481
|
-
|
1482
|
-
end
|
1817
|
+
|
1818
|
+
# r.last(3) # goes into an infinite loop!
|
1483
1819
|
end
|
1484
1820
|
|
1485
1821
|
|
@@ -1517,14 +1853,12 @@ gem "minitest"
|
|
1517
1853
|
assert_equal 'z', rs.last
|
1518
1854
|
assert !rs.cover?(?z)
|
1519
1855
|
assert rs.cover?(?x)
|
1520
|
-
|
1856
|
+
assert (rs === ?x) # This used to be nil.
|
1521
1857
|
assert_equal RangeExtd::Infinity::NEGATIVE, rs.begin # It is Infinity,
|
1522
1858
|
refute_equal(-Float::INFINITY, rs.begin)
|
1523
1859
|
assert ! rs.begin.positive?
|
1524
|
-
|
1525
|
-
assert_raises
|
1526
|
-
rs.last(3)
|
1527
|
-
end
|
1860
|
+
assert( (..?z).size == rs.size )
|
1861
|
+
#assert_raises(TypeError){ rs.last(3) } # => infiite loop
|
1528
1862
|
end
|
1529
1863
|
|
1530
1864
|
def test_infinity_unary_operators
|
@@ -1549,13 +1883,13 @@ gem "minitest"
|
|
1549
1883
|
assert (RaE(?c,?c,T,T) != RaE(1, 1, T, T)) # (1<...1) != (?c<...?c) # - because of Fixnum and String
|
1550
1884
|
assert_equal RaE(3, 4, T, T), RaE(1.0, 1.0, T, T) # (1.0<...1.0) == (3<...4) # => true
|
1551
1885
|
|
1552
|
-
# RangeExtd#eql?
|
1886
|
+
# RangeExtd#eql? (removed from doc)
|
1553
1887
|
assert_equal (1.0...5.0), (1...5) # (1...5) == (1.0...5.0) # => true
|
1554
|
-
|
1555
|
-
|
1556
|
-
|
1557
|
-
|
1558
|
-
|
1888
|
+
refute (1...5).eql?(1.0...5.0) # (1...5).eql?(1.0...5.0) # => false
|
1889
|
+
refute RaE(1, 1, T,T).eql?(RangeExtd::NONE) # (1<...1).eql?( RangeExtd::NONE) # => true
|
1890
|
+
refute RaE(?a, ?b, T,T).eql?(RangeExtd::NONE) # (?a<...?b).eql?(RangeExtd::NONE) # => true
|
1891
|
+
refute RaE(1, 1, T,T).eql?(RaE(3,4,T,T)) # (1<...1).eql?( 3<...4) # => true
|
1892
|
+
refute RaE(1.0,1.0,T,T).eql?(RaE(3,4,T,T)) # (1.0<...1.0).eql?(3<...4) # => false
|
1559
1893
|
|
1560
1894
|
# RangeExtd#===
|
1561
1895
|
assert ((?D..?z) === ?c)
|
@@ -1656,8 +1990,8 @@ gem "minitest"
|
|
1656
1990
|
# RangeExtd#step # => the same as each
|
1657
1991
|
|
1658
1992
|
# RangeExtd.valid?
|
1659
|
-
assert
|
1660
|
-
assert
|
1993
|
+
assert RangeExtd.valid?(nil..nil)
|
1994
|
+
assert RangeExtd.valid?(nil...nil)
|
1661
1995
|
assert RangeExtd.valid?(0..0) # => true
|
1662
1996
|
assert !RangeExtd.valid?(0...0) # => false
|
1663
1997
|
assert !RangeExtd.valid?(0..0, true) # => false
|
@@ -1702,11 +2036,11 @@ gem "minitest"
|
|
1702
2036
|
|
1703
2037
|
# Range#==
|
1704
2038
|
assert !(1...1).valid?
|
1705
|
-
assert
|
2039
|
+
assert (nil...nil).valid?
|
1706
2040
|
assert !((1...1) == RangeExtd(1, 1, true, true)) # => false.
|
1707
2041
|
|
1708
2042
|
# Range#valid?
|
1709
|
-
assert
|
2043
|
+
assert (nil..nil).valid?
|
1710
2044
|
assert (0..0).valid? # => true
|
1711
2045
|
assert !(0...0).valid? # => false
|
1712
2046
|
assert !(2..-1).valid? # => false
|
@@ -1716,7 +2050,7 @@ gem "minitest"
|
|
1716
2050
|
assert RangeExtd::ALL.valid? # => true
|
1717
2051
|
|
1718
2052
|
# Range#empty?
|
1719
|
-
|
2053
|
+
refute (nil..nil).empty?
|
1720
2054
|
assert_nil (1...1).empty? # => nil
|
1721
2055
|
assert !(1..1).empty? # => false
|
1722
2056
|
assert RangeExtd(1...1, true).empty? # => true
|
@@ -1737,7 +2071,7 @@ gem "minitest"
|
|
1737
2071
|
# class Infinity
|
1738
2072
|
assert_equal( -1, (?z <=> RangeExtd::Infinity::POSITIVE))
|
1739
2073
|
assert_equal 1, (RangeExtd::Infinity::POSITIVE <=> ?z)
|
1740
|
-
|
2074
|
+
assert_nil( (50 <=> RangeExtd::Infinity::POSITIVE), 'When "range_extd/numeric" is NOT required, 50 is not comparable with POSITIVE-infinity.')
|
1741
2075
|
assert_equal 1, (RangeExtd::Infinity::POSITIVE <=> 50)
|
1742
2076
|
end # def test_in_document
|
1743
2077
|
|