Exemplo n.º 1
0
def test_instantiating_zbyte():
    zb = ZByte(bytes.fromhex('12'))
    assert zb.bytes == b'\x12', 'Instantiate ZByte from bytes'

    zb = ZByte(memoryview(bytes.fromhex('42')))
    assert zb.bytes == b'\x42', 'Instantiate ZByte from memoryview'

    zb = ZByte.from_int(-1)
    assert zb.bytes == b'\xff', 'Instantiate ZByte from int'

    zb = ZByte.from_unsigned_int(0xf0)
    assert zb.bytes == b'\xf0', 'Instantiate ZByte from unsigned int'

    with pytest.raises(TypeError):
        ZByte(22)

    with pytest.raises(ValueError):
        ZByte(b'')

    with pytest.raises(ValueError):
        ZByte.from_int(-129)

    with pytest.raises(ValueError):
        ZByte.from_int(128)

    with pytest.raises(ValueError):
        ZByte.from_unsigned_int(-1)

    with pytest.raises(ValueError):
        ZByte.from_unsigned_int(256)
Exemplo n.º 2
0
def test_routine_calls(stack: ZMachineStack):
    ret_a = PC(1234)
    ret_var_a = ZByte.from_int(2)
    a_locals = [
        ZWord.from_int(1),
        ZWord.from_int(2),
        ZWord.from_int(3),
        ZByte.from_int(4)
    ]

    ret_b = PC(5678)
    ret_var_b = ZByte.from_unsigned_int(45)
    b_locals = [ZWord.from_int(42), ZWord.from_int(56)]

    stack.push_routine_call(ret_a, len(a_locals), ret_var_a, *a_locals)
    assert stack.local_var(0) == a_locals[0]
    assert stack.local_var(1) == a_locals[1]
    assert stack.local_var(2) == a_locals[2]
    stack.set_local_var(3, ZWord.from_int(34))
    assert stack.local_var(3) == ZWord.from_int(34)
    stack.push(ZWord.from_int(69))

    stack.push_routine_call(ret_b, len(b_locals), ret_var_b, *b_locals)
    assert stack.local_var(0) == b_locals[0]
    assert stack.local_var(1) == b_locals[1]
    stack.push(ZWord.from_int(342))
    ret_pc, ret_var = stack.pop_routine_call()
    assert ret_pc == ret_b
    assert ret_var == ret_var

    # Now ensure the previous call's variables are still correct
    assert stack.local_var(0) == a_locals[0]
    assert stack.local_var(1) == a_locals[1]
    assert stack.local_var(2) == a_locals[2]
    assert stack.local_var(3) == ZWord.from_int(34)
    assert stack.pop() == ZWord.from_int(69)

    ret_pc, ret_var = stack.pop_routine_call()
    assert ret_pc == ret_a
    assert ret_var == ret_var_a