Beispiel #1
0
def test_vm_input():
    base_state = VmState(
        vm_code_buffer=io.BytesIO(gen_bytecode("INPUT r1")),
        vm_code_pointer=0,
    )

    with mock.patch('interpreter.src.virtual_machine.vm.io_ops.input') as inp:
        inp.side_effect = ['a', 1]
        state = vm_input(base_state)

    assert state.vm_code_pointer == 12
    assert state.vm_registers[0].value == 1

    base_state = VmState(
        vm_code_buffer=io.BytesIO(gen_bytecode("INPUT @r1")),
        vm_code_pointer=0,
    )

    with mock.patch('interpreter.src.virtual_machine.vm.io_ops.input') as inp:
        inp.side_effect = ['a', 1]
        state = vm_input(base_state)

    assert state.vm_code_pointer == 12
    mem_addr = state.vm_registers[0].value
    assert state.vm_memory[mem_addr] == 1
Beispiel #2
0
def test_execute():
    bcode_lines = [
        gen_bytecode("MOV r1, 3"),
        gen_bytecode("MOV r2, 3"),
        gen_bytecode("ADD r1, r2"),
    ]
    bcode = io.BytesIO(b"".join(bcode_lines))

    end_state = execute_bytecode(bcode)

    assert end_state.vm_code_pointer == 12 * 3
    assert end_state.vm_registers[0].value == 6
Beispiel #3
0
def test_vm_ret_error():
    base_state = VmState(
        vm_code_buffer=io.BytesIO(gen_bytecode("RET")),
        vm_code_pointer=0,
    )

    with pytest.raises(Exception):
        vm_ret(base_state)
Beispiel #4
0
def test_vm_nop():
    base_state = VmState(
        vm_code_buffer=io.BytesIO(gen_bytecode("NOP")),
        vm_code_pointer=0,
    )

    state = vm_nop(base_state)

    assert state.vm_code_pointer == 12
Beispiel #5
0
def test_vm_label():
    base_state = VmState(
        vm_code_buffer=io.BytesIO(gen_bytecode("LABEL main")),
        vm_code_pointer=0,
        vm_labels={2: 100500}
    )

    state = vm_label(base_state)

    assert state.vm_labels == {2: 100500, 1: 0}
Beispiel #6
0
def test_vm_end():
    bcode = b"".join(
        [gen_bytecode("MOV r1, 12"),
         gen_bytecode("END")]
    )
    base_state = VmState(
        vm_code_buffer=io.BytesIO(bcode),
        vm_code_pointer=12,
    )

    base_state.vm_code_buffer.seek(12)

    state = vm_end(base_state)

    last_code_addr = base_state.vm_code_pointer

    end_pointer = len(base_state.vm_code_buffer.read1())

    assert state.vm_code_pointer == (last_code_addr + end_pointer)
Beispiel #7
0
def test_vm_cmp():
    # In-place
    base_state = VmState(
        vm_code_buffer=io.BytesIO(gen_bytecode("CMP 3, 7")),
        vm_code_pointer=0,
    )

    state = vm_cmp(base_state)

    assert not state.vm_registers[5].value
    assert not state.vm_registers[7].value
    assert state.vm_registers[6]
    assert state.vm_registers[8]

    base_state = VmState(
        vm_code_buffer=io.BytesIO(gen_bytecode("CMP r1, r2")),
        vm_code_pointer=0,
    )
    base_state.vm_registers[0].value = 7
    base_state.vm_registers[1].value = 3

    state = vm_cmp(base_state)

    assert not state.vm_registers[5].value
    assert not state.vm_registers[6].value
    assert state.vm_registers[7].value
    assert state.vm_registers[8].value

    base_state = VmState(
        vm_code_buffer=io.BytesIO(gen_bytecode("CMP @r1, @r2")),
        vm_code_pointer=0,
    )

    base_state.vm_registers[0].value = 7
    base_state.vm_registers[1].value = 7

    state = vm_cmp(base_state)

    assert state.vm_registers[5].value
    assert not state.vm_registers[6].value
    assert not state.vm_registers[7].value
    assert not state.vm_registers[8].value
Beispiel #8
0
def test_generate_jump_bad_label():
    base_state = VmState(
        vm_code_buffer=io.BytesIO(gen_bytecode("JMP LABEL")),
        vm_code_pointer=0,
        vm_labels={2: 100500}
    )

    jmp_code = generate_jump("JMP", lambda x: True)

    with pytest.raises(Exception):
        jmp_code(base_state)
Beispiel #9
0
def test_vm_ret():
    base_state = VmState(
        vm_code_buffer=io.BytesIO(gen_bytecode("RET")),
        vm_code_pointer=0,
        vm_call_stack=[12, ]
    )

    state = vm_ret(base_state)

    assert state.vm_code_pointer == 12 + 12

    assert state.vm_call_stack == []
Beispiel #10
0
def test_vm_call():
    base_state = VmState(
        vm_code_buffer=io.BytesIO(gen_bytecode("CALL abc")),
        vm_code_pointer=0,
        vm_labels={1: 14}
    )

    state = vm_call(base_state)

    assert state.vm_code_pointer == 14+12

    assert state.vm_call_stack == [0, ]
Beispiel #11
0
def test_vm_print():
    base_state = VmState(
        vm_code_buffer=io.BytesIO(gen_bytecode("PRINT r1")),
        vm_code_pointer=0,
    )

    with mock.patch('interpreter.src.virtual_machine.vm.io_ops.print') as p:
        p.return_value = 1
        state = vm_print(base_state)

        p.assert_called_with("VM PRINT: 0")

    assert state.vm_code_pointer == 12

    base_state = VmState(
        vm_code_buffer=io.BytesIO(gen_bytecode("PRINT @r1")),
        vm_code_pointer=0,
    )

    with mock.patch('interpreter.src.virtual_machine.vm.io_ops.print') as p:
        p.return_value = 1
        state = vm_print(base_state)

        p.assert_called_with("VM PRINT: 0")

    assert state.vm_code_pointer == 12

    base_state = VmState(
        vm_code_buffer=io.BytesIO(gen_bytecode("PRINT 12")),
        vm_code_pointer=0,
    )

    with mock.patch('interpreter.src.virtual_machine.vm.io_ops.print') as p:
        p.return_value = 1
        state = vm_print(base_state)

        p.assert_called_with("VM PRINT: 12")

    assert state.vm_code_pointer == 12
Beispiel #12
0
def test_generate_jump():
    base_state = VmState(
        vm_code_buffer=io.BytesIO(gen_bytecode("JMP LABEL")),
        vm_code_pointer=0,
        vm_labels={1: 100500}
    )

    jmp_code = generate_jump("JMP", lambda x: True)

    assert jmp_code.__name__ == 'vm_jmp'

    state = jmp_code(base_state)

    assert state.vm_code_pointer == 100500 + 12
Beispiel #13
0
def test_vm_print_error():
    bcode = gen_bytecode("PRINT r1")

    op_code = struct.unpack('=hbibi', bcode)
    op_code = list(op_code)
    op_code[1] = 0

    bcode = struct.pack('=hbibi', *op_code)

    base_state = VmState(
        vm_code_buffer=io.BytesIO(bcode),
        vm_code_pointer=0,
    )

    with pytest.raises(Exception):
        vm_print(base_state)
Beispiel #14
0
def test_vm_cmp_error_2_arg():
    bcode = gen_bytecode("CMP r1, 11")

    op_code = struct.unpack('=hbibi', bcode)
    op_code = list(op_code)
    op_code[-2] = 0

    bcode = struct.pack('=hbibi', *op_code)

    base_state = VmState(
        vm_code_buffer=io.BytesIO(bcode),
        vm_code_pointer=0,
    )

    with pytest.raises(Exception):
        vm_cmp(base_state)
Beispiel #15
0
def test_vm_input_error():
    bcode = gen_bytecode("INPUT r1")

    op_code = struct.unpack('=hbibi', bcode)
    op_code = list(op_code)
    op_code[1] = 0

    bcode = struct.pack('=hbibi', *op_code)

    base_state = VmState(
        vm_code_buffer=io.BytesIO(bcode),
        vm_code_pointer=0,
    )

    with mock.patch('interpreter.src.virtual_machine.vm.io_ops.input') as inp:
        inp.side_effect = ['a', 1]
        with pytest.raises(Exception):
            vm_input(base_state)