Search in sources :

Example 1 with TypeDescriptor

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

the class TestUtil method findApplicableParameters.

static List<List<Map.Entry<String, ? extends Snippet>>> findApplicableParameters(final Snippet operator, final Collection<Map.Entry<String, ? extends Snippet>> valueConstructors) {
    List<? extends TypeDescriptor> opParameterTypes = operator.getParameterTypes();
    final List<List<Map.Entry<String, ? extends Snippet>>> params = new ArrayList<>(opParameterTypes.size());
    for (int i = 0; i < opParameterTypes.size(); i++) {
        params.add(new ArrayList<>());
        final TypeDescriptor paramTypeDesc = opParameterTypes.get(i);
        for (Map.Entry<String, ? extends Snippet> constructor : valueConstructors) {
            if (paramTypeDesc.isAssignable(constructor.getValue().getReturnType())) {
                params.get(i).add(constructor);
            }
        }
    }
    return params;
}
Also used : TypeDescriptor(org.graalvm.polyglot.tck.TypeDescriptor) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) AbstractMap(java.util.AbstractMap) Map(java.util.Map)

Example 2 with TypeDescriptor

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

the class TypeDescriptorTest method testCreate.

@Test
public void testCreate() {
    TypeDescriptor t = TypeDescriptor.union(TypeDescriptor.array(TypeDescriptor.STRING), TypeDescriptor.array(TypeDescriptor.NUMBER));
    Assert.assertEquals(TypeDescriptor.array(TypeDescriptor.union(TypeDescriptor.STRING, TypeDescriptor.NUMBER)), t);
    t = TypeDescriptor.union(TypeDescriptor.NUMBER, TypeDescriptor.array(TypeDescriptor.STRING), TypeDescriptor.array(TypeDescriptor.NUMBER));
    Assert.assertEquals(TypeDescriptor.union(TypeDescriptor.NUMBER, TypeDescriptor.array(TypeDescriptor.union(TypeDescriptor.STRING, TypeDescriptor.NUMBER))), t);
    t = TypeDescriptor.union(TypeDescriptor.ARRAY, TypeDescriptor.array(TypeDescriptor.NUMBER));
    Assert.assertEquals(TypeDescriptor.ARRAY, t);
}
Also used : TypeDescriptor(org.graalvm.polyglot.tck.TypeDescriptor) Test(org.junit.Test)

Example 3 with TypeDescriptor

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

the class TypeDescriptorTest method testExecutable.

@Test
public void testExecutable() {
    final TypeDescriptor exeBottom = TypeDescriptor.EXECUTABLE;
    final TypeDescriptor exeTop = TypeDescriptor.EXECUTABLE_ANY;
    final TypeDescriptor exeAnyNoArgs = TypeDescriptor.executable(TypeDescriptor.ANY);
    final TypeDescriptor exeAnyStr = TypeDescriptor.executable(TypeDescriptor.ANY, TypeDescriptor.STRING);
    final TypeDescriptor exeAnyStrNum = TypeDescriptor.executable(TypeDescriptor.ANY, TypeDescriptor.STRING, TypeDescriptor.NUMBER);
    final TypeDescriptor exeStrNoArgs = TypeDescriptor.executable(TypeDescriptor.STRING);
    final TypeDescriptor exeStrStr = TypeDescriptor.executable(TypeDescriptor.STRING, TypeDescriptor.STRING);
    final TypeDescriptor exeAnyUnionUnion = TypeDescriptor.executable(TypeDescriptor.ANY, TypeDescriptor.union(TypeDescriptor.NUMBER, TypeDescriptor.STRING), TypeDescriptor.union(TypeDescriptor.NUMBER, TypeDescriptor.OBJECT));
    final List<TypeDescriptor> eds = new ArrayList<>();
    Collections.addAll(eds, exeBottom, exeAnyNoArgs, exeAnyStr, exeAnyStrNum, exeStrNoArgs, exeStrStr, exeAnyUnionUnion);
    final List<TypeDescriptor> otherTypes = new ArrayList<>();
    Collections.addAll(otherTypes, PREDEFINED);
    otherTypes.remove(TypeDescriptor.EXECUTABLE);
    otherTypes.remove(TypeDescriptor.EXECUTABLE_ANY);
    otherTypes.add(TypeDescriptor.array(TypeDescriptor.BOOLEAN));
    otherTypes.add(TypeDescriptor.union(TypeDescriptor.BOOLEAN, TypeDescriptor.NUMBER));
    for (TypeDescriptor td : otherTypes) {
        for (TypeDescriptor ed : eds) {
            Assert.assertFalse(ed.isAssignable(td));
            Assert.assertFalse(td.isAssignable(ed));
        }
    }
    Assert.assertTrue(exeTop.isAssignable(exeTop));
    Assert.assertTrue(exeTop.isAssignable(exeBottom));
    Assert.assertTrue(exeTop.isAssignable(exeAnyNoArgs));
    Assert.assertTrue(exeTop.isAssignable(exeAnyStr));
    Assert.assertTrue(exeTop.isAssignable(exeAnyStrNum));
    Assert.assertTrue(exeTop.isAssignable(exeStrNoArgs));
    Assert.assertTrue(exeTop.isAssignable(exeStrStr));
    Assert.assertTrue(exeTop.isAssignable(exeAnyUnionUnion));
    Assert.assertFalse(exeBottom.isAssignable(exeTop));
    Assert.assertTrue(exeBottom.isAssignable(exeAnyNoArgs));
    Assert.assertFalse(exeBottom.isAssignable(exeAnyStr));
    Assert.assertFalse(exeBottom.isAssignable(exeAnyStrNum));
    Assert.assertTrue(exeBottom.isAssignable(exeStrNoArgs));
    Assert.assertFalse(exeBottom.isAssignable(exeStrStr));
    Assert.assertFalse(exeBottom.isAssignable(exeAnyUnionUnion));
    Assert.assertFalse(exeAnyNoArgs.isAssignable(exeTop));
    Assert.assertTrue(exeAnyNoArgs.isAssignable(exeBottom));
    Assert.assertFalse(exeAnyNoArgs.isAssignable(exeAnyStr));
    Assert.assertFalse(exeAnyNoArgs.isAssignable(exeAnyStrNum));
    Assert.assertTrue(exeAnyNoArgs.isAssignable(exeStrNoArgs));
    Assert.assertFalse(exeAnyNoArgs.isAssignable(exeStrStr));
    Assert.assertFalse(exeAnyNoArgs.isAssignable(exeAnyUnionUnion));
    Assert.assertFalse(exeAnyStr.isAssignable(exeTop));
    Assert.assertTrue(exeAnyStr.isAssignable(exeBottom));
    Assert.assertTrue(exeAnyStr.isAssignable(exeAnyNoArgs));
    Assert.assertFalse(exeAnyStr.isAssignable(exeAnyStrNum));
    Assert.assertTrue(exeAnyStr.isAssignable(exeStrNoArgs));
    Assert.assertTrue(exeAnyStr.isAssignable(exeStrStr));
    Assert.assertFalse(exeAnyStr.isAssignable(exeAnyUnionUnion));
    Assert.assertFalse(exeAnyStrNum.isAssignable(exeTop));
    Assert.assertTrue(exeAnyStrNum.isAssignable(exeBottom));
    Assert.assertTrue(exeAnyStrNum.isAssignable(exeAnyNoArgs));
    Assert.assertTrue(exeAnyStrNum.isAssignable(exeAnyStr));
    Assert.assertTrue(exeAnyStrNum.isAssignable(exeStrNoArgs));
    Assert.assertTrue(exeAnyStrNum.isAssignable(exeStrStr));
    Assert.assertTrue(exeAnyStrNum.isAssignable(exeAnyUnionUnion));
    Assert.assertFalse(exeStrNoArgs.isAssignable(exeTop));
    Assert.assertFalse(exeStrNoArgs.isAssignable(exeBottom));
    Assert.assertFalse(exeStrNoArgs.isAssignable(exeAnyNoArgs));
    Assert.assertFalse(exeStrNoArgs.isAssignable(exeAnyStr));
    Assert.assertFalse(exeStrNoArgs.isAssignable(exeAnyStrNum));
    Assert.assertFalse(exeStrNoArgs.isAssignable(exeStrStr));
    Assert.assertFalse(exeStrNoArgs.isAssignable(exeAnyUnionUnion));
    Assert.assertFalse(exeStrStr.isAssignable(exeTop));
    Assert.assertFalse(exeStrStr.isAssignable(exeBottom));
    Assert.assertFalse(exeStrStr.isAssignable(exeAnyNoArgs));
    Assert.assertFalse(exeStrStr.isAssignable(exeAnyStr));
    Assert.assertFalse(exeStrStr.isAssignable(exeAnyStrNum));
    Assert.assertTrue(exeStrStr.isAssignable(exeStrNoArgs));
    Assert.assertFalse(exeStrStr.isAssignable(exeAnyUnionUnion));
    Assert.assertFalse(exeAnyUnionUnion.isAssignable(exeTop));
    Assert.assertTrue(exeAnyUnionUnion.isAssignable(exeBottom));
    Assert.assertTrue(exeAnyUnionUnion.isAssignable(exeAnyNoArgs));
    Assert.assertFalse(exeAnyUnionUnion.isAssignable(exeAnyStr));
    Assert.assertFalse(exeAnyUnionUnion.isAssignable(exeAnyStrNum));
    Assert.assertTrue(exeAnyUnionUnion.isAssignable(exeStrNoArgs));
    Assert.assertFalse(exeAnyUnionUnion.isAssignable(exeStrStr));
    // Arrays
    final TypeDescriptor ae1 = TypeDescriptor.array(TypeDescriptor.EXECUTABLE);
    final TypeDescriptor ae2 = TypeDescriptor.array(TypeDescriptor.executable(TypeDescriptor.ANY, TypeDescriptor.BOOLEAN));
    final TypeDescriptor ae3 = TypeDescriptor.array(TypeDescriptor.EXECUTABLE_ANY);
    final TypeDescriptor ab = TypeDescriptor.array(TypeDescriptor.BOOLEAN);
    Assert.assertFalse(ae1.isAssignable(ae2));
    Assert.assertFalse(ae1.isAssignable(ae3));
    Assert.assertFalse(ae1.isAssignable(ab));
    Assert.assertTrue(ae2.isAssignable(ae1));
    Assert.assertFalse(ae2.isAssignable(ae3));
    Assert.assertFalse(ae2.isAssignable(ab));
    Assert.assertTrue(ae3.isAssignable(ae1));
    Assert.assertTrue(ae3.isAssignable(ae2));
    Assert.assertFalse(ae3.isAssignable(ab));
    // Unions
    final TypeDescriptor ue1 = TypeDescriptor.union(TypeDescriptor.EXECUTABLE, TypeDescriptor.OBJECT);
    final TypeDescriptor ue2 = TypeDescriptor.union(TypeDescriptor.executable(TypeDescriptor.ANY, TypeDescriptor.BOOLEAN), TypeDescriptor.STRING);
    final TypeDescriptor ue3 = TypeDescriptor.union(TypeDescriptor.executable(TypeDescriptor.ANY, TypeDescriptor.BOOLEAN), TypeDescriptor.STRING, TypeDescriptor.OBJECT);
    final TypeDescriptor ue4 = TypeDescriptor.union(TypeDescriptor.EXECUTABLE_ANY, TypeDescriptor.STRING, TypeDescriptor.OBJECT);
    final TypeDescriptor up = TypeDescriptor.union(TypeDescriptor.BOOLEAN, TypeDescriptor.NUMBER);
    Assert.assertFalse(ue1.isAssignable(ue2));
    Assert.assertFalse(ue1.isAssignable(ue3));
    Assert.assertFalse(ue1.isAssignable(up));
    Assert.assertFalse(ue1.isAssignable(ue4));
    Assert.assertFalse(ue2.isAssignable(ue1));
    Assert.assertFalse(ue2.isAssignable(ue3));
    Assert.assertFalse(ue2.isAssignable(up));
    Assert.assertFalse(ue2.isAssignable(ue4));
    Assert.assertTrue(ue3.isAssignable(ue1));
    Assert.assertTrue(ue3.isAssignable(ue2));
    Assert.assertFalse(ue3.isAssignable(up));
    Assert.assertFalse(ue3.isAssignable(ue4));
    Assert.assertTrue(ue4.isAssignable(ue1));
    Assert.assertTrue(ue4.isAssignable(ue2));
    Assert.assertTrue(ue4.isAssignable(ue3));
    Assert.assertFalse(ue4.isAssignable(up));
    // strictParameterCount
    final TypeDescriptor exeStrictAnyAny = TypeDescriptor.executable(TypeDescriptor.ANY, false, TypeDescriptor.ANY);
    final TypeDescriptor exeAnyNum = TypeDescriptor.executable(TypeDescriptor.ANY, TypeDescriptor.NUMBER);
    final TypeDescriptor exeAnyNumNum = TypeDescriptor.executable(TypeDescriptor.ANY, TypeDescriptor.NUMBER, TypeDescriptor.NUMBER);
    Assert.assertTrue(exeAnyNum.isAssignable(exeStrictAnyAny));
    Assert.assertFalse(exeAnyNumNum.isAssignable(exeStrictAnyAny));
}
Also used : TypeDescriptor(org.graalvm.polyglot.tck.TypeDescriptor) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 4 with TypeDescriptor

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

the class TypeDescriptorTest method testArray.

@Test
public void testArray() {
    final TypeDescriptor numArray = TypeDescriptor.array(TypeDescriptor.NUMBER);
    final TypeDescriptor strArray = TypeDescriptor.array(TypeDescriptor.STRING);
    final TypeDescriptor numArrayArray = TypeDescriptor.array(TypeDescriptor.array(TypeDescriptor.NUMBER));
    for (TypeDescriptor td : PREDEFINED) {
        Assert.assertFalse(numArray.isAssignable(td));
        Assert.assertFalse(strArray.isAssignable(td));
        Assert.assertFalse(numArrayArray.isAssignable(td));
    }
    for (TypeDescriptor td : PREDEFINED) {
        Assert.assertFalse(td != TypeDescriptor.ARRAY && td.isAssignable(numArray));
        Assert.assertFalse(td != TypeDescriptor.ARRAY && td.isAssignable(strArray));
        Assert.assertFalse(td != TypeDescriptor.ARRAY && td.isAssignable(numArrayArray));
    }
    Assert.assertTrue(TypeDescriptor.ARRAY.isAssignable(numArray));
    Assert.assertTrue(TypeDescriptor.ARRAY.isAssignable(strArray));
    Assert.assertTrue(TypeDescriptor.ARRAY.isAssignable(numArrayArray));
    Assert.assertFalse(numArray.isAssignable(strArray));
    Assert.assertFalse(numArray.isAssignable(numArrayArray));
    Assert.assertFalse(strArray.isAssignable(numArray));
    Assert.assertFalse(strArray.isAssignable(numArrayArray));
    Assert.assertFalse(numArrayArray.isAssignable(numArray));
    Assert.assertFalse(numArrayArray.isAssignable(strArray));
    Assert.assertTrue(numArray.isAssignable(numArray));
    Assert.assertTrue(strArray.isAssignable(strArray));
    Assert.assertTrue(numArrayArray.isAssignable(numArrayArray));
    final TypeDescriptor objOrArrayNum = TypeDescriptor.union(TypeDescriptor.OBJECT, numArray);
    Assert.assertFalse(numArray.isAssignable(objOrArrayNum));
    Assert.assertTrue(objOrArrayNum.isAssignable(numArray));
}
Also used : TypeDescriptor(org.graalvm.polyglot.tck.TypeDescriptor) Test(org.junit.Test)

Example 5 with TypeDescriptor

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

the class ErrorTypeTest method findBestApplicableArg.

private static Map.Entry<String, ? extends Snippet> findBestApplicableArg(final List<Map.Entry<String, ? extends Snippet>> applicableTypes, final Collection<? extends Snippet> overloads, final int parameterIndex) {
    final Iterator<Map.Entry<String, ? extends Snippet>> it = applicableTypes.iterator();
    final Collection<TypeDescriptor> overloadsTypes = new ArrayList<>();
    for (Snippet overload : overloads) {
        final List<? extends TypeDescriptor> params = overload.getParameterTypes();
        if (parameterIndex < params.size()) {
            overloadsTypes.add(params.get(parameterIndex));
        }
    }
    Map.Entry<String, ? extends Snippet> bestSoFar = it.next();
    while (isCoveredByOverload(bestSoFar, overloadsTypes) && it.hasNext()) {
        bestSoFar = it.next();
    }
    return bestSoFar;
}
Also used : TypeDescriptor(org.graalvm.polyglot.tck.TypeDescriptor) ArrayList(java.util.ArrayList) Snippet(org.graalvm.polyglot.tck.Snippet) HashMap(java.util.HashMap) AbstractMap(java.util.AbstractMap) Map(java.util.Map)

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