Skip to content

Commit

Permalink
Test to verify integer operation on foreign objects
Browse files Browse the repository at this point in the history
  • Loading branch information
JaroslavTulach committed Sep 13, 2023
1 parent cc4ff63 commit 4614be2
Show file tree
Hide file tree
Showing 18 changed files with 221 additions and 152 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package org.enso.interpreter.node.expression.builtin.number.integer;

import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.node.expression.builtin.number.utils.BigIntegerOps;
import org.enso.interpreter.node.expression.builtin.number.utils.ToEnsoNumberNode;
import org.enso.interpreter.runtime.number.EnsoBigInteger;

import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.Fallback;
Expand All @@ -14,6 +9,10 @@
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import com.oracle.truffle.api.library.CachedLibrary;
import com.oracle.truffle.api.nodes.Node.Child;
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.node.expression.builtin.number.utils.BigIntegerOps;
import org.enso.interpreter.node.expression.builtin.number.utils.ToEnsoNumberNode;
import org.enso.interpreter.runtime.number.EnsoBigInteger;

@BuiltinMethod(type = "Integer", name = "+", description = "Addition of numbers.")
public abstract class AddNode extends IntegerNode {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.nodes.Node.Child;
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.runtime.number.EnsoBigInteger;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.oracle.truffle.api.dsl.Cached.Shared;
import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.nodes.Node.Child;
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.node.expression.builtin.number.utils.BigIntegerOps;
import org.enso.interpreter.runtime.number.EnsoBigInteger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.nodes.Node.Child;
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.runtime.number.EnsoBigInteger;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.nodes.Node.Child;
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.node.expression.builtin.number.utils.BigIntegerOps;
import org.enso.interpreter.runtime.number.EnsoBigInteger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.nodes.Node.Child;
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.runtime.number.EnsoBigInteger;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.nodes.Node.Child;
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.node.expression.builtin.number.utils.BigIntegerOps;
import org.enso.interpreter.runtime.EnsoContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.nodes.Node.Child;
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.node.expression.builtin.number.utils.BigIntegerOps;
import org.enso.interpreter.runtime.EnsoContext;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package org.enso.interpreter.node.expression.builtin.number.integer;

import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.error.PanicException;
import org.enso.interpreter.runtime.library.dispatch.TypesLibrary;
import org.enso.interpreter.runtime.number.EnsoBigInteger;

import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.nodes.Node;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.error.PanicException;
import org.enso.interpreter.runtime.library.dispatch.TypesLibrary;
import org.enso.interpreter.runtime.number.EnsoBigInteger;

abstract class IntegerNode extends Node {
IntegerNode() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.nodes.Node.Child;
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.node.expression.builtin.number.utils.BigIntegerOps;
import org.enso.interpreter.runtime.EnsoContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.nodes.Node.Child;
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.node.expression.builtin.number.utils.BigIntegerOps;
import org.enso.interpreter.runtime.EnsoContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.Specialization;

import com.oracle.truffle.api.nodes.Node.Child;
import java.math.BigInteger;

import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.node.expression.builtin.number.utils.BigIntegerOps;
import org.enso.interpreter.node.expression.builtin.number.utils.ToEnsoNumberNode;
import org.enso.interpreter.runtime.number.EnsoBigInteger;

import com.oracle.truffle.api.nodes.Node.Child;

@BuiltinMethod(type = "Integer", name = "^", description = "Exponentiation of numbers.")
public abstract class PowNode extends IntegerNode {
private @Child ToEnsoNumberNode toEnsoNumberNode = ToEnsoNumberNode.create();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.enso.interpreter.node.expression.builtin.number.integer;

import com.oracle.truffle.api.nodes.Node.Child;
import com.oracle.truffle.api.profiles.BranchProfile;
import com.oracle.truffle.api.profiles.CountingConditionProfile;
import com.oracle.truffle.api.profiles.PrimitiveValueProfile;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.Specialization;

import com.oracle.truffle.api.nodes.Node.Child;
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.node.expression.builtin.number.utils.BigIntegerOps;
import org.enso.interpreter.node.expression.builtin.number.utils.ToEnsoNumberNode;
import org.enso.interpreter.runtime.number.EnsoBigInteger;

import com.oracle.truffle.api.nodes.Node.Child;

@BuiltinMethod(type = "Integer", name = "-", description = "Subtraction of numbers.")
public abstract class SubtractNode extends IntegerNode {
private @Child ToEnsoNumberNode toEnsoNumberNode = ToEnsoNumberNode.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.nodes.Node.Child;
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.node.expression.builtin.number.utils.BigIntegerOps;
import org.enso.interpreter.runtime.number.EnsoBigInteger;
Expand Down
124 changes: 0 additions & 124 deletions engine/runtime/src/test/java/org/enso/interpreter/test/EqualsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,130 +199,6 @@ public void testVectorsEquality() {
});
}

@ExportLibrary(InteropLibrary.class)
static final class WrappedPrimitive implements TruffleObject {
private final Object value;

WrappedPrimitive(long value) {
this.value = value;
}

WrappedPrimitive(boolean value) {
this.value = value;
}

WrappedPrimitive(double value) {
this.value = value;
}

WrappedPrimitive(BigInteger value) {
this.value = value;
}

WrappedPrimitive(String value) {
this.value = value;
}

@ExportMessage
boolean isString() {
return value instanceof String;
}

@ExportMessage
String asString() {
return (String) value;
}

@ExportMessage
boolean isNumber() {
return value instanceof Number;
}

@ExportMessage
boolean isBoolean() {
return value instanceof Boolean;
}

@ExportMessage
boolean asBoolean() {
return (Boolean) value;
}

@ExportMessage
boolean fitsInByte() {
return false;
}

@ExportMessage
boolean fitsInShort() {
return false;
}

@ExportMessage
boolean fitsInInt() {
return false;
}

@ExportMessage
boolean fitsInLong() {
return value instanceof Long;
}

@ExportMessage
boolean fitsInFloat() {
return false;
}

@ExportMessage
boolean fitsInDouble() {
return value instanceof Double;
}

@ExportMessage
byte asByte() throws UnsupportedMessageException {
throw UnsupportedMessageException.create();
}

@ExportMessage
short asShort() throws UnsupportedMessageException {
throw UnsupportedMessageException.create();
}

@ExportMessage
int asInt() throws UnsupportedMessageException {
throw UnsupportedMessageException.create();
}

@ExportMessage
long asLong() throws UnsupportedMessageException {
return (Long) value;
}

@ExportMessage
float asFloat() throws UnsupportedMessageException {
throw UnsupportedMessageException.create();
}

@ExportMessage
double asDouble() throws UnsupportedMessageException {
return (Double) value;
}

@ExportMessage
boolean fitsInBigInteger() {
return value instanceof BigInteger;
}

@ExportMessage
BigInteger asBigInteger() throws UnsupportedMessageException {
return (BigInteger) value;
}

Object asDirect() {
return value;
}
}

@Test
public void testTruffleNumberLong() {
var ensoNumber = unwrapValue(context, createValue(context, "1", ""));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package org.enso.interpreter.test;

import java.util.Random;

import org.enso.polyglot.MethodNames;
import org.graalvm.polyglot.Context;
import org.junit.AfterClass;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.junit.BeforeClass;
import org.junit.experimental.theories.DataPoints;
import org.junit.experimental.theories.FromDataPoints;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.runner.RunWith;

@RunWith(Theories.class)
public class NumbersTest extends TestBase {

@DataPoints("operation")
public static final String[] OPERATION = {
" +", " -", " ^", " *", " %", " <=", " <", " >=", " >", " /",
".div", ".bit_xor", ".bit_shift", ".bit_shift_r", ".bit_or", ".bit_and"
};
private static Context ctx;

@BeforeClass
public static void initContext() {
ctx = createDefaultContext();
}

@AfterClass
public static void closeContext() {
ctx.close();
}

@Theory
public void verifyOperationOnForeignObject(
@FromDataPoints("operation") String operation
) {
executeInContext(ctx, () -> {
var code = """
fn a b = a{op} b
""".replace("{op}", operation);
var fn = ctx.eval("enso", code).invokeMember(MethodNames.Module.EVAL_EXPRESSION, "fn");

var r = new Random();
var n1 = r.nextInt();
var n2 = r.nextInt();
System.out.println(n1 + operation + " " + n2);

var r1 = fn.execute(n1, n2);

var wrap2 = ctx.asValue(new WrappedPrimitive(n2));
var r2 = fn.execute(n1, wrap2);

assertEquals("r1: " + r1 + " r2: " + r2, r1.isBoolean(), r2.isBoolean());
assertEquals("r1: " + r1 + " r2: " + r2, r1.fitsInLong(), r2.fitsInLong());
assertEquals("r1: " + r1 + " r2: " + r2, r1.fitsInDouble(), r2.fitsInDouble());
assertEquals("r1: " + r1 + " r2: " + r2, r1.fitsInBigInteger(), r2.fitsInBigInteger());

if (r1.fitsInLong()) {
assertEquals("Results for " + n1 + operation + " " + n2, r1.asLong(), r2.asLong());
} else if (r1.fitsInDouble()) {
assertEquals("Results for " + n1 + operation + " " + n2, r1.asDouble(), r2.asDouble(), 0.1);
} else if (r1.fitsInBigInteger()) {
assertEquals("Results for " + n1 + operation + " " + n2, r1.asBigInteger(), r2.asBigInteger());
} else if (r1.isBoolean()) {
assertEquals("Results for " + n1 + operation + " " + n2, r1.asBoolean(), r2.asBoolean());
} else {
fail("Doesn't fit: " + r1);
}

return null;
});
}
}
Loading

0 comments on commit 4614be2

Please sign in to comment.