Home | History | Annotate | Download | only in releasetools
      1 #
      2 # Copyright (C) 2015 The Android Open Source Project
      3 #
      4 # Licensed under the Apache License, Version 2.0 (the "License");
      5 # you may not use this file except in compliance with the License.
      6 # You may obtain a copy of the License at
      7 #
      8 #      http://www.apache.org/licenses/LICENSE-2.0
      9 #
     10 # Unless required by applicable law or agreed to in writing, software
     11 # distributed under the License is distributed on an "AS IS" BASIS,
     12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 # See the License for the specific language governing permissions and
     14 # limitations under the License.
     15 #
     16 
     17 import unittest
     18 
     19 from rangelib import RangeSet
     20 
     21 class RangeSetTest(unittest.TestCase):
     22 
     23   def test_union(self):
     24     self.assertEqual(RangeSet("10-19 30-34").union(RangeSet("18-29")),
     25                      RangeSet("10-34"))
     26     self.assertEqual(RangeSet("10-19 30-34").union(RangeSet("22 32")),
     27                      RangeSet("10-19 22 30-34"))
     28 
     29   def test_intersect(self):
     30     self.assertEqual(RangeSet("10-19 30-34").intersect(RangeSet("18-32")),
     31                      RangeSet("18-19 30-32"))
     32     self.assertEqual(RangeSet("10-19 30-34").intersect(RangeSet("22-28")),
     33                      RangeSet(""))
     34 
     35   def test_subtract(self):
     36     self.assertEqual(RangeSet("10-19 30-34").subtract(RangeSet("18-32")),
     37                      RangeSet("10-17 33-34"))
     38     self.assertEqual(RangeSet("10-19 30-34").subtract(RangeSet("22-28")),
     39                      RangeSet("10-19 30-34"))
     40 
     41   def test_overlaps(self):
     42     self.assertTrue(RangeSet("10-19 30-34").overlaps(RangeSet("18-32")))
     43     self.assertFalse(RangeSet("10-19 30-34").overlaps(RangeSet("22-28")))
     44 
     45   def test_size(self):
     46     self.assertEqual(RangeSet("10-19 30-34").size(), 15)
     47     self.assertEqual(RangeSet("").size(), 0)
     48 
     49   def test_map_within(self):
     50     self.assertEqual(RangeSet("0-9").map_within(RangeSet("3-4")),
     51                      RangeSet("3-4"))
     52     self.assertEqual(RangeSet("10-19").map_within(RangeSet("13-14")),
     53                      RangeSet("3-4"))
     54     self.assertEqual(
     55         RangeSet("10-19 30-39").map_within(RangeSet("17-19 30-32")),
     56         RangeSet("7-12"))
     57     self.assertEqual(
     58         RangeSet("10-19 30-39").map_within(RangeSet("12-13 17-19 30-32")),
     59         RangeSet("2-3 7-12"))
     60 
     61   def test_first(self):
     62     self.assertEqual(RangeSet("0-9").first(1), RangeSet("0"))
     63     self.assertEqual(RangeSet("10-19").first(5), RangeSet("10-14"))
     64     self.assertEqual(RangeSet("10-19").first(15), RangeSet("10-19"))
     65     self.assertEqual(RangeSet("10-19 30-39").first(3), RangeSet("10-12"))
     66     self.assertEqual(RangeSet("10-19 30-39").first(15),
     67                      RangeSet("10-19 30-34"))
     68     self.assertEqual(RangeSet("10-19 30-39").first(30),
     69                      RangeSet("10-19 30-39"))
     70     self.assertEqual(RangeSet("0-9").first(0), RangeSet(""))
     71 
     72   def test_extend(self):
     73     self.assertEqual(RangeSet("0-9").extend(1), RangeSet("0-10"))
     74     self.assertEqual(RangeSet("10-19").extend(15), RangeSet("0-34"))
     75     self.assertEqual(RangeSet("10-19 30-39").extend(4), RangeSet("6-23 26-43"))
     76     self.assertEqual(RangeSet("10-19 30-39").extend(10), RangeSet("0-49"))
     77 
     78   def test_equality(self):
     79     self.assertTrue(RangeSet("") == RangeSet(""))
     80     self.assertTrue(RangeSet("3") == RangeSet("3"))
     81     self.assertTrue(RangeSet("3 5") == RangeSet("5 3"))
     82     self.assertTrue(
     83         RangeSet("10-19 30-39") == RangeSet("30-32 10-14 33-39 15-19"))
     84     self.assertTrue(RangeSet("") != RangeSet("3"))
     85     self.assertTrue(RangeSet("10-19") != RangeSet("10-19 20"))
     86 
     87     self.assertFalse(RangeSet(""))
     88     self.assertTrue(RangeSet("3"))
     89 
     90   def test_init(self):
     91     self.assertIsNotNone(RangeSet(""))
     92     self.assertIsNotNone(RangeSet("3"))
     93     self.assertIsNotNone(RangeSet("3 5"))
     94     self.assertIsNotNone(RangeSet("10 19 30-39"))
     95 
     96     with self.assertRaises(AssertionError):
     97       RangeSet(data=[0])
     98 
     99   def test_str(self):
    100     self.assertEqual(str(RangeSet("0-9")), "0-9")
    101     self.assertEqual(str(RangeSet("2-10 12")), "2-10 12")
    102     self.assertEqual(str(RangeSet("11 2-10 12 1 0")), "0-12")
    103     self.assertEqual(str(RangeSet("")), "empty")
    104 
    105   def test_to_string_raw(self):
    106     self.assertEqual(RangeSet("0-9").to_string_raw(), "2,0,10")
    107     self.assertEqual(RangeSet("2-10 12").to_string_raw(), "4,2,11,12,13")
    108     self.assertEqual(RangeSet("11 2-10 12 1 0").to_string_raw(), "2,0,13")
    109 
    110     with self.assertRaises(AssertionError):
    111       RangeSet("").to_string_raw()
    112 
    113   def test_monotonic(self):
    114     self.assertTrue(RangeSet("0-9").monotonic)
    115     self.assertTrue(RangeSet("2-9").monotonic)
    116     self.assertTrue(RangeSet("2-9 30 31 35").monotonic)
    117     self.assertTrue(RangeSet("").monotonic)
    118     self.assertTrue(RangeSet("0-4 5-9").monotonic)
    119     self.assertFalse(RangeSet("5-9 0-4").monotonic)
    120     self.assertFalse(RangeSet("258768-259211 196604").monotonic)
    121 
    122     self.assertTrue(RangeSet(data=[0, 10]).monotonic)
    123     self.assertTrue(RangeSet(data=[0, 10, 15, 20]).monotonic)
    124     self.assertTrue(RangeSet(data=[2, 9, 30, 31, 31, 32, 35, 36]).monotonic)
    125     self.assertTrue(RangeSet(data=[0, 5, 5, 10]).monotonic)
    126     self.assertFalse(RangeSet(data=[5, 10, 0, 5]).monotonic)
    127