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)))])
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))])
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))])
def test_int_load(): locals_ = Locals(5) locals_.store(2, SOME_INT) assert_incrementing_instruction( instruction='iload_2', locals=locals_, expected=[ Push(SOME_INT) ] )
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) ] )
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))])
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)) ])
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))) ])
def _constant_test(mnemonic, value): assert_incrementing_instruction(instruction=mnemonic, expected=[Push(value)])
def test_bipush(): value = 5 assert_incrementing_instruction( instruction=literal_instruction('bipush', value), expected=[Push(Integer.create_instance(value))])
def test_swap(): first, second = _create_integers(2) assert_incrementing_instruction( instruction='swap', op_stack=[first, second], expected=[Pop(2), Push(first), Push(second)])
def test_push(): machine = complex_machine() machine.act(Push(SOME_INT)) assert machine.frames.peek().op_stack.peek() == SOME_INT
def test_neg(): assert_incrementing_instruction( instruction='ineg', op_stack=[Integer.create_instance(3)], expected=[Pop(), Push(Integer.create_instance(-3))])