def setup(self): """Init state.""" self.ram = create_autospec(RandomAccessMemory, True, True) self.registers = create_autospec(RegisterMemory, True, True) self.alu = create_autospec(ArithmeticLogicUnit, True, True) self.control_unit = AbstractControlUnit(self.registers, self.ram, self.alu, WORD_SIZE) assert self.control_unit.operand_size == WORD_SIZE
class TestAbstractControlUnit: """Test case for abstract control unit.""" ram = None registers = None alu = None control_unit = None def setup(self): """Init state.""" self.ram = create_autospec(RandomAccessMemory, True, True) self.registers = create_autospec(RegisterMemory, True, True) self.alu = create_autospec(ArithmeticLogicUnit, True, True) self.control_unit = AbstractControlUnit(self.registers, self.ram, self.alu, WORD_SIZE) assert self.control_unit.operand_size == WORD_SIZE def test_get_status(self): """Test halt interaction between ALU and CU.""" self.registers.fetch.return_value = 0 assert self.control_unit.get_status() == RUNNING self.registers.fetch.return_value = HALT assert self.control_unit.get_status() == HALTED def test_abstract_methods(self): """Abstract class.""" with raises(NotImplementedError): self.control_unit.fetch_and_decode() with raises(NotImplementedError): self.control_unit.load() with raises(NotImplementedError): self.control_unit.execute() with raises(NotImplementedError): self.control_unit.write_back() def test_step_and_run(self): """Test command execution.""" def do_nothing(): """Empty function.""" self.control_unit.fetch_and_decode = create_autospec(do_nothing) self.control_unit.load = create_autospec(do_nothing) self.control_unit.execute = create_autospec(do_nothing) self.control_unit.write_back = create_autospec(do_nothing) self.control_unit.step() self.control_unit.fetch_and_decode.assert_called_once_with() self.control_unit.load.assert_called_once_with() self.control_unit.execute.assert_called_once_with() self.control_unit.write_back.assert_called_once_with() self.control_unit.get_status = create_autospec(do_nothing) self.control_unit.get_status.return_value = HALTED self.control_unit.run() self.control_unit.get_status.assert_called_with() self.control_unit.fetch_and_decode.assert_called_once_with() self.control_unit.load.assert_called_once_with() self.control_unit.execute.assert_called_once_with() self.control_unit.write_back.assert_called_once_with()