@Test public void canCmplDoubleWithNaN() { initial.setRegisters(0, 0.5D, "D", 2, Double.NaN, "D"); expected.setRegisters(0, -1, "I"); VMTester.test(CLASS_NAME, "cmplDouble()V", initial, expected); }
public void setFields(String className, Object... params) { for (int i = 0; i < params.length; i += 2) { String fieldDescriptor = (String) params[i]; String type = fieldDescriptor.split(":")[1]; HeapItem item = new HeapItem(params[i + 1], type); setField(className, fieldDescriptor, item); } }
public static ExecutionGraphManipulator getGraphManipulator(String className, String methodSignature, Object... args) { VMState initial = new VMState(); if (args.length > 0) { initial.setRegisters(args); } return getGraphManipulator(className, methodSignature, initial); }
@Test public void invokeMethodOutsideClassThatAccessesThisClassReturnsExpectedValue() { String value = "i have been initialized"; initial.setFields(CLASS_NAME, "sometimes_initialized:Ljava/lang/String;", value); expected.setFields(CLASS_WITH_STATIC_INIT, "string:Ljava/lang/String;", "Uhhh, about 11, sir."); expected.setRegisters(MethodState.ResultRegister, value, "Ljava/lang/String;"); VMTester.test(CLASS_NAME, "invokeMethodOutsideClassThatAccessesThisClass()V", initial, expected); }
public void setRegisters(Object... params) { // register, value, type for (int i = 0; i < params.length; i += 3) { Integer register = (Integer) params[i]; String type = (String) params[i + 2]; HeapItem item = new HeapItem(params[i + 1], type); setRegister(register, item); } }
public static void testState(ExecutionGraph graph, VMState expectedState) { assertNotNull("Graph is null. Failed to execute method.", graph); testRegisterState(graph, expectedState.getRegisters()); testClassState(graph, expectedState.getFields()); }
@Test public void getStaticByte() { initial.setFields(CLASS_NAME, "myByte:B", (byte) 0xf); expected.setRegisters(0, (byte) 0xf, "B"); VMTester.test(CLASS_NAME, "getStaticByte()V", initial, expected); }
private static ExecutionGraphManipulator getOptimizedGraph(String methodName, Object... args) { VMState initial = new VMState(); if (args.length > 0) { initial.setRegisters(args); } return getOptimizedGraph(methodName, initial); }
public void setRegister(Integer register, Object value, String type) { HeapItem item = new HeapItem(value, type); setRegister(register, item); }
private static ExecutionContext buildInitializedContext(VirtualMachine vm, String methodSignature, VMState state) { VirtualMethod method = vm.getClassManager().getMethod(methodSignature); ExecutionContext context = vm.spawnRootContext(method); int registerCount = context.getMethodState().getRegisterCount(); setupMethodState(context, state.getRegisters(), registerCount); setupClassStates(context, vm, state.getFields()); return context; }
@Test public void canCmpgFloatWithLessThan() { initial.setRegisters(0, 0.5F, "F", 1, 20.5F, "F"); expected.setRegisters(0, -1, "I"); VMTester.test(CLASS_NAME, "cmpgFloat()V", initial, expected); }
@Test public void putStaticByte() { initial.setRegisters(0, (byte) 0xFF, "B"); expected.setFields(CLASS_NAME, "myByte:B", (byte) 0xFF); VMTester.test(CLASS_NAME, "putStaticByte()V", initial, expected); }
private static ExecutionGraphManipulator getOptimizedGraph(String methodName, Object... args) { VMState initial = new VMState(); if (args.length > 0) { initial.setRegisters(args); } ExecutionGraphManipulator manipulator = OptimizerTester.getGraphManipulator(CLASS_NAME, methodName, initial); UnreflectionStrategy strategy = new UnreflectionStrategy(manipulator); strategy.perform(); return manipulator; }
@Test @SuppressWarnings({ "unchecked" }) public void canReflectivelyInstantiateAnEnum() throws ClassNotFoundException { String className = "Lextends_enum;"; String methodDescriptor = "<init>(Ljava/lang/String;II)V"; String enumName = "NONE"; VirtualMachine vm = VMTester.spawnVM(); VirtualClass virtualClass = vm.getClassManager().getVirtualClass(className); VirtualMethod method = virtualClass.getMethod(methodDescriptor); int offset = method.getRegisterCount() - method.getParameterSize(); initial.setRegister(offset, new UninitializedInstance(virtualClass), className); initial.setRegister(offset + 1, enumName, CommonTypes.STRING); initial.setRegister(offset + 2, 0, CommonTypes.INTEGER); initial.setRegister(offset + 3, 0, CommonTypes.INTEGER); ExecutionGraph graph = VMTester.execute(vm, className, methodDescriptor, initial); HeapItem instance = graph.getTerminatingRegisterConsensus(0); Class<? extends Enum> klazz = (Class<? extends Enum>) vm.getClassLoader().loadClass(ClassNameUtils.internalToSource(className)); Object expectedValue = Enum.valueOf(klazz, enumName); assertEquals(expectedValue, instance.getValue()); }
@Test public void canCmplLongWithEqual() { initial.setRegisters(0, 0x100L, "J", 2, 0x100L, "J"); expected.setRegisters(0, 0, "I"); VMTester.test(CLASS_NAME, "cmpLong()V", initial, expected); }
@Test public void getStaticString() { initial.setFields(CLASS_NAME, "myString:Ljava/lang/String;", "They tried and died."); expected.setRegisters(0, "They tried and died.", "Ljava/lang/String;"); VMTester.test(CLASS_NAME, "getStaticString()V", initial, expected); }