def test_field_get(): lines = [['>', 'v'], ['^', '<']] fld = Field(2, 2, lines) assert fld.get_symbol_at(0, 0) == '>' assert fld.get_symbol_at(1, 0) == 'v' assert fld.get_symbol_at(0, 1) == '^' assert fld.get_symbol_at(1, 1) == '<'
def test_executor_p(): lines = [['>', 'v'], ['<', '^']] fld = Field(2, 2, lines) stk = Stack() crt = Caret(stk, fld) stk.push(ord('@')) stk.push(1) stk.push(0) exec_p(crt) assert fld.get_symbol_at(1, 0) == '@'
def test_load_file(): fld = Field.load_file("tests/fld_test_program.txt") assert fld.width == 11 assert fld.height == 5 assert fld.get_symbol_at(0, 0) == ' ' assert fld.get_symbol_at(10, 0) == 'v' assert fld.get_symbol_at(10, 4) == '<' assert fld.get_symbol_at(0, 4) == '^'
def test_executor_g(): lines = [['>', 'v'], ['<', '^']] fld = Field(2, 2, lines) stk = Stack() crt = Caret(stk, fld) stk.push(1) stk.push(0) exec_g(crt) assert stk.peek() == ord('v')
def test_caret_move_behind_top_border(): lines = [['>', 'v'], ['<', '^']] fld = Field(2, 2, lines) stk = Stack() crt = Caret(stk, fld) crt.set_direction(Up) crt.move(fld) assert crt.direction == Up assert crt.pos == Vec(0, 1)
def test_caret_move_first(): lines = [['>', 'v'], ['<', '^']] fld = Field(2, 2, lines) stk = Stack() crt = Caret(stk, fld) crt.move(fld) crt.read_instruction(fld) assert crt.direction == Vec(0, 0) assert crt.current_instruction == '>'
def test_field_print(): try: field = Field.load_file("tests/fld_test_program.txt") stack = Stack() caret = Caret(stack, field, True) term = Terminal() runner.print_field(caret, field, term) except Exception as e: raise e
def test_field_set(): lines = [['>', 'v'], ['<', '^']] fld = Field(2, 2, lines) fld.set_symbol_at(0, 0, 'v') fld.set_symbol_at(1, 0, '<') fld.set_symbol_at(1, 1, '^') fld.set_symbol_at(0, 1, '>') assert fld.get_symbol_at(0, 0) == 'v' assert fld.get_symbol_at(1, 0) == '<' assert fld.get_symbol_at(0, 1) == '>' assert fld.get_symbol_at(1, 1) == '^'
def test_caret_move(): lines = [['>', 'v'], ['<', '^']] fld = Field(2, 2, lines) stk = Stack() crt = Caret(stk, fld) crt.set_direction(Right) crt.move(fld) crt.read_instruction(fld) assert crt.direction == Right assert crt.current_instruction == 'v'
def test_field_init(): lines = [['>', 'v'], ['<', '^']] try: fld = Field(2, 2, lines) except Exception as e: raise e else: assert fld is not None assert fld.width == 2 assert fld.height == 2
def test_caret_switch_string_mode(): lines = [['>', 'v'], ['<', '^']] fld = Field(2, 2, lines) stk = Stack() crt = Caret(stk, fld) assert not crt.string_mode crt.switch_string_mode() assert crt.string_mode crt.switch_string_mode() assert not crt.string_mode
def test_caret_changes_direction(): lines = [['>', 'v'], ['<', '^']] fld = Field(2, 2, lines) stk = Stack() crt = Caret(stk, fld) crt.set_direction(Down) assert crt.direction == Down crt.set_direction(Left) assert crt.direction == Left crt.set_direction(Right) assert crt.direction == Right crt.set_direction(Up) assert crt.direction == Up
def test_caret_init(): lines = [['>', 'v'], ['<', '^']] fld = Field(2, 2, lines) stk = Stack() try: crt = Caret(stk, fld) except Exception as e: raise e else: assert crt is not None assert crt.pos is not None assert crt.string_mode is False assert crt.field is fld assert crt.direction == Vec(0, 0)
def test_vertical_if(): lines = [['|']] fld = Field(1, 1, lines) stk = Stack() crt = Caret(stk, fld) stk.push(174) crt.read_instruction(fld) crt.execute_instruction() assert crt.direction == Up stk.push(0) crt.read_instruction(fld) crt.execute_instruction() assert crt.direction == Down
def test_horizontal_if(): lines = [['_']] fld = Field(1, 1, lines) stk = Stack() crt = Caret(stk, fld) stk.push(174) crt.read_instruction(fld) crt.execute_instruction() assert crt.direction == Left stk.push(0) crt.read_instruction(fld) crt.execute_instruction() assert crt.direction == Right
def test_trampoline(): lines = [['>', '#', '1', '.']] fld = Field(4, 1, lines) stk = Stack() crt = Caret(stk, fld) crt.read_instruction(fld) crt.execute_instruction() assert crt.direction == Right crt.move(fld) crt.read_instruction(fld) assert crt.current_instruction == '#' crt.execute_instruction() crt.move(fld) crt.read_instruction(fld) assert crt.current_instruction == '.'
def test_change_direction(): lines = [['>', 'v'], ['^', '<']] fld = Field(2, 2, lines) stk = Stack() crt = Caret(stk, fld) crt.move(fld) crt.read_instruction(fld) crt.execute_instruction() assert crt.direction == Right crt.move(fld) crt.read_instruction(fld) crt.execute_instruction() assert crt.direction == Down crt.move(fld) crt.read_instruction(fld) crt.execute_instruction() assert crt.direction == Left crt.move(fld) crt.read_instruction(fld) crt.execute_instruction() assert crt.direction == Up crt.move(fld) assert crt.pos == Vec(0, 0)
def main(execute=True): term = Terminal() if from_file: field = Field.load_file(filename) elif from_pipe: lines = sys.stdin.readlines() text = lines[0] for line in lines: text += line program = text field = Field.from_text(program) else: return stack = Stack() caret = Caret(stack, field, max(3, to_int(term.height) - field.height), debug) if debug: logger.set_output(caret) caret.executor.execute = execute logger.debug("Objects created") # if super_debug: # input("Continue?") if debug: try: char = readchar() if char == 'c': print('\nForced exit') return except Exception: print("You shouldn't use pipe without -p option") return # field height shouldn't change print_field(caret, field, term) caret.read_instruction(field) caret.execute_instruction() if caret.direction == Vec(0, 0): caret.direction = Right logger.debug("Starting loop") while caret.executor.execute: caret.move(field) caret.read_instruction(field) if debug or super_debug: print_field(caret, field, term) else: print(caret.diff, end='') caret.execute_instruction() if debug: char = readchar() if char == 'c': print('\nForced exit') return logger.debug("Move performed") print()