def test_autoregion(self): expected = [ hdtypes.Region(prefix=1, mask=0L, desired_f=3), hdtypes.Region(prefix=1, mask=0x8000000000000000, desired_f=3) ] returned = list((region + stringEnd).parseString("auto 1 3")) self.assertEqual(expected, returned)
def test_auto1(self): expected = [ hdtypes.Region(1, 0, 4), hdtypes.Region(1, 0x8000000000000000, 4) ] returned = _fill_to_region(0, 1, 4, 1 << 64) self.assertEqual(expected, returned)
def test_static_has_smaller_prefix(self): expected = [ hdtypes.Region(prefix=2, mask=0L, desired_f=3), hdtypes.Region(prefix=2, mask=0x4000000000000000, desired_f=3), hdtypes.Region(prefix=1, mask=0x8000000000000000, desired_f=2) ] returned = list( (region + stringEnd).parseString("region 1 0x8000000000000000 2 auto 2 3")) self.assertEqual(expected, returned)
def test_static_has_bigger_prefix_different_mask(self): expected = [ hdtypes.Region(prefix=1, mask=0L, desired_f=3), hdtypes.Region(prefix=2, mask=0x8000000000000000, desired_f=3), hdtypes.Region(prefix=2, mask=0xc000000000000000, desired_f=2) ] returned = list( (region + stringEnd).parseString("region 2 0xc000000000000000 2 auto 1 3")) self.assertEqual(expected, returned)
def test_static_with_bigger_target_prefix(self): expected = [ hdtypes.Region(3, 0x6000000000000000, 4), hdtypes.Region(3, 0x8000000000000000, 4), hdtypes.Region(5, 0xa000000000000000, 4), hdtypes.Region(7, 0xa800000000000000, 4) ] returned = _fill_to_region(0x6000000000000000, 3, 4, 0xaa00000000000000) self.assertEqual(expected, returned)
def test_static_with_bigger_upper_bound_prefix(self): expected = [ hdtypes.Region(8, 0x5500000000000000, 4), hdtypes.Region(7, 0x5600000000000000, 4), hdtypes.Region(5, 0x5800000000000000, 4), hdtypes.Region(3, 0x6000000000000000, 4) ] returned = _fill_to_region(0x5500000000000000, 3, 4, 0x8000000000000000) self.assertEqual(expected, returned)
def test_auto3(self): expected = [ hdtypes.Region(3, 0, 4), hdtypes.Region(3, 0x2000000000000000, 4), hdtypes.Region(3, 0x4000000000000000, 4), hdtypes.Region(3, 0x6000000000000000, 4), hdtypes.Region(3, 0x8000000000000000, 4), hdtypes.Region(3, 0xa000000000000000, 4), hdtypes.Region(3, 0xc000000000000000, 4), hdtypes.Region(3, 0xe000000000000000, 4) ] returned = _fill_to_region(0, 3, 4, 1 << 64) self.assertEqual(expected, returned)
def test_multiple_out_of_order_static_for_one_auto(self): expected = [ hdtypes.Region(prefix=3, mask=0L, desired_f=3), hdtypes.Region(prefix=4, mask=0x2000000000000000, desired_f=2), hdtypes.Region(prefix=4, mask=0x3000000000000000, desired_f=3), hdtypes.Region(prefix=4, mask=0x4000000000000000, desired_f=3), hdtypes.Region(prefix=4, mask=0x5000000000000000, desired_f=2), hdtypes.Region(prefix=3, mask=0x6000000000000000, desired_f=3), hdtypes.Region(prefix=2, mask=0x8000000000000000, desired_f=3), hdtypes.Region(prefix=2, mask=0xc000000000000000, desired_f=3) ] returned = list( (region + stringEnd).parseString("""region 4 0x5000000000000000 2 region 4 0x2000000000000000 2 auto 2 3""")) self.assertEqual(expected, returned)
def _fill_to_region(upper_bound, auto_prefix, auto_f, target): regions = [] while upper_bound < target: boundstr = bin(upper_bound)[2:] boundstr = '0' * (65 - len(boundstr)) + boundstr targetstr = bin(target)[2:] targetstr = '0' * (65 - len(targetstr)) + targetstr if targetstr[:auto_prefix + 1] == boundstr[:auto_prefix + 1] and \ targetstr.startswith(boundstr.rstrip('0')): # We need to build ever smaller increments to approach the target. index = boundstr.rfind('1') + 1 index = targetstr.find('1', index) assert (index >= 0) regions.append(hdtypes.Region(index, upper_bound, auto_f)) elif boundstr[auto_prefix + 1:].strip('0') == '': # We need to create an undivided automatic region. regions.append(hdtypes.Region(auto_prefix, upper_bound, auto_f)) else: # We need finish this divided automatic region. index = boundstr.rfind('1') assert (index >= 0) regions.append(hdtypes.Region(index, upper_bound, auto_f)) upper_bound = _upper_bound_of(regions[-1]) return regions
def parse_regions(regions): # Determine the automatic interval. auto_prefix = None auto_incREMENT = None auto_f = None end_idx = None if regions[-1][0] == "auto": if regions[-1][1] > 64 or regions[-1][1] < 0: raise ValueError("Regions must use use 0 <= prefix <= 64") auto_prefix = regions[-1][1] auto_increment = 1 << (64 - regions[-1][1]) auto_f = regions[-1][2] end_idx = -1 # Determine the static regions staticregions = [] for region in regions[:end_idx]: assert (region[0] in ("region", )) staticregions.append( hdtypes.Region(mask=region[2], prefix=region[1], desired_f=region[3])) staticregions.sort() regions = [] upper_bound = 0 for region in sorted(staticregions, key=lambda x: x.mask): if auto_prefix: regions += _fill_to_region(upper_bound, auto_prefix, auto_f, region.mask) if regions: upper_bound = _upper_bound_of(regions[-1]) if (upper_bound != region.mask): print upper_bound, region, auto_prefix, regions assert (upper_bound == region.mask) if upper_bound != region.mask: raise ValueError( 'Static regions without an "auto" statement must be contiguous' ) regions.append(region) upper_bound = _upper_bound_of(regions[-1]) regions += _fill_to_region(upper_bound, auto_prefix, auto_f, 1 << 64) return regions
def test_static_is_everything(self): expected = [hdtypes.Region(prefix=0, mask=0L, desired_f=2)]