@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 testFortyTwo() throws Exception { TruffleVM.Symbol fourtyTwo = findGlobalSymbol(fourtyTwo()); Object res = fourtyTwo.invoke(null); assert res instanceof Number : "should yield a number, but was: " + res; Number n = (Number) res; assert 42 == n.intValue() : "The value is 42 = " + n.intValue(); }
@Test(expected = IOException.class) public void testInvalidTestMethod() throws Exception { String mime = mimeType(); String code = invalidCode(); Object ret = vm().eval(mime, code); fail("Should yield IOException, but returned " + ret); }
private void doPlusWithFloat(float a, float b) throws Exception { PolyglotEngine.Value plus = findGlobalSymbol(plus(float.class, float.class)); Number n = plus.execute(a, b).as(Number.class); assertDouble("Correct value computed: (" + a + " + " + b + ")", a + b, n.floatValue()); }
/** @since 0.8 or earlier */ @Test public void testPrimitiveidentityFloat() throws Exception { String id = identity(); if (id == null) { return; } PolyglotEngine.Value apply = findGlobalSymbol(id); float value = RANDOM.nextInt(1000) + RANDOM.nextFloat(); Number n = (Number) apply.execute(value).get(); assertDouble("The same value returned", value, n.floatValue()); }
/** @since 0.8 or earlier */ @Test public void testPrimitiveidentityByte() throws Exception { String id = identity(); if (id == null) { return; } PolyglotEngine.Value apply = findGlobalSymbol(id); byte value = (byte) RANDOM.nextInt(100); Number n = (Number) apply.execute(value).get(); assertEquals("The same value returned", value, n.byteValue()); }
/** @since 0.8 or earlier */ @Test public void testEvaluateSource() throws Exception { Language language = vm().getLanguages().get(mimeType()); assertNotNull("Language for " + mimeType() + " found", language); PolyglotEngine.Value function = vm().findGlobalSymbol(evaluateSource()); assertNotNull(evaluateSource() + " found", function); double expect = Math.floor(RANDOM.nextDouble() * 100000.0) / 10.0; Object parsed = function.execute("application/x-tck", "" + expect).get(); assertTrue("Expecting numeric result, was:" + expect, parsed instanceof Number); double value = ((Number) parsed).doubleValue(); assertDouble("Gets the double", expect, value); }
/** @since 0.8 or earlier */ @Test public void testGlobalObjectIsAccessible() throws Exception { String globalObjectFunction = globalObject(); if (globalObjectFunction == null) { return; } Language language = vm().getLanguages().get(mimeType()); assertNotNull("Language for " + mimeType() + " found", language); PolyglotEngine.Value function = vm().findGlobalSymbol(globalObjectFunction); Object global = function.execute().get(); assertEquals("Global from the language same with Java obtained one", language.getGlobalObject().get(), global); assertIsObjectOfLanguage(global); }
/** @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 testSumRealOfComplexNumbersAsStructuredDataColumnBased() throws Exception { String id = complexSumReal(); if (id == null) { return; } PolyglotEngine.Value apply = findGlobalSymbol(id); Schema schema = new Schema(3, false, Arrays.asList(ComplexNumber.REAL_IDENTIFIER, ComplexNumber.IMAGINARY_IDENTIFIER), Arrays.asList(Type.DOUBLE, Type.DOUBLE)); byte[] buffer = new byte[6 * Double.SIZE / Byte.SIZE]; putDoubles(buffer, new double[]{2, 30, 10, -1, -1, -1}); StructuredData numbers = new StructuredData(buffer, schema); Number n = (Number) apply.execute(numbers).get(); assertDouble("The same value returned", 42.0, n.doubleValue()); }
@Test public void testNull() throws Exception { if (getClass() == TruffleTCK.class) { return; } TruffleVM.Symbol retNull = findGlobalSymbol(returnsNull()); Object res = retNull.invoke(null); assertNull("Should yield real Java null", res); }
private void doPlusWithInts(int a, int b) throws Exception { PolyglotEngine.Value plus = findGlobalSymbol(plus(int.class, int.class)); Number n = plus.execute(a, b).as(Number.class); assert a + b == n.intValue() : "(" + a + " + " + b + ") = " + n.intValue(); }
/** @since 0.8 or earlier */ @Test public void readWriteCharValue() throws Exception { String id = valuesObject(); ValuesObject values = findGlobalSymbol(id).execute().as(ValuesObject.class); assertEquals("Zero", '0', values.charValue()); String letters = "P\u0159\u00EDli\u0161 \u017Elu\u0165ou\u010Dk\u00FD k\u016F\u0148 \u00FAp\u011Bl \u010F\u00E1belsk\u00E9 \u00F3dy"; final char value = letters.charAt(RANDOM.nextInt(letters.length())); values.charValue(value); assertEquals("Correct value", value, values.charValue()); }
private void assertIsObjectOfLanguage(Object obj) throws Exception { enterTCK(); // hack to ensure entered PolyglotRuntime.Instrument instr = vm().getRuntime().getInstruments().get(TckInstrument.ID); TruffleLanguage.Env env = TruffleTCKAccessor.engineAccess().getEnvForInstrument(instr, null, mimeType()); assertTrue(obj.toString(), TruffleTCKAccessor.langAccess().isObjectOfLanguage(env, obj)); }
/** @since 0.8 or earlier */ @Test public void testSumRealOfComplexNumbersB() throws Exception { String id = complexSumReal(); if (id == null) { return; } PolyglotEngine.Value apply = findGlobalSymbol(id); ComplexNumbersB numbers = new ComplexNumbersB(new double[]{2, 30, 10}, new double[]{-1, -1, -1}); Number n = (Number) apply.execute(numbers).get(); assertDouble("The same value returned", 42.0, n.doubleValue()); }
/** @since 0.8 or earlier */ @Test public void readWriteFloatValue() throws Exception { String id = valuesObject(); ValuesObject values = findGlobalSymbol(id).execute().as(ValuesObject.class); assertDouble("Zero", 0, values.floatValue()); final float value = RANDOM.nextFloat() * 1000.0f; values.floatValue(value); assertDouble("Correct value", value, values.floatValue()); }
/** @since 0.16 */ @Test public void testReadFromObjectWithElement() throws Exception { String id = objectWithElement(); if (id == null) { return; } PolyglotEngine.Value apply = findGlobalSymbol(id); TruffleObject truffleObject = (TruffleObject) apply.execute().get(); assertIsObjectOfLanguage(truffleObject); List<?> object = JavaInterop.asJavaObject(List.class, truffleObject); Assert.assertEquals(42.0, ((Number) object.get(2)).doubleValue(), 0.1); }
/** @since 0.16 */ @Test public void testIsExecutable() throws Exception { String id = functionAddNumbers(); if (id == null) { return; } PolyglotEngine.Value apply = findGlobalSymbol(id); TruffleObject truffleObject = (TruffleObject) apply.execute().get(); assertIsObjectOfLanguage(truffleObject); MessageInterface object = JavaInterop.asJavaObject(MessageInterface.class, truffleObject); Assert.assertEquals(true, object.isExecutable()); }
/** @since 0.16 */ @Test public void testReadFromObjectWithValueProperty() throws Exception { String id = objectWithValueProperty(); if (id == null) { return; } PolyglotEngine.Value apply = findGlobalSymbol(id); TruffleObject truffleObject = (TruffleObject) apply.execute().get(); assertIsObjectOfLanguage(truffleObject); ObjectWithValueInterface object = JavaInterop.asJavaObject(ObjectWithValueInterface.class, truffleObject); Assert.assertEquals(42.0, object.value(), 0.1); }
/** @since 0.8 or earlier */ @Test public void multiplyTwoVariables() throws Exception { final String firstVar = "var" + (char) ('A' + RANDOM.nextInt(24)); final String secondVar = "var" + (char) ('0' + RANDOM.nextInt(10)); String mulCode = multiplyCode(firstVar, secondVar); // @formatter:off Source source = Source.newBuilder("TCK42:" + mimeType() + ":" + mulCode). name("evaluate " + firstVar + " * " + secondVar). mimeType("application/x-tck"). build(); // @formatter:on final PolyglotEngine.Value evalSource = vm().eval(source); final PolyglotEngine.Value invokeMul = evalSource.execute(firstVar, secondVar); Object result = invokeMul.get(); assertTrue("Expecting numeric result, was:" + result + " for " + firstVar + " and " + secondVar, result instanceof Number); assertEquals("Right value for " + firstVar + " and " + secondVar, 42, ((Number) result).intValue()); }