am 6458fd96
: Merge "rangelib.py: Fix the bug in monotonic check."
* commit '6458fd96d5d0cd3b54a02a5f9c276f49e566cb53': rangelib.py: Fix the bug in monotonic check.
This commit is contained in:
commit
4988f18b1f
2 changed files with 60 additions and 4 deletions
|
@ -24,12 +24,13 @@ class RangeSet(object):
|
|||
lots of runs."""
|
||||
|
||||
def __init__(self, data=None):
|
||||
# TODO(tbao): monotonic is broken when passing in a tuple.
|
||||
self.monotonic = False
|
||||
if isinstance(data, str):
|
||||
self._parse_internal(data)
|
||||
elif data:
|
||||
assert len(data) % 2 == 0
|
||||
self.data = tuple(self._remove_pairs(data))
|
||||
self.monotonic = all(x < y for x, y in zip(self.data, self.data[1:]))
|
||||
else:
|
||||
self.data = ()
|
||||
|
||||
|
@ -39,8 +40,10 @@ class RangeSet(object):
|
|||
|
||||
def __eq__(self, other):
|
||||
return self.data == other.data
|
||||
|
||||
def __ne__(self, other):
|
||||
return self.data != other.data
|
||||
|
||||
def __nonzero__(self):
|
||||
return bool(self.data)
|
||||
|
||||
|
@ -74,9 +77,9 @@ class RangeSet(object):
|
|||
monotonic = True
|
||||
for p in text.split():
|
||||
if "-" in p:
|
||||
s, e = p.split("-")
|
||||
data.append(int(s))
|
||||
data.append(int(e)+1)
|
||||
s, e = (int(x) for x in p.split("-"))
|
||||
data.append(s)
|
||||
data.append(e+1)
|
||||
if last <= s <= e:
|
||||
last = e
|
||||
else:
|
||||
|
@ -95,6 +98,9 @@ class RangeSet(object):
|
|||
|
||||
@staticmethod
|
||||
def _remove_pairs(source):
|
||||
"""Remove consecutive duplicate items to simplify the result.
|
||||
|
||||
[1, 2, 2, 5, 5, 10] will become [1, 10]."""
|
||||
last = None
|
||||
for i in source:
|
||||
if i == last:
|
||||
|
@ -117,6 +123,7 @@ class RangeSet(object):
|
|||
return " ".join(out)
|
||||
|
||||
def to_string_raw(self):
|
||||
assert self.data
|
||||
return str(len(self.data)) + "," + ",".join(str(i) for i in self.data)
|
||||
|
||||
def union(self, other):
|
||||
|
|
|
@ -44,6 +44,7 @@ class RangeSetTest(unittest.TestCase):
|
|||
|
||||
def test_size(self):
|
||||
self.assertEqual(RangeSet("10-19 30-34").size(), 15)
|
||||
self.assertEqual(RangeSet("").size(), 0)
|
||||
|
||||
def test_map_within(self):
|
||||
self.assertEqual(RangeSet("0-9").map_within(RangeSet("3-4")),
|
||||
|
@ -74,3 +75,51 @@ class RangeSetTest(unittest.TestCase):
|
|||
self.assertEqual(RangeSet("10-19 30-39").extend(4), RangeSet("6-23 26-43"))
|
||||
self.assertEqual(RangeSet("10-19 30-39").extend(10), RangeSet("0-49"))
|
||||
|
||||
def test_equality(self):
|
||||
self.assertTrue(RangeSet("") == RangeSet(""))
|
||||
self.assertTrue(RangeSet("3") == RangeSet("3"))
|
||||
self.assertTrue(RangeSet("3 5") == RangeSet("5 3"))
|
||||
self.assertTrue(
|
||||
RangeSet("10-19 30-39") == RangeSet("30-32 10-14 33-39 15-19"))
|
||||
self.assertTrue(RangeSet("") != RangeSet("3"))
|
||||
self.assertTrue(RangeSet("10-19") != RangeSet("10-19 20"))
|
||||
|
||||
self.assertFalse(RangeSet(""))
|
||||
self.assertTrue(RangeSet("3"))
|
||||
|
||||
def test_init(self):
|
||||
self.assertIsNotNone(RangeSet(""))
|
||||
self.assertIsNotNone(RangeSet("3"))
|
||||
self.assertIsNotNone(RangeSet("3 5"))
|
||||
self.assertIsNotNone(RangeSet("10 19 30-39"))
|
||||
|
||||
with self.assertRaises(AssertionError):
|
||||
RangeSet(data=[0])
|
||||
|
||||
def test_str(self):
|
||||
self.assertEqual(str(RangeSet("0-9")), "0-9")
|
||||
self.assertEqual(str(RangeSet("2-10 12")), "2-10 12")
|
||||
self.assertEqual(str(RangeSet("11 2-10 12 1 0")), "0-12")
|
||||
self.assertEqual(str(RangeSet("")), "empty")
|
||||
|
||||
def test_to_string_raw(self):
|
||||
self.assertEqual(RangeSet("0-9").to_string_raw(), "2,0,10")
|
||||
self.assertEqual(RangeSet("2-10 12").to_string_raw(), "4,2,11,12,13")
|
||||
self.assertEqual(RangeSet("11 2-10 12 1 0").to_string_raw(), "2,0,13")
|
||||
|
||||
with self.assertRaises(AssertionError):
|
||||
RangeSet("").to_string_raw()
|
||||
|
||||
def test_monotonic(self):
|
||||
self.assertTrue(RangeSet("0-9").monotonic)
|
||||
self.assertTrue(RangeSet("2-9").monotonic)
|
||||
self.assertTrue(RangeSet("2-9 30 31 35").monotonic)
|
||||
self.assertTrue(RangeSet("").monotonic)
|
||||
self.assertTrue(RangeSet("0-4 5-9").monotonic)
|
||||
self.assertFalse(RangeSet("5-9 0-4").monotonic)
|
||||
|
||||
self.assertTrue(RangeSet(data=[0, 10]).monotonic)
|
||||
self.assertTrue(RangeSet(data=[0, 10, 15, 20]).monotonic)
|
||||
self.assertTrue(RangeSet(data=[2, 9, 30, 31, 31, 32, 35, 36]).monotonic)
|
||||
self.assertTrue(RangeSet(data=[0, 5, 5, 10]).monotonic)
|
||||
self.assertFalse(RangeSet(data=[5, 10, 0, 5]).monotonic)
|
||||
|
|
Loading…
Reference in a new issue