@Test public void testMaxOrMinValue() throws Exception { TruffleVM.Symbol apply = findGlobalSymbol(applyNumbers()); Object res = apply.invoke(null, new MaxMinObject(true)); assert res instanceof Number : "result should be a number: " + res; Number n = (Number) res; assert 42 == n.intValue() : "32 > 18 and plus 10"; }
@Test public void testMaxOrMinValue2() throws Exception { TruffleVM.Symbol apply = findGlobalSymbol(applyNumbers()); Object res = apply.invoke(null, new MaxMinObject(false)); assert res instanceof Number : "result should be a number: " + res; Number n = (Number) res; assert 28 == n.intValue() : "18 < 32 and plus 10"; }
/** @since 0.8 or earlier */ @Test public void testPrimitiveReturnTypeFloat() throws Exception { PolyglotEngine.Value apply = findGlobalSymbol(applyNumbers()); float value = RANDOM.nextInt(1000) + RANDOM.nextFloat(); TruffleObject fn = JavaInterop.asTruffleFunction(ObjectBinaryOperation.class, new ConstantFunction(value)); Number n = apply.execute(fn).as(Number.class); assertDouble("The same value returned (" + value + " + 10): ", value + 10, n.floatValue()); }
/** @since 0.8 or earlier */ @Test public void testPrimitiveReturnTypeDouble() throws Exception { PolyglotEngine.Value apply = findGlobalSymbol(applyNumbers()); double value = RANDOM.nextInt(1000) + RANDOM.nextDouble(); TruffleObject fn = JavaInterop.asTruffleFunction(ObjectBinaryOperation.class, new ConstantFunction(value)); Number n = apply.execute(fn).as(Number.class); assertDouble("The same value returned (" + value + " + 10): ", value + 10, n.doubleValue()); }
/** @since 0.8 or earlier */ @Test public void testPrimitiveReturnTypeShort() throws Exception { PolyglotEngine.Value apply = findGlobalSymbol(applyNumbers()); short value = (short) RANDOM.nextInt(100); TruffleObject fn = JavaInterop.asTruffleFunction(ObjectBinaryOperation.class, new ConstantFunction(value)); Number n = apply.execute(fn).as(Number.class); assertEquals("The same value returned (" + value + " + 10): ", value + 10, n.shortValue()); }
/** @since 0.8 or earlier */ @Test public void testPrimitiveReturnTypeByte() throws Exception { PolyglotEngine.Value apply = findGlobalSymbol(applyNumbers()); byte value = (byte) RANDOM.nextInt(100); TruffleObject fn = JavaInterop.asTruffleFunction(ObjectBinaryOperation.class, new ConstantFunction(value)); Number n = apply.execute(fn).as(Number.class); assertEquals("The same value returned (" + value + " + 10): ", value + 10, n.byteValue()); }
/** @since 0.8 or earlier */ @Test public void testPrimitiveReturnTypeInt() throws Exception { PolyglotEngine.Value apply = findGlobalSymbol(applyNumbers()); int value = RANDOM.nextInt(100); TruffleObject fn = JavaInterop.asTruffleFunction(ObjectBinaryOperation.class, new ConstantFunction(value)); Number n = apply.execute(fn).as(Number.class); assertEquals("The same value returned (" + value + " + 10): ", value + 10, n.intValue()); }
/** @since 0.8 or earlier */ @Test public void testPrimitiveReturnTypeLong() throws Exception { PolyglotEngine.Value apply = findGlobalSymbol(applyNumbers()); long value = RANDOM.nextInt(1000); TruffleObject fn = JavaInterop.asTruffleFunction(ObjectBinaryOperation.class, new ConstantFunction(value)); Number n = apply.execute(fn).as(Number.class); assertEquals("The same value returned (" + value + " + 10): ", value + 10, n.longValue()); }
/** @since 0.8 or earlier */ @Test public void testMaxOrMinValue() throws Exception { PolyglotEngine.Value apply = findGlobalSymbol(applyNumbers()); TruffleObject fn = JavaInterop.asTruffleFunction(LongBinaryOperation.class, new MaxMinObject(true)); Object res = apply.execute(fn).get(); assert res instanceof Number : "result should be a number: " + res; Number n = (Number) res; assert 42 == n.intValue() : "32 > 18 and plus 10"; }
/** @since 0.8 or earlier */ @Test public void testMaxOrMinValue2() throws Exception { PolyglotEngine.Value apply = findGlobalSymbol(applyNumbers()); TruffleObject fn = JavaInterop.asTruffleFunction(LongBinaryOperation.class, new MaxMinObject(false)); final PolyglotEngine.Value result = apply.execute(fn); try { Boolean res = result.as(Boolean.class); fail("Cannot be converted to Boolean: " + res); } catch (ClassCastException ex) { // correct } Number n = result.as(Number.class); assert 28 == n.intValue() : "18 < 32 and plus 10"; }
/** @since 0.15 */ @Test public void testRootNodeName() throws Exception { final int[] haltCount = new int[1]; final String name = applyNumbers(); final String[] actualName = new String[1]; final PolyglotEngine engine = prepareVM(PolyglotEngine.newBuilder()); final PolyglotEngine.Value apply = engine.findGlobalSymbol(name); final int value = RANDOM.nextInt(100); final TruffleObject fn = JavaInterop.asTruffleFunction(ObjectBinaryOperation.class, new ConstantFunction(value)); try (DebuggerSession session = Debugger.find(engine).startSession(new SuspendedCallback() { public void onSuspend(SuspendedEvent ev) { actualName[0] = ev.getTopStackFrame().getName(); haltCount[0] = haltCount[0] + 1; } })) { session.suspendNextExecution(); apply.execute(fn).as(Number.class); } assertEquals(1, haltCount[0]); assertEquals(name, actualName[0]); }