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);
}
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;
}
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);
}
}
}
}
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);
}
}
}
}
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;
}
Aggregations