Ejemplo n.º 1
0
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')
Ejemplo n.º 2
0
def test_stack_swap():
    stk = Stack()
    stk.push(2)
    stk.push(3)
    stk.swap()
    assert stk.peek() == 2
    assert stk.pop() == 2
    assert stk.peek() == 3
    assert stk.pop() == 3
Ejemplo n.º 3
0
def test_stack_push_pop():
    stk = Stack()
    stk.push(2)
    stk.push(3)
    assert stk.peek() == 3
    assert stk.pop() == 3
    assert stk.peek() == 2
    assert stk.pop() == 2
Ejemplo n.º 4
0
def test_stack_init():
    try:
        stk = Stack()
    except Exception as e:
        raise e
    else:
        assert stk is not None
Ejemplo n.º 5
0
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 == '>'
Ejemplo n.º 6
0
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)
Ejemplo n.º 7
0
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
Ejemplo n.º 8
0
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'
Ejemplo n.º 9
0
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
Ejemplo n.º 10
0
def test_stack_to_string():
    stk = Stack()
    assert str(stk) == "[ ]"
    stk.push(2)
    assert str(stk) == "[2]"
    stk.push(3)
    assert str(stk) == "[3, 2]"
Ejemplo n.º 11
0
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
Ejemplo n.º 12
0
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)
Ejemplo n.º 13
0
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) == '@'
Ejemplo n.º 14
0
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 == '.'
Ejemplo n.º 15
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
Ejemplo n.º 16
0
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
Ejemplo n.º 17
0
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)
Ejemplo n.º 18
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()