Skip to content

Commit

Permalink
Add Range#overlap?
Browse files Browse the repository at this point in the history
  • Loading branch information
marcandre committed Jan 4, 2024
1 parent 2a7444b commit f6d55c5
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ itself, JRuby and Rubinius.
#### Range

- `reverse_each` (with correct handling of endless Ranges)
- `overlap?`

## Ruby 3.2 backports

Expand Down
3 changes: 3 additions & 0 deletions lib/backports/3.3.0/range.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require 'backports/tools/require_relative_dir'

Backports.require_relative_dir
18 changes: 18 additions & 0 deletions lib/backports/3.3.0/range/overlap.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
unless Range.method_defined? :overlap?
class Range
def overlap?(other)
raise TypeError, "wrong argument type #{other.class} (expected Range)" unless other.is_a?(Range)

[
[other.begin, self.end, exclude_end?],
[self.begin, other.end, other.exclude_end?],
[self.begin, self.end, exclude_end?],
[other.begin, other.end, other.exclude_end?],
].all? do |from, to, excl|
less = (from || -Float::INFINITY) <=> (to || Float::INFINITY)
less = +1 if less == 0 && excl
less && less <= 0
end
end
end
end
67 changes: 67 additions & 0 deletions test/overlap_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
require './test/test_helper'
require 'backports/3.3.0/range/overlap'

class OverlapTest < Test::Unit::TestCase
def test_overlap?
assert_not_operator(0..2, :overlap?, -2..-1)
assert_not_operator(0..2, :overlap?, -2...0)
assert_operator(0..2, :overlap?, -1..0)
assert_operator(0..2, :overlap?, 1..2)
assert_operator(0..2, :overlap?, 2..3)
assert_not_operator(0..2, :overlap?, 3..4)
assert_not_operator(0...2, :overlap?, 2..3)

assert_operator(nil..0, :overlap?, -1..0)
assert_operator(nil...0, :overlap?, -1..0)
assert_operator(nil..0, :overlap?, 0..1)
assert_operator(nil..0, :overlap?, nil..1)
assert_not_operator(nil..0, :overlap?, 1..2)
assert_not_operator(nil...0, :overlap?, 0..1)

assert_not_operator(0..nil, :overlap?, -2..-1)
assert_not_operator(0..nil, :overlap?, nil...0)
assert_operator(0..nil, :overlap?, -1..0)
assert_operator(0..nil, :overlap?, nil..0)
assert_operator(0..nil, :overlap?, 0..1)
assert_operator(0..nil, :overlap?, 1..2)
assert_operator(0..nil, :overlap?, 1..nil)

assert_not_operator((1..3), :overlap?, ('a'..'d'))

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

assert_operator((1..3), :overlap?, (2..4))
assert_operator((1...3), :overlap?, (2..3))
assert_operator((2..3), :overlap?, (1..2))
assert_operator((nil..3), :overlap?, (3..nil))
assert_operator((nil..nil), :overlap?, (3..nil))
assert_operator((nil...nil), :overlap?, (nil..nil))

assert_raise(TypeError) { (1..3).overlap?(1) }

assert_not_operator((1..2), :overlap?, (2...2))
assert_not_operator((2...2), :overlap?, (1..2))

assert_not_operator((4..1), :overlap?, (2..3))
assert_not_operator((4..1), :overlap?, (nil..3))
assert_not_operator((4..1), :overlap?, (2..nil))

assert_not_operator((1..4), :overlap?, (3..2))
assert_not_operator((nil..4), :overlap?, (3..2))
assert_not_operator((1..nil), :overlap?, (3..2))

assert_not_operator((4..5), :overlap?, (2..3))
assert_not_operator((4..5), :overlap?, (2...4))

assert_not_operator((1..2), :overlap?, (3..4))
assert_not_operator((1...3), :overlap?, (3..4))

assert_not_operator((4..5), :overlap?, (2..3))
assert_not_operator((4..5), :overlap?, (2...4))

assert_not_operator((1..2), :overlap?, (3..4))
assert_not_operator((1...3), :overlap?, (3..4))
assert_not_operator((nil...3), :overlap?, (3..nil))
end if ((nil..1) rescue false)
end

0 comments on commit f6d55c5

Please sign in to comment.