def test_graphics(): memory = Memory() screen = FakeScreen(memory) cpu = Cpu(GRAPHICS, screen=screen, memory=memory) cpu.run_until_exit_or_halt() assert cpu.output == [] assert memory[210] == 254
def run_program(cpu: Cpu): try: while True: cpu.run_until_exit_or_halt() if cpu.has_exited: print(f"Program exited with code {cpu.exit_code}") return if cpu.halted: print(f"Program halted at {cpu.instruction_pointer}") print_debugger_help() while cpu.halted: choice = input("> ") if choice.startswith("1"): cpu.halted = False elif choice.startswith("2"): cpu.run_one_cycle() if cpu.has_exited: print(f"Program exited with code {cpu.exit_code}") return elif choice.startswith("3"): print(cpu.dump()) else: print_debugger_help() except Exception as e: raise Exception(f"Program crashed: {e}") from e
def test_breakpoint(): cpu = Cpu(BREAKPOINT) cpu.run_until_exit_or_halt() assert cpu.output == ["1"] assert cpu.halted cpu.halted = False cpu.run_until_exit_or_halt() assert cpu.output == ["1", "2"]
def test_fibonacci(): cpu = Cpu(FIBONACCI) cpu.run_until_exit_or_halt() # Fibonacci sequence loops around at 255 assert cpu.output == ["0", "1", "1", "2", "3", "5", "8", "13", "21", "34", "55", "89", "144", "233", "121", "98"]
def test_subroutine(): cpu = Cpu(SUBROUTINE) cpu.run_until_exit_or_halt() assert cpu.output == ["1", "2", "3"]
def test_add_two_args(): cpu = Cpu(ADD_TWO_ARGS, args=[38, 4]) cpu.run_until_exit_or_halt() assert cpu.output == ["42"]
def test_1337(): cpu = Cpu(PRINT_1337) cpu.run_until_exit_or_halt() assert cpu.output == ["1", "3", "3", "7"]