Search in sources :

Example 6 with TypeDescriptor

use of org.graalvm.polyglot.tck.TypeDescriptor in project graal by oracle.

the class IdentityFunctionTest method createIdentitySnippet.

static Snippet createIdentitySnippet(String lang) {
    LanguageProvider tli = context.getInstalledProviders().get(lang);
    Value value = tli.createIdentityFunction(context.getContext());
    if (!value.canExecute()) {
        throw new AssertionError(String.format("Result of createIdentityFunction for tck provider %s did not return an executable value. Returned value '%s'.", lang, value));
    }
    return (Snippet.newBuilder("identity", tli.createIdentityFunction(context.getContext()), ANY).parameterTypes(ANY).resultVerifier(new ResultVerifier() {

        public void accept(SnippetRun snippetRun) throws PolyglotException {
            final PolyglotException exception = snippetRun.getException();
            if (exception != null) {
                throw exception;
            }
            Value parameter = snippetRun.getParameters().get(0);
            TypeDescriptor parameterType = TypeDescriptor.forValue(parameter);
            TypeDescriptor resultType = TypeDescriptor.forValue(snippetRun.getResult());
            if (!resultType.isAssignable(parameterType) || !resultType.isAssignable(resultType)) {
                throw new AssertionError(String.format("Identity function return type must parameter type. Expected %s got %s.", parameterType, resultType));
            }
        }
    }).build());
}
Also used : LanguageProvider(org.graalvm.polyglot.tck.LanguageProvider) ResultVerifier(org.graalvm.polyglot.tck.ResultVerifier) TypeDescriptor(org.graalvm.polyglot.tck.TypeDescriptor) Value(org.graalvm.polyglot.Value) PolyglotException(org.graalvm.polyglot.PolyglotException)

Example 7 with TypeDescriptor

use of org.graalvm.polyglot.tck.TypeDescriptor in project graal by oracle.

the class TypeDescriptorTest method testAny.

@Test
public void testAny() {
    Assert.assertTrue(TypeDescriptor.ARRAY.isAssignable(TypeDescriptor.array(TypeDescriptor.ANY)));
    Assert.assertTrue(TypeDescriptor.array(TypeDescriptor.ANY).isAssignable(TypeDescriptor.ARRAY));
    Assert.assertFalse(TypeDescriptor.EXECUTABLE.isAssignable(TypeDescriptor.ANY));
    Assert.assertTrue(TypeDescriptor.ANY.isAssignable(TypeDescriptor.EXECUTABLE));
    Assert.assertFalse(TypeDescriptor.executable(TypeDescriptor.ANY).isAssignable(TypeDescriptor.ANY));
    Assert.assertTrue(TypeDescriptor.ANY.isAssignable(TypeDescriptor.executable(TypeDescriptor.ANY)));
    Assert.assertTrue(TypeDescriptor.ANY.isAssignable(TypeDescriptor.executable(TypeDescriptor.ANY, TypeDescriptor.STRING, TypeDescriptor.NUMBER)));
    Assert.assertTrue(TypeDescriptor.EXECUTABLE.isAssignable(TypeDescriptor.executable(TypeDescriptor.ANY)));
    Assert.assertTrue(TypeDescriptor.executable(TypeDescriptor.ANY).isAssignable(TypeDescriptor.EXECUTABLE));
    for (TypeDescriptor td : PREDEFINED) {
        Assert.assertTrue(TypeDescriptor.ANY.isAssignable(td));
        Assert.assertFalse(td.isAssignable(TypeDescriptor.ANY));
    }
    Assert.assertTrue(TypeDescriptor.ANY.isAssignable(TypeDescriptor.ANY));
    final TypeDescriptor union = TypeDescriptor.union(PREDEFINED);
    Assert.assertTrue(TypeDescriptor.ANY.isAssignable(union));
    Assert.assertFalse(union.isAssignable(TypeDescriptor.ANY));
    final TypeDescriptor unionWithAny = TypeDescriptor.union(union, TypeDescriptor.ANY);
    Assert.assertTrue(unionWithAny.isAssignable(TypeDescriptor.ANY));
    final TypeDescriptor intersection = TypeDescriptor.intersection(PREDEFINED);
    Assert.assertTrue(TypeDescriptor.ANY.isAssignable(intersection));
    Assert.assertFalse(intersection.isAssignable(TypeDescriptor.ANY));
    final TypeDescriptor arrayNum = TypeDescriptor.array(TypeDescriptor.NUMBER);
    final TypeDescriptor arrayAny = TypeDescriptor.array(TypeDescriptor.ANY);
    Assert.assertTrue(TypeDescriptor.ANY.isAssignable(arrayNum));
    Assert.assertTrue(TypeDescriptor.ANY.isAssignable(arrayAny));
    Assert.assertFalse(arrayNum.isAssignable(TypeDescriptor.ANY));
    Assert.assertFalse(arrayAny.isAssignable(TypeDescriptor.ANY));
    Assert.assertTrue(arrayAny.isAssignable(arrayNum));
    Assert.assertFalse(arrayNum.isAssignable(arrayAny));
}
Also used : TypeDescriptor(org.graalvm.polyglot.tck.TypeDescriptor) Test(org.junit.Test)

Example 8 with TypeDescriptor

use of org.graalvm.polyglot.tck.TypeDescriptor in project graal by oracle.

the class TypeDescriptorTest method testUnion.

@Test
public void testUnion() {
    final TypeDescriptor numOrBool = TypeDescriptor.union(TypeDescriptor.NUMBER, TypeDescriptor.BOOLEAN);
    final TypeDescriptor numOrBoolOrStr = TypeDescriptor.union(numOrBool, TypeDescriptor.STRING);
    for (TypeDescriptor td : PREDEFINED) {
        Assert.assertFalse(td != TypeDescriptor.NUMBER && td != TypeDescriptor.BOOLEAN && td.isAssignable(numOrBool));
        Assert.assertFalse(td != TypeDescriptor.NUMBER && td != TypeDescriptor.BOOLEAN && numOrBool.isAssignable(td));
    }
    Assert.assertTrue(numOrBool.isAssignable(TypeDescriptor.BOOLEAN));
    Assert.assertTrue(numOrBoolOrStr.isAssignable(TypeDescriptor.BOOLEAN));
    Assert.assertFalse(TypeDescriptor.BOOLEAN.isAssignable(numOrBool));
    Assert.assertFalse(TypeDescriptor.BOOLEAN.isAssignable(numOrBoolOrStr));
    Assert.assertTrue(numOrBool.isAssignable(TypeDescriptor.NUMBER));
    Assert.assertTrue(numOrBoolOrStr.isAssignable(TypeDescriptor.NUMBER));
    Assert.assertFalse(TypeDescriptor.NUMBER.isAssignable(numOrBool));
    Assert.assertFalse(TypeDescriptor.NUMBER.isAssignable(numOrBoolOrStr));
    Assert.assertTrue(numOrBoolOrStr.isAssignable(TypeDescriptor.STRING));
    Assert.assertFalse(TypeDescriptor.STRING.isAssignable(numOrBoolOrStr));
    Assert.assertTrue(numOrBoolOrStr.isAssignable(numOrBool));
    Assert.assertFalse(numOrBool.isAssignable(numOrBoolOrStr));
    final TypeDescriptor arrNumberOrBool = TypeDescriptor.union(TypeDescriptor.array(TypeDescriptor.NUMBER), TypeDescriptor.BOOLEAN);
    final TypeDescriptor arrNumberOrString = TypeDescriptor.union(TypeDescriptor.array(TypeDescriptor.NUMBER), TypeDescriptor.STRING);
    final TypeDescriptor arrBoolOrString = TypeDescriptor.union(TypeDescriptor.array(TypeDescriptor.BOOLEAN), TypeDescriptor.STRING);
    final TypeDescriptor arrNumberOrBoolOrStr = TypeDescriptor.union(TypeDescriptor.array(TypeDescriptor.NUMBER), TypeDescriptor.BOOLEAN, TypeDescriptor.STRING);
    Assert.assertFalse(arrNumberOrBool.isAssignable(arrNumberOrString));
    Assert.assertFalse(arrNumberOrBool.isAssignable(arrBoolOrString));
    Assert.assertTrue(arrNumberOrBoolOrStr.isAssignable(arrNumberOrString));
    final TypeDescriptor arrNumBool = TypeDescriptor.array(numOrBool);
    final TypeDescriptor arrNum = TypeDescriptor.array(TypeDescriptor.NUMBER);
    final TypeDescriptor numOrBoolOrArrNumBool = TypeDescriptor.union(numOrBool, arrNumBool);
    Assert.assertTrue(numOrBoolOrArrNumBool.isAssignable(arrNum));
    final TypeDescriptor objOrArrNum = TypeDescriptor.union(TypeDescriptor.OBJECT, arrNum);
    final TypeDescriptor boolOrArrNum = TypeDescriptor.union(TypeDescriptor.BOOLEAN, arrNum);
    Assert.assertFalse(numOrBoolOrArrNumBool.isAssignable(objOrArrNum));
    Assert.assertTrue(numOrBoolOrArrNumBool.isAssignable(boolOrArrNum));
}
Also used : TypeDescriptor(org.graalvm.polyglot.tck.TypeDescriptor) Test(org.junit.Test)

Example 9 with TypeDescriptor

use of org.graalvm.polyglot.tck.TypeDescriptor in project graal by oracle.

the class TypeDescriptorTest method testIntersection.

@Test
public void testIntersection() {
    final TypeDescriptor strAndObj = TypeDescriptor.intersection(TypeDescriptor.STRING, TypeDescriptor.OBJECT);
    final TypeDescriptor strAndNum = TypeDescriptor.intersection(TypeDescriptor.STRING, TypeDescriptor.NUMBER);
    final TypeDescriptor strAndNumAndObj = TypeDescriptor.intersection(strAndObj, strAndNum);
    Assert.assertTrue(strAndObj.isAssignable(strAndObj));
    Assert.assertFalse(strAndObj.isAssignable(strAndNum));
    Assert.assertTrue(strAndObj.isAssignable(strAndNumAndObj));
    Assert.assertTrue(strAndNum.isAssignable(strAndNum));
    Assert.assertFalse(strAndNum.isAssignable(strAndObj));
    Assert.assertTrue(strAndNum.isAssignable(strAndNumAndObj));
    Assert.assertTrue(strAndNumAndObj.isAssignable(strAndNumAndObj));
    Assert.assertFalse(strAndNumAndObj.isAssignable(strAndNum));
    Assert.assertFalse(strAndNumAndObj.isAssignable(strAndObj));
    for (TypeDescriptor predefined : PREDEFINED) {
        Assert.assertFalse(strAndNum.isAssignable(predefined));
        Assert.assertFalse(strAndObj.isAssignable(predefined));
        Assert.assertFalse(strAndNumAndObj.isAssignable(predefined));
    }
    Assert.assertFalse(TypeDescriptor.ARRAY.isAssignable(strAndNum));
    Assert.assertFalse(TypeDescriptor.ARRAY.isAssignable(strAndObj));
    Assert.assertFalse(TypeDescriptor.ARRAY.isAssignable(strAndNumAndObj));
    Assert.assertFalse(TypeDescriptor.BOOLEAN.isAssignable(strAndNum));
    Assert.assertFalse(TypeDescriptor.BOOLEAN.isAssignable(strAndObj));
    Assert.assertFalse(TypeDescriptor.BOOLEAN.isAssignable(strAndNumAndObj));
    Assert.assertFalse(TypeDescriptor.HOST_OBJECT.isAssignable(strAndNum));
    Assert.assertFalse(TypeDescriptor.HOST_OBJECT.isAssignable(strAndObj));
    Assert.assertFalse(TypeDescriptor.HOST_OBJECT.isAssignable(strAndNumAndObj));
    Assert.assertFalse(TypeDescriptor.NATIVE_POINTER.isAssignable(strAndNum));
    Assert.assertFalse(TypeDescriptor.NATIVE_POINTER.isAssignable(strAndObj));
    Assert.assertFalse(TypeDescriptor.NATIVE_POINTER.isAssignable(strAndNumAndObj));
    Assert.assertFalse(TypeDescriptor.NULL.isAssignable(strAndNum));
    Assert.assertFalse(TypeDescriptor.NULL.isAssignable(strAndObj));
    Assert.assertFalse(TypeDescriptor.NULL.isAssignable(strAndNumAndObj));
    Assert.assertTrue(TypeDescriptor.NUMBER.isAssignable(strAndNum));
    Assert.assertFalse(TypeDescriptor.NUMBER.isAssignable(strAndObj));
    Assert.assertTrue(TypeDescriptor.NUMBER.isAssignable(strAndNumAndObj));
    Assert.assertFalse(TypeDescriptor.OBJECT.isAssignable(strAndNum));
    Assert.assertTrue(TypeDescriptor.OBJECT.isAssignable(strAndObj));
    Assert.assertTrue(TypeDescriptor.OBJECT.isAssignable(strAndNumAndObj));
    Assert.assertTrue(TypeDescriptor.STRING.isAssignable(strAndNum));
    Assert.assertTrue(TypeDescriptor.STRING.isAssignable(strAndObj));
    Assert.assertTrue(TypeDescriptor.STRING.isAssignable(strAndNumAndObj));
    final TypeDescriptor boolOrNum = TypeDescriptor.union(TypeDescriptor.BOOLEAN, TypeDescriptor.NUMBER);
    final TypeDescriptor strOrNum = TypeDescriptor.union(TypeDescriptor.STRING, TypeDescriptor.NUMBER);
    Assert.assertFalse(strAndNum.isAssignable(boolOrNum));
    Assert.assertFalse(strAndNum.isAssignable(strOrNum));
    Assert.assertTrue(boolOrNum.isAssignable(strAndNum));
    Assert.assertTrue(strOrNum.isAssignable(strAndNum));
    final TypeDescriptor product = TypeDescriptor.intersection(boolOrNum, strOrNum);
    // Should be [number | [boolean & number] | [boolean & string] | [string & number]]
    Assert.assertTrue(product.equals(TypeDescriptor.union(TypeDescriptor.NUMBER, TypeDescriptor.intersection(TypeDescriptor.BOOLEAN, TypeDescriptor.NUMBER), TypeDescriptor.intersection(TypeDescriptor.BOOLEAN, TypeDescriptor.STRING), TypeDescriptor.intersection(TypeDescriptor.NUMBER, TypeDescriptor.STRING))));
    Assert.assertTrue(product.isAssignable(strAndNum));
    Assert.assertFalse(product.isAssignable(strAndObj));
    Assert.assertTrue(product.isAssignable(strAndNumAndObj));
    Assert.assertFalse(strAndNum.isAssignable(product));
    Assert.assertFalse(strAndObj.isAssignable(product));
    Assert.assertFalse(strAndNumAndObj.isAssignable(product));
    final TypeDescriptor numAndArrNum = TypeDescriptor.intersection(TypeDescriptor.NUMBER, TypeDescriptor.array(TypeDescriptor.NUMBER));
    Assert.assertFalse(numAndArrNum.isAssignable(TypeDescriptor.NUMBER));
    Assert.assertFalse(numAndArrNum.isAssignable(TypeDescriptor.array(TypeDescriptor.NUMBER)));
    Assert.assertTrue(TypeDescriptor.NUMBER.isAssignable(numAndArrNum));
    Assert.assertTrue(TypeDescriptor.array(TypeDescriptor.NUMBER).isAssignable(numAndArrNum));
    Assert.assertTrue(TypeDescriptor.union(TypeDescriptor.array(TypeDescriptor.NUMBER), TypeDescriptor.OBJECT).isAssignable(numAndArrNum));
    final TypeDescriptor arrAndArrNum = TypeDescriptor.intersection(TypeDescriptor.ARRAY, TypeDescriptor.array(TypeDescriptor.NUMBER));
    Assert.assertTrue(arrAndArrNum.isAssignable(TypeDescriptor.ARRAY));
    Assert.assertTrue(arrAndArrNum.isAssignable(TypeDescriptor.array(TypeDescriptor.NUMBER)));
    Assert.assertTrue(TypeDescriptor.ARRAY.isAssignable(arrAndArrNum));
    Assert.assertFalse(TypeDescriptor.array(TypeDescriptor.NUMBER).isAssignable(arrAndArrNum));
    final TypeDescriptor numAndStr = TypeDescriptor.intersection(TypeDescriptor.NUMBER, TypeDescriptor.STRING);
    final TypeDescriptor numAndStrAndBool = TypeDescriptor.intersection(TypeDescriptor.NUMBER, TypeDescriptor.STRING, TypeDescriptor.BOOLEAN);
    final TypeDescriptor numAndStrAndObj = TypeDescriptor.intersection(TypeDescriptor.NUMBER, TypeDescriptor.STRING, TypeDescriptor.OBJECT);
    Assert.assertTrue(numAndStr.isAssignable(TypeDescriptor.union(numAndStrAndBool, numAndStrAndObj)));
}
Also used : TypeDescriptor(org.graalvm.polyglot.tck.TypeDescriptor) Test(org.junit.Test)

Aggregations

TypeDescriptor (org.graalvm.polyglot.tck.TypeDescriptor)9 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)3 AbstractMap (java.util.AbstractMap)2 Map (java.util.Map)2 HashMap (java.util.HashMap)1 List (java.util.List)1 PolyglotException (org.graalvm.polyglot.PolyglotException)1 Value (org.graalvm.polyglot.Value)1 LanguageProvider (org.graalvm.polyglot.tck.LanguageProvider)1 ResultVerifier (org.graalvm.polyglot.tck.ResultVerifier)1 Snippet (org.graalvm.polyglot.tck.Snippet)1