示例#1
0
def test_comparisons():
    values = 43, 23
    for name, op in BOOLEAN_COMPARISONS.items():
        assert_incrementing_instruction(
            instruction=name,
            op_stack=[*(Integer.create_instance(v) for v in values)],
            expected=[Pop(2), Push(bool_to_num(op(*values)))])
示例#2
0
def test_conversions():
    for name, types in CONVERSION_DICT.items():
        source, target = types
        value = 3
        assert_incrementing_instruction(
            instruction=name,
            op_stack=[source.create_instance(value)],
            expected=[Pop(), Push(target.create_instance(value))])
示例#3
0
def _duplication_test(instruction, stack_string, amount_to_take,
                      index_for_insertion):
    assert_incrementing_instruction(
        instruction=instruction,
        op_stack=_translate_stack_string(stack_string),
        expected=[
            DuplicateTop(amount_to_take=amount_to_take,
                         index_for_insertion=index_for_insertion)
        ])
示例#4
0
def test_ldc():
    value = 5.0
    constants = ConstantPool()
    constant = constants.create_double(value)

    assert_incrementing_instruction(
        instruction=constant_instruction('ldc', constant),
        constants=constants,
        expected=[Push(Double.create_instance(value))])
示例#5
0
def test_int_load():
    locals_ = Locals(5)
    locals_.store(2, SOME_INT)
    assert_incrementing_instruction(
        instruction='iload_2',
        locals=locals_,
        expected=[
            Push(SOME_INT)
        ]
    )
示例#6
0
def test_int_store_with_built_in_index():
    assert_incrementing_instruction(
        instruction='istore_2',

        op_stack=[SOME_INT],

        expected=[
            StoreInLocals(index=2, value=SOME_INT),
            Pop
        ]
    )
示例#7
0
def test_int_load_from_array():
    array = ArrayReferenceType(Integer).create_instance([SOME_INT])
    index = Integer.create_instance(0)
    assert_incrementing_instruction(
        instruction='iaload',

        op_stack=[index, array],

        expected=[
            Pop(2),
            Push(SOME_INT)
        ]
    )
示例#8
0
def test_int_store():
    index = 1

    assert_incrementing_instruction(
        instruction=literal_instruction('istore', index),

        op_stack=[SOME_INT],

        expected=[
            StoreInLocals(value=SOME_INT, index=index),
            Pop,
        ]
    )
示例#9
0
def test_int_store_into_array():
    value = SOME_INT
    index = Integer.create_instance(1)
    array = ArrayReferenceType(Integer).create_instance([0])

    assert_incrementing_instruction(
        instruction='iastore',

        op_stack=[value, index, array],

        expected=[
            StoreIntoArray(array=array, index=index.value, value=value),
            Pop(3)
        ]
    )
示例#10
0
def test_string_constant():
    text = 'some_text'
    consts = ConstantPool()
    const = consts.create_string(text)

    chars = [ord(c) for c in text]
    char_array = ArrayReferenceType(Integer).create_instance(chars)
    hash_value = hash(text) % (2**32)
    hash_ = Integer.create_instance(hash_value)

    reference_type = ObjectReferenceType('java/lang/String')
    jvm_object = JvmObject({'hash': hash_, 'value': char_array})

    assert_incrementing_instruction(
        instruction=constant_instruction('ldc', const),
        constants=consts,
        expected=[Push(reference_type.create_instance(jvm_object))])
示例#11
0
文件: test_math.py 项目: ronyhe/pyjvm
def test_binary_math():
    left = 6
    right = 3
    for operator in OPERATORS:
        if not operator.operands == 2:
            continue
        for name, args in operator.bytecode_args().items():
            op, type_, _ = args
            assert_incrementing_instruction(
                instruction=name,
                op_stack=[
                    type_.create_instance(right),
                    type_.create_instance(left)
                ],
                expected=[
                    Pop(2),
                    Push(type_.create_instance(op(left, right)))
                ])
示例#12
0
文件: test_math.py 项目: ronyhe/pyjvm
def test_iinc():
    original_value = 8
    local_index = 2
    amount_to_add = 5

    locals_ = Locals(local_index + 1)
    locals_.store(local_index, Integer.create_instance(original_value))

    instruction = Instruction.create(
        'iinc', [local_operand(local_index),
                 literal_operand(amount_to_add)])

    assert_incrementing_instruction(
        instruction=instruction,
        locals=locals_,
        expected=[
            Push(Integer.create_instance(original_value + amount_to_add))
        ])
示例#13
0
def _constant_test(mnemonic, value):
    assert_incrementing_instruction(instruction=mnemonic,
                                    expected=[Push(value)])
示例#14
0
def test_bipush():
    value = 5
    assert_incrementing_instruction(
        instruction=literal_instruction('bipush', value),
        expected=[Push(Integer.create_instance(value))])
示例#15
0
def test_swap():
    first, second = _create_integers(2)
    assert_incrementing_instruction(
        instruction='swap',
        op_stack=[first, second],
        expected=[Pop(2), Push(first), Push(second)])
示例#16
0
def test_pop():
    assert_incrementing_instruction(instruction='pop',
                                    op_stack=[SOME_INT],
                                    expected=[Pop()])
示例#17
0
文件: test_math.py 项目: ronyhe/pyjvm
def test_neg():
    assert_incrementing_instruction(
        instruction='ineg',
        op_stack=[Integer.create_instance(3)],
        expected=[Pop(), Push(Integer.create_instance(-3))])