class Memory(SpecialSetMemory): def __init__(self): super().__init__() self.mask = BitMask("X" * 36) def __setitem__(self, index, value): masked_value = self.mask.apply(value) if masked_value < 0 or masked_value >= 2 ** 36: raise ValueError(f"{value} not valid.") self.set_at_raw(index, masked_value)
def test_setting_single_bit_to_1(): bitmask = BitMask("XXXX1") my_int = 2 assert 3 == bitmask.apply(my_int) bitmask = BitMask("1XXXX") my_int = 2 assert 18 == bitmask.apply(my_int)
def __init__(self): super().__init__() self.mask = BitMask("X" * 36)
def test_memory_applies_mask(): mem = Memory() mem.mask = BitMask("XX01") mem[3] = 6 # 6 = 110. With mask becomes 101 = 5 assert 5 == mem[3]
def test_repr(): bitmask = BitMask("X11X001") assert repr(bitmask) == 'BitMask("X11X001")'
def test_general_setting(): bitmask = BitMask("XXX01") assert 1 == bitmask.apply(2)
def test_setting_single_but_to_0(): bitmask = BitMask("XXX0X") assert 1 == bitmask.apply(3)
def test_runner_calls_mask(): memory = DummyMemory() runner = Runner(memory) runner.execute("mask = XX10X") assert BitMask("XX10X") == memory.mask