From 16536a53a057778240b1a3a923b5dde31b79bd90 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Wed, 26 Aug 2015 17:07:14 -0700 Subject: [PATCH] DO NOT MERGE rangelib.py: Fix the bug in monotonic check. RangeSet("2-10").monotonic gives a wrong result of "False". Fix the bug and add more tests into test_rangelib.py. Bug: 28760775 Change-Id: I04780571b45ecafd34040f405756b9745a9e21a5 (cherry picked from commit e8f756137c39fc3292014f2eab5f84c6e5135797) --- tools/releasetools/rangelib.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/tools/releasetools/rangelib.py b/tools/releasetools/rangelib.py index ec735aba32..aa572cc334 100644 --- a/tools/releasetools/rangelib.py +++ b/tools/releasetools/rangelib.py @@ -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):