Search in sources :

Example 1 with Snippet

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

the class SLTCKLanguageProvider method createExpressions.

@Override
public Collection<? extends Snippet> createExpressions(Context context) {
    final Collection<Snippet> res = new ArrayList<>();
    final Value fnc = eval(context, String.format(PATTERN_BIN_OP_FNC, "add", "+"), "add");
    Snippet.Builder opb = Snippet.newBuilder("+", fnc, TypeDescriptor.NUMBER).parameterTypes(TypeDescriptor.NUMBER, TypeDescriptor.NUMBER);
    res.add(opb.build());
    opb = Snippet.newBuilder("+", fnc, TypeDescriptor.STRING).parameterTypes(TypeDescriptor.STRING, TypeDescriptor.ANY);
    res.add(opb.build());
    opb = Snippet.newBuilder("+", fnc, TypeDescriptor.STRING).parameterTypes(TypeDescriptor.ANY, TypeDescriptor.STRING);
    res.add(opb.build());
    res.add(createBinaryOperator(context, "-", "sub", TypeDescriptor.NUMBER, TypeDescriptor.NUMBER, TypeDescriptor.NUMBER).build());
    res.add(createBinaryOperator(context, "*", "mul", TypeDescriptor.NUMBER, TypeDescriptor.NUMBER, TypeDescriptor.NUMBER).build());
    res.add(createBinaryOperator(context, "/", "div", TypeDescriptor.NUMBER, TypeDescriptor.NUMBER, TypeDescriptor.NUMBER).resultVerifier((snippetRun) -> {
        final Value dividend = snippetRun.getParameters().get(0);
        final Value divider = snippetRun.getParameters().get(1);
        final PolyglotException exception = snippetRun.getException();
        if (dividend.isNumber() && divider.fitsInDouble() && divider.asDouble() == 0) {
            Assert.assertNotNull(exception);
        } else if (exception != null) {
            throw exception;
        } else {
            Assert.assertTrue(TypeDescriptor.NUMBER.isAssignable(TypeDescriptor.forValue(snippetRun.getResult())));
        }
    }).build());
    res.add(createBinaryOperator(context, "==", "eq", TypeDescriptor.BOOLEAN, TypeDescriptor.ANY, TypeDescriptor.ANY).build());
    res.add(createBinaryOperator(context, "!=", "neq", TypeDescriptor.BOOLEAN, TypeDescriptor.ANY, TypeDescriptor.ANY).build());
    res.add(createBinaryOperator(context, "<=", "le", TypeDescriptor.BOOLEAN, TypeDescriptor.NUMBER, TypeDescriptor.NUMBER).build());
    res.add(createBinaryOperator(context, ">=", "ge", TypeDescriptor.BOOLEAN, TypeDescriptor.NUMBER, TypeDescriptor.NUMBER).build());
    res.add(createBinaryOperator(context, "<", "l", TypeDescriptor.BOOLEAN, TypeDescriptor.NUMBER, TypeDescriptor.NUMBER).build());
    res.add(createBinaryOperator(context, ">", "g", TypeDescriptor.BOOLEAN, TypeDescriptor.NUMBER, TypeDescriptor.NUMBER).build());
    res.add(createBinaryOperator(context, "||", "or", TypeDescriptor.BOOLEAN, TypeDescriptor.BOOLEAN, TypeDescriptor.ANY).resultVerifier((snippetRun) -> {
        final Value firstParam = snippetRun.getParameters().get(0);
        final Value secondParam = snippetRun.getParameters().get(1);
        final PolyglotException exception = snippetRun.getException();
        if (firstParam.isBoolean() && !firstParam.asBoolean() && !secondParam.isBoolean()) {
            Assert.assertNotNull(exception);
        } else if (exception != null) {
            throw exception;
        } else {
            Assert.assertTrue(TypeDescriptor.BOOLEAN.isAssignable(TypeDescriptor.forValue(snippetRun.getResult())));
        }
    }).build());
    res.add(createBinaryOperator(context, "&&", "land", TypeDescriptor.BOOLEAN, TypeDescriptor.BOOLEAN, TypeDescriptor.ANY).resultVerifier((snippetRun) -> {
        final Value firstParam = snippetRun.getParameters().get(0);
        final Value secondParam = snippetRun.getParameters().get(1);
        final PolyglotException exception = snippetRun.getException();
        if (firstParam.isBoolean() && firstParam.asBoolean() && !secondParam.isBoolean()) {
            Assert.assertNotNull(exception);
        } else if (exception != null) {
            throw exception;
        } else {
            Assert.assertTrue(TypeDescriptor.BOOLEAN.isAssignable(TypeDescriptor.forValue(snippetRun.getResult())));
        }
    }).build());
    res.add(createPostfixOperator(context, "()", "callNoArg", TypeDescriptor.NULL, TypeDescriptor.executable(TypeDescriptor.ANY)).build());
    res.add(createPostfixOperator(context, "(1)", "callOneArg", TypeDescriptor.NULL, TypeDescriptor.executable(TypeDescriptor.ANY, TypeDescriptor.NUMBER)).build());
    res.add(createPostfixOperator(context, "(1, \"\")", "callTwoArgs", TypeDescriptor.NULL, TypeDescriptor.executable(TypeDescriptor.ANY, TypeDescriptor.NUMBER, TypeDescriptor.STRING)).build());
    return Collections.unmodifiableCollection(res);
}
Also used : ArrayList(java.util.ArrayList) Value(org.graalvm.polyglot.Value) Snippet(org.graalvm.polyglot.tck.Snippet) PolyglotException(org.graalvm.polyglot.PolyglotException)

Example 2 with Snippet

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

the class TestUtil method createTestRuns.

static Collection<? extends TestRun> createTestRuns(final Set<? extends String> requiredLanguages, final Set<? extends String> requiredValueLanguages, final Function<String, ? extends Collection<? extends Snippet>> snippetsProvider, final Function<String, ? extends Collection<? extends Snippet>> valuesProvider) {
    final Collection<TestRun> testRuns = new LinkedHashSet<>();
    for (String opLanguage : requiredLanguages) {
        for (Snippet operator : snippetsProvider.apply(opLanguage)) {
            for (String parLanguage : requiredValueLanguages) {
                final Collection<Map.Entry<String, ? extends Snippet>> valueConstructors = new HashSet<>();
                for (Snippet snippet : valuesProvider.apply(parLanguage)) {
                    valueConstructors.add(new AbstractMap.SimpleImmutableEntry<>(parLanguage, snippet));
                }
                final List<List<Map.Entry<String, ? extends Snippet>>> applicableParams = findApplicableParameters(operator, valueConstructors);
                boolean canBeInvoked = true;
                for (List<Map.Entry<String, ? extends Snippet>> param : applicableParams) {
                    canBeInvoked &= !param.isEmpty();
                    if (!canBeInvoked) {
                        break;
                    }
                }
                if (canBeInvoked) {
                    computeAllPermutations(new AbstractMap.SimpleImmutableEntry<>(opLanguage, operator), applicableParams, testRuns);
                }
            }
        }
    }
    return testRuns;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Snippet(org.graalvm.polyglot.tck.Snippet) AbstractMap(java.util.AbstractMap) Entry(java.util.Map.Entry) ArrayList(java.util.ArrayList) List(java.util.List) AbstractMap(java.util.AbstractMap) Map(java.util.Map) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 3 with Snippet

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

the class ErrorTypeTest method computeSnippets.

private static void computeSnippets(final String snippetLanguage, final Collection<? extends Snippet> snippets, final Map<String, Collection<? extends Snippet>> overloads, final Collection<? super TestRun> collector) {
    final Set<? extends String> requiredValueLanguages = TestUtil.getRequiredValueLanguages(context);
    for (Snippet snippet : snippets) {
        for (String parLanguage : requiredValueLanguages) {
            if (snippetLanguage.equals(parLanguage)) {
                continue;
            }
            final Collection<Map.Entry<String, ? extends Snippet>> valueConstructors = new HashSet<>();
            for (Snippet valueConstructor : context.getValueConstructors(null, parLanguage)) {
                valueConstructors.add(new AbstractMap.SimpleImmutableEntry<>(parLanguage, valueConstructor));
            }
            final List<List<Map.Entry<String, ? extends Snippet>>> applicableParams = TestUtil.findApplicableParameters(snippet, valueConstructors);
            if (!applicableParams.isEmpty()) {
                final Collection<? extends Snippet> operatorOverloads = new ArrayList<>(overloads.get(snippet.getId()));
                operatorOverloads.remove(snippet);
                computeAllInvalidPermutations(new AbstractMap.SimpleImmutableEntry<>(snippetLanguage, snippet), applicableParams, valueConstructors, operatorOverloads, collector);
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Snippet(org.graalvm.polyglot.tck.Snippet) AbstractMap(java.util.AbstractMap) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) AbstractMap(java.util.AbstractMap) Map(java.util.Map) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 4 with Snippet

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

the class ErrorTypeTest method computeAllInvalidPermutations.

private static void computeAllInvalidPermutations(final Map.Entry<String, ? extends Snippet> operator, final List<List<Map.Entry<String, ? extends Snippet>>> applicableArgs, final Collection<Map.Entry<String, ? extends Snippet>> allValueConstructors, final Collection<? extends Snippet> overloads, final Collection<? super TestRun> collector) {
    for (int i = 0; i < applicableArgs.size(); i++) {
        final Set<Map.Entry<String, ? extends Snippet>> nonApplicableArgs = new HashSet<>(allValueConstructors);
        nonApplicableArgs.removeAll(applicableArgs.get(i));
        if (!nonApplicableArgs.isEmpty()) {
            final List<List<Map.Entry<String, ? extends Snippet>>> args = new ArrayList<>(applicableArgs.size());
            boolean canBeInvoked = true;
            for (int j = 0; j < applicableArgs.size(); j++) {
                if (i == j) {
                    args.add(new ArrayList<>(nonApplicableArgs));
                } else {
                    final List<Map.Entry<String, ? extends Snippet>> slotArgs = applicableArgs.get(j);
                    if (slotArgs.isEmpty()) {
                        canBeInvoked = false;
                        break;
                    } else {
                        args.add(Collections.singletonList(findBestApplicableArg(slotArgs, overloads, j)));
                    }
                }
            }
            if (canBeInvoked) {
                final Collection<TestRun> tmp = new ArrayList<>();
                TestUtil.computeAllPermutations(operator, args, tmp);
                if (!overloads.isEmpty()) {
                    for (Iterator<TestRun> it = tmp.iterator(); it.hasNext(); ) {
                        TestRun test = it.next();
                        boolean remove = false;
                        for (Snippet overload : overloads) {
                            if (areParametersAssignable(overload.getParameterTypes(), test.gatActualParameterTypes())) {
                                remove = true;
                                break;
                            }
                        }
                        if (remove) {
                            it.remove();
                        }
                    }
                }
                collector.addAll(tmp);
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Snippet(org.graalvm.polyglot.tck.Snippet) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) AbstractMap(java.util.AbstractMap) Map(java.util.Map) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 5 with Snippet

use of org.graalvm.polyglot.tck.Snippet 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

Snippet (org.graalvm.polyglot.tck.Snippet)6 ArrayList (java.util.ArrayList)5 AbstractMap (java.util.AbstractMap)4 LinkedHashSet (java.util.LinkedHashSet)4 Map (java.util.Map)4 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 List (java.util.List)3 Entry (java.util.Map.Entry)1 PolyglotException (org.graalvm.polyglot.PolyglotException)1 Value (org.graalvm.polyglot.Value)1 TypeDescriptor (org.graalvm.polyglot.tck.TypeDescriptor)1