Skip to content

Instantly share code, notes, and snippets.

@jikkujose
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jikkujose/9f136adbf994ef9884e7 to your computer and use it in GitHub Desktop.
Save jikkujose/9f136adbf994ef9884e7 to your computer and use it in GitHub Desktop.
require 'minitest'
# require 'minitest/reporters'
# Minitest::Reporters.use! [Minitest::Reporters::DefaultReporter.new({color: true})]
class Range
include Comparable
def <=>(other)
self.begin <=> other.begin
end
def self.overlap?(*ranges)
edges = ranges.sort.flat_map { |range| [range.begin, range.end] }
edges != edges.sort.uniq
end
end
class TestRangeCompare < Minitest::Test
PROVIDED_RANGES = [39600..82800, 39600..70200, 70200..80480].shuffle
OVERLAPPING_RANGES = [2..12, 6..36, 42..96].shuffle
NON_OVERLAPPING_RANGES = [2..12, 16..36, 42..96].shuffle
def test_overlap
assert_equal true, Range.overlap?(*OVERLAPPING_RANGES)
assert_equal false, Range.overlap?(*NON_OVERLAPPING_RANGES)
assert_equal true, Range.overlap?(*PROVIDED_RANGES)
end
end
Minitest.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment