Search in sources :

Example 41 with Value

use of org.graalvm.polyglot.Value in project graal by oracle.

the class ValueHostConversionTest method testStringInstantiaion.

@Test
public void testStringInstantiaion() {
    Value stringClass = context.asValue(String.class);
    assertEquals("", stringClass.newInstance().asString());
    assertEquals("foo", stringClass.newInstance("foo").asString());
}
Also used : ValueAssert.assertValue(com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue) Value(org.graalvm.polyglot.Value) Test(org.junit.Test)

Example 42 with Value

use of org.graalvm.polyglot.Value in project graal by oracle.

the class ValueHostConversionTest method testShortHierarchy.

@Test
public void testShortHierarchy() {
    Value hierarchy = context.asValue(new ShortHierarchy()).getMember("hierarchy");
    final short minValue = Short.MIN_VALUE;
    final short maxValue = Short.MAX_VALUE;
    assertEquals("short", hierarchy.execute(minValue).asString());
    assertEquals("short", hierarchy.execute(maxValue).asString());
    assertEquals("short", hierarchy.execute((int) minValue).asString());
    assertEquals("short", hierarchy.execute((int) maxValue).asString());
    assertEquals("short", hierarchy.execute((long) minValue).asString());
    assertEquals("short", hierarchy.execute((long) maxValue).asString());
    assertEquals("short", hierarchy.execute((float) minValue).asString());
    assertEquals("short", hierarchy.execute((float) maxValue).asString());
    assertEquals("short", hierarchy.execute((double) minValue).asString());
    assertEquals("short", hierarchy.execute((double) maxValue).asString());
    assertEquals("byte", hierarchy.execute(Byte.MIN_VALUE).asString());
    assertEquals("byte", hierarchy.execute(Byte.MAX_VALUE).asString());
    assertEquals("number", hierarchy.execute(Integer.MIN_VALUE).asString());
    assertEquals("number", hierarchy.execute(Integer.MAX_VALUE).asString());
    assertEquals("number", hierarchy.execute(Long.MIN_VALUE).asString());
    assertEquals("number", hierarchy.execute(Long.MAX_VALUE).asString());
    assertEquals("number", hierarchy.execute(Float.MIN_VALUE).asString());
    assertEquals("number", hierarchy.execute(Float.MAX_VALUE).asString());
    assertEquals("number", hierarchy.execute(Double.MIN_VALUE).asString());
    assertEquals("number", hierarchy.execute(Double.MAX_VALUE).asString());
    assertEquals("number", hierarchy.execute(new BigDecimal("1")).asString());
    assertEquals("object", hierarchy.execute("").asString());
    assertEquals("object", hierarchy.execute(false).asString());
}
Also used : ValueAssert.assertValue(com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue) Value(org.graalvm.polyglot.Value) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 43 with Value

use of org.graalvm.polyglot.Value in project graal by oracle.

the class ValueHostConversionTest method testFloatHierarchy.

@Test
public void testFloatHierarchy() {
    Value hierarchy = context.asValue(new FloatHierarchy()).getMember("hierarchy");
    final float minValue = Float.MIN_VALUE;
    final float maxValue = Float.MAX_VALUE;
    assertEquals("float", hierarchy.execute(minValue).asString());
    assertEquals("float", hierarchy.execute(maxValue).asString());
    assertEquals("float", hierarchy.execute(Float.NaN).asString());
    assertEquals("float", hierarchy.execute(Float.NEGATIVE_INFINITY).asString());
    assertEquals("float", hierarchy.execute(Float.POSITIVE_INFINITY).asString());
    assertEquals("float", hierarchy.execute(-0.0f).asString());
    assertEquals("float", hierarchy.execute((double) minValue).asString());
    assertEquals("float", hierarchy.execute((double) maxValue).asString());
    assertEquals("float", hierarchy.execute(Double.NaN).asString());
    assertEquals("float", hierarchy.execute(Double.NEGATIVE_INFINITY).asString());
    assertEquals("float", hierarchy.execute(Double.POSITIVE_INFINITY).asString());
    assertEquals("float", hierarchy.execute(-0.0d).asString());
    assertEquals("byte", hierarchy.execute(Byte.MIN_VALUE).asString());
    assertEquals("byte", hierarchy.execute(Byte.MAX_VALUE).asString());
    assertEquals("short", hierarchy.execute(Short.MIN_VALUE).asString());
    assertEquals("short", hierarchy.execute(Short.MAX_VALUE).asString());
    assertEquals("int", hierarchy.execute(Integer.MIN_VALUE).asString());
    assertEquals("int", hierarchy.execute(Integer.MAX_VALUE).asString());
    assertEquals("long", hierarchy.execute(Long.MIN_VALUE).asString());
    assertEquals("long", hierarchy.execute(Long.MAX_VALUE).asString());
    assertEquals("number", hierarchy.execute(Double.MIN_VALUE).asString());
    assertEquals("number", hierarchy.execute(Double.MAX_VALUE).asString());
    assertEquals("number", hierarchy.execute(new BigDecimal("1")).asString());
    assertEquals("object", hierarchy.execute("").asString());
    assertEquals("object", hierarchy.execute(false).asString());
}
Also used : ValueAssert.assertValue(com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue) Value(org.graalvm.polyglot.Value) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 44 with Value

use of org.graalvm.polyglot.Value in project graal by oracle.

the class ValueHostConversionTest method testAsFloat.

@Test
public void testAsFloat() {
    Number[] canConvert = { Byte.MIN_VALUE, Byte.MAX_VALUE, Short.MIN_VALUE, Short.MAX_VALUE, -(1 << 24 - 1), 1 << 24 - 1, -(1L << 24 - 1), 1L << 24 - 1, 0d, -0d, Double.NaN, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, -(Math.pow(2, 24) - 1), +(Math.pow(2, 24) - 1) };
    for (Number number : canConvert) {
        Value value = context.asValue(number);
        assertTrue(number.toString(), value.fitsInFloat());
        assertEquals(number.toString(), number.floatValue(), value.asFloat(), 0f);
    }
    Number[] cannotConvert = { Integer.MIN_VALUE, Integer.MAX_VALUE, -(1 << 24), 1 << 24, Long.MIN_VALUE, Long.MAX_VALUE, -(1L << 24), 1L << 24, Double.MIN_VALUE, Double.MAX_VALUE };
    for (Number number : cannotConvert) {
        ValueAssert.assertFails(() -> context.asValue(number).asFloat(), ClassCastException.class);
    }
}
Also used : ValueAssert.assertValue(com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue) Value(org.graalvm.polyglot.Value) Test(org.junit.Test)

Example 45 with Value

use of org.graalvm.polyglot.Value in project graal by oracle.

the class ValueHostConversionTest method testAsByte.

@Test
public void testAsByte() {
    Number[] canConvert = { Byte.MIN_VALUE, Byte.MAX_VALUE, (short) Byte.MIN_VALUE, (short) Byte.MAX_VALUE, (int) Byte.MIN_VALUE, (int) Byte.MAX_VALUE, (long) Byte.MIN_VALUE, (long) Byte.MAX_VALUE, 0d, (double) Byte.MIN_VALUE, (double) Byte.MAX_VALUE, 0f, (float) Byte.MIN_VALUE, (float) Byte.MAX_VALUE };
    for (Number number : canConvert) {
        Value value = context.asValue(number);
        assertTrue(number.toString(), value.fitsInByte());
        assertEquals(number.toString(), number.byteValue(), value.asByte());
    }
    Number[] cannotConvert = { Byte.MIN_VALUE - 1, Byte.MAX_VALUE + 1, Byte.MIN_VALUE - 1L, Byte.MAX_VALUE + 1L, -0d, Double.NaN, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, (double) (Byte.MIN_VALUE - 1), (double) (Byte.MAX_VALUE + 1), -0f, Float.NaN, Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY, (float) (Byte.MIN_VALUE - 1), (float) (Byte.MAX_VALUE + 1) };
    for (Number number : cannotConvert) {
        ValueAssert.assertFails(() -> context.asValue(number).asByte(), ClassCastException.class);
    }
}
Also used : ValueAssert.assertValue(com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue) Value(org.graalvm.polyglot.Value) Test(org.junit.Test)

Aggregations

Value (org.graalvm.polyglot.Value)277 Test (org.junit.Test)203 ValueAssert.assertValue (com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue)65 Context (org.graalvm.polyglot.Context)58 BoxedTestValue (com.oracle.truffle.llvm.test.interop.values.BoxedTestValue)43 PolyglotException (org.graalvm.polyglot.PolyglotException)42 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)34 ProxyObject (org.graalvm.polyglot.proxy.ProxyObject)34 Source (org.graalvm.polyglot.Source)31 ArrayList (java.util.ArrayList)30 CEntryPoint (org.graalvm.nativeimage.c.function.CEntryPoint)23 ProxyExecutable (org.graalvm.polyglot.proxy.ProxyExecutable)18 HashMap (java.util.HashMap)14 TruffleContext (com.oracle.truffle.api.TruffleContext)11 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)11 NullValue (com.oracle.truffle.llvm.test.interop.values.NullValue)11 UnsupportedTypeException (com.oracle.truffle.api.interop.UnsupportedTypeException)10 DebugValue (com.oracle.truffle.api.debug.DebugValue)9 LanguageContext (com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext)9 List (java.util.List)9